Skip to content

Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.

Make FixBrokenGoals migration idempotent (#3405)

Adrian Gruntkowskidec193e90458
diff --git a/lib/plausible_release.ex b/lib/plausible_release.ex
index a63506315b..9ed38f7aec 100644
--- a/lib/plausible_release.ex
+++ b/lib/plausible_release.ex
@@ -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")
diff --git a/priv/repo/migrations/20230914071244_fix_broken_goals.exs b/priv/repo/migrations/20230914071244_fix_broken_goals.exs
index 4586b64d33..d65369926d 100644
--- a/priv/repo/migrations/20230914071244_fix_broken_goals.exs
+++ b/priv/repo/migrations/20230914071244_fix_broken_goals.exs
@@ -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
diff --git a/rel/overlays/pending-migrations.sh b/rel/overlays/pending-migrations.sh
new file mode 100755
index 0000000000..12b2ce9e0d
--- /dev/null
+++ b/rel/overlays/pending-migrations.sh
@@ -0,0 +1,6 @@
+#!/bin/sh
+# lists pending migrations
+
+BIN_DIR=$(dirname "$0")
+
+"${BIN_DIR}"/bin/plausible eval Plausible.Release.pending_migrations