activerecord-multi-tenant 1.0.1 → 1.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (50) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +2 -0
  3. data/.travis.yml +8 -14
  4. data/Appraisals +28 -20
  5. data/CHANGELOG.md +29 -0
  6. data/Gemfile.lock +85 -67
  7. data/README.md +1 -1
  8. data/activerecord-multi-tenant.gemspec +1 -1
  9. data/gemfiles/.bundle/config +2 -0
  10. data/gemfiles/active_record_5.2.gemfile +10 -2
  11. data/gemfiles/active_record_5.2.gemfile.lock +104 -98
  12. data/gemfiles/{active_record_5.1.gemfile → active_record_6.0.gemfile} +2 -2
  13. data/gemfiles/active_record_6.0.gemfile.lock +198 -0
  14. data/gemfiles/{rails_4.1.gemfile → active_record_6.1.gemfile} +2 -2
  15. data/gemfiles/active_record_6.1.gemfile.lock +198 -0
  16. data/gemfiles/rails_5.2.gemfile +10 -2
  17. data/gemfiles/rails_5.2.gemfile.lock +109 -103
  18. data/gemfiles/{rails_5.0.gemfile → rails_6.0.gemfile} +2 -2
  19. data/gemfiles/rails_6.0.gemfile.lock +198 -0
  20. data/gemfiles/{rails_5.1.gemfile → rails_6.1.gemfile} +2 -2
  21. data/gemfiles/rails_6.1.gemfile.lock +198 -0
  22. data/lib/activerecord-multi-tenant.rb +1 -0
  23. data/lib/activerecord-multi-tenant/arel_visitors_depth_first.rb +200 -0
  24. data/lib/activerecord-multi-tenant/controller_extensions.rb +2 -6
  25. data/lib/activerecord-multi-tenant/copy_from_client.rb +2 -2
  26. data/lib/activerecord-multi-tenant/migrations.rb +2 -2
  27. data/lib/activerecord-multi-tenant/model_extensions.rb +13 -14
  28. data/lib/activerecord-multi-tenant/multi_tenant.rb +9 -0
  29. data/lib/activerecord-multi-tenant/persistence_extension.rb +13 -0
  30. data/lib/activerecord-multi-tenant/query_rewriter.rb +60 -104
  31. data/lib/activerecord-multi-tenant/sidekiq.rb +2 -1
  32. data/lib/activerecord-multi-tenant/version.rb +1 -1
  33. data/spec/activerecord-multi-tenant/controller_extensions_spec.rb +19 -24
  34. data/spec/activerecord-multi-tenant/model_extensions_spec.rb +54 -104
  35. data/spec/activerecord-multi-tenant/query_rewriter_spec.rb +8 -0
  36. data/spec/activerecord-multi-tenant/record_finding_spec.rb +36 -0
  37. data/spec/activerecord-multi-tenant/record_modifications_spec.rb +60 -3
  38. data/spec/activerecord-multi-tenant/schema_dumper_tester.rb +0 -0
  39. data/spec/activerecord-multi-tenant/sidekiq_spec.rb +4 -4
  40. data/spec/schema.rb +1 -4
  41. data/spec/spec_helper.rb +1 -6
  42. metadata +17 -17
  43. data/gemfiles/active_record_5.1.gemfile.lock +0 -173
  44. data/gemfiles/rails_4.0.gemfile +0 -8
  45. data/gemfiles/rails_4.0.gemfile.lock +0 -141
  46. data/gemfiles/rails_4.1.gemfile.lock +0 -146
  47. data/gemfiles/rails_4.2.gemfile +0 -8
  48. data/gemfiles/rails_4.2.gemfile.lock +0 -169
  49. data/gemfiles/rails_5.0.gemfile.lock +0 -175
  50. data/gemfiles/rails_5.1.gemfile.lock +0 -175
