activerecord-multi-tenant 1.0.2 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -10,3 +10,4 @@ require_relative 'activerecord-multi-tenant/query_rewriter'
10
10
  require_relative 'activerecord-multi-tenant/query_monitor'
11
11
  require_relative 'activerecord-multi-tenant/version'
12
12
  require_relative 'activerecord-multi-tenant/with_lock'
13
+ require_relative 'activerecord-multi-tenant/persistence_extension'
@@ -0,0 +1,13 @@
1
+ module ActiveRecord
2
+ module Persistence
3
+ alias :delete_orig :delete
4
+
5
+ def delete
6
+ if persisted? && MultiTenant.current_tenant_id.nil?
7
+ MultiTenant.with(self.public_send(self.class.partition_key)) { delete_orig }
8
+ else
9
+ delete_orig
10
+ end
11
+ end
12
+ end
13
+ end
@@ -1,3 +1,3 @@
1
1
  module MultiTenant
2
- VERSION = '1.0.2'
2
+ VERSION = '1.0.3'
3
3
  end
@@ -348,15 +348,9 @@ describe MultiTenant do
348
348
  end
349
349
 
350
350
  it "applies the team_id conditions in the where clause" do
351
- expected_sql = if uses_prepared_statements? && ActiveRecord::VERSION::MAJOR == 5 && ActiveRecord::VERSION::MINOR == 0
352
- <<-sql
353
- SELECT "sub_tasks".* FROM "sub_tasks" INNER JOIN "tasks" ON "sub_tasks"."task_id" = "tasks"."id" AND "sub_tasks"."account_id" = "tasks"."account_id" WHERE "tasks"."account_id" = 1 AND "tasks"."project_id" = 1
354
- sql
355
- else
356
- <<-sql
351
+ expected_sql = <<-sql
357
352
  SELECT "sub_tasks".* FROM "sub_tasks" INNER JOIN "tasks" ON "sub_tasks"."task_id" = "tasks"."id" AND "sub_tasks"."account_id" = "tasks"."account_id" WHERE "tasks"."account_id" = 1 AND "sub_tasks"."account_id" = 1 AND "tasks"."project_id" = 1
358
353
  sql
359
- end
360
354
 
361
355
  account1 = Account.create! name: 'Account 1'
362
356
 
@@ -418,7 +412,7 @@ describe MultiTenant do
418
412
  account1 = Account.create! name: 'Account 1'
419
413
  category1 = Category.create! name: 'Category 1'
420
414
 
421
- expected_sql = if uses_prepared_statements? && ActiveRecord::VERSION::MAJOR == 5 && ActiveRecord::VERSION::MINOR > 1
415
+ expected_sql = if uses_prepared_statements? && ActiveRecord::VERSION::MAJOR == 5 && ActiveRecord::VERSION::MINOR > 0
422
416
  <<-sql
423
417
  SELECT "projects"."id" AS t0_r0, "projects"."account_id" AS t0_r1, "projects"."name" AS t0_r2, "categories"."id" AS t1_r0, "categories"."name" AS t1_r1 FROM "projects" LEFT OUTER JOIN "project_categories" ON "project_categories"."project_id" = "projects"."id" AND "project_categories"."account_id" = 1 AND "projects"."account_id" = 1 LEFT OUTER JOIN "categories" ON "categories"."id" = "project_categories"."category_id" AND "project_categories"."account_id" = 1 WHERE "projects"."account_id" = 1
424
418
  sql
@@ -461,7 +455,7 @@ describe MultiTenant do
461
455
  category1 = Category.create! name: 'Category 1'
462
456
 
463
457
  MultiTenant.with(account1) do
464
- expected_sql = if uses_prepared_statements? && ActiveRecord::VERSION::MAJOR == 5 && ActiveRecord::VERSION::MINOR > 1
458
+ expected_sql = if uses_prepared_statements? && ActiveRecord::VERSION::MAJOR == 5 && ActiveRecord::VERSION::MINOR > 0
465
459
  <<-sql
