Skip to content

Commit 18740ba

Browse files
committed
feature: only execute numbered files
closes #357
1 parent e015e86 commit 18740ba

4 files changed

Lines changed: 76 additions & 2 deletions

File tree

src/Migration/DevMigrator.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ public function getMigrationFileList():array {
6262
}
6363

6464
$fileList = glob("$this->path/*.sql");
65+
$fileList = array_values(array_filter($fileList, function(string $file):bool {
66+
return preg_match("/^\d+.*\.sql$/", basename($file)) === 1;
67+
}));
6568
sort($fileList);
6669
return $fileList;
6770
}
@@ -108,7 +111,7 @@ public function checkIntegrity(array $migrationFileList):void {
108111
public function extractNumberFromFilename(string $pathName):int {
109112
$file = new SplFileInfo($pathName);
110113
$filename = $file->getFilename();
111-
preg_match("/(\d+)-?.*\.sql/", $filename, $matches);
114+
preg_match("/^(\d+)-?.*\.sql$/", $filename, $matches);
112115

113116
if(!isset($matches[1])) {
114117
throw new MigrationFileNameFormatException($filename);

src/Migration/Migrator.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@ public function getMigrationFileList():array {
118118
}
119119

120120
$fileList = glob("$this->path/*.sql");
121+
$fileList = array_values(array_filter($fileList, function(string $file):bool {
122+
return preg_match("/^\d+.*\.sql$/", basename($file)) === 1;
123+
}));
121124
sort($fileList);
122125
return $fileList;
123126
}
@@ -186,7 +189,7 @@ public function checkIntegrity(
186189
public function extractNumberFromFilename(string $pathName):int {
187190
$file = new SplFileInfo($pathName);
188191
$filename = $file->getFilename();
189-
preg_match("/(\d+)-?.*\.sql/", $filename, $matches);
192+
preg_match("/^(\d+)-?.*\.sql$/", $filename, $matches);
190193

191194
if(!isset($matches[1])) {
192195
throw new MigrationFileNameFormatException($filename);

test/phpunit/Migration/DevMigratorTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,4 +146,26 @@ public function testDevMigratorThrowsOnDuplicateSequenceNumbers():void {
146146
$this->expectException(MigrationSequenceOrderException::class);
147147
$devMigrator->checkFileListOrder($devMigrator->getMigrationFileList());
148148
}
149+
150+
public function testDevMigratorIgnoresNonNumericFilesAndThrowsOnResultingGap():void {
151+
$project = $this->createProjectDir();
152+
$databasePath = $project . DIRECTORY_SEPARATOR . "dev.sqlite";
153+
$settings = $this->createSettings($project, $databasePath);
154+
$devPath = $project . DIRECTORY_SEPARATOR . "query" . DIRECTORY_SEPARATOR . "_migration" . DIRECTORY_SEPARATOR . "dev";
155+
mkdir($devPath, 0775, true);
156+
file_put_contents($devPath . DIRECTORY_SEPARATOR . "001-first.sql", "select 1");
157+
file_put_contents($devPath . DIRECTORY_SEPARATOR . "a002-second.sql", "select 1");
158+
file_put_contents($devPath . DIRECTORY_SEPARATOR . "003-third.sql", "select 1");
159+
160+
$devMigrator = new DevMigrator($settings, $devPath);
161+
$fileList = $devMigrator->getMigrationFileList();
162+
163+
self::assertSame([
164+
$devPath . DIRECTORY_SEPARATOR . "001-first.sql",
165+
$devPath . DIRECTORY_SEPARATOR . "003-third.sql",
166+
], $fileList);
167+
168+
$this->expectException(MigrationSequenceOrderException::class);
169+
$devMigrator->checkFileListOrder($fileList);
170+
}
149171
}

test/phpunit/Migration/MigratorTest.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,28 @@ public function testCheckFileListOrderOutOfOrder():void {
154154
$migrator->checkFileListOrder($outOfOrder);
155155
}
156156

157+
public function testCheckFileListOrderIgnoresNonNumericFilesAndThrowsOnResultingGap():void {
158+
$path = $this->getMigrationDirectory();
159+
$files = [
160+
"001-first.sql",
161+
"a002-second.sql",
162+
"003-third.sql",
163+
];
164+
$this->createFiles($files, $path);
165+
166+
$settings = $this->createSettings($path);
167+
$migrator = new Migrator($settings, $path);
168+
$actualFileList = $migrator->getMigrationFileList();
169+
170+
self::assertSame([
171+
$path . DIRECTORY_SEPARATOR . "001-first.sql",
172+
$path . DIRECTORY_SEPARATOR . "003-third.sql",
173+
], $actualFileList);
174+
175+
$this->expectException(MigrationSequenceOrderException::class);
176+
$migrator->checkFileListOrder($actualFileList);
177+
}
178+
157179
/** @dataProvider dataMigrationFileList */
158180
public function testCheckIntegrityGood(array $fileList) {
159181
$path = $this->getMigrationDirectory();
@@ -531,6 +553,30 @@ public function testNonSqlExtensions(array $fileList) {
531553
self::assertNull($exception);
532554
}
533555

556+
public function testPerformMigrationIgnoresNonNumericPrefixedSqlFiles():void {
557+
$path = $this->getMigrationDirectory();
558+
file_put_contents($path . DIRECTORY_SEPARATOR . "0001-create-test.sql", self::MIGRATION_CREATE);
559+
file_put_contents(
560+
$path . DIRECTORY_SEPARATOR . "a0002-ignored.sql",
561+
"alter table `test` add `ignored_column` varchar(32)"
562+
);
563+
564+
$settings = $this->createSettings($path);
565+
$migrator = new Migrator($settings, $path);
566+
$migrator->createMigrationTable();
567+
568+
$actualFileList = $migrator->getMigrationFileList();
569+
self::assertSame([
570+
$path . DIRECTORY_SEPARATOR . "0001-create-test.sql",
571+
], $actualFileList);
572+
573+
$migrator->performMigration($actualFileList);
574+
575+
$db = new Database($settings);
576+
$result = $db->executeSql("PRAGMA table_info(test);");
577+
self::assertCount(2, $result->fetchAll());
578+
}
579+
534580
/** @dataProvider dataMigrationFileList */
535581
public function testMigrationThrowsExceptionWhenNoMigrationTable(array $fileList) {
536582
$path = $this->getMigrationDirectory();

0 commit comments

Comments
 (0)