Skip to content

Commit

Permalink
Supported Rails 5.2 (#41)
Browse files Browse the repository at this point in the history
* Add splat arguments to `ActiveRecord::QueryMethods#build_arel` .
* Support for adding `partition_key` with `model.update` and `model.destroy` .
  * `#build_arel` is not called with `model.update` and `model.destroy` in Rails 5.2.
  * Override `ActiveRecord::ConnectionAdapters::DatabaseStatements#update` / `#delete`
* Add `gemfiles/active_record_5.2.gemfile`
  • Loading branch information
osyo-manga authored and lfittl committed Jun 22, 2018
1 parent e440877 commit bcf15a7
Show file tree
Hide file tree
Showing 6 changed files with 248 additions and 8 deletions.
3 changes: 1 addition & 2 deletions .travis.yml
Expand Up @@ -16,11 +16,10 @@ gemfile:
- gemfiles/rails_5.1.gemfile
- gemfiles/rails_5.2.gemfile
- gemfiles/active_record_5.1.gemfile
- gemfiles/active_record_5.2.gemfile

matrix:
fast_finish: true
allow_failures:
- gemfile: gemfiles/rails_5.2.gemfile
exclude:
- rvm: 2.4.1
gemfile: gemfiles/rails_4.0.gemfile
Expand Down
4 changes: 4 additions & 0 deletions Appraisals
Expand Up @@ -25,3 +25,7 @@ end
appraise 'active-record-5.1' do
gem 'activerecord', '5.1.0'
end

appraise 'active-record-5.2' do
gem 'activerecord', '5.2.0'
end
8 changes: 8 additions & 0 deletions gemfiles/active_record_5.2.gemfile
@@ -0,0 +1,8 @@
# This file was generated by Appraisal

source "https://rubygems.org"

gem "appraisal"
gem "activerecord", "5.2.0"

gemspec :path => "../"
182 changes: 182 additions & 0 deletions gemfiles/active_record_5.2.gemfile.lock
@@ -0,0 +1,182 @@
PATH
remote: ..
specs:
activerecord-multi-tenant (0.8.1)
rails (>= 4.0)
request_store (>= 1.0.5)

GEM
remote: https://rubygems.org/
specs:
actioncable (5.2.0)
actionpack (= 5.2.0)
nio4r (~> 2.0)
websocket-driver (>= 0.6.1)
actionmailer (5.2.0)
actionpack (= 5.2.0)
actionview (= 5.2.0)
activejob (= 5.2.0)
mail (~> 2.5, >= 2.5.4)
rails-dom-testing (~> 2.0)
actionpack (5.2.0)
actionview (= 5.2.0)
activesupport (= 5.2.0)
rack (~> 2.0)
rack-test (>= 0.6.3)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.0, >= 1.0.2)
actionview (5.2.0)
activesupport (= 5.2.0)
builder (~> 3.1)
erubi (~> 1.4)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.0, >= 1.0.3)
activejob (5.2.0)
activesupport (= 5.2.0)
globalid (>= 0.3.6)
activemodel (5.2.0)
activesupport (= 5.2.0)
activerecord (5.2.0)
activemodel (= 5.2.0)
activesupport (= 5.2.0)
arel (>= 9.0)
activestorage (5.2.0)
actionpack (= 5.2.0)
activerecord (= 5.2.0)
marcel (~> 0.3.1)
activesupport (5.2.0)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (>= 0.7, < 2)
minitest (~> 5.1)
tzinfo (~> 1.1)
appraisal (2.2.0)
bundler
rake
thor (>= 0.14.0)
arel (9.0.0)
builder (3.2.3)
byebug (10.0.2)
coderay (1.1.2)
concurrent-ruby (1.0.5)
connection_pool (2.2.2)
crass (1.0.4)
diff-lcs (1.3)
erubi (1.7.1)
globalid (0.4.1)
activesupport (>= 4.2.0)
i18n (1.0.1)
concurrent-ruby (~> 1.0)
loofah (2.2.2)
crass (~> 1.0.2)
nokogiri (>= 1.5.9)
mail (2.7.0)
mini_mime (>= 0.1.1)
marcel (0.3.2)
mimemagic (~> 0.3.2)
method_source (0.9.0)
mimemagic (0.3.2)
mini_mime (1.0.0)
mini_portile2 (2.3.0)
minitest (5.11.3)
nio4r (2.3.1)
nokogiri (1.8.2)
mini_portile2 (~> 2.3.0)
pg (1.0.0)
pry (0.11.3)
coderay (~> 1.1.0)
method_source (~> 0.9.0)
pry-byebug (3.6.0)
byebug (~> 10.0)
pry (~> 0.10)
rack (2.0.5)
rack-protection (2.0.3)
rack
rack-test (1.0.0)
rack (>= 1.0, < 3)
rails (5.2.0)
actioncable (= 5.2.0)
actionmailer (= 5.2.0)
actionpack (= 5.2.0)
actionview (= 5.2.0)
activejob (= 5.2.0)
activemodel (= 5.2.0)
activerecord (= 5.2.0)
activestorage (= 5.2.0)
activesupport (= 5.2.0)
bundler (>= 1.3.0)
railties (= 5.2.0)
sprockets-rails (>= 2.0.0)
rails-dom-testing (2.0.3)
activesupport (>= 4.2.0)
nokogiri (>= 1.6)
rails-html-sanitizer (1.0.4)
loofah (~> 2.2, >= 2.2.2)
railties (5.2.0)
actionpack (= 5.2.0)
activesupport (= 5.2.0)
method_source
rake (>= 0.8.7)
thor (>= 0.18.1, < 2.0)
rake (12.3.1)
redis (4.0.1)
request_store (1.4.1)
rack (>= 1.4)
rspec (3.7.0)
rspec-core (~> 3.7.0)
rspec-expectations (~> 3.7.0)
rspec-mocks (~> 3.7.0)
rspec-core (3.7.1)
rspec-support (~> 3.7.0)
rspec-expectations (3.7.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.7.0)
rspec-mocks (3.7.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.7.0)
rspec-rails (3.7.2)
actionpack (>= 3.0)
activesupport (>= 3.0)
railties (>= 3.0)
rspec-core (~> 3.7.0)
rspec-expectations (~> 3.7.0)
rspec-mocks (~> 3.7.0)
rspec-support (~> 3.7.0)
rspec-support (3.7.1)
sidekiq (5.1.3)
concurrent-ruby (~> 1.0)
connection_pool (~> 2.2, >= 2.2.0)
rack-protection (>= 1.5.0)
redis (>= 3.3.5, < 5)
sprockets (3.7.1)
concurrent-ruby (~> 1.0)
rack (> 1, < 3)
sprockets-rails (3.2.1)
actionpack (>= 4.0)
activesupport (>= 4.0)
sprockets (>= 3.0.0)
thor (0.20.0)
thread_safe (0.3.6)
tzinfo (1.2.5)
thread_safe (~> 0.1)
websocket-driver (0.7.0)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.3)

