diff --git a/src/ext/backup/class.backup.php b/src/ext/backup/class.backup.php index f4cb8c0..a8bd7e7 100644 --- a/src/ext/backup/class.backup.php +++ b/src/ext/backup/class.backup.php @@ -2,7 +2,7 @@ /* This file is a part of myTinyTodo. - (C) Copyright 2023 Max Pozdeev + (C) Copyright 2023-2025 Max Pozdeev Licensed under the GNU GPL version 2 or any later. See file COPYRIGHT for details. */ @@ -86,11 +86,9 @@ class Backup } $db = \DBConnection::instance(); $props = null; - if ($db::DBTYPE == \DBConnection::DBTYPE_MYSQL) { - $autoinc = $this->getMysqlTableAutoIncrement($table); - if ($autoinc != '') { - $props = ['auto_increment' => $autoinc]; - } + $autoinc = $this->getTableAutoIncrement($table); + if ($autoinc != '') { + $props = ['auto_increment' => $autoinc]; } $this->writeOpeningTag($group, $props); $q = $db->dq("SELECT * FROM $table"); @@ -120,11 +118,26 @@ class Backup $this->writeClosingTag($entity); } - function getMysqlTableAutoIncrement(string $table): string + function getTableAutoIncrement($table): string { $db = \DBConnection::instance(); - $r = $db->sqa("SHOW TABLE STATUS WHERE Name=?", [$table]); - return (string)$r['Auto_increment'] ?? ''; + if ($db::DBTYPE == \DBConnection::DBTYPE_MYSQL) { + $r = $db->sqa("SHOW TABLE STATUS WHERE Name=?", [$table]); + return (string)$r['Auto_increment'] ?? ''; + } + else if ($db::DBTYPE == \DBConnection::DBTYPE_SQLITE) { + $seq = (int)$db->sq("SELECT seq FROM sqlite_sequence WHERE name=?", [$table]); + if ($seq > 0) + return (string)$seq; + } + else if ($db::DBTYPE == \DBConnection::DBTYPE_POSTGRES) { + if ($db->tableFieldExists($table, 'id')) { + $v = (int)$db->sq("SELECT last_value FROM ". $table. '_id_seq'); + if ($v > 0) + return (string)$v; + } + } + return ''; } diff --git a/src/ext/backup/class.restore.php b/src/ext/backup/class.restore.php index eeccb31..94051a0 100644 --- a/src/ext/backup/class.restore.php +++ b/src/ext/backup/class.restore.php @@ -2,7 +2,7 @@ /* This file is a part of myTinyTodo. - (C) Copyright 2023 Max Pozdeev + (C) Copyright 2023-2025 Max Pozdeev Licensed under the GNU GPL version 2 or any later. See file COPYRIGHT for details. */ @@ -217,6 +217,9 @@ class Restore case DBConnection::DBTYPE_POSTGRES: $db->ex("ALTER TABLE {$db->prefix}$table ALTER COLUMN id RESTART WITH ". (int)$autoinc); break; + case DBConnection::DBTYPE_SQLITE: + $db->ex("UPDATE sqlite_sequence SET seq=? WHERE name=?", [$autoinc, $db->prefix. $table]); + break; default: break; } @@ -232,8 +235,9 @@ class Restore $db->ex("TRUNCATE TABLE $table RESTART IDENTITY"); } else { - // we do not use TRUNCATE on mysql due to autocommit - // sqlite has truncate optimizer + # - we do not use TRUNCATE on mysql due to autocommit + # - sqlite has truncate optimizer while delete all to make it faster + # - no need to reset auto_increment sequence before inserting lower ids $db->ex("DELETE FROM $table"); } } diff --git a/src/ext/backup/extension.json b/src/ext/backup/extension.json index 1ef2a14..a175e8c 100644 --- a/src/ext/backup/extension.json +++ b/src/ext/backup/extension.json @@ -1,6 +1,6 @@ { "bundleId": "backup", "name": "Backup", - "version": "1.0", + "version": "1.1", "description": "Backup" }