ros-apartment 2.3.0 → 2.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (153) hide show
  1. checksums.yaml +4 -4
  2. data/.pryrc +5 -3
  3. data/.rubocop.yml +22 -0
  4. data/.rubocop_todo.yml +29 -0
  5. data/.story_branch.yml +5 -0
  6. data/.travis.yml +20 -36
  7. data/Appraisals +16 -29
  8. data/Gemfile +5 -2
  9. data/Guardfile +3 -1
  10. data/HISTORY.md +57 -0
  11. data/README.md +64 -21
  12. data/Rakefile +36 -22
  13. data/TODO.md +0 -1
  14. data/apartment.gemspec +17 -10
  15. data/gemfiles/rails_4_2.gemfile +12 -10
  16. data/gemfiles/rails_5_0.gemfile +2 -1
  17. data/gemfiles/rails_5_1.gemfile +2 -1
  18. data/gemfiles/rails_5_2.gemfile +2 -1
  19. data/gemfiles/rails_6_0.gemfile +6 -5
  20. data/gemfiles/rails_master.gemfile +2 -1
  21. data/lib/apartment.rb +38 -14
  22. data/lib/apartment/active_record/connection_handling.rb +17 -0
  23. data/lib/apartment/active_record/internal_metadata.rb +11 -0
  24. data/lib/apartment/active_record/log_subscriber.rb +41 -0
  25. data/lib/apartment/active_record/schema_migration.rb +13 -0
  26. data/lib/apartment/adapters/abstract_adapter.rb +49 -45
  27. data/lib/apartment/adapters/abstract_jdbc_adapter.rb +4 -3
  28. data/lib/apartment/adapters/jdbc_mysql_adapter.rb +3 -3
  29. data/lib/apartment/adapters/jdbc_postgresql_adapter.rb +20 -13
  30. data/lib/apartment/adapters/mysql2_adapter.rb +12 -9
  31. data/lib/apartment/adapters/postgis_adapter.rb +3 -2
  32. data/lib/apartment/adapters/postgresql_adapter.rb +59 -27
  33. data/lib/apartment/adapters/sqlite3_adapter.rb +18 -8
  34. data/lib/apartment/console.rb +35 -3
  35. data/lib/apartment/custom_console.rb +42 -0
  36. data/lib/apartment/deprecation.rb +2 -1
  37. data/lib/apartment/elevators/domain.rb +4 -3
  38. data/lib/apartment/elevators/first_subdomain.rb +3 -2
  39. data/lib/apartment/elevators/generic.rb +4 -3
  40. data/lib/apartment/elevators/host.rb +6 -1
  41. data/lib/apartment/elevators/host_hash.rb +6 -2
  42. data/lib/apartment/elevators/subdomain.rb +9 -5
  43. data/lib/apartment/migrator.rb +4 -3
  44. data/lib/apartment/model.rb +27 -0
  45. data/lib/apartment/railtie.rb +27 -15
  46. data/lib/apartment/reloader.rb +2 -1
  47. data/lib/apartment/tasks/enhancements.rb +4 -6
  48. data/lib/apartment/tasks/task_helper.rb +35 -0
  49. data/lib/apartment/tenant.rb +19 -9
  50. data/lib/apartment/version.rb +3 -1
  51. data/lib/generators/apartment/install/install_generator.rb +4 -3
  52. data/lib/generators/apartment/install/templates/apartment.rb +8 -2
  53. data/lib/tasks/apartment.rake +22 -44
  54. metadata +51 -230
  55. data/spec/adapters/jdbc_mysql_adapter_spec.rb +0 -19
  56. data/spec/adapters/jdbc_postgresql_adapter_spec.rb +0 -41
  57. data/spec/adapters/mysql2_adapter_spec.rb +0 -59
  58. data/spec/adapters/postgresql_adapter_spec.rb +0 -61
  59. data/spec/adapters/sqlite3_adapter_spec.rb +0 -83
  60. data/spec/apartment_spec.rb +0 -11
  61. data/spec/config/database.yml.sample +0 -49
  62. data/spec/dummy/Rakefile +0 -7
  63. data/spec/dummy/app/controllers/application_controller.rb +0 -6
  64. data/spec/dummy/app/helpers/application_helper.rb +0 -2
  65. data/spec/dummy/app/models/company.rb +0 -3
  66. data/spec/dummy/app/models/user.rb +0 -3
  67. data/spec/dummy/app/views/application/index.html.erb +0 -1
  68. data/spec/dummy/app/views/layouts/application.html.erb +0 -14
  69. data/spec/dummy/config.ru +0 -4
  70. data/spec/dummy/config/application.rb +0 -49
  71. data/spec/dummy/config/boot.rb +0 -11
  72. data/spec/dummy/config/database.yml.sample +0 -44
  73. data/spec/dummy/config/environment.rb +0 -5
  74. data/spec/dummy/config/environments/development.rb +0 -28
  75. data/spec/dummy/config/environments/production.rb +0 -51
  76. data/spec/dummy/config/environments/test.rb +0 -34
  77. data/spec/dummy/config/initializers/apartment.rb +0 -4
  78. data/spec/dummy/config/initializers/backtrace_silencers.rb +0 -7
  79. data/spec/dummy/config/initializers/inflections.rb +0 -10
  80. data/spec/dummy/config/initializers/mime_types.rb +0 -5
  81. data/spec/dummy/config/initializers/secret_token.rb +0 -7
  82. data/spec/dummy/config/initializers/session_store.rb +0 -8
  83. data/spec/dummy/config/locales/en.yml +0 -5
  84. data/spec/dummy/config/routes.rb +0 -3
  85. data/spec/dummy/db/migrate/20110613152810_create_dummy_models.rb +0 -39
  86. data/spec/dummy/db/migrate/20111202022214_create_table_books.rb +0 -14
  87. data/spec/dummy/db/migrate/20180415260934_create_public_tokens.rb +0 -13
  88. data/spec/dummy/db/schema.rb +0 -55
  89. data/spec/dummy/db/seeds.rb +0 -5
  90. data/spec/dummy/db/seeds/import.rb +0 -5
  91. data/spec/dummy/public/404.html +0 -26
  92. data/spec/dummy/public/422.html +0 -26
  93. data/spec/dummy/public/500.html +0 -26
  94. data/spec/dummy/public/favicon.ico +0 -0
  95. data/spec/dummy/public/stylesheets/.gitkeep +0 -0
  96. data/spec/dummy/script/rails +0 -6
  97. data/spec/dummy_engine/.gitignore +0 -8
  98. data/spec/dummy_engine/Gemfile +0 -15
  99. data/spec/dummy_engine/Rakefile +0 -34
  100. data/spec/dummy_engine/bin/rails +0 -12
  101. data/spec/dummy_engine/config/initializers/apartment.rb +0 -51
  102. data/spec/dummy_engine/dummy_engine.gemspec +0 -24
  103. data/spec/dummy_engine/lib/dummy_engine.rb +0 -4
  104. data/spec/dummy_engine/lib/dummy_engine/engine.rb +0 -4
  105. data/spec/dummy_engine/lib/dummy_engine/version.rb +0 -3
  106. data/spec/dummy_engine/test/dummy/Rakefile +0 -6
  107. data/spec/dummy_engine/test/dummy/config.ru +0 -4
  108. data/spec/dummy_engine/test/dummy/config/application.rb +0 -22
  109. data/spec/dummy_engine/test/dummy/config/boot.rb +0 -5
  110. data/spec/dummy_engine/test/dummy/config/database.yml +0 -25
  111. data/spec/dummy_engine/test/dummy/config/environment.rb +0 -5
  112. data/spec/dummy_engine/test/dummy/config/environments/development.rb +0 -37
  113. data/spec/dummy_engine/test/dummy/config/environments/production.rb +0 -78
  114. data/spec/dummy_engine/test/dummy/config/environments/test.rb +0 -39
  115. data/spec/dummy_engine/test/dummy/config/initializers/assets.rb +0 -8
  116. data/spec/dummy_engine/test/dummy/config/initializers/backtrace_silencers.rb +0 -7
  117. data/spec/dummy_engine/test/dummy/config/initializers/cookies_serializer.rb +0 -3
  118. data/spec/dummy_engine/test/dummy/config/initializers/filter_parameter_logging.rb +0 -4
  119. data/spec/dummy_engine/test/dummy/config/initializers/inflections.rb +0 -16
  120. data/spec/dummy_engine/test/dummy/config/initializers/mime_types.rb +0 -4
  121. data/spec/dummy_engine/test/dummy/config/initializers/session_store.rb +0 -3
  122. data/spec/dummy_engine/test/dummy/config/initializers/wrap_parameters.rb +0 -14
  123. data/spec/dummy_engine/test/dummy/config/locales/en.yml +0 -23
  124. data/spec/dummy_engine/test/dummy/config/routes.rb +0 -56
  125. data/spec/dummy_engine/test/dummy/config/secrets.yml +0 -22
  126. data/spec/examples/connection_adapter_examples.rb +0 -42
  127. data/spec/examples/generic_adapter_custom_configuration_example.rb +0 -95
  128. data/spec/examples/generic_adapter_examples.rb +0 -163
  129. data/spec/examples/schema_adapter_examples.rb +0 -234
  130. data/spec/integration/apartment_rake_integration_spec.rb +0 -107
  131. data/spec/integration/query_caching_spec.rb +0 -81
  132. data/spec/integration/use_within_an_engine_spec.rb +0 -28
  133. data/spec/schemas/v1.rb +0 -16
  134. data/spec/schemas/v2.rb +0 -43
  135. data/spec/schemas/v3.rb +0 -49
  136. data/spec/spec_helper.rb +0 -61
  137. data/spec/support/apartment_helpers.rb +0 -43
  138. data/spec/support/capybara_sessions.rb +0 -15
  139. data/spec/support/config.rb +0 -10
  140. data/spec/support/contexts.rb +0 -52
  141. data/spec/support/requirements.rb +0 -35
  142. data/spec/support/setup.rb +0 -46
  143. data/spec/tasks/apartment_rake_spec.rb +0 -129
  144. data/spec/tenant_spec.rb +0 -190
  145. data/spec/unit/config_spec.rb +0 -112
  146. data/spec/unit/elevators/domain_spec.rb +0 -32
  147. data/spec/unit/elevators/first_subdomain_spec.rb +0 -24
  148. data/spec/unit/elevators/generic_spec.rb +0 -54
  149. data/spec/unit/elevators/host_hash_spec.rb +0 -32
  150. data/spec/unit/elevators/host_spec.rb +0 -89
  151. data/spec/unit/elevators/subdomain_spec.rb +0 -76
  152. data/spec/unit/migrator_spec.rb +0 -77
  153. data/spec/unit/reloader_spec.rb +0 -24