PLATFORMS
ruby

DEPENDENCIES
activerecord (= 5.2.0)
activerecord-multi-tenant!
appraisal
pg
pry
pry-byebug
rake
rspec (>= 3.0)
rspec-rails
sidekiq
thor

BUNDLED WITH
1.16.1
22 changes: 20 additions & 2 deletions lib/activerecord-multi-tenant/query_rewriter.rb
Expand Up @@ -203,6 +203,24 @@ def join_to_delete(delete, *args)
end
delete
end

if ActiveRecord::VERSION::MAJOR >= 5 && ActiveRecord::VERSION::MINOR >= 2
def update(arel, name = nil, binds = [])
model = MultiTenant.multi_tenant_model_for_table(arel.ast.relation.table_name)
if model.present? && !MultiTenant.with_write_only_mode_enabled?
arel.where(MultiTenant::TenantEnforcementClause.new(model.arel_table[model.partition_key]))
end
super(arel, name, binds)
end

def delete(arel, name = nil, binds = [])
model = MultiTenant.multi_tenant_model_for_table(arel.ast.left.table_name)
if model.present? && !MultiTenant.with_write_only_mode_enabled?
arel.where(MultiTenant::TenantEnforcementClause.new(model.arel_table[model.partition_key]))
end
super(arel, name, binds)
end
end
end
end

Expand All @@ -215,8 +233,8 @@ def join_to_delete(delete, *args)
module ActiveRecord
module QueryMethods
alias :build_arel_orig :build_arel
def build_arel
arel = build_arel_orig
def build_arel(*args)
arel = build_arel_orig(*args)

if MultiTenant.current_tenant_id && !MultiTenant.with_write_only_mode_enabled?
visitor = MultiTenant::ArelTenantVisitor.new(arel)
Expand Down
37 changes: 33 additions & 4 deletions spec/activerecord-multi-tenant/query_rewriter_spec.rb
Expand Up @@ -14,19 +14,48 @@
end
}.to change { project.reload.name }.from("Project 1").to("New Name")
end

it "update the record" do
expect {
MultiTenant.with(account) do
project.update(name: "New Name")
end
}.to change { project.reload.name }.from("Project 1").to("New Name")
end
end

context "when bulk deleting" do
let!(:account) { Account.create!(name: "Test Account") }
let!(:project) { Project.create(name: "Project 1", account: account) }
let!(:manager) { Manager.create(name: "Manager", project: project, account: account) }
let!(:project1) { Project.create(name: "Project 1", account: account) }
let!(:project2) { Project.create(name: "Project 2", account: account) }
let!(:project3) { Project.create(name: "Project 3", account: account) }
let!(:manager1) { Manager.create(name: "Manager 1", project: project1, account: account) }
let!(:manager2) { Manager.create(name: "Manager 2", project: project2, account: account) }

it "deletes the records" do
it "delete_all the records" do
expect {
MultiTenant.with(account) do
Project.joins(:manager).delete_all
end
}.to change { Project.count }.from(1).to(0)
}.to change { Project.count }.from(3).to(1)
end

it "delete the record" do
expect {
MultiTenant.with(account) do
project1.delete
Project.delete(project2.id)
end
}.to change { Project.count }.from(3).to(1)
end

it "destroy the record" do
expect {
MultiTenant.with(account) do
project1.destroy
Project.destroy(project2.id)
end
}.to change { Project.count }.from(3).to(1)
end
end
end

0 comments on commit bcf15a7

Please sign in to comment.