mirror of
https://github.com/maxpozdeev/mytinytodo.git
synced 2026-03-11 08:55:27 +00:00
* better handling of auto_increment in backup extension
This commit is contained in:
parent
ac1df904ca
commit
dd4c38438b
3 changed files with 30 additions and 13 deletions
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
/*
|
||||
This file is a part of myTinyTodo.
|
||||
(C) Copyright 2023 Max Pozdeev <maxpozdeev@gmail.com>
|
||||
(C) Copyright 2023-2025 Max Pozdeev <maxpozdeev@gmail.com>
|
||||
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 '';
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
/*
|
||||
This file is a part of myTinyTodo.
|
||||
(C) Copyright 2023 Max Pozdeev <maxpozdeev@gmail.com>
|
||||
(C) Copyright 2023-2025 Max Pozdeev <maxpozdeev@gmail.com>
|
||||
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");
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"bundleId": "backup",
|
||||
"name": "Backup",
|
||||
"version": "1.0",
|
||||
"version": "1.1",
|
||||
"description": "Backup"
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue