-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathsql_with_connection.rb
More file actions
37 lines (31 loc) · 1.08 KB
/
Copy pathsql_with_connection.rb
File metadata and controls
37 lines (31 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
require File.expand_path("../example_setup", __FILE__)
require "github/sql"
class SomeModel < ActiveRecord::Base
self.abstract_class = true
establish_connection({
adapter: "mysql2",
database: "github_ds_test",
username: "root",
})
end
ActiveRecord::Base.transaction do
# Insert bar on base connection.
GitHub::SQL.run <<-SQL, key: "bar", value: "baz", connection: ActiveRecord::Base.connection
INSERT INTO example_key_values (`key`, `value`) VALUES (:key, :value)
SQL
SomeModel.transaction do
# Insert foo on different connection.
GitHub::SQL.run <<-SQL, key: "foo", value: "bar", connection: SomeModel.connection
INSERT INTO example_key_values (`key`, `value`) VALUES (:key, :value)
SQL
end
# Roll back "bar" insertion.
raise ActiveRecord::Rollback
end
# Show that "bar" key is not here because that connection's transaction was
# rolled back. SomeModel is a different connection and started a different
# transaction, which succeeded, so "foo" key was created.
p GitHub::SQL.values <<-SQL
SELECT `key` FROM example_key_values
SQL
# ["foo"]