@@ -58,93 +58,12 @@ def upgrade() -> None:
5858 sa .PrimaryKeyConstraint ("id" ),
5959 )
6060
61- # 3. Trigger Function: Sync defaults into arrays
62- op .execute ("""
63- CREATE OR REPLACE FUNCTION sync_config_defaults()
64- RETURNS trigger AS $$
65- BEGIN
66- IF NEW.download_configs IS NULL THEN
67- NEW.download_configs := ARRAY[NEW.default_number_of_downloads];
68- ELSIF NOT (NEW.default_number_of_downloads = ANY(NEW.download_configs)) THEN
69- NEW.download_configs := array_append(NEW.download_configs, NEW.default_number_of_downloads);
70- END IF;
71-
72- IF NEW.time_configs IS NULL THEN
73- NEW.time_configs := ARRAY[NEW.default_expiry];
74- ELSIF NOT (NEW.default_expiry = ANY(NEW.time_configs)) THEN
75- NEW.time_configs := array_append(NEW.time_configs, NEW.default_expiry);
76- END IF;
77-
78- RETURN NEW;
79- END;
80- $$ LANGUAGE plpgsql;
81- """ )
82-
83- # 4. Trigger Function: Singleton
84- op .execute ("""
85- CREATE OR REPLACE FUNCTION enforce_singleton()
86- RETURNS trigger AS $$
87- BEGIN
88- IF (SELECT COUNT(*) FROM config) >= 1 THEN
89- RAISE EXCEPTION 'Only one row is allowed in config';
90- END IF;
91- RETURN NEW;
92- END;
93- $$ LANGUAGE plpgsql;
94- """ )
95-
96- # 5. Trigger Function: Delete Prevention
97- op .execute ("""
98- CREATE OR REPLACE FUNCTION prevent_config_deletion()
99- RETURNS trigger AS $$
100- BEGIN
101- RAISE EXCEPTION 'The configuration record cannot be deleted';
102- END;
103- $$ LANGUAGE plpgsql;
104- """ )
105-
106- # 6. Trigger Function: Validate File Type Exclusivity
107- # Uses the Postgres overlap operator (&&) to check for common elements
108- op .execute ("""
109- CREATE OR REPLACE FUNCTION validate_file_types()
110- RETURNS trigger AS $$
111- BEGIN
112- IF NEW.allowed_file_types && NEW.banned_file_types THEN
113- RAISE EXCEPTION 'Conflict: allowed_file_types and banned_file_types cannot share common extensions';
114- END IF;
115- RETURN NEW;
116- END;
117- $$ LANGUAGE plpgsql;
118- """ )
119-
120- # 7. Apply Triggers
121- op .execute (
122- "CREATE TRIGGER sync_defaults_trigger BEFORE INSERT OR UPDATE ON config FOR EACH ROW EXECUTE FUNCTION sync_config_defaults();"
123- )
124- op .execute (
125- "CREATE TRIGGER singleton_trigger BEFORE INSERT ON config FOR EACH ROW EXECUTE FUNCTION enforce_singleton();"
126- )
127- op .execute (
128- "CREATE TRIGGER prevent_deletion_trigger BEFORE DELETE ON config FOR EACH ROW EXECUTE FUNCTION prevent_config_deletion();"
129- )
130- op .execute (
131- "CREATE TRIGGER validate_file_types_trigger BEFORE INSERT OR UPDATE ON config FOR EACH ROW EXECUTE FUNCTION validate_file_types();"
132- )
133-
134- # 8. Seed Initial Data
61+ # 3. Seed Initial Data
13562 config_instance = Config ()
13663 data = config_instance .model_dump (exclude = {"id" })
13764 op .bulk_insert (sa .table ("config" , * [sa .column (k ) for k in data .keys ()]), [data ])
13865
13966
14067def downgrade () -> None :
141- op .execute ("DROP TRIGGER IF EXISTS validate_file_types_trigger ON config;" )
142- op .execute ("DROP TRIGGER IF EXISTS prevent_deletion_trigger ON config;" )
143- op .execute ("DROP TRIGGER IF EXISTS singleton_trigger ON config;" )
144- op .execute ("DROP TRIGGER IF EXISTS sync_defaults_trigger ON config;" )
145- op .execute ("DROP FUNCTION IF EXISTS validate_file_types();" )
146- op .execute ("DROP FUNCTION IF EXISTS prevent_config_deletion();" )
147- op .execute ("DROP FUNCTION IF EXISTS enforce_singleton();" )
148- op .execute ("DROP FUNCTION IF EXISTS sync_config_defaults();" )
14968 op .drop_table ("file" )
15069 op .drop_table ("config" )
0 commit comments