From 917441e8c5ad8207b5acf8e80d6e44e12b668c9d Mon Sep 17 00:00:00 2001 From: Jan Tojnar Date: Sat, 15 Apr 2017 20:44:11 +0200 Subject: [PATCH] Re-create sqlite table instead of using different schema --- daos/sqlite/Database.php | 45 ++++++++++++++++++++++++---------------- 1 file changed, 27 insertions(+), 18 deletions(-) diff --git a/daos/sqlite/Database.php b/daos/sqlite/Database.php index 3c116fc5dd..0557580606 100644 --- a/daos/sqlite/Database.php +++ b/daos/sqlite/Database.php @@ -57,9 +57,7 @@ public function __construct() { uid VARCHAR(255) NOT NULL, link TEXT NOT NULL, updatetime DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, - author VARCHAR(255), - shared BOOL, - lastseen DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP + author VARCHAR(255) ); '); @@ -106,7 +104,7 @@ public function __construct() { '); \F3::get('db')->exec(' - INSERT INTO version (version) VALUES (11); + INSERT INTO version (version) VALUES (8); '); \F3::get('db')->exec(' @@ -198,18 +196,29 @@ public function __construct() { } if (strnatcmp($version, '11') < 0) { \F3::get('db')->exec([ - 'DROP TRIGGER update_updatetime_trigger', - 'ALTER TABLE items ADD lastseen DATETIME', - // Needs to be a trigger since SQLite does not allow adding fields with dynamic defaults. - // https://marc.info/?l=sqlite-users&m=122283678813925 - 'CREATE TRIGGER insert_lastseen_trigger - AFTER INSERT ON items FOR EACH ROW - BEGIN - UPDATE items - SET lastseen = CURRENT_TIMESTAMP - WHERE id = NEW.id; - END', - 'UPDATE items SET lastseen = CURRENT_TIMESTAMP', + // table needs to be re-created because ALTER TABLE is rather limited + // https://sqlite.org/lang_altertable.html#otheralter + 'CREATE TABLE new_items ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + datetime DATETIME NOT NULL, + title TEXT NOT NULL, + content TEXT NOT NULL, + thumbnail TEXT, + icon TEXT, + unread BOOL NOT NULL, + starred BOOL NOT NULL, + source INT NOT NULL, + uid VARCHAR(255) NOT NULL, + link TEXT NOT NULL, + updatetime DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, + author VARCHAR(255), + shared BOOL, + lastseen DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP + )', + 'INSERT INTO new_items SELECT *, CURRENT_TIMESTAMP FROM items', + 'DROP TABLE items', + 'ALTER TABLE new_items RENAME TO items', + 'CREATE INDEX source ON items (source)', 'CREATE TRIGGER update_updatetime_trigger AFTER UPDATE ON items FOR EACH ROW WHEN ( @@ -220,8 +229,8 @@ public function __construct() { UPDATE items SET updatetime = CURRENT_TIMESTAMP WHERE id = NEW.id; - END;', - 'INSERT INTO version (version) VALUES (11);' + END', + 'INSERT INTO version (version) VALUES (11)' ]); } }