Skip to content

Commit 813ba6d

Browse files
committed
Use instanceof for driver type checking and extract helper method
- Replace string operations on class names with instanceof checks - Extract runMigrationsAndDispatch() to reduce code duplication
1 parent 415523f commit 813ba6d

1 file changed

Lines changed: 26 additions & 16 deletions

File tree

src/Command/ResetCommand.php

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
use Cake\Console\ConsoleIo;
1919
use Cake\Console\ConsoleOptionParser;
2020
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;
2125
use Cake\Datasource\ConnectionManager;
2226
use Cake\Event\EventDispatcherTrait;
2327
use Migrations\Config\ConfigInterface;
@@ -118,10 +122,7 @@ public function execute(Arguments $args, ConsoleIo $io): ?int
118122
$io->out('');
119123
$io->out('Running migrations...');
120124

121-
$result = $this->runMigrations($args, $io);
122-
$this->dispatchEvent('Migration.afterReset');
123-
124-
return $result;
125+
return $this->runMigrationsAndDispatch($args, $io);
125126
}
126127

127128
// Show what will be dropped
@@ -157,10 +158,7 @@ public function execute(Arguments $args, ConsoleIo $io): ?int
157158

158159
// Re-run migrations
159160
if (!$dryRun) {
160-
$result = $this->runMigrations($args, $io);
161-
$this->dispatchEvent('Migration.afterReset');
162-
163-
return $result;
161+
return $this->runMigrationsAndDispatch($args, $io);
164162
}
165163

166164
$io->info('DRY-RUN: Would re-run all migrations.');
@@ -192,17 +190,16 @@ protected function getTablesToDrop(Connection $connection): array
192190
protected function dropTables(Connection $connection, array $tables, ConsoleIo $io): void
193191
{
194192
$driver = $connection->getDriver();
195-
$driverClass = get_class($driver);
196193

197194
// For PostgreSQL and SQL Server, we need to drop foreign keys first
198195
// or use CASCADE in the drop statement
199-
if (str_contains($driverClass, 'Postgres')) {
196+
if ($driver instanceof Postgres) {
200197
foreach ($tables as $table) {
201198
$quotedTable = $driver->quoteIdentifier($table);
202199
$io->verbose("Dropping table: {$table}");
203200
$connection->execute("DROP TABLE IF EXISTS {$quotedTable} CASCADE");
204201
}
205-
} elseif (str_contains($driverClass, 'Sqlserver')) {
202+
} elseif ($driver instanceof Sqlserver) {
206203
// Drop all foreign key constraints first
207204
$this->dropForeignKeyConstraints($connection, $tables, $io);
208205

@@ -241,9 +238,8 @@ protected function dropTables(Connection $connection, array $tables, ConsoleIo $
241238
protected function dropForeignKeyConstraints(Connection $connection, array $tables, ConsoleIo $io): void
242239
{
243240
$driver = $connection->getDriver();
244-
$driverClass = get_class($driver);
245241

246-
if (!str_contains($driverClass, 'Sqlserver')) {
242+
if (!$driver instanceof Sqlserver) {
247243
return;
248244
}
249245

@@ -276,15 +272,29 @@ protected function dropForeignKeyConstraints(Connection $connection, array $tabl
276272
protected function setForeignKeyChecks(Connection $connection, bool $enable): void
277273
{
278274
$driver = $connection->getDriver();
279-
$driverClass = get_class($driver);
280275

281-
if (str_contains($driverClass, 'Mysql')) {
276+
if ($driver instanceof Mysql) {
282277
$connection->execute('SET FOREIGN_KEY_CHECKS = ' . ($enable ? '1' : '0'));
283-
} elseif (str_contains($driverClass, 'Sqlite')) {
278+
} elseif ($driver instanceof Sqlite) {
284279
$connection->execute('PRAGMA foreign_keys = ' . ($enable ? 'ON' : 'OFF'));
285280
}
286281
}
287282

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+
288298
/**
289299
* Run migrations.
290300
*

0 commit comments

Comments
 (0)