mirror of
https://github.com/vrana/adminer.git
synced 2025-08-08 15:47:00 +02:00
SQLite: Fix changing primary key
This commit is contained in:
@@ -449,21 +449,49 @@ if (isset($_GET["sqlite"]) || isset($_GET["sqlite2"])) {
|
|||||||
}
|
}
|
||||||
$alter = array();
|
$alter = array();
|
||||||
$originals = array();
|
$originals = array();
|
||||||
$primary_key = false;
|
|
||||||
foreach ($fields as $field) {
|
foreach ($fields as $field) {
|
||||||
if ($field[1]) {
|
if ($field[1]) {
|
||||||
if ($field[1][6]) {
|
$alter[] = ($use_all_fields ? $field[1] : "ADD " . implode($field[1]));
|
||||||
$primary_key = true;
|
|
||||||
}
|
|
||||||
$alter[] = ($use_all_fields ? " " : "ADD ") . implode($field[1]);
|
|
||||||
if ($field[0] != "") {
|
if ($field[0] != "") {
|
||||||
$originals[$field[0]] = $field[1][0];
|
$originals[$field[0]] = $field[1][0];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if ($use_all_fields) {
|
if (!$use_all_fields) {
|
||||||
|
foreach ($alter as $val) {
|
||||||
|
if (!queries("ALTER TABLE " . table($table) . " $val")) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if ($table != $name && !queries("ALTER TABLE " . table($table) . " RENAME TO " . table($name))) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
} elseif (!recreate_table($table, $name, $alter, $originals, $foreign)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if ($auto_increment) {
|
||||||
|
queries("UPDATE sqlite_sequence SET seq = $auto_increment WHERE name = " . q($name)); // ignores error
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
function recreate_table($table, $name, $alter, $originals, $foreign, $indexes = array()) {
|
||||||
|
queries("BEGIN");
|
||||||
if ($table != "") {
|
if ($table != "") {
|
||||||
$indexes = array();
|
$primary_key = false;
|
||||||
|
foreach ($alter as $key => $field) {
|
||||||
|
if ($field[6]) {
|
||||||
|
$primary_key = true;
|
||||||
|
}
|
||||||
|
$alter[$key] = " " . implode($field);
|
||||||
|
}
|
||||||
|
$drop_indexes = array();
|
||||||
|
foreach ($indexes as $key => $val) {
|
||||||
|
if ($val[2] == "DROP") {
|
||||||
|
$drop_indexes[$val[1]] = true;
|
||||||
|
unset($indexes[$key]);
|
||||||
|
}
|
||||||
|
}
|
||||||
foreach (indexes($table) as $key_name => $index) {
|
foreach (indexes($table) as $key_name => $index) {
|
||||||
$columns = array();
|
$columns = array();
|
||||||
foreach ($index["columns"] as $key => $column) {
|
foreach ($index["columns"] as $key => $column) {
|
||||||
@@ -473,6 +501,7 @@ if (isset($_GET["sqlite"]) || isset($_GET["sqlite2"])) {
|
|||||||
$columns[] = $originals[$column] . ($index["descs"][$key] ? " DESC" : "");
|
$columns[] = $originals[$column] . ($index["descs"][$key] ? " DESC" : "");
|
||||||
}
|
}
|
||||||
$columns = "(" . implode(", ", $columns) . ")";
|
$columns = "(" . implode(", ", $columns) . ")";
|
||||||
|
if (!$drop_indexes[$key_name]) {
|
||||||
if ($index["type"] != "PRIMARY") {
|
if ($index["type"] != "PRIMARY") {
|
||||||
$indexes[] = array($index["type"], $key_name, $columns);
|
$indexes[] = array($index["type"], $key_name, $columns);
|
||||||
} elseif (!$primary_key) {
|
} elseif (!$primary_key) {
|
||||||
@@ -480,28 +509,6 @@ if (isset($_GET["sqlite"]) || isset($_GET["sqlite2"])) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!recreate_table($table, $name, $alter, $originals, $foreign, $indexes)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
foreach ($alter as $val) {
|
|
||||||
if (!queries("ALTER TABLE " . table($table) . " $val")) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if ($table != $name && !queries("ALTER TABLE " . table($table) . " RENAME TO " . table($name))) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if ($auto_increment) {
|
|
||||||
queries("UPDATE sqlite_sequence SET seq = $auto_increment WHERE name = " . q($name)); // ignores error
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
function recreate_table($table, $name, $alter, $originals, $foreign, $indexes) {
|
|
||||||
queries("BEGIN");
|
|
||||||
if ($table != "") {
|
|
||||||
foreach (foreign_keys($table) as $foreign_key) {
|
foreach (foreign_keys($table) as $foreign_key) {
|
||||||
$columns = array();
|
$columns = array();
|
||||||
foreach ($foreign_key["source"] as $column) {
|
foreach ($foreign_key["source"] as $column) {
|
||||||
@@ -559,28 +566,13 @@ if (isset($_GET["sqlite"]) || isset($_GET["sqlite2"])) {
|
|||||||
function alter_indexes($table, $alter) {
|
function alter_indexes($table, $alter) {
|
||||||
foreach ($alter as $primary) {
|
foreach ($alter as $primary) {
|
||||||
if ($primary[0] == "PRIMARY") {
|
if ($primary[0] == "PRIMARY") {
|
||||||
$indexes = array();
|
$fields = array();
|
||||||
foreach (indexes($table) as $key_name => $index) {
|
|
||||||
$columns = array();
|
|
||||||
foreach ($index["columns"] as $key => $column) {
|
|
||||||
$columns[] = idf_escape($column) . ($index["descs"][$key] ? " DESC" : "");
|
|
||||||
}
|
|
||||||
$indexes[$key_name] = array($index["type"], $key_name, "(" . implode(", ", $columns) . ")");
|
|
||||||
}
|
|
||||||
foreach ($alter as $val) {
|
|
||||||
if ($val[2] == "DROP") {
|
|
||||||
unset($indexes[$val[1]]);
|
|
||||||
} elseif ($val != $primary) {
|
|
||||||
$indexes[] = $val;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
$alter = array();
|
|
||||||
$originals = array();
|
$originals = array();
|
||||||
foreach (fields($table) as $name => $field) {
|
foreach (fields($table) as $name => $field) {
|
||||||
$alter[] = " " . implode(process_field($field, $field));
|
$fields[] = process_field($field, $field);
|
||||||
$originals[$name] = idf_escape($name);
|
$originals[$name] = idf_escape($name);
|
||||||
}
|
}
|
||||||
return recreate_table($table, $table, $alter, $originals, array(" PRIMARY KEY $primary[2]"), $indexes);
|
return recreate_table($table, $table, $fields, $originals, array(), $alter);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
foreach (array_reverse($alter) as $val) {
|
foreach (array_reverse($alter) as $val) {
|
||||||
|
Reference in New Issue
Block a user