Skip to content

Commit

Permalink
Add raise InvalidSequenceId
Browse files Browse the repository at this point in the history
- Error in case of forget insert sequencer record
  • Loading branch information
hirocaster committed Mar 31, 2016
1 parent fb26562 commit 65df599
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
3 changes: 3 additions & 0 deletions lib/active_record/sharding/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,8 @@ class MissingShardingKeyAttribute < Error

class MissingPrimaryKey < Error
end

class InvalidSequenceId < Error
end
end
end
4 changes: 3 additions & 1 deletion lib/active_record/sharding/sequencer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ def current_sequence_id

def next_sequence_id(offset = 1)
raise NegativeNumberOffsetError if offset < 1
execute_sql "id +#{offset}"
sequence_id = execute_sql "id +#{offset}"
raise ActiveRecord::Sharding::InvalidSequenceId if sequence_id.zero?
sequence_id
end

def execute_sql(last_insert_id_args)
Expand Down
20 changes: 20 additions & 0 deletions spec/active_record/sharding/sequencer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,26 @@ def self.name
end
end

let(:sequencer_args) do
{ sequencer_name: "user" }
end

context "forget insert sequencer record" do
before do
ActiveRecord::Sharding::DatabaseTasks.drop_sequencer_database sequencer_args
ActiveRecord::Sharding::DatabaseTasks.create_sequencer_database sequencer_args
ActiveRecord::Sharding::DatabaseTasks.create_table_sequencer_database sequencer_args
end

after do
ActiveRecord::Sharding::DatabaseTasks.insert_initial_record_sequencer_database sequencer_args # forget this in setup
end

it "raise ActiveRecord::Sharding::InvalidSequenceId" do
expect { model.next_sequence_id }.to raise_error ActiveRecord::Sharding::InvalidSequenceId
end
end

describe '#current_sequence_id' do
it "returns current sequence id" do
current_id = model.current_sequence_id
Expand Down

0 comments on commit 65df599

Please sign in to comment.