1 | <?php |
||
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) |
|
128 | } |
||
129 |