diff --git a/src/Migration/Migrator.php b/src/Migration/Migrator.php index 6d47d26..03c6ff2 100644 --- a/src/Migration/Migrator.php +++ b/src/Migration/Migrator.php @@ -27,12 +27,14 @@ class Migrator { protected string $tableName; protected string $charset; protected string $collate; + protected Settings $settings; public function __construct( Settings $settings, string $path, string $tableName = "_migration" ) { + $this->settings = clone $settings; $this->schema = $settings->getSchema(); $this->path = $path; $this->tableName = $tableName; @@ -298,6 +300,14 @@ public function resetMigrationSequence(int $numberToForce):void { */ public function deleteAndRecreateSchema():void { if($this->driver === Settings::DRIVER_SQLITE) { + unset($this->dbClient); + + if($this->schema !== Settings::SCHEMA_IN_MEMORY + && is_file($this->schema)) { + unlink($this->schema); + } + + $this->dbClient = new Database($this->settings); return; } diff --git a/test/phpunit/Cli/ExecuteCommandTest.php b/test/phpunit/Cli/ExecuteCommandTest.php index 614fbc7..30a49ca 100644 --- a/test/phpunit/Cli/ExecuteCommandTest.php +++ b/test/phpunit/Cli/ExecuteCommandTest.php @@ -247,6 +247,49 @@ public function testExecuteWithResetWithNumber():void { } } + public function testExecuteWithForceResetsSqliteDatabaseAndRerunsFromMigrationOne():void { + $project = $this->createProjectDir(); + $sqlitePath = str_replace("\\", "/", $project . DIRECTORY_SEPARATOR . "cli-test.db"); + $this->writeConfigIni($project, $sqlitePath); + $this->createMigrations($project, 2); + + $cwdBackup = getcwd(); + chdir($project); + try { + $cmd = new ExecuteCommand(); + $initialStreams = $this->makeStreamFiles(); + $cmd->setStream($initialStreams["stream"]); + $cmd->run(new ArgumentValueList()); + + $settings = new Settings($project . DIRECTORY_SEPARATOR . "query", Settings::DRIVER_SQLITE, $sqlitePath); + $db = new Database($settings); + $db->executeSql("insert into `test` (`id`, `name`, `new_column_2`) values (1, 'before-force', 'value')"); + $rowCountBeforeForce = $db->executeSql("select count(*) as c from `test`")->fetch()?->getInt("c"); + self::assertSame(1, $rowCountBeforeForce); + + unset($db); + + $forceStreams = $this->makeStreamFiles(); + $cmd->setStream($forceStreams["stream"]); + $args = new ArgumentValueList(); + $args->set("force"); + $cmd->run($args); + + list("out" => $out) = $this->readFromFiles($forceStreams["out"], $forceStreams["err"]); + self::assertStringContainsString("Migration 1:", $out); + self::assertStringContainsString("2 migrations were completed successfully.", $out); + + $dbAfterForce = new Database($settings); + $rowCountAfterForce = $dbAfterForce->executeSql("select count(*) as c from `test`")->fetch()?->getInt("c"); + self::assertSame(0, $rowCountAfterForce); + $migrationCount = $dbAfterForce->executeSql("select count(*) as c from `_migration`")->fetch()?->getInt("c"); + self::assertSame(2, $migrationCount); + } + finally { + chdir($cwdBackup); + } + } + public function testOptionalParameterListContainsCliOverrides():void { $command = new ExecuteCommand(); $parameterNames = array_map(