diff --git a/spec/model/multiple_connections.cr b/spec/model/multiple_connections_spec.cr similarity index 80% rename from spec/model/multiple_connections.cr rename to spec/model/multiple_connections_spec.cr index 714062257..92b5383d2 100644 --- a/spec/model/multiple_connections.cr +++ b/spec/model/multiple_connections_spec.cr @@ -33,7 +33,6 @@ module MultipleConnectionsSpec def self.reinit reinit_migration_manager ModelSpecMigration1234.new.apply(Clear::Migration::Direction::UP) - init_secondary_db end describe "Clear::Model" do @@ -46,7 +45,6 @@ module MultipleConnectionsSpec 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 @@ -56,10 +54,10 @@ module MultipleConnectionsSpec 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) + p = PostStat.new({post_id: 1}) + p.save + p.persisted?.should be_true + p.post_id.should eq(1) end end @@ -69,6 +67,7 @@ module MultipleConnectionsSpec p = PostStat.new({post_id: 1}) p.save + p = PostStat.query.first.not_nil! p.post_id = 2 p.save @@ -76,6 +75,15 @@ module MultipleConnectionsSpec p.post_id.should eq(2) end end + + it "can update data on the secondary database" do + temporary do + reinit + p = PostStat.new({post_id: 1}) + p.save + p.delete.should be_true + end + end end end end diff --git a/spec/spec_helper.cr b/spec/spec_helper.cr index f0a09949e..a114cfd83 100644 --- a/spec/spec_helper.cr +++ b/spec/spec_helper.cr @@ -6,29 +6,24 @@ def initdb system("echo \"DROP DATABASE IF EXISTS clear_spec;\" | psql -U postgres") system("echo \"CREATE DATABASE clear_spec;\" | psql -U postgres") - Clear::SQL.init("postgres://postgres@localhost/clear_spec") - - {% if flag?(:quiet) %} - Clear.logger.level = ::Logger::ERROR - {% else %} - Clear.logger.level = ::Logger::DEBUG - {% end %} -end - -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") Clear::SQL.init({ - "deafult" => "postgres://postgres@localhost/clear_spec", + "default" => "postgres://postgres@localhost/clear_spec", "secondary" => "postgres://postgres@localhost/clear_secondary_spec", }) + + {% if flag?(:quiet) %} + Clear.logger.level = ::Logger::ERROR + {% else %} + Clear.logger.level = ::Logger::DEBUG + {% end %} end def reinit_migration_manager - Clear::Migration::Manager.instance.reinit! + Clear::Migration::Manager.instance.reinit! end def temporary(&block) diff --git a/src/clear/model/modules/has_saving.cr b/src/clear/model/modules/has_saving.cr index 56984fe12..47b319abf 100644 --- a/src/clear/model/modules/has_saving.cr +++ b/src/clear/model/modules/has_saving.cr @@ -20,7 +20,7 @@ module Clear::Model::HasSaving if h.any? with_triggers(:update) do - Clear::SQL.update(self.class.table).set(update_h).where { var("#{self.class.pkey}") == pkey }.execute + Clear::SQL.update(@@connection, self.class.table).set(update_h).where { var("#{self.class.pkey}") == pkey }.execute end end else @@ -54,7 +54,7 @@ module Clear::Model::HasSaving return false unless persisted? with_triggers(:delete) do - Clear::SQL::DeleteQuery.new.from(self.class.table).where{ var("#{self.class.pkey}") == pkey }.execute + Clear::SQL::DeleteQuery.new(@@connection).from(self.class.table).where{ var("#{self.class.pkey}") == pkey }.execute @persisted = false clear_change_flags diff --git a/src/clear/sql/delete_query.cr b/src/clear/sql/delete_query.cr index cbcb54b90..651fec103 100644 --- a/src/clear/sql/delete_query.cr +++ b/src/clear/sql/delete_query.cr @@ -3,12 +3,21 @@ require "./query/*" class Clear::SQL::DeleteQuery getter from : Symbolic? + include Query::Connection include Query::Where include Query::Execute include Query::Change def initialize(@from = nil, @wheres = [] of Clear::Expression::Node) + @connection = "default" + @connection_name = "default" + end + + def initialize(@connection : Symbolic, + @from = nil, + @wheres = [] of Clear::Expression::Node) + @connection_name = @connection end def from(x) diff --git a/src/clear/sql/insert_query.cr b/src/clear/sql/insert_query.cr index 00c58cdae..d9719db7b 100644 --- a/src/clear/sql/insert_query.cr +++ b/src/clear/sql/insert_query.cr @@ -65,11 +65,6 @@ 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 diff --git a/src/clear/sql/query/execute.cr b/src/clear/sql/query/execute.cr index 2de0e52e1..1da7e06ce 100644 --- a/src/clear/sql/query/execute.cr +++ b/src/clear/sql/query/execute.cr @@ -2,6 +2,6 @@ require "db" module Clear::SQL::Query::Execute def execute - Clear::SQL.execute(to_sql) + Clear::SQL.execute(self.connection_name, to_sql) end end diff --git a/src/clear/sql/sql.cr b/src/clear/sql/sql.cr index 527fa6734..9d8b1280a 100644 --- a/src/clear/sql/sql.cr +++ b/src/clear/sql/sql.cr @@ -176,7 +176,11 @@ module Clear # Start a DELETE table query def delete(table = nil) - Clear::SQL::DeleteQuery.new(from: table) + Clear::SQL::DeleteQuery.new("default", from: table) + end + + def delete(connection : Symbolic, table = nil) + Clear::SQL::DeleteQuery.new(connection, from: table) end # Start an INSERT INTO table query @@ -203,7 +207,11 @@ module Clear # Start a UPDATE table query def update(table) - Clear::SQL::UpdateQuery.new(table) + Clear::SQL::UpdateQuery.new("default", table) + end + + def update(connection : Symbolic, table) + Clear::SQL::UpdateQuery.new(connection, table) end # Start a SELECT FROM table query diff --git a/src/clear/sql/update_query.cr b/src/clear/sql/update_query.cr index 3c1a76ab1..07bd74a7d 100644 --- a/src/clear/sql/update_query.cr +++ b/src/clear/sql/update_query.cr @@ -8,14 +8,23 @@ class Clear::SQL::UpdateQuery alias UpdateInstruction = Hash(String, Updatable) | String @values : Array(UpdateInstruction) = [] of UpdateInstruction + @connection : Symbolic @table : String + include Query::Connection include Query::Change include Query::Where include Query::Execute def initialize(table, @wheres = [] of Clear::Expression::Node) @table = table.to_s + @connection = "default" + @connection_name = "default" + end + + def initialize(@connection : Symbolic, table, @wheres = [] of Clear::Expression::Node) + @table = table.to_s + @connection_name = @connection end def set(row : NamedTuple)