fix: add cleanup triggers for orphaned conditions

When a regex or language is deleted, the FK cascade deletes the
condition_patterns/condition_languages row but leaves the parent
condition orphaned. These triggers clean up the parent condition.
This commit is contained in:
Sam Chau
2026-02-11 02:36:34 +10:30
parent 29557fdfe8
commit 00bca26d38
3 changed files with 27 additions and 1 deletions
+19
View File
@@ -218,6 +218,16 @@ CREATE TABLE condition_patterns (
FOREIGN KEY (regular_expression_name) REFERENCES regular_expressions(name) ON DELETE CASCADE ON UPDATE CASCADE
);
-- Clean up orphaned conditions when their pattern reference is cascade-deleted
-- (e.g., when the referenced regex is deleted upstream)
CREATE TRIGGER trg_condition_patterns_cleanup
AFTER DELETE ON condition_patterns
BEGIN
DELETE FROM custom_format_conditions
WHERE custom_format_name = OLD.custom_format_name
AND name = OLD.condition_name;
END;
-- Language-based conditions
-- Uses stable keys: (custom_format_name, condition_name) and language_name
CREATE TABLE condition_languages (
@@ -230,6 +240,15 @@ CREATE TABLE condition_languages (
FOREIGN KEY (language_name) REFERENCES languages(name) ON DELETE CASCADE ON UPDATE CASCADE
);
-- Clean up orphaned conditions when their language reference is cascade-deleted
CREATE TRIGGER trg_condition_languages_cleanup
AFTER DELETE ON condition_languages
BEGIN
DELETE FROM custom_format_conditions
WHERE custom_format_name = OLD.custom_format_name
AND name = OLD.condition_name;
END;
-- Indexer flag conditions (e.g., "Scene", "Freeleech")
-- Uses stable key: (custom_format_name, condition_name)
CREATE TABLE condition_indexer_flags (