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

Assign next_sequence_id automatically before put/save #28

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 22 additions & 0 deletions lib/active_record/sharding/facade.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require "active_support/concern"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/FrozenStringLiteralComment: Missing magic comment # frozen_string_literal: true.


module ActiveRecord
module Sharding
module Facade
extend ActiveSupport::Concern

include Model
include Sequencer

included do
before_put do |attributes|
attributes[:id] ||= next_sequence_id
end

before_save on: :create do
self.id ||= self.class.next_sequence_id
end
end
end
end
end
1 change: 1 addition & 0 deletions lib/activerecord-sharding.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
require "active_record/sharding/sequencer"
require "active_record/sharding/sequencer_repository"
require "active_record/sharding/sequencer_config"
require "active_record/sharding/facade"

module ActiveRecord
module Sharding
Expand Down
47 changes: 47 additions & 0 deletions spec/active_record/sharding/facade_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
describe ActiveRecord::Sharding::Facade do
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Metrics/BlockLength: Block has too many lines. [37/25]
Style/FrozenStringLiteralComment: Missing magic comment # frozen_string_literal: true.

let!(:model) do
Class.new(ActiveRecord::Base) do
def self.name
"User"
end

include ActiveRecord::Sharding::Facade

use_sharding :user, :modulo
define_sharding_key :id
use_sequencer :user
end
end

describe "including modules" do
it "includes ActiveRecord::Sharding::Model and ActiveRecord::Sharding::Sequencer" do
expect(model).to be_include ActiveRecord::Sharding::Model
expect(model).to be_include ActiveRecord::Sharding::Sequencer
end
end

shared_examples_for "successfully" do
it "inserts a record" do
expect(alice.persisted?).to be true
expect(alice.id).to eq model.current_sequence_id
expect(alice.name).to eq "Alice"
expect(alice.class.name).to match(/User::ShardFor/)
end
end

describe ".put!" do
let(:alice) { model.put!(name: "Alice") }

it_behaves_like "successfully"
end

describe ".new and #save" do
let(:alice) do
alice = model.shard_for(1).new(name: "Alice")
alice.save
alice
end

it_behaves_like "successfully"
end
end