Unverified Commit 3edde1fc authored by alexpott's avatar alexpott

Issue #2881522 by mondrake, tstoeckler, daffie, alexpott, amateescu, catch:...

Issue #2881522 by mondrake, tstoeckler, daffie, alexpott, amateescu, catch: Add a Schema::findPrimaryKeyColumns method to remove database specific logic from test
parent f9f81a30
......@@ -522,6 +522,17 @@ public function dropPrimaryKey($table) {
return TRUE;
}
/**
* {@inheritdoc}
*/
protected function findPrimaryKeyColumns($table) {
if (!$this->tableExists($table)) {
return FALSE;
}
$result = $this->connection->query("SHOW KEYS FROM {" . $table . "} WHERE Key_name = 'PRIMARY'")->fetchAllAssoc('Column_name');
return array_keys($result);
}
/**
* {@inheritdoc}
*/
......
......@@ -715,6 +715,29 @@ public function dropPrimaryKey($table) {
return TRUE;
}
/**
* {@inheritdoc}
*/
protected function findPrimaryKeyColumns($table) {
if (!$this->tableExists($table)) {
return FALSE;
}
// Fetch the 'indkey' column from 'pg_index' to figure out the order of the
// primary key.
// @todo Use 'array_position()' to be able to perform the ordering in SQL
// directly when 9.5 is the minimum PostgreSQL version.
$result = $this->connection->query("SELECT a.attname, i.indkey FROM pg_index i JOIN pg_attribute a ON a.attrelid = i.indrelid AND a.attnum = ANY(i.indkey) WHERE i.indrelid = '{" . $table . "}'::regclass AND i.indisprimary")->fetchAllKeyed();
if (!$result) {
return [];
}
$order = explode(' ', reset($result));
$columns = array_combine($order, array_keys($result));
ksort($columns);
return array_values($columns);
}
/**
* {@inheritdoc}
*/
......
......@@ -501,14 +501,21 @@ protected function introspectSchema($table) {
if ($length) {
$schema['fields'][$row->name]['length'] = $length;
}
// $row->pk contains a number that reflects the primary key order. We
// use that as the key and sort (by key) below to return the primary key
// in the same order that it is stored in.
if ($row->pk) {
$schema['primary key'][] = $row->name;
$schema['primary key'][$row->pk] = $row->name;
}
}
else {
throw new \Exception("Unable to parse the column type " . $row->type);
}
}
ksort($schema['primary key']);
// Re-key the array because $row->pk starts counting at 1.
$schema['primary key'] = array_values($schema['primary key']);
$indexes = [];
$result = $this->connection->query('PRAGMA ' . $info['schema'] . '.index_list(' . $info['table'] . ')');
foreach ($result as $row) {
......@@ -741,6 +748,17 @@ public function dropPrimaryKey($table) {
return TRUE;
}
/**
* {@inheritdoc}
*/
protected function findPrimaryKeyColumns($table) {
if (!$this->tableExists($table)) {
return FALSE;
}
$schema = $this->introspectSchema($table);
return $schema['primary key'];
}
/**
* {@inheritdoc}
*/
......
......@@ -408,6 +408,26 @@ public function fieldExists($table, $column) {
*/
abstract public function dropPrimaryKey($table);
/**
* Finds the primary key columns of a table, from the database.
*
* @param string $table
* The name of the table.
*
* @return string[]|false
* A simple array with the names of the columns composing the table's
* primary key, or FALSE if the table does not exist.
*
* @throws \RuntimeException
* If the driver does not override this method.
*/
protected function findPrimaryKeyColumns($table) {
if (!$this->tableExists($table)) {
return FALSE;
}
throw new \RuntimeException("The '" . $this->connection->driver() . "' database driver does not implement " . __METHOD__);
}
/**
* Add a unique key.
*
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment