Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -217,13 +217,11 @@ def empty_insert_statement_value(primary_key = nil)
end

# Writes LOB values from attributes for specified columns
# TODO: composite primary key support — the +id+ lookup and the +WHERE+
# clause below assume a scalar +klass.primary_key+. For CPK models with
# CLOB/BLOB columns, fixture insertion and the post-save LOB rewrite
# path will fail. Handle the +Array+ case by zipping columns/values and
# building a multi-column predicate.
def write_lobs(table_name, klass, attributes, columns) # :nodoc:
id = quote(attributes[klass.primary_key])
pk_predicate = Array(klass.primary_key).map { |pk|
"#{quote_column_name(pk)} = #{quote(attributes[pk])}"
}.join(" AND ")

columns.each do |col|
value = attributes[col.name]
# changed sequence of next two lines - should check if value is nil before converting to yaml
Expand All @@ -234,7 +232,7 @@ def write_lobs(table_name, klass, attributes, columns) # :nodoc:
uncached do
unless lob_record = select_one(sql = <<~SQL.squish, "Writable Large Object")
SELECT #{quote_column_name(col.name)} FROM #{quote_table_name(table_name)}
WHERE #{quote_column_name(klass.primary_key)} = #{id} FOR UPDATE
WHERE #{pk_predicate} FOR UPDATE
SQL
raise ActiveRecord::RecordNotFound, "statement #{sql} returned no rows"
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,17 @@
t.string :title
t.integer :revision
end
create_table :cpk_docs, primary_key: [:author_id, :id], force: true do |t|
t.integer :author_id
t.integer :id
t.text :body
t.binary :data
end
end

stub_const("UberBarcode", Class.new(ActiveRecord::Base) { self.table_name = "uber_barcodes" })
stub_const("CpkBook", Class.new(ActiveRecord::Base) { self.table_name = "cpk_books" })
stub_const("CpkDoc", Class.new(ActiveRecord::Base) { self.table_name = "cpk_docs" })
end

after(:each) do
Expand All @@ -50,6 +57,7 @@
drop_table :barcodes_reverse, if_exists: true
drop_table :travels, if_exists: true
drop_table :cpk_books, if_exists: true
drop_table :cpk_docs, if_exists: true
end
@conn.schema_cache.clear!
end
Expand Down Expand Up @@ -340,4 +348,33 @@
expect(UberBarcode.pluck(:region, :code)).to eq([["US", 200]])
end
end

describe "CLOB / BLOB columns" do
it "create! persists a CLOB body above the inline VARCHAR2 limit" do
body = "x" * 5000
CpkDoc.create!(id: [1, 1], body: body)
expect(CpkDoc.find([1, 1]).body).to eq(body)
end

describe "with prepared_statements disabled" do
around(:each) do |example|
old_prepared_statements = @conn.prepared_statements
@conn.instance_variable_set(:@prepared_statements, false)
example.run
@conn.instance_variable_set(:@prepared_statements, old_prepared_statements)
end

it "create! writes a CLOB body via write_lobs when prepared_statements is false" do
body = "x" * 5000
CpkDoc.create!(id: [2, 2], body: body)
expect(CpkDoc.find([2, 2]).body).to eq(body)
end

it "create! writes a BLOB data column via write_lobs when prepared_statements is false" do
data = ("\x00\x01\x02\x03".b * 1500)
CpkDoc.create!(id: [3, 3], data: data)
expect(CpkDoc.find([3, 3]).data).to eq(data)
end
end
end
end