|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
4
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
5
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
6
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
7
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
8
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
9
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
10
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
11
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
12
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
13
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
14
|
|
|
* |
|
15
|
|
|
* This software consists of voluntary contributions made by many individuals |
|
16
|
|
|
* and is licensed under the LGPL. For more information, see |
|
17
|
|
|
* <http://www.doctrine-project.org>. |
|
18
|
|
|
*/ |
|
19
|
|
|
|
|
20
|
|
|
namespace Doctrine\DBAL\Migrations; |
|
21
|
|
|
|
|
22
|
|
|
use Doctrine\DBAL\Exception\InvalidArgumentException; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @deprecated |
|
26
|
|
|
* |
|
27
|
|
|
* @see \Doctrine\DBAL\Migrations\FileQueryWriter |
|
28
|
|
|
*/ |
|
29
|
|
|
class SqlFileWriter |
|
30
|
|
|
{ |
|
31
|
|
|
private $migrationsColumnName; |
|
32
|
|
|
|
|
33
|
|
|
private $migrationsTableName; |
|
34
|
|
|
|
|
35
|
|
|
private $destPath; |
|
36
|
|
|
|
|
37
|
|
|
/** @var null|OutputWriter */ |
|
38
|
|
|
private $outputWriter; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param string $migrationsColumnName |
|
42
|
|
|
* @param string $migrationsTableName |
|
43
|
|
|
* @param string $destPath |
|
44
|
|
|
* @param \Doctrine\DBAL\Migrations\OutputWriter $outputWriter |
|
45
|
|
|
*/ |
|
46
|
18 |
|
public function __construct( |
|
47
|
|
|
$migrationsColumnName, |
|
48
|
|
|
$migrationsTableName, |
|
49
|
|
|
$destPath, |
|
50
|
|
|
OutputWriter $outputWriter = null |
|
51
|
|
|
) { |
|
52
|
18 |
|
if (empty($migrationsColumnName)) { |
|
53
|
1 |
|
$this->throwInvalidArgumentException('Migrations column name cannot be empty.'); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
17 |
|
if (empty($migrationsTableName)) { |
|
57
|
2 |
|
$this->throwInvalidArgumentException('Migrations table name cannot be empty.'); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
15 |
|
if (empty($destPath)) { |
|
61
|
|
|
$this->throwInvalidArgumentException('Destination file must be specified.'); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
15 |
|
$this->migrationsColumnName = $migrationsColumnName; |
|
65
|
15 |
|
$this->migrationsTableName = $migrationsTableName; |
|
66
|
15 |
|
$this->destPath = $destPath; |
|
67
|
15 |
|
$this->outputWriter = $outputWriter; |
|
68
|
15 |
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @param array $queriesByVersion array Keys are versions and values are arrays of SQL queries (they must be castable to string) |
|
72
|
|
|
* @param string $direction |
|
73
|
|
|
* @return int|bool |
|
74
|
|
|
*/ |
|
75
|
14 |
|
public function write(array $queriesByVersion, $direction) |
|
76
|
|
|
{ |
|
77
|
14 |
|
$path = $this->buildMigrationFilePath(); |
|
78
|
14 |
|
$string = $this->buildMigrationFile($queriesByVersion, $direction); |
|
79
|
|
|
|
|
80
|
14 |
|
if ($this->outputWriter) { |
|
81
|
8 |
|
$this->outputWriter->write("\n" . sprintf('Writing migration file to "<info>%s</info>"', $path)); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
14 |
|
return file_put_contents($path, $string); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
14 |
|
private function buildMigrationFile(array $queriesByVersion, string $direction) : string |
|
88
|
|
|
{ |
|
89
|
14 |
|
$string = sprintf("-- Doctrine Migration File Generated on %s\n", date('Y-m-d H:i:s')); |
|
90
|
|
|
|
|
91
|
14 |
|
foreach ($queriesByVersion as $version => $queries) { |
|
92
|
14 |
|
$string .= "\n-- Version " . $version . "\n"; |
|
93
|
|
|
|
|
94
|
14 |
|
foreach ($queries as $query) { |
|
95
|
14 |
|
$string .= $query . ";\n"; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
|
|
99
|
14 |
|
$string .= $this->getVersionUpdateQuery($version, $direction); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
14 |
|
return $string; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
14 |
|
private function getVersionUpdateQuery(string $version, string $direction) : string |
|
106
|
|
|
{ |
|
107
|
14 |
|
if ($direction === Version::DIRECTION_DOWN) { |
|
108
|
6 |
|
$query = "DELETE FROM %s WHERE %s = '%s';\n"; |
|
109
|
|
|
} else { |
|
110
|
8 |
|
$query = "INSERT INTO %s (%s) VALUES ('%s');\n"; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
14 |
|
return sprintf($query, $this->migrationsTableName, $this->migrationsColumnName, $version); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
14 |
|
private function buildMigrationFilePath() : string |
|
117
|
|
|
{ |
|
118
|
14 |
|
$path = $this->destPath; |
|
119
|
|
|
|
|
120
|
14 |
|
if (is_dir($path)) { |
|
121
|
10 |
|
$path = realpath($path); |
|
122
|
10 |
|
$path = $path . '/doctrine_migration_' . date('YmdHis') . '.sql'; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
14 |
|
return $path; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
3 |
|
protected function throwInvalidArgumentException($message) |
|
129
|
|
|
{ |
|
130
|
3 |
|
throw new InvalidArgumentException($message); |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|