Skip to content

Commit

Permalink
单独存储数据库版本号
Browse files Browse the repository at this point in the history
  • Loading branch information
Yurunsoft committed Jun 26, 2023
1 parent 7c2efe8 commit 35bc5c5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
12 changes: 10 additions & 2 deletions src/Handler/FileMigrationHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class FileMigrationHandler implements IMigrationHandler

private ?array $data = null;

private ?string $version = null;

public function __init(): void
{
$this->data = $this->loadData();
Expand Down Expand Up @@ -50,12 +52,13 @@ public function saveData(): void

public function getCurrentVersion(): string
{
return $this->data['version'] ??= '';
return $this->version ??= (file_get_contents($this->getVersionFile()) ?: '0');
}

public function setCurrentVersion(string $version): self
{
$this->data['version'] = $version;
$this->version = $version;
File::putContents($this->getVersionFile(), $version);

return $this;
}
Expand Down Expand Up @@ -222,4 +225,9 @@ protected function loadData(): array
return [];
}
}

protected function getVersionFile(): string
{
return $this->path('version');
}
}
18 changes: 9 additions & 9 deletions tests/MigrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ public function testMigrateGenerate(): void
$generateCommand = \dirname(__DIR__) . '/example/bin/imi-cli generate/model "app\Model" --app-namespace "app" --prefix=tb_ --override=base --lengthCheck --sqlSingleLine';
$generateCommandNoMigration = $generateCommand . ' --no-migration';
$dataFile = \dirname(__DIR__) . '/example/.migration/data.json';
$versionFile = \dirname(__DIR__) . '/example/.migration/version';

$modelPath = \dirname(__DIR__) . '/example/Model';
shell_exec('rm -rf ' . \dirname(__DIR__) . '/Model');
Expand All @@ -150,9 +151,8 @@ public function testMigrateGenerate(): void
$this->assertEquals(0, $resultCode);
$this->assertTrue(is_file($dataFile));
$this->assertNotEquals('[]', $content = file_get_contents($dataFile));
$data = json_decode($content, true, \JSON_THROW_ON_ERROR);
$version = $data['version'] ?? null;
$this->assertIsString($version);
$this->assertTrue(is_file($versionFile));
$this->assertIsString($version = file_get_contents($versionFile));
$versionPath = $path . '/' . $version;

// 迁移文件内容校验
Expand Down Expand Up @@ -209,8 +209,8 @@ public function testMigrateGenerate(): void
$this->assertEquals(0, $resultCode);
$this->assertTrue(is_file($dataFile));
$this->assertNotEquals('[]', $content = file_get_contents($dataFile));
$data2 = json_decode($content, true, \JSON_THROW_ON_ERROR);
$tmpVersion = $data2['version'] ?? null;
$this->assertTrue(is_file($versionFile));
$this->assertIsString($tmpVersion = file_get_contents($versionFile));
$this->assertEquals($version, $tmpVersion); // 版本无变化

// 回滚成功
Expand All @@ -223,8 +223,8 @@ public function testMigrateGenerate(): void
$this->assertEquals(0, $resultCode);
$this->assertTrue(is_file($dataFile));
$this->assertNotEquals('[]', $content = file_get_contents($dataFile));
$data2 = json_decode($content, true, \JSON_THROW_ON_ERROR);
$tmpVersion = $data2['version'] ?? null;
$this->assertTrue(is_file($versionFile));
$this->assertIsString($tmpVersion = file_get_contents($versionFile));
$this->assertEquals('0', $tmpVersion);

// 迁移
Expand All @@ -235,8 +235,8 @@ public function testMigrateGenerate(): void
$this->assertEquals(0, $resultCode);
$this->assertTrue(is_file($dataFile));
$this->assertNotEquals('[]', $content = file_get_contents($dataFile));
$data2 = json_decode($content, true, \JSON_THROW_ON_ERROR);
$tmpVersion = $data2['version'] ?? null;
$this->assertTrue(is_file($versionFile));
$this->assertIsString($tmpVersion = file_get_contents($versionFile));
$this->assertEquals($version, $tmpVersion); // 版本无变化
}
finally
Expand Down

0 comments on commit 35bc5c5

Please sign in to comment.