ros-apartment 2.4.0 → 2.7.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (139) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +5 -0
  3. data/.rubocop_todo.yml +1 -1
  4. data/.story_branch.yml +1 -0
  5. data/.travis.yml +13 -30
  6. data/Appraisals +4 -17
  7. data/Gemfile +2 -2
  8. data/Guardfile +3 -1
  9. data/HISTORY.md +90 -0
  10. data/README.md +31 -16
  11. data/Rakefile +34 -22
  12. data/TODO.md +0 -1
  13. data/gemfiles/rails_5_0.gemfile +10 -11
  14. data/gemfiles/rails_5_1.gemfile +10 -11
  15. data/gemfiles/rails_5_2.gemfile +9 -10
  16. data/gemfiles/rails_6_0.gemfile +10 -11
  17. data/gemfiles/rails_master.gemfile +10 -11
  18. data/lib/apartment.rb +31 -13
  19. data/lib/apartment/active_record/connection_handling.rb +17 -0
  20. data/lib/apartment/active_record/internal_metadata.rb +11 -0
  21. data/lib/apartment/active_record/log_subscriber.rb +41 -0
  22. data/lib/apartment/active_record/schema_migration.rb +13 -0
  23. data/lib/apartment/adapters/abstract_adapter.rb +17 -13
  24. data/lib/apartment/adapters/jdbc_postgresql_adapter.rb +9 -4
  25. data/lib/apartment/adapters/mysql2_adapter.rb +2 -0
  26. data/lib/apartment/adapters/postgresql_adapter.rb +33 -4
  27. data/lib/apartment/adapters/sqlite3_adapter.rb +2 -0
  28. data/lib/apartment/console.rb +7 -0
  29. data/lib/apartment/custom_console.rb +23 -7
  30. data/lib/apartment/model.rb +27 -0
  31. data/lib/apartment/railtie.rb +19 -15
  32. data/lib/apartment/tasks/task_helper.rb +35 -0
  33. data/lib/apartment/tenant.rb +16 -5
  34. data/lib/apartment/version.rb +1 -1
  35. data/lib/generators/apartment/install/templates/apartment.rb +5 -0
  36. data/lib/tasks/apartment.rake +16 -37
  37. data/{apartment.gemspec → ros-apartment.gemspec} +12 -6
  38. metadata +16 -213
  39. data/.github/workflows/.rubocop-linter.yml +0 -22
  40. data/spec/adapters/jdbc_mysql_adapter_spec.rb +0 -20
  41. data/spec/adapters/jdbc_postgresql_adapter_spec.rb +0 -39
  42. data/spec/adapters/mysql2_adapter_spec.rb +0 -61
  43. data/spec/adapters/postgresql_adapter_spec.rb +0 -63
  44. data/spec/adapters/sqlite3_adapter_spec.rb +0 -101
  45. data/spec/apartment_spec.rb +0 -13
  46. data/spec/config/database.yml.sample +0 -49
  47. data/spec/dummy/Rakefile +0 -7
  48. data/spec/dummy/app/controllers/application_controller.rb +0 -7
  49. data/spec/dummy/app/helpers/application_helper.rb +0 -4
  50. data/spec/dummy/app/models/application_record.rb +0 -6
  51. data/spec/dummy/app/models/company.rb +0 -5
  52. data/spec/dummy/app/models/user.rb +0 -5
  53. data/spec/dummy/app/views/application/index.html.erb +0 -1
  54. data/spec/dummy/app/views/layouts/application.html.erb +0 -14
  55. data/spec/dummy/config.ru +0 -6
  56. data/spec/dummy/config/application.rb +0 -51
  57. data/spec/dummy/config/boot.rb +0 -13
  58. data/spec/dummy/config/database.yml.sample +0 -44
  59. data/spec/dummy/config/environment.rb +0 -7
  60. data/spec/dummy/config/environments/development.rb +0 -29
  61. data/spec/dummy/config/environments/production.rb +0 -53
  62. data/spec/dummy/config/environments/test.rb +0 -36
  63. data/spec/dummy/config/initializers/apartment.rb +0 -6
  64. data/spec/dummy/config/initializers/backtrace_silencers.rb +0 -8
  65. data/spec/dummy/config/initializers/inflections.rb +0 -11
  66. data/spec/dummy/config/initializers/mime_types.rb +0 -6
  67. data/spec/dummy/config/initializers/secret_token.rb +0 -9
  68. data/spec/dummy/config/initializers/session_store.rb +0 -10
  69. data/spec/dummy/config/locales/en.yml +0 -5
  70. data/spec/dummy/config/routes.rb +0 -5
  71. data/spec/dummy/db/migrate/20110613152810_create_dummy_models.rb +0 -39
  72. data/spec/dummy/db/migrate/20111202022214_create_table_books.rb +0 -14
  73. data/spec/dummy/db/migrate/20180415260934_create_public_tokens.rb +0 -13
  74. data/spec/dummy/db/schema.rb +0 -55
  75. data/spec/dummy/db/seeds.rb +0 -5
  76. data/spec/dummy/db/seeds/import.rb +0 -7
  77. data/spec/dummy/public/404.html +0 -26
  78. data/spec/dummy/public/422.html +0 -26
  79. data/spec/dummy/public/500.html +0 -26
  80. data/spec/dummy/public/favicon.ico +0 -0
  81. data/spec/dummy/public/stylesheets/.gitkeep +0 -0
  82. data/spec/dummy/script/rails +0 -8
  83. data/spec/dummy_engine/.gitignore +0 -8
  84. data/spec/dummy_engine/Gemfile +0 -15
  85. data/spec/dummy_engine/Rakefile +0 -34
  86. data/spec/dummy_engine/bin/rails +0 -12
  87. data/spec/dummy_engine/config/initializers/apartment.rb +0 -52
  88. data/spec/dummy_engine/dummy_engine.gemspec +0 -24
  89. data/spec/dummy_engine/lib/dummy_engine.rb +0 -6
  90. data/spec/dummy_engine/lib/dummy_engine/engine.rb +0 -6
  91. data/spec/dummy_engine/lib/dummy_engine/version.rb +0 -5
  92. data/spec/dummy_engine/test/dummy/Rakefile +0 -6
  93. data/spec/dummy_engine/test/dummy/config.ru +0 -6
  94. data/spec/dummy_engine/test/dummy/config/application.rb +0 -24
  95. data/spec/dummy_engine/test/dummy/config/boot.rb +0 -7
  96. data/spec/dummy_engine/test/dummy/config/database.yml +0 -25
  97. data/spec/dummy_engine/test/dummy/config/environment.rb +0 -7
  98. data/spec/dummy_engine/test/dummy/config/environments/development.rb +0 -39
  99. data/spec/dummy_engine/test/dummy/config/environments/production.rb +0 -80
  100. data/spec/dummy_engine/test/dummy/config/environments/test.rb +0 -41
  101. data/spec/dummy_engine/test/dummy/config/initializers/assets.rb +0 -10
  102. data/spec/dummy_engine/test/dummy/config/initializers/backtrace_silencers.rb +0 -8
  103. data/spec/dummy_engine/test/dummy/config/initializers/cookies_serializer.rb +0 -5
  104. data/spec/dummy_engine/test/dummy/config/initializers/filter_parameter_logging.rb +0 -6
  105. data/spec/dummy_engine/test/dummy/config/initializers/inflections.rb +0 -17
  106. data/spec/dummy_engine/test/dummy/config/initializers/mime_types.rb +0 -5
  107. data/spec/dummy_engine/test/dummy/config/initializers/session_store.rb +0 -5
  108. data/spec/dummy_engine/test/dummy/config/initializers/wrap_parameters.rb +0 -16
  109. data/spec/dummy_engine/test/dummy/config/locales/en.yml +0 -23
  110. data/spec/dummy_engine/test/dummy/config/routes.rb +0 -58
  111. data/spec/dummy_engine/test/dummy/config/secrets.yml +0 -22
  112. data/spec/examples/connection_adapter_examples.rb +0 -44
  113. data/spec/examples/generic_adapter_custom_configuration_example.rb +0 -93
  114. data/spec/examples/generic_adapter_examples.rb +0 -164
  115. data/spec/examples/schema_adapter_examples.rb +0 -239
  116. data/spec/integration/apartment_rake_integration_spec.rb +0 -107
  117. data/spec/integration/query_caching_spec.rb +0 -83
  118. data/spec/integration/use_within_an_engine_spec.rb +0 -28
  119. data/spec/schemas/v1.rb +0 -15
  120. data/spec/schemas/v2.rb +0 -41
  121. data/spec/schemas/v3.rb +0 -47
  122. data/spec/spec_helper.rb +0 -63
  123. data/spec/support/apartment_helpers.rb +0 -47
  124. data/spec/support/capybara_sessions.rb +0 -15
  125. data/spec/support/config.rb +0 -13
  126. data/spec/support/contexts.rb +0 -54
  127. data/spec/support/requirements.rb +0 -48
  128. data/spec/support/setup.rb +0 -46
  129. data/spec/tasks/apartment_rake_spec.rb +0 -124
  130. data/spec/tenant_spec.rb +0 -194
  131. data/spec/unit/config_spec.rb +0 -111
  132. data/spec/unit/elevators/domain_spec.rb +0 -33
  133. data/spec/unit/elevators/first_subdomain_spec.rb +0 -26
  134. data/spec/unit/elevators/generic_spec.rb +0 -55
  135. data/spec/unit/elevators/host_hash_spec.rb +0 -33
  136. data/spec/unit/elevators/host_spec.rb +0 -89
  137. data/spec/unit/elevators/subdomain_spec.rb +0 -77
  138. data/spec/unit/migrator_spec.rb +0 -78
  139. data/spec/unit/reloader_spec.rb +0 -24
