diff --git a/tests/lib/DB/MigrationServiceTest.php b/tests/lib/DB/MigrationServiceTest.php index 72edd4aa4dfbf..0366344a0cbb9 100644 --- a/tests/lib/DB/MigrationServiceTest.php +++ b/tests/lib/DB/MigrationServiceTest.php @@ -22,7 +22,9 @@ use OCP\App\AppPathNotFoundException; use OCP\IDBConnection; use OCP\Migration\IMigrationStep; +use OCP\Server; use PHPUnit\Framework\Attributes\DataProvider; +use PHPUnit\Framework\Attributes\Group; use PHPUnit\Framework\Attributes\TestWith; use PHPUnit\Framework\MockObject\MockObject; use Psr\Log\LoggerInterface; @@ -32,6 +34,7 @@ * * @package Test\DB */ +#[Group('DB')] class MigrationServiceTest extends \Test\TestCase { private Connection&MockObject $db; @@ -171,10 +174,10 @@ public function testExecuteStepWithoutSchemaChange(): void { public static function dataGetMigration(): array { return [ - ['current', '20170130180001'], - ['prev', '20170130180000'], - ['next', '20170130180002'], - ['latest', '20170130180003'], + ['current', '10000Date20200819121721'], + ['prev', '8000Date20200407115318'], + ['next', '20000Date20240717180417'], + ['latest', '20000Date20240718031959'], ]; } @@ -190,18 +193,85 @@ public function testGetMigration($alias, $expected): void { ->getMock(); $migrationService->expects($this->any())->method('getMigratedVersions')->willReturn( - ['20170130180000', '20170130180001'] + [ + '8000Date20200407115318', + '10000Date20200819121721', + ] ); $migrationService->expects($this->any())->method('findMigrations')->willReturn( - ['20170130180000' => 'X', '20170130180001' => 'Y', '20170130180002' => 'Z', '20170130180003' => 'A'] + [ + '20000Date20240718031959' => 'D', + '10000Date20200819121721' => 'B', + '8000Date20200407115318' => 'A', + '20000Date20240717180417' => 'C', + ] ); - $this->assertEquals( - ['20170130180000', '20170130180001', '20170130180002', '20170130180003'], - $migrationService->getAvailableVersions()); + $this->assertSame([ + '8000Date20200407115318', + '10000Date20200819121721', + '20000Date20240717180417', + '20000Date20240718031959', + ], $migrationService->getAvailableVersions()); $migration = $migrationService->getMigration($alias); - $this->assertEquals($expected, $migration); + $this->assertSame($expected, $migration); + } + + #[Group('DB')] + public function testGetMigratedVersionsSortsByVersionThenDate(): void { + /** @var Connection $db */ + $db = Server::get(IDBConnection::class); + $appId = 'migration_sort_' . bin2hex(random_bytes(8)); + + $migrationService = new class('testing', $db, $appId) extends MigrationService { + public function __construct( + string $appName, + Connection $connection, + private string $migrationApp, + ) { + parent::__construct($appName, $connection); + } + + #[\Override] + public function getApp(): string { + return $this->migrationApp; + } + }; + + // Ensure the migrations table exists before inserting the fixtures. + self::assertSame([], $migrationService->getMigratedVersions()); + + $versions = [ + '20000Date20240718031959', + '10000Date20200819121721', + '8000Date20200407115318', + '20000Date20240717180417', + ]; + + try { + foreach ($versions as $version) { + $db->insertIfNotExist('*PREFIX*migrations', [ + 'app' => $appId, + 'version' => $version, + ]); + } + + self::assertSame([ + '8000Date20200407115318', + '10000Date20200819121721', + '20000Date20240717180417', + '20000Date20240718031959', + ], $migrationService->getMigratedVersions()); + } finally { + $qb = $db->getQueryBuilder(); + $qb->delete('migrations') + ->where($qb->expr()->eq( + 'app', + $qb->createNamedParameter($appId), + )) + ->executeStatement(); + } } public function testMigrate(): void { @@ -211,15 +281,26 @@ public function testMigrate(): void { ->getMock(); $migrationService->expects($this->any())->method('getMigratedVersions')->willReturn( - ['20170130180000', '20170130180001'] + [ + '8000Date20200407115318', + '10000Date20200819121721', + ] ); $migrationService->expects($this->any())->method('findMigrations')->willReturn( - ['20170130180000' => 'X', '20170130180001' => 'Y', '20170130180002' => 'Z', '20170130180003' => 'A'] + [ + '20000Date20240718031959' => 'D', + '10000Date20200819121721' => 'B', + '8000Date20200407115318' => 'A', + '20000Date20240717180417' => 'C', + ] ); - $this->assertEquals( - ['20170130180000', '20170130180001', '20170130180002', '20170130180003'], - $migrationService->getAvailableVersions()); + $this->assertSame([ + '8000Date20200407115318', + '10000Date20200819121721', + '20000Date20240717180417', + '20000Date20240718031959', + ], $migrationService->getAvailableVersions()); $calls = []; $migrationService @@ -230,7 +311,10 @@ public function testMigrate(): void { }); $migrationService->migrate(); - self::assertEquals(['20170130180002', '20170130180003'], $calls); + self::assertSame([ + '20000Date20240717180417', + '20000Date20240718031959', + ], $calls); } #[DataProvider('dataEnsureNamingConstraintsTableName')]