Skip to content

Commit c82b3c0

Browse files
author
Joakim Reinert
committed
add all read methods to LiveTransaction and have them accept tx in Repo
1 parent 58f4fc0 commit c82b3c0

File tree

3 files changed

+211
-73
lines changed

3 files changed

+211
-73
lines changed

spec/transactions_spec.cr

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,54 @@ describe Crecto do
333333
Repo.all(User, Query.where(name: "perform_all")).size.should eq 2
334334
Repo.all(User, Query.where(name: "perform_all_io2oj999")).size.should eq 0
335335
end
336+
337+
it "allows reading records inserted inside the transaction" do
338+
insert_user = User.new
339+
insert_user.name = "insert_user"
340+
341+
Repo.transaction! do |tx|
342+
id = tx.insert!(insert_user).instance.id
343+
tx.get(User, id).should_not eq(nil)
344+
tx.get!(User, id).should_not eq(nil)
345+
tx.get(User, id, Query.new).should_not eq(nil)
346+
tx.get!(User, id, Query.new).should_not eq(nil)
347+
tx.get_by(User, id: id).should_not eq(nil)
348+
tx.get_by!(User, id: id).should_not eq(nil)
349+
tx.get_by(User, id: id).should_not eq(nil)
350+
tx.get_by!(User, id: id).should_not eq(nil)
351+
tx.get_by(User, Query.where(id: id)).should_not eq(nil)
352+
tx.get_by!(User, Query.where(id: id)).should_not eq(nil)
353+
tx.all(User, Query.where(id: id)).first.should_not eq(nil)
354+
tx.all(User, Query.where(id: id), preloads: [] of Symbol).first.should_not eq(nil)
355+
end
356+
end
357+
358+
it "allows nesting transactions" do
359+
Repo.delete_all(Post)
360+
Repo.delete_all(User)
361+
362+
insert_user = User.new
363+
insert_user.name = "nested_transactions_insert_user"
364+
invalid_user = User.new
365+
delete_user = quick_create_user("nested_transactions_delete_user")
366+
367+
Repo.transaction! do |tx|
368+
tx.insert!(insert_user)
369+
370+
expect_raises Crecto::InvalidChangeset do
371+
Repo.transaction! do |inner_tx|
372+
inner_tx.delete!(delete_user)
373+
inner_tx.insert!(invalid_user)
374+
end
375+
end
376+
end
377+
378+
# check insert happened
379+
Repo.all(User, Query.where(name: "nested_transactions_insert_user")).size.should eq 1
380+
381+
# check delete didn't happen
382+
Repo.all(User, Query.where(name: "nested_transactions_delete_user")).size.should eq 1
383+
end
336384
end
337385
end
338386
end

src/crecto/live_transaction.cr

Lines changed: 91 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,86 @@
1-
require "./multi"
1+
require "./repo/query"
22

33
module Crecto
44
class LiveTransaction(T)
5+
alias Query = Repo::Query
6+
57
def initialize(@tx : DB::Transaction, @repo : T)
68
end
79

10+
def raw_exec(args : Array)
11+
@repo.raw_exec(args, @tx)
12+
end
13+
14+
def raw_exec(*args)
15+
@repo.raw_exec(*args, @tx)
16+
end
17+
18+
def raw_query(query, *args)
19+
@repo.raw_query(query, *args, @tx) do |rs|
20+
yield rs
21+
end
22+
end
23+
24+
def raw_query(query, args : Array)
25+
@repo.raw_query(query, args, @tx)
26+
end
27+
28+
def raw_query(query, *args)
29+
@repo.raw_query(query, *args)
30+
end
31+
32+
def raw_scalar(*args)
33+
@repo.raw_scalar(*args, @tx)
34+
end
35+
36+
def all(queryable, query : Query? = Query.new, **opts)
37+
@repo.all(queryable, query, @tx, **opts)
38+
end
39+
40+
def all(queryable, query = Query.new)
41+
@repo.all(queryable, query, @tx)
42+
end
43+
44+
def get(queryable, id)
45+
@repo.get(queryable, id, @tx)
46+
end
47+
48+
def get!(queryable, id)
49+
@repo.get!(queryable, id, @tx)
50+
end
51+
52+
def get(queryable, id, query : Query)
53+
@repo.get(queryable, id, query, @tx)
54+
end
55+
56+
def get!(queryable, id, query : Query)
57+
@repo.get!(queryable, id, query, @tx)
58+
end
59+
60+
def get_by(queryable, **opts)
61+
@repo.get_by(queryable, @tx, **opts)
62+
end
63+
64+
def get_by(queryable, query)
65+
@repo.get_by(queryable, query, @tx)
66+
end
67+
68+
def get_by!(queryable, **opts)
69+
@repo.get_by!(queryable, @tx, **opts)
70+
end
71+
72+
def get_by!(queryable, query)
73+
@repo.get_by!(queryable, query, @tx)
74+
end
75+
76+
def get_association(queryable_instance, association_name : Symbol)
77+
@repo.get_association(queryable_instance, association_name, @tx)
78+
end
79+
80+
def get_association!(queryable_instance, association_name : Symbol)
81+
@repo.get_association!(queryable_instance, association_name, @tx)
82+
end
83+
884
{% for type in %w[insert insert! delete delete! update update!] %}
985
def {{type.id}}(queryable : Crecto::Model)
1086
@repo.{{type.id}}(queryable, @tx)
@@ -26,5 +102,19 @@ module Crecto
26102
def update_all(queryable, query, update_tuple : NamedTuple)
27103
update_all(queryable, query, update_tuple.to_h)
28104
end
105+
106+
def aggregate(queryable, aggregate_function : Symbol, field : Symbol)
107+
@repo.aggregate(queryable, aggregate_function, field, @tx)
108+
end
109+
110+
def aggregate(queryable, aggregate_function : Symbol, field : Symbol, query : Crecto::Repo::Query)
111+
@repo.aggregate(queryable, aggregate_function, field, query, @tx)
112+
end
113+
114+
def transaction!
115+
@repo.transaction!(@tx) do |tx|
116+
yield tx
117+
end
118+
end
29119
end
30120
end

0 commit comments

Comments
 (0)