Skip to content

Commit

Permalink
Merge pull request #81 from drecom/feature/fix_boolean_insert_error
Browse files Browse the repository at this point in the history
Fixes sql parser errors when inserting values with boolean columns
  • Loading branch information
Gussan committed Oct 1, 2018
2 parents 91c2e0c + aaeaf14 commit 775db4e
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 3 deletions.
30 changes: 29 additions & 1 deletion lib/active_record/turntable/sql_tree_patch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class << self
end

class SQLTree::Token
extended_keywords = %w(BINARY LIMIT OFFSET INDEX KEY USE FORCE IGNORE)
extended_keywords = %w(BINARY LIMIT OFFSET INDEX KEY USE FORCE IGNORE TRUE FALSE)
KEYWORDS.concat(extended_keywords)

extended_keywords.each do |kwd|
Expand Down Expand Up @@ -264,6 +264,34 @@ def to_sql(options = {})

class Value
leaf :escape

def to_sql(options = {})
case value
when nil; 'NULL'
when true; 'TRUE'
when false; 'FALSE'
when String; quote_str(@value)
when Numeric; @value.to_s
when Date; @value.strftime("'%Y-%m-%d'")
when DateTime, Time; @value.strftime("'%Y-%m-%d %H:%M:%S'")
else raise "Don't know how te represent this value in SQL!"
end
end

def self.parse(tokens)
case tokens.next
when SQLTree::Token::String, SQLTree::Token::Number
SQLTree::Node::Expression::Value.new(tokens.current.literal)
when SQLTree::Token::NULL
SQLTree::Node::Expression::Value.new(nil)
when SQLTree::Token::TRUE
SQLTree::Node::Expression::Value.new(true)
when SQLTree::Token::FALSE
SQLTree::Node::Expression::Value.new(false)
else
raise SQLTree::Parser::UnexpectedToken.new(tokens.current, :literal)
end
end
end

class EscapedValue < Value
Expand Down
12 changes: 12 additions & 0 deletions spec/active_record/turntable/mixer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@
end
end

context "#build_insert_fader" do
context "with boolean `TRUE` column value" do
subject { UserProfile.create(user: user, published: true) }

let(:user) { create(:user) }

it { expect { subject }.not_to raise_error }
its(:errors) { is_expected.to be_empty }
its(:published) { is_expected.to eq(true) }
end
end

context "#find_shard_keys" do
subject { @mixer.find_shard_keys(sql_tree.where, "users", "id") }

Expand Down
10 changes: 8 additions & 2 deletions spec/factories/user_profiles.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
FactoryBot.define do
factory :user_profile do
birthday { Faker::Date.birthday }
published { false }
user

trait :created_yesterday do
created_at 1.day.ago
updated_at 1.day.ago
created_at { 1.day.ago }
updated_at { 1.day.ago }
end

trait :published do
published { true }
end
end
end
1 change: 1 addition & 0 deletions spec/migrations/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
t.belongs_to :user, null: false
t.date :birthday
t.text :data
t.boolean :published, null: false, default: false
t.integer :lock_version, null: false, default: 0
t.datetime :deleted_at, default: nil
t.timestamps
Expand Down

0 comments on commit 775db4e

Please sign in to comment.