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 inconsistent behaviour across adapters with read types and "create" command #432

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions lib/rom/sql/commands/create.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ def finalize(tuples, *)
# @api private
def insert(tuples)
pks = tuples.map { |tuple| relation.insert(tuple) }
relation.where(relation.primary_key => pks).to_a
relation.dataset.where(relation.primary_key => pks).to_a
end

# Executes multi_insert statement and returns inserted tuples
#
# @api private
def multi_insert(tuples)
pks = relation.multi_insert(tuples, return: :primary_key)
relation.where(relation.primary_key => pks).to_a
relation.dataset.where(relation.primary_key => pks).to_a
end

# Yields tuples for insertion or return an enumerator
Expand Down
16 changes: 16 additions & 0 deletions spec/integration/commands/create_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,22 @@ def self.[](input)
])
end

context "with json notes" do
include_context "json_notes"

before do
conf.commands(:json_notes) do
define(:create)
end
end

it "writes and reads back custom type" do
json_notes = commands[:json_notes]

expect(json_notes[:create].call(note: "this is my note")).to eq([{id: 1, note: "this is my note"}])
end
end

it "re-raises not-null constraint violation error" do
expect {
create_user.call(name: nil)
Expand Down
19 changes: 19 additions & 0 deletions spec/integration/commands/update_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,25 @@ def by_name(name)

expect(result).to eq([{id: 1, name: "Josie"}, {id: 2, name: "Josie"}])
end

context "with json notes" do
include_context "json_notes"

before do
conf.commands(:json_notes) do
define(:update)
end
end

let(:json_notes) { container.relations[:json_notes] }

it "writes and reads back custom type" do
note_id = conn[:json_notes].insert(note: "note version 1")
result = json_notes.by_pk(note_id).command(:update).call(note: "note version 2")

expect(result).to eq([{id: 1, note: "note version 2"}])
end
end
end
end
end
23 changes: 23 additions & 0 deletions spec/shared/json_notes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

RSpec.shared_context "json_notes" do
before do
inferrable_relations.concat %i[json_notes]
end

before do |_example|
conn.create_table :json_notes do
primary_key :id
String :note
end

write_type = Dry.Types.Constructor(String) { |value| JSON.dump({content: value}) }
read_type = Dry.Types.Constructor(String) { |value| JSON.parse(value)["content"] }

conf.relation(:json_notes) do
schema(infer: true) do
attribute :note, write_type, read: read_type
end
end
end
end
Loading