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\DBALException; |
23
|
|
|
use Doctrine\DBAL\Exception\InvalidArgumentException; |
24
|
|
|
|
25
|
|
|
class SqlFileWriter |
26
|
|
|
{ |
27
|
|
|
private $migrationsColumnName; |
28
|
|
|
|
29
|
|
|
private $migrationsTableName; |
30
|
|
|
|
31
|
|
|
private $destPath; |
32
|
|
|
|
33
|
|
|
/** @var null|OutputWriter */ |
34
|
|
|
private $outputWriter; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param string $migrationsColumnName |
38
|
|
|
* @param string $migrationsTableName |
39
|
|
|
* @param string $destPath |
40
|
|
|
* @param \Doctrine\DBAL\Migrations\OutputWriter $outputWriter |
41
|
|
|
*/ |
42
|
14 |
|
public function __construct($migrationsColumnName, $migrationsTableName, $destPath, OutputWriter $outputWriter = null) |
43
|
|
|
{ |
44
|
14 |
|
if (empty($migrationsColumnName)) { |
45
|
1 |
|
$this->throwInvalidArgumentException('Migrations column name cannot be empty.'); |
46
|
|
|
} |
47
|
13 |
|
$this->migrationsColumnName = $migrationsColumnName; |
48
|
13 |
|
if (empty($migrationsTableName)) { |
49
|
2 |
|
$this->throwInvalidArgumentException('Migrations table name cannot be empty.'); |
50
|
|
|
} |
51
|
11 |
|
$this->migrationsTableName = $migrationsTableName; |
52
|
|
|
|
53
|
11 |
|
if (empty($destPath)) { |
54
|
|
|
$this->throwInvalidArgumentException('Destination file must be specified.'); |
55
|
|
|
} |
56
|
11 |
|
$this->destPath = $destPath; |
57
|
|
|
|
58
|
11 |
|
$this->outputWriter = $outputWriter; |
59
|
11 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param array $queriesByVersion array Keys are versions and values are arrays of SQL queries (they must be castable to string) |
63
|
|
|
* @param string $direction |
64
|
|
|
* @return int|bool |
65
|
|
|
*/ |
66
|
10 |
|
public function write(array $queriesByVersion, $direction) |
67
|
|
|
{ |
68
|
10 |
|
$path = $this->buildMigrationFilePath(); |
69
|
10 |
|
$string = $this->buildMigrationFile($queriesByVersion, $direction); |
70
|
|
|
|
71
|
10 |
|
if ($this->outputWriter) { |
72
|
8 |
|
$this->outputWriter->write("\n" . sprintf('Writing migration file to "<info>%s</info>"', $path)); |
73
|
|
|
} |
74
|
|
|
|
75
|
10 |
|
return file_put_contents($path, $string); |
76
|
|
|
} |
77
|
|
|
|
78
|
10 |
|
private function buildMigrationFile(array $queriesByVersion, $direction) |
79
|
|
|
{ |
80
|
10 |
|
$string = sprintf("-- Doctrine Migration File Generated on %s\n", date('Y-m-d H:i:s')); |
81
|
|
|
|
82
|
10 |
|
foreach ($queriesByVersion as $version => $queries) { |
83
|
10 |
|
$string .= "\n-- Version " . $version . "\n"; |
84
|
10 |
|
foreach ($queries as $query) { |
85
|
10 |
|
$string .= $query . ";\n"; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
|
89
|
10 |
|
$string .= $this->getVersionUpdateQuery($version, $direction); |
90
|
|
|
} |
91
|
|
|
|
92
|
10 |
|
return $string; |
93
|
|
|
} |
94
|
|
|
|
95
|
10 |
|
private function getVersionUpdateQuery($version, $direction) |
96
|
|
|
{ |
97
|
10 |
|
if ($direction == Version::DIRECTION_DOWN) { |
98
|
4 |
|
$query = "DELETE FROM %s WHERE %s = '%s';\n"; |
99
|
|
|
} else { |
100
|
6 |
|
$query = "INSERT INTO %s (%s) VALUES ('%s');\n"; |
101
|
|
|
} |
102
|
|
|
|
103
|
10 |
|
return sprintf($query, $this->migrationsTableName, $this->migrationsColumnName, $version); |
104
|
|
|
} |
105
|
|
|
|
106
|
10 |
|
private function buildMigrationFilePath() |
107
|
|
|
{ |
108
|
10 |
|
$path = $this->destPath; |
109
|
10 |
|
if (is_dir($path)) { |
110
|
6 |
|
$path = realpath($path); |
111
|
6 |
|
$path = $path . '/doctrine_migration_' . date('YmdHis') . '.sql'; |
112
|
|
|
} |
113
|
|
|
|
114
|
10 |
|
return $path; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* This only exists for backwards-compatibiliy with DBAL 2.4 |
119
|
|
|
*/ |
120
|
3 |
|
protected function throwInvalidArgumentException($message) |
121
|
|
|
{ |
122
|
3 |
|
if (class_exists('Doctrine\DBAL\Exception\InvalidArgumentException')) { |
123
|
3 |
|
throw new InvalidArgumentException($message); |
124
|
|
|
} else { |
125
|
|
|
throw new DBALException($message); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|