@@ -348,40 +348,24 @@ 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 == 4 && ActiveRecord::VERSION::MINOR < 2
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 "sub_tasks"."account_id" = 1 AND "tasks"."project_id" = $1
354
- sql
355
- elsif uses_prepared_statements? && ActiveRecord::VERSION::MAJOR == 5 && ActiveRecord::VERSION::MINOR == 0
356
- <<-sql
357
- 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
358
- sql
359
- else
360
- <<-sql
351
+ expected_sql = <<-sql
361
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
362
353
  sql
363
- end
354
+
364
355
  account1 = Account.create! name: 'Account 1'
365
356
 
366
357
  MultiTenant.with(account1) do
367
358
  project1 = Project.create! name: 'Project 1'
368
359
  task1 = Task.create! name: 'Task 1', project: project1
369
360
  subtask1 = SubTask.create! task: task1
370
-
371
361
  expect(project1.sub_tasks.to_sql).to eq(expected_sql.strip)
372
362
  expect(project1.sub_tasks).to include(subtask1)
373
363
  end
374
364
 
375
365
  MultiTenant.without do
376
- expected_sql = if uses_prepared_statements? && ActiveRecord::VERSION::MAJOR == 4 && ActiveRecord::VERSION::MINOR < 2
377
- <<-sql
378
- 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"."project_id" = $1
379
- sql
380
- else
381
- <<-sql
382
- 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"."project_id" = 1
366
+ expected_sql = <<-sql
367
+ 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"."project_id" = 1
383
368
  sql
384
- end
385
369
 
386
370
  project = Project.first
387
371
  expect(project.sub_tasks.to_sql).to eq(expected_sql.strip)
@@ -389,43 +373,33 @@ describe MultiTenant do
389
373
  end
390
374
 
391
375
  it "tests joins between distributed and reference table" do
392
- expected_sql = if uses_prepared_statements? && ActiveRecord::VERSION::MAJOR == 4 && ActiveRecord::VERSION::MINOR < 2
393
- <<-sql
394
- SELECT "categories".* FROM "categories" INNER JOIN "project_categories" ON "categories"."id" = "project_categories"."category_id" WHERE "project_categories"."account_id" = 1 AND "project_categories"."project_id" = $1
395
- sql
396
- else
397
- <<-sql
376
+ expected_sql = <<-sql
398
377
  SELECT "categories".* FROM "categories" INNER JOIN "project_categories" ON "categories"."id" = "project_categories"."category_id" WHERE "project_categories"."account_id" = 1 AND "project_categories"."project_id" = 1
399
- sql
400
- end
378
+ sql
401
379
  account1 = Account.create! name: 'Account 1'
402
380
  category1 = Category.create! name: 'Category 1'
403
381
 
404
382
  MultiTenant.with(account1) do
405
383
  project1 = Project.create! name: 'Project 1'
406
384
  projectcategory = ProjectCategory.create! name: 'project cat 1', project: project1, category: category1
385
+
407
386
  expect(project1.categories.to_sql).to eq(expected_sql.strip)
408
387
  expect(project1.categories).to include(category1)
409
388
  expect(project1.project_categories).to include(projectcategory)
410
389
  end
411
390
 
412
391
  MultiTenant.without do
413
- expected_sql = if uses_prepared_statements? && ActiveRecord::VERSION::MAJOR == 4 && ActiveRecord::VERSION::MINOR < 2
414
- <<-sql
415
- SELECT "categories".* FROM "categories" INNER JOIN "project_categories" ON "categories"."id" = "project_categories"."category_id" WHERE "project_categories"."project_id" = $1
416
- sql
417
- else
418
- <<-sql
392
+ expected_sql = <<-sql
419
393
  SELECT "categories".* FROM "categories" INNER JOIN "project_categories" ON "categories"."id" = "project_categories"."category_id" WHERE "project_categories"."project_id" = 1
420
394
  sql
421
- end
395
+
422
396
  project = Project.first
423
397
  expect(project.categories.to_sql).to eq(expected_sql.strip)
424
398
  expect(project.categories).to include(category1)
