From 9e9b5de03f0f32e85c92e72f237861a19c50dfe5 Mon Sep 17 00:00:00 2001 From: frvade Date: Mon, 25 May 2020 14:53:08 +0300 Subject: [PATCH] Fix ambiguous column error (#5) --- .rubocop.yml | 2 +- lib/sequel/extensions/batches/yielder.rb | 5 +++-- sequel-batches.gemspec | 4 ++-- spec/sequel/extensions/batches_spec.rb | 4 ++++ spec/spec_helper.rb | 16 +++++++++------- 5 files changed, 19 insertions(+), 12 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index 5874f4e..142f1d9 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -5,5 +5,5 @@ AllCops: DisplayCopNames: true TargetRubyVersion: 2.3 -Naming/UncommunicativeMethodParamName: +Naming/MethodParameterName: Enabled: false diff --git a/lib/sequel/extensions/batches/yielder.rb b/lib/sequel/extensions/batches/yielder.rb index 09eaf2d..7e896d0 100644 --- a/lib/sequel/extensions/batches/yielder.rb +++ b/lib/sequel/extensions/batches/yielder.rb @@ -26,7 +26,8 @@ def call base_ds end - current_instance = db.from(working_ds.limit(of)).select(*pk).order(*pk).last or break + working_ds_pk = working_ds.select(*qualified_pk).limit(of) + current_instance = db.from(working_ds_pk).select(*pk).order(*pk).last or break working_ds = working_ds.where(generate_conditions(current_instance.to_h, sign: :<=)) yield working_ds @@ -67,7 +68,7 @@ def setup_base_ds base_ds = base_ds.where(generate_conditions(check_pk(start), sign: :>=)) if start base_ds = base_ds.where(generate_conditions(check_pk(finish), sign: :<=)) if finish - pk_ds = db.from(base_ds).select(*pk).order(*pk) + pk_ds = db.from(base_ds.select(*qualified_pk)).select(*pk).order(*pk) actual_start = pk_ds.first actual_finish = pk_ds.last diff --git a/sequel-batches.gemspec b/sequel-batches.gemspec index b27ad00..de168e1 100644 --- a/sequel-batches.gemspec +++ b/sequel-batches.gemspec @@ -5,8 +5,8 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) Gem::Specification.new do |spec| spec.name = "sequel-batches" - spec.version = "1.0.0" - spec.authors = ["fiscal-cliff", "umbrellio"] + spec.version = "1.0.1" + spec.authors = %w[fiscal-cliff umbrellio] spec.email = ["oss@umbrellio.biz"] spec.summary = "The extension mimics AR5 batches api" diff --git a/spec/sequel/extensions/batches_spec.rb b/spec/sequel/extensions/batches_spec.rb index 7e4cc62..37a165f 100644 --- a/spec/sequel/extensions/batches_spec.rb +++ b/spec/sequel/extensions/batches_spec.rb @@ -87,4 +87,8 @@ it "raises MissingPKError in case of missing pk" do expect { DB[:points].in_batches {} }.to raise_error(Sequel::Extensions::Batches::MissingPKError) end + + it "qualifies pk to mitigate ambiguous column error" do + expect { DB[:data, :data2].in_batches {} }.not_to raise_error + end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 8f2648f..abea11c 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -53,15 +53,17 @@ def connect config.before(:all) do DB.extension :batches - DB.drop_table?(:data) - DB.create_table?(:data) do - primary_key :id - column :created_at, "text" - column :value, "int" + %i[data data2].each do |table| + DB.drop_table?(table) + DB.create_table?(table) do + primary_key :id + column :created_at, "text" + column :value, "int" + end + + DB[table].multi_insert(YAML.load_file("./spec/fixtures/data.yml")) end - DB[:data].multi_insert(YAML.load_file("./spec/fixtures/data.yml")) - DB.drop_table?(:points) DB.create_table?(:points) do column :x, "int"