Skip to content

Commit

Permalink
Re-create sqlite table instead of using different schema
Browse files Browse the repository at this point in the history
  • Loading branch information
jtojnar committed Apr 15, 2017
1 parent fa41fd2 commit 917441e
Showing 1 changed file with 27 additions and 18 deletions.
45 changes: 27 additions & 18 deletions daos/sqlite/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
');

Expand Down Expand Up @@ -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('
Expand Down Expand Up @@ -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 (
Expand All @@ -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)'
]);
}
}
Expand Down

0 comments on commit 917441e

Please sign in to comment.