Skip to content

Commit 48a3e76

Browse files
committed
refactor deprecated with_transaction function
1 parent 0ce5e85 commit 48a3e76

File tree

4 files changed

+89
-91
lines changed

4 files changed

+89
-91
lines changed

lib/mongo/bulk_write.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ defmodule Mongo.BulkWrite do
182182
Everything is done in memory, so this use case is limited by memory. A better approach seems to use streaming bulk writes.
183183
"""
184184
@spec write(GenServer.server(), UnorderedBulk.t() | OrderedBulk.t(), Keyword.t()) :: Mongo.BulkWriteResult.t()
185+
def write(topology_pid, bulk, opts \\ [])
185186
def write(topology_pid, %UnorderedBulk{} = bulk, opts) do
186187
in_write_session(topology_pid, &one_bulk_write(&1, topology_pid, bulk, &2), opts)
187188
end

test/mongo/grid_fs/upload_test.exs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ defmodule Mongo.GridFs.UploadTest do
33

44
alias Mongo.GridFs.Bucket
55
alias Mongo.GridFs.Upload
6-
alias Mongo.Session
76

87
setup_all do
98
assert {:ok, pid} = Mongo.TestConnection.connect()
@@ -96,10 +95,9 @@ defmodule Mongo.GridFs.UploadTest do
9695
bucket = Bucket.new(c.pid)
9796

9897
{:ok, upload_stream} =
99-
Session.with_transaction(
98+
Mongo.transaction(
10099
c.pid,
101-
fn opt ->
102-
bucket = Bucket.add_session(bucket, opt)
100+
fn ->
103101
upload_stream = Upload.open_upload_stream(bucket, "my-example-file.txt", %{tag: "checked", chk_sum: chksum})
104102
File.stream!(src_filename, [], 512) |> Stream.into(upload_stream) |> Stream.run()
105103
{:ok, upload_stream}
@@ -122,10 +120,9 @@ defmodule Mongo.GridFs.UploadTest do
122120
bucket = Bucket.new(c.pid)
123121

124122
{:error, upload_stream} =
125-
Session.with_transaction(
123+
Mongo.transaction(
126124
c.pid,
127-
fn opt ->
128-
bucket = Bucket.add_session(bucket, opt)
125+
fn ->
129126
upload_stream = Upload.open_upload_stream(bucket, "my-example-file.txt", %{tag: "checked", chk_sum: chksum})
130127
File.stream!(src_filename, [], 512) |> Stream.into(upload_stream) |> Stream.run()
131128
{:error, upload_stream}

test/mongo/session_test.exs

Lines changed: 43 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -209,20 +209,20 @@ defmodule Mongo.SessionTest do
209209
end
210210

211211
@tag :mongo_4_2
212-
test "with_transaction", %{top: top} do
212+
test "transaction", %{top: top} do
213213
coll = "dogs_with_commit_transaction"
214214

215215
Mongo.insert_one(top, coll, %{name: "Wuff"})
216216
Mongo.delete_many(top, coll, %{})
217217

218-
Session.with_transaction(
218+
Mongo.transaction(
219219
top,
220-
fn opts ->
221-
{:ok, %InsertOneResult{:inserted_id => id}} = Mongo.insert_one(top, coll, %{name: "Greta"}, opts)
220+
fn ->
221+
{:ok, %InsertOneResult{:inserted_id => id}} = Mongo.insert_one(top, coll, %{name: "Greta"})
222222
assert id != nil
223-
{:ok, %InsertOneResult{:inserted_id => id}} = Mongo.insert_one(top, coll, %{name: "Waldo"}, opts)
223+
{:ok, %InsertOneResult{:inserted_id => id}} = Mongo.insert_one(top, coll, %{name: "Waldo"})
224224
assert id != nil
225-
{:ok, %InsertOneResult{:inserted_id => id}} = Mongo.insert_one(top, coll, %{name: "Tom"}, opts)
225+
{:ok, %InsertOneResult{:inserted_id => id}} = Mongo.insert_one(top, coll, %{name: "Tom"})
226226
assert id != nil
227227
{:ok, :ok}
228228
end,
@@ -233,20 +233,20 @@ defmodule Mongo.SessionTest do
233233
end
234234

235235
@tag :mongo_4_2
236-
test "with_transaction_causal_consistency", %{top: top} do
236+
test "transaction_causal_consistency", %{top: top} do
237237
coll = "dogs_with_commit_transaction_causal_consistency"
238238

239239
Mongo.insert_one(top, coll, %{name: "Wuff"})
240240
Mongo.delete_many(top, coll, %{})
241241

242-
Session.with_transaction(
242+
Mongo.transaction(
243243
top,
244-
fn opts ->
245-
{:ok, %InsertOneResult{:inserted_id => id}} = Mongo.insert_one(top, coll, %{name: "Greta"}, opts)
244+
fn ->
245+
{:ok, %InsertOneResult{:inserted_id => id}} = Mongo.insert_one(top, coll, %{name: "Greta"})
246246
assert id != nil
247-
{:ok, %InsertOneResult{:inserted_id => id}} = Mongo.insert_one(top, coll, %{name: "Waldo"}, opts)
247+
{:ok, %InsertOneResult{:inserted_id => id}} = Mongo.insert_one(top, coll, %{name: "Waldo"})
248248
assert id != nil
249-
{:ok, %InsertOneResult{:inserted_id => id}} = Mongo.insert_one(top, coll, %{name: "Tom"}, opts)
249+
{:ok, %InsertOneResult{:inserted_id => id}} = Mongo.insert_one(top, coll, %{name: "Tom"})
250250
assert id != nil
251251
{:ok, :ok}
252252
end,
@@ -258,21 +258,21 @@ defmodule Mongo.SessionTest do
258258
end
259259

260260
@tag :mongo_4_2
261-
test "with_transaction_abort", %{top: top} do
261+
test "transaction_abort", %{top: top} do
262262
coll = "dogs_with_about_transaction"
263263

264264
Mongo.insert_one(top, coll, %{name: "Wuff"})
265265
Mongo.delete_many(top, coll, %{})
266266

267-
assert {:error, :error} ==
268-
Session.with_transaction(
267+
assert :error ==
268+
Mongo.transaction(
269269
top,
270-
fn opts ->
271-
{:ok, %InsertOneResult{:inserted_id => id}} = Mongo.insert_one(top, coll, %{name: "Greta"}, opts)
270+
fn ->
271+
{:ok, %InsertOneResult{:inserted_id => id}} = Mongo.insert_one(top, coll, %{name: "Greta"})
272272
assert id != nil
273-
{:ok, %InsertOneResult{:inserted_id => id}} = Mongo.insert_one(top, coll, %{name: "Waldo"}, opts)
273+
{:ok, %InsertOneResult{:inserted_id => id}} = Mongo.insert_one(top, coll, %{name: "Waldo"})
274274
assert id != nil
275-
{:ok, %InsertOneResult{:inserted_id => id}} = Mongo.insert_one(top, coll, %{name: "Tom"}, opts)
275+
{:ok, %InsertOneResult{:inserted_id => id}} = Mongo.insert_one(top, coll, %{name: "Tom"})
276276
assert id != nil
277277
:error
278278
end,
@@ -283,21 +283,21 @@ defmodule Mongo.SessionTest do
283283
end
284284

285285
@tag :mongo_4_2
286-
test "with_transaction_abort_exception", %{top: top} do
286+
test "transaction_abort_exception", %{top: top} do
287287
coll = "dogs_with_transaction_abort_exception"
288288

289289
Mongo.insert_one(top, coll, %{name: "Wuff"})
290290
Mongo.delete_many(top, coll, %{})
291291

292292
assert {:error, %ArgumentError{message: "test"}} ==
293-
Session.with_transaction(
293+
Mongo.transaction(
294294
top,
295-
fn opts ->
296-
{:ok, %InsertOneResult{:inserted_id => id}} = Mongo.insert_one(top, coll, %{name: "Greta"}, opts)
295+
fn ->
296+
{:ok, %InsertOneResult{:inserted_id => id}} = Mongo.insert_one(top, coll, %{name: "Greta"})
297297
assert id != nil
298-
{:ok, %InsertOneResult{:inserted_id => id}} = Mongo.insert_one(top, coll, %{name: "Waldo"}, opts)
298+
{:ok, %InsertOneResult{:inserted_id => id}} = Mongo.insert_one(top, coll, %{name: "Waldo"})
299299
assert id != nil
300-
{:ok, %InsertOneResult{:inserted_id => id}} = Mongo.insert_one(top, coll, %{name: "Tom"}, opts)
300+
{:ok, %InsertOneResult{:inserted_id => id}} = Mongo.insert_one(top, coll, %{name: "Tom"})
301301
assert id != nil
302302

303303
raise(ArgumentError, "test")
@@ -328,10 +328,10 @@ defmodule Mongo.SessionTest do
328328
|> UnorderedBulk.delete_one(%{kind: "dog"})
329329

330330
{:ok, result} =
331-
Session.with_transaction(
331+
Mongo.transaction(
332332
top,
333-
fn opts ->
334-
{:ok, BulkWrite.write(top, bulk, opts)}
333+
fn ->
334+
{:ok, BulkWrite.write(top, bulk)}
335335
end,
336336
w: 1
337337
)
@@ -363,10 +363,10 @@ defmodule Mongo.SessionTest do
363363
assert {:ok, _} = Mongo.admin_command(top, cmd)
364364

365365
{:error, [result | _xs]} =
366-
Session.with_transaction(
366+
Mongo.transaction(
367367
top,
368-
fn opts ->
369-
%BulkWriteResult{errors: errors} = result = BulkWrite.write(top, bulk, opts)
368+
fn ->
369+
%BulkWriteResult{errors: errors} = result = BulkWrite.write(top, bulk)
370370

371371
case Enum.empty?(errors) do
372372
true -> {:ok, result}
@@ -387,16 +387,16 @@ defmodule Mongo.SessionTest do
387387
Mongo.delete_many(top, coll, %{})
388388

389389
assert {:ok, :ok} =
390-
Session.with_transaction(
390+
Mongo.transaction(
391391
top,
392-
fn opts ->
392+
fn ->
393393
1..1000
394394
|> Stream.map(fn
395395
1 -> BulkOps.get_insert_one(%{count: 1})
396396
1000 -> BulkOps.get_delete_one(%{count: 999})
397397
i -> BulkOps.get_update_one(%{count: i - 1}, %{"$set": %{count: i}})
398398
end)
399-
|> OrderedBulk.write(top, coll, 25, opts)
399+
|> OrderedBulk.write(top, coll, 25)
400400
|> Stream.run()
401401

402402
{:ok, :ok}
@@ -407,23 +407,23 @@ defmodule Mongo.SessionTest do
407407

408408
@tag :mongo_4_2
409409
test "commit empty transaction", %{top: top} do
410-
assert {:ok, :ok} =
411-
Session.with_transaction(
410+
assert :ok =
411+
Mongo.transaction(
412412
top,
413413
fn _opts ->
414-
{:ok, :ok}
414+
:ok
415415
end,
416416
w: 1
417417
)
418418
end
419419

420420
@tag :mongo_4_2
421421
test "abort empty transaction", %{top: top} do
422-
assert {:error, :ok} =
423-
Session.with_transaction(
422+
assert :error =
423+
Mongo.transaction(
424424
top,
425425
fn _opts ->
426-
{:error, :ok}
426+
:error
427427
end,
428428
w: 1
429429
)
@@ -450,10 +450,10 @@ defmodule Mongo.SessionTest do
450450
|> OrderedBulk.delete_one(%{kind: "cat"})
451451

452452
{:ok, result} =
453-
Session.with_transaction(
453+
Mongo.transaction(
454454
top,
455-
fn opts ->
456-
result = BulkWrite.write(top, bulk, opts)
455+
fn ->
456+
result = BulkWrite.write(top, bulk)
457457
{:ok, result}
458458
end,
459459
w: 1

test/mongo/transaction_retries_test.exs

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -55,72 +55,72 @@ defmodule Mongo.TransactionRetriesTest do
5555
end
5656

5757
@tag :rs_required
58-
test "with_transaction, return an error", %{pid: top} do
58+
test "transaction, return an error", %{pid: top} do
5959
coll = unique_collection()
6060

6161
:ok = Mongo.create(top, coll)
6262

63-
assert {:error, %Mongo.Error{}} =
64-
Session.with_transaction(top, fn opts ->
65-
{:ok, _} = Mongo.insert_one(top, coll, %{name: "Greta"}, opts)
66-
{:ok, _} = Mongo.insert_one(top, coll, %{name: "Waldo"}, opts)
67-
{:ok, _} = Mongo.insert_one(top, coll, %{name: "Tom"}, opts)
63+
cmd = [
64+
configureFailPoint: "failCommand",
65+
mode: [times: 1],
66+
data: [errorCode: 3, failCommands: ["commitTransaction"]]
67+
]
6868

69-
cmd = [
70-
configureFailPoint: "failCommand",
71-
mode: [times: 1],
72-
data: [errorCode: 3, failCommands: ["commitTransaction"]]
73-
]
69+
{:ok, _doc} = Mongo.admin_command(top, cmd)
7470

75-
{:ok, _doc} = Mongo.admin_command(top, cmd)
71+
assert {:error, %Mongo.Error{}} =
72+
Mongo.transaction(top, fn ->
73+
{:ok, _} = Mongo.insert_one(top, coll, %{name: "Greta"})
74+
{:ok, _} = Mongo.insert_one(top, coll, %{name: "Waldo"})
75+
{:ok, _} = Mongo.insert_one(top, coll, %{name: "Tom"})
7676

7777
{:ok, []}
7878
end)
7979
end
8080

8181
@tag :rs_required
82-
test "with_transaction, retry commit", %{pid: top} do
82+
test "transaction, retry commit", %{pid: top} do
8383
coll = unique_collection()
8484

8585
:ok = Mongo.create(top, coll)
8686

87-
assert {:ok, []} =
88-
Session.with_transaction(top, fn opts ->
89-
{:ok, _} = Mongo.insert_one(top, coll, %{name: "Greta"}, opts)
90-
{:ok, _} = Mongo.insert_one(top, coll, %{name: "Waldo"}, opts)
91-
{:ok, _} = Mongo.insert_one(top, coll, %{name: "Tom"}, opts)
87+
cmd = [
88+
configureFailPoint: "failCommand",
89+
mode: [times: 3],
90+
data: [errorCode: 6, failCommands: ["commitTransaction"], errorLabels: ["UnknownTransactionCommitResult"]]
91+
]
9292

93-
cmd = [
94-
configureFailPoint: "failCommand",
95-
mode: [times: 3],
96-
data: [errorCode: 6, failCommands: ["commitTransaction"], errorLabels: ["UnknownTransactionCommitResult"]]
97-
]
93+
{:ok, _doc} = Mongo.admin_command(top, cmd)
9894

99-
{:ok, _doc} = Mongo.admin_command(top, cmd)
95+
assert {:ok, []} =
96+
Mongo.transaction(top, fn ->
97+
{:ok, _} = Mongo.insert_one(top, coll, %{name: "Greta"})
98+
{:ok, _} = Mongo.insert_one(top, coll, %{name: "Waldo"})
99+
{:ok, _} = Mongo.insert_one(top, coll, %{name: "Tom"})
100100

101101
{:ok, []}
102102
end)
103103
end
104104

105105
@tag :rs_required
106-
test "with_transaction, retry commit timeout", %{pid: top, catcher: catcher} do
106+
test "transaction, retry commit timeout", %{pid: top, catcher: catcher} do
107107
coll = unique_collection()
108108

109109
:ok = Mongo.create(top, coll)
110110

111-
assert {:error, %Mongo.Error{code: 6, error_labels: ["UnknownTransactionCommitResult"]}} =
112-
Session.with_transaction(
113-
top,
114-
fn opts ->
115-
{:ok, _} = Mongo.insert_one(top, coll, %{name: "Greta"}, opts)
111+
cmd = [
112+
configureFailPoint: "failCommand",
113+
mode: "alwaysOn",
114+
data: [errorCode: 6, failCommands: ["commitTransaction"], errorLabels: ["UnknownTransactionCommitResult"]]
115+
]
116116

117-
cmd = [
118-
configureFailPoint: "failCommand",
119-
mode: "alwaysOn",
120-
data: [errorCode: 6, failCommands: ["commitTransaction"], errorLabels: ["UnknownTransactionCommitResult"]]
121-
]
117+
{:ok, _doc} = Mongo.admin_command(top, cmd)
122118

123-
{:ok, _doc} = Mongo.admin_command(top, cmd)
119+
assert {:error, %Mongo.Error{code: 6, error_labels: ["UnknownTransactionCommitResult"]}} =
120+
Mongo.transaction(
121+
top,
122+
fn ->
123+
{:ok, _} = Mongo.insert_one(top, coll, %{name: "Greta"})
124124

125125
{:ok, []}
126126
end,
@@ -129,11 +129,11 @@ defmodule Mongo.TransactionRetriesTest do
129129

130130
Mongo.admin_command(top, configureFailPoint: "failCommand", mode: "off")
131131

132-
assert [:configureFailPoint, :abortTransaction, :configureFailPoint, :insert, :create] = get_succeeded_events(catcher)
132+
assert [:configureFailPoint, :abortTransaction, :insert, :configureFailPoint, :create] = get_succeeded_events(catcher)
133133
end
134134

135135
@tag :rs_required
136-
test "with_transaction, retry transaction timeout", %{pid: top, catcher: catcher} do
136+
test "transaction, retry transaction timeout", %{pid: top, catcher: catcher} do
137137
coll = unique_collection()
138138

139139
:ok = Mongo.create(top, coll)
@@ -147,10 +147,10 @@ defmodule Mongo.TransactionRetriesTest do
147147
{:ok, _doc} = Mongo.admin_command(top, cmd)
148148

149149
assert {:error, %Mongo.Error{code: 6, error_labels: ["TransientTransactionError"]}} =
150-
Session.with_transaction(
150+
Mongo.transaction(
151151
top,
152-
fn opts ->
153-
{:ok, _} = Mongo.insert_one(top, coll, %{name: "Greta"}, opts)
152+
fn ->
153+
{:ok, _} = Mongo.insert_one(top, coll, %{name: "Greta"})
154154
{:ok, []}
155155
end,
156156
transaction_retry_timeout_s: 2

0 commit comments

Comments
 (0)