From 2494c95b6a89986f56de501de9bfef4345aedcd5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Tue, 27 Oct 2020 18:41:50 +0100 Subject: [PATCH] Notify the repository of schema migration operations --- lib/ecto/migration/schema_migration.ex | 8 +++++--- lib/ecto/migrator.ex | 3 +-- test/test_repo.exs | 5 ++++- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/lib/ecto/migration/schema_migration.ex b/lib/ecto/migration/schema_migration.ex index 93248f7a..1da5944c 100644 --- a/lib/ecto/migration/schema_migration.ex +++ b/lib/ecto/migration/schema_migration.ex @@ -12,7 +12,9 @@ defmodule Ecto.Migration.SchemaMigration do timestamps updated_at: false end - @opts [timeout: :infinity, log: false] + # The migration flag is used to signal to the repository + # we are in a migration operation. + @opts [timeout: :infinity, log: false, schema_migration: true] def ensure_schema_migrations_table!(repo, config, opts) do {repo, source} = get_repo_and_source(repo, config) @@ -29,9 +31,9 @@ defmodule Ecto.Migration.SchemaMigration do repo.__adapter__().execute_ddl(meta, {:create_if_not_exists, table, commands}, @opts) end - def versions(repo, config) do + def versions(repo, config, prefix) do {repo, source} = get_repo_and_source(repo, config) - {repo, from(m in source, select: type(m.version, :integer))} + {repo, from(m in source, select: type(m.version, :integer)), [prefix: prefix] ++ @opts} end def up(repo, config, version, prefix) do diff --git a/lib/ecto/migrator.ex b/lib/ecto/migrator.ex index 5b7db841..4c9d41b2 100644 --- a/lib/ecto/migrator.ex +++ b/lib/ecto/migrator.ex @@ -500,8 +500,7 @@ defmodule Ecto.Migrator do end end - all_opts = [prefix: opts[:prefix], timeout: :infinity, log: false] - {migration_repo, query} = SchemaMigration.versions(repo, config) + {migration_repo, query, all_opts} = SchemaMigration.versions(repo, config, opts[:prefix]) result = if should_lock? do diff --git a/test/test_repo.exs b/test/test_repo.exs index 05713330..cc2b34f2 100644 --- a/test/test_repo.exs +++ b/test/test_repo.exs @@ -32,16 +32,19 @@ defmodule EctoSQL.TestAdapter do # Migration emulation - def execute(_, _, {:nocache, {:all, %{from: %{source: {"schema_migrations", _}}}}}, _, _) do + def execute(_, _, {:nocache, {:all, %{from: %{source: {"schema_migrations", _}}}}}, _, opts) do + true = opts[:schema_migration] {length(migrated_versions()), Enum.map(migrated_versions(), &[elem(&1, 0)])} end def execute(_, _meta, {:nocache, {:delete_all, %{from: %{source: {"schema_migrations", _}}}}}, [version], opts) do + true = opts[:schema_migration] Process.put(:migrated_versions, List.delete(migrated_versions(), {version, opts[:prefix]})) {1, nil} end def insert(_, %{source: "schema_migrations"}, val, _, _, opts) do + true = opts[:schema_migration] version = Keyword.fetch!(val, :version) Process.put(:migrated_versions, [{version, opts[:prefix]} | migrated_versions()]) {:ok, []}