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
This commit is contained in:
Adrian Gruntkowski 2023-10-10 14:13:11 +02:00 committed by GitHub
parent 21297b666f
commit dec193e904
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 41 additions and 1 deletions

View File

@ -25,6 +25,13 @@ defmodule Plausible.Release do
IO.puts("Migrations successful!")
end
def pending_migrations do
prepare()
IO.puts("Pending migrations")
IO.puts("")
Enum.each(repos(), &list_pending_migrations_for/1)
end
def seed do
prepare()
# Run seed script
@ -92,6 +99,28 @@ defmodule Plausible.Release do
{:ok, _, _} = Ecto.Migrator.with_repo(repo, &Ecto.Migrator.run(&1, :up, all: true))
end
defp list_pending_migrations_for(repo) do
IO.puts("Listing pending migrations for #{repo}")
IO.puts("")
migration_directory = Ecto.Migrator.migrations_path(repo)
pending =
repo
|> Ecto.Migrator.migrations([migration_directory])
|> Enum.filter(fn {status, _version, _migration} -> status == :down end)
if pending == [] do
IO.puts("No pending migrations")
else
Enum.each(pending, fn {_, version, migration} ->
IO.puts("* #{version}_#{migration}")
end)
end
IO.puts("")
end
defp ensure_repo_created(repo) do
IO.puts("create #{inspect(repo)} database if it doesn't exist")

View File

@ -9,6 +9,11 @@ defmodule Plausible.Repo.Migrations.FixBrokenGoals do
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
@ -23,7 +28,7 @@ defmodule Plausible.Repo.Migrations.FixBrokenGoals do
def down do
execute("""
ALTER TABLE goals
DROP CONSTRAINT check_event_name_or_page_path;
DROP CONSTRAINT IF EXISTS check_event_name_or_page_path;
""")
end
end

View File

@ -0,0 +1,6 @@
#!/bin/sh
# lists pending migrations
BIN_DIR=$(dirname "$0")
"${BIN_DIR}"/bin/plausible eval Plausible.Release.pending_migrations