Skip to content

Commit

Permalink
Remove src\TestSupport\TestSchemaTrait from yiisoft\db. (#180)
Browse files Browse the repository at this point in the history
* Remove src\TestSupport\TestSchemaTrait from yiisoft\db.
  • Loading branch information
terabytesoftw committed Dec 16, 2022
1 parent 94498ba commit f0583f4
Show file tree
Hide file tree
Showing 4 changed files with 647 additions and 533 deletions.
117 changes: 69 additions & 48 deletions src/Schema.php
Expand Up @@ -23,11 +23,11 @@
use function array_values;
use function bindec;
use function explode;
use function ksort;
use function md5;
use function preg_match;
use function preg_match_all;
use function serialize;
use function str_replace;
use function stripos;
use function strtolower;
use function trim;
Expand Down Expand Up @@ -178,6 +178,8 @@ public function findUniqueIndexes(TableSchemaInterface $table): array
}
}

ksort($uniqueIndexes);

return $uniqueIndexes;
}

Expand Down Expand Up @@ -237,6 +239,7 @@ protected function findColumns(TableSchemaInterface $table): bool
* @param TableSchemaInterface $table the table metadata.
*
* @throws Exception
* @throws InvalidConfigException
* @throws Throwable
*/
protected function findConstraints(TableSchemaInterface $table): void
Expand All @@ -260,64 +263,56 @@ protected function findConstraints(TableSchemaInterface $table): void
`kcu`.`CONSTRAINT_NAME` = `rc`.`CONSTRAINT_NAME` AND
`kcu`.`TABLE_SCHEMA` = `rc`.`CONSTRAINT_SCHEMA` AND
`kcu`.`TABLE_NAME` = `rc`.`TABLE_NAME`
WHERE
`rc`.`CONSTRAINT_SCHEMA` = COALESCE(:schemaName, DATABASE()) AND
`rc`.`TABLE_NAME` = :tableName
WHERE `rc`.`CONSTRAINT_SCHEMA` = COALESCE(:schemaName, DATABASE()) AND `rc`.`TABLE_NAME` = :tableName
SQL;

try {
$rows = $this->db->createCommand($sql, [
':schemaName' => $table->getSchemaName(),
':tableName' => $table->getName(),
])->queryAll();

$constraints = [];
$constraints = [];
$rows = $this->db->createCommand($sql, [
':schemaName' => $table->getSchemaName(),
':tableName' => $table->getName(),
])->queryAll();

/** @psalm-var RowConstraint $row */
foreach ($rows as $row) {
$constraints[$row['constraint_name']]['referenced_table_name'] = $row['referenced_table_name'];
$constraints[$row['constraint_name']]['columns'][$row['column_name']] = $row['referenced_column_name'];
}
/** @psalm-var RowConstraint $row */
foreach ($rows as $row) {
$constraints[$row['constraint_name']]['referenced_table_name'] = $row['referenced_table_name'];
$constraints[$row['constraint_name']]['columns'][$row['column_name']] = $row['referenced_column_name'];
}

$table->foreignKeys([]);
$table->foreignKeys([]);

/**
* @var array{referenced_table_name: string, columns: array} $constraint
*/
foreach ($constraints as $name => $constraint) {
$table->foreignKey($name, array_merge(
/**
* @var array{referenced_table_name: string, columns: array} $constraint
*/
foreach ($constraints as $name => $constraint) {
$table->foreignKey(
$name,
array_merge(
[$constraint['referenced_table_name']],
$constraint['columns']
));
}
} catch (Exception $e) {
$previous = $e->getPrevious();

if ($previous === null || !str_contains($previous->getMessage(), 'SQLSTATE[42S02')) {
throw $e;
}

// table does not exist, try to determine the foreign keys using the table creation sql
$sql = $this->getCreateTableSql($table);
$regexp = '/FOREIGN KEY\s+\(([^)]+)\)\s+REFERENCES\s+([^(^\s]+)\s*\(([^)]+)\)/mi';

if (preg_match_all($regexp, $sql, $matches, PREG_SET_ORDER)) {
foreach ($matches as $match) {
$fks = array_map('trim', explode(',', str_replace('`', '', $match[1])));
$pks = array_map('trim', explode(',', str_replace('`', '', $match[3])));
$constraint = [str_replace('`', '', $match[2])];
),
);
}
}

foreach ($fks as $k => $name) {
$constraint[$name] = $pks[$k];
}
/**
* @throws Exception
* @throws InvalidConfigException
* @throws Throwable
*/
protected function findSchemaNames(): array
{
$sql = <<<SQL
SELECT schema_name FROM information_schema.schemata WHERE schema_name NOT IN ('information_schema', 'mysql', 'performance_schema', 'sys')
SQL;

$table->foreignKey(md5(serialize($constraint)), $constraint);
}
$table->foreignKeys(array_values($table->getForeignKeys()));
}
}
return $this->db->createCommand($sql)->queryColumn();
}

/**
* @throws Exception
* @throws InvalidConfigException
* @throws Throwable
*/
protected function findTableComment(TableSchemaInterface $tableSchema): void
{
$sql = <<<SQL
Expand Down Expand Up @@ -361,6 +356,32 @@ protected function findTableNames(string $schema = ''): array
return $this->db->createCommand($sql)->queryColumn();
}

/**
* @throws Exception
* @throws InvalidConfigException
* @throws Throwable
*/
protected function findViewNames(string $schema = ''): array
{
$sql = match ($schema) {
'' => <<<SQL
SELECT table_name as view FROM information_schema.tables WHERE table_type LIKE 'VIEW' AND table_schema != 'sys'
SQL,
default => <<<SQL
SELECT table_name as view FROM information_schema.tables WHERE table_type LIKE 'VIEW' AND table_schema = '$schema'
SQL,
};

/** @psalm-var string[][] $views */
$views = $this->db->createCommand($sql)->queryAll();

foreach ($views as $key => $view) {
$views[$key] = $view['view'];
}

return $views;
}

/**
* Returns the cache key for the specified table name.
*
Expand Down

0 comments on commit f0583f4

Please sign in to comment.