Skip to content

Commit

Permalink
add all read methods to LiveTransaction and have them accept tx in Repo
Browse files Browse the repository at this point in the history
  • Loading branch information
Joakim Reinert authored and repomaa committed Oct 24, 2019
1 parent 9d56ed7 commit b1512e8
Show file tree
Hide file tree
Showing 3 changed files with 211 additions and 74 deletions.
48 changes: 48 additions & 0 deletions spec/transactions_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,54 @@ describe Crecto do
Repo.all(User, Query.where(name: "perform_all")).size.should eq 2
Repo.all(User, Query.where(name: "perform_all_io2oj999")).size.should eq 0
end

it "allows reading records inserted inside the transaction" do
insert_user = User.new
insert_user.name = "insert_user"

Repo.transaction! do |tx|
id = tx.insert!(insert_user).instance.id
tx.get(User, id).should_not eq(nil)
tx.get!(User, id).should_not eq(nil)
tx.get(User, id, Query.new).should_not eq(nil)
tx.get!(User, id, Query.new).should_not eq(nil)
tx.get_by(User, id: id).should_not eq(nil)
tx.get_by!(User, id: id).should_not eq(nil)
tx.get_by(User, id: id).should_not eq(nil)
tx.get_by!(User, id: id).should_not eq(nil)
tx.get_by(User, Query.where(id: id)).should_not eq(nil)
tx.get_by!(User, Query.where(id: id)).should_not eq(nil)
tx.all(User, Query.where(id: id)).first.should_not eq(nil)
tx.all(User, Query.where(id: id), preloads: [] of Symbol).first.should_not eq(nil)
end
end

it "allows nesting transactions" do
Repo.delete_all(Post)
Repo.delete_all(User)

insert_user = User.new
insert_user.name = "nested_transactions_insert_user"
invalid_user = User.new
delete_user = quick_create_user("nested_transactions_delete_user")

Repo.transaction! do |tx|
tx.insert!(insert_user)

expect_raises Crecto::InvalidChangeset do
Repo.transaction! do |inner_tx|
inner_tx.delete!(delete_user)
inner_tx.insert!(invalid_user)
end
end
end

# check insert happened
Repo.all(User, Query.where(name: "nested_transactions_insert_user")).size.should eq 1

# check delete didn't happen
Repo.all(User, Query.where(name: "nested_transactions_delete_user")).size.should eq 1
end
end
end
end
92 changes: 91 additions & 1 deletion src/crecto/live_transaction.cr
Original file line number Diff line number Diff line change
@@ -1,10 +1,86 @@
require "./multi"
require "./repo/query"

module Crecto
class LiveTransaction(T)
alias Query = Repo::Query

def initialize(@tx : DB::Transaction, @repo : T)
end

def raw_exec(args : Array)
@repo.raw_exec(args, @tx)
end

def raw_exec(*args)
@repo.raw_exec(*args, @tx)
end

def raw_query(query, *args)
@repo.raw_query(query, *args, @tx) do |rs|
yield rs
end
end

def raw_query(query, args : Array)
@repo.raw_query(query, args, @tx)
end

def raw_query(query, *args)
@repo.raw_query(query, *args)
end

def raw_scalar(*args)
@repo.raw_scalar(*args, @tx)
end

def all(queryable, query : Query? = Query.new, **opts)
@repo.all(queryable, query, @tx, **opts)
end

def all(queryable, query = Query.new)
@repo.all(queryable, query, @tx)
end

def get(queryable, id)
@repo.get(queryable, id, @tx)
end

def get!(queryable, id)
@repo.get!(queryable, id, @tx)
end

def get(queryable, id, query : Query)
@repo.get(queryable, id, query, @tx)
end

def get!(queryable, id, query : Query)
@repo.get!(queryable, id, query, @tx)
end

def get_by(queryable, **opts)
@repo.get_by(queryable, @tx, **opts)
end

def get_by(queryable, query)
@repo.get_by(queryable, query, @tx)
end

def get_by!(queryable, **opts)
@repo.get_by!(queryable, @tx, **opts)
end

def get_by!(queryable, query)
@repo.get_by!(queryable, query, @tx)
end

def get_association(queryable_instance, association_name : Symbol)
@repo.get_association(queryable_instance, association_name, @tx)
end

def get_association!(queryable_instance, association_name : Symbol)
@repo.get_association!(queryable_instance, association_name, @tx)
end

{% for type in %w[insert insert! delete delete! update update!] %}
def {{type.id}}(queryable : Crecto::Model)
@repo.{{type.id}}(queryable, @tx)
Expand All @@ -26,5 +102,19 @@ module Crecto
def update_all(queryable, query, update_tuple : NamedTuple)
update_all(queryable, query, update_tuple.to_h)
end

def aggregate(queryable, aggregate_function : Symbol, field : Symbol)
@repo.aggregate(queryable, aggregate_function, field, @tx)
end

def aggregate(queryable, aggregate_function : Symbol, field : Symbol, query : Crecto::Repo::Query)
@repo.aggregate(queryable, aggregate_function, field, query, @tx)
end

def transaction!
@repo.transaction!(@tx) do |tx|
yield tx
end
end
end
end
Loading

0 comments on commit b1512e8

Please sign in to comment.