@@ -36,170 +36,170 @@ |
||
36 | 36 | |
37 | 37 | class GenerateFromSchemaFileCommand extends GenerateCommand { |
38 | 38 | |
39 | - /** @var IConfig */ |
|
40 | - protected $config; |
|
39 | + /** @var IConfig */ |
|
40 | + protected $config; |
|
41 | 41 | |
42 | - public function __construct(IConfig $config, IAppManager $appManager, IDBConnection $connection) { |
|
43 | - parent::__construct($connection, $appManager); |
|
44 | - $this->config = $config; |
|
45 | - } |
|
42 | + public function __construct(IConfig $config, IAppManager $appManager, IDBConnection $connection) { |
|
43 | + parent::__construct($connection, $appManager); |
|
44 | + $this->config = $config; |
|
45 | + } |
|
46 | 46 | |
47 | 47 | |
48 | - protected function configure() { |
|
49 | - parent::configure(); |
|
48 | + protected function configure() { |
|
49 | + parent::configure(); |
|
50 | 50 | |
51 | - $this->setName('migrations:generate-from-schema'); |
|
52 | - } |
|
51 | + $this->setName('migrations:generate-from-schema'); |
|
52 | + } |
|
53 | 53 | |
54 | - public function execute(InputInterface $input, OutputInterface $output) { |
|
55 | - $appName = $input->getArgument('app'); |
|
56 | - $version = $input->getArgument('version'); |
|
54 | + public function execute(InputInterface $input, OutputInterface $output) { |
|
55 | + $appName = $input->getArgument('app'); |
|
56 | + $version = $input->getArgument('version'); |
|
57 | 57 | |
58 | - if (!preg_match('/^\d{1,16}$/',$version)) { |
|
59 | - $output->writeln('<error>The given version is invalid. Only 0-9 are allowed (max. 16 digits)</error>'); |
|
60 | - return 1; |
|
61 | - } |
|
58 | + if (!preg_match('/^\d{1,16}$/',$version)) { |
|
59 | + $output->writeln('<error>The given version is invalid. Only 0-9 are allowed (max. 16 digits)</error>'); |
|
60 | + return 1; |
|
61 | + } |
|
62 | 62 | |
63 | - $schemaFile = $this->appManager->getAppPath($appName) . '/appinfo/database.xml'; |
|
64 | - if (!file_exists($schemaFile)) { |
|
65 | - $output->writeln('<error>App ' . $appName . ' does not have a database.xml file</error>'); |
|
66 | - return 2; |
|
67 | - } |
|
63 | + $schemaFile = $this->appManager->getAppPath($appName) . '/appinfo/database.xml'; |
|
64 | + if (!file_exists($schemaFile)) { |
|
65 | + $output->writeln('<error>App ' . $appName . ' does not have a database.xml file</error>'); |
|
66 | + return 2; |
|
67 | + } |
|
68 | 68 | |
69 | - $reader = new MDB2SchemaReader($this->config, $this->connection->getDatabasePlatform()); |
|
70 | - $schema = new Schema(); |
|
71 | - $reader->loadSchemaFromFile($schemaFile, $schema); |
|
69 | + $reader = new MDB2SchemaReader($this->config, $this->connection->getDatabasePlatform()); |
|
70 | + $schema = new Schema(); |
|
71 | + $reader->loadSchemaFromFile($schemaFile, $schema); |
|
72 | 72 | |
73 | - $schemaBody = $this->schemaToMigration($schema); |
|
73 | + $schemaBody = $this->schemaToMigration($schema); |
|
74 | 74 | |
75 | - $ms = new MigrationService($appName, $this->connection, new ConsoleOutput($output)); |
|
75 | + $ms = new MigrationService($appName, $this->connection, new ConsoleOutput($output)); |
|
76 | 76 | |
77 | - $date = date('YmdHis'); |
|
78 | - $path = $this->generateMigration($ms, 'Version' . $version . 'Date' . $date, $schemaBody); |
|
77 | + $date = date('YmdHis'); |
|
78 | + $path = $this->generateMigration($ms, 'Version' . $version . 'Date' . $date, $schemaBody); |
|
79 | 79 | |
80 | - $output->writeln("New migration class has been generated to <info>$path</info>"); |
|
81 | - return 0; |
|
82 | - } |
|
80 | + $output->writeln("New migration class has been generated to <info>$path</info>"); |
|
81 | + return 0; |
|
82 | + } |
|
83 | 83 | |
84 | - /** |
|
85 | - * @param Schema $schema |
|
86 | - * @return string |
|
87 | - */ |
|
88 | - protected function schemaToMigration(Schema $schema) { |
|
89 | - $content = <<<'EOT' |
|
84 | + /** |
|
85 | + * @param Schema $schema |
|
86 | + * @return string |
|
87 | + */ |
|
88 | + protected function schemaToMigration(Schema $schema) { |
|
89 | + $content = <<<'EOT' |
|
90 | 90 | /** @var ISchemaWrapper $schema */ |
91 | 91 | $schema = $schemaClosure(); |
92 | 92 | |
93 | 93 | EOT; |
94 | 94 | |
95 | - foreach ($schema->getTables() as $table) { |
|
96 | - $content .= str_replace('{{table-name}}', substr($table->getName(), 3), <<<'EOT' |
|
95 | + foreach ($schema->getTables() as $table) { |
|
96 | + $content .= str_replace('{{table-name}}', substr($table->getName(), 3), <<<'EOT' |
|
97 | 97 | |
98 | 98 | if (!$schema->hasTable('{{table-name}}')) { |
99 | 99 | $table = $schema->createTable('{{table-name}}'); |
100 | 100 | |
101 | 101 | EOT |
102 | - ); |
|
102 | + ); |
|
103 | 103 | |
104 | - foreach ($table->getColumns() as $column) { |
|
105 | - $content .= str_replace(['{{name}}', '{{type}}'], [$column->getName(), $column->getType()->getName()], <<<'EOT' |
|
104 | + foreach ($table->getColumns() as $column) { |
|
105 | + $content .= str_replace(['{{name}}', '{{type}}'], [$column->getName(), $column->getType()->getName()], <<<'EOT' |
|
106 | 106 | $table->addColumn('{{name}}', '{{type}}', [ |
107 | 107 | |
108 | 108 | EOT |
109 | - ); |
|
110 | - if ($column->getAutoincrement()) { |
|
111 | - $content .= <<<'EOT' |
|
109 | + ); |
|
110 | + if ($column->getAutoincrement()) { |
|
111 | + $content .= <<<'EOT' |
|
112 | 112 | 'autoincrement' => true, |
113 | 113 | |
114 | 114 | EOT; |
115 | - } |
|
116 | - $content .= str_replace('{{notnull}}', $column->getNotnull() ? 'true' : 'false', <<<'EOT' |
|
115 | + } |
|
116 | + $content .= str_replace('{{notnull}}', $column->getNotnull() ? 'true' : 'false', <<<'EOT' |
|
117 | 117 | 'notnull' => {{notnull}}, |
118 | 118 | |
119 | 119 | EOT |
120 | - ); |
|
121 | - if ($column->getLength() !== null) { |
|
122 | - $content .= str_replace('{{length}}', $column->getLength(), <<<'EOT' |
|
120 | + ); |
|
121 | + if ($column->getLength() !== null) { |
|
122 | + $content .= str_replace('{{length}}', $column->getLength(), <<<'EOT' |
|
123 | 123 | 'length' => {{length}}, |
124 | 124 | |
125 | 125 | EOT |
126 | - ); |
|
127 | - } |
|
128 | - $default = $column->getDefault(); |
|
129 | - if ($default !== null) { |
|
130 | - if (is_string($default)) { |
|
131 | - $default = "'$default'"; |
|
132 | - } else if (is_bool($default)) { |
|
133 | - $default = ($default === true) ? 'true' : 'false'; |
|
134 | - } |
|
135 | - $content .= str_replace('{{default}}', $default, <<<'EOT' |
|
126 | + ); |
|
127 | + } |
|
128 | + $default = $column->getDefault(); |
|
129 | + if ($default !== null) { |
|
130 | + if (is_string($default)) { |
|
131 | + $default = "'$default'"; |
|
132 | + } else if (is_bool($default)) { |
|
133 | + $default = ($default === true) ? 'true' : 'false'; |
|
134 | + } |
|
135 | + $content .= str_replace('{{default}}', $default, <<<'EOT' |
|
136 | 136 | 'default' => {{default}}, |
137 | 137 | |
138 | 138 | EOT |
139 | - ); |
|
140 | - } |
|
141 | - if ($column->getUnsigned()) { |
|
142 | - $content .= <<<'EOT' |
|
139 | + ); |
|
140 | + } |
|
141 | + if ($column->getUnsigned()) { |
|
142 | + $content .= <<<'EOT' |
|
143 | 143 | 'unsigned' => true, |
144 | 144 | |
145 | 145 | EOT; |
146 | - } |
|
146 | + } |
|
147 | 147 | |
148 | - $content .= <<<'EOT' |
|
148 | + $content .= <<<'EOT' |
|
149 | 149 | ]); |
150 | 150 | |
151 | 151 | EOT; |
152 | - } |
|
152 | + } |
|
153 | 153 | |
154 | - $content .= <<<'EOT' |
|
154 | + $content .= <<<'EOT' |
|
155 | 155 | |
156 | 156 | EOT; |
157 | 157 | |
158 | - $primaryKey = $table->getPrimaryKey(); |
|
159 | - if ($primaryKey !== null) { |
|
160 | - $content .= str_replace('{{columns}}', implode('\', \'', $primaryKey->getUnquotedColumns()), <<<'EOT' |
|
158 | + $primaryKey = $table->getPrimaryKey(); |
|
159 | + if ($primaryKey !== null) { |
|
160 | + $content .= str_replace('{{columns}}', implode('\', \'', $primaryKey->getUnquotedColumns()), <<<'EOT' |
|
161 | 161 | $table->setPrimaryKey(['{{columns}}']); |
162 | 162 | |
163 | 163 | EOT |
164 | - ); |
|
165 | - } |
|
166 | - |
|
167 | - foreach ($table->getIndexes() as $index) { |
|
168 | - if ($index->isPrimary()) { |
|
169 | - continue; |
|
170 | - } |
|
171 | - |
|
172 | - if ($index->isUnique()) { |
|
173 | - $content .= str_replace( |
|
174 | - ['{{columns}}', '{{name}}'], |
|
175 | - [implode('\', \'', $index->getUnquotedColumns()), $index->getName()], |
|
176 | - <<<'EOT' |
|
164 | + ); |
|
165 | + } |
|
166 | + |
|
167 | + foreach ($table->getIndexes() as $index) { |
|
168 | + if ($index->isPrimary()) { |
|
169 | + continue; |
|
170 | + } |
|
171 | + |
|
172 | + if ($index->isUnique()) { |
|
173 | + $content .= str_replace( |
|
174 | + ['{{columns}}', '{{name}}'], |
|
175 | + [implode('\', \'', $index->getUnquotedColumns()), $index->getName()], |
|
176 | + <<<'EOT' |
|
177 | 177 | $table->addUniqueIndex(['{{columns}}'], '{{name}}'); |
178 | 178 | |
179 | 179 | EOT |
180 | - ); |
|
181 | - } else { |
|
182 | - $content .= str_replace( |
|
183 | - ['{{columns}}', '{{name}}'], |
|
184 | - [implode('\', \'', $index->getUnquotedColumns()), $index->getName()], |
|
185 | - <<<'EOT' |
|
180 | + ); |
|
181 | + } else { |
|
182 | + $content .= str_replace( |
|
183 | + ['{{columns}}', '{{name}}'], |
|
184 | + [implode('\', \'', $index->getUnquotedColumns()), $index->getName()], |
|
185 | + <<<'EOT' |
|
186 | 186 | $table->addIndex(['{{columns}}'], '{{name}}'); |
187 | 187 | |
188 | 188 | EOT |
189 | - ); |
|
190 | - } |
|
191 | - } |
|
189 | + ); |
|
190 | + } |
|
191 | + } |
|
192 | 192 | |
193 | - $content .= <<<'EOT' |
|
193 | + $content .= <<<'EOT' |
|
194 | 194 | } |
195 | 195 | |
196 | 196 | EOT; |
197 | - } |
|
197 | + } |
|
198 | 198 | |
199 | - $content .= <<<'EOT' |
|
199 | + $content .= <<<'EOT' |
|
200 | 200 | return $schema; |
201 | 201 | EOT; |
202 | 202 | |
203 | - return $content; |
|
204 | - } |
|
203 | + return $content; |
|
204 | + } |
|
205 | 205 | } |