forked from pi-hole/pi-hole
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request pi-hole#3321 from pi-hole/release/v5.0
Pi-hole core v5.0
- Loading branch information
Showing
37 changed files
with
2,178 additions
and
1,108 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
#!/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. | ||
|
||
readonly scriptPath="/etc/.pihole/advanced/Scripts/database_migration/gravity" | ||
|
||
upgrade_gravityDB(){ | ||
local database piholeDir auditFile version | ||
database="${1}" | ||
piholeDir="${2}" | ||
auditFile="${piholeDir}/auditlog.list" | ||
|
||
# Get database version | ||
version="$(sqlite3 "${database}" "SELECT \"value\" FROM \"info\" WHERE \"property\" = 'version';")" | ||
|
||
if [[ "$version" == "1" ]]; then | ||
# This migration script upgrades the gravity.db file by | ||
# adding the domain_audit table | ||
echo -e " ${INFO} Upgrading gravity database from version 1 to 2" | ||
sqlite3 "${database}" < "${scriptPath}/1_to_2.sql" | ||
version=2 | ||
|
||
# Store audit domains in database table | ||
if [ -e "${auditFile}" ]; then | ||
echo -e " ${INFO} Migrating content of ${auditFile} into new database" | ||
# database_table_from_file is defined in gravity.sh | ||
database_table_from_file "domain_audit" "${auditFile}" | ||
fi | ||
fi | ||
if [[ "$version" == "2" ]]; then | ||
# This migration script upgrades the gravity.db file by | ||
# renaming the regex table to regex_blacklist, and | ||
# creating a new regex_whitelist table + corresponding linking table and views | ||
echo -e " ${INFO} Upgrading gravity database from version 2 to 3" | ||
sqlite3 "${database}" < "${scriptPath}/2_to_3.sql" | ||
version=3 | ||
fi | ||
if [[ "$version" == "3" ]]; then | ||
# This migration script unifies the formally separated domain | ||
# lists into a single table with a UNIQUE domain constraint | ||
echo -e " ${INFO} Upgrading gravity database from version 3 to 4" | ||
sqlite3 "${database}" < "${scriptPath}/3_to_4.sql" | ||
version=4 | ||
fi | ||
if [[ "$version" == "4" ]]; then | ||
# This migration script upgrades the gravity and list views | ||
# implementing necessary changes for per-client blocking | ||
echo -e " ${INFO} Upgrading gravity database from version 4 to 5" | ||
sqlite3 "${database}" < "${scriptPath}/4_to_5.sql" | ||
version=5 | ||
fi | ||
if [[ "$version" == "5" ]]; then | ||
# This migration script upgrades the adlist view | ||
# to return an ID used in gravity.sh | ||
echo -e " ${INFO} Upgrading gravity database from version 5 to 6" | ||
sqlite3 "${database}" < "${scriptPath}/5_to_6.sql" | ||
version=6 | ||
fi | ||
if [[ "$version" == "6" ]]; then | ||
# This migration script adds a special group with ID 0 | ||
# which is automatically associated to all clients not | ||
# having their own group assignments | ||
echo -e " ${INFO} Upgrading gravity database from version 6 to 7" | ||
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 | ||
# 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=8 | ||
fi | ||
if [[ "$version" == "8" ]]; then | ||
# This migration fixes some issues that were introduced | ||
# in the previous migration script. | ||
echo -e " ${INFO} Upgrading gravity database from version 8 to 9" | ||
sqlite3 "${database}" < "${scriptPath}/8_to_9.sql" | ||
version=9 | ||
fi | ||
if [[ "$version" == "9" ]]; then | ||
# This migration drops unused tables and creates triggers to remove | ||
# obsolete groups assignments when the linked items are deleted | ||
echo -e " ${INFO} Upgrading gravity database from version 9 to 10" | ||
sqlite3 "${database}" < "${scriptPath}/9_to_10.sql" | ||
version=10 | ||
fi | ||
if [[ "$version" == "10" ]]; then | ||
# This adds timestamp and an optional comment field to the client table | ||
# These fields are only temporary and will be replaces by the columns | ||
# defined in gravity.db.sql during gravity swapping. We add them here | ||
# to keep the copying process generic (needs the same columns in both the | ||
# source and the destination databases). | ||
echo -e " ${INFO} Upgrading gravity database from version 10 to 11" | ||
sqlite3 "${database}" < "${scriptPath}/10_to_11.sql" | ||
version=11 | ||
fi | ||
if [[ "$version" == "11" ]]; then | ||
# Rename group 0 from "Unassociated" to "Default" | ||
echo -e " ${INFO} Upgrading gravity database from version 11 to 12" | ||
sqlite3 "${database}" < "${scriptPath}/11_to_12.sql" | ||
version=12 | ||
fi | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
.timeout 30000 | ||
|
||
BEGIN TRANSACTION; | ||
|
||
ALTER TABLE client ADD COLUMN date_added INTEGER; | ||
ALTER TABLE client ADD COLUMN date_modified INTEGER; | ||
ALTER TABLE client ADD COLUMN comment TEXT; | ||
|
||
CREATE TRIGGER tr_client_update AFTER UPDATE ON client | ||
BEGIN | ||
UPDATE client SET date_modified = (cast(strftime('%s', 'now') as int)) WHERE id = NEW.id; | ||
END; | ||
|
||
UPDATE info SET value = 11 WHERE property = 'version'; | ||
|
||
COMMIT; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
.timeout 30000 | ||
|
||
PRAGMA FOREIGN_KEYS=OFF; | ||
|
||
BEGIN TRANSACTION; | ||
|
||
UPDATE "group" SET name = 'Default' WHERE id = 0; | ||
UPDATE "group" SET description = 'The default group' WHERE id = 0; | ||
|
||
DROP TRIGGER IF EXISTS tr_group_zero; | ||
|
||
CREATE TRIGGER tr_group_zero AFTER DELETE ON "group" | ||
BEGIN | ||
INSERT OR IGNORE INTO "group" (id,enabled,name,description) VALUES (0,1,'Default','The default group'); | ||
END; | ||
|
||
UPDATE info SET value = 12 WHERE property = 'version'; | ||
|
||
COMMIT; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
.timeout 30000 | ||
|
||
BEGIN TRANSACTION; | ||
|
||
CREATE TABLE domain_audit | ||
( | ||
id INTEGER PRIMARY KEY AUTOINCREMENT, | ||
domain TEXT UNIQUE NOT NULL, | ||
date_added INTEGER NOT NULL DEFAULT (cast(strftime('%s', 'now') as int)) | ||
); | ||
|
||
UPDATE info SET value = 2 WHERE property = 'version'; | ||
|
||
COMMIT; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
.timeout 30000 | ||
|
||
PRAGMA FOREIGN_KEYS=OFF; | ||
|
||
BEGIN TRANSACTION; | ||
|
||
ALTER TABLE regex RENAME TO regex_blacklist; | ||
|
||
CREATE TABLE regex_blacklist_by_group | ||
( | ||
regex_blacklist_id INTEGER NOT NULL REFERENCES regex_blacklist (id), | ||
group_id INTEGER NOT NULL REFERENCES "group" (id), | ||
PRIMARY KEY (regex_blacklist_id, group_id) | ||
); | ||
|
||
INSERT INTO regex_blacklist_by_group SELECT * FROM regex_by_group; | ||
DROP TABLE regex_by_group; | ||
DROP VIEW vw_regex; | ||
DROP TRIGGER tr_regex_update; | ||
|
||
CREATE VIEW vw_regex_blacklist AS SELECT DISTINCT domain | ||
FROM regex_blacklist | ||
LEFT JOIN regex_blacklist_by_group ON regex_blacklist_by_group.regex_blacklist_id = regex_blacklist.id | ||
LEFT JOIN "group" ON "group".id = regex_blacklist_by_group.group_id | ||
WHERE regex_blacklist.enabled = 1 AND (regex_blacklist_by_group.group_id IS NULL OR "group".enabled = 1) | ||
ORDER BY regex_blacklist.id; | ||
|
||
CREATE TRIGGER tr_regex_blacklist_update AFTER UPDATE ON regex_blacklist | ||
BEGIN | ||
UPDATE regex_blacklist SET date_modified = (cast(strftime('%s', 'now') as int)) WHERE domain = NEW.domain; | ||
END; | ||
|
||
CREATE TABLE regex_whitelist | ||
( | ||
id INTEGER PRIMARY KEY AUTOINCREMENT, | ||
domain TEXT UNIQUE NOT NULL, | ||
enabled BOOLEAN NOT NULL DEFAULT 1, | ||
date_added INTEGER NOT NULL DEFAULT (cast(strftime('%s', 'now') as int)), | ||
date_modified INTEGER NOT NULL DEFAULT (cast(strftime('%s', 'now') as int)), | ||
comment TEXT | ||
); | ||
|
||
CREATE TABLE regex_whitelist_by_group | ||
( | ||
regex_whitelist_id INTEGER NOT NULL REFERENCES regex_whitelist (id), | ||
group_id INTEGER NOT NULL REFERENCES "group" (id), | ||
PRIMARY KEY (regex_whitelist_id, group_id) | ||
); | ||
|
||
CREATE VIEW vw_regex_whitelist AS SELECT DISTINCT domain | ||
FROM regex_whitelist | ||
LEFT JOIN regex_whitelist_by_group ON regex_whitelist_by_group.regex_whitelist_id = regex_whitelist.id | ||
LEFT JOIN "group" ON "group".id = regex_whitelist_by_group.group_id | ||
WHERE regex_whitelist.enabled = 1 AND (regex_whitelist_by_group.group_id IS NULL OR "group".enabled = 1) | ||
ORDER BY regex_whitelist.id; | ||
|
||
CREATE TRIGGER tr_regex_whitelist_update AFTER UPDATE ON regex_whitelist | ||
BEGIN | ||
UPDATE regex_whitelist SET date_modified = (cast(strftime('%s', 'now') as int)) WHERE domain = NEW.domain; | ||
END; | ||
|
||
|
||
UPDATE info SET value = 3 WHERE property = 'version'; | ||
|
||
COMMIT; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
.timeout 30000 | ||
|
||
PRAGMA FOREIGN_KEYS=OFF; | ||
|
||
BEGIN TRANSACTION; | ||
|
||
CREATE TABLE domainlist | ||
( | ||
id INTEGER PRIMARY KEY AUTOINCREMENT, | ||
type INTEGER NOT NULL DEFAULT 0, | ||
domain TEXT UNIQUE NOT NULL, | ||
enabled BOOLEAN NOT NULL DEFAULT 1, | ||
date_added INTEGER NOT NULL DEFAULT (cast(strftime('%s', 'now') as int)), | ||
date_modified INTEGER NOT NULL DEFAULT (cast(strftime('%s', 'now') as int)), | ||
comment TEXT | ||
); | ||
|
||
ALTER TABLE whitelist ADD COLUMN type INTEGER; | ||
UPDATE whitelist SET type = 0; | ||
INSERT INTO domainlist (type,domain,enabled,date_added,date_modified,comment) | ||
SELECT type,domain,enabled,date_added,date_modified,comment FROM whitelist; | ||
|
||
ALTER TABLE blacklist ADD COLUMN type INTEGER; | ||
UPDATE blacklist SET type = 1; | ||
INSERT INTO domainlist (type,domain,enabled,date_added,date_modified,comment) | ||
SELECT type,domain,enabled,date_added,date_modified,comment FROM blacklist; | ||
|
||
ALTER TABLE regex_whitelist ADD COLUMN type INTEGER; | ||
UPDATE regex_whitelist SET type = 2; | ||
INSERT INTO domainlist (type,domain,enabled,date_added,date_modified,comment) | ||
SELECT type,domain,enabled,date_added,date_modified,comment FROM regex_whitelist; | ||
|
||
ALTER TABLE regex_blacklist ADD COLUMN type INTEGER; | ||
UPDATE regex_blacklist SET type = 3; | ||
INSERT INTO domainlist (type,domain,enabled,date_added,date_modified,comment) | ||
SELECT type,domain,enabled,date_added,date_modified,comment FROM regex_blacklist; | ||
|
||
DROP TABLE whitelist_by_group; | ||
DROP TABLE blacklist_by_group; | ||
DROP TABLE regex_whitelist_by_group; | ||
DROP TABLE regex_blacklist_by_group; | ||
CREATE TABLE domainlist_by_group | ||
( | ||
domainlist_id INTEGER NOT NULL REFERENCES domainlist (id), | ||
group_id INTEGER NOT NULL REFERENCES "group" (id), | ||
PRIMARY KEY (domainlist_id, group_id) | ||
); | ||
|
||
DROP TRIGGER tr_whitelist_update; | ||
DROP TRIGGER tr_blacklist_update; | ||
DROP TRIGGER tr_regex_whitelist_update; | ||
DROP TRIGGER tr_regex_blacklist_update; | ||
CREATE TRIGGER tr_domainlist_update AFTER UPDATE ON domainlist | ||
BEGIN | ||
UPDATE domainlist SET date_modified = (cast(strftime('%s', 'now') as int)) WHERE domain = NEW.domain; | ||
END; | ||
|
||
DROP VIEW vw_whitelist; | ||
CREATE VIEW vw_whitelist AS SELECT domain, domainlist.id AS id, domainlist_by_group.group_id AS group_id | ||
FROM domainlist | ||
LEFT JOIN domainlist_by_group ON domainlist_by_group.domainlist_id = domainlist.id | ||
LEFT JOIN "group" ON "group".id = domainlist_by_group.group_id | ||
WHERE domainlist.enabled = 1 AND (domainlist_by_group.group_id IS NULL OR "group".enabled = 1) | ||
AND domainlist.type = 0 | ||
ORDER BY domainlist.id; | ||
|
||
DROP VIEW vw_blacklist; | ||
CREATE VIEW vw_blacklist AS SELECT domain, domainlist.id AS id, domainlist_by_group.group_id AS group_id | ||
FROM domainlist | ||
LEFT JOIN domainlist_by_group ON domainlist_by_group.domainlist_id = domainlist.id | ||
LEFT JOIN "group" ON "group".id = domainlist_by_group.group_id | ||
WHERE domainlist.enabled = 1 AND (domainlist_by_group.group_id IS NULL OR "group".enabled = 1) | ||
AND domainlist.type = 1 | ||
ORDER BY domainlist.id; | ||
|
||
DROP VIEW vw_regex_whitelist; | ||
CREATE VIEW vw_regex_whitelist AS SELECT domain, domainlist.id AS id, domainlist_by_group.group_id AS group_id | ||
FROM domainlist | ||
LEFT JOIN domainlist_by_group ON domainlist_by_group.domainlist_id = domainlist.id | ||
LEFT JOIN "group" ON "group".id = domainlist_by_group.group_id | ||
WHERE domainlist.enabled = 1 AND (domainlist_by_group.group_id IS NULL OR "group".enabled = 1) | ||
AND domainlist.type = 2 | ||
ORDER BY domainlist.id; | ||
|
||
DROP VIEW vw_regex_blacklist; | ||
CREATE VIEW vw_regex_blacklist AS SELECT domain, domainlist.id AS id, domainlist_by_group.group_id AS group_id | ||
FROM domainlist | ||
LEFT JOIN domainlist_by_group ON domainlist_by_group.domainlist_id = domainlist.id | ||
LEFT JOIN "group" ON "group".id = domainlist_by_group.group_id | ||
WHERE domainlist.enabled = 1 AND (domainlist_by_group.group_id IS NULL OR "group".enabled = 1) | ||
AND domainlist.type = 3 | ||
ORDER BY domainlist.id; | ||
|
||
UPDATE info SET value = 4 WHERE property = 'version'; | ||
|
||
COMMIT; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
.timeout 30000 | ||
|
||
PRAGMA FOREIGN_KEYS=OFF; | ||
|
||
BEGIN TRANSACTION; | ||
|
||
DROP TABLE gravity; | ||
CREATE TABLE gravity | ||
( | ||
domain TEXT NOT NULL, | ||
adlist_id INTEGER NOT NULL REFERENCES adlist (id), | ||
PRIMARY KEY(domain, adlist_id) | ||
); | ||
|
||
DROP VIEW vw_gravity; | ||
CREATE VIEW vw_gravity AS SELECT domain, adlist_by_group.group_id AS group_id | ||
FROM gravity | ||
LEFT JOIN adlist_by_group ON adlist_by_group.adlist_id = gravity.adlist_id | ||
LEFT JOIN adlist ON adlist.id = gravity.adlist_id | ||
LEFT JOIN "group" ON "group".id = adlist_by_group.group_id | ||
WHERE adlist.enabled = 1 AND (adlist_by_group.group_id IS NULL OR "group".enabled = 1); | ||
|
||
CREATE TABLE client | ||
( | ||
id INTEGER PRIMARY KEY AUTOINCREMENT, | ||
ip TEXT NOL NULL UNIQUE | ||
); | ||
|
||
CREATE TABLE client_by_group | ||
( | ||
client_id INTEGER NOT NULL REFERENCES client (id), | ||
group_id INTEGER NOT NULL REFERENCES "group" (id), | ||
PRIMARY KEY (client_id, group_id) | ||
); | ||
|
||
UPDATE info SET value = 5 WHERE property = 'version'; | ||
|
||
COMMIT; |
Oops, something went wrong.