Skip to content

Commit 7480f09

Browse files
committed
allow setting Onyx::SQL::Repository#db with DB::Transaction instances
1 parent ec866c9 commit 7480f09

File tree

3 files changed

+122
-0
lines changed

3 files changed

+122
-0
lines changed

db_spec/pg/repository/db_spec.cr

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
require "../pg_spec"
2+
require "../../repository_spec"
3+
4+
describe "Repository(Postgres)#db=" do
5+
repo = repo(:postgresql)
6+
7+
context "with DB::Database" do
8+
it "sets the db to the given database" do
9+
old_db = repo.db
10+
11+
DB.open(ENV["POSTGRESQL_URL"]) do |db|
12+
repo.db = db
13+
new_db = repo.db
14+
15+
new_db.should_not be old_db
16+
new_db.should be_a(::DB::Database)
17+
new_db.should be db
18+
end
19+
20+
repo.db = old_db
21+
end
22+
end
23+
24+
context "with DB::Connection" do
25+
it "sets the db to the given connection" do
26+
old_db = repo.db
27+
28+
DB.connect(ENV["POSTGRESQL_URL"]) do |connection|
29+
repo.db = connection
30+
new_db = repo.db
31+
32+
new_db.should_not be old_db
33+
new_db.should be_a(::DB::Connection)
34+
new_db.should be connection
35+
end
36+
37+
repo.db = old_db
38+
end
39+
end
40+
41+
context "with DB::Transaction" do
42+
it "sets the db to the given transaction's connection" do
43+
old_db = repo.db
44+
45+
DB.open(ENV["POSTGRESQL_URL"]) do |db|
46+
db.transaction do |tx|
47+
repo.db = tx
48+
new_db = repo.db
49+
50+
new_db.should_not be old_db
51+
new_db.should be_a(::DB::Connection)
52+
new_db.should be tx.connection
53+
end
54+
end
55+
56+
repo.db = old_db
57+
end
58+
end
59+
end
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
require "../sqlite3_spec"
2+
require "../../repository_spec"
3+
4+
describe "Repository(Sqlite3)#db=" do
5+
repo = repo(:sqlite3)
6+
7+
context "with DB::Database" do
8+
it "sets the db to the given database" do
9+
old_db = repo.db
10+
11+
DB.open(ENV["SQLITE3_URL"]) do |db|
12+
repo.db = db
13+
new_db = repo.db
14+
15+
new_db.should_not be old_db
16+
new_db.should be_a(::DB::Database)
17+
new_db.should be db
18+
end
19+
20+
repo.db = old_db
21+
end
22+
end
23+
24+
context "with DB::Connection" do
25+
it "sets the db to the given connection" do
26+
old_db = repo.db
27+
28+
DB.connect(ENV["SQLITE3_URL"]) do |connection|
29+
repo.db = connection
30+
new_db = repo.db
31+
32+
new_db.should_not be old_db
33+
new_db.should be_a(::DB::Connection)
34+
new_db.should be connection
35+
end
36+
37+
repo.db = old_db
38+
end
39+
end
40+
41+
context "with DB::Transaction" do
42+
it "sets the db to the given transaction's connection" do
43+
old_db = repo.db
44+
45+
DB.open(ENV["SQLITE3_URL"]) do |db|
46+
db.transaction do |tx|
47+
repo.db = tx
48+
new_db = repo.db
49+
50+
new_db.should_not be old_db
51+
new_db.should be_a(::DB::Connection)
52+
new_db.should be tx.connection
53+
end
54+
end
55+
56+
repo.db = old_db
57+
end
58+
end
59+
end

src/onyx-sql/repository.cr

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ module Onyx::SQL
3232
def initialize(@db : ::DB::Database | ::DB::Connection, @logger : Logger = Logger::Standard.new)
3333
end
3434

35+
def db=(transaction : ::DB::Transaction)
36+
self.db = transaction.connection
37+
end
38+
3539
protected def postgresql?
3640
{% if Object.all_subclasses.any? { |sc| sc.stringify == "PG::Driver" } %}
3741
return db.is_a?(PG::Driver)

0 commit comments

Comments
 (0)