Skip to content

Commit bf80ce9

Browse files
committed
Pass migration_source to adapters
1 parent 304c453 commit bf80ce9

File tree

6 files changed

+67
-69
lines changed

6 files changed

+67
-69
lines changed

lib/ecto/adapters/myxql.ex

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -227,29 +227,25 @@ defmodule Ecto.Adapters.MyXQL do
227227
def lock_for_migrations(meta, opts, fun) do
228228
%{opts: adapter_opts, repo: repo} = meta
229229

230-
if Keyword.get(adapter_opts, :migration_lock, true) do
231-
if Keyword.fetch(adapter_opts, :pool_size) == {:ok, 1} do
232-
Ecto.Adapters.SQL.raise_migration_pool_size_error()
233-
end
230+
if Keyword.fetch(adapter_opts, :pool_size) == {:ok, 1} do
231+
Ecto.Adapters.SQL.raise_migration_pool_size_error()
232+
end
234233

235-
opts = opts ++ [log: false, timeout: :infinity]
234+
opts = opts ++ [log: false, timeout: :infinity]
236235

237-
{:ok, result} =
238-
transaction(meta, opts, fn ->
239-
lock_name = "\"ecto_#{inspect(repo)}\""
236+
{:ok, result} =
237+
transaction(meta, opts, fn ->
238+
lock_name = "\"ecto_#{inspect(repo)}\""
240239

241-
try do
242-
{:ok, _} = Ecto.Adapters.SQL.query(meta, "SELECT GET_LOCK(#{lock_name}, -1)", [], opts)
243-
fun.()
244-
after
245-
{:ok, _} = Ecto.Adapters.SQL.query(meta, "SELECT RELEASE_LOCK(#{lock_name})", [], opts)
246-
end
247-
end)
240+
try do
241+
{:ok, _} = Ecto.Adapters.SQL.query(meta, "SELECT GET_LOCK(#{lock_name}, -1)", [], opts)
242+
fun.()
243+
after
244+
{:ok, _} = Ecto.Adapters.SQL.query(meta, "SELECT RELEASE_LOCK(#{lock_name})", [], opts)
245+
end
246+
end)
248247

249-
result
250-
else
251-
fun.()
252-
end
248+
result
253249
end
254250

255251
@impl true

lib/ecto/adapters/postgres.ex

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -207,27 +207,23 @@ defmodule Ecto.Adapters.Postgres do
207207
def lock_for_migrations(meta, opts, fun) do
208208
%{opts: adapter_opts} = meta
209209

210-
if Keyword.get(adapter_opts, :migration_lock, true) do
211-
if Keyword.fetch(adapter_opts, :pool_size) == {:ok, 1} do
212-
Ecto.Adapters.SQL.raise_migration_pool_size_error()
213-
end
214-
215-
opts = opts ++ [log: false, timeout: :infinity]
216-
217-
{:ok, result} =
218-
transaction(meta, opts, fn ->
219-
# SHARE UPDATE EXCLUSIVE MODE is the first lock that locks
220-
# itself but still allows updates to happen, see
221-
# # https://www.postgresql.org/docs/9.4/explicit-locking.html
222-
source = Keyword.get(adapter_opts, :migration_source, "schema_migrations")
223-
{:ok, _} = Ecto.Adapters.SQL.query(meta, "LOCK TABLE \"#{source}\" IN SHARE UPDATE EXCLUSIVE MODE", [], opts)
224-
fun.()
225-
end)
226-
227-
result
228-
else
229-
fun.()
210+
if Keyword.fetch(adapter_opts, :pool_size) == {:ok, 1} do
211+
Ecto.Adapters.SQL.raise_migration_pool_size_error()
230212
end
213+
214+
opts = opts ++ [log: false, timeout: :infinity]
215+
216+
{:ok, result} =
217+
transaction(meta, opts, fn ->
218+
# SHARE UPDATE EXCLUSIVE MODE is the first lock that locks
219+
# itself but still allows updates to happen, see
220+
# # https://www.postgresql.org/docs/9.4/explicit-locking.html
221+
source = Keyword.get(opts, :migration_source, "schema_migrations")
222+
{:ok, _} = Ecto.Adapters.SQL.query(meta, "LOCK TABLE \"#{source}\" IN SHARE UPDATE EXCLUSIVE MODE", [], opts)
223+
fun.()
224+
end)
225+
226+
result
231227
end
232228

233229
@impl true

lib/ecto/adapters/sql.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -579,7 +579,7 @@ defmodule Ecto.Adapters.SQL do
579579
Application.ensure_all_started(driver, type)
580580
end
581581

582-
@pool_opts [:timeout, :pool, :pool_size, :migration_lock] ++
582+
@pool_opts [:timeout, :pool, :pool_size] ++
583583
[:queue_target, :queue_interval, :ownership_timeout]
584584

585585
@doc false

lib/ecto/adapters/tds.ex

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -280,23 +280,19 @@ defmodule Ecto.Adapters.Tds do
280280
def lock_for_migrations(meta, opts, fun) do
281281
%{opts: adapter_opts, repo: repo} = meta
282282

