Forces the use of the index on Addressables and Replaceables

This commit is contained in:
Vitor Pamplona
2025-12-26 14:29:10 -05:00
parent eeb0c11c65
commit 720e4d2c87
2 changed files with 8 additions and 4 deletions
@@ -36,6 +36,8 @@ class AddressableModule : IModule {
// if a newer addressable is inserted the unique index
// above will be triggered. Delete cascade will take
// care of the event_tags table
// the duplicate: kind >= 30000 AND kind < 40000
// helps SQLlite find the index above
db.execSQL(
"""
CREATE TRIGGER delete_older_addressable_event
@@ -48,7 +50,8 @@ class AddressableModule : IModule {
event_headers.kind = NEW.kind AND
event_headers.pubkey = NEW.pubkey AND
event_headers.d_tag = NEW.d_tag AND
event_headers.created_at < NEW.created_at;
event_headers.created_at < NEW.created_at AND
event_headers.kind >= 30000 AND event_headers.kind < 40000;
END;
""".trimIndent(),
)
@@ -28,7 +28,7 @@ class ReplaceableModule : IModule {
"""
CREATE UNIQUE INDEX replaceable_idx
ON event_headers (kind, pubkey)
WHERE (kind >= 10000 AND kind < 20000) OR (kind IN (0, 3))
WHERE (kind IN (0, 3)) OR (kind >= 10000 AND kind < 20000)
""".trimIndent(),
)
@@ -41,14 +41,15 @@ class ReplaceableModule : IModule {
CREATE TRIGGER delete_older_replaceable_event
BEFORE INSERT ON event_headers
FOR EACH ROW
WHEN (NEW.kind >= 10000 AND NEW.kind < 20000) OR (NEW.kind IN (0, 3))
WHEN (NEW.kind IN (0, 3)) OR (NEW.kind >= 10000 AND NEW.kind < 20000)
BEGIN
-- Delete older records if this is the newest
DELETE FROM event_headers
WHERE
event_headers.kind = NEW.kind AND
event_headers.pubkey = NEW.pubkey AND
event_headers.created_at < NEW.created_at;
event_headers.created_at < NEW.created_at AND
((event_headers.kind IN (0, 3)) OR (event_headers.kind >= 10000 AND event_headers.kind < 20000));
END;
""".trimIndent(),
)