analytics/priv/repo/migrations/20230914071244_fix_broken_goals.exs
Adrian Gruntkowski dec193e904
Make FixBrokenGoals migration idempotent (#3405)
* Make FixBrokenGoals migration idempotent

The migration in question was renamed in order to fix order of executing migrations when run
from the ground up (via https://github.com/plausible/analytics/pull/3378).

As a side effect, it's executed again on databases that had it applied earlier, with
a different timestamp prefix.

As this migration is safe to run multiple times, it was modified to make forward
migration work gracefully when constraint already exists.

* Add `pending-migrations.sh` release script
2023-10-10 14:13:11 +02:00

35 lines
764 B
Elixir

defmodule Plausible.Repo.Migrations.FixBrokenGoals do
use Ecto.Migration
@disable_ddl_transaction true
@disable_migration_lock true
def up do
execute("""
UPDATE goals SET page_path = NULL WHERE page_path IS NOT NULL AND event_name IS NOT NULL
""")
execute("""
ALTER TABLE goals
DROP CONSTRAINT IF EXISTS check_event_name_or_page_path;
""")
execute("""
ALTER TABLE goals
ADD CONSTRAINT check_event_name_or_page_path
CHECK (
(event_name IS NOT NULL AND page_path IS NULL) OR
(event_name IS NULL AND page_path IS NOT NULL)
)
NOT VALID;
""")
end
def down do
execute("""
ALTER TABLE goals
DROP CONSTRAINT IF EXISTS check_event_name_or_page_path;
""")
end
end