From 96e1ed8ae2aefdffcd97dad5694f1dc0b8e0a930 Mon Sep 17 00:00:00 2001 From: Yasen Pramatarov Date: Mon, 27 May 2024 16:13:13 +0300 Subject: [PATCH] Adds checks for DB consistency. --- jvb/jilo.db | Bin 0 -> 12288 bytes jvb/jitsi-stats | 27 +++++++++++++++------------ jvb/jitsi-stats.schema | 16 ---------------- 3 files changed, 15 insertions(+), 28 deletions(-) delete mode 100644 jvb/jitsi-stats.schema diff --git a/jvb/jilo.db b/jvb/jilo.db index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..f1d28f04f4426b38e6bb3b6575790f6bfadcc56c 100644 GIT binary patch literal 12288 zcmeI#K}!Nb6bJBG4NSodI_#m#fCn{7RyssQkjhY|MXhV(Nla&BLTAMtg6d=)`-pv+ z#$C;=2)ZW!$L!4Pdo#1_?>5b=hESZk-gwwmbeF6X!yw0$5<=p$M-M^7biXp2PX{A1 z|7{v4#fP`FuEy3{B-7L#0s;_#00bZa0SG_<0uX=z1eQl&@*GQ;rfIykRQGnkebrTA z7q8l^>@ez-D-A}2Jk6)54$-Hz3x}OBn_k+r^RnIU)LDBwsND?Bu@_eC$C+AsOJ!RE;UOkSu=jP|t!ck60 z#qanyR|xkB>GeO4N);wsM#9WyjhD^f?|Ytf`IyTd_aoET7l}yW4<3uvoo{~|sc6uJ zla?Mw-M9>^slxPbt}p!v2tWV=5P$##AOHafKmY;|fB*!RSYUepH!|-;Hv|MA009U< u00Izz00bZa0SG_<0{>4SYb28!hv`)vXV;a|{+_jCm2{()br7_oWmzAJQH}@z literal 0 HcmV?d00001 diff --git a/jvb/jitsi-stats b/jvb/jitsi-stats index 39ebc98..3acc153 100755 --- a/jvb/jitsi-stats +++ b/jvb/jitsi-stats @@ -6,7 +6,7 @@ DEFAULT_LOGFILE="./jvb.log" DEFAULT_ROTATED_LOGFILE="./jvb.log.1" # Default SQLite database file -DEFAULT_DB="./jitsi-stats.db" +DEFAULT_DB="./jilo.db" # Configuration file CONFIG_FILE="./jilo.conf" @@ -27,16 +27,16 @@ DB=${DB:-$DEFAULT_DB} db_get_state="SELECT filename, filetime, filesize, position, inode FROM state WHERE jitsi_component = 'JVB';" db_set_state_template="UPDATE state SET time=datetime('now'), filename='%s', filetime='%s', filesize='%s', position='%s', inode='%s' WHERE jitsi_component = 'JVB';" db_insert_template="INSERT INTO conferences (conference_name, conference_id, start, end) VALUES ('%s', '%s', '%s', '%s');" -db_create=" +db_drop=" DROP TABLE IF EXISTS conferences; -CREATE TABLE conferences ( +DROP TABLE IF EXISTS state;" +db_create="CREATE TABLE conferences ( id INTEGER PRIMARY_KEY, conference_name TEXT, conference_id TEXT, start TEXT, end TEXT ); -DROP TABLE IF EXISTS state; CREATE TABLE state ( id INTEGER PRIMARY_KEY, jitsi_component TEXT, @@ -46,12 +46,11 @@ CREATE TABLE state ( filesize INTEGER, position INTEGER CHECK(typeof(position)='integer'), inode INTEGER -); -INSERT OR REPLACE INTO state (id, jitsi_component, time, filename, filetime, filesize, position, inode) VALUES (1, 'JVB', '1970-01-01 00:00:00.000', '', 0, 0, 0, 0);" +);" +db_init="INSERT OR REPLACE INTO state (id, jitsi_component, time, filename, filetime, filesize, position, inode) VALUES (1, 'JVB', '1970-01-01 00:00:00.000', '', 0, 0, 0, 0);" db_flush=" DELETE FROM conferences; -DELETE FROM state; -INSERT OR REPLACE INTO state (id, jitsi_component, time, filename, filetime, filesize, position, inode) VALUES (1, 'JVB', '1970-01-01 00:00:00.000', '', 0, 0, 0, 0);" +DELETE FROM state;" help="Usage:\n\t$0 [OPTION]\nOptions:\n\t--create-db|-d - create the database\n\t--flush|-f - flush the tables\n\t--check|-c - check for new data\n\t--parse|-p [-v] - parse the logs [verbosely]" @@ -187,13 +186,16 @@ shift $((OPTIND -1)) case "$cmd" in --create-db) + sqlite3 "$DB" "$db_drop" sqlite3 "$DB" "$db_create" + sqlite3 "$DB" "$db_init" echo "Database created." exit 0 ;; --flush) sqlite3 "$DB" "$db_flush" + sqlite3 "$DB" "$db_init" echo "Tables flushed." exit 0 ;; @@ -201,14 +203,15 @@ case "$cmd" in --check) # First check if database exists - if [[ ! -f $"DB" ]]; then + if [[ ! -f "$DB" ]]; then echo "Database not found. If it's a fresh install, please install the database first." exit 1 fi - # Check the state table - if ! sqlite3 "$DB" ".schema state" &>/dev/null; then - echo "The database doesn't contain the required table \"state\". Please check it, and if needed, reinstall it." + # compare the DB schema to the expected one + current_db_schema=$(sqlite3 "$DB" .schema) + if [[ "$current_db_schema" != "$db_create" ]]; then + echo "The database doesn't match the expected schema. Please check it, and if needed, reinstall it." exit 1 fi diff --git a/jvb/jitsi-stats.schema b/jvb/jitsi-stats.schema deleted file mode 100644 index caeba5f..0000000 --- a/jvb/jitsi-stats.schema +++ /dev/null @@ -1,16 +0,0 @@ -CREATE TABLE IF NOT EXISTS conferences ( - id INTEGER PRIMARY_KEY, - conference_name TEXT, - conference_id TEXT, - start TEXT, - end TEXT -); -CREATE TABLE IF NOT EXISTS state ( - id INTEGER PRIMARY_KEY, - time TEXT, - filename TEXT, - filetime INTEGER, - position INTEGER CHECK(typeof(position)='integer'), - inode INTEGER -); -INSERT OR IGNORE INTO state (id, time, filename, filetime, position, inode) VALUES (1, '1970-01-01 00:00:00.000', '', 0, 0, 0);