425
399
 
426
400
  expected_sql = <<-sql
427
401
  SELECT "projects".* FROM "projects" INNER JOIN "project_categories" ON "project_categories"."project_id" = "projects"."id" AND "project_categories"."account_id" = "projects"."account_id" INNER JOIN "categories" ON "categories"."id" = "project_categories"."category_id" WHERE "projects"."account_id" = 1
428
- sql
402
+ sql
429
403
 
430
404
  expect(Project.where(account_id: 1).joins(:categories).to_sql).to eq(expected_sql.strip)
431
405
  project = Project.where(account_id: 1).joins(:categories).first
@@ -438,17 +412,13 @@ describe MultiTenant do
438
412
  account1 = Account.create! name: 'Account 1'
439
413
  category1 = Category.create! name: 'Category 1'
440
414
 
441
- 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::MAJOR == 6 && ActiveRecord::VERSION::MINOR >= 1))
442
416
  <<-sql
443
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
444
418
  sql
445
- elsif uses_prepared_statements? && ActiveRecord::VERSION::MAJOR == 4 && ActiveRecord::VERSION::MINOR == 0
446
- <<-sql
447
- SELECT "projects".* FROM "projects" WHERE "projects"."account_id" = 1
448
- sql
449
419
  else
450
420
  <<-sql
451
- 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" = "projects"."account_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
421
+ 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"."account_id" = 1 AND "project_categories"."project_id" = "projects"."id" 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
452
422
  sql
453
423
  end
454
424
 
@@ -464,15 +434,9 @@ describe MultiTenant do
464
434
  end
465
435
 
466
436
  MultiTenant.without do
467
- expected_sql = if uses_prepared_statements? && ActiveRecord::VERSION::MAJOR == 4 && ActiveRecord::VERSION::MINOR == 0
468
- <<-sql
469
- SELECT "projects".* FROM "projects" WHERE "projects"."account_id" = 1
470
- sql
471
- else
472
- <<-sql
437
+ expected_sql = <<-sql
473
438
  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" = "projects"."account_id" LEFT OUTER JOIN "categories" ON "categories"."id" = "project_categories"."category_id" WHERE "projects"."account_id" = 1
474
- sql
475
- end
439
+ sql
476
440
 
477
441
  expect(Project.where(account_id: 1).eager_load(:categories).to_sql).to eq(expected_sql.strip)
478
442
 
@@ -487,13 +451,13 @@ describe MultiTenant do
487
451
  category1 = Category.create! name: 'Category 1'
488
452
 
489
453
  MultiTenant.with(account1) do
490
- expected_sql = if uses_prepared_statements? && ActiveRecord::VERSION::MAJOR == 5 && ActiveRecord::VERSION::MINOR > 1
454
+ expected_sql = if uses_prepared_statements? && (ActiveRecord::VERSION::MAJOR == 5 || (ActiveRecord::VERSION::MAJOR == 6 && ActiveRecord::VERSION::MINOR >= 1))
491
455
  <<-sql
492
456
  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
493
457
  sql
494
458
  else
495
459
  <<-sql
496
- SELECT "tasks".* FROM "tasks" INNER JOIN "projects" ON "projects"."id" = "tasks"."project_id" AND "projects"."account_id" = "tasks"."account_id" LEFT JOIN project_categories pc ON project.category_id = pc.id WHERE "projects"."account_id" = 1 AND "tasks"."account_id" = 1
460
+ SELECT "tasks".* FROM "tasks" INNER JOIN "projects" ON "projects"."account_id" = 1 AND "projects"."id" = "tasks"."project_id" LEFT JOIN project_categories pc ON project.category_id = pc.id WHERE "tasks"."account_id" = 1
497
461
  sql
498
462
  end
499
463
 
@@ -501,7 +465,6 @@ describe MultiTenant do
501
465
  projectcategory = ProjectCategory.create! name: 'project cat 1', project: project1, category: category1
502
466
 
