[DATABASE][MariaDB] Properly account for foreign keys

This commit is contained in:
Alexei Sorokin 2019-09-13 10:34:48 +03:00 committed by Diogo Peralta Cordeiro
parent acd64ba54a
commit cfc93bacbb

View File

@ -148,39 +148,44 @@ class MysqlSchema extends Schema
// INFORMATION_SCHEMA's CONSTRAINTS and KEY_COLUMN_USAGE tables give // INFORMATION_SCHEMA's CONSTRAINTS and KEY_COLUMN_USAGE tables give
// good info on primary and unique keys but don't list ANY info on // good info on primary and unique keys but don't list ANY info on
// multi-value keys, which is lame-o. Sigh. // multi-value keys, which is lame-o. Sigh.
// $keyColumns = $this->fetchMetaInfo($table, 'KEY_COLUMN_USAGE', 'CONSTRAINT_NAME, ORDINAL_POSITION');
// Let's go old school and use SHOW INDEX :D
//
$keyInfo = $this->fetchIndexInfo($table);
$keys = []; $keys = [];
$keyTypes = []; $fkeys = [];
foreach ($keyInfo as $row) {
$name = $row['Key_name'];
$column = $row['Column_name'];
if (!isset($keys[$name])) { foreach ($keyColumns as $row) {
$keys[$name] = []; $keyName = $row['CONSTRAINT_NAME'];
$keyCol = $row['COLUMN_NAME'];
if (!isset($keys[$keyName])) {
$keys[$keyName] = [];
}
$keys[$keyName][] = $keyCol;
if (!is_null($row['REFERENCED_TABLE_NAME'])) {
$fkeys[] = $keyName;
}
} }
$keys[$name][] = $column;
if ($name == 'PRIMARY') { foreach ($keys as $keyName => $cols) {
$type = 'primary key'; if ($keyName === 'PRIMARY') {
} elseif ($row['Non_unique'] == 0) { $def['primary key'] = $cols;
$type = 'unique keys'; } elseif (in_array($keyName, $fkeys)) {
} elseif ($row['Index_type'] === 'FULLTEXT') { $fkey = $this->fetchForeignKeyInfo($table, $keyName);
$type = 'fulltext indexes'; $colMap = array_combine($cols, $fkey['cols']);
$def['foreign keys'][$keyName] = [$fkey['table_name'], $colMap];
} else { } else {
$type = 'indexes'; $def['unique keys'][$keyName] = $cols;
} }
$keyTypes[$name] = $type;
} }
foreach ($keyTypes as $name => $type) { $indexInfo = $this->fetchIndexInfo($table);
if ($type == 'primary key') {
// there can be only one foreach ($indexInfo as $row) {
$def[$type] = $keys[$name]; $keyName = $row['key_name'];
$cols = $row['cols'];
if ($row['key_type'] === 'FULLTEXT') {
$def['fulltext indexes'][$keyName] = $cols;
} else { } else {
$def[$type][$name] = $keys[$name]; $def['indexes'][$keyName] = $cols;
} }
} }
} }
@ -229,17 +234,65 @@ class MysqlSchema extends Schema
} }
/** /**
* Pull 'SHOW INDEX' data for the given table. * Pull index and keys information for the given table.
* *
* @param string $table * @param string $table
* @return array of arrays * @return array of arrays
* @throws PEAR_Exception * @throws PEAR_Exception
*/ */
public function fetchIndexInfo($table) public function fetchIndexInfo(string $table): array
{ {
$query = "SHOW INDEX FROM `%s`"; $query = 'SELECT INDEX_NAME AS `key_name`, INDEX_TYPE AS `key_type`, COLUMN_NAME AS `col` ' .
$sql = sprintf($query, $table); 'FROM INFORMATION_SCHEMA.STATISTICS ' .
return $this->fetchQueryData($sql); 'WHERE TABLE_SCHEMA = \'%s\' AND TABLE_NAME = \'%s\' AND NON_UNIQUE = TRUE ' .
'AND INDEX_NAME NOT IN (SELECT CONSTRAINT_NAME FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE WHERE REFERENCED_TABLE_NAME IS NOT NULL) ' .
'ORDER BY SEQ_IN_INDEX';
$schema = $this->conn->dsn['database'];
$sql = sprintf($query, $schema, $table);
$data = $this->fetchQueryData($sql);
$rows = [];
foreach ($data as $row) {
$name = $row['key_name'];
if (isset($rows[$name])) {
$rows[$name]['cols'][] = $row['col'];
} else {
$row['cols'] = [$row['col']];
unset($row['col']);
$rows[$name] = $row;
}
}
return array_values($rows);
}
/**
* @param string $table
* @param string $constraint_name
* @return array array of rows with keys: table_name, cols (array of strings)
* @throws PEAR_Exception
*/
public function fetchForeignKeyInfo(string $table, string $constraint_name): array
{
$query = 'SELECT REFERENCED_TABLE_NAME AS `table_name`, REFERENCED_COLUMN_NAME AS `col` ' .
'FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE ' .
'WHERE TABLE_SCHEMA = \'%s\' AND TABLE_NAME = \'%s\' AND CONSTRAINT_NAME = \'%s\' ' .
'AND REFERENCED_TABLE_SCHEMA IS NOT NULL ' .
'ORDER BY POSITION_IN_UNIQUE_CONSTRAINT';
$schema = $this->conn->dsn['database'];
$sql = sprintf($query, $schema, $table, $constraint_name);
$data = $this->fetchQueryData($sql);
if (count($data) < 1) {
throw new Exception('Could not find foreign key ' . $constraint_name . ' on table ' . $table);
}
$info = [
'table_name' => $data[0]['table_name'],
'cols' => [],
];
foreach ($data as $row) {
$info['cols'][] = $row['col'];
}
return $info;
} }
/** /**