From b32b5ad6e97044dd410d2717340631388bcb631b Mon Sep 17 00:00:00 2001 From: DL6ER Date: Fri, 20 Dec 2019 00:09:10 +0000 Subject: [PATCH 1/3] Update gravity database to version 8. This enforces uniqueness on the group name. Signed-off-by: DL6ER --- .../Scripts/database_migration/gravity-db.sh | 7 ++++++ .../database_migration/gravity/7_to_8.sql | 23 +++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 advanced/Scripts/database_migration/gravity/7_to_8.sql diff --git a/advanced/Scripts/database_migration/gravity-db.sh b/advanced/Scripts/database_migration/gravity-db.sh index 28054643..6dc88bf3 100644 --- a/advanced/Scripts/database_migration/gravity-db.sh +++ b/advanced/Scripts/database_migration/gravity-db.sh @@ -72,4 +72,11 @@ upgrade_gravityDB(){ sqlite3 "${database}" < "${scriptPath}/6_to_7.sql" version=7 fi + if [[ "$version" == "7" ]]; then + # This migration script recreated the group table + # to ensure uniqueness on the group name + echo -e " ${INFO} Upgrading gravity database from version 7 to 8" + sqlite3 "${database}" < "${scriptPath}/7_to_8.sql" + version=7 + fi } diff --git a/advanced/Scripts/database_migration/gravity/7_to_8.sql b/advanced/Scripts/database_migration/gravity/7_to_8.sql new file mode 100644 index 00000000..8f79bde8 --- /dev/null +++ b/advanced/Scripts/database_migration/gravity/7_to_8.sql @@ -0,0 +1,23 @@ +.timeout 30000 + +PRAGMA FOREIGN_KEYS=OFF; + +BEGIN TRANSACTION; + +ALTER TABLE "group" RENAME TO "group__"; + +CREATE TABLE "group" +( + id INTEGER PRIMARY KEY AUTOINCREMENT, + enabled BOOLEAN NOT NULL DEFAULT 1, + name TEXT UNIQUE NOT NULL, + description TEXT +); + +INSERT OR IGNORE INTO "group" SELECT * FROM "group__"; + +DROP TABLE "group__"; + +UPDATE info SET value = 8 WHERE property = 'version'; + +COMMIT; From e589e665a785142e9c22383a6089f8810d36027b Mon Sep 17 00:00:00 2001 From: DL6ER Date: Fri, 20 Dec 2019 00:14:58 +0000 Subject: [PATCH 2/3] Also add date_added and date_modified fields to group table. Signed-off-by: DL6ER --- advanced/Scripts/database_migration/gravity-db.sh | 3 ++- advanced/Scripts/database_migration/gravity/7_to_8.sql | 9 ++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/advanced/Scripts/database_migration/gravity-db.sh b/advanced/Scripts/database_migration/gravity-db.sh index 6dc88bf3..bc650d3f 100644 --- a/advanced/Scripts/database_migration/gravity-db.sh +++ b/advanced/Scripts/database_migration/gravity-db.sh @@ -75,8 +75,9 @@ upgrade_gravityDB(){ if [[ "$version" == "7" ]]; then # This migration script recreated the group table # to ensure uniqueness on the group name + # We also add date_added and date_modified columns echo -e " ${INFO} Upgrading gravity database from version 7 to 8" sqlite3 "${database}" < "${scriptPath}/7_to_8.sql" - version=7 + version=8 fi } diff --git a/advanced/Scripts/database_migration/gravity/7_to_8.sql b/advanced/Scripts/database_migration/gravity/7_to_8.sql index 8f79bde8..0e58408e 100644 --- a/advanced/Scripts/database_migration/gravity/7_to_8.sql +++ b/advanced/Scripts/database_migration/gravity/7_to_8.sql @@ -11,10 +11,17 @@ CREATE TABLE "group" id INTEGER PRIMARY KEY AUTOINCREMENT, enabled BOOLEAN NOT NULL DEFAULT 1, name TEXT UNIQUE NOT NULL, + date_added INTEGER NOT NULL DEFAULT (cast(strftime('%s', 'now') as int)), + date_modified INTEGER NOT NULL DEFAULT (cast(strftime('%s', 'now') as int)), description TEXT ); -INSERT OR IGNORE INTO "group" SELECT * FROM "group__"; +CREATE TRIGGER tr_group_update AFTER UPDATE ON "group" + BEGIN + UPDATE "group" SET date_modified = (cast(strftime('%s', 'now') as int)) WHERE id = NEW.id; + END; + +INSERT OR IGNORE INTO "group" (id,enabled,name,description) SELECT id,enabled,name,description FROM "group__"; DROP TABLE "group__"; From eda7f40fefc4717e5cebc40f13a03cbb18ab492c Mon Sep 17 00:00:00 2001 From: DL6ER Date: Fri, 20 Dec 2019 00:42:59 +0000 Subject: [PATCH 3/3] Reinstall trigger that prevents group zero from being deleted. Signed-off-by: DL6ER --- advanced/Scripts/database_migration/gravity/7_to_8.sql | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/advanced/Scripts/database_migration/gravity/7_to_8.sql b/advanced/Scripts/database_migration/gravity/7_to_8.sql index 0e58408e..412475da 100644 --- a/advanced/Scripts/database_migration/gravity/7_to_8.sql +++ b/advanced/Scripts/database_migration/gravity/7_to_8.sql @@ -21,6 +21,11 @@ CREATE TRIGGER tr_group_update AFTER UPDATE ON "group" UPDATE "group" SET date_modified = (cast(strftime('%s', 'now') as int)) WHERE id = NEW.id; END; +CREATE TRIGGER tr_group_zero AFTER DELETE ON "group" + BEGIN + INSERT OR IGNORE INTO "group" (id,enabled,name) VALUES (0,1,'Unassociated'); + END; + INSERT OR IGNORE INTO "group" (id,enabled,name,description) SELECT id,enabled,name,description FROM "group__"; DROP TABLE "group__";