Skip to content

Commit

Permalink
Fixing the specs to handle insert, update, and delete on secondary da…
Browse files Browse the repository at this point in the history
…tabases.
  • Loading branch information
russ authored and anykeyh committed Jun 24, 2018
1 parent 75408e5 commit ba9b82f
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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

Expand All @@ -69,13 +67,23 @@ module MultipleConnectionsSpec
p = PostStat.new({post_id: 1})
p.save

p = PostStat.query.first.not_nil!
p.post_id = 2
p.save

p = PostStat.query.first.not_nil!
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
21 changes: 8 additions & 13 deletions spec/spec_helper.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions src/clear/model/modules/has_saving.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions src/clear/sql/delete_query.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 0 additions & 5 deletions src/clear/sql/insert_query.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/clear/sql/query/execute.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
12 changes: 10 additions & 2 deletions src/clear/sql/sql.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
9 changes: 9 additions & 0 deletions src/clear/sql/update_query.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit ba9b82f

Please sign in to comment.