|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Doctrine\DBAL\Tests\Functional\Schema; |
| 6 | + |
| 7 | +use Doctrine\DBAL\Exception; |
| 8 | +use Doctrine\DBAL\Schema\AbstractSchemaManager; |
| 9 | +use Doctrine\DBAL\Schema\Comparator; |
| 10 | +use Doctrine\DBAL\Schema\Table; |
| 11 | +use Doctrine\DBAL\Tests\FunctionalTestCase; |
| 12 | +use Doctrine\DBAL\Types\Types; |
| 13 | + |
| 14 | +use function array_merge; |
| 15 | + |
| 16 | +final class SchemaManagerTest extends FunctionalTestCase |
| 17 | +{ |
| 18 | + private AbstractSchemaManager $schemaManager; |
| 19 | + |
| 20 | + /** @throws Exception */ |
| 21 | + protected function setUp(): void |
| 22 | + { |
| 23 | + $this->schemaManager = $this->connection->createSchemaManager(); |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * @param callable(AbstractSchemaManager):Comparator $comparatorFactory |
| 28 | + * |
| 29 | + * @dataProvider dataEmptyDiffRegardlessOfForeignTableQuotes |
| 30 | + */ |
| 31 | + public function testEmptyDiffRegardlessOfForeignTableQuotes( |
| 32 | + callable $comparatorFactory, |
| 33 | + string $foreignTableName |
| 34 | + ): void { |
| 35 | + if (! $this->connection->getDatabasePlatform()->supportsSchemas()) { |
| 36 | + self::markTestSkipped('Platform does not support schemas.'); |
| 37 | + } |
| 38 | + |
| 39 | + $this->dropAndCreateSchema('other_schema'); |
| 40 | + |
| 41 | + $tableForeign = new Table($foreignTableName); |
| 42 | + $tableForeign->addColumn('id', 'integer'); |
| 43 | + $tableForeign->setPrimaryKey(['id']); |
| 44 | + $this->dropAndCreateTable($tableForeign); |
| 45 | + |
| 46 | + $tableTo = new Table('other_schema.other_table'); |
| 47 | + $tableTo->addColumn('id', 'integer'); |
| 48 | + $tableTo->addColumn('user_id', 'integer'); |
| 49 | + $tableTo->setPrimaryKey(['id']); |
| 50 | + $tableTo->addForeignKeyConstraint($tableForeign, ['user_id'], ['id']); |
| 51 | + $this->dropAndCreateTable($tableTo); |
| 52 | + |
| 53 | + $schemaFrom = $this->schemaManager->introspectSchema(); |
| 54 | + $tableFrom = $schemaFrom->getTable('other_schema.other_table'); |
| 55 | + |
| 56 | + $diff = $comparatorFactory($this->schemaManager)->compareTables($tableFrom, $tableTo); |
| 57 | + self::assertTrue($diff->isEmpty()); |
| 58 | + } |
| 59 | + |
| 60 | + /** @return iterable<mixed[]> */ |
| 61 | + public static function dataEmptyDiffRegardlessOfForeignTableQuotes(): iterable |
| 62 | + { |
| 63 | + foreach (ComparatorTestUtils::comparatorProvider() as $comparatorArguments) { |
| 64 | + foreach ( |
| 65 | + [ |
| 66 | + 'unquoted' => ['other_schema.user'], |
| 67 | + 'partially quoted' => ['other_schema."user"'], |
| 68 | + 'fully quoted' => ['"other_schema"."user"'], |
| 69 | + ] as $testArguments |
| 70 | + ) { |
| 71 | + yield array_merge($comparatorArguments, $testArguments); |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * @param callable(AbstractSchemaManager):Comparator $comparatorFactory |
| 78 | + * |
| 79 | + * @dataProvider dataDropIndexInAnotherSchema |
| 80 | + */ |
| 81 | + public function testDropIndexInAnotherSchema(callable $comparatorFactory, string $tableName): void |
| 82 | + { |
| 83 | + if (! $this->connection->getDatabasePlatform()->supportsSchemas()) { |
| 84 | + self::markTestSkipped('Platform does not support schemas.'); |
| 85 | + } |
| 86 | + |
| 87 | + $this->dropAndCreateSchema('other_schema'); |
| 88 | + $this->dropAndCreateSchema('case'); |
| 89 | + |
| 90 | + $tableFrom = new Table($tableName); |
| 91 | + $tableFrom->addColumn('id', Types::INTEGER); |
| 92 | + $tableFrom->addColumn('name', Types::STRING); |
| 93 | + $tableFrom->addUniqueIndex(['name'], 'some_table_name_unique_index'); |
| 94 | + $this->dropAndCreateTable($tableFrom); |
| 95 | + |
| 96 | + $tableTo = clone $tableFrom; |
| 97 | + $tableTo->dropIndex('some_table_name_unique_index'); |
| 98 | + |
| 99 | + $diff = $comparatorFactory($this->schemaManager)->compareTables($tableFrom, $tableTo); |
| 100 | + self::assertNotFalse($diff); |
| 101 | + |
| 102 | + $this->schemaManager->alterTable($diff); |
| 103 | + $tableFinal = $this->schemaManager->introspectTable($tableName); |
| 104 | + self::assertEmpty($tableFinal->getIndexes()); |
| 105 | + } |
| 106 | + |
| 107 | + /** @return iterable<mixed[]> */ |
| 108 | + public static function dataDropIndexInAnotherSchema(): iterable |
| 109 | + { |
| 110 | + foreach (ComparatorTestUtils::comparatorProvider() as $comparatorScenario => $comparatorArguments) { |
| 111 | + foreach ( |
| 112 | + [ |
| 113 | + 'default schema' => ['some_table'], |
| 114 | + 'unquoted schema' => ['other_schema.some_table'], |
| 115 | + 'quoted schema' => ['"other_schema".some_table'], |
| 116 | + 'reserved schema' => ['case.some_table'], |
| 117 | + ] as $testScenario => $testArguments |
| 118 | + ) { |
| 119 | + yield $comparatorScenario . ' - ' . $testScenario => array_merge($comparatorArguments, $testArguments); |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | +} |
0 commit comments