diff --git a/lib/event_store/tasks/migrate.ex b/lib/event_store/tasks/migrate.ex index b873ccfe..74a8f6a3 100644 --- a/lib/event_store/tasks/migrate.ex +++ b/lib/event_store/tasks/migrate.ex @@ -69,7 +69,35 @@ defmodule EventStore.Tasks.Migrate do Storage.Lock.try_acquire_exclusive_lock(conn, -1, schema: schema) end + # Upstream can record its own 1.5.0, which would otherwise hide the fork's + # lock generation migration behind a version that is already recorded. defp available_migrations(conn, schema) do + migrations = newer_migrations(conn, schema) + + if "1.5.0" in migrations or lock_generation?(conn, schema), + do: migrations, + else: migrations ++ ["1.5.0"] + end + + defp lock_generation?(conn, schema) do + %Postgrex.Result{rows: [[exists]]} = + Postgrex.query!( + conn, + """ + SELECT EXISTS ( + SELECT 1 FROM information_schema.columns + WHERE table_schema = $1 + AND table_name = 'subscriptions' + AND column_name = 'lock_generation' + ) + """, + [schema] + ) + + exists + end + + defp newer_migrations(conn, schema) do case event_store_schema_version(conn, schema) do %Version{} = event_store_version -> # Only run newer migrations diff --git a/lib/event_store/tasks/migrations.ex b/lib/event_store/tasks/migrations.ex index a8527611..b0f43074 100644 --- a/lib/event_store/tasks/migrations.ex +++ b/lib/event_store/tasks/migrations.ex @@ -80,7 +80,27 @@ defmodule EventStore.Tasks.Migrations do completed = query_schema_migrations(config, schema) latest_migration = completed |> Enum.reverse() |> Enum.at(0, nil) - completed ++ pending_migrations(latest_migration) + pending = pending_migrations(latest_migration) + + # Upstream can record its own 1.5.0, which would otherwise hide the fork's + # lock generation migration behind a version that is already recorded. + if Enum.any?(pending, &(&1.version == "1.5.0")) or lock_generation?(config, schema), + do: completed ++ pending, + else: completed ++ pending ++ [%Migration{version: "1.5.0"}] + end + + defp lock_generation?(config, schema) do + %Postgrex.Result{rows: [[exists]]} = + run_query(config, """ + SELECT EXISTS ( + SELECT 1 FROM information_schema.columns + WHERE table_schema = '#{schema}' + AND table_name = 'subscriptions' + AND column_name = 'lock_generation' + ) + """) + + exists end defp pending_migrations(nil), do: @available_migrations diff --git a/priv/event_store/migrations/v1.5.0.sql b/priv/event_store/migrations/v1.5.0.sql index ebbddb42..8ff4185c 100644 --- a/priv/event_store/migrations/v1.5.0.sql +++ b/priv/event_store/migrations/v1.5.0.sql @@ -3,6 +3,7 @@ BEGIN ALTER TABLE subscriptions ADD COLUMN lock_generation bigint NOT NULL DEFAULT 0; -- Record schema migration - INSERT INTO schema_migrations (major_version, minor_version, patch_version) VALUES (1, 5, 0); + INSERT INTO schema_migrations (major_version, minor_version, patch_version) VALUES (1, 5, 0) + ON CONFLICT DO NOTHING; END; $migration$ LANGUAGE plpgsql; diff --git a/test/list_event_store_migrations_test.exs b/test/list_event_store_migrations_test.exs index f45bab7d..0e689d2d 100644 --- a/test/list_event_store_migrations_test.exs +++ b/test/list_event_store_migrations_test.exs @@ -73,6 +73,21 @@ defmodule EventStore.ListEventStoreMigrationsTest do end end + describe "event store that recorded upstream's 1.5.0" do + test "lists the lock generation migration as pending", %{config: config} do + create_new_database(config) + + {:ok, conn} = Postgrex.start_link(config) + Postgrex.query!(conn, "ALTER TABLE subscriptions DROP COLUMN lock_generation", []) + :ok = GenServer.stop(conn) + + states = capture_io(fn -> send(self(), EventStore.Tasks.Migrations.exec(config)) end) + + assert_received states when is_list(states) + assert :pending in states + end + end + defp create_new_database(config) do :ok = EventStore.Storage.Database.drop(config) :ok = EventStore.Storage.Database.create(config) diff --git a/test/migrate_event_store_test.exs b/test/migrate_event_store_test.exs index 861e40f7..0f9db109 100644 --- a/test/migrate_event_store_test.exs +++ b/test/migrate_event_store_test.exs @@ -88,6 +88,24 @@ defmodule EventStore.MigrateEventStoreTest do end end + describe "migrate an event store that recorded upstream's 1.5.0" do + test "should still add the lock generation", %{config: config} do + restore_pre_migration_event_store(config) + + with_conn(config, fn conn -> + Postgrex.query!( + conn, + "INSERT INTO schema_migrations (major_version, minor_version, patch_version) VALUES (1, 5, 0)", + [] + ) + end) + + migrate_eventstore(config, quiet: true, is_mix: false) + + assert has_lock_generation?(config, "public") + end + end + defp has_lock_generation?(config, schema) do with_conn(config, fn conn -> %Postgrex.Result{rows: [[exists]]} =