diff --git a/src/ssb.db.c b/src/ssb.db.c index c4af262b..e2864ada 100644 --- a/src/ssb.db.c +++ b/src/ssb.db.c @@ -142,12 +142,6 @@ void tf_ssb_db_init(tf_ssb_t* ssb) " ref TEXT, " " UNIQUE(message, ref)" ")"); - _tf_ssb_db_exec(db, "CREATE TRIGGER IF NOT EXISTS messages_ai_refs AFTER INSERT ON messages BEGIN " - "INSERT INTO messages_refs(message, ref) " - "SELECT new.id, j.value FROM json_each(new.content) as j WHERE " - "j.value LIKE '&%.sha256' OR " - "j.value LIKE '%%%.sha256' OR " - "j.value LIKE '@%.ed25519'; END"); printf("Populating messages_refs...\n"); _tf_ssb_db_exec(db, "INSERT INTO messages_refs(message, ref) " "SELECT messages.id, j.value FROM messages, json_each(messages.content) as j WHERE " @@ -158,6 +152,14 @@ void tf_ssb_db_init(tf_ssb_t* ssb) printf("Done.\n"); } + _tf_ssb_db_exec(db, "DROP TRIGGER IF EXISTS messages_ai_refs"); + _tf_ssb_db_exec(db, "CREATE TRIGGER IF NOT EXISTS messages_ai_refs AFTER INSERT ON messages BEGIN " + "INSERT INTO messages_refs(message, ref) " + "SELECT DISTINCT new.id, j.value FROM json_each(new.content) as j WHERE " + "j.value LIKE '&%.sha256' OR " + "j.value LIKE '%%%.sha256' OR " + "j.value LIKE '@%.ed25519' " + "ON CONFLICT DO NOTHING; END"); _tf_ssb_db_exec(db, "DROP TRIGGER IF EXISTS messages_ad_refs"); _tf_ssb_db_exec(db, "CREATE TRIGGER IF NOT EXISTS messages_ad_refs AFTER DELETE ON messages BEGIN DELETE FROM messages_refs WHERE messages_refs.message = old.id; END");