activerecord-multi-tenant 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +2 -0
  3. data/.travis.yml +6 -14
  4. data/Appraisals +24 -24
  5. data/CHANGELOG.md +22 -0
  6. data/Gemfile.lock +79 -63
  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 +102 -96
  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_5.2.gemfile +10 -2
  15. data/gemfiles/rails_5.2.gemfile.lock +109 -103
  16. data/gemfiles/{rails_5.0.gemfile → rails_6.0.gemfile} +2 -2
  17. data/gemfiles/rails_6.0.gemfile.lock +198 -0
  18. data/lib/activerecord-multi-tenant.rb +1 -0
  19. data/lib/activerecord-multi-tenant/controller_extensions.rb +2 -6
  20. data/lib/activerecord-multi-tenant/copy_from_client.rb +2 -2
  21. data/lib/activerecord-multi-tenant/migrations.rb +2 -2
  22. data/lib/activerecord-multi-tenant/model_extensions.rb +8 -15
  23. data/lib/activerecord-multi-tenant/multi_tenant.rb +9 -0
  24. data/lib/activerecord-multi-tenant/persistence_extension.rb +13 -0
  25. data/lib/activerecord-multi-tenant/query_rewriter.rb +59 -105
  26. data/lib/activerecord-multi-tenant/sidekiq.rb +2 -1
  27. data/lib/activerecord-multi-tenant/version.rb +1 -1
  28. data/spec/activerecord-multi-tenant/controller_extensions_spec.rb +19 -24
  29. data/spec/activerecord-multi-tenant/model_extensions_spec.rb +54 -104
  30. data/spec/activerecord-multi-tenant/query_rewriter_spec.rb +40 -0
  31. data/spec/activerecord-multi-tenant/record_modifications_spec.rb +60 -3
  32. data/spec/activerecord-multi-tenant/schema_dumper_tester.rb +0 -0
  33. data/spec/activerecord-multi-tenant/sidekiq_spec.rb +4 -4
  34. data/spec/schema.rb +1 -4
  35. data/spec/spec_helper.rb +1 -6
  36. metadata +15 -20
  37. data/gemfiles/active_record_5.1.gemfile.lock +0 -173
  38. data/gemfiles/rails_4.0.gemfile +0 -8
  39. data/gemfiles/rails_4.0.gemfile.lock +0 -141
  40. data/gemfiles/rails_4.1.gemfile +0 -8
  41. data/gemfiles/rails_4.1.gemfile.lock +0 -146
  42. data/gemfiles/rails_4.2.gemfile +0 -8
  43. data/gemfiles/rails_4.2.gemfile.lock +0 -169
  44. data/gemfiles/rails_5.0.gemfile.lock +0 -175
  45. data/gemfiles/rails_5.1.gemfile +0 -8
  46. data/gemfiles/rails_5.1.gemfile.lock +0 -175
@@ -18,7 +18,8 @@ module Sidekiq::Middleware::MultiTenant
18
18
  class Server
19
19
  def call(worker_class, msg, queue)
20
20
  if msg.has_key?('multi_tenant')
21
- MultiTenant.with(msg['multi_tenant']['id']) do
21
+ tenant = msg['multi_tenant']['class'].constantize.find(msg['multi_tenant']['id'])
22
+ MultiTenant.with(tenant) do
22
23
  yield
23
24
  end
24
25
  else
@@ -1,3 +1,3 @@
1
1
  module MultiTenant
2
- VERSION = '1.0.0'
2
+ VERSION = '1.1.0'
3
3
  end
@@ -21,11 +21,7 @@ describe "Controller Extensions", type: :controller do
21
21
  describe ApplicationController, type: :controller do
22
22
  controller do
23
23
  def index
24
- if ActionPack::VERSION::MAJOR >= 5
25
- render body: 'custom called'
26
- else
27
- render text: 'custom called'
28
- end
24
+ render body: 'custom called'
29
25
  end
30
26
  end
31
27
 
@@ -35,30 +31,29 @@ describe "Controller Extensions", type: :controller do
35
31
  end