283-
if Keyword.get(adapter_opts, :migration_lock, true) do
284-
if Keyword.fetch(adapter_opts, :pool_size) == {:ok, 1} do
285-
Ecto.Adapters.SQL.raise_migration_pool_size_error()
286-
end
287-
288-
opts = opts ++ [log: false, timeout: :infinity]
289-
290-
{:ok, result} =
291-
transaction(meta, opts, fn ->
292-
lock_name = "'ecto_#{inspect(repo)}'"
293-
Ecto.Adapters.SQL.query!(meta, "sp_getapplock @Resource = #{lock_name}, @LockMode = 'Exclusive', @LockOwner = 'Transaction', @LockTimeout = -1", [], opts)
294-
fun.()
295-
end)
296-
297-
result
298-
else
299-
fun.()
283+
if Keyword.fetch(adapter_opts, :pool_size) == {:ok, 1} do
284+
Ecto.Adapters.SQL.raise_migration_pool_size_error()
300285
end
286+
287+
opts = opts ++ [log: false, timeout: :infinity]
288+
289+
{:ok, result} =
290+
transaction(meta, opts, fn ->
291+
lock_name = "'ecto_#{inspect(repo)}'"
292+
Ecto.Adapters.SQL.query!(meta, "sp_getapplock @Resource = #{lock_name}, @LockMode = 'Exclusive', @LockOwner = 'Transaction', @LockTimeout = -1", [], opts)
293+
fun.()
294+
end)
295+
296+
result
301297
end
302298
end

lib/ecto/migrator.ex

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,8 +477,14 @@ defmodule Ecto.Migrator do
477477

478478
{migration_repo, query, all_opts} = SchemaMigration.versions(repo, config, opts[:prefix])
479479

480+
migration_lock? =
481+
Keyword.get(opts, :migration_lock, Keyword.get(config, :migration_lock, true))
482+
483+
opts =
484+
Keyword.put_new(opts, :migration_source, config[:migration_source] || "schema_migrations")
485+
480486
result =
481-
if lock_or_migration_number do
487+
if lock_or_migration_number && migration_lock? do
482488
# If there is a migration_repo, it wins over dynamic_repo,
483489
# otherwise the dynamic_repo is the one locked in migrations.
484490
meta_repo = if migration_repo != repo, do: migration_repo, else: dynamic_repo

test/ecto/migrator_test.exs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ defmodule Ecto.MigratorTest do
66
import ExUnit.CaptureLog
77

88
alias EctoSQL.TestRepo
9-
alias Ecto.Migration.SchemaMigration
109

1110
defmodule Migration do
1211
use Ecto.Migration
@@ -259,14 +258,6 @@ defmodule Ecto.MigratorTest do
259258
end)
260259
end
261260

262-
test "custom schema migrations table is right" do
263-
assert {_repo, "schema_migrations"} =
264-
SchemaMigration.get_repo_and_source(TestRepo, TestRepo.config())
265-
266-
assert {_repo, "my_schema_migrations"} =
267-
SchemaMigration.get_repo_and_source(MigrationSourceRepo, MigrationSourceRepo.config())
268-
end
269-
270261
test "migrator prefix" do
271262
capture_log(fn ->
272263
:ok = up(TestRepo, 10, ChangeMigration, prefix: :custom)
@@ -403,6 +394,17 @@ defmodule Ecto.MigratorTest do
403394
refute_received {:lock_for_migrations, _, _, _}
404395
end
405396

397+
test "on migration_source" do
398+
assert up(TestRepo, 9, Migration, log: false, migration_source: "custom") == :ok
399+
assert_receive {:lock_for_migrations, _, _, opts}
400+
assert opts[:migration_source] == "custom"
401+
end
402+
403+
test "on migration_lock" do
404+
assert up(TestRepo, 9, Migration, log: false, migration_lock: false) == :ok
405+
refute_receive {:lock_for_migrations, _, _, _}
406+
end
407+
406408
test "on run" do
407409
in_tmp fn path ->
408410
create_migration "13_sample.exs"
@@ -435,8 +437,9 @@ defmodule Ecto.MigratorTest do
435437
expected_result = [{:up, 15, "sample"}]
436438
assert migrations(TestRepo, path, skip_table_creation: true) == expected_result
437439

438-
assert_receive {:lock_for_migrations, _, _, [skip_table_creation: true]}
440+
assert_receive {:lock_for_migrations, _, _, opts}
439441
refute_received {:lock_for_migrations, _, _, _}
442+
assert opts[:skip_table_creation] == true
440443

441444
assert match?(nil, last_command())
442445
end
@@ -449,7 +452,8 @@ defmodule Ecto.MigratorTest do
449452
expected_result = [{:up, 15, "sample"}]
450453
assert migrations(TestRepo, path) == expected_result
451454

452-
assert_receive {:lock_for_migrations, _, _, []}
455+
assert_receive {:lock_for_migrations, _, _, opts}
456+
assert opts[:migration_source] == "schema_migrations"
453457
refute_received {:lock_for_migrations, _, _, _}
454458

455459
assert match?({:create_if_not_exists, %_{name: :schema_migrations}, _}, last_command())

0 commit comments

Comments
 (0)