Skip to content

Commit 82769af

Browse files
Merge pull request #63637 from nextcloud/backport/63536/stable33
[stable33] test(db): cover migration version sorting
2 parents 39cbb84 + 365b3b0 commit 82769af

1 file changed

Lines changed: 100 additions & 16 deletions

File tree

‎tests/lib/DB/MigrationServiceTest.php‎

Lines changed: 100 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222
use OCP\App\AppPathNotFoundException;
2323
use OCP\IDBConnection;
2424
use OCP\Migration\IMigrationStep;
25+
use OCP\Server;
2526
use PHPUnit\Framework\Attributes\DataProvider;
27+
use PHPUnit\Framework\Attributes\Group;
2628
use PHPUnit\Framework\Attributes\TestWith;
2729
use PHPUnit\Framework\MockObject\MockObject;
2830
use Psr\Log\LoggerInterface;
@@ -32,6 +34,7 @@
3234
*
3335
* @package Test\DB
3436
*/
37+
#[Group('DB')]
3538
class MigrationServiceTest extends \Test\TestCase {
3639
private Connection&MockObject $db;
3740

@@ -170,10 +173,10 @@ public function testExecuteStepWithoutSchemaChange(): void {
170173

171174
public static function dataGetMigration(): array {
172175
return [
173-
['current', '20170130180001'],
174-
['prev', '20170130180000'],
175-
['next', '20170130180002'],
176-
['latest', '20170130180003'],
176+
['current', '10000Date20200819121721'],
177+
['prev', '8000Date20200407115318'],
178+
['next', '20000Date20240717180417'],
179+
['latest', '20000Date20240718031959'],
177180
];
178181
}
179182

@@ -189,18 +192,85 @@ public function testGetMigration($alias, $expected): void {
189192
->getMock();
190193

191194
$migrationService->expects($this->any())->method('getMigratedVersions')->willReturn(
192-
['20170130180000', '20170130180001']
195+
[
196+
'8000Date20200407115318',
197+
'10000Date20200819121721',
198+
]
193199
);
194200
$migrationService->expects($this->any())->method('findMigrations')->willReturn(
195-
['20170130180000' => 'X', '20170130180001' => 'Y', '20170130180002' => 'Z', '20170130180003' => 'A']
201+
[
202+
'20000Date20240718031959' => 'D',
203+
'10000Date20200819121721' => 'B',
204+
'8000Date20200407115318' => 'A',
205+
'20000Date20240717180417' => 'C',
206+
]
196207
);
197208

198-
$this->assertEquals(
199-
['20170130180000', '20170130180001', '20170130180002', '20170130180003'],
200-
$migrationService->getAvailableVersions());
209+
$this->assertSame([
210+
'8000Date20200407115318',
211+
'10000Date20200819121721',
212+
'20000Date20240717180417',
213+
'20000Date20240718031959',
214+
], $migrationService->getAvailableVersions());
201215

202216
$migration = $migrationService->getMigration($alias);
203-
$this->assertEquals($expected, $migration);
217+
$this->assertSame($expected, $migration);
218+
}
219+
220+
#[Group('DB')]
221+
public function testGetMigratedVersionsSortsByVersionThenDate(): void {
222+
/** @var Connection $db */
223+
$db = Server::get(Connection::class);
224+
$appId = 'migration_sort_' . bin2hex(random_bytes(8));
225+
226+
$migrationService = new class('testing', $db, $appId) extends MigrationService {
227+
public function __construct(
228+
string $appName,
229+
Connection $connection,
230+
private string $migrationApp,
231+
) {
232+
parent::__construct($appName, $connection);
233+
}
234+
235+
#[\Override]
236+
public function getApp(): string {
237+
return $this->migrationApp;
238+
}
239+
};
240+
241+
// Ensure the migrations table exists before inserting the fixtures.
242+
self::assertSame([], $migrationService->getMigratedVersions());
243+
244+
$versions = [
245+
'20000Date20240718031959',
246+
'10000Date20200819121721',
247+
'8000Date20200407115318',
248+
'20000Date20240717180417',
249+
];
250+
251+
try {
252+
foreach ($versions as $version) {
253+
$db->insertIfNotExist('*PREFIX*migrations', [
254+
'app' => $appId,
255+
'version' => $version,
256+
]);
257+
}
258+
259+
self::assertSame([
260+
'8000Date20200407115318',
261+
'10000Date20200819121721',
262+
'20000Date20240717180417',
263+
'20000Date20240718031959',
264+
], $migrationService->getMigratedVersions());
265+
} finally {
266+
$qb = $db->getQueryBuilder();
267+
$qb->delete('migrations')
268+
->where($qb->expr()->eq(
269+
'app',
270+
$qb->createNamedParameter($appId),
271+
))
272+
->executeStatement();
273+
}
204274
}
205275

206276
public function testMigrate(): void {
@@ -210,15 +280,26 @@ public function testMigrate(): void {
210280
->getMock();
211281

212282
$migrationService->expects($this->any())->method('getMigratedVersions')->willReturn(
213-
['20170130180000', '20170130180001']
283+
[
284+
'8000Date20200407115318',
285+
'10000Date20200819121721',
286+
]
214287
);
215288
$migrationService->expects($this->any())->method('findMigrations')->willReturn(
216-
['20170130180000' => 'X', '20170130180001' => 'Y', '20170130180002' => 'Z', '20170130180003' => 'A']
289+
[
290+
'20000Date20240718031959' => 'D',
291+
'10000Date20200819121721' => 'B',
292+
'8000Date20200407115318' => 'A',
293+
'20000Date20240717180417' => 'C',
294+
]
217295
);
218296

219-
$this->assertEquals(
220-
['20170130180000', '20170130180001', '20170130180002', '20170130180003'],
221-
$migrationService->getAvailableVersions());
297+
$this->assertSame([
298+
'8000Date20200407115318',
299+
'10000Date20200819121721',
300+
'20000Date20240717180417',
301+
'20000Date20240718031959',
302+
], $migrationService->getAvailableVersions());
222303

223304
$calls = [];
224305
$migrationService
@@ -229,7 +310,10 @@ public function testMigrate(): void {
229310
});
230311

231312
$migrationService->migrate();
232-
self::assertEquals(['20170130180002', '20170130180003'], $calls);
313+
self::assertSame([
314+
'20000Date20240717180417',
315+
'20000Date20240718031959',
316+
], $calls);
233317
}
234318

235319
#[DataProvider('dataEnsureNamingConstraintsTableName')]

0 commit comments

Comments
 (0)