Skip to content

Commit 465ff59

Browse files
committed
Improve reset command logic
- Remove sessions from protected tables (true nuclear option) - Rename protectedTables to trackingTables for clarity - Clear seed records (cake_seeds) along with migration records - Rename clearMigrationRecords to clearTrackingRecords
1 parent c6367c4 commit 465ff59

1 file changed

Lines changed: 20 additions & 10 deletions

File tree

src/Command/ResetCommand.php

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,17 @@ class ResetCommand extends Command
3737
use EventDispatcherTrait;
3838

3939
/**
40-
* Tables that should never be dropped.
40+
* Migration/seed tracking tables that should not be dropped.
41+
*
42+
* These tables are kept (structure preserved) but their contents
43+
* are cleared so migrations can run fresh.
4144
*
4245
* @var array<string>
4346
*/
44-
protected array $protectedTables = [
47+
protected array $trackingTables = [
4548
'cake_migrations',
4649
'cake_seeds',
4750
'phinxlog',
48-
'sessions',
4951
];
5052

5153
/**
@@ -163,7 +165,7 @@ public function execute(Arguments $args, ConsoleIo $io): ?int
163165
$this->dropTables($connection, $tablesToDrop, $io);
164166
/** @var string|null $plugin */
165167
$plugin = $args->getOption('plugin');
166-
$this->clearMigrationRecords($connection, $plugin, $io);
168+
$this->clearTrackingRecords($connection, $plugin, $io);
167169
} else {
168170
$io->info('DRY-RUN: Would drop ' . count($tablesToDrop) . ' table(s).');
169171
}
@@ -194,11 +196,11 @@ protected function getTablesToDrop(Connection $connection): array
194196
$schema = $connection->getDriver()->schemaDialect();
195197
$tables = $schema->listTables();
196198

197-
// Filter out protected tables
199+
// Filter out migration/seed tracking tables
198200
$tablesToDrop = [];
199201
foreach ($tables as $table) {
200-
// Skip migration tracking tables
201-
if (in_array($table, $this->protectedTables, true)) {
202+
// Skip migration/seed tracking tables (we clear their contents instead)
203+
if (in_array($table, $this->trackingTables, true)) {
202204
continue;
203205
}
204206
// Skip plugin phinxlog tables
@@ -316,18 +318,18 @@ protected function setForeignKeyChecks(Connection $connection, bool $enable): vo
316318
}
317319

318320
/**
319-
* Clear migration records from the tracking table.
321+
* Clear migration and seed records from tracking tables.
320322
*
321323
* @param \Cake\Database\Connection $connection Database connection
322324
* @param string|null $plugin Plugin name
323325
* @param \Cake\Console\ConsoleIo $io Console IO
324326
* @return void
325327
*/
326-
protected function clearMigrationRecords(Connection $connection, ?string $plugin, ConsoleIo $io): void
328+
protected function clearTrackingRecords(Connection $connection, ?string $plugin, ConsoleIo $io): void
327329
{
328330
$schema = $connection->getDriver()->schemaDialect();
329331

330-
// Clear unified table if exists
332+
// Clear unified migrations table if exists
331333
if ($schema->hasTable('cake_migrations')) {
332334
$query = $connection->deleteQuery()->delete('cake_migrations');
333335
if ($plugin !== null) {
@@ -345,6 +347,14 @@ protected function clearMigrationRecords(Connection $connection, ?string $plugin
345347
->execute();
346348
$io->verbose("Cleared migration records from {$legacyTable}");
347349
}
350+
351+
// Clear seed tracking table if exists
352+
if ($schema->hasTable('cake_seeds')) {
353+
$connection->deleteQuery()
354+
->delete('cake_seeds')
355+
->execute();
356+
$io->verbose('Cleared seed records from cake_seeds');
357+
}
348358
}
349359

350360
/**

0 commit comments

Comments
 (0)