503
467
  project1.tasks.create! name: 'baz'
504
-
505
468
  expect(Task.joins(:project).joins('LEFT JOIN project_categories pc ON project.category_id = pc.id').to_sql).to eq(expected_sql.strip)
506
469
  end
507
470
 
@@ -509,73 +472,60 @@ describe MultiTenant do
509
472
  expected_sql = <<-sql
510
473
  SELECT "tasks".* FROM "tasks" INNER JOIN "projects" ON "projects"."id" = "tasks"."project_id" AND "projects"."account_id" = "tasks"."account_id" LEFT JOIN project_categories pc ON project.category_id = pc.id WHERE "tasks"."account_id" = 1
511
474
  sql
475
+
512
476
  expect(Task.where(account_id: 1).joins(:project).joins('LEFT JOIN project_categories pc ON project.category_id = pc.id').to_sql).to eq(expected_sql.strip)
513
477
 
514
478
  end
515
479
 
516
480
  end
517
481
 
482
+ it "only applies clauses when a tenant is set" do
483
+ account = Account.create! name: 'Account 1'
484
+ project = Project.create! name: 'Project 1', account: account
485
+ project2 = Project.create! name: 'Project 2', account: Account.create!(name: 'Account2')
518
486
 
519
-
520
- # Versions earlier than 4.2 pass an arel object to find_by_sql(...) and it would make
521
- # this test unnecesssarily complicated to support that
522
- if ActiveRecord::VERSION::MAJOR > 4 || (ActiveRecord::VERSION::MAJOR == 4 && ActiveRecord::VERSION::MINOR >= 2)
523
- it "only applies clauses when a tenant is set" do
524
- account = Account.create! name: 'Account 1'
525
- project = Project.create! name: 'Project 1', account: account
526
- project2 = Project.create! name: 'Project 2', account: Account.create!(name: 'Account2')
527
-
528
- MultiTenant.with(account) do
529
- expected_sql = if uses_prepared_statements? && ActiveRecord::VERSION::MAJOR > 4
530
- <<-sql.strip
531
- SELECT "projects".* FROM "projects" WHERE "projects"."account_id" = #{account.id} AND "projects"."id" = $1 LIMIT $2
487
+ MultiTenant.with(account) do
488
+ expected_sql = if uses_prepared_statements? && ActiveRecord::VERSION::MAJOR > 5
489
+ <<-sql.strip
490
+ SELECT "projects".* FROM "projects" WHERE "projects"."account_id" = #{account.id} AND "projects"."id" = $1 LIMIT $2
532
491
  sql
533
- elsif uses_prepared_statements? && ActiveRecord::VERSION::MAJOR == 4
534
- <<-sql.strip
535
- SELECT "projects".* FROM "projects" WHERE "projects"."account_id" = #{account.id} AND "projects"."id" = $1 LIMIT 1
536
- sql
537
- else
538
- <<-sql.strip
539
- SELECT "projects".* FROM "projects" WHERE "projects"."account_id" = #{account.id} AND "projects"."id" = #{project.id} LIMIT 1
492
+ else
493
+ <<-sql.strip
494
+ SELECT "projects".* FROM "projects" WHERE "projects"."account_id" = #{account.id} AND "projects"."id" = $1 LIMIT $2
540
495
  sql
541
- end
542
- expect(Project).to receive(:find_by_sql).with(expected_sql, any_args).and_call_original
543
- expect(Project.find(project.id)).to eq(project)
544
- end
496
+ end
545
497
 
