Upgrade DB fixes

This commit is contained in:
Jokob-sk
2023-06-10 09:01:24 +10:00
parent 649e280ce1
commit 5867961383
4 changed files with 28 additions and 28 deletions
+1
View File
@@ -2,6 +2,7 @@
.DS_Store .DS_Store
config/pialert.conf config/pialert.conf
db/* db/*
db/pialert.db
front/log/* front/log/*
front/plugins/**/*.log front/plugins/**/*.log
**/%40eaDir/ **/%40eaDir/
Regular → Executable
View File
+2 -2
View File
@@ -7,9 +7,9 @@ services:
network_mode: "host" network_mode: "host"
restart: unless-stopped restart: unless-stopped
volumes: volumes:
- ${APP_DATA_LOCATION}/pialert/config:/home/pi/pialert/config - ${APP_DATA_LOCATION}/pialert_dev/config:/home/pi/pialert/config
# - ${APP_DATA_LOCATION}/pialert/db/pialert.db:/home/pi/pialert/db/pialert.db # - ${APP_DATA_LOCATION}/pialert/db/pialert.db:/home/pi/pialert/db/pialert.db
- ${APP_DATA_LOCATION}/pialert/db:/home/pi/pialert/db - ${APP_DATA_LOCATION}/pialert_dev/db:/home/pi/pialert/db
# (optional) useful for debugging if you have issues setting up the container # (optional) useful for debugging if you have issues setting up the container
- ${LOGS_LOCATION}:/home/pi/pialert/front/log - ${LOGS_LOCATION}:/home/pi/pialert/front/log
# DELETE START anyone trying to use this file: comment out / delete BELOW lines, they are only for development purposes # DELETE START anyone trying to use this file: comment out / delete BELOW lines, they are only for development purposes
+24 -25
View File
@@ -135,10 +135,9 @@ class DB():
""" """
Check the current tables in the DB and upgrade them if neccessary Check the current tables in the DB and upgrade them if neccessary
""" """
sql = self.sql #TO-DO
# indicates, if Online_History table is available # indicates, if Online_History table is available
onlineHistoryAvailable = sql.execute(""" onlineHistoryAvailable = self.sql.execute("""
SELECT name FROM sqlite_master WHERE type='table' SELECT name FROM sqlite_master WHERE type='table'
AND name='Online_History'; AND name='Online_History';
""").fetchall() != [] """).fetchall() != []
@@ -154,11 +153,11 @@ class DB():
# Drop table if available, but incompatible # Drop table if available, but incompatible
if onlineHistoryAvailable and isIncompatible: if onlineHistoryAvailable and isIncompatible:
mylog('none','[upgradeDB] Table is incompatible, Dropping the Online_History table') mylog('none','[upgradeDB] Table is incompatible, Dropping the Online_History table')
sql.execute("DROP TABLE Online_History;") self.sql.execute("DROP TABLE Online_History;")
onlineHistoryAvailable = False onlineHistoryAvailable = False
if onlineHistoryAvailable == False : if onlineHistoryAvailable == False :
sql.execute(""" self.sql.execute("""
CREATE TABLE "Online_History" ( CREATE TABLE "Online_History" (
"Index" INTEGER, "Index" INTEGER,
"Scan_Date" TEXT, "Scan_Date" TEXT,
@@ -172,13 +171,13 @@ class DB():
# Alter Devices table # Alter Devices table
# dev_Network_Node_MAC_ADDR column # dev_Network_Node_MAC_ADDR column
dev_Network_Node_MAC_ADDR_missing = sql.execute (""" dev_Network_Node_MAC_ADDR_missing = self.sql.execute ("""
SELECT COUNT(*) AS CNTREC FROM pragma_table_info('Devices') WHERE name='dev_Network_Node_MAC_ADDR' SELECT COUNT(*) AS CNTREC FROM pragma_table_info('Devices') WHERE name='dev_Network_Node_MAC_ADDR'
""").fetchone()[0] == 0 """).fetchone()[0] == 0
if dev_Network_Node_MAC_ADDR_missing : if dev_Network_Node_MAC_ADDR_missing :
mylog('verbose', ["[upgradeDB] Adding dev_Network_Node_MAC_ADDR to the Devices table"]) mylog('verbose', ["[upgradeDB] Adding dev_Network_Node_MAC_ADDR to the Devices table"])
sql.execute(""" self.sql.execute("""
ALTER TABLE "Devices" ADD "dev_Network_Node_MAC_ADDR" TEXT ALTER TABLE "Devices" ADD "dev_Network_Node_MAC_ADDR" TEXT
""") """)
@@ -189,23 +188,23 @@ class DB():
if dev_Network_Node_port_missing : if dev_Network_Node_port_missing :
mylog('verbose', ["[upgradeDB] Adding dev_Network_Node_port to the Devices table"]) mylog('verbose', ["[upgradeDB] Adding dev_Network_Node_port to the Devices table"])
sql.execute(""" self.sql.execute("""
ALTER TABLE "Devices" ADD "dev_Network_Node_port" INTEGER ALTER TABLE "Devices" ADD "dev_Network_Node_port" INTEGER
""") """)
# dev_Icon column # dev_Icon column
dev_Icon_missing = sql.execute (""" dev_Icon_missing = self.sql.execute ("""
SELECT COUNT(*) AS CNTREC FROM pragma_table_info('Devices') WHERE name='dev_Icon' SELECT COUNT(*) AS CNTREC FROM pragma_table_info('Devices') WHERE name='dev_Icon'
""").fetchone()[0] == 0 """).fetchone()[0] == 0
if dev_Icon_missing : if dev_Icon_missing :
mylog('verbose', ["[upgradeDB] Adding dev_Icon to the Devices table"]) mylog('verbose', ["[upgradeDB] Adding dev_Icon to the Devices table"])
sql.execute(""" self.sql.execute("""
ALTER TABLE "Devices" ADD "dev_Icon" TEXT ALTER TABLE "Devices" ADD "dev_Icon" TEXT
""") """)
# indicates, if Settings table is available # indicates, if Settings table is available
settingsMissing = sql.execute(""" settingsMissing = self.sql.execute("""
SELECT name FROM sqlite_master WHERE type='table' SELECT name FROM sqlite_master WHERE type='table'
AND name='Settings'; AND name='Settings';
""").fetchone() == None """).fetchone() == None
@@ -214,9 +213,9 @@ class DB():
mylog('verbose', ["[upgradeDB] Re-creating Settings table"]) mylog('verbose', ["[upgradeDB] Re-creating Settings table"])
if settingsMissing == False: if settingsMissing == False:
sql.execute("DROP TABLE Settings;") self.sql.execute("DROP TABLE Settings;")
sql.execute(""" self.sql.execute("""
CREATE TABLE "Settings" ( CREATE TABLE "Settings" (
"Code_Name" TEXT, "Code_Name" TEXT,
"Display_Name" TEXT, "Display_Name" TEXT,
@@ -231,19 +230,19 @@ class DB():
""") """)
# indicates, if Pholus_Scan table is available # indicates, if Pholus_Scan table is available
pholusScanMissing = sql.execute(""" pholusScanMissing = self.sql.execute("""
SELECT name FROM sqlite_master WHERE type='table' SELECT name FROM sqlite_master WHERE type='table'
AND name='Pholus_Scan'; AND name='Pholus_Scan';
""").fetchone() == None """).fetchone() == None
# if pholusScanMissing == False: # if pholusScanMissing == False:
# # Re-creating Pholus_Scan table # # Re-creating Pholus_Scan table
# sql.execute("DROP TABLE Pholus_Scan;") # self.sql.execute("DROP TABLE Pholus_Scan;")
# pholusScanMissing = True # pholusScanMissing = True
if pholusScanMissing: if pholusScanMissing:
mylog('verbose', ["[upgradeDB] Re-creating Pholus_Scan table"]) mylog('verbose', ["[upgradeDB] Re-creating Pholus_Scan table"])
sql.execute(""" self.sql.execute("""
CREATE TABLE "Pholus_Scan" ( CREATE TABLE "Pholus_Scan" (
"Index" INTEGER, "Index" INTEGER,
"Info" TEXT, "Info" TEXT,
@@ -258,16 +257,16 @@ class DB():
""") """)
# indicates, if Nmap_Scan table is available # indicates, if Nmap_Scan table is available
nmapScanMissing = sql.execute(""" nmapScanMissing = self.sql.execute("""
SELECT name FROM sqlite_master WHERE type='table' SELECT name FROM sqlite_master WHERE type='table'
AND name='Nmap_Scan'; AND name='Nmap_Scan';
""").fetchone() == None """).fetchone() == None
# Re-creating Parameters table # Re-creating Parameters table
mylog('verbose', ["[upgradeDB] Re-creating Parameters table"]) mylog('verbose', ["[upgradeDB] Re-creating Parameters table"])
sql.execute("DROP TABLE Parameters;") self.sql.execute("DROP TABLE Parameters;")
sql.execute(""" self.sql.execute("""
CREATE TABLE "Parameters" ( CREATE TABLE "Parameters" (
"par_ID" TEXT PRIMARY KEY, "par_ID" TEXT PRIMARY KEY,
"par_Value" TEXT "par_Value" TEXT
@@ -284,7 +283,7 @@ class DB():
if nmapScanMissing: if nmapScanMissing:
mylog('verbose', ["[upgradeDB] Re-creating Nmap_Scan table"]) mylog('verbose', ["[upgradeDB] Re-creating Nmap_Scan table"])
sql.execute(""" self.sql.execute("""
CREATE TABLE "Nmap_Scan" ( CREATE TABLE "Nmap_Scan" (
"Index" INTEGER, "Index" INTEGER,
"MAC" TEXT, "MAC" TEXT,
@@ -315,7 +314,7 @@ class DB():
ForeignKey TEXT NOT NULL, ForeignKey TEXT NOT NULL,
PRIMARY KEY("Index" AUTOINCREMENT) PRIMARY KEY("Index" AUTOINCREMENT)
); """ ); """
sql.execute(sql_Plugins_Objects) self.sql.execute(sql_Plugins_Objects)
# Plugin execution results # Plugin execution results
sql_Plugins_Events = """ CREATE TABLE IF NOT EXISTS Plugins_Events( sql_Plugins_Events = """ CREATE TABLE IF NOT EXISTS Plugins_Events(
@@ -335,7 +334,7 @@ class DB():
ForeignKey TEXT NOT NULL, ForeignKey TEXT NOT NULL,
PRIMARY KEY("Index" AUTOINCREMENT) PRIMARY KEY("Index" AUTOINCREMENT)
); """ ); """
sql.execute(sql_Plugins_Events) self.sql.execute(sql_Plugins_Events)
# Plugin execution history # Plugin execution history
sql_Plugins_History = """ CREATE TABLE IF NOT EXISTS Plugins_History( sql_Plugins_History = """ CREATE TABLE IF NOT EXISTS Plugins_History(
@@ -355,19 +354,19 @@ class DB():
ForeignKey TEXT NOT NULL, ForeignKey TEXT NOT NULL,
PRIMARY KEY("Index" AUTOINCREMENT) PRIMARY KEY("Index" AUTOINCREMENT)
); """ ); """
sql.execute(sql_Plugins_History) self.sql.execute(sql_Plugins_History)
# Dynamically generated language strings # Dynamically generated language strings
# indicates, if Language_Strings table is available # indicates, if Language_Strings table is available
languageStringsMissing = sql.execute(""" languageStringsMissing = self.sql.execute("""
SELECT name FROM sqlite_master WHERE type='table' SELECT name FROM sqlite_master WHERE type='table'
AND name='Plugins_Language_Strings'; AND name='Plugins_Language_Strings';
""").fetchone() == None """).fetchone() == None
if languageStringsMissing == False: if languageStringsMissing == False:
sql.execute("DROP TABLE Plugins_Language_Strings;") self.sql.execute("DROP TABLE Plugins_Language_Strings;")
sql.execute(""" CREATE TABLE IF NOT EXISTS Plugins_Language_Strings( self.sql.execute(""" CREATE TABLE IF NOT EXISTS Plugins_Language_Strings(
"Index" INTEGER, "Index" INTEGER,
Language_Code TEXT NOT NULL, Language_Code TEXT NOT NULL,
String_Key TEXT NOT NULL, String_Key TEXT NOT NULL,