From 1dbe6c83c38e64d2fb1026d65524a377ac990102 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Fri, 5 Jul 2019 13:54:18 +0200 Subject: [PATCH] Add database upgrading mechanism for adding the audit table. Signed-off-by: DL6ER --- .../Scripts/database_migration/gravity-db.sh | 21 +++++++++++++++++++ .../database_migration/gravity/1_to_2.sql | 9 ++++++++ advanced/Templates/gravity.db.sql | 11 +++++++++- 3 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 advanced/Scripts/database_migration/gravity-db.sh create mode 100644 advanced/Scripts/database_migration/gravity/1_to_2.sql diff --git a/advanced/Scripts/database_migration/gravity-db.sh b/advanced/Scripts/database_migration/gravity-db.sh new file mode 100644 index 00000000..2c5669f0 --- /dev/null +++ b/advanced/Scripts/database_migration/gravity-db.sh @@ -0,0 +1,21 @@ +#!/usr/bin/env bash +# shellcheck disable=SC1090 + +# Pi-hole: A black hole for Internet advertisements +# (c) 2019 Pi-hole, LLC (https://pi-hole.net) +# Network-wide ad blocking via your own hardware. +# +# Updates gravity.db database +# +# This file is copyright under the latest version of the EUPL. +# Please see LICENSE file for your rights under this license. + +upgrade_gravityDB(){ + version=$(sqlite3 "$1" "SELECT "value" FROM "info" WHERE "property" = 'version';") + echo $version + case "$version" in + 1) + sqlite3 "$1" < "/etc/.pihole/advanced/Scripts/database_migration/gravity/1_to_2.sql" + ;; + esac +} diff --git a/advanced/Scripts/database_migration/gravity/1_to_2.sql b/advanced/Scripts/database_migration/gravity/1_to_2.sql new file mode 100644 index 00000000..63b7f56f --- /dev/null +++ b/advanced/Scripts/database_migration/gravity/1_to_2.sql @@ -0,0 +1,9 @@ +CREATE TABLE audit +( + id INTEGER PRIMARY KEY AUTOINCREMENT, + domain TEXT UNIQUE NOT NULL, + date_added INTEGER NOT NULL DEFAULT (cast(strftime('%s', 'now') as int)), + comment TEXT +); + +UPDATE info SET value = 2 WHERE property = 'version'; diff --git a/advanced/Templates/gravity.db.sql b/advanced/Templates/gravity.db.sql index 91a38ef7..eef9ac80 100644 --- a/advanced/Templates/gravity.db.sql +++ b/advanced/Templates/gravity.db.sql @@ -80,13 +80,22 @@ CREATE TABLE gravity ( domain TEXT PRIMARY KEY ); + +CREATE TABLE audit +( + id INTEGER PRIMARY KEY AUTOINCREMENT, + domain TEXT UNIQUE NOT NULL, + date_added INTEGER NOT NULL DEFAULT (cast(strftime('%s', 'now') as int)), + comment TEXT +); + CREATE TABLE info ( property TEXT PRIMARY KEY, value TEXT NOT NULL ); -INSERT INTO info VALUES("version","1"); +INSERT INTO info VALUES("version","2"); CREATE VIEW vw_gravity AS SELECT domain FROM gravity