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
@@ -1,10 +0,0 @@
1
- require 'yaml'
2
-
3
- module Apartment
4
- module Test
5
-
6
- def self.config
7
- @config ||= YAML.load(ERB.new(IO.read('spec/config/database.yml')).result)
8
- end
9
- end
10
- end
@@ -1,52 +0,0 @@
1
- # Some shared contexts for specs
2
-
3
- shared_context "with default schema", :default_schema => true do
4
- let(:default_schema){ Apartment::Test.next_db }
5
-
6
- before do
7
- Apartment::Test.create_schema(default_schema)
8
- Apartment.default_schema = default_schema
9
- end
10
-
11
- after do
12
- # resetting default_schema so we can drop and any further resets won't try to access droppped schema
13
- Apartment.default_schema = nil
14
- Apartment::Test.drop_schema(default_schema)
15
- end
16
- end
17
-
18
- # Some default setup for elevator specs
19
- shared_context "elevators", elevator: true do
20
- let(:company1) { mock_model(Company, database: db1).as_null_object }
21
- let(:company2) { mock_model(Company, database: db2).as_null_object }
22
-
23
- let(:api) { Apartment::Tenant }
24
-
25
- before do
26
- Apartment.reset # reset all config
27
- Apartment.seed_after_create = false
28
- Apartment.use_schemas = true
29
- api.reload!(config)
30
- api.create(db1)
31
- api.create(db2)
32
- end
33
-
34
- after do
35
- api.drop(db1)
36
- api.drop(db2)
37
- end
38
- end
39
-
40
- shared_context "persistent_schemas", :persistent_schemas => true do
41
- let(:persistent_schemas){ ['hstore', 'postgis'] }
42
-
43
- before do
44
- persistent_schemas.map{|schema| subject.create(schema) }
45
- Apartment.persistent_schemas = persistent_schemas
46
- end
47
-
48
- after do
49
- Apartment.persistent_schemas = []
50
- persistent_schemas.map{|schema| subject.drop(schema) }
51
- end
52
- end
@@ -1,35 +0,0 @@
1
- module Apartment
2
- module Spec
3
-
4
- #
5
- # Define the interface methods required to
6
- # use an adapter shared example
7
- #
8
- #
9
- module AdapterRequirements
10
- extend ActiveSupport::Concern
11
-
12
- included do
13
- before do
14
- subject.create(db1)
15
- subject.create(db2)
16
- end
17
-
18
- after do
19
- # Reset before dropping (can't drop a db you're connected to)
20
- subject.reset
21
-
22
- # sometimes we manually drop these schemas in testing, don't care if we can't drop, hence rescue
23
- subject.drop(db1) rescue true
24
- subject.drop(db2) rescue true
25
- end
26
- end
27
-
28
- %w{subject tenant_names default_tenant}.each do |method|
29
- define_method method do
30
- raise "You must define a `#{method}` method in your host group"
31
- end unless defined?(method)
32
- end
33
- end
34
- end
35
- end
@@ -1,46 +0,0 @@
1
- module Apartment
2
- module Spec
3
- module Setup
4
-
5
- def self.included(base)
6
- base.instance_eval do
7
- let(:db1){ Apartment::Test.next_db }
8
- let(:db2){ Apartment::Test.next_db }
9
- let(:connection){ ActiveRecord::Base.connection }
10
-
11
- # This around ensures that we run these hooks before and after
12
- # any before/after hooks defined in individual tests
13
- # Otherwise these actually get run after test defined hooks
14
- around(:each) do |example|
15
-
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,129 +0,0 @@
1
- require 'spec_helper'
2
- require 'rake'
3
- require 'apartment/migrator'
4
- require 'apartment/tenant'
5
-
6
- describe "apartment rake tasks" do
7
-
8
- before do
9
- @rake = Rake::Application.new
10
- Rake.application = @rake
11
- load 'tasks/apartment.rake'
12
- # stub out rails tasks
13
- Rake::Task.define_task('db:migrate')
14
- Rake::Task.define_task('db:seed')
15
- Rake::Task.define_task('db:rollback')
16
- Rake::Task.define_task('db:migrate:up')
17
- Rake::Task.define_task('db:migrate:down')
18
- Rake::Task.define_task('db:migrate:redo')
19
- end
20
-
21
- after do
22
- Rake.application = nil
23
- ENV['VERSION'] = nil # linux users reported env variable carrying on between tests
24
- end
25
-
26
- after(:all) do
27
- Apartment::Test.load_schema
28
- end
29
-
30
- let(:version){ '1234' }
31
-
32
- context 'database migration' do
33
-
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
-
54
- context "without a version" do
55
- before do
56
- ENV['VERSION'] = nil
57
- end
58
-
59
- it "requires a version to migrate to" do
60
- expect{
61
- @rake['apartment:migrate:up'].invoke
62
- }.to raise_error("VERSION is required")
63
- end
64
- end
65
-
66
- context "with version" do
67
-
68
- before do
69
- ENV['VERSION'] = version
70
- end
71
-
72
- it "migrates up to a specific version" do
73
- expect(Apartment::Migrator).to receive(:run).with(:up, anything, version.to_i).exactly(tenant_count).times
74
- @rake['apartment:migrate:up'].invoke
75
- end
76
- end
77
- end
78
-
79
- describe "apartment:migrate:down" do
80
-
81
- context "without a version" do
82
- before do
83
- ENV['VERSION'] = nil
84
- end
85
-
86
- it "requires a version to migrate to" do
87
- expect{
88
- @rake['apartment:migrate:down'].invoke
89
- }.to raise_error("VERSION is required")
90
- end
91
- end
92
-
93
- context "with version" do
94
-
95
- before do
96
- ENV['VERSION'] = version
97
- end
98
-
99
- it "migrates up to a specific version" do
100
- expect(Apartment::Migrator).to receive(:run).with(:down, anything, version.to_i).exactly(tenant_count).times
101
- @rake['apartment:migrate:down'].invoke
102
- end
103
- end
104
- end
105
-
106
- describe "apartment:rollback" do
107
- let(:step){ '3' }
108
-
109
- it "should rollback dbs" do
110
- expect(Apartment::Migrator).to receive(:rollback).exactly(tenant_count).times
111
- @rake['apartment:rollback'].invoke
112
- end
113
-
114
- it "should rollback dbs STEP amt" do
115
- expect(Apartment::Migrator).to receive(:rollback).with(anything, step.to_i).exactly(tenant_count).times
116
- ENV['STEP'] = step
117
- @rake['apartment:rollback'].invoke
118
- end
119
- end
120
-
121
- describe "apartment:drop" do
122
- it "should migrate public and all multi-tenant dbs" do
123
- expect(Apartment::Tenant).to receive(:drop).exactly(tenant_count).times
124
- @rake['apartment:drop'].invoke
125
- end
126
- end
127
-
128
- end
129
- end
@@ -1,190 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Apartment::Tenant do
4
- context "using mysql", database: :mysql do
5
-
6
- before { subject.reload!(config) }
7
-
8
- describe "#adapter" do
9
- it "should load mysql adapter" do
10
- subject.adapter
11
- expect(Apartment::Adapters::Mysql2Adapter).to be_a(Class)
12
- end
13
- end
14
-
15
- # TODO this doesn't belong here, but there aren't integration tests currently for mysql
16
- # where to put???
17
- describe "exception recovery", :type => :request do
18
- before do
19
- subject.create db1
20
- end
21
- after{ subject.drop db1 }
22
-
23
- # it "should recover from incorrect database" do
24
- # session = Capybara::Session.new(:rack_test, Capybara.app)
25
- # session.visit("http://#{db1}.com")
26
- # expect {
27
- # session.visit("http://this-database-should-not-exist.com")
28
- # }.to raise_error
29
- # session.visit("http://#{db1}.com")
30
- # end
31
- end
32
-
33
- # TODO re-organize these tests
34
- context "with prefix and schemas" do
35
- describe "#create" do
36
- before do
37
- Apartment.configure do |config|
38
- config.prepend_environment = true
39
- config.use_schemas = true
40
- end
41
-
42
- subject.reload!(config)
43
- end
44
-
45
- after { subject.drop "db_with_prefix" rescue nil }
46
-
47
- it "should create a new database" do
48
- subject.create "db_with_prefix"
49
- end
50
- end
51
- end
52
- end
53
-
54
- context "using postgresql", database: :postgresql do
55
- before do
56
- Apartment.use_schemas = true
57
- subject.reload!(config)
58
- end
59
-
60
- describe "#adapter" do
61
- it "should load postgresql adapter" do
62
- expect(subject.adapter).to be_a(Apartment::Adapters::PostgresqlSchemaAdapter)
63
- end
64
-
65
- it "raises exception with invalid adapter specified" do
66
- subject.reload!(config.merge(adapter: 'unknown'))
67
-
68
- expect {
69
- Apartment::Tenant.adapter
70
- }.to raise_error(RuntimeError)
71
- end
72
-
73
- context "threadsafety" do
74
- before { subject.create db1 }
75
- after { subject.drop db1 }
76
-
77
- it 'has a threadsafe adapter' do
78
- subject.switch!(db1)
79
- thread = Thread.new { expect(subject.current).to eq(Apartment.default_tenant) }
80
- thread.join
81
- expect(subject.current).to eq(db1)
82
- end
83
- end
84
- end
85
-
86
- # TODO above spec are also with use_schemas=true
87
- context "with schemas" do
88
- before do
89
- Apartment.configure do |config|
90
- config.excluded_models = []
91
- config.use_schemas = true
92
- config.seed_after_create = true
93
- end
94
- subject.create db1
95
- end
96
-
97
- after{ subject.drop db1 }
98
-
99
- describe "#create" do
100
- it "should seed data" do
101
- subject.switch! db1
102
- expect(User.count).to be > 0
103
- end
104
- end
105
-
106
- describe "#switch!" do
107
-
108
- let(:x){ rand(3) }
109
-
110
- context "creating models" do
111
-
112
- before{ subject.create db2 }
113
- after{ subject.drop db2 }
114
-
115
- it "should create a model instance in the current schema" do
116
- subject.switch! db2
117
- db2_count = User.count + x.times{ User.create }
118
-
119
- subject.switch! db1
120
- db_count = User.count + x.times{ User.create }
121
-
122
- subject.switch! db2
123
- expect(User.count).to eq(db2_count)
124
-
125
- subject.switch! db1
126
- expect(User.count).to eq(db_count)
127
- end
128
- end
129
-
130
- context "with excluded models" do
131
-
132
- before do
133
- Apartment.configure do |config|
134
- config.excluded_models = ["Company"]
135
- end
136
- subject.init
137
- end
138
-
139
- after do
140
- # Apartment::Tenant.init creates per model connection.
141
- # Remove the connection after testing not to unintentionally keep the connection across tests.
142
- Apartment.excluded_models.each do |excluded_model|
143
- excluded_model.constantize.remove_connection
144
- end
145
- end
146
-
147
- it "should create excluded models in public schema" do
148
- subject.reset # ensure we're on public schema
149
- count = Company.count + x.times{ Company.create }
150
-
151
- subject.switch! db1
152
- x.times{ Company.create }
153
- expect(Company.count).to eq(count + x)
154
- subject.reset
155
- expect(Company.count).to eq(count + x)
156
- end
157
- end
158
- end
159
- end
160
-
161
- context "seed paths" do
162
- before do
163
- Apartment.configure do |config|
164
- config.excluded_models = []
165
- config.use_schemas = true
166
- config.seed_after_create = true
167
- end
168
- end
169
-
170
- after{ subject.drop db1 }
171
-
172
- it 'should seed from default path' do
173
- subject.create db1
174
- subject.switch! db1
175
- expect(User.count).to eq(3)
176
- expect(User.first.name).to eq('Some User 0')
177
- end
178
-
179
- it 'should seed from custom path' do
180
- Apartment.configure do |config|
181
- config.seed_data_file = "#{Rails.root}/db/seeds/import.rb"
182
- end
183
- subject.create db1
184
- subject.switch! db1
185
- expect(User.count).to eq(6)
186
- expect(User.first.name).to eq('Different User 0')
187
- end
188
- end
189
- end
190
- end