Skip to content

Commit

Permalink
Notify the repository of schema migration operations
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed Oct 27, 2020
1 parent 7dbb164 commit 2494c95
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
8 changes: 5 additions & 3 deletions lib/ecto/migration/schema_migration.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
3 changes: 1 addition & 2 deletions lib/ecto/migrator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion test/test_repo.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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, []}
Expand Down

0 comments on commit 2494c95

Please sign in to comment.