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
35 changes: 1 addition & 34 deletions lib/staging_table/session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,14 @@ class Session
# - after_transfer: ->(session, result) { ... }
CALLBACK_OPTIONS = %i[before_insert after_insert before_transfer after_transfer].freeze

# All non-callback options the session understands. Any other keyword
# passed to `.new` raises `ConfigurationError` so typos surface up front
# instead of silently disappearing (see issue #33).
KNOWN_OPTIONS = %i[
batch_size
transfer_strategy
conflict_target
conflict_action
excluded_columns
include_indexes
].freeze

def initialize(source_model, **options)
@source_model = source_model
config = StagingTable.configuration
@callbacks = options.slice(*CALLBACK_OPTIONS)
non_callback_options = options.except(*CALLBACK_OPTIONS)
validate_options!(non_callback_options)
@options = {
batch_size: config.default_batch_size,
transfer_strategy: config.default_transfer_strategy
}.merge(non_callback_options)
}.merge(options.except(*CALLBACK_OPTIONS))
@table_created = false
end

Expand Down Expand Up @@ -196,24 +182,5 @@ def normalize_record(record)

record.attributes_for_database
end

def validate_options!(options)
unknown = options.keys - KNOWN_OPTIONS
return if unknown.empty?

messages = unknown.map do |key|
suggestion = DidYouMean::SpellChecker.new(dictionary: KNOWN_OPTIONS.map(&:to_s)).correct(key.to_s).first
if suggestion
"#{key.inspect} (did you mean #{suggestion.to_sym.inspect}?)"
else
key.inspect
end
end

allowed = (KNOWN_OPTIONS + CALLBACK_OPTIONS).sort.map(&:inspect).join(", ")
raise ConfigurationError,
"Unknown option(s) for StagingTable::Session: #{messages.join(', ')}. " \
"Allowed options: #{allowed}."
end
end
end
44 changes: 0 additions & 44 deletions spec/staging_table/session_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -249,50 +249,6 @@
end
end

describe ".new option validation" do
it "accepts all documented KNOWN_OPTIONS" do
expect {
described_class.new(TestUser,
batch_size: 500,
transfer_strategy: :insert,
conflict_target: [:email],
conflict_action: :update,
excluded_columns: %w[created_at],
include_indexes: true)
}.not_to raise_error
end

it "still accepts callback options alongside known options" do
expect {
described_class.new(TestUser,
batch_size: 100,
before_insert: ->(_) {},
after_transfer: ->(_, _) {})
}.not_to raise_error
end

it "raises ConfigurationError on a typo'd option" do
expect {
described_class.new(TestUser, transfer_stratgy: :upsert)
}.to raise_error(StagingTable::ConfigurationError, /:transfer_stratgy/)
end

it "suggests the closest known option in the error message" do
expect {
described_class.new(TestUser, confict_target: [:email])
}.to raise_error(StagingTable::ConfigurationError, /did you mean :conflict_target/)
end

it "lists every unknown option when multiple are passed" do
expect {
described_class.new(TestUser, transfer_stratgy: :upsert, made_up: 1)
}.to raise_error(StagingTable::ConfigurationError) { |e|
expect(e.message).to include(":transfer_stratgy")
expect(e.message).to include(":made_up")
}
end
end

context "with MySQL", :mysql do
include_examples "session"
end
Expand Down
Loading