data/Rakefile CHANGED
@@ -1,19 +1,25 @@
1
- require 'bundler' rescue 'You must `gem install bundler` and `bundle install` to run rake tasks'
1
+ # frozen_string_literal: true
2
+
3
+ begin
4
+ require 'bundler'
5
+ rescue StandardError
6
+ 'You must `gem install bundler` and `bundle install` to run rake tasks'
7
+ end
2
8
  Bundler.setup
3
9
  Bundler::GemHelper.install_tasks
4
10
 
5
11
  require 'appraisal'
6
12
 
7
- require "rspec"
8
- require "rspec/core/rake_task"
13
+ require 'rspec'
14
+ require 'rspec/core/rake_task'
9
15
 
10
- RSpec::Core::RakeTask.new(:spec => %w{ db:copy_credentials db:test:prepare }) do |spec|
11
- spec.pattern = "spec/**/*_spec.rb"
16
+ RSpec::Core::RakeTask.new(spec: %w[db:copy_credentials db:test:prepare]) do |spec|
17
+ spec.pattern = 'spec/**/*_spec.rb'
12
18
  # spec.rspec_opts = '--order rand:47078'
13
19
  end
14
20
 
15
21
  namespace :spec do
16
- [:tasks, :unit, :adapters, :integration].each do |type|
22
+ %i[tasks unit adapters integration].each do |type|
17
23
  RSpec::Core::RakeTask.new(type => :spec) do |spec|