546
- MultiTenant.without do
547
- expected_sql = if uses_prepared_statements? && ActiveRecord::VERSION::MAJOR > 4
548
- <<-sql.strip
549
- SELECT "projects".* FROM "projects" WHERE "projects"."id" = $1 LIMIT $2
550
- sql
551
- elsif uses_prepared_statements? && ActiveRecord::VERSION::MAJOR == 4
552
- <<-sql.strip
553
- SELECT "projects".* FROM "projects" WHERE "projects"."id" = $1 LIMIT 1
498
+ expect(Project).to receive(:find_by_sql).with(expected_sql, any_args).and_call_original
499
+ expect(Project.find(project.id)).to eq(project)
500
+ end
501
+
502
+ MultiTenant.without do
503
+ expected_sql = if uses_prepared_statements? && ActiveRecord::VERSION::MAJOR > 5
504
+ <<-sql.strip
505
+ SELECT "projects".* FROM "projects" WHERE "projects"."id" = $1 LIMIT $2
554
506
  sql
555
- else
556
- <<-sql.strip
557
- SELECT "projects".* FROM "projects" WHERE "projects"."id" = #{project2.id} LIMIT 1
507
+ else
508
+ <<-sql.strip
509
+ SELECT "projects".* FROM "projects" WHERE "projects"."id" = $1 LIMIT $2
558
510
  sql
559
- end
560
- expect(Project).to receive(:find_by_sql).with(expected_sql, any_args).and_call_original
561
- expect(Project.find(project2.id)).to eq(project2)
562
- end
511
+ end
512
+
513
+ expect(Project).to receive(:find_by_sql).with(expected_sql, any_args).and_call_original
514
+ expect(Project.find(project2.id)).to eq(project2)
563
515
  end
564
516
  end
565
517
 
566
- # Versions earlier than 4.1 have a different behaviour regarding unsaved associations
567
- if ActiveRecord::VERSION::MAJOR > 4 || (ActiveRecord::VERSION::MAJOR == 4 && ActiveRecord::VERSION::MINOR >= 1)
568
- describe 'with unsaved association' do
569
- before do
570
- @account = Account.create!(name: 'reflection tenant')
571
- @manager = Manager.new(account: @account)
572
- MultiTenant.current_tenant = @account
573
- @account.update! name: 'reflection tenant update'
574
- end
575
518
 
576
- it 'persists the reflected association' do
577
- expect(@manager.persisted?).to eq(true)
578
- end
519
+ describe 'with unsaved association' do
520
+ before do
521
+ @account = Account.create!(name: 'reflection tenant')
522
+ @manager = Manager.new(account: @account)
523
+ MultiTenant.current_tenant = @account
524
+ @account.update! name: 'reflection tenant update'
525
+ end
526
+
527
+ it 'persists the reflected association' do
528
+ expect(@manager.persisted?).to eq(true)
579
529
  end
580
530
  end
581
531
 
@@ -90,4 +90,12 @@ describe "Query Rewriter" do
90
90
  }.to change { Project.count }.from(3).to(1)
91
91
  end
92
92
  end
93
+
94
+ context "when update without arel" do
95
+ it "can call method" do
96
+ expect {
97
+ ActiveRecord::Base.connection.update("SELECT 1")
98
+ }.not_to raise_error
99
+ end
100
+ end
93
101
  end
@@ -59,4 +59,40 @@ describe MultiTenant, 'Record finding' do
59
59
  expect(second_found).to eq(second_record)
60
60
  end
61
61
  end
62
+
63
+ context 'model with has_many relation through multi-tenant model' do
64
+ let(:tenant_1) { Account.create! name: 'Tenant 1' }
65
+ let(:project_1) { tenant_1.projects.create! }
66
+
67
+ let(:tenant_2) { Account.create! name: 'Tenant 2' }
68
+ let(:project_2) { tenant_2.projects.create! }
69
+
70
+ let(:category) { Category.create! name: 'Category' }
71
+
72
+ before do
73
+ ProjectCategory.create! account: tenant_1, name: '1', project: project_1, category: category
74
+ ProjectCategory.create! account: tenant_2, name: '2', project: project_2, category: category
75
+ end
76
+
77
+ it 'can get model without creating query cache' do
78
+ MultiTenant.with(tenant_1) do
79
+ found_category = Project.find(project_1.id).categories.to_a.first
80
+ expect(found_category).to eq(category)
81
+ end
82
+ end
83
+
84
+ it 'can get model for other tenant' do
85
+ MultiTenant.with(tenant_2) do
86
+ found_category = Project.find(project_2.id).categories.to_a.first
87
+ expect(found_category).to eq(category)
88
+ end
89
+ end
90
+
91
+ it 'can get model without current_tenant' do
92
+ MultiTenant.without do
93
+ found_category = Project.find(project_2.id).categories.to_a.first
94
+ expect(found_category).to eq(category)
95
+ end
96
+ end
97
+ end
62
98
  end
