Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug with saving binary column incorrectly #13

Merged
merged 1 commit into from
Nov 19, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,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 @@ -214,7 +214,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.shard_for(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 @@ -35,6 +35,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