466
460
  SELECT "tasks".* FROM "tasks" INNER JOIN "projects" ON "projects"."id" = "tasks"."project_id" AND "projects"."account_id" = 1 LEFT JOIN project_categories pc ON project.category_id = pc.id WHERE "tasks"."account_id" = 1
467
461
  sql
@@ -2,13 +2,57 @@ require 'spec_helper'
2
2
 
3
3
  describe MultiTenant, 'Record modifications' do
4
4
  let(:account) { Account.create! name: 'test' }
5
- let(:project) { account.projects.create! name: 'something' }
5
+ let(:account2) { Account.create! name: 'test2' }
6
+ let(:project) { Project.create! name: 'something', account: account }
7
+ let(:project2) { Project.create! name: 'something2', account: account2, id: project.id }
8
+
9
+
10
+ it 'includes the tenant_id in DELETEs when using object.destroy' do
11
+ # two records with same id but different account_id
12
+ # when doing project.destroy it should delete only the current one
13
+ # by adding account_id to the destroy
14
+
15
+ expect(project.account).to eq(account)
16
+ expect(project2.account).to eq(account2)
17
+ expect(project.id).to eq(project2.id)
18
+
19
+ MultiTenant.without() do
20
+ expect(Project.count).to eq(2)
21
+ project.destroy
22
+ expect(Project.count).to eq(1)
23
+ end
24
+
25
+ MultiTenant.with(account) do
26
+ expect(Project.where(id: project.id).first).not_to be_present
27
+ end
28
+ MultiTenant.with(account2) do
29
+ expect(Project.where(id: project2.id).first).to be_present
30
+ end
31
+
32
+ end
33
+
34
+ it 'includes the tenant_id in DELETEs when using object.delete' do
35
+ # two records with same id but different account_id
36
+ # when project.delete it should delete only the current one
37
+ # by adding account_id to the destroy
38
+
39
+ expect(project.account).to eq(account)
40
+ expect(project2.account).to eq(account2)
41
+ expect(project.id).to eq(project2.id)
42
+
43
+ MultiTenant.without() do
44
+ expect(Project.count).to eq(2)
45
+ project.delete
46
+ expect(Project.count).to eq(1)
47
+ end
6
48
 
7
- it 'includes the tenant_id in DELETEs' do
8
- project.destroy
9
49
  MultiTenant.with(account) do
10
50
  expect(Project.where(id: project.id).first).not_to be_present
11
51
  end
52
+ MultiTenant.with(account2) do
53
+ expect(Project.where(id: project2.id).first).to be_present
54
+ end
55
+
12
56
  end
13
57
 
14
58
  it 'includes the tenant_id in UPDATEs' do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord-multi-tenant
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Citus Data
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-09-20 00:00:00.000000000 Z
11
+ date: 2019-10-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: request_store
@@ -191,6 +191,7 @@ files:
191
191
  - lib/activerecord-multi-tenant/migrations.rb
192
192
  - lib/activerecord-multi-tenant/model_extensions.rb
193
193
  - lib/activerecord-multi-tenant/multi_tenant.rb
194
+ - lib/activerecord-multi-tenant/persistence_extension.rb
194
195
  - lib/activerecord-multi-tenant/query_monitor.rb
195
196
  - lib/activerecord-multi-tenant/query_rewriter.rb
196
197
  - lib/activerecord-multi-tenant/sidekiq.rb
@@ -204,6 +205,7 @@ files:
204
205
  - spec/activerecord-multi-tenant/record_callback_spec.rb
205
206
  - spec/activerecord-multi-tenant/record_finding_spec.rb
206
207
  - spec/activerecord-multi-tenant/record_modifications_spec.rb
208
+ - spec/activerecord-multi-tenant/schema_dumper_tester.rb
207
209
  - spec/activerecord-multi-tenant/sidekiq_spec.rb
208
210
  - spec/database.yml
209
211
  - spec/schema.rb