@@ -2,13 +2,70 @@ 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
6
24
 
7
- it 'includes the tenant_id in DELETEs' do
8
- project.destroy
9
25
  MultiTenant.with(account) do
10
26
  expect(Project.where(id: project.id).first).not_to be_present
11
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
48
+
49
+ MultiTenant.with(account) do
50
+ expect(Project.where(id: project.id).first).not_to be_present
51
+ end
52
+ MultiTenant.with(account2) do
53
+ expect(Project.where(id: project2.id).first).to be_present
54
+ end
55
+ end
56
+
57
+ it 'test delete for reference tables' do
58
+ category1 = Category.create! name: 'Category 1'
59
+ expect(Category.count).to eq(1)
60
+ category1.delete
61
+ expect(Category.count).to eq(0)
62
+ end
63
+
64
+ it 'test delete for non distributed tables' do
65
+ unscoped = UnscopedModel.create! name: 'Canada'
66
+ expect(UnscopedModel.count).to eq(1)
67
+ unscoped.delete
68
+ expect(UnscopedModel.count).to eq(0)
12
69
  end
13
70
 
14
71
  it 'includes the tenant_id in UPDATEs' do
@@ -4,14 +4,14 @@ require 'activerecord-multi-tenant/sidekiq'
4
4
 
5
5
  describe MultiTenant, 'Sidekiq' do
6
6
  let(:server) { Sidekiq::Middleware::MultiTenant::Server.new }
7
+ let(:account) { Account.create(name: 'test') }
7
8
 
8
9
  describe 'server middleware' do
9
10
  it 'sets the multitenant context when provided in message' do
