From 82e876a89276636de2cc8645a36c717154d7dab2 Mon Sep 17 00:00:00 2001 From: Cory McWilliams Date: Fri, 24 Jan 2025 18:06:31 -0500 Subject: [PATCH] ssb: Only re-populate the messages_stats table on first creation. --- src/ssb.db.c | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/ssb.db.c b/src/ssb.db.c index 3d311ec0..81244a5a 100644 --- a/src/ssb.db.c +++ b/src/ssb.db.c @@ -108,12 +108,16 @@ void tf_ssb_db_init(tf_ssb_t* ssb) " flags INTEGER," " UNIQUE(author, sequence)" ")"); - _tf_ssb_db_exec(db, - "CREATE TABLE IF NOT EXISTS messages_stats (" - " author TEXT PRIMARY KEY," - " max_sequence INTEGER," - " max_timestamp READ" - ")"); + if (!_tf_ssb_db_has_rows(db, "PRAGMA table_list('messages_stats')")) + { + _tf_ssb_db_exec(db, + "CREATE TABLE IF NOT EXISTS messages_stats (" + " author TEXT PRIMARY KEY," + " max_sequence INTEGER," + " max_timestamp READ" + ")"); + _tf_ssb_db_exec(db, "INSERT OR REPLACE INTO messages_stats (author, max_sequence, max_timestamp) SELECT author, MAX(sequence), MAX(timestamp) FROM messages GROUP BY author"); + } _tf_ssb_db_exec(db, "CREATE TRIGGER IF NOT EXISTS messages_ai_stats AFTER INSERT ON messages BEGIN INSERT INTO messages_stats(author, max_sequence, max_timestamp) VALUES (new.author, " "new.sequence, new.timestamp) ON CONFLICT DO UPDATE SET max_sequence = MAX(max_sequence, excluded.max_sequence), max_timestamp = MAX(max_timestamp, " @@ -121,7 +125,6 @@ void tf_ssb_db_init(tf_ssb_t* ssb) _tf_ssb_db_exec(db, "CREATE TRIGGER IF NOT EXISTS messages_ad_stats AFTER DELETE ON messages BEGIN UPDATE messages_stats SET max_sequence = (SELECT MAX(messages.sequence) FROM messages WHERE " "messages.author = old.author), max_timestamp = (SELECT MAX(messages.timestamp) FROM messages WHERE messages.author = old.author); END"); - _tf_ssb_db_exec(db, "INSERT OR REPLACE INTO messages_stats (author, max_sequence, max_timestamp) SELECT author, MAX(sequence), MAX(timestamp) FROM messages GROUP BY author"); if (_tf_ssb_db_has_rows(db, "SELECT name FROM pragma_table_info('messages') WHERE name = 'content' AND type == 'TEXT'")) {