2019-06-03 05:41:00 -04:00
# frozen_string_literal: true
class AddThemeIdToJavascriptCache < ActiveRecord :: Migration [ 5 . 2 ]
def up
2019-06-04 05:01:20 -04:00
# Delete any javascript caches with broken foreign keys
execute << ~ SQL
DELETE FROM javascript_caches jc
WHERE NOT EXISTS (
SELECT 1
FROM theme_fields tf
WHERE tf . id = jc . theme_field_id
) ;
SQL
2019-06-03 05:41:00 -04:00
make_changes
execute " ALTER TABLE javascript_caches ADD CONSTRAINT enforce_theme_or_theme_field CHECK ((theme_id IS NOT NULL AND theme_field_id IS NULL) OR (theme_id IS NULL AND theme_field_id IS NOT NULL)) "
end
2023-12-15 10:46:04 -05:00
2019-06-03 05:41:00 -04:00
def down
execute " ALTER TABLE javascript_caches DROP CONSTRAINT enforce_theme_or_theme_field "
revert { make_changes }
end
private
def make_changes
add_reference :javascript_caches , :theme , foreign_key : { on_delete : :cascade }
add_foreign_key :javascript_caches , :theme_fields , on_delete : :cascade
begin
Migration :: SafeMigrate . disable!
change_column_null :javascript_caches , :theme_field_id , true
ensure
Migration :: SafeMigrate . enable!
end
end
end