10
- tenant_id = 1234
11
- server.call(double, {'bogus' => 'message',
12
- 'multi_tenant' => { 'class' => MultiTenant.current_tenant_class, 'id' => tenant_id}},
11
+ server.call(double,{'bogus' => 'message',
12
+ 'multi_tenant' => { 'class' => account.class.name, 'id' => account.id}},
13
13
  'bogus_queue') do
14
- expect(MultiTenant.current_tenant).to eq(tenant_id)
14
+ expect(MultiTenant.current_tenant).to eq(account)
15
15
  end
16
16
  end
17
17
 
@@ -181,10 +181,7 @@ end
181
181
  class Comment < ActiveRecord::Base
182
182
  multi_tenant :account
183
183
  belongs_to :commentable, polymorphic: true
184
-
185
- if ActiveRecord::VERSION::MAJOR >= 4
186
- belongs_to :task, -> { where(comments: { commentable_type: 'Task' }) }, foreign_key: 'commentable_id'
187
- end
184
+ belongs_to :task, -> { where(comments: { commentable_type: 'Task' }) }, foreign_key: 'commentable_id'
188
185
  end
189
186
 
190
187
  class Organization < ActiveRecord::Base
@@ -43,12 +43,7 @@ MultiTenantTest::Application.config.secret_token = 'x' * 40
43
43
  MultiTenantTest::Application.config.secret_key_base = 'y' * 40
44
44
 
45
45
  def uses_prepared_statements?
46
- if ActiveRecord::VERSION::MAJOR == 4
47
- config = ActiveRecord::Base.connection.instance_variable_get(:@config)
48
- config.fetch(:prepared_statements, true)
49
- else
50
- ActiveRecord::Base.connection.prepared_statements
51
- end
46
+ ActiveRecord::Base.connection.prepared_statements
52
47
  end
53
48
 
54
49
  require 'schema'
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.1
4
+ version: 1.1.1
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-08-27 00:00:00.000000000 Z
11
+ date: 2021-01-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: request_store
@@ -30,14 +30,14 @@ dependencies:
30
30
  requirements:
31
31
  - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: '4.0'
33
+ version: '4.2'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
- version: '4.0'
40
+ version: '4.2'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rspec
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -167,29 +167,28 @@ files:
167
167
  - Rakefile
168
168
  - activerecord-multi-tenant.gemspec
169
169
  - docker-compose.yml
170
- - gemfiles/active_record_5.1.gemfile
171
- - gemfiles/active_record_5.1.gemfile.lock
170
+ - gemfiles/.bundle/config
172
171
  - gemfiles/active_record_5.2.gemfile
173
172
  - gemfiles/active_record_5.2.gemfile.lock
174
- - gemfiles/rails_4.0.gemfile
175
- - gemfiles/rails_4.0.gemfile.lock
176
- - gemfiles/rails_4.1.gemfile
177
- - gemfiles/rails_4.1.gemfile.lock
178
- - gemfiles/rails_4.2.gemfile
179
- - gemfiles/rails_4.2.gemfile.lock
180
- - gemfiles/rails_5.0.gemfile
181
- - gemfiles/rails_5.0.gemfile.lock
182
- - gemfiles/rails_5.1.gemfile
183
- - gemfiles/rails_5.1.gemfile.lock
173
+ - gemfiles/active_record_6.0.gemfile
174
+ - gemfiles/active_record_6.0.gemfile.lock
175
+ - gemfiles/active_record_6.1.gemfile
176
+ - gemfiles/active_record_6.1.gemfile.lock
184
177
  - gemfiles/rails_5.2.gemfile
185
178
  - gemfiles/rails_5.2.gemfile.lock
179
+ - gemfiles/rails_6.0.gemfile
180
+ - gemfiles/rails_6.0.gemfile.lock
181
+ - gemfiles/rails_6.1.gemfile
182
+ - gemfiles/rails_6.1.gemfile.lock
186
183
  - lib/activerecord-multi-tenant.rb
184
+ - lib/activerecord-multi-tenant/arel_visitors_depth_first.rb
187
185
  - lib/activerecord-multi-tenant/controller_extensions.rb
188
186
  - lib/activerecord-multi-tenant/copy_from_client.rb
189
187
  - lib/activerecord-multi-tenant/fast_truncate.rb
190
188
  - lib/activerecord-multi-tenant/migrations.rb
191
189
  - lib/activerecord-multi-tenant/model_extensions.rb
192
190
  - lib/activerecord-multi-tenant/multi_tenant.rb
191
+ - lib/activerecord-multi-tenant/persistence_extension.rb
193
192
  - lib/activerecord-multi-tenant/query_monitor.rb
194
193
  - lib/activerecord-multi-tenant/query_rewriter.rb
195
194
  - lib/activerecord-multi-tenant/sidekiq.rb
@@ -203,6 +202,7 @@ files:
203
202
  - spec/activerecord-multi-tenant/record_callback_spec.rb
204
203
  - spec/activerecord-multi-tenant/record_finding_spec.rb
205
204
  - spec/activerecord-multi-tenant/record_modifications_spec.rb
205
+ - spec/activerecord-multi-tenant/schema_dumper_tester.rb
206
206
  - spec/activerecord-multi-tenant/sidekiq_spec.rb
207
207
  - spec/database.yml
208
208
  - spec/schema.rb
@@ -226,7 +226,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
226
226
  - !ruby/object:Gem::Version
227
227
  version: '0'
228
228
  requirements: []
229
- rubygems_version: 3.0.3
229
+ rubygems_version: 3.1.2
230
230
  signing_key:
231
231
  specification_version: 4
232
232
  summary: ActiveRecord/Rails integration for multi-tenant databases, in particular