diff --git a/CHANGELOG.md b/CHANGELOG.md index 236b97905..c496b58a0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Yii Database Change Log +## 3.0.0 under development + +- Chg #1175: Remove `getSchemaChecks()`, `getSchemaDefaultValues()`, `getSchemaIndexes()`, `getSchemaPrimaryKeys()` + and `getSchemaUniques()` methods from `ConstraintSchemaInterface` and `AbstractSchema` (@KalimeroMK) + ## 2.0.2 under development - Enh #1172: Refactor `Query::queryScalar()` to use a cloned `Query` object (@darkspock) diff --git a/src/Constraint/ConstraintSchemaInterface.php b/src/Constraint/ConstraintSchemaInterface.php index d5644a4e9..687f16861 100644 --- a/src/Constraint/ConstraintSchemaInterface.php +++ b/src/Constraint/ConstraintSchemaInterface.php @@ -15,34 +15,6 @@ */ interface ConstraintSchemaInterface { - /** - * Returns check constraints for all tables in the database. - * - * @param string $schema The schema of the tables. Defaults to empty string, meaning the current or default schema - * name. - * @param bool $refresh Whether to fetch the latest available table schemas. If this is `false`, cached data may be - * returned if available. - * - * @return Check[][] The check constraints for all tables in the database, indexed by table name. - * - * @psalm-return array - */ - public function getSchemaChecks(string $schema = '', bool $refresh = false): array; - - /** - * Returns default value constraints for all tables in the database. - * - * @param string $schema The schema of the tables. Defaults to empty string, meaning the current or default schema - * name. - * @param bool $refresh Whether to fetch the latest available table schemas. If this is `false`, cached data may be - * returned if available. - * - * @return DefaultValue[][] The default value constraints for all tables in the database, indexed by table name. - * - * @psalm-return array - */ - public function getSchemaDefaultValues(string $schema = '', bool $refresh = false): array; - /** * Returns foreign keys for all tables in the database. * @@ -58,49 +30,6 @@ public function getSchemaDefaultValues(string $schema = '', bool $refresh = fals */ public function getSchemaForeignKeys(string $schema = '', bool $refresh = false): array; - /** - * Returns indexes for all tables in the database. - * - * @param string $schema The schema of the tables. Defaults to empty string, meaning the current or default schema - * name. - * @param bool $refresh Whether to fetch the latest available table schemas. If this is false, cached data may be - * returned if available. - * - * @return Index[][] The indexes for all tables in the database, indexed by table name. - * - * @psalm-return array - */ - public function getSchemaIndexes(string $schema = '', bool $refresh = false): array; - - /** - * Returns primary keys for all tables in the database. - * - * @param string $schema The schema of the tables. Defaults to empty string, meaning the current or default schema - * name. - * @param bool $refresh Whether to fetch the latest available table schemas. If this is `false`, cached data may be - * returned if available. - * - * @return Index[] The primary keys for all tables in the database, indexed by table name. Tables without a primary - * key are omitted. - * - * @psalm-return array - */ - public function getSchemaPrimaryKeys(string $schema = '', bool $refresh = false): array; - - /** - * Returns unique constraints for all tables in the database. - * - * @param string $schema The schema of the tables. Defaults to empty string, meaning the current or default schema - * name. - * @param bool $refresh Whether to fetch the latest available table schemas. If this is `false`, cached data may be - * returned if available. - * - * @return Index[][] The unique constraints for all tables in the database, indexed by table name. - * - * @psalm-return array - */ - public function getSchemaUniques(string $schema = '', bool $refresh = false): array; - /** * Obtains the check constraints' information for the named table. * diff --git a/src/Schema/AbstractSchema.php b/src/Schema/AbstractSchema.php index 335fcc425..3dff186e7 100644 --- a/src/Schema/AbstractSchema.php +++ b/src/Schema/AbstractSchema.php @@ -108,30 +108,12 @@ final public function getResultColumn(array $metadata): ?ColumnInterface return $column; } - public function getSchemaChecks(string $schema = '', bool $refresh = false): array - { - /** @var array */ - return $this->getSchemaMetadata($schema, SchemaInterface::CHECKS, $refresh); - } - - public function getSchemaDefaultValues(string $schema = '', bool $refresh = false): array - { - /** @var array */ - return $this->getSchemaMetadata($schema, SchemaInterface::DEFAULT_VALUES, $refresh); - } - public function getSchemaForeignKeys(string $schema = '', bool $refresh = false): array { /** @var array */ return $this->getSchemaMetadata($schema, SchemaInterface::FOREIGN_KEYS, $refresh); } - public function getSchemaIndexes(string $schema = '', bool $refresh = false): array - { - /** @var array */ - return $this->getSchemaMetadata($schema, SchemaInterface::INDEXES, $refresh); - } - public function getSchemaNames(bool $refresh = false): array { if (empty($this->schemaNames) || $refresh) { @@ -141,18 +123,6 @@ public function getSchemaNames(bool $refresh = false): array return $this->schemaNames; } - public function getSchemaPrimaryKeys(string $schema = '', bool $refresh = false): array - { - /** @var array */ - return $this->getSchemaMetadata($schema, SchemaInterface::PRIMARY_KEY, $refresh); - } - - public function getSchemaUniques(string $schema = '', bool $refresh = false): array - { - /** @var array */ - return $this->getSchemaMetadata($schema, SchemaInterface::UNIQUES, $refresh); - } - public function getTableChecks(string $name, bool $refresh = false): array { /** @var Check[] */ @@ -407,10 +377,11 @@ protected function findTableNames(string $schema): array * @param bool $refresh Whether to fetch the latest available table metadata. If this is `false`, cached data may be * returned if available. * - * @return Check[][]|DefaultValue[][]|ForeignKey[][]|Index[]|Index[][]|TableSchemaInterface[] The metadata of the given type for all + * @return ForeignKey[][]|TableSchemaInterface[] The metadata of the given type for all * tables in the given schema, indexed by table name. * - * @psalm-return array + * @psalm-param SchemaInterface::FOREIGN_KEYS|SchemaInterface::SCHEMA $type + * @psalm-return array */ protected function getSchemaMetadata(string $schema, string $type, bool $refresh): array { @@ -492,21 +463,16 @@ protected function loadTableTypeMetadata(string $type, string $name): array|Tabl /** * This method returns the desired metadata type for table name (with refresh if needed). * - * @return Check[]|DefaultValue[]|ForeignKey[]|Index|Index[]|TableSchemaInterface|null + * @return ForeignKey[]|TableSchemaInterface|null */ protected function getTableTypeMetadata( string $type, string $name, bool $refresh = false, - ): array|Index|TableSchemaInterface|null { + ): array|TableSchemaInterface|null { return match ($type) { SchemaInterface::SCHEMA => $this->getTableSchema($name, $refresh), - SchemaInterface::PRIMARY_KEY => $this->getTablePrimaryKey($name, $refresh), - SchemaInterface::UNIQUES => $this->getTableUniques($name, $refresh), SchemaInterface::FOREIGN_KEYS => $this->getTableForeignKeys($name, $refresh), - SchemaInterface::INDEXES => $this->getTableIndexes($name, $refresh), - SchemaInterface::DEFAULT_VALUES => $this->getTableDefaultValues($name, $refresh), - SchemaInterface::CHECKS => $this->getTableChecks($name, $refresh), default => null, }; } diff --git a/tests/Common/CommonSchemaTest.php b/tests/Common/CommonSchemaTest.php index 5b56a4bf2..82d76864f 100644 --- a/tests/Common/CommonSchemaTest.php +++ b/tests/Common/CommonSchemaTest.php @@ -161,40 +161,6 @@ public function testGetNonExistingTableSchema(): void $this->assertNull($schema->getTableSchema('nonexisting_table')); } - public function testGetSchemaChecks(): void - { - $this->loadFixture(); - - $schema = $this->getSharedConnection()->getSchema(); - $tableChecks = $schema->getSchemaChecks(); - $tableNames = $schema->getTableNames(); - - $this->assertIsArray($tableChecks); - - foreach ($tableChecks as $tableName => $checks) { - $this->assertContains($tableName, $tableNames); - $this->assertIsArray($checks); - $this->assertContainsOnlyInstancesOf(Check::class, $checks); - } - } - - public function testGetSchemaDefaultValues(): void - { - $this->loadFixture(); - - $schema = $this->getSharedConnection()->getSchema(); - $tableDefaultValues = $schema->getSchemaDefaultValues(); - $tableNames = $schema->getTableNames(); - - $this->assertIsArray($tableDefaultValues); - - foreach ($tableDefaultValues as $tableName => $defaultValues) { - $this->assertContains($tableName, $tableNames); - $this->assertIsArray($defaultValues); - $this->assertContainsOnlyInstancesOf(DefaultValue::class, $defaultValues); - } - } - public function testGetSchemaForeignKeys(): void { $this->loadFixture(); @@ -223,60 +189,6 @@ public function testGetSchemaForeignKeys(): void } } - public function testGetSchemaIndexes(): void - { - $this->loadFixture(); - - $schema = $this->getSharedConnection()->getSchema(); - $tableIndexes = $schema->getSchemaIndexes(); - $tableNames = $schema->getTableNames(); - - $this->assertNotEmpty($tableIndexes); - - $this->assertIsArray($tableIndexes); - - foreach ($tableIndexes as $tableName => $indexes) { - $this->assertContains($tableName, $tableNames); - $this->assertIsArray($indexes); - $this->assertContainsOnlyInstancesOf(Index::class, $indexes); - } - } - - public function testGetSchemaPrimaryKeys(): void - { - $this->loadFixture(); - - $schema = $this->getSharedConnection()->getSchema(); - $tablePks = $schema->getSchemaPrimaryKeys(); - $tableNames = $schema->getTableNames(); - - $this->assertNotEmpty($tablePks); - - $this->assertIsArray($tablePks); - $this->assertContainsOnlyInstancesOf(Index::class, $tablePks); - - foreach (array_keys($tablePks) as $tableName) { - $this->assertContains($tableName, $tableNames); - } - } - - public function testGetSchemaUniques(): void - { - $this->loadFixture(); - - $schema = $this->getSharedConnection()->getSchema(); - $tableUniques = $schema->getSchemaUniques(); - $tableNames = $schema->getTableNames(); - - $this->assertIsArray($tableUniques); - - foreach ($tableUniques as $tableName => $uniques) { - $this->assertContains($tableName, $tableNames); - $this->assertIsArray($uniques); - $this->assertContainsOnlyInstancesOf(Index::class, $uniques); - } - } - public function testGetTableChecks(): void { $db = $this->getSharedConnection(); diff --git a/tests/Db/Schema/SchemaTest.php b/tests/Db/Schema/SchemaTest.php index 37856b865..90a15d0b2 100644 --- a/tests/Db/Schema/SchemaTest.php +++ b/tests/Db/Schema/SchemaTest.php @@ -42,77 +42,68 @@ public function testFindViewNames(): void $this->assertSame([], Assert::invokeMethod($schema, 'findViewNames', ['dbo'])); } - public function testGetSchemaChecks(): void + public function testGetSchemaForeignKeys(): void { $db = $this->getSharedConnection(); - $checks = [new Check('check_1', ['col1', 'col2'], 'col1 > col2')]; + $foreignKeys = [new ForeignKey( + 'CN_constraints_3', + ['C_fk_id_1, C_fk_id_2'], + 'dev', + 'T_constraints_2', + ['C_id_1', 'C_id_2'], + )]; $schemaMock = $this->getMockBuilder(Schema::class) - ->onlyMethods(['findTableNames', 'loadTableChecks']) + ->onlyMethods(['findTableNames', 'loadTableForeignKeys']) ->setConstructorArgs([$db, TestHelper::createMemorySchemaCache()]) ->getMock(); $schemaMock->expects($this->once())->method('findTableNames')->willReturn(['T_constraints_1']); - $schemaMock->expects($this->once())->method('loadTableChecks')->willReturn($checks); - $tableChecks = $schemaMock->getSchemaChecks(); + $schemaMock->expects($this->once())->method('loadTableForeignKeys')->willReturn($foreignKeys); + $tableForeignKeys = $schemaMock->getSchemaForeignKeys(); - $this->assertSame(['T_constraints_1' => $checks], $tableChecks); + $this->assertSame(['T_constraints_1' => $foreignKeys], $tableForeignKeys); } - public function testGetSchemaDefaultValues(): void + public function testGetTableChecks(): void { $db = $this->getSharedConnection(); - $defaultValues = [new DefaultValue('DF__T_constra__C_def__6203C3C6', ['C_default'], '((0))')]; + $checks = [new Check('check_1', ['col1', 'col2'], 'col1 > col2')]; $schemaMock = $this->getMockBuilder(Schema::class) - ->onlyMethods(['findTableNames', 'loadTableDefaultValues']) + ->onlyMethods(['loadTableChecks']) ->setConstructorArgs([$db, TestHelper::createMemorySchemaCache()]) ->getMock(); - $schemaMock->expects($this->once())->method('findTableNames')->willReturn(['T_constraints_1']); - $schemaMock->expects($this->once())->method('loadTableDefaultValues')->willReturn($defaultValues); - $tableDefaultValues = $schemaMock->getSchemaDefaultValues(); + $schemaMock->expects($this->once())->method('loadTableChecks')->willReturn($checks); - $this->assertSame(['T_constraints_1' => $defaultValues], $tableDefaultValues); + $this->assertSame($checks, $schemaMock->getTableChecks('T_constraints_1')); } - public function testGetSchemaForeignKeys(): void + public function testGetTableDefaultValues(): void { $db = $this->getSharedConnection(); - $foreignKeys = [new ForeignKey( - 'CN_constraints_3', - ['C_fk_id_1, C_fk_id_2'], - 'dev', - 'T_constraints_2', - ['C_id_1', 'C_id_2'], - )]; + $defaultValues = [new DefaultValue('df_1', ['col1'], 42)]; $schemaMock = $this->getMockBuilder(Schema::class) - ->onlyMethods(['findTableNames', 'loadTableForeignKeys']) + ->onlyMethods(['loadTableDefaultValues']) ->setConstructorArgs([$db, TestHelper::createMemorySchemaCache()]) ->getMock(); - $schemaMock->expects($this->once())->method('findTableNames')->willReturn(['T_constraints_1']); - $schemaMock->expects($this->once())->method('loadTableForeignKeys')->willReturn($foreignKeys); - $tableForeignKeys = $schemaMock->getSchemaForeignKeys(); + $schemaMock->expects($this->once())->method('loadTableDefaultValues')->willReturn($defaultValues); - $this->assertSame(['T_constraints_1' => $foreignKeys], $tableForeignKeys); - // The result is indexed by the name of the table the foreign keys belong to, see https://github.com/yiisoft/db/issues/1176 - $this->assertSame(['T_constraints_1'], array_keys($tableForeignKeys)); - $this->assertSame('T_constraints_2', $tableForeignKeys['T_constraints_1'][0]->foreignTableName); + $this->assertSame($defaultValues, $schemaMock->getTableDefaultValues('T_constraints_1')); } - public function testGetSchemaIndexes(): void + public function testGetTableIndexes(): void { $db = $this->getSharedConnection(); - $indexes = [new Index('PK__T_constr__A9FAE80AC2B18E65', ['"C_id'], true, true)]; + $indexes = [new Index('PK__T_constr__A9FAE80AC2B18E65', ['C_id'], true, true)]; $schemaMock = $this->getMockBuilder(Schema::class) - ->onlyMethods(['findTableNames', 'loadTableIndexes']) + ->onlyMethods(['loadTableIndexes']) ->setConstructorArgs([$db, TestHelper::createMemorySchemaCache()]) ->getMock(); - $schemaMock->expects($this->once())->method('findTableNames')->willReturn(['T_constraints_1']); $schemaMock->expects($this->once())->method('loadTableIndexes')->willReturn($indexes); - $tableIndexes = $schemaMock->getSchemaIndexes(); - $this->assertSame(['T_constraints_1' => $indexes], $tableIndexes); + $this->assertSame($indexes, $schemaMock->getTableIndexes('T_constraints_1')); } public function testGetSchemaNames(): void @@ -146,44 +137,6 @@ public function testHasSchema(): void $this->assertFalse($schema->hasSchema('no_such_schema')); } - public function testGetSchemaPrimaryKeys(): void - { - $db = $this->getSharedConnection(); - - $pksConstraint = new Index('PK__T_constr__A9FAE80AC2B18E65', ['"C_id'], true, true); - $schemaMock = $this->getMockBuilder(Schema::class) - ->onlyMethods(['findTableNames', 'getTablePrimaryKey']) - ->setConstructorArgs([$db, TestHelper::createMemorySchemaCache()]) - ->getMock(); - $schemaMock->expects($this->once())->method('findTableNames')->willReturn(['T_constraints_1']); - $schemaMock->expects($this->once())->method('getTablePrimaryKey')->willReturn($pksConstraint); - $tablePks = $schemaMock->getSchemaPrimaryKeys(); - - $this->assertIsArray($tablePks); - $this->assertContainsOnlyInstancesOf(Index::class, $tablePks); - } - - public function testGetSchemaUniques(): void - { - $db = $this->getSharedConnection(); - - $uniquesConstraint = [new Index('CN_unique', ['C_unique'], true)]; - $schemaMock = $this->getMockBuilder(Schema::class) - ->onlyMethods(['findTableNames', 'getTableUniques']) - ->setConstructorArgs([$db, TestHelper::createMemorySchemaCache()]) - ->getMock(); - $schemaMock->expects($this->once())->method('findTableNames')->willReturn(['T_constraints_1']); - $schemaMock->expects($this->once())->method('getTableUniques')->willReturn($uniquesConstraint); - $tableUniques = $schemaMock->getSchemaUniques(); - - $this->assertIsArray($tableUniques); - - foreach ($tableUniques as $uniques) { - $this->assertIsArray($uniques); - $this->assertContainsOnlyInstancesOf(Index::class, $uniques); - } - } - public function getTableSchema(): void { $db = $this->getSharedConnection();