18
24
  spec.pattern = "spec/#{type}/**/*_spec.rb"
19
25
  end
@@ -27,11 +33,11 @@ task :console do
27
33
  Pry.start
28
34
  end
29
35
 
30
- task :default => :spec
36
+ task default: :spec
31
37
 
32
38
  namespace :db do
33
39
  namespace :test do
34
- task :prepare => %w{postgres:drop_db postgres:build_db mysql:drop_db mysql:build_db}
40
+ task prepare: %w[postgres:drop_db postgres:build_db mysql:drop_db mysql:build_db]
35
41
  end
36
42
 
37
43
  desc "copy sample database credential files over if real files don't exist"
@@ -40,29 +46,33 @@ namespace :db do
40
46
  apartment_db_file = 'spec/config/database.yml'
41
47
  rails_db_file = 'spec/dummy/config/database.yml'
42
48
 
43
- FileUtils.copy(apartment_db_file + '.sample', apartment_db_file, :verbose => true) unless File.exists?(apartment_db_file)
44
- FileUtils.copy(rails_db_file + '.sample', rails_db_file, :verbose => true) unless File.exists?(rails_db_file)
49
+ FileUtils.copy(apartment_db_file + '.sample', apartment_db_file, verbose: true) unless File.exist?(apartment_db_file)
50
+ FileUtils.copy(rails_db_file + '.sample', rails_db_file, verbose: true) unless File.exist?(rails_db_file)
45
51
  end
