@@ -3,12 +3,12 @@ defmodule Ecto.Multi do
3
3
`Ecto.Multi` is a data structure for grouping multiple Repo operations.
4
4
5
5
`Ecto.Multi` makes it possible to pack operations that should be
6
- performed in a single database transaction and gives a way to introspect
6
+ performed in a single database transaction and provides a way to introspect
7
7
the queued operations without actually performing them. Each operation
8
8
is given a name that is unique and will identify its result in case of
9
- success or failure.
9
+ either success or failure.
10
10
11
- If a multi is valid (i.e. all the changesets in it are valid),
11
+ If a Multi is valid (i.e. all the changesets in it are valid),
12
12
all operations will be executed in the order they were added.
13
13
14
14
The `Ecto.Multi` structure should be considered opaque. You can use
@@ -19,41 +19,41 @@ defmodule Ecto.Multi do
19
19
structure that can be used for introspection.
20
20
21
21
> #### When to use Ecto.Multi? {: .info}
22
-
22
+ >
23
23
> `Ecto.Multi` is particularly useful when the set of operations to perform
24
- > is dynamic. For most use cases, however , using regular control flow within
24
+ > is dynamic. For most other use cases, using regular control flow within
25
25
> [`Repo.transact(fun)`](`c:Ecto.Repo.transact/2`) and returning
26
26
> `{:ok, result}` or `{:error, reason}` is more straightforward.
27
27
28
28
## Changesets
29
29
30
- If multi contains operations that accept changesets (like `insert/4`,
31
- `update/4` or `delete/4`) they will be checked before starting the
32
- transaction. If any changeset has errors, the transaction won't even
33
- be started and the error will be immediately returned.
30
+ If a Multi contains operations that accept changesets (like `insert/4`,
31
+ `update/4` or `delete/4`), they will be checked before starting the
32
+ transaction. If any changeset has errors, the transaction will not be
33
+ started and the error will immediately be returned.
34
34
35
- Note: `insert/4`, `update/4`, `insert_or_update/4`, and `delete/4`
35
+ Note: `insert/4`, `update/4`, `insert_or_update/4` and `delete/4`
36
36
variants that accept a function do not perform these checks since
37
37
the functions are executed after the transaction has started.
38
38
39
39
## Run
40
40
41
- Multi allows you to run arbitrary functions as part of your transaction
41
+ ` Multi` allows you to run arbitrary functions as part of your transaction
42
42
via `run/3` and `run/5`. This is especially useful when an operation
43
43
depends on the value of a previous operation. For this reason, the
44
44
function given as a callback to `run/3` and `run/5` will receive the repo
45
- as the first argument, and all changes performed by the multi so far as a
46
- map for the second argument.
45
+ as the first argument, and all changes performed by the Multi so far as a
46
+ map as the second argument.
47
47
48
48
The function given to `run` must return `{:ok, value}` or `{:error, value}`
49
49
as its result. Returning an error will abort any further operations
50
- and make the whole multi fail.
50
+ and make the Multi fail.
51
51
52
52
## Example
53
53
54
- Let's look at an example definition and usage. The use case we'll be
55
- looking into is resetting a password. We need to update the account
56
- with proper information, log the request and remove all current sessions:
54
+ Let's look at an example definition and usage: resetting a password. We need
55
+ to update the account with proper information, log the request and remove
56
+ all current sessions:
57
57
58
58
defmodule PasswordManager do
59
59
alias Ecto.Multi
@@ -70,24 +70,24 @@ defmodule Ecto.Multi do
70
70
71
71
Repo.transact(PasswordManager.reset(account, params))
72
72
73
- By pattern matching on the result we can differentiate different conditions:
73
+ By pattern matching on the result we can differentiate various conditions:
74
74
75
75
case result do
76
76
{:ok, %{account: account, log: log, sessions: sessions}} ->
77
- # Operation was successful, we can access results (exactly the same
78
- # we would get from running corresponding Repo functions) under keys
79
- # we used for naming the operations.
77
+ # The Multi was successful. We can access results , which are as
78
+ # we would get from running the corresponding Repo functions, under
79
+ # keys we used for naming the operations.
80
80
{:error, failed_operation, failed_value, changes_so_far} ->
81
81
# One of the operations failed. We can access the operation's failure
82
- # value (like changeset for operations on changesets) to prepare a
82
+ # value (such as a changeset for operations on changesets) to prepare a
83
83
# proper response. We also get access to the results of any operations
84
- # that succeeded before the indicated operation failed. However, any
85
- # successful operations would have been rolled back.
84
+ # that succeeded before the indicated operation failed. ( However,
85
+ # successful operations were rolled back.)
86
86
end
87
87
88
88
We can also easily unit test our transaction without actually running it.
89
- Since changesets can use in-memory- data, we can use an account that is
90
- constructed in memory as well ( without persisting it to the database) :
89
+ Since changesets can use in-memory data, we can use an account that is
90
+ constructed in memory as well, without persisting it to the database:
91
91
92
92
test "dry run password reset" do
93
93
account = %Account{password: "letmein"}
@@ -180,9 +180,9 @@ defmodule Ecto.Multi do
180
180
end
181
181
182
182
@ doc """
183
- Appends the second multi to the first one .
183
+ Appends the second Multi to the first.
184
184
185
- All names must be unique between both structures.
185
+ All names must be unique within both structures.
186
186
187
187
## Example
188
188
@@ -198,9 +198,9 @@ defmodule Ecto.Multi do
198
198
end
199
199
200
200
@ doc """
201
- Prepends the second multi to the first one .
201
+ Prepends the second Multi to the first.
202
202
203
- All names must be unique between both structures.
203
+ All names must be unique within both structures.
204
204
205
205
## Example
206
206
@@ -237,14 +237,14 @@ defmodule Ecto.Multi do
237
237
end
238
238
239
239
@ doc """
240
- Merges a multi returned dynamically by an anonymous function.
240
+ Merges a Multi returned dynamically by an anonymous function.
241
241
242
- This function is useful when the multi to be merged requires information
243
- from the original multi. Hence the second argument is an anonymous function
244
- that receives the multi changes so far. The anonymous function must return
245
- another multi .
242
+ This function is useful when the Multi to be merged requires information
243
+ from the original Multi. The second argument is an anonymous function
244
+ that receives the Multi changes so far. The anonymous function must return
245
+ another Multi .
246
246
247
- If you would prefer to simply merge two multis together, see `append/2` or
247
+ If you would prefer to simply merge two Multis together, see `append/2` or
248
248
`prepend/2`.
249
249
250
250
Duplicated operations are not allowed.
@@ -268,10 +268,10 @@ defmodule Ecto.Multi do
268
268
end
269
269
270
270
@ doc """
271
- Merges a multi returned dynamically by calling `module` and `function` with `args`.
271
+ Merges a Multi returned dynamically by calling `module` and `function` with `args`.
272
272
273
- Similar to `merge/2`, but allows to pass module name, function and arguments.
274
- The function should return an `Ecto.Multi`, and receives changes so far
273
+ Similar to `merge/2` but allows passing of module name, function and
274
+ arguments. The function should return an `Ecto.Multi`, and receives changes so far
275
275
as the first argument (prepended to those passed in the call to the function).
276
276
277
277
Duplicated operations are not allowed.
@@ -283,9 +283,9 @@ defmodule Ecto.Multi do
283
283
end
284
284
285
285
@ doc """
286
- Adds an insert operation to the multi .
286
+ Adds an insert operation to the Multi .
287
287
288
- The `name` must be unique from other statements in the multi .
288
+ The `name` must be unique within the Multi .
289
289
290
290
The remaining arguments and options are the same as in `c:Ecto.Repo.insert/2`.
291
291
@@ -324,9 +324,9 @@ defmodule Ecto.Multi do
324
324
end
325
325
326
326
@ doc """
327
- Adds an update operation to the multi .
327
+ Adds an update operation to the Multi .
328
328
329
- The `name` must be unique from other statements in the multi .
329
+ The `name` must be unique within the Multi .
330
330
331
331
The remaining arguments and options are the same as in `c:Ecto.Repo.update/2`.
332
332
@@ -358,9 +358,9 @@ defmodule Ecto.Multi do
358
358
end
359
359
360
360
@ doc """
361
- Inserts or updates a changeset depending on whether the changeset was persisted or not .
361
+ Inserts or updates a changeset depending on whether or not the changeset was persisted.
362
362
363
- The `name` must be unique from other statements in the multi .
363
+ The `name` must be unique within the Multi .
364
364
365
365
The remaining arguments and options are the same as in `c:Ecto.Repo.insert_or_update/2`.
366
366
@@ -402,9 +402,9 @@ defmodule Ecto.Multi do
402
402
end
403
403
404
404
@ doc """
405
- Adds a delete operation to the multi .
405
+ Adds a delete operation to the Multi .
406
406
407
- The `name` must be unique from other statements in the multi .
407
+ The `name` must be unique within the Multi .
408
408
409
409
The remaining arguments and options are the same as in `c:Ecto.Repo.delete/2`.
410
410
@@ -450,9 +450,9 @@ defmodule Ecto.Multi do
450
450
end
451
451
452
452
@ doc """
453
- Runs a query expecting one result and stores it in the multi .
453
+ Runs a query expecting one result and stores the result in the Multi .
454
454
455
- The `name` must be unique from other statements in the multi .
455
+ The `name` must be unique within the Multi .
456
456
457
457
The remaining arguments and options are the same as in `c:Ecto.Repo.one/2`.
458
458
@@ -482,11 +482,11 @@ defmodule Ecto.Multi do
482
482
end
483
483
484
484
@ doc """
485
- Runs a query and stores all entries in the multi .
485
+ Runs a query and stores all results in the Multi .
486
486
487
- The `name` must be unique from other statements in the multi .
487
+ The `name` must be unique within the Multi .
488
488
489
- The remaining arguments and options are the same as in `c:Ecto.Repo.all/2` does .
489
+ The remaining arguments and options are the same as in `c:Ecto.Repo.all/2`.
490
490
491
491
## Example
492
492
@@ -515,9 +515,9 @@ defmodule Ecto.Multi do
515
515
end
516
516
517
517
@ doc """
518
- Checks if there exists an entry matching the given query and stores a boolean in the multi .
518
+ Checks if an entry matching the given query exists and stores a boolean in the Multi .
519
519
520
- The `name` must be unique from other statements in the multi .
520
+ The `name` must be unique within the Multi .
521
521
522
522
The remaining arguments and options are the same as in `c:Ecto.Repo.exists?/2`.
523
523
@@ -566,10 +566,10 @@ defmodule Ecto.Multi do
566
566
end
567
567
568
568
@ doc """
569
- Causes the multi to fail with the given value.
569
+ Causes the Multi to fail with the given value.
570
570
571
- Running the multi in a transaction will execute
572
- no previous steps and returns the value of the first
571
+ Running the Multi in a transaction will execute
572
+ no previous steps and return the value of the first
573
573
error added.
574
574
"""
575
575
@ spec error ( t , name , error :: term ) :: t
@@ -578,10 +578,10 @@ defmodule Ecto.Multi do
578
578
end
579
579
580
580
@ doc """
581
- Adds a function to run as part of the multi .
581
+ Adds a function to run as part of the Multi .
582
582
583
583
The function should return either `{:ok, value}` or `{:error, value}`,
584
- and receives the repo as the first argument, and the changes so far
584
+ and receives the repo as the first argument and the changes so far
585
585
as the second argument.
586
586
587
587
## Example
@@ -598,11 +598,11 @@ defmodule Ecto.Multi do
598
598
end
599
599
600
600
@ doc """
601
- Adds a function to run as part of the multi .
601
+ Adds a function to run as part of the Multi .
602
602
603
- Similar to `run/3`, but allows to pass module name, function and arguments.
603
+ Similar to `run/3`, but allows passing of module name, function and arguments.
604
604
The function should return either `{:ok, value}` or `{:error, value}`, and
605
- receives the repo as the first argument, and the changes so far as the
605
+ receives the repo as the first argument and the changes so far as the
606
606
second argument (prepended to those passed in the call to the function).
607
607
"""
608
608
@ spec run ( t , name , module , function , args ) :: t when function: atom , args: [ any ]
@@ -612,9 +612,9 @@ defmodule Ecto.Multi do
612
612
end
613
613
614
614
@ doc """
615
- Adds an insert_all operation to the multi .
615
+ Adds an ` insert_all` operation to the Multi .
616
616
617
- Accepts the same arguments and options as `c:Ecto.Repo.insert_all/3` does .
617
+ Accepts the same arguments and options as `c:Ecto.Repo.insert_all/3`.
618
618
619
619
## Example
620
620
@@ -661,9 +661,9 @@ defmodule Ecto.Multi do
661
661
end
662
662
663
663
@ doc """
664
- Adds an update_all operation to the multi .
664
+ Adds an ` update_all` operation to the Multi .
665
665
666
- Accepts the same arguments and options as `c:Ecto.Repo.update_all/3` does .
666
+ Accepts the same arguments and options as `c:Ecto.Repo.update_all/3`.
667
667
668
668
## Example
669
669
@@ -705,9 +705,9 @@ defmodule Ecto.Multi do
705
705
end
706
706
707
707
@ doc """
708
- Adds a delete_all operation to the multi .
708
+ Adds a ` delete_all` operation to the Multi .
709
709
710
- Accepts the same arguments and options as `c:Ecto.Repo.delete_all/2` does .
710
+ Accepts the same arguments and options as `c:Ecto.Repo.delete_all/2`.
711
711
712
712
## Example
713
713
@@ -754,7 +754,7 @@ defmodule Ecto.Multi do
754
754
end
755
755
756
756
@ doc """
757
- Returns the list of operations stored in `multi` .
757
+ Returns the list of operations stored in the Multi .
758
758
759
759
Always use this function when you need to access the operations you
760
760
have defined in `Ecto.Multi`. Inspecting the `Ecto.Multi` struct internals
@@ -776,26 +776,26 @@ defmodule Ecto.Multi do
776
776
@ doc """
777
777
Adds a value to the changes so far under the given name.
778
778
779
- The given `value` is added to the multi before the transaction starts.
779
+ The given `value` is added to the Multi before the transaction starts.
780
780
If you would like to run arbitrary functions as part of your transaction,
781
781
see `run/3` or `run/5`.
782
782
783
783
## Example
784
784
785
785
Imagine there is an existing company schema that you retrieved from
786
- the database. You can insert it as a change in the multi using `put/3`:
786
+ the database. You can insert it as a change in the Multi using `put/3`:
787
787
788
788
Ecto.Multi.new()
789
789
|> Ecto.Multi.put(:company, company)
790
790
|> Ecto.Multi.insert(:user, fn changes -> User.changeset(changes.company) end)
791
791
|> Ecto.Multi.insert(:person, fn changes -> Person.changeset(changes.user, changes.company) end)
792
792
|> MyApp.Repo.transact()
793
793
794
- In the example above there isn't a large benefit in putting the
795
- `company` in the multi, because you could also access the
794
+ In the example above, there isn't a significant benefit in putting
795
+ the `company` in the Multi because you could also access the
796
796
`company` variable directly inside the anonymous function.
797
797
798
- However, the benefit of `put/3` is when composing `Ecto.Multi`s.
798
+ However, the benefit of `put/3` is seen when composing `Ecto.Multi`s.
799
799
If the insert operations above were defined in another module,
800
800
you could use `put(:company, company)` to inject changes that
801
801
will be accessed by other functions down the chain, removing
@@ -807,14 +807,14 @@ defmodule Ecto.Multi do
807
807
end
808
808
809
809
@ doc """
810
- Inspects results from a Multi
810
+ Inspects results from a Multi.
811
811
812
- By default, the name is shown as a label to the inspect, custom labels are
812
+ By default, the name is shown as a label to the inspect. Custom labels are
813
813
supported through the `IO.inspect/2` `label` option.
814
814
815
815
## Options
816
816
817
- All options for IO.inspect/2 are supported, it also support the following ones :
817
+ All options for IO.inspect/2 are supported, as well as :
818
818
819
819
* `:only` - A field or a list of fields to inspect, will print the entire
820
820
map by default.
@@ -949,7 +949,7 @@ defmodule Ecto.Multi do
949
949
{ Map . merge ( changes , new_changes ) , MapSet . union ( names , new_names ) }
950
950
951
951
common ->
952
- raise "cannot merge multi, the following operations were found in " <>
952
+ raise "cannot merge Multi; the following operations were found in " <>
953
953
"both Ecto.Multi: #{ Kernel . inspect ( common ) } "
954
954
end
955
955
end
0 commit comments