Browse Source

Add support for an older SQLite in constraints (fixes #14483) (#14497)

tags/2.0.13
Sergey Makinen 7 years ago committed by Alexander Makarov
parent
commit
92d2245ca6
  1. 30
      framework/db/sqlite/Schema.php

30
framework/db/sqlite/Schema.php

@ -399,6 +399,16 @@ class Schema extends \yii\db\Schema
{
$indexes = $this->db->createCommand('PRAGMA INDEX_LIST (' . $this->quoteValue($tableName) . ')')->queryAll();
$indexes = $this->normalizePdoRowKeyCase($indexes, true);
$tableColumns = null;
if (!empty($indexes) && !isset($indexes[0]['origin'])) {
/*
* SQLite may not have an "origin" column in INDEX_LIST
* See https://www.sqlite.org/src/info/2743846cdba572f6
*/
$tableColumns = $this->db->createCommand('PRAGMA TABLE_INFO (' . $this->quoteValue($tableName) . ')')->queryAll();
$tableColumns = $this->normalizePdoRowKeyCase($tableColumns, true);
$tableColumns = ArrayHelper::index($tableColumns, 'cid');
}
$result = [
'primaryKey' => null,
'indexes' => [],
@ -408,6 +418,15 @@ class Schema extends \yii\db\Schema
$columns = $this->db->createCommand('PRAGMA INDEX_INFO (' . $this->quoteValue($index['name']) . ')')->queryAll();
$columns = $this->normalizePdoRowKeyCase($columns, true);
ArrayHelper::multisort($columns, 'seqno', SORT_ASC, SORT_NUMERIC);
if ($tableColumns !== null) {
// SQLite may not have an "origin" column in INDEX_LIST
$index['origin'] = 'c';
if (!empty($columns) && $tableColumns[$columns[0]['cid']]['pk'] > 0) {
$index['origin'] = 'pk';
} elseif ($index['unique'] && $this->isSystemIdentifier($index['name'])) {
$index['origin'] = 'u';
}
}
$result['indexes'][] = new IndexConstraint([
'isPrimary' => $index['origin'] === 'pk',
'isUnique' => (bool) $index['unique'],
@ -430,4 +449,15 @@ class Schema extends \yii\db\Schema
}
return $result[$returnType];
}
/**
* Return whether the specified identifier is a SQLite system identifier.
* @param string $identifier
* @return bool
* @see https://www.sqlite.org/src/artifact/74108007d286232f
*/
private function isSystemIdentifier($identifier)
{
return strpos($identifier, 'sqlite_') === 0;
}
}

Loading…
Cancel
Save