]> git.joonet.de Git - adminer.git/commitdiff
SQLite: Preserve auto increment when recreating table
authorJakub Vrana <jakub@vrana.cz>
Wed, 21 Aug 2019 09:54:07 +0000 (11:54 +0200)
committerJakub Vrana <jakub@vrana.cz>
Wed, 21 Aug 2019 09:54:07 +0000 (11:54 +0200)
adminer/drivers/sqlite.inc.php
changes.txt

index d49aee64fcf3d3d8e1bc51a9f3a77b42d5becc5b..db2d7b24017e54f26e22e93b3168b1b630a13e95 100644 (file)
@@ -509,7 +509,7 @@ if (isset($_GET["sqlite"]) || isset($_GET["sqlite2"])) {
                        if ($table != $name && !queries("ALTER TABLE " . table($table) . " RENAME TO " . table($name))) {
                                return false;
                        }
-               } elseif (!recreate_table($table, $name, $alter, $originals, $foreign)) {
+               } elseif (!recreate_table($table, $name, $alter, $originals, $foreign, $auto_increment)) {
                        return false;
                }
                if ($auto_increment) {
@@ -523,7 +523,8 @@ if (isset($_GET["sqlite"]) || isset($_GET["sqlite2"])) {
                return true;
        }
 
-       function recreate_table($table, $name, $fields, $originals, $foreign, $indexes = array()) {
+       function recreate_table($table, $name, $fields, $originals, $foreign, $auto_increment, $indexes = array()) {
+               global $connection;
                if ($table != "") {
                        if (!$fields) {
                                foreach (fields($table) as $key => $field) {
@@ -597,12 +598,16 @@ if (isset($_GET["sqlite"]) || isset($_GET["sqlite2"])) {
                                $trigger = trigger($trigger_name);
                                $triggers[] = "CREATE TRIGGER " . idf_escape($trigger_name) . " " . implode(" ", $timing_event) . " ON " . table($name) . "\n$trigger[Statement]";
                        }
+                       $auto_increment = $auto_increment ? 0 : $connection->result("SELECT seq FROM sqlite_sequence WHERE name = " . q($table)); // if $auto_increment is set then it will be updated later
                        if (!queries("DROP TABLE " . table($table)) // drop before creating indexes and triggers to allow using old names
                                || !queries("ALTER TABLE " . table("adminer_$name") . " RENAME TO " . table($name))
                                || !alter_indexes($name, $indexes)
                        ) {
                                return false;
                        }
+                       if ($auto_increment) {
+                               queries("UPDATE sqlite_sequence SET seq = $auto_increment WHERE name = " . q($name)); // ignores error
+                       }
                        foreach ($triggers as $trigger) {
                                if (!queries($trigger)) {
                                        return false;
@@ -624,7 +629,7 @@ if (isset($_GET["sqlite"]) || isset($_GET["sqlite2"])) {
        function alter_indexes($table, $alter) {
                foreach ($alter as $primary) {
                        if ($primary[0] == "PRIMARY") {
-                               return recreate_table($table, $table, array(), array(), array(), $alter);
+                               return recreate_table($table, $table, array(), array(), array(), 0, $alter);
                        }
                }
                foreach (array_reverse($alter) as $val) {
index 298a3078c68bad319c3ca14833f324aa29a522d3..2a1e438a74b651dffa20cc016b3b6de42f0d4e1f 100644 (file)
@@ -5,6 +5,7 @@ MySQL: Skip editing generated columns
 SQLite: Quote strings stored in integer columns in export (bug #696)
 SQLite: Handle error in altering table (bug #697)
 SQLite: Allow setting auto increment for empty tables
+SQLite: Preserve auto increment when recreating table
 
 Adminer 4.7.2 (released 2019-07-18):
 Do not attempt logging in without password (bug #676)