46
52
  end
47
53
 
48
54
  namespace :postgres do
49
55
  require 'active_record'
50
- require "#{File.join(File.dirname(__FILE__), 'spec', 'support', 'config')}"
56
+ require File.join(File.dirname(__FILE__), 'spec', 'support', 'config').to_s
51
57
 
52
58
  desc 'Build the PostgreSQL test databases'
53
59
  task :build_db do
54
60
  params = []
55
- params << "-E UTF8"
61
+ params << '-E UTF8'
56
62
  params << pg_config['database']
57
63
  params << "-U#{pg_config['username']}"
58
64
  params << "-h#{pg_config['host']}" if pg_config['host']
59
65
  params << "-p#{pg_config['port']}" if pg_config['port']
60
- %x{ createdb #{params.join(' ')} } rescue "test db already exists"
66
+ begin
67
+ `createdb #{params.join(' ')}`
68
+ rescue StandardError
69
+ 'test db already exists'
70
+ end
61
71
  ActiveRecord::Base.establish_connection pg_config
62
72
  migrate
63
73
  end
64
74
 
65
- desc "drop the PostgreSQL test database"
75
+ desc 'drop the PostgreSQL test database'
66
76
  task :drop_db do
67
77
  puts "dropping database #{pg_config['database']}"
68
78
  params = []
@@ -70,14 +80,13 @@ namespace :postgres do
70
80
  params << "-U#{pg_config['username']}"
71
81
  params << "-h#{pg_config['host']}" if pg_config['host']
72
82
  params << "-p#{pg_config['port']}" if pg_config['port']
73
- %x{ dropdb #{params.join(' ')} }
83
+ `dropdb #{params.join(' ')}`
74
84
  end
75
-
76
85
  end
77
86
 
78
87
  namespace :mysql do
79
88
  require 'active_record'
80
- require "#{File.join(File.dirname(__FILE__), 'spec', 'support', 'config')}"
89
+ require File.join(File.dirname(__FILE__), 'spec', 'support', 'config').to_s
81
90
 
82
91
  desc 'Build the MySQL test databases'
83
92
  task :build_db do
@@ -85,24 +94,29 @@ namespace :mysql do
85
94
  params << "-h #{my_config['host']}" if my_config['host']
86
95
  params << "-u #{my_config['username']}" if my_config['username']
87
96
  params << "-p#{my_config['password']}" if my_config['password']
88
- %x{ mysqladmin #{params.join(' ')} create #{my_config['database']} } rescue "test db already exists"
97
+ params << "--port #{my_config['port']}" if my_config['port']
98
+ begin
99
+ `mysqladmin #{params.join(' ')} create #{my_config['database']}`
100
+ rescue StandardError
101
+ 'test db already exists'
102
+ end
89
103
  ActiveRecord::Base.establish_connection my_config
90
104
  migrate
91
105
  end
92
106
 
93
- desc "drop the MySQL test database"
107
+ desc 'drop the MySQL test database'
94
108
  task :drop_db do
95
109
  puts "dropping database #{my_config['database']}"
96
110
  params = []
97
111
  params << "-h #{my_config['host']}" if my_config['host']
98
112
  params << "-u #{my_config['username']}" if my_config['username']
99
113
  params << "-p#{my_config['password']}" if my_config['password']
100
- %x{ mysqladmin #{params.join(' ')} drop #{my_config['database']} --force}
114
+ params << "--port #{my_config['port']}" if my_config['port']
115
+ `mysqladmin #{params.join(' ')} drop #{my_config['database']} --force`
101
116
  end
102
-
103
117
  end
104
118
 
105
- # TODO clean this up
119
+ # TODO: clean this up
106
120
  def config
107
121
  Apartment::Test.config['connections']
108
122
  end
data/TODO.md CHANGED
@@ -46,6 +46,5 @@
46
46
 
47
47
  Quick TODOs
48
48
 
49
- 1. `default_tenant` should be up to the adapter, not the Apartment class, deprecate `default_schema`
50
49
  2. deprecation.rb rescues everything, we have a hard dependency on ActiveSupport so this is unnecessary
51
50
  3.
@@ -11,7 +11,15 @@ Gem::Specification.new do |s|
11
11
  s.summary = 'A Ruby gem for managing database multitenancy. Apartment Gem drop in replacement'
12
12
  s.description = 'Apartment allows Rack applications to deal with database multitenancy through ActiveRecord'
13
13
  s.email = ['ryan@influitive.com', 'brad@influitive.com', 'rui.p.baltazar@gmail.com']
14
- s.files = `git ls-files`.split($/)
14
+ # Specify which files should be added to the gem when it is released.
15
+ # The `git ls-files -z` loads the files in the RubyGem that have been
16
+ # added into git.
17
+ s.files = Dir.chdir(File.expand_path(__dir__)) do
18
+ `git ls-files -z`.split("\x0").reject do |f|
19
+ # NOTE: ignore all test related
20
+ f.match(%r{^(test|spec|features)/})
21
+ end
22
+ end
15
23
  s.executables = s.files.grep(%r{^bin/}).map { |f| File.basename(f) }
16
24
  s.test_files = s.files.grep(%r{^(test|spec|features)/})
17
25
  s.require_paths = ['lib']
@@ -20,13 +28,13 @@ Gem::Specification.new do |s|
20
28
  s.licenses = ['MIT']
21
29
 
22
30
  # must be >= 3.1.2 due to bug in prepared_statements
23
- s.add_dependency 'activerecord', '>= 3.1.2', '< 6.1'
24
- s.add_dependency 'parallel', '>= 0.7.1'
25
- s.add_dependency 'public_suffix', '>= 2'
26
- s.add_dependency 'rack', '>= 1.3.6'
31
+ s.add_dependency 'activerecord', '>= 5.0.0', '< 6.1'
32
+ s.add_dependency 'parallel', '< 2.0'
33
+ s.add_dependency 'public_suffix', '>= 2.0.5', '< 5.0'
34
+ s.add_dependency 'rack', '>= 1.3.6', '< 3.0'
27
35
 
28
- s.add_development_dependency 'appraisal'
29
- s.add_development_dependency 'bundler', '>= 1.3', '< 2.0'
36
+ s.add_development_dependency 'appraisal', '~> 2.2'
37
+ s.add_development_dependency 'bundler', '>= 1.3', '< 3.0'
30
38
  s.add_development_dependency 'capybara', '~> 2.0'
31
39
  s.add_development_dependency 'rake', '~> 0.9'
32
40
  s.add_development_dependency 'rspec', '~> 3.4'
@@ -38,10 +46,9 @@ Gem::Specification.new do |s|
38
46
  s.add_development_dependency 'activerecord-jdbcpostgresql-adapter'
39
47
  s.add_development_dependency 'jdbc-mysql'
40
48
  s.add_development_dependency 'jdbc-postgres'
41
- s.add_development_dependency 'jruby-openssl'
42
49
  else
43
- s.add_development_dependency 'mysql2'
44
- s.add_development_dependency 'pg'
50
+ s.add_development_dependency 'mysql2', '~> 0.5'
51
+ s.add_development_dependency 'pg', '~> 1.2'
45
52
  s.add_development_dependency 'sqlite3', '~> 1.3.6'
46
53
  end
47
54
  end
@@ -1,23 +1,25 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # This file was generated by Appraisal
2
4
 
3
- source "http://rubygems.org"
5
+ source 'http://rubygems.org'
4
6
 
5
- gem "rails", "~> 4.2.0"
7
+ gem 'rails', '~> 4.2.0'
6
8
 
7
9
  group :local do
8
- gem "pry"
9
- gem "guard-rspec", "~> 4.2"
10
+ gem 'guard-rspec', '~> 4.2'
11
+ gem 'pry'
10
12
  end
11
13
 
12
14
  platforms :ruby do
13
- gem "pg", "< 1.0.0"
14
- gem "mysql2", "~> 0.4.0"
15
+ gem 'mysql2', '~> 0.4.0'
16
+ gem 'pg', '< 1.0.0'
15
17
  end
16
18
 
17
19
  platforms :jruby do
18
- gem "activerecord-jdbc-adapter", "~> 1.3"
19
- gem "activerecord-jdbcpostgresql-adapter", "~> 1.3"
20
- gem "activerecord-jdbcmysql-adapter", "~> 1.3"
20
+ gem 'activerecord-jdbc-adapter', '~> 1.3'
21
+ gem 'activerecord-jdbcmysql-adapter', '~> 1.3'
22
+ gem 'activerecord-jdbcpostgresql-adapter', '~> 1.3'
21
23
  end
22
24
 
23
- gemspec path: "../"
25
+ gemspec path: '../'
@@ -2,11 +2,12 @@
2
2
 
3
3
  source "http://rubygems.org"
4
4
 
5
+ gem "perx-rubocop", "~> 0.0.3"
5
6
  gem "rails", "~> 5.0.0"
6
7
 
7
8
  group :local do
8
- gem "pry"
9
9
  gem "guard-rspec", "~> 4.2"
10
+ gem "pry"
10
11
  end
11
12
 
12
13
  platforms :ruby do
@@ -2,11 +2,12 @@
2
2
 
3
3
  source "http://rubygems.org"
4
4
 
5
+ gem "perx-rubocop", "~> 0.0.3"
5
6
  gem "rails", "~> 5.1.0"
6
7
 
7
8
  group :local do
8
- gem "pry"
9
9
  gem "guard-rspec", "~> 4.2"
10
+ gem "pry"
10
11
  end
11
12
 
12
13
  platforms :ruby do
@@ -2,11 +2,12 @@
2
2
 
3
3
  source "http://rubygems.org"
4
4
 
5
+ gem "perx-rubocop", "~> 0.0.3"
5
6
  gem "rails", "~> 5.2.0"
6
7
 
7
8
  group :local do
8
- gem "pry"
9
9
  gem "guard-rspec", "~> 4.2"
10
+ gem "pry"
10
11
  end
11
12
 
12
13
  platforms :jruby do
@@ -2,11 +2,12 @@
2
2
 
3
3
  source "http://rubygems.org"
4
4
 
5
- gem "rails", "~> 6.0.0.rc1"
5
+ gem "perx-rubocop", "~> 0.0.3"
6
+ gem "rails", "~> 6.0.0"
6
7
 
7
8
  group :local do
8
- gem "pry"
9
9
  gem "guard-rspec", "~> 4.2"
10
+ gem "pry"
10
11
  end
11
12
 
12
13
  platforms :ruby do
@@ -14,9 +15,9 @@ platforms :ruby do
14
15
  end
15
16
 
16
17
  platforms :jruby do
17
- gem "activerecord-jdbc-adapter", "~> 60.0.rc1"
18
- gem "activerecord-jdbcpostgresql-adapter", "~> 60.0.rc1"
19
- gem "activerecord-jdbcmysql-adapter", "~> 60.0.rc1"
18
+ gem "activerecord-jdbc-adapter", "~> 60.0"
19
+ gem "activerecord-jdbcpostgresql-adapter", "~> 60.0"
20
+ gem "activerecord-jdbcmysql-adapter", "~> 60.0"
20
21
  end
21
22
 
22
23
  gemspec path: "../"
@@ -2,11 +2,12 @@
2
2
 
3
3
  source "http://rubygems.org"
4
4
 
5
+ gem "perx-rubocop", "~> 0.0.3"
5
6
  gem "rails", git: "https://github.com/rails/rails.git"
6
7
 
7
8
  group :local do
8
- gem "pry"
9
9
  gem "guard-rspec", "~> 4.2"
10
+ gem "pry"
10
11
  end
11
12
 
12
13
  platforms :ruby do
@@ -1,22 +1,45 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'apartment/railtie' if defined?(Rails)
2
4
  require 'active_support/core_ext/object/blank'
3
5
  require 'forwardable'
4
6
  require 'active_record'
5
7
  require 'apartment/tenant'
6
8
 
7
- module Apartment
9
+ # require_relative 'apartment/arel/visitors/postgresql'
8
10
 
9
- class << self
11
+ require_relative 'apartment/active_record/log_subscriber'
12
+ require_relative 'apartment/active_record/connection_handling' if ActiveRecord.version.release >= Gem::Version.new('6.0')
13
+
14
+ if ActiveRecord.version.release >= Gem::Version.new('6.1')
15
+ require_relative 'apartment/active_record/schema_migration'
16
+ require_relative 'apartment/active_record/internal_metadata'
17
+ end
10
18
 
19
+ module Apartment
20
+ class << self
11
21
  extend Forwardable
12
22
 
13
- ACCESSOR_METHODS = [:use_schemas, :use_sql, :seed_after_create, :prepend_environment, :append_environment, :with_multi_server_setup]
14
- WRITER_METHODS = [:tenant_names, :database_schema_file, :excluded_models, :default_schema, :persistent_schemas, :connection_class, :tld_length, :db_migrate_tenants, :seed_data_file, :parallel_migration_threads, :pg_excluded_names]
23
+ ACCESSOR_METHODS = %i[use_schemas use_sql seed_after_create prepend_environment default_tenant
24
+ append_environment with_multi_server_setup tenant_presence_check active_record_log].freeze
25
+
26
+ WRITER_METHODS = %i[tenant_names database_schema_file excluded_models
27
+ persistent_schemas connection_class
28
+ db_migrate_tenants seed_data_file
29
+ parallel_migration_threads pg_excluded_names].freeze
15
30
 
16
31
  attr_accessor(*ACCESSOR_METHODS)
17
32
  attr_writer(*WRITER_METHODS)
18
33
 
19
- def_delegators :connection_class, :connection, :connection_config, :establish_connection
34
+ if ActiveRecord.version.release >= Gem::Version.new('6.1')
35
+ def_delegators :connection_class, :connection, :connection_db_config, :establish_connection
36
+
37
+ def connection_config
38
+ connection_db_config.configuration_hash
39
+ end
40
+ else
41
+ def_delegators :connection_class, :connection, :connection_config, :establish_connection
42
+ end
20
43
 
21
44
  # configure apartment with available options
22
45
  def configure
@@ -31,8 +54,12 @@ module Apartment
31
54
  extract_tenant_config
32
55
  end
33
56
 
57
+ def tld_length=(_)
58
+ Apartment::Deprecation.warn('`config.tld_length` have no effect because it was removed in https://github.com/influitive/apartment/pull/309')
59
+ end
60
+
34
61
  def db_config_for(tenant)
35
- (tenants_with_config[tenant] || connection_config).with_indifferent_access
62
+ (tenants_with_config[tenant] || connection_config)
36
63
  end
37
64
 
38
65
  # Whether or not db:migrate should also migrate tenants
@@ -48,15 +75,9 @@ module Apartment
48
75
  @excluded_models || []
49
76
  end
50
77
 
51
- def default_schema
52
- @default_schema || "public" # TODO 'public' is postgres specific
53
- end
54
-
55
78
  def parallel_migration_threads
56
79
  @parallel_migration_threads || 0
57
80
  end
58
- alias :default_tenant :default_schema
59
- alias :default_tenant= :default_schema=
60
81
 
61
82
  def persistent_schemas
62
83
  @persistent_schemas || []
@@ -75,7 +96,7 @@ module Apartment
75
96
  def seed_data_file
76
97
  return @seed_data_file if defined?(@seed_data_file)
77
98
 
78
- @seed_data_file = "#{Rails.root}/db/seeds.rb"
99
+ @seed_data_file = Rails.root.join('db', 'seeds.rb')
79
100
  end
80
101
 
81
102
  def pg_excluded_names
@@ -84,11 +105,14 @@ module Apartment
84
105
 
85
106
  # Reset all the config for Apartment
86
107
  def reset
87
- (ACCESSOR_METHODS + WRITER_METHODS).each{|method| remove_instance_variable(:"@#{method}") if instance_variable_defined?(:"@#{method}") }
108
+ (ACCESSOR_METHODS + WRITER_METHODS).each do |method|
109
+ remove_instance_variable(:"@#{method}") if instance_variable_defined?(:"@#{method}")
110
+ end
88
111
  end
89
112
 
90
113
  def extract_tenant_config
91
114
  return {} unless @tenant_names
115
+
92
116
  values = @tenant_names.respond_to?(:call) ? @tenant_names.call : @tenant_names
93
117
  unless values.is_a? Hash
94
118
  values = values.each_with_object({}) do |tenant, hash|
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveRecord
4
+ module ConnectionHandling
5
+ def connected_to_with_tenant(database: nil, role: nil, prevent_writes: false, &blk)
6
+ current_tenant = Apartment::Tenant.current
7
+
8
+ connected_to_without_tenant(database: database, role: role, prevent_writes: prevent_writes) do
9
+ Apartment::Tenant.switch!(current_tenant)
10
+ yield(blk)
11
+ end
12
+ end
13
+
14
+ alias connected_to_without_tenant connected_to
15
+ alias connected_to connected_to_with_tenant
16
+ end
17
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ # rubocop:disable Rails/ApplicationRecord
4
+ class InternalMetadata < ActiveRecord::Base # :nodoc:
5
+ class << self
6
+ def table_exists?
7
+ connection.table_exists?(table_name)
8
+ end
9
+ end
10
+ end
11
+ # rubocop:enable Rails/ApplicationRecord