|
18 | 18 | use Cake\Console\ConsoleIo; |
19 | 19 | use Cake\Console\ConsoleOptionParser; |
20 | 20 | use Cake\Database\Connection; |
| 21 | +use Cake\Database\Driver\Mysql; |
| 22 | +use Cake\Database\Driver\Postgres; |
| 23 | +use Cake\Database\Driver\Sqlite; |
| 24 | +use Cake\Database\Driver\Sqlserver; |
21 | 25 | use Cake\Datasource\ConnectionManager; |
22 | 26 | use Cake\Event\EventDispatcherTrait; |
23 | 27 | use Migrations\Config\ConfigInterface; |
@@ -118,10 +122,7 @@ public function execute(Arguments $args, ConsoleIo $io): ?int |
118 | 122 | $io->out(''); |
119 | 123 | $io->out('Running migrations...'); |
120 | 124 |
|
121 | | - $result = $this->runMigrations($args, $io); |
122 | | - $this->dispatchEvent('Migration.afterReset'); |
123 | | - |
124 | | - return $result; |
| 125 | + return $this->runMigrationsAndDispatch($args, $io); |
125 | 126 | } |
126 | 127 |
|
127 | 128 | // Show what will be dropped |
@@ -157,10 +158,7 @@ public function execute(Arguments $args, ConsoleIo $io): ?int |
157 | 158 |
|
158 | 159 | // Re-run migrations |
159 | 160 | if (!$dryRun) { |
160 | | - $result = $this->runMigrations($args, $io); |
161 | | - $this->dispatchEvent('Migration.afterReset'); |
162 | | - |
163 | | - return $result; |
| 161 | + return $this->runMigrationsAndDispatch($args, $io); |
164 | 162 | } |
165 | 163 |
|
166 | 164 | $io->info('DRY-RUN: Would re-run all migrations.'); |
@@ -192,17 +190,16 @@ protected function getTablesToDrop(Connection $connection): array |
192 | 190 | protected function dropTables(Connection $connection, array $tables, ConsoleIo $io): void |
193 | 191 | { |
194 | 192 | $driver = $connection->getDriver(); |
195 | | - $driverClass = get_class($driver); |
196 | 193 |
|
197 | 194 | // For PostgreSQL and SQL Server, we need to drop foreign keys first |
198 | 195 | // or use CASCADE in the drop statement |
199 | | - if (str_contains($driverClass, 'Postgres')) { |
| 196 | + if ($driver instanceof Postgres) { |
200 | 197 | foreach ($tables as $table) { |
201 | 198 | $quotedTable = $driver->quoteIdentifier($table); |
202 | 199 | $io->verbose("Dropping table: {$table}"); |
203 | 200 | $connection->execute("DROP TABLE IF EXISTS {$quotedTable} CASCADE"); |
204 | 201 | } |
205 | | - } elseif (str_contains($driverClass, 'Sqlserver')) { |
| 202 | + } elseif ($driver instanceof Sqlserver) { |
206 | 203 | // Drop all foreign key constraints first |
207 | 204 | $this->dropForeignKeyConstraints($connection, $tables, $io); |
208 | 205 |
|
@@ -241,9 +238,8 @@ protected function dropTables(Connection $connection, array $tables, ConsoleIo $ |
241 | 238 | protected function dropForeignKeyConstraints(Connection $connection, array $tables, ConsoleIo $io): void |
242 | 239 | { |
243 | 240 | $driver = $connection->getDriver(); |
244 | | - $driverClass = get_class($driver); |
245 | 241 |
|
246 | | - if (!str_contains($driverClass, 'Sqlserver')) { |
| 242 | + if (!$driver instanceof Sqlserver) { |
247 | 243 | return; |
248 | 244 | } |
249 | 245 |
|
@@ -276,15 +272,29 @@ protected function dropForeignKeyConstraints(Connection $connection, array $tabl |
276 | 272 | protected function setForeignKeyChecks(Connection $connection, bool $enable): void |
277 | 273 | { |
278 | 274 | $driver = $connection->getDriver(); |
279 | | - $driverClass = get_class($driver); |
280 | 275 |
|
281 | | - if (str_contains($driverClass, 'Mysql')) { |
| 276 | + if ($driver instanceof Mysql) { |
282 | 277 | $connection->execute('SET FOREIGN_KEY_CHECKS = ' . ($enable ? '1' : '0')); |
283 | | - } elseif (str_contains($driverClass, 'Sqlite')) { |
| 278 | + } elseif ($driver instanceof Sqlite) { |
284 | 279 | $connection->execute('PRAGMA foreign_keys = ' . ($enable ? 'ON' : 'OFF')); |
285 | 280 | } |
286 | 281 | } |
287 | 282 |
|
| 283 | + /** |
| 284 | + * Run migrations and dispatch afterReset event. |
| 285 | + * |
| 286 | + * @param \Cake\Console\Arguments $args The command arguments. |
| 287 | + * @param \Cake\Console\ConsoleIo $io The console io |
| 288 | + * @return int|null The exit code |
| 289 | + */ |
| 290 | + protected function runMigrationsAndDispatch(Arguments $args, ConsoleIo $io): ?int |
| 291 | + { |
| 292 | + $result = $this->runMigrations($args, $io); |
| 293 | + $this->dispatchEvent('Migration.afterReset'); |
| 294 | + |
| 295 | + return $result; |
| 296 | + } |
| 297 | + |
288 | 298 | /** |
289 | 299 | * Run migrations. |
290 | 300 | * |
|
0 commit comments