Skip to content
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
71 changes: 0 additions & 71 deletions src/Constraint/ConstraintSchemaInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, Check[]>
*/
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<string, DefaultValue[]>
*/
public function getSchemaDefaultValues(string $schema = '', bool $refresh = false): array;

/**
* Returns foreign keys for all tables in the database.
*
Expand All @@ -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<string, Index[]>
*/
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<string, Index>
*/
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<string, Index[]>
*/
public function getSchemaUniques(string $schema = '', bool $refresh = false): array;

/**
* Obtains the check constraints' information for the named table.
*
Expand Down
44 changes: 5 additions & 39 deletions src/Schema/AbstractSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,30 +108,12 @@ final public function getResultColumn(array $metadata): ?ColumnInterface
return $column;
}

public function getSchemaChecks(string $schema = '', bool $refresh = false): array
{
/** @var array<string, Check[]> */
return $this->getSchemaMetadata($schema, SchemaInterface::CHECKS, $refresh);
}

public function getSchemaDefaultValues(string $schema = '', bool $refresh = false): array
{
/** @var array<string, DefaultValue[]> */
return $this->getSchemaMetadata($schema, SchemaInterface::DEFAULT_VALUES, $refresh);
}

public function getSchemaForeignKeys(string $schema = '', bool $refresh = false): array
{
/** @var array<string, ForeignKey[]> */
return $this->getSchemaMetadata($schema, SchemaInterface::FOREIGN_KEYS, $refresh);
}

public function getSchemaIndexes(string $schema = '', bool $refresh = false): array
{
/** @var array<string, Index[]> */
return $this->getSchemaMetadata($schema, SchemaInterface::INDEXES, $refresh);
}

public function getSchemaNames(bool $refresh = false): array
{
if (empty($this->schemaNames) || $refresh) {
Expand All @@ -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<string, Index> */
return $this->getSchemaMetadata($schema, SchemaInterface::PRIMARY_KEY, $refresh);
}

public function getSchemaUniques(string $schema = '', bool $refresh = false): array
{
/** @var array<string, Index[]> */
return $this->getSchemaMetadata($schema, SchemaInterface::UNIQUES, $refresh);
}

public function getTableChecks(string $name, bool $refresh = false): array
{
/** @var Check[] */
Expand Down Expand Up @@ -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<string, Check[]|DefaultValue[]|ForeignKey[]|Index|Index[]|TableSchemaInterface>
* @psalm-param SchemaInterface::FOREIGN_KEYS|SchemaInterface::SCHEMA $type
* @psalm-return array<string, ForeignKey[]|TableSchemaInterface>
*/
protected function getSchemaMetadata(string $schema, string $type, bool $refresh): array
{
Expand Down Expand Up @@ -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,
};
}
Expand Down
88 changes: 0 additions & 88 deletions tests/Common/CommonSchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
Loading
Loading