Skip to content

Commit 70ea3ba

Browse files
authored
fix: sqlite can be migrated with --force (#395)
closes #352
1 parent 18740ba commit 70ea3ba

2 files changed

Lines changed: 53 additions & 0 deletions

File tree

src/Migration/Migrator.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,14 @@ class Migrator {
2727
protected string $tableName;
2828
protected string $charset;
2929
protected string $collate;
30+
protected Settings $settings;
3031

3132
public function __construct(
3233
Settings $settings,
3334
string $path,
3435
string $tableName = "_migration"
3536
) {
37+
$this->settings = clone $settings;
3638
$this->schema = $settings->getSchema();
3739
$this->path = $path;
3840
$this->tableName = $tableName;
@@ -298,6 +300,14 @@ public function resetMigrationSequence(int $numberToForce):void {
298300
*/
299301
public function deleteAndRecreateSchema():void {
300302
if($this->driver === Settings::DRIVER_SQLITE) {
303+
unset($this->dbClient);
304+
305+
if($this->schema !== Settings::SCHEMA_IN_MEMORY
306+
&& is_file($this->schema)) {
307+
unlink($this->schema);
308+
}
309+
310+
$this->dbClient = new Database($this->settings);
301311
return;
302312
}
303313

test/phpunit/Cli/ExecuteCommandTest.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,49 @@ public function testExecuteWithResetWithNumber():void {
247247
}
248248
}
249249

250+
public function testExecuteWithForceResetsSqliteDatabaseAndRerunsFromMigrationOne():void {
251+
$project = $this->createProjectDir();
252+
$sqlitePath = str_replace("\\", "/", $project . DIRECTORY_SEPARATOR . "cli-test.db");
253+
$this->writeConfigIni($project, $sqlitePath);
254+
$this->createMigrations($project, 2);
255+
256+
$cwdBackup = getcwd();
257+
chdir($project);
258+
try {
259+
$cmd = new ExecuteCommand();
260+
$initialStreams = $this->makeStreamFiles();
261+
$cmd->setStream($initialStreams["stream"]);
262+
$cmd->run(new ArgumentValueList());
263+
264+
$settings = new Settings($project . DIRECTORY_SEPARATOR . "query", Settings::DRIVER_SQLITE, $sqlitePath);
265+
$db = new Database($settings);
266+
$db->executeSql("insert into `test` (`id`, `name`, `new_column_2`) values (1, 'before-force', 'value')");
267+
$rowCountBeforeForce = $db->executeSql("select count(*) as c from `test`")->fetch()?->getInt("c");
268+
self::assertSame(1, $rowCountBeforeForce);
269+
270+
unset($db);
271+
272+
$forceStreams = $this->makeStreamFiles();
273+
$cmd->setStream($forceStreams["stream"]);
274+
$args = new ArgumentValueList();
275+
$args->set("force");
276+
$cmd->run($args);
277+
278+
list("out" => $out) = $this->readFromFiles($forceStreams["out"], $forceStreams["err"]);
279+
self::assertStringContainsString("Migration 1:", $out);
280+
self::assertStringContainsString("2 migrations were completed successfully.", $out);
281+
282+
$dbAfterForce = new Database($settings);
283+
$rowCountAfterForce = $dbAfterForce->executeSql("select count(*) as c from `test`")->fetch()?->getInt("c");
284+
self::assertSame(0, $rowCountAfterForce);
285+
$migrationCount = $dbAfterForce->executeSql("select count(*) as c from `_migration`")->fetch()?->getInt("c");
286+
self::assertSame(2, $migrationCount);
287+
}
288+
finally {
289+
chdir($cwdBackup);
290+
}
291+
}
292+
250293
public function testOptionalParameterListContainsCliOverrides():void {
251294
$command = new ExecuteCommand();
252295
$parameterNames = array_map(

0 commit comments

Comments
 (0)