Skip to content

Commit 953b8c2

Browse files
authored
Forwards the :database option to the migration collection calls (#177)
* Forwards the :database option to the migration collection calls This is a way we can support dynamic multitenancy. If you pass in the :database option into the `migrate/1` function, it will forward it onto each migrations `up` function, but we also need it to use the database option for each of the calls to the migration collection, so that each tenant's migration states are kept separate. * formatting
1 parent a51589c commit 953b8c2

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

lib/mongo/migration.ex

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,12 @@ defmodule Mongo.Migration do
3434
def lock(opts \\ []) do
3535
topology = get_config(opts)[:topology]
3636
collection = get_config(opts)[:collection]
37+
collection_opts = get_database_opts(opts) |> Keyword.put(:upsert, true)
3738

3839
query = %{_id: "lock", used: false}
3940
set = %{"$set": %{used: true}}
4041

41-
case Mongo.update_one(topology, collection, query, set, upsert: true) do
42+
case Mongo.update_one(topology, collection, query, set, collection_opts) do
4243
{:ok, %{modified_count: 1}} ->
4344
IO.puts("🔒 #{collection} locked")
4445
:locked
@@ -57,8 +58,9 @@ defmodule Mongo.Migration do
5758
collection = get_config(opts)[:collection]
5859
query = %{_id: "lock", used: true}
5960
set = %{"$set": %{used: false}}
61+
collection_opts = get_database_opts(opts)
6062

61-
case Mongo.update_one(topology, collection, query, set) do
63+
case Mongo.update_one(topology, collection, query, set, collection_opts) do
6264
{:ok, %{modified_count: 1}} ->
6365
IO.puts("🔓 #{collection} unlocked")
6466
:unlocked
@@ -71,8 +73,9 @@ defmodule Mongo.Migration do
7173
defp run_up(version, mod, opts) do
7274
topology = get_config(opts)[:topology]
7375
collection = get_config(opts)[:collection]
76+
collection_opts = get_database_opts(opts)
7477

75-
case Mongo.find_one(topology, collection, %{version: version}) do
78+
case Mongo.find_one(topology, collection, %{version: version}, collection_opts) do
7679
nil ->
7780
## check, if the function supports options
7881

@@ -87,7 +90,7 @@ defmodule Mongo.Migration do
8790
raise "The module does not export the up function!"
8891
end
8992

90-
Mongo.insert_one(topology, collection, %{version: version})
93+
Mongo.insert_one(topology, collection, %{version: version}, collection_opts)
9194
IO.puts("⚡️ Successfully migrated #{mod}")
9295

9396
_other ->
@@ -103,8 +106,9 @@ defmodule Mongo.Migration do
103106
defp run_down(version, mod, opts) do
104107
topology = get_config(opts)[:topology]
105108
collection = get_config(opts)[:collection]
109+
collection_opts = get_database_opts(opts)
106110

107-
case Mongo.find_one(topology, collection, %{version: version}) do
111+
case Mongo.find_one(topology, collection, %{version: version}, collection_opts) do
108112
%{"version" => _version} ->
109113
## check, if the function supports options
110114
cond do
@@ -118,7 +122,7 @@ defmodule Mongo.Migration do
118122
raise "The module does not export the down function!"
119123
end
120124

121-
Mongo.delete_one(topology, collection, %{version: version})
125+
Mongo.delete_one(topology, collection, %{version: version}, collection_opts)
122126
IO.puts("💥 Successfully dropped #{mod}")
123127

124128
_other ->
@@ -177,4 +181,8 @@ defmodule Mongo.Migration do
177181
{mod, version}
178182
end)
179183
end
184+
185+
defp get_database_opts(opts) do
186+
if opts[:database], do: [database: opts[:database]], else: []
187+
end
180188
end

test/mongo/migration_test.exs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,18 @@ defmodule Mongo.MigrationTest do
1212
assert :unlocked == Migration.unlock()
1313
assert {:error, :not_locked} == Migration.unlock()
1414
end
15+
16+
test "test lock and unlock with database options", %{pid: top} do
17+
Mongo.drop_collection(top, "migrations", database: "one")
18+
Mongo.drop_collection(top, "migrations", database: "two")
19+
Patch.patch(Mongo.Migration, :get_config, fn _ -> [topology: top, collection: "migrations", path: "migrations", otp_app: :mongodb_driver] end)
20+
assert :locked == Migration.lock(database: "one")
21+
assert :locked == Migration.lock(database: "two")
22+
assert {:error, :already_locked} == Migration.lock(database: "one")
23+
assert {:error, :already_locked} == Migration.lock(database: "two")
24+
assert :unlocked == Migration.unlock(database: "one")
25+
assert :unlocked == Migration.unlock(database: "two")
26+
assert {:error, :not_locked} == Migration.unlock(database: "one")
27+
assert {:error, :not_locked} == Migration.unlock(database: "two")
28+
end
1529
end

0 commit comments

Comments
 (0)