Skip to content

Commit

Permalink
Merge pull request mixigroup#13 from drecom/feature/fix_binary_insert
Browse files Browse the repository at this point in the history
Fix bug with saving binary column incorrectly
  • Loading branch information
Gussan authored and utah-KT committed Nov 5, 2019
1 parent 9a8c901 commit 93897ab
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 1 deletion.
1 change: 1 addition & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ namespace :turntable do
ActiveRecord::Base.connection.create_table :users do |t|
t.string :nickname
t.string :thumbnail_url
t.binary :blob
t.datetime :joined_at
t.datetime :deleted_at
t.timestamps
Expand Down
2 changes: 1 addition & 1 deletion lib/active_record/turntable/mixer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ def build_insert_fader(tree, method, query, *args, &block)
tree.values = [[SQLTree::Node::Expression::Variable.new("\\0")]]
sql = tree.to_sql
value_sql = vs.map do |val|
"(#{val.map { |v| @proxy.connection.quote(v.value)}.join(', ')})"
"(#{val.map { |v| "#{v.escape}#{@proxy.connection.quote(v.value)}" }.join(', ')})"
end.join(', ')
sql.gsub!('("\0")') { value_sql }
shards_with_query[@proxy.cluster.select_shard(k)] = sql
Expand Down
4 changes: 4 additions & 0 deletions lib/active_record/turntable/sql_tree_patch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,10 @@ def to_sql(options = {})
end
end

class Value
leaf :escape
end

class EscapedValue < Value
def initialize(value, escape = nil)
@value = value
Expand Down
14 changes: 14 additions & 0 deletions spec/active_record/turntable/active_record_ext/persistence_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,20 @@
Card.create!(:name => 'foobar')
}

context "When creating record" do
context "with blob column" do
let(:blob_value) { "\123\123\123" }
let(:user) {
u = User.new(:nickname => 'x', :blob => blob_value )
u.id = 1
u.save
u
}
subject { user }
its(:blob) { is_expected.to eq(user.reload.blob) }
end
end

context "When the model is sharded by surrogate key" do
it "should not changed from normal operation when updating" do
user.nickname = "fizzbuzz"
Expand Down
1 change: 1 addition & 0 deletions spec/migrations/001_create_users.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ def self.up
create_table :users do |t|
t.string :nickname
t.string :thumbnail_url
t.binary :blob
t.datetime :joined_at
t.datetime :deleted_at

Expand Down

0 comments on commit 93897ab

Please sign in to comment.