diff options
Diffstat (limited to 'tests/lib/DB/MigrationsTest.php')
-rw-r--r-- | tests/lib/DB/MigrationsTest.php | 284 |
1 files changed, 232 insertions, 52 deletions
diff --git a/tests/lib/DB/MigrationsTest.php b/tests/lib/DB/MigrationsTest.php index d982a47623b..2b39b26d852 100644 --- a/tests/lib/DB/MigrationsTest.php +++ b/tests/lib/DB/MigrationsTest.php @@ -1,16 +1,13 @@ <?php /** - * Copyright (c) 2016 Thomas Müller <thomas.mueller@tmit.eu> - * This file is licensed under the Affero General Public License version 3 or - * later. - * See the COPYING-README file. + * SPDX-FileCopyrightText: 2018-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace Test\DB; -use Doctrine\DBAL\Platforms\OraclePlatform; -use Doctrine\DBAL\Platforms\PostgreSqlPlatform; use Doctrine\DBAL\Schema\Column; use Doctrine\DBAL\Schema\ForeignKeyConstraint; use Doctrine\DBAL\Schema\Index; @@ -22,8 +19,21 @@ use Doctrine\DBAL\Types\Type; use OC\DB\Connection; use OC\DB\MigrationService; use OC\DB\SchemaWrapper; +use OC\Migration\MetadataManager; +use OCP\App\IAppManager; use OCP\IDBConnection; +use OCP\Migration\Attributes\AddColumn; +use OCP\Migration\Attributes\AddIndex; +use OCP\Migration\Attributes\ColumnType; +use OCP\Migration\Attributes\CreateTable; +use OCP\Migration\Attributes\DropColumn; +use OCP\Migration\Attributes\DropIndex; +use OCP\Migration\Attributes\DropTable; +use OCP\Migration\Attributes\IndexType; +use OCP\Migration\Attributes\ModifyColumn; use OCP\Migration\IMigrationStep; +use OCP\Server; +use PHPUnit\Framework\MockObject\MockObject; /** * Class MigrationsTest @@ -31,10 +41,9 @@ use OCP\Migration\IMigrationStep; * @package Test\DB */ class MigrationsTest extends \Test\TestCase { - /** @var MigrationService | \PHPUnit\Framework\MockObject\MockObject */ - private $migrationService; - /** @var \PHPUnit\Framework\MockObject\MockObject | IDBConnection $db */ - private $db; + private MigrationService|MockObject $migrationService; + private MockObject|IDBConnection $db; + private IAppManager $appManager; protected function setUp(): void { parent::setUp(); @@ -42,16 +51,18 @@ class MigrationsTest extends \Test\TestCase { $this->db = $this->createMock(Connection::class); $this->db->expects($this->any())->method('getPrefix')->willReturn('test_oc_'); $this->migrationService = new MigrationService('testing', $this->db); + + $this->appManager = Server::get(IAppManager::class); } - public function testGetters() { + public function testGetters(): void { $this->assertEquals('testing', $this->migrationService->getApp()); $this->assertEquals(\OC::$SERVERROOT . '/apps/testing/lib/Migration', $this->migrationService->getMigrationsDirectory()); $this->assertEquals('OCA\Testing\Migration', $this->migrationService->getMigrationsNamespace()); $this->assertEquals('test_oc_migrations', $this->migrationService->getMigrationsTableName()); } - public function testCore() { + public function testCore(): void { $this->migrationService = new MigrationService('core', $this->db); $this->assertEquals('core', $this->migrationService->getApp()); @@ -61,7 +72,7 @@ class MigrationsTest extends \Test\TestCase { } - public function testExecuteUnknownStep() { + public function testExecuteUnknownStep(): void { $this->expectException(\InvalidArgumentException::class); $this->expectExceptionMessage('Version 20170130180000 is unknown.'); @@ -69,7 +80,7 @@ class MigrationsTest extends \Test\TestCase { } - public function testUnknownApp() { + public function testUnknownApp(): void { $this->expectException(\Exception::class); $this->expectExceptionMessage('App not found'); @@ -77,12 +88,12 @@ class MigrationsTest extends \Test\TestCase { } - public function testExecuteStepWithUnknownClass() { + public function testExecuteStepWithUnknownClass(): void { $this->expectException(\Exception::class); $this->expectExceptionMessage('Migration step \'X\' is unknown'); $this->migrationService = $this->getMockBuilder(MigrationService::class) - ->setMethods(['findMigrations']) + ->onlyMethods(['findMigrations']) ->setConstructorArgs(['testing', $this->db]) ->getMock(); $this->migrationService->expects($this->any())->method('findMigrations')->willReturn( @@ -91,7 +102,7 @@ class MigrationsTest extends \Test\TestCase { $this->migrationService->executeStep('20170130180000'); } - public function testExecuteStepWithSchemaChange() { + public function testExecuteStepWithSchemaChange(): void { $schema = $this->createMock(Schema::class); $this->db->expects($this->any()) ->method('createSchema') @@ -123,7 +134,7 @@ class MigrationsTest extends \Test\TestCase { ->method('postSchemaChange'); $this->migrationService = $this->getMockBuilder(MigrationService::class) - ->setMethods(['createInstance']) + ->onlyMethods(['createInstance']) ->setConstructorArgs(['testing', $this->db]) ->getMock(); @@ -134,7 +145,7 @@ class MigrationsTest extends \Test\TestCase { $this->migrationService->executeStep('20170130180000'); } - public function testExecuteStepWithoutSchemaChange() { + public function testExecuteStepWithoutSchemaChange(): void { $schema = $this->createMock(Schema::class); $this->db->expects($this->any()) ->method('createSchema') @@ -153,7 +164,7 @@ class MigrationsTest extends \Test\TestCase { ->method('postSchemaChange'); $this->migrationService = $this->getMockBuilder(MigrationService::class) - ->setMethods(['createInstance']) + ->onlyMethods(['createInstance']) ->setConstructorArgs(['testing', $this->db]) ->getMock(); @@ -164,7 +175,7 @@ class MigrationsTest extends \Test\TestCase { $this->migrationService->executeStep('20170130180000'); } - public function dataGetMigration() { + public static function dataGetMigration(): array { return [ ['current', '20170130180001'], ['prev', '20170130180000'], @@ -174,13 +185,13 @@ class MigrationsTest extends \Test\TestCase { } /** - * @dataProvider dataGetMigration * @param string $alias * @param string $expected */ - public function testGetMigration($alias, $expected) { + #[\PHPUnit\Framework\Attributes\DataProvider('dataGetMigration')] + public function testGetMigration($alias, $expected): void { $this->migrationService = $this->getMockBuilder(MigrationService::class) - ->setMethods(['getMigratedVersions', 'findMigrations']) + ->onlyMethods(['getMigratedVersions', 'findMigrations']) ->setConstructorArgs(['testing', $this->db]) ->getMock(); $this->migrationService->expects($this->any())->method('getMigratedVersions')->willReturn( @@ -198,28 +209,39 @@ class MigrationsTest extends \Test\TestCase { $this->assertEquals($expected, $migration); } - public function testMigrate() { + public function testMigrate(): void { $this->migrationService = $this->getMockBuilder(MigrationService::class) - ->setMethods(['getMigratedVersions', 'findMigrations', 'executeStep']) + ->onlyMethods(['getMigratedVersions', 'findMigrations', 'executeStep']) ->setConstructorArgs(['testing', $this->db]) ->getMock(); - $this->migrationService->expects($this->any())->method('getMigratedVersions')->willReturn( - ['20170130180000', '20170130180001'] - ); - $this->migrationService->expects($this->any())->method('findMigrations')->willReturn( - ['20170130180000' => 'X', '20170130180001' => 'Y', '20170130180002' => 'Z', '20170130180003' => 'A'] - ); + $this->migrationService->method('getMigratedVersions') + ->willReturn( + ['20170130180000', '20170130180001'] + ); + $this->migrationService->method('findMigrations') + ->willReturn( + ['20170130180000' => 'X', '20170130180001' => 'Y', '20170130180002' => 'Z', '20170130180003' => 'A'] + ); $this->assertEquals( ['20170130180000', '20170130180001', '20170130180002', '20170130180003'], - $this->migrationService->getAvailableVersions()); + $this->migrationService->getAvailableVersions() + ); - $this->migrationService->expects($this->exactly(2))->method('executeStep') - ->withConsecutive(['20170130180002'], ['20170130180003']); + $calls = [ + ['20170130180002', false], + ['20170130180003', false], + ]; + $this->migrationService->expects($this->exactly(2)) + ->method('executeStep') + ->willReturnCallback(function () use (&$calls): void { + $expected = array_shift($calls); + $this->assertEquals($expected, func_get_args()); + }); $this->migrationService->migrate(); } - public function testEnsureOracleConstraintsValid() { + public function testEnsureOracleConstraintsValid(): void { $column = $this->createMock(Column::class); $column->expects($this->once()) ->method('getName') @@ -282,7 +304,7 @@ class MigrationsTest extends \Test\TestCase { self::invokePrivate($this->migrationService, 'ensureOracleConstraints', [$sourceSchema, $schema, 3]); } - public function testEnsureOracleConstraintsValidWithPrimaryKey() { + public function testEnsureOracleConstraintsValidWithPrimaryKey(): void { $index = $this->createMock(Index::class); $index->expects($this->any()) ->method('getName') @@ -325,11 +347,11 @@ class MigrationsTest extends \Test\TestCase { self::invokePrivate($this->migrationService, 'ensureOracleConstraints', [$sourceSchema, $schema, 3]); } - public function testEnsureOracleConstraintsValidWithPrimaryKeyDefault() { + public function testEnsureOracleConstraintsValidWithPrimaryKeyDefault(): void { $defaultName = 'PRIMARY'; - if ($this->db->getDatabasePlatform() instanceof PostgreSqlPlatform) { + if ($this->db->getDatabaseProvider() === IDBConnection::PLATFORM_POSTGRES) { $defaultName = \str_repeat('a', 26) . '_' . \str_repeat('b', 30) . '_seq'; - } elseif ($this->db->getDatabasePlatform() instanceof OraclePlatform) { + } elseif ($this->db->getDatabaseProvider() === IDBConnection::PLATFORM_ORACLE) { $defaultName = \str_repeat('a', 26) . '_seq'; } @@ -379,7 +401,7 @@ class MigrationsTest extends \Test\TestCase { } - public function testEnsureOracleConstraintsTooLongTableName() { + public function testEnsureOracleConstraintsTooLongTableName(): void { $this->expectException(\InvalidArgumentException::class); $table = $this->createMock(Table::class); @@ -404,13 +426,13 @@ class MigrationsTest extends \Test\TestCase { } - public function testEnsureOracleConstraintsTooLongPrimaryWithDefault() { + public function testEnsureOracleConstraintsTooLongPrimaryWithDefault(): void { $this->expectException(\InvalidArgumentException::class); $defaultName = 'PRIMARY'; - if ($this->db->getDatabasePlatform() instanceof PostgreSqlPlatform) { + if ($this->db->getDatabaseProvider() === IDBConnection::PLATFORM_POSTGRES) { $defaultName = \str_repeat('a', 27) . '_' . \str_repeat('b', 30) . '_seq'; - } elseif ($this->db->getDatabasePlatform() instanceof OraclePlatform) { + } elseif ($this->db->getDatabaseProvider() === IDBConnection::PLATFORM_ORACLE) { $defaultName = \str_repeat('a', 27) . '_seq'; } @@ -457,7 +479,7 @@ class MigrationsTest extends \Test\TestCase { } - public function testEnsureOracleConstraintsTooLongPrimaryWithName() { + public function testEnsureOracleConstraintsTooLongPrimaryWithName(): void { $this->expectException(\InvalidArgumentException::class); $index = $this->createMock(Index::class); @@ -500,7 +522,7 @@ class MigrationsTest extends \Test\TestCase { } - public function testEnsureOracleConstraintsTooLongColumnName() { + public function testEnsureOracleConstraintsTooLongColumnName(): void { $this->expectException(\InvalidArgumentException::class); $column = $this->createMock(Column::class); @@ -534,7 +556,7 @@ class MigrationsTest extends \Test\TestCase { } - public function testEnsureOracleConstraintsTooLongIndexName() { + public function testEnsureOracleConstraintsTooLongIndexName(): void { $this->expectException(\InvalidArgumentException::class); $index = $this->createMock(Index::class); @@ -571,7 +593,7 @@ class MigrationsTest extends \Test\TestCase { } - public function testEnsureOracleConstraintsTooLongForeignKeyName() { + public function testEnsureOracleConstraintsTooLongForeignKeyName(): void { $this->expectException(\InvalidArgumentException::class); $foreignKey = $this->createMock(ForeignKeyConstraint::class); @@ -611,7 +633,7 @@ class MigrationsTest extends \Test\TestCase { } - public function testEnsureOracleConstraintsNoPrimaryKey() { + public function testEnsureOracleConstraintsNoPrimaryKey(): void { $this->markTestSkipped('Test disabled for now due to multiple reasons, see https://github.com/nextcloud/server/pull/31580#issuecomment-1069182234 for details.'); $this->expectException(\InvalidArgumentException::class); @@ -652,7 +674,7 @@ class MigrationsTest extends \Test\TestCase { } - public function testEnsureOracleConstraintsTooLongSequenceName() { + public function testEnsureOracleConstraintsTooLongSequenceName(): void { $this->expectException(\InvalidArgumentException::class); $sequence = $this->createMock(Sequence::class); @@ -680,7 +702,7 @@ class MigrationsTest extends \Test\TestCase { } - public function testEnsureOracleConstraintsBooleanNotNull() { + public function testEnsureOracleConstraintsBooleanNotNull(): void { $this->expectException(\InvalidArgumentException::class); $column = $this->createMock(Column::class); @@ -720,7 +742,7 @@ class MigrationsTest extends \Test\TestCase { } - public function testEnsureOracleConstraintsStringLength4000() { + public function testEnsureOracleConstraintsStringLength4000(): void { $this->expectException(\InvalidArgumentException::class); $column = $this->createMock(Column::class); @@ -758,4 +780,162 @@ class MigrationsTest extends \Test\TestCase { self::invokePrivate($this->migrationService, 'ensureOracleConstraints', [$sourceSchema, $schema, 3]); } + + + public function testExtractMigrationAttributes(): void { + $metadataManager = Server::get(MetadataManager::class); + $this->appManager->loadApp('testing'); + + $this->assertEquals($this->getMigrationMetadata(), json_decode(json_encode($metadataManager->extractMigrationAttributes('testing')), true)); + + $this->appManager->disableApp('testing'); + } + + public function testDeserializeMigrationMetadata(): void { + $metadataManager = Server::get(MetadataManager::class); + $this->assertEquals( + [ + 'core' => [], + 'apps' => [ + 'testing' => [ + '30000Date20240102030405' => [ + new DropTable('old_table'), + new CreateTable('new_table', + description: 'Table is used to store things, but also to get more things', + notes: ['this is a notice', 'and another one, if really needed'] + ), + new AddColumn('my_table'), + new AddColumn('my_table', 'another_field'), + new AddColumn('other_table', 'last_one', ColumnType::DATE), + new AddIndex('my_table'), + new AddIndex('my_table', IndexType::PRIMARY), + new DropColumn('other_table'), + new DropColumn('other_table', 'old_column', + description: 'field is not used anymore and replaced by \'last_one\'' + ), + new DropIndex('other_table'), + new ModifyColumn('other_table'), + new ModifyColumn('other_table', 'this_field'), + new ModifyColumn('other_table', 'this_field', ColumnType::BIGINT) + ] + ] + ] + ], + $metadataManager->getMigrationsAttributesFromReleaseMetadata( + [ + 'core' => [], + 'apps' => ['testing' => $this->getMigrationMetadata()] + ] + ) + ); + } + + private function getMigrationMetadata(): array { + return [ + '30000Date20240102030405' => [ + [ + 'class' => 'OCP\\Migration\\Attributes\\DropTable', + 'table' => 'old_table', + 'description' => '', + 'notes' => [], + 'columns' => [] + ], + [ + 'class' => 'OCP\\Migration\\Attributes\\CreateTable', + 'table' => 'new_table', + 'description' => 'Table is used to store things, but also to get more things', + 'notes' => [ + 'this is a notice', + 'and another one, if really needed' + ], + 'columns' => [] + ], + [ + 'class' => 'OCP\\Migration\\Attributes\\AddColumn', + 'table' => 'my_table', + 'description' => '', + 'notes' => [], + 'name' => '', + 'type' => '' + ], + [ + 'class' => 'OCP\\Migration\\Attributes\\AddColumn', + 'table' => 'my_table', + 'description' => '', + 'notes' => [], + 'name' => 'another_field', + 'type' => '' + ], + [ + 'class' => 'OCP\\Migration\\Attributes\\AddColumn', + 'table' => 'other_table', + 'description' => '', + 'notes' => [], + 'name' => 'last_one', + 'type' => 'date' + ], + [ + 'class' => 'OCP\\Migration\\Attributes\\AddIndex', + 'table' => 'my_table', + 'description' => '', + 'notes' => [], + 'type' => '' + ], + [ + 'class' => 'OCP\\Migration\\Attributes\\AddIndex', + 'table' => 'my_table', + 'description' => '', + 'notes' => [], + 'type' => 'primary' + ], + [ + 'class' => 'OCP\\Migration\\Attributes\\DropColumn', + 'table' => 'other_table', + 'description' => '', + 'notes' => [], + 'name' => '', + 'type' => '' + ], + [ + 'class' => 'OCP\\Migration\\Attributes\\DropColumn', + 'table' => 'other_table', + 'description' => 'field is not used anymore and replaced by \'last_one\'', + 'notes' => [], + 'name' => 'old_column', + 'type' => '' + ], + [ + 'class' => 'OCP\\Migration\\Attributes\\DropIndex', + 'table' => 'other_table', + 'description' => '', + 'notes' => [], + 'type' => '' + ], + [ + 'class' => 'OCP\\Migration\\Attributes\\ModifyColumn', + 'table' => 'other_table', + 'description' => '', + 'notes' => [], + 'name' => '', + 'type' => '' + ], + [ + 'class' => 'OCP\\Migration\\Attributes\\ModifyColumn', + 'table' => 'other_table', + 'description' => '', + 'notes' => [], + 'name' => 'this_field', + 'type' => '' + ], + [ + 'class' => 'OCP\\Migration\\Attributes\\ModifyColumn', + 'table' => 'other_table', + 'description' => '', + 'notes' => [], + 'name' => 'this_field', + 'type' => 'bigint' + ], + ] + ]; + } } |