Skip to content

Commit 0838a61

Browse files
Merge pull request #63638 from nextcloud/backport/63536/stable34
[stable34] test(db): cover migration version sorting
2 parents e8edb07 + b89bc2a commit 0838a61

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

@@ -171,10 +174,10 @@ public function testExecuteStepWithoutSchemaChange(): void {
171174

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

@@ -190,18 +193,85 @@ public function testGetMigration($alias, $expected): void {
190193
->getMock();
191194

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

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

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

207277
public function testMigrate(): void {
@@ -211,15 +281,26 @@ public function testMigrate(): void {
211281
->getMock();
212282

213283
$migrationService->expects($this->any())->method('getMigratedVersions')->willReturn(
214-
['20170130180000', '20170130180001']
284+
[
285+
'8000Date20200407115318',
286+
'10000Date20200819121721',
287+
]
215288
);
216289
$migrationService->expects($this->any())->method('findMigrations')->willReturn(
217-
['20170130180000' => 'X', '20170130180001' => 'Y', '20170130180002' => 'Z', '20170130180003' => 'A']
290+
[
291+
'20000Date20240718031959' => 'D',
292+
'10000Date20200819121721' => 'B',
293+
'8000Date20200407115318' => 'A',
294+
'20000Date20240717180417' => 'C',
295+
]
218296
);
219297

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

224305
$calls = [];
225306
$migrationService
@@ -230,7 +311,10 @@ public function testMigrate(): void {
230311
});
231312

232313
$migrationService->migrate();
233-
self::assertEquals(['20170130180002', '20170130180003'], $calls);
314+
self::assertSame([
315+
'20000Date20240717180417',
316+
'20000Date20240718031959',
317+
], $calls);
234318
}
235319

236320
#[DataProvider('dataEnsureNamingConstraintsTableName')]

0 commit comments

Comments
 (0)