@@ -1,13 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'yaml'
4
-
5
- module Apartment
6
- module Test
7
- def self.config
8
- # rubocop:disable Security/YAMLLoad
9
- @config ||= YAML.load(ERB.new(IO.read('spec/config/database.yml')).result)
10
- # rubocop:enable Security/YAMLLoad
11
- end
12
- end
13
- end
@@ -1,54 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # Some shared contexts for specs
4
-
5
- shared_context 'with default schema', default_schema: true do
6
- let(:default_schema) { Apartment::Test.next_db }
7
-
8
- before do
9
- Apartment::Test.create_schema(default_schema)
10
- Apartment.default_schema = default_schema
11
- end
12
-
13
- after do
14
- # resetting default_schema so we can drop and any further resets won't try to access droppped schema
15
- Apartment.default_schema = nil
16
- Apartment::Test.drop_schema(default_schema)
17
- end
18
- end
19
-
20
- # Some default setup for elevator specs
21
- shared_context 'elevators', elevator: true do
22
- let(:company1) { mock_model(Company, database: db1).as_null_object }
23
- let(:company2) { mock_model(Company, database: db2).as_null_object }
24
-
25
- let(:api) { Apartment::Tenant }
26
-
27
- before do
28
- Apartment.reset # reset all config
29
- Apartment.seed_after_create = false
30
- Apartment.use_schemas = true
31
- api.reload!(config)
32
- api.create(db1)
33
- api.create(db2)
34
- end
35
-
36
- after do
37
- api.drop(db1)
38
- api.drop(db2)
39
- end
40
- end
41
-
42
- shared_context 'persistent_schemas', persistent_schemas: true do
43
- let(:persistent_schemas) { %w[hstore postgis] }
44
-
45
- before do
46
- persistent_schemas.map { |schema| subject.create(schema) }
47
- Apartment.persistent_schemas = persistent_schemas
48
- end
49
-
50
- after do
51
- Apartment.persistent_schemas = []
52
- persistent_schemas.map { |schema| subject.drop(schema) }
53
- end
54
- end
@@ -1,48 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Apartment
4
- module Spec
5
- #
6
- # Define the interface methods required to
7
- # use an adapter shared example
8
- #
9
- #
10
- module AdapterRequirements
11
- extend ActiveSupport::Concern
12
-
13
- included do
14
- before do
15
- subject.create(db1)
16
- subject.create(db2)
17
- end
18
-
19
- after do
20
- # Reset before dropping (can't drop a db you're connected to)
21
- subject.reset
22
-
23
- # sometimes we manually drop these schemas in testing, don't care if
24
- # we can't drop, hence rescue
25
- begin
26
- subject.drop(db1)
27
- rescue StandardError => _e
28
- true
29
- end
30
-
31
- begin
32
- subject.drop(db2)
33
- rescue StandardError => _e
34
- true
35
- end
36
- end
37
- end
38
-
39
- %w[subject tenant_names default_tenant].each do |method|
40
- next if defined?(method)
41
-
42
- define_method method do
43
- raise "You must define a `#{method}` method in your host group"
44
- end
45
- end
46
- end
47
- end
48
- end
@@ -1,46 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Apartment
4
- module Spec
5
- module Setup
6
- def self.included(base)
7
- base.instance_eval do
8
- let(:db1) { Apartment::Test.next_db }
9
- let(:db2) { Apartment::Test.next_db }
10
- let(:connection) { ActiveRecord::Base.connection }
11
-
12
- # This around ensures that we run these hooks before and after
13
- # any before/after hooks defined in individual tests
14
- # Otherwise these actually get run after test defined hooks
15
- around(:each) do |example|
16
- def config
17
- db = RSpec.current_example.metadata.fetch(:database, :postgresql)
18
-
19
- Apartment::Test.config['connections'][db.to_s].symbolize_keys
20
- end
21
-
22
- # before
23
- Apartment::Tenant.reload!(config)
24
- ActiveRecord::Base.establish_connection config
25
-
26
- example.run
27
-
28
- # after
29
- Rails.configuration.database_configuration = {}
30
- ActiveRecord::Base.clear_all_connections!
31
-
32
- Apartment.excluded_models.each do |model|
33
- klass = model.constantize
34
-
35
- Apartment.connection_class.remove_connection(klass)
36
- klass.clear_all_connections!
37
- klass.reset_table_name
38
- end
39
- Apartment.reset
40
- Apartment::Tenant.reload!
41
- end
42
- end
43
- end
44
- end
45
- end
46
- end
@@ -1,124 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'spec_helper'
4
- require 'rake'
5
- require 'apartment/migrator'
6
- require 'apartment/tenant'
7
-
8
- describe 'apartment rake tasks' do
9
- before do
10
- @rake = Rake::Application.new
11
- Rake.application = @rake
12
- load 'tasks/apartment.rake'
13
- # stub out rails tasks
14
- Rake::Task.define_task('db:migrate')
15
- Rake::Task.define_task('db:seed')
16
- Rake::Task.define_task('db:rollback')
17
- Rake::Task.define_task('db:migrate:up')
18
- Rake::Task.define_task('db:migrate:down')
19
- Rake::Task.define_task('db:migrate:redo')
20
- end
21
-
22
- after do
23
- Rake.application = nil
24
- ENV['VERSION'] = nil # linux users reported env variable carrying on between tests
25
- end
26
-
27
- after(:all) do
28
- Apartment::Test.load_schema
29
- end
30
-
31
- let(:version) { '1234' }
32
-
33
- context 'database migration' do
34
- let(:tenant_names) { 3.times.map { Apartment::Test.next_db } }
35
- let(:tenant_count) { tenant_names.length }
36
-
37
- before do
38
- allow(Apartment).to receive(:tenant_names).and_return tenant_names
39
- end
40
-
41
- describe 'apartment:migrate' do
42
- before do
43
- allow(ActiveRecord::Migrator).to receive(:migrate) # don't care about this
44
- end
45
-
46
- it 'should migrate public and all multi-tenant dbs' do
47
- expect(Apartment::Migrator).to receive(:migrate).exactly(tenant_count).times
48
- @rake['apartment:migrate'].invoke
49
- end
50
- end
51
-
52
- describe 'apartment:migrate:up' do
53
- context 'without a version' do
54
- before do
55
- ENV['VERSION'] = nil
56
- end
57
-
58
- it 'requires a version to migrate to' do
59
- expect do
60
- @rake['apartment:migrate:up'].invoke
61
- end.to raise_error('VERSION is required')
62
- end
63
- end
64
-
65
- context 'with version' do
66
- before do
67
- ENV['VERSION'] = version
68
- end
69
-
70
- it 'migrates up to a specific version' do
71
- expect(Apartment::Migrator).to receive(:run).with(:up, anything, version.to_i).exactly(tenant_count).times
72
- @rake['apartment:migrate:up'].invoke
73
- end
74
- end
75
- end
76
-
77
- describe 'apartment:migrate:down' do
78
- context 'without a version' do
79
- before do
80
- ENV['VERSION'] = nil
81
- end
82
-
83
- it 'requires a version to migrate to' do
84
- expect do
85
- @rake['apartment:migrate:down'].invoke
86
- end.to raise_error('VERSION is required')
87
- end
88
- end
89
-
90
- context 'with version' do
91
- before do
92
- ENV['VERSION'] = version
93
- end
94
-
95
- it 'migrates up to a specific version' do
96
- expect(Apartment::Migrator).to receive(:run).with(:down, anything, version.to_i).exactly(tenant_count).times
97
- @rake['apartment:migrate:down'].invoke
98
- end
99
- end
100
- end
101
-
102
- describe 'apartment:rollback' do
103
- let(:step) { '3' }
104
-
105
- it 'should rollback dbs' do
106
- expect(Apartment::Migrator).to receive(:rollback).exactly(tenant_count).times
107
- @rake['apartment:rollback'].invoke
108
- end
109
-
110
- it 'should rollback dbs STEP amt' do
111
- expect(Apartment::Migrator).to receive(:rollback).with(anything, step.to_i).exactly(tenant_count).times
112
- ENV['STEP'] = step
113
- @rake['apartment:rollback'].invoke
114
- end
115
- end
116
-
117
- describe 'apartment:drop' do
118
- it 'should migrate public and all multi-tenant dbs' do
119
- expect(Apartment::Tenant).to receive(:drop).exactly(tenant_count).times
120
- @rake['apartment:drop'].invoke
121
- end
122
- end
123
- end
124
- end
@@ -1,194 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'spec_helper'
4
-
5
- describe Apartment::Tenant do
6
- context 'using mysql', database: :mysql do
7
- before { subject.reload!(config) }
8
-
9
- describe '#adapter' do
10
- it 'should load mysql adapter' do
11
- subject.adapter
12
- expect(Apartment::Adapters::Mysql2Adapter).to be_a(Class)
13
- end
14
- end
15
-
16
- # TODO: this doesn't belong here, but there aren't integration tests currently for mysql
17
- # where to put???
18
- describe 'exception recovery', type: :request do
19
- before do
20
- subject.create db1
21
- end
22
- after { subject.drop db1 }
23
-
24
- # it "should recover from incorrect database" do
25
- # session = Capybara::Session.new(:rack_test, Capybara.app)
26
- # session.visit("http://#{db1}.com")
27
- # expect {
28
- # session.visit("http://this-database-should-not-exist.com")
29
- # }.to raise_error
30
- # session.visit("http://#{db1}.com")
31
- # end
32
- end
33
-
34
- # TODO: re-organize these tests
35
- context 'with prefix and schemas' do
36
- describe '#create' do
37
- before do
38
- Apartment.configure do |config|
39
- config.prepend_environment = true
40
- config.use_schemas = true
41
- end
42
-
43
- subject.reload!(config)
44
- end
45
-
46
- after do
47
- begin
48
- subject.drop 'db_with_prefix'
49
- rescue StandardError => _e
50
- nil
51
- end
52
- end
53
-
54
- it 'should create a new database' do
55
- subject.create 'db_with_prefix'
56
- end
57
- end
58
- end
59
- end
60
-
61
- context 'using postgresql', database: :postgresql do
62
- before do
63
- Apartment.use_schemas = true
64
- subject.reload!(config)
65
- end
66
-
67
- describe '#adapter' do
68
- it 'should load postgresql adapter' do
69
- expect(subject.adapter).to be_a(Apartment::Adapters::PostgresqlSchemaAdapter)
70
- end
71
-
72
- it 'raises exception with invalid adapter specified' do
73
- subject.reload!(config.merge(adapter: 'unknown'))
74
-
75
- expect do
76
- Apartment::Tenant.adapter
77
- end.to raise_error(RuntimeError)
78
- end
79
-
80
- context 'threadsafety' do
81
- before { subject.create db1 }
82
- after { subject.drop db1 }
83
-
84
- it 'has a threadsafe adapter' do
85
- subject.switch!(db1)
86
- thread = Thread.new { expect(subject.current).to eq(Apartment.default_tenant) }
87
- thread.join
88
- expect(subject.current).to eq(db1)
89
- end
90
- end
91
- end
92
-
93
- # TODO: above spec are also with use_schemas=true
94
- context 'with schemas' do
95
- before do
96
- Apartment.configure do |config|
97
- config.excluded_models = []
98
- config.use_schemas = true
99
- config.seed_after_create = true
100
- end
101
- subject.create db1
102
- end
103
-
104
- after { subject.drop db1 }
105
-
106
- describe '#create' do
107
- it 'should seed data' do
108
- subject.switch! db1
109
- expect(User.count).to be > 0
110
- end
111
- end
112
-
113
- describe '#switch!' do
114
- let(:x) { rand(3) }
115
-
116
- context 'creating models' do
117
- before { subject.create db2 }
118
- after { subject.drop db2 }
119
-
120
- it 'should create a model instance in the current schema' do
121
- subject.switch! db2
122
- db2_count = User.count + x.times { User.create }
123
-
124
- subject.switch! db1
125
- db_count = User.count + x.times { User.create }
126
-
127
- subject.switch! db2
128
- expect(User.count).to eq(db2_count)
129
-
130
- subject.switch! db1
131
- expect(User.count).to eq(db_count)
132
- end
133
- end
134
-
135
- context 'with excluded models' do
136
- before do
137
- Apartment.configure do |config|
138
- config.excluded_models = ['Company']
139
- end
140
- subject.init
141
- end
142
-
143
- after do
144
- # Apartment::Tenant.init creates per model connection.
145
- # Remove the connection after testing not to unintentionally keep the connection across tests.
146
- Apartment.excluded_models.each do |excluded_model|
147
- excluded_model.constantize.remove_connection
148
- end
149
- end
150
-
151
- it 'should create excluded models in public schema' do
152
- subject.reset # ensure we're on public schema
153
- count = Company.count + x.times { Company.create }
154
-
155
- subject.switch! db1
156
- x.times { Company.create }
157
- expect(Company.count).to eq(count + x)
158
- subject.reset
159
- expect(Company.count).to eq(count + x)
160
- end
161
- end
162
- end
163
- end
164
-
165
- context 'seed paths' do
166
- before do
167
- Apartment.configure do |config|
168
- config.excluded_models = []
169
- config.use_schemas = true
170
- config.seed_after_create = true
171
- end
172
- end
173
-
174
- after { subject.drop db1 }
175
-
176
- it 'should seed from default path' do
177
- subject.create db1
178
- subject.switch! db1
179
- expect(User.count).to eq(3)
180
- expect(User.first.name).to eq('Some User 0')
181
- end
182
-
183
- it 'should seed from custom path' do
184
- Apartment.configure do |config|
185
- config.seed_data_file = Rails.root.join('db', 'seeds', 'import.rb')
186
- end
187
- subject.create db1
188
- subject.switch! db1
189
- expect(User.count).to eq(6)
190
- expect(User.first.name).to eq('Different User 0')
191
- end
192
- end
193
- end
194
- end