36
32
  end
37
33
 
38
- if ActionPack::VERSION::MAJOR >= 5
39
- class APIApplicationController < ActionController::API
40
- include Rails.application.routes.url_helpers
41
- set_current_tenant_through_filter
42
- before_action :your_method_that_finds_the_current_tenant
43
34
 
44
- def your_method_that_finds_the_current_tenant
45
- current_account = Account.new
46
- current_account.name = 'account1'
47
- set_current_tenant(current_account)
48
- end
35
+ class APIApplicationController < ActionController::API
36
+ include Rails.application.routes.url_helpers
37
+ set_current_tenant_through_filter
38
+ before_action :your_method_that_finds_the_current_tenant
39
+
40
+ def your_method_that_finds_the_current_tenant
41
+ current_account = Account.new
42
+ current_account.name = 'account1'
43
+ set_current_tenant(current_account)
49
44
  end
45
+ end
50
46
 
51
- describe APIApplicationController, type: :controller do
52
- controller do
53
- def index
54
- render body: 'custom called'
55
- end
47
+ describe APIApplicationController, type: :controller do
48
+ controller do
49
+ def index
50
+ render body: 'custom called'
56
51
  end
52
+ end
57
53
 
58
- it 'Finds the correct tenant using the filter command' do
59
- get :index
60
- expect(MultiTenant.current_tenant.name).to eq 'account1'
61
- end
54
+ it 'Finds the correct tenant using the filter command' do
55
+ get :index
56
+ expect(MultiTenant.current_tenant.name).to eq 'account1'
62
57
  end
63
58
  end
64
59
  end
@@ -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
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
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
 
@@ -15,6 +15,12 @@ describe "Query Rewriter" do
15
15
  }.to change { project.reload.name }.from("Project 1").to("New Name")
16
16
  end
17
17
 
18
+ it "updates the records without a current tenant" do
19
+ expect {
20
+ Project.joins(:manager).update_all(name: "New Name")
21
+ }.to change { project.reload.name }.from("Project 1").to("New Name")
22
+ end
23
+
18
24
  it "update the record" do
19
25
  expect {
20
26
  MultiTenant.with(account) do
@@ -22,6 +28,12 @@ describe "Query Rewriter" do
22
28
  end
23
29
  }.to change { project.reload.name }.from("Project 1").to("New Name")
24
30
  end
31
+
32
+ it "update the record without a current tenant" do
33
+ expect {
34
+ project.update(name: "New Name")
35
+ }.to change { project.reload.name }.from("Project 1").to("New Name")
36
+ end
25
37
  end
26
38
 
27
39
  context "when bulk deleting" do
@@ -40,6 +52,12 @@ describe "Query Rewriter" do
40
52
  }.to change { Project.count }.from(3).to(1)
41
53
  end
42
54
 
55
+ it "delete_all the records without a current tenant" do
56
+ expect {
57
+ Project.joins(:manager).delete_all
58
+ }.to change { Project.count }.from(3).to(1)
59
+ end
60
+
43
61
  it "delete the record" do
44
62
  expect {
45
63
  MultiTenant.with(account) do
@@ -49,6 +67,13 @@ describe "Query Rewriter" do
49
67
  }.to change { Project.count }.from(3).to(1)
50
68
  end
51
69
 
70
+ it "delete the record without a current tenant" do
71
+ expect {
72
+ project1.delete
73
+ Project.delete(project2.id)
74
+ }.to change { Project.count }.from(3).to(1)
75
+ end
76
+
52
77
  it "destroy the record" do
53
78
  expect {
54
79
  MultiTenant.with(account) do
@@ -57,5 +82,20 @@ describe "Query Rewriter" do
57
82
  end
58
83
  }.to change { Project.count }.from(3).to(1)
59
84
  end
85
+
86
+ it "destroy the record without a current tenant" do
87
+ expect {
88
+ project1.destroy
89
+ Project.destroy(project2.id)
90
+ }.to change { Project.count }.from(3).to(1)
91
+ end
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
60
100
  end
61
101
  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