Skip to content

Commit

Permalink
Add raise InvalidPrimaryKey
Browse files Browse the repository at this point in the history
- Use auto_increment_id of PrimaryKey == 0 in MySQL, not use sequencer id
  • Loading branch information
hirocaster committed Mar 31, 2016
1 parent 65df599 commit fe9eadd
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/active_record/sharding/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,8 @@ class MissingPrimaryKey < Error

class InvalidSequenceId < Error
end

class InvalidPrimaryKey < Error
end
end
end
1 change: 1 addition & 0 deletions lib/active_record/sharding/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ def define_parent_methods(&block)

def validate_id!
raise ActiveRecord::Sharding::MissingPrimaryKey if attributes[self.class.primary_key].nil?
raise ActiveRecord::Sharding::InvalidPrimaryKey if attributes[self.class.primary_key].zero?
end
end
end
Expand Down
4 changes: 4 additions & 0 deletions spec/active_record/sharding/model_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ def find_from_all_by_name(name)
expect(alice.class.name).to match(/User::ShardFor/)
end

it "raise InvalidPrimaryKey" do
expect { model.put! id: 0, name: "invalid_id" }.to raise_error ActiveRecord::Sharding::InvalidPrimaryKey
end

context "in transaction" do
it "when rollback" do
before_record_count = model.all_shards.map(&:count).reduce(:+)
Expand Down

0 comments on commit fe9eadd

Please sign in to comment.