Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions lib/event_store/tasks/migrate.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
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
Expand Down
22 changes: 21 additions & 1 deletion lib/event_store/tasks/migrations.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion priv/event_store/migrations/v1.5.0.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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;
15 changes: 15 additions & 0 deletions test/list_event_store_migrations_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
18 changes: 18 additions & 0 deletions test/migrate_event_store_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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]]} =
Expand Down
Loading