From 7fa37a359ae89affb68e7d00e2ccc3e787e711e8 Mon Sep 17 00:00:00 2001 From: Yordis Prieto Date: Tue, 27 Oct 2026 15:08:12 -0400 Subject: [PATCH 01/11] test(migrations): a database the fork creates records no schema version above upstream's Signed-off-by: Yordis Prieto --- test/migrate_event_store_test.exs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/test/migrate_event_store_test.exs b/test/migrate_event_store_test.exs index 0f9db10..de08e81 100644 --- a/test/migrate_event_store_test.exs +++ b/test/migrate_event_store_test.exs @@ -88,6 +88,14 @@ defmodule EventStore.MigrateEventStoreTest do end end + describe "event store created by the fork" do + test "should record no schema version above upstream's", %{config: config} do + create_new_database(config) + + assert "1.3.2" == latest_version(config, "public") + 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) From bab633051f93119b579de9d9db122ee9f872a11e Mon Sep 17 00:00:00 2001 From: Yordis Prieto Date: Tue, 27 Oct 2026 16:32:36 -0400 Subject: [PATCH 02/11] fix(migrations): leave upstream's schema versions to upstream so a database moved back still runs its migrations Signed-off-by: Yordis Prieto --- lib/event_store/sql/init.ex | 2 +- lib/event_store/tasks/migrate.ex | 17 +++++++++-------- lib/event_store/tasks/migrations.ex | 3 +-- .../subscription_lock_generation.sql | 1 + priv/event_store/migrations/v1.5.0.sql | 9 --------- test/migrate_event_store_test.exs | 2 +- 6 files changed, 13 insertions(+), 21 deletions(-) create mode 100644 priv/event_store/fork_migrations/subscription_lock_generation.sql delete mode 100644 priv/event_store/migrations/v1.5.0.sql diff --git a/lib/event_store/sql/init.ex b/lib/event_store/sql/init.ex index 18f5a97..2f56a5a 100644 --- a/lib/event_store/sql/init.ex +++ b/lib/event_store/sql/init.ex @@ -277,7 +277,7 @@ defmodule EventStore.Sql.Init do defp record_event_store_schema_version do """ INSERT INTO schema_migrations (major_version, minor_version, patch_version) - VALUES (1, 5, 0); + VALUES (1, 3, 2); """ end end diff --git a/lib/event_store/tasks/migrate.ex b/lib/event_store/tasks/migrate.ex index 74a8f6a..4d8708b 100644 --- a/lib/event_store/tasks/migrate.ex +++ b/lib/event_store/tasks/migrate.ex @@ -69,14 +69,15 @@ 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) + migrations = + conn |> newer_migrations(schema) |> Enum.map(&{"v#{&1}", "migrations/v#{&1}.sql"}) - if "1.5.0" in migrations or lock_generation?(conn, schema), + if lock_generation?(conn, schema), do: migrations, - else: migrations ++ ["1.5.0"] + else: + migrations ++ + [{"subscription_lock_generation", "fork_migrations/subscription_lock_generation.sql"}] end defp lock_generation?(conn, schema) do @@ -116,10 +117,10 @@ defmodule EventStore.Tasks.Migrate do end defp migrate(conn, schema, opts, migrations) do - for migration <- migrations do - write_info("Running migration v#{migration}...", opts) + for {migration, file} <- migrations do + write_info("Running migration #{migration}...", opts) - path = Application.app_dir(:eventstore, "priv/event_store/migrations/v#{migration}.sql") + path = Application.app_dir(:eventstore, "priv/event_store/#{file}") script = File.read!(path) statements = [ diff --git a/lib/event_store/tasks/migrations.ex b/lib/event_store/tasks/migrations.ex index b0f4307..b0bd378 100644 --- a/lib/event_store/tasks/migrations.ex +++ b/lib/event_store/tasks/migrations.ex @@ -19,8 +19,7 @@ defmodule EventStore.Tasks.Migrations do "1.1.0", "1.2.0", "1.3.0", - "1.3.2", - "1.5.0" + "1.3.2" ] @dialyzer {:no_return, exec: 2, handle_response: 1} diff --git a/priv/event_store/fork_migrations/subscription_lock_generation.sql b/priv/event_store/fork_migrations/subscription_lock_generation.sql new file mode 100644 index 0000000..ec5e304 --- /dev/null +++ b/priv/event_store/fork_migrations/subscription_lock_generation.sql @@ -0,0 +1 @@ +ALTER TABLE subscriptions ADD COLUMN lock_generation bigint NOT NULL DEFAULT 0; diff --git a/priv/event_store/migrations/v1.5.0.sql b/priv/event_store/migrations/v1.5.0.sql deleted file mode 100644 index 8ff4185..0000000 --- a/priv/event_store/migrations/v1.5.0.sql +++ /dev/null @@ -1,9 +0,0 @@ -DO $migration$ -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) - ON CONFLICT DO NOTHING; -END; -$migration$ LANGUAGE plpgsql; diff --git a/test/migrate_event_store_test.exs b/test/migrate_event_store_test.exs index de08e81..df91fc6 100644 --- a/test/migrate_event_store_test.exs +++ b/test/migrate_event_store_test.exs @@ -83,7 +83,7 @@ defmodule EventStore.MigrateEventStoreTest do migrate_eventstore(Keyword.put(config, :schema, "example"), quiet: true, is_mix: false) assert has_lock_generation?(config, "example") - assert "1.5.0" == latest_version(config, "example") + assert "1.3.2" == latest_version(config, "example") assert public_history == migration_history(config, "public") end end From b462533dfa2d92401e6280146f54f430a74c8044 Mon Sep 17 00:00:00 2001 From: Yordis Prieto Date: Tue, 27 Oct 2026 16:33:18 -0400 Subject: [PATCH 03/11] test(migrations): migrating a database the fork recorded as 1.5.0 leaves no version above upstream's Signed-off-by: Yordis Prieto --- test/migrate_event_store_test.exs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/test/migrate_event_store_test.exs b/test/migrate_event_store_test.exs index df91fc6..d4aa2e2 100644 --- a/test/migrate_event_store_test.exs +++ b/test/migrate_event_store_test.exs @@ -96,6 +96,25 @@ defmodule EventStore.MigrateEventStoreTest do end end + describe "migrate an event store the fork recorded as 1.5.0" do + test "should leave no schema version above upstream's", %{config: config} do + create_new_database(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") + assert "1.3.2" == latest_version(config, "public") + 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) From 4385652e30aa67c8d32c7ca5183be74b4d5be84a Mon Sep 17 00:00:00 2001 From: Yordis Prieto Date: Tue, 27 Oct 2026 16:33:51 -0400 Subject: [PATCH 04/11] fix(migrations): forget the 1.5.0 an earlier fork release recorded so upstream's migrate still sees what it has not run Signed-off-by: Yordis Prieto --- lib/event_store/tasks/migrate.ex | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/lib/event_store/tasks/migrate.ex b/lib/event_store/tasks/migrate.ex index 4d8708b..5a6ef72 100644 --- a/lib/event_store/tasks/migrate.ex +++ b/lib/event_store/tasks/migrate.ex @@ -32,6 +32,7 @@ defmodule EventStore.Tasks.Migrate do {:ok, conn} = Postgrex.start_link(config) try do + forget_fork_schema_version(conn, schema) migrations = available_migrations(conn, schema) migrate(conn, schema, opts, migrations) @@ -69,6 +70,21 @@ defmodule EventStore.Tasks.Migrate do Storage.Lock.try_acquire_exclusive_lock(conn, -1, schema: schema) end + # Earlier fork releases recorded the lock generation as schema version 1.5.0, + # which would hide upstream migrations up to 1.5.0 from upstream's migrate. + defp forget_fork_schema_version(conn, schema) do + if lock_generation?(conn, schema) do + Postgrex.query!( + conn, + """ + DELETE FROM "#{schema}".schema_migrations + WHERE (major_version, minor_version, patch_version) = (1, 5, 0) + """, + [] + ) + end + end + defp available_migrations(conn, schema) do migrations = conn |> newer_migrations(schema) |> Enum.map(&{"v#{&1}", "migrations/v#{&1}.sql"}) From 65ac8ca704915665598ad94cebad252b759c30e9 Mon Sep 17 00:00:00 2001 From: Yordis Prieto Date: Tue, 27 Oct 2026 16:34:19 -0400 Subject: [PATCH 05/11] test(migrations): migrating again keeps a 1.5.0 that upstream recorded Signed-off-by: Yordis Prieto --- test/migrate_event_store_test.exs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/test/migrate_event_store_test.exs b/test/migrate_event_store_test.exs index d4aa2e2..9a144c5 100644 --- a/test/migrate_event_store_test.exs +++ b/test/migrate_event_store_test.exs @@ -131,6 +131,25 @@ defmodule EventStore.MigrateEventStoreTest do assert has_lock_generation?(config, "public") end + + test "should keep upstream's 1.5.0 when migrated again", %{config: config} do + create_new_database(config) + + with_conn(config, fn conn -> + Postgrex.query!(conn, "ALTER TABLE subscriptions DROP COLUMN lock_generation", []) + + 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) + migrate_eventstore(config, quiet: true, is_mix: false) + + assert "1.5.0" == latest_version(config, "public") + end end defp has_lock_generation?(config, schema) do From 2aa4e02835f1c55a27d76c4b1a43ac2f31a970bd Mon Sep 17 00:00:00 2001 From: Yordis Prieto Date: Tue, 27 Oct 2026 17:26:21 -0400 Subject: [PATCH 06/11] fix(migrations): record the fork's migrations apart from upstream's so a later migrate cannot mistake upstream's 1.5.0 for the fork's Signed-off-by: Yordis Prieto --- lib/event_store/sql/init.ex | 23 +++++++- lib/event_store/tasks/migrate.ex | 55 ++++++------------- lib/event_store/tasks/migrations.ex | 7 +++ .../subscription_lock_generation.sql | 21 ++++++- test/migrate_event_store_test.exs | 3 + 5 files changed, 69 insertions(+), 40 deletions(-) diff --git a/lib/event_store/sql/init.ex b/lib/event_store/sql/init.ex index 2f56a5a..9f21620 100644 --- a/lib/event_store/sql/init.ex +++ b/lib/event_store/sql/init.ex @@ -30,7 +30,9 @@ defmodule EventStore.Sql.Init do create_subscription_index(), create_snapshots_table(column_data_type), create_schema_migrations_table(), - record_event_store_schema_version() + record_event_store_schema_version(), + create_fork_schema_migrations_table(), + record_fork_schema_migrations() ] end @@ -280,4 +282,23 @@ defmodule EventStore.Sql.Init do VALUES (1, 3, 2); """ end + + # record execution of the fork's own upgrade scripts, apart from upstream's + # versions so that upstream's migrate still sees the versions it has not run + def create_fork_schema_migrations_table do + """ + CREATE TABLE IF NOT EXISTS fork_schema_migrations + ( + name text PRIMARY KEY NOT NULL, + migrated_at timestamp with time zone DEFAULT NOW() NOT NULL + ); + """ + end + + defp record_fork_schema_migrations do + """ + INSERT INTO fork_schema_migrations (name) + VALUES ('subscription_lock_generation'); + """ + end end diff --git a/lib/event_store/tasks/migrate.ex b/lib/event_store/tasks/migrate.ex index 5a6ef72..1333185 100644 --- a/lib/event_store/tasks/migrate.ex +++ b/lib/event_store/tasks/migrate.ex @@ -6,6 +6,7 @@ defmodule EventStore.Tasks.Migrate do import EventStore.Tasks.Output alias EventStore.{Config, Storage} + alias EventStore.Sql.Init alias EventStore.Tasks.Migrations @dialyzer {:no_return, exec: 2, handle_response: 1} @@ -32,7 +33,6 @@ defmodule EventStore.Tasks.Migrate do {:ok, conn} = Postgrex.start_link(config) try do - forget_fork_schema_version(conn, schema) migrations = available_migrations(conn, schema) migrate(conn, schema, opts, migrations) @@ -70,48 +70,27 @@ defmodule EventStore.Tasks.Migrate do Storage.Lock.try_acquire_exclusive_lock(conn, -1, schema: schema) end - # Earlier fork releases recorded the lock generation as schema version 1.5.0, - # which would hide upstream migrations up to 1.5.0 from upstream's migrate. - defp forget_fork_schema_version(conn, schema) do - if lock_generation?(conn, schema) do - Postgrex.query!( - conn, - """ - DELETE FROM "#{schema}".schema_migrations - WHERE (major_version, minor_version, patch_version) = (1, 5, 0) - """, - [] - ) - end - end - defp available_migrations(conn, schema) do - migrations = + upstream = conn |> newer_migrations(schema) |> Enum.map(&{"v#{&1}", "migrations/v#{&1}.sql"}) - if lock_generation?(conn, schema), - do: migrations, - else: - migrations ++ - [{"subscription_lock_generation", "fork_migrations/subscription_lock_generation.sql"}] + fork = + conn + |> pending_fork_migrations(schema) + |> Enum.map(&{&1, "fork_migrations/#{&1}.sql"}) + + upstream ++ fork 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 + defp pending_fork_migrations(conn, schema) do + {:ok, %Postgrex.Result{rows: rows}} = + Postgrex.transaction(conn, fn transaction -> + Postgrex.query!(transaction, ~s(SET LOCAL search_path TO "#{schema}";), []) + Postgrex.query!(transaction, Init.create_fork_schema_migrations_table(), []) + Postgrex.query!(transaction, "SELECT name FROM fork_schema_migrations", []) + end) + + Migrations.available_fork_migrations() -- List.flatten(rows) end defp newer_migrations(conn, schema) do diff --git a/lib/event_store/tasks/migrations.ex b/lib/event_store/tasks/migrations.ex index b0bd378..81a25fb 100644 --- a/lib/event_store/tasks/migrations.ex +++ b/lib/event_store/tasks/migrations.ex @@ -22,6 +22,10 @@ defmodule EventStore.Tasks.Migrations do "1.3.2" ] + @available_fork_migrations [ + "subscription_lock_generation" + ] + @dialyzer {:no_return, exec: 2, handle_response: 1} @doc """ @@ -58,6 +62,9 @@ defmodule EventStore.Tasks.Migrations do @doc false def available_migrations(), do: @available_migrations + @doc false + def available_fork_migrations(), do: @available_fork_migrations + defp list_migration(%Migration{version: version, state: :pending}, opts) do write_info(" #{version |> String.pad_trailing(10)}\tpending", opts) end diff --git a/priv/event_store/fork_migrations/subscription_lock_generation.sql b/priv/event_store/fork_migrations/subscription_lock_generation.sql index ec5e304..e585363 100644 --- a/priv/event_store/fork_migrations/subscription_lock_generation.sql +++ b/priv/event_store/fork_migrations/subscription_lock_generation.sql @@ -1 +1,20 @@ -ALTER TABLE subscriptions ADD COLUMN lock_generation bigint NOT NULL DEFAULT 0; +DO $migration$ +BEGIN + -- Earlier fork releases added the lock generation and recorded it as schema + -- version 1.5.0, which would hide upstream migrations up to 1.5.0 from + -- upstream's migrate. + IF EXISTS ( + SELECT 1 FROM information_schema.columns + WHERE table_schema = current_schema() + AND table_name = 'subscriptions' + AND column_name = 'lock_generation' + ) THEN + DELETE FROM schema_migrations + WHERE (major_version, minor_version, patch_version) = (1, 5, 0); + ELSE + ALTER TABLE subscriptions ADD COLUMN lock_generation bigint NOT NULL DEFAULT 0; + END IF; + + INSERT INTO fork_schema_migrations (name) VALUES ('subscription_lock_generation'); +END; +$migration$ LANGUAGE plpgsql; diff --git a/test/migrate_event_store_test.exs b/test/migrate_event_store_test.exs index 9a144c5..ddb0a55 100644 --- a/test/migrate_event_store_test.exs +++ b/test/migrate_event_store_test.exs @@ -101,6 +101,8 @@ defmodule EventStore.MigrateEventStoreTest do create_new_database(config) with_conn(config, fn conn -> + Postgrex.query!(conn, "DROP TABLE fork_schema_migrations", []) + Postgrex.query!( conn, "INSERT INTO schema_migrations (major_version, minor_version, patch_version) VALUES (1, 5, 0)", @@ -136,6 +138,7 @@ defmodule EventStore.MigrateEventStoreTest do create_new_database(config) with_conn(config, fn conn -> + Postgrex.query!(conn, "DROP TABLE fork_schema_migrations", []) Postgrex.query!(conn, "ALTER TABLE subscriptions DROP COLUMN lock_generation", []) Postgrex.query!( From 26251f5ed8c9478bbfb16477807a0db23c6e51bf Mon Sep 17 00:00:00 2001 From: Yordis Prieto Date: Tue, 27 Oct 2026 17:26:40 -0400 Subject: [PATCH 07/11] test(migrations): list the fork's migrations by name Signed-off-by: Yordis Prieto --- test/list_event_store_migrations_test.exs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/test/list_event_store_migrations_test.exs b/test/list_event_store_migrations_test.exs index 0e689d2..6030c3b 100644 --- a/test/list_event_store_migrations_test.exs +++ b/test/list_event_store_migrations_test.exs @@ -26,7 +26,7 @@ defmodule EventStore.ListEventStoreMigrationsTest do 1.2.0 \tpending 1.3.0 \tpending 1.3.2 \tpending - 1.5.0 \tpending + subscription_lock_generation\tpending """ @@ -63,6 +63,7 @@ defmodule EventStore.ListEventStoreMigrationsTest do output |> String.split("\n") |> Enum.drop(5) |> Enum.filter(&(String.trim(&1) != "")) assert Enum.all?(migrations, &String.contains?(&1, "completed")) + assert Enum.any?(migrations, &String.contains?(&1, "subscription_lock_generation")) end test "exits normally when running via mix", %{config: config} do @@ -78,6 +79,7 @@ defmodule EventStore.ListEventStoreMigrationsTest do create_new_database(config) {:ok, conn} = Postgrex.start_link(config) + Postgrex.query!(conn, "DROP TABLE fork_schema_migrations", []) Postgrex.query!(conn, "ALTER TABLE subscriptions DROP COLUMN lock_generation", []) :ok = GenServer.stop(conn) From 770f797ac2fcdffc62c44b098fc31df63a8567c9 Mon Sep 17 00:00:00 2001 From: Yordis Prieto Date: Tue, 27 Oct 2026 17:27:27 -0400 Subject: [PATCH 08/11] fix(migrations): list the fork's migrations by name so their status does not read as an upstream version Signed-off-by: Yordis Prieto --- lib/event_store/tasks/migrations.ex | 52 +++++++++++++++++++---------- 1 file changed, 34 insertions(+), 18 deletions(-) diff --git a/lib/event_store/tasks/migrations.ex b/lib/event_store/tasks/migrations.ex index 81a25fb..37bc8e3 100644 --- a/lib/event_store/tasks/migrations.ex +++ b/lib/event_store/tasks/migrations.ex @@ -2,6 +2,10 @@ defmodule EventStore.Tasks.Migration do defstruct version: nil, state: :pending, migrated_at: nil end +defmodule EventStore.Tasks.ForkMigration do + defstruct name: nil, state: :pending, migrated_at: nil +end + defmodule EventStore.Tasks.Migrations do @moduledoc """ Task to show the migration status of EventStore @@ -10,7 +14,7 @@ defmodule EventStore.Tasks.Migrations do import EventStore.Tasks.Output alias EventStore.Config - alias EventStore.Tasks.Migration + alias EventStore.Tasks.{ForkMigration, Migration} @available_migrations [ "0.13.0", @@ -65,6 +69,14 @@ defmodule EventStore.Tasks.Migrations do @doc false def available_fork_migrations(), do: @available_fork_migrations + defp list_migration(%ForkMigration{name: name, state: :pending}, opts) do + write_info(" #{name |> String.pad_trailing(10)}\tpending", opts) + end + + defp list_migration(%ForkMigration{name: name, state: :completed, migrated_at: time}, opts) do + write_info(" #{name |> String.pad_trailing(10)}\tcompleted\t#{time}", opts) + end + defp list_migration(%Migration{version: version, state: :pending}, opts) do write_info(" #{version |> String.pad_trailing(10)}\tpending", opts) end @@ -86,27 +98,31 @@ defmodule EventStore.Tasks.Migrations do completed = query_schema_migrations(config, schema) latest_migration = completed |> Enum.reverse() |> Enum.at(0, nil) - pending = pending_migrations(latest_migration) + completed ++ pending_migrations(latest_migration) ++ fork_migrations(config, schema) + end + + defp fork_migrations(config, schema) do + completed = query_fork_schema_migrations(config, schema) - # 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"}] + Enum.map(@available_fork_migrations, fn name -> + Map.get(completed, name, %ForkMigration{name: name}) + end) end - defp lock_generation?(config, schema) do + defp query_fork_schema_migrations(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 + run_query(config, "SELECT to_regclass('\"#{schema}\".fork_schema_migrations') IS NOT NULL") + + if exists do + %Postgrex.Result{rows: rows} = + run_query(config, ~s(SELECT name, migrated_at FROM "#{schema}".fork_schema_migrations)) + + Map.new(rows, fn [name, migrated_at] -> + {name, %ForkMigration{name: name, state: :completed, migrated_at: migrated_at}} + end) + else + %{} + end end defp pending_migrations(nil), do: @available_migrations From 237aa73e97acf1e65fec08741c8eea557233bb40 Mon Sep 17 00:00:00 2001 From: Yordis Prieto Date: Tue, 27 Oct 2026 17:27:50 -0400 Subject: [PATCH 09/11] chore(docs): say where the fork records its migrations so operators know moving back to upstream is safe Signed-off-by: Yordis Prieto --- guides/Upgrades.md | 2 ++ .../fork_migrations/subscription_lock_generation.sql | 6 +++--- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/guides/Upgrades.md b/guides/Upgrades.md index 910cc29..264c41e 100644 --- a/guides/Upgrades.md +++ b/guides/Upgrades.md @@ -14,6 +14,8 @@ GRANT SELECT, UPDATE ON "public".subscriptions TO session_mode_role; Without these grants, persistent subscriptions never subscribe and the node logs an error naming the missing privileges. +This fork records its own migrations by name in a `fork_schema_migrations` table and leaves `schema_migrations` to upstream's versions, so a database moved back to upstream EventStore still runs every upstream migration it has not applied. + ## Migrate before rolling nodes Run the migration before starting nodes on the new version. A node on a version that expects a newer schema cannot acquire subscription locks against an unmigrated database and keeps retrying until the migration has run. Nodes on the previous version keep working against the migrated schema once the migration is in place. diff --git a/priv/event_store/fork_migrations/subscription_lock_generation.sql b/priv/event_store/fork_migrations/subscription_lock_generation.sql index e585363..2ebde67 100644 --- a/priv/event_store/fork_migrations/subscription_lock_generation.sql +++ b/priv/event_store/fork_migrations/subscription_lock_generation.sql @@ -1,8 +1,8 @@ DO $migration$ BEGIN - -- Earlier fork releases added the lock generation and recorded it as schema - -- version 1.5.0, which would hide upstream migrations up to 1.5.0 from - -- upstream's migrate. + -- Unreleased builds of the fork added the lock generation and recorded it as + -- schema version 1.5.0, which would hide upstream migrations up to 1.5.0 + -- from upstream's migrate. IF EXISTS ( SELECT 1 FROM information_schema.columns WHERE table_schema = current_schema() From cee00cbc603e13af13912ab1c6ce0225aa926636 Mon Sep 17 00:00:00 2001 From: Yordis Prieto Date: Tue, 27 Oct 2026 17:39:21 -0400 Subject: [PATCH 10/11] test(migrations): keep upstream's latest version when the fork's 1.5.0 was the only one recorded Signed-off-by: Yordis Prieto --- test/migrate_event_store_test.exs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/test/migrate_event_store_test.exs b/test/migrate_event_store_test.exs index ddb0a55..e739db2 100644 --- a/test/migrate_event_store_test.exs +++ b/test/migrate_event_store_test.exs @@ -115,6 +115,27 @@ defmodule EventStore.MigrateEventStoreTest do assert has_lock_generation?(config, "public") assert "1.3.2" == latest_version(config, "public") end + + test "should keep upstream's latest version when 1.5.0 was the only one recorded", %{ + config: config + } do + create_new_database(config) + + with_conn(config, fn conn -> + Postgrex.query!(conn, "DROP TABLE fork_schema_migrations", []) + Postgrex.query!(conn, "DELETE FROM schema_migrations", []) + + 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 "1.3.2" == latest_version(config, "public") + end end describe "migrate an event store that recorded upstream's 1.5.0" do From f049d15ae4e1510ee4811f6c8be8387a0c6991cb Mon Sep 17 00:00:00 2001 From: Yordis Prieto Date: Tue, 27 Oct 2026 17:42:34 -0400 Subject: [PATCH 11/11] fix(migrations): keep upstream's latest version recorded when the fork's 1.5.0 was the only one Signed-off-by: Yordis Prieto --- .../fork_migrations/subscription_lock_generation.sql | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/priv/event_store/fork_migrations/subscription_lock_generation.sql b/priv/event_store/fork_migrations/subscription_lock_generation.sql index 2ebde67..9844466 100644 --- a/priv/event_store/fork_migrations/subscription_lock_generation.sql +++ b/priv/event_store/fork_migrations/subscription_lock_generation.sql @@ -11,6 +11,13 @@ BEGIN ) THEN DELETE FROM schema_migrations WHERE (major_version, minor_version, patch_version) = (1, 5, 0); + + -- Event stores those builds created recorded 1.5.0 alone, and an empty + -- history makes upstream's migrate run every migration again. + IF NOT EXISTS (SELECT 1 FROM schema_migrations) THEN + INSERT INTO schema_migrations (major_version, minor_version, patch_version) + VALUES (1, 3, 2); + END IF; ELSE ALTER TABLE subscriptions ADD COLUMN lock_generation bigint NOT NULL DEFAULT 0; END IF;