Skip to content

Commit

Permalink
Trying to get the update to work on a secondary connection.
Browse files Browse the repository at this point in the history
I need a second pair of eyes on this.
  • Loading branch information
russ authored and anykeyh committed Jun 24, 2018
1 parent af91ef8 commit 75408e5
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 9 deletions.
81 changes: 81 additions & 0 deletions spec/model/multiple_connections.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
require "../spec_helper"

module MultipleConnectionsSpec
class Post
include Clear::Model

self.table = "models_posts"

column id : Int32, primary: true, presence: false
column title : String
end

class PostStat
include Clear::Model

self.connection = "secondary"
self.table = "models_post_stats"

column id : Int32, primary: true, presence: false
column post_id : Int32
end

class ModelSpecMigration1234
include Clear::Migration

def change(dir)
create_table "models_posts" do |t|
t.string "title", index: true
end
end
end

def self.reinit
reinit_migration_manager
ModelSpecMigration1234.new.apply(Clear::Migration::Direction::UP)
init_secondary_db
end

describe "Clear::Model" do
context "multiple connections" do
it "know about the different connections on models" do
Post.connection.should eq "default"
PostStat.connection.should eq "secondary"
end

it "can load data from the default database" do
temporary do
reinit

p = Post.new({title: "some post"})
p.save
p.persisted?.should be_true
end
end

it "can insert data into the secondary database" do
temporary do
reinit
p = PostStat.new({post_id: 1})
p.save
p.persisted?.should be_true
p.post_id.should eq(1)
end
end

it "can update data on the secondary database" do
temporary do
reinit
p = PostStat.new({post_id: 1})
p.save

p.post_id = 2
p.save

p = PostStat.query.first.not_nil!
p.post_id.should eq(2)
end
end
end
end
end
2 changes: 1 addition & 1 deletion spec/spec_helper.cr
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def init_secondary_db
system("echo \"DROP DATABASE IF EXISTS clear_secondary_spec;\" | psql -U postgres")
system("echo \"CREATE DATABASE clear_secondary_spec;\" | psql -U postgres")
system("echo \"CREATE TABLE models_post_stats (id serial PRIMARY KEY, post_id INTEGER);\" | psql -U postgres clear_secondary_spec")
system("echo \"INSERT INTO models_post_stats VALUES (1, 1);\" | psql -U postgres clear_secondary_spec")
# system("echo \"INSERT INTO models_post_stats VALUES (1, 1);\" | psql -U postgres clear_secondary_spec")

Clear::SQL.init({
"deafult" => "postgres://postgres@localhost/clear_spec",
Expand Down
2 changes: 1 addition & 1 deletion src/clear/model/model.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require "./converter/**"
require "./validation/**"

module Clear::Model
include Clear::Model::Connection
include Clear::Model::HasColumns
include Clear::Model::HasHooks
include Clear::Model::HasTimestamps
Expand All @@ -14,7 +15,6 @@ module Clear::Model
include Clear::Model::HasValidation
include Clear::Model::HasRelations
include Clear::Model::HasScope
include Clear::Model::Connection
include Clear::Model::ClassMethods
include Clear::Model::IsPolymorphic

Expand Down
2 changes: 1 addition & 1 deletion src/clear/model/modules/has_saving.cr
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ module Clear::Model::HasSaving
else
with_triggers(:create) do
@persisted = true
hash = Clear::SQL.insert_into(self.class.table, to_h).returning("*").execute
hash = Clear::SQL.insert_into(@@connection, self.class.table, to_h).returning("*").execute
self.set(hash)
end
end
Expand Down
11 changes: 10 additions & 1 deletion src/clear/sql/insert_query.cr
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,17 @@ class Clear::SQL::InsertQuery
getter returning : String?

def initialize(@table : Selectable)
@connection = "default"
end

def initialize(@connection : Symbolic, @table : Selectable)
end

def fetch(&block : Hash(String, ::Clear::SQL::Any) -> Void)
Clear::SQL.log_query to_sql do
h = {} of String => ::Clear::SQL::Any

Clear::SQL.connection(self.connection_name).query(to_sql) do |rs|
Clear::SQL.connection(@connection).query(to_sql) do |rs|
fetch_result_set(h, rs) { |x| yield(x) }
end
end
Expand Down Expand Up @@ -61,6 +65,11 @@ class Clear::SQL::InsertQuery
o = {} of String => ::Clear::SQL::Any

if @returning.nil?
puts "="* 50
puts self.connection_name
puts to_sql
puts "="* 50

Clear::SQL.execute(self.connection_name, to_sql)
else
# return {} of String => ::Clear::SQL::Any
Expand Down
6 changes: 3 additions & 3 deletions src/clear/sql/query/fetch.cr
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ module Clear::SQL::Query::Fetch
trigger_before_query

Clear::SQL.transaction do
cnx = Clear::SQL.connection(self.connection_name)
cnx = Clear::SQL.connection(connection_name)
cursor_name = "__cursor_#{Time.now.epoch ^ (rand * 0xfffffff).to_i}__"

cursor_declaration = "DECLARE #{cursor_name} CURSOR FOR #{to_sql}"
Expand Down Expand Up @@ -55,7 +55,7 @@ module Clear::SQL::Query::Fetch
trigger_before_query

Clear::SQL.log_query to_sql do
Clear::SQL.connection(self.connection_name).scalar(to_sql).as(T)
Clear::SQL.connection(connection_name).scalar(to_sql).as(T)
end
end

Expand All @@ -71,7 +71,7 @@ module Clear::SQL::Query::Fetch
to_sql = self.to_sql

rs = uninitialized PG::ResultSet
Clear::SQL.log_query(to_sql) { rs = Clear::SQL.connection(self.connection_name).query(to_sql) }
Clear::SQL.log_query(to_sql) { rs = Clear::SQL.connection(connection_name).query(to_sql) }

o = [] of Hash(String, ::Clear::SQL::Any)
fetch_result_set(h, rs) { |x| o << x.dup }
Expand Down
16 changes: 14 additions & 2 deletions src/clear/sql/sql.cr
Original file line number Diff line number Diff line change
Expand Up @@ -181,12 +181,24 @@ module Clear

# Start an INSERT INTO table query
def insert_into(table, *args)
Clear::SQL::InsertQuery.new(table).insert(*args)
Clear::SQL::InsertQuery.new("default", table).insert(*args)
end

def insert_into(connection : Symbolic, table, *args)
Clear::SQL::InsertQuery.new(connection, table).insert(*args)
end

# Alias of `insert_into`, for hurry developers
def insert(table, *args)
insert_into(table, *args)
insert_into("default", table, *args)
end

def insert(table, args : NamedTuple)
insert_into("default", table, args)
end

def insert(connection : Symbolic, table, *args)
insert_into(connection, table, *args)
end

# Start a UPDATE table query
Expand Down

0 comments on commit 75408e5

Please sign in to comment.