ros-apartment 2.3.0.alpha1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.github/ISSUE_TEMPLATE.md +21 -0
- data/.gitignore +15 -0
- data/.pryrc +3 -0
- data/.rspec +4 -0
- data/.travis.yml +65 -0
- data/Appraisals +71 -0
- data/Gemfile +10 -0
- data/Guardfile +24 -0
- data/HISTORY.md +398 -0
- data/README.md +576 -0
- data/Rakefile +128 -0
- data/TODO.md +51 -0
- data/apartment.gemspec +46 -0
- data/docker-compose.yml +33 -0
- data/gemfiles/rails_4_2.gemfile +23 -0
- data/gemfiles/rails_5_0.gemfile +22 -0
- data/gemfiles/rails_5_1.gemfile +22 -0
- data/gemfiles/rails_5_2.gemfile +18 -0
- data/gemfiles/rails_6_0.gemfile +22 -0
- data/gemfiles/rails_master.gemfile +22 -0
- data/lib/apartment.rb +118 -0
- data/lib/apartment/adapters/abstract_adapter.rb +269 -0
- data/lib/apartment/adapters/abstract_jdbc_adapter.rb +18 -0
- data/lib/apartment/adapters/jdbc_mysql_adapter.rb +19 -0
- data/lib/apartment/adapters/jdbc_postgresql_adapter.rb +56 -0
- data/lib/apartment/adapters/mysql2_adapter.rb +71 -0
- data/lib/apartment/adapters/postgis_adapter.rb +12 -0
- data/lib/apartment/adapters/postgresql_adapter.rb +236 -0
- data/lib/apartment/adapters/sqlite3_adapter.rb +56 -0
- data/lib/apartment/console.rb +12 -0
- data/lib/apartment/deprecation.rb +10 -0
- data/lib/apartment/elevators/domain.rb +22 -0
- data/lib/apartment/elevators/first_subdomain.rb +17 -0
- data/lib/apartment/elevators/generic.rb +32 -0
- data/lib/apartment/elevators/host.rb +30 -0
- data/lib/apartment/elevators/host_hash.rb +22 -0
- data/lib/apartment/elevators/subdomain.rb +62 -0
- data/lib/apartment/migrator.rb +51 -0
- data/lib/apartment/railtie.rb +67 -0
- data/lib/apartment/reloader.rb +21 -0
- data/lib/apartment/tasks/enhancements.rb +57 -0
- data/lib/apartment/tenant.rb +66 -0
- data/lib/apartment/version.rb +3 -0
- data/lib/generators/apartment/install/USAGE +5 -0
- data/lib/generators/apartment/install/install_generator.rb +10 -0
- data/lib/generators/apartment/install/templates/apartment.rb +109 -0
- data/lib/tasks/apartment.rake +145 -0
- data/spec/adapters/jdbc_mysql_adapter_spec.rb +19 -0
- data/spec/adapters/jdbc_postgresql_adapter_spec.rb +41 -0
- data/spec/adapters/mysql2_adapter_spec.rb +59 -0
- data/spec/adapters/postgresql_adapter_spec.rb +61 -0
- data/spec/adapters/sqlite3_adapter_spec.rb +83 -0
- data/spec/apartment_spec.rb +11 -0
- data/spec/config/database.yml.sample +49 -0
- data/spec/dummy/Rakefile +7 -0
- data/spec/dummy/app/controllers/application_controller.rb +6 -0
- data/spec/dummy/app/helpers/application_helper.rb +2 -0
- data/spec/dummy/app/models/company.rb +3 -0
- data/spec/dummy/app/models/user.rb +3 -0
- data/spec/dummy/app/views/application/index.html.erb +1 -0
- data/spec/dummy/app/views/layouts/application.html.erb +14 -0
- data/spec/dummy/config.ru +4 -0
- data/spec/dummy/config/application.rb +49 -0
- data/spec/dummy/config/boot.rb +11 -0
- data/spec/dummy/config/database.yml.sample +44 -0
- data/spec/dummy/config/environment.rb +5 -0
- data/spec/dummy/config/environments/development.rb +28 -0
- data/spec/dummy/config/environments/production.rb +51 -0
- data/spec/dummy/config/environments/test.rb +34 -0
- data/spec/dummy/config/initializers/apartment.rb +4 -0
- data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/dummy/config/initializers/inflections.rb +10 -0
- data/spec/dummy/config/initializers/mime_types.rb +5 -0
- data/spec/dummy/config/initializers/secret_token.rb +7 -0
- data/spec/dummy/config/initializers/session_store.rb +8 -0
- data/spec/dummy/config/locales/en.yml +5 -0
- data/spec/dummy/config/routes.rb +3 -0
- data/spec/dummy/db/migrate/20110613152810_create_dummy_models.rb +39 -0
- data/spec/dummy/db/migrate/20111202022214_create_table_books.rb +14 -0
- data/spec/dummy/db/migrate/20180415260934_create_public_tokens.rb +13 -0
- data/spec/dummy/db/schema.rb +55 -0
- data/spec/dummy/db/seeds.rb +5 -0
- data/spec/dummy/db/seeds/import.rb +5 -0
- data/spec/dummy/public/404.html +26 -0
- data/spec/dummy/public/422.html +26 -0
- data/spec/dummy/public/500.html +26 -0
- data/spec/dummy/public/favicon.ico +0 -0
- data/spec/dummy/public/stylesheets/.gitkeep +0 -0
- data/spec/dummy/script/rails +6 -0
- data/spec/dummy_engine/.gitignore +8 -0
- data/spec/dummy_engine/Gemfile +15 -0
- data/spec/dummy_engine/Rakefile +34 -0
- data/spec/dummy_engine/bin/rails +12 -0
- data/spec/dummy_engine/config/initializers/apartment.rb +51 -0
- data/spec/dummy_engine/dummy_engine.gemspec +24 -0
- data/spec/dummy_engine/lib/dummy_engine.rb +4 -0
- data/spec/dummy_engine/lib/dummy_engine/engine.rb +4 -0
- data/spec/dummy_engine/lib/dummy_engine/version.rb +3 -0
- data/spec/dummy_engine/test/dummy/Rakefile +6 -0
- data/spec/dummy_engine/test/dummy/config.ru +4 -0
- data/spec/dummy_engine/test/dummy/config/application.rb +22 -0
- data/spec/dummy_engine/test/dummy/config/boot.rb +5 -0
- data/spec/dummy_engine/test/dummy/config/database.yml +25 -0
- data/spec/dummy_engine/test/dummy/config/environment.rb +5 -0
- data/spec/dummy_engine/test/dummy/config/environments/development.rb +37 -0
- data/spec/dummy_engine/test/dummy/config/environments/production.rb +78 -0
- data/spec/dummy_engine/test/dummy/config/environments/test.rb +39 -0
- data/spec/dummy_engine/test/dummy/config/initializers/assets.rb +8 -0
- data/spec/dummy_engine/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/dummy_engine/test/dummy/config/initializers/cookies_serializer.rb +3 -0
- data/spec/dummy_engine/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
- data/spec/dummy_engine/test/dummy/config/initializers/inflections.rb +16 -0
- data/spec/dummy_engine/test/dummy/config/initializers/mime_types.rb +4 -0
- data/spec/dummy_engine/test/dummy/config/initializers/session_store.rb +3 -0
- data/spec/dummy_engine/test/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/spec/dummy_engine/test/dummy/config/locales/en.yml +23 -0
- data/spec/dummy_engine/test/dummy/config/routes.rb +56 -0
- data/spec/dummy_engine/test/dummy/config/secrets.yml +22 -0
- data/spec/examples/connection_adapter_examples.rb +42 -0
- data/spec/examples/generic_adapter_custom_configuration_example.rb +95 -0
- data/spec/examples/generic_adapter_examples.rb +163 -0
- data/spec/examples/schema_adapter_examples.rb +234 -0
- data/spec/integration/apartment_rake_integration_spec.rb +107 -0
- data/spec/integration/query_caching_spec.rb +81 -0
- data/spec/integration/use_within_an_engine_spec.rb +28 -0
- data/spec/schemas/v1.rb +16 -0
- data/spec/schemas/v2.rb +43 -0
- data/spec/schemas/v3.rb +49 -0
- data/spec/spec_helper.rb +61 -0
- data/spec/support/apartment_helpers.rb +43 -0
- data/spec/support/capybara_sessions.rb +15 -0
- data/spec/support/config.rb +10 -0
- data/spec/support/contexts.rb +52 -0
- data/spec/support/requirements.rb +35 -0
- data/spec/support/setup.rb +46 -0
- data/spec/tasks/apartment_rake_spec.rb +129 -0
- data/spec/tenant_spec.rb +190 -0
- data/spec/unit/config_spec.rb +112 -0
- data/spec/unit/elevators/domain_spec.rb +32 -0
- data/spec/unit/elevators/first_subdomain_spec.rb +24 -0
- data/spec/unit/elevators/generic_spec.rb +54 -0
- data/spec/unit/elevators/host_hash_spec.rb +32 -0
- data/spec/unit/elevators/host_spec.rb +89 -0
- data/spec/unit/elevators/subdomain_spec.rb +76 -0
- data/spec/unit/migrator_spec.rb +77 -0
- data/spec/unit/reloader_spec.rb +24 -0
- metadata +487 -0
@@ -0,0 +1,234 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
shared_examples_for "a schema based apartment adapter" do
|
4
|
+
include Apartment::Spec::AdapterRequirements
|
5
|
+
|
6
|
+
let(:schema1){ db1 }
|
7
|
+
let(:schema2){ db2 }
|
8
|
+
let(:public_schema){ default_tenant }
|
9
|
+
|
10
|
+
describe "#init" do
|
11
|
+
|
12
|
+
before do
|
13
|
+
Apartment.configure do |config|
|
14
|
+
config.excluded_models = ["Company"]
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
after do
|
19
|
+
# Apartment::Tenant.init creates per model connection.
|
20
|
+
# Remove the connection after testing not to unintentionally keep the connection across tests.
|
21
|
+
Apartment.excluded_models.each do |excluded_model|
|
22
|
+
excluded_model.constantize.remove_connection
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should process model exclusions" do
|
27
|
+
Apartment::Tenant.init
|
28
|
+
|
29
|
+
expect(Company.table_name).to eq("public.companies")
|
30
|
+
end
|
31
|
+
|
32
|
+
context "with a default_schema", :default_schema => true do
|
33
|
+
|
34
|
+
it "should set the proper table_name on excluded_models" do
|
35
|
+
Apartment::Tenant.init
|
36
|
+
|
37
|
+
expect(Company.table_name).to eq("#{default_schema}.companies")
|
38
|
+
end
|
39
|
+
|
40
|
+
it 'sets the search_path correctly' do
|
41
|
+
Apartment::Tenant.init
|
42
|
+
|
43
|
+
expect(User.connection.schema_search_path).to match(%r|#{default_schema}|)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
context "persistent_schemas", :persistent_schemas => true do
|
48
|
+
it "sets the persistent schemas in the schema_search_path" do
|
49
|
+
Apartment::Tenant.init
|
50
|
+
expect(connection.schema_search_path).to end_with persistent_schemas.map { |schema| %{"#{schema}"} }.join(', ')
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
#
|
56
|
+
# Creates happen already in our before_filter
|
57
|
+
#
|
58
|
+
describe "#create" do
|
59
|
+
|
60
|
+
it "should load schema.rb to new schema" do
|
61
|
+
connection.schema_search_path = schema1
|
62
|
+
expect(connection.tables).to include('companies')
|
63
|
+
end
|
64
|
+
|
65
|
+
it "should yield to block if passed and reset" do
|
66
|
+
subject.drop(schema2) # so we don't get errors on creation
|
67
|
+
|
68
|
+
@count = 0 # set our variable so its visible in and outside of blocks
|
69
|
+
|
70
|
+
subject.create(schema2) do
|
71
|
+
@count = User.count
|
72
|
+
expect(connection.schema_search_path).to start_with %{"#{schema2}"}
|
73
|
+
User.create
|
74
|
+
end
|
75
|
+
|
76
|
+
expect(connection.schema_search_path).not_to start_with %{"#{schema2}"}
|
77
|
+
|
78
|
+
subject.switch(schema2){ expect(User.count).to eq(@count + 1) }
|
79
|
+
end
|
80
|
+
|
81
|
+
context "numeric database names" do
|
82
|
+
let(:db){ 1234 }
|
83
|
+
it "should allow them" do
|
84
|
+
expect {
|
85
|
+
subject.create(db)
|
86
|
+
}.to_not raise_error
|
87
|
+
expect(tenant_names).to include(db.to_s)
|
88
|
+
end
|
89
|
+
|
90
|
+
after{ subject.drop(db) }
|
91
|
+
end
|
92
|
+
|
93
|
+
end
|
94
|
+
|
95
|
+
describe "#drop" do
|
96
|
+
it "should raise an error for unknown database" do
|
97
|
+
expect {
|
98
|
+
subject.drop "unknown_database"
|
99
|
+
}.to raise_error(Apartment::TenantNotFound)
|
100
|
+
end
|
101
|
+
|
102
|
+
context "numeric database names" do
|
103
|
+
let(:db){ 1234 }
|
104
|
+
|
105
|
+
it "should be able to drop them" do
|
106
|
+
subject.create(db)
|
107
|
+
expect {
|
108
|
+
subject.drop(db)
|
109
|
+
}.to_not raise_error
|
110
|
+
expect(tenant_names).not_to include(db.to_s)
|
111
|
+
end
|
112
|
+
|
113
|
+
after { subject.drop(db) rescue nil }
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
describe "#switch" do
|
118
|
+
it "connects and resets" do
|
119
|
+
subject.switch(schema1) do
|
120
|
+
expect(connection.schema_search_path).to start_with %{"#{schema1}"}
|
121
|
+
end
|
122
|
+
|
123
|
+
expect(connection.schema_search_path).to start_with %{"#{public_schema}"}
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
describe "#reset" do
|
128
|
+
it "should reset connection" do
|
129
|
+
subject.switch!(schema1)
|
130
|
+
subject.reset
|
131
|
+
expect(connection.schema_search_path).to start_with %{"#{public_schema}"}
|
132
|
+
end
|
133
|
+
|
134
|
+
context "with default_schema", :default_schema => true do
|
135
|
+
it "should reset to the default schema" do
|
136
|
+
subject.switch!(schema1)
|
137
|
+
subject.reset
|
138
|
+
expect(connection.schema_search_path).to start_with %{"#{default_schema}"}
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
context "persistent_schemas", :persistent_schemas => true do
|
143
|
+
before do
|
144
|
+
subject.switch!(schema1)
|
145
|
+
subject.reset
|
146
|
+
end
|
147
|
+
|
148
|
+
it "maintains the persistent schemas in the schema_search_path" do
|
149
|
+
expect(connection.schema_search_path).to end_with persistent_schemas.map { |schema| %{"#{schema}"} }.join(', ')
|
150
|
+
end
|
151
|
+
|
152
|
+
context "with default_schema", :default_schema => true do
|
153
|
+
it "prioritizes the switched schema to front of schema_search_path" do
|
154
|
+
subject.reset # need to re-call this as the default_schema wasn't set at the time that the above reset ran
|
155
|
+
expect(connection.schema_search_path).to start_with %{"#{default_schema}"}
|
156
|
+
end
|
157
|
+
end
|
158
|
+
end
|
159
|
+
end
|
160
|
+
|
161
|
+
describe "#switch!" do
|
162
|
+
it "should connect to new schema" do
|
163
|
+
subject.switch!(schema1)
|
164
|
+
expect(connection.schema_search_path).to start_with %{"#{schema1}"}
|
165
|
+
end
|
166
|
+
|
167
|
+
it "should reset connection if database is nil" do
|
168
|
+
subject.switch!
|
169
|
+
expect(connection.schema_search_path).to eq(%{"#{public_schema}"})
|
170
|
+
end
|
171
|
+
|
172
|
+
it "should raise an error if schema is invalid" do
|
173
|
+
expect {
|
174
|
+
subject.switch! 'unknown_schema'
|
175
|
+
}.to raise_error(Apartment::TenantNotFound)
|
176
|
+
end
|
177
|
+
|
178
|
+
context "numeric databases" do
|
179
|
+
let(:db){ 1234 }
|
180
|
+
|
181
|
+
it "should connect to them" do
|
182
|
+
subject.create(db)
|
183
|
+
expect {
|
184
|
+
subject.switch!(db)
|
185
|
+
}.to_not raise_error
|
186
|
+
|
187
|
+
expect(connection.schema_search_path).to start_with %{"#{db.to_s}"}
|
188
|
+
end
|
189
|
+
|
190
|
+
after{ subject.drop(db) }
|
191
|
+
end
|
192
|
+
|
193
|
+
describe "with default_schema specified", :default_schema => true do
|
194
|
+
before do
|
195
|
+
subject.switch!(schema1)
|
196
|
+
end
|
197
|
+
|
198
|
+
it "should switch out the default schema rather than public" do
|
199
|
+
expect(connection.schema_search_path).not_to include default_schema
|
200
|
+
end
|
201
|
+
|
202
|
+
it "should still switch to the switched schema" do
|
203
|
+
expect(connection.schema_search_path).to start_with %{"#{schema1}"}
|
204
|
+
end
|
205
|
+
end
|
206
|
+
|
207
|
+
context "persistent_schemas", :persistent_schemas => true do
|
208
|
+
|
209
|
+
before{ subject.switch!(schema1) }
|
210
|
+
|
211
|
+
it "maintains the persistent schemas in the schema_search_path" do
|
212
|
+
expect(connection.schema_search_path).to end_with persistent_schemas.map { |schema| %{"#{schema}"} }.join(', ')
|
213
|
+
end
|
214
|
+
|
215
|
+
it "prioritizes the switched schema to front of schema_search_path" do
|
216
|
+
expect(connection.schema_search_path).to start_with %{"#{schema1}"}
|
217
|
+
end
|
218
|
+
end
|
219
|
+
end
|
220
|
+
|
221
|
+
describe "#current" do
|
222
|
+
it "should return the current schema name" do
|
223
|
+
subject.switch!(schema1)
|
224
|
+
expect(subject.current).to eq(schema1)
|
225
|
+
end
|
226
|
+
|
227
|
+
context "persistent_schemas", :persistent_schemas => true do
|
228
|
+
it "should exlude persistent_schemas" do
|
229
|
+
subject.switch!(schema1)
|
230
|
+
expect(subject.current).to eq(schema1)
|
231
|
+
end
|
232
|
+
end
|
233
|
+
end
|
234
|
+
end
|
@@ -0,0 +1,107 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
describe "apartment rake tasks", database: :postgresql do
|
5
|
+
|
6
|
+
before do
|
7
|
+
@rake = Rake::Application.new
|
8
|
+
Rake.application = @rake
|
9
|
+
Dummy::Application.load_tasks
|
10
|
+
|
11
|
+
# rails tasks running F up the schema...
|
12
|
+
Rake::Task.define_task('db:migrate')
|
13
|
+
Rake::Task.define_task('db:seed')
|
14
|
+
Rake::Task.define_task('db:rollback')
|
15
|
+
Rake::Task.define_task('db:migrate:up')
|
16
|
+
Rake::Task.define_task('db:migrate:down')
|
17
|
+
Rake::Task.define_task('db:migrate:redo')
|
18
|
+
|
19
|
+
Apartment.configure do |config|
|
20
|
+
config.use_schemas = true
|
21
|
+
config.excluded_models = ["Company"]
|
22
|
+
config.tenant_names = lambda{ Company.pluck(:database) }
|
23
|
+
end
|
24
|
+
Apartment::Tenant.reload!(config)
|
25
|
+
|
26
|
+
# fix up table name of shared/excluded models
|
27
|
+
Company.table_name = 'public.companies'
|
28
|
+
end
|
29
|
+
|
30
|
+
after { Rake.application = nil }
|
31
|
+
|
32
|
+
context "with x number of databases" do
|
33
|
+
|
34
|
+
let(:x){ 1 + rand(5) } # random number of dbs to create
|
35
|
+
let(:db_names){ x.times.map{ Apartment::Test.next_db } }
|
36
|
+
let!(:company_count){ db_names.length }
|
37
|
+
|
38
|
+
before do
|
39
|
+
db_names.collect do |db_name|
|
40
|
+
Apartment::Tenant.create(db_name)
|
41
|
+
Company.create :database => db_name
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
after do
|
46
|
+
db_names.each{ |db| Apartment::Tenant.drop(db) }
|
47
|
+
Company.delete_all
|
48
|
+
end
|
49
|
+
|
50
|
+
context "with ActiveRecord below 5.2.0" do
|
51
|
+
before do
|
52
|
+
allow(ActiveRecord::Migrator).to receive(:migrations_paths) { %w(spec/dummy/db/migrate) }
|
53
|
+
allow(Apartment::Migrator).to receive(:activerecord_below_5_2?) { true }
|
54
|
+
end
|
55
|
+
|
56
|
+
describe "#migrate" do
|
57
|
+
it "should migrate all databases" do
|
58
|
+
expect(ActiveRecord::Migrator).to receive(:migrate).exactly(company_count).times
|
59
|
+
|
60
|
+
@rake['apartment:migrate'].invoke
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
describe "#rollback" do
|
65
|
+
it "should rollback all dbs" do
|
66
|
+
expect(ActiveRecord::Migrator).to receive(:rollback).exactly(company_count).times
|
67
|
+
|
68
|
+
@rake['apartment:rollback'].invoke
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
context "with ActiveRecord above or equal to 5.2.0" do
|
74
|
+
let(:migration_context_double) { double(:migration_context) }
|
75
|
+
|
76
|
+
before do
|
77
|
+
allow(Apartment::Migrator).to receive(:activerecord_below_5_2?) { false }
|
78
|
+
end
|
79
|
+
|
80
|
+
describe "#migrate" do
|
81
|
+
it "should migrate all databases" do
|
82
|
+
allow(ActiveRecord::Base.connection).to receive(:migration_context) { migration_context_double }
|
83
|
+
expect(migration_context_double).to receive(:migrate).exactly(company_count).times
|
84
|
+
|
85
|
+
@rake['apartment:migrate'].invoke
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
describe "#rollback" do
|
90
|
+
it "should rollback all dbs" do
|
91
|
+
allow(ActiveRecord::Base.connection).to receive(:migration_context) { migration_context_double }
|
92
|
+
expect(migration_context_double).to receive(:rollback).exactly(company_count).times
|
93
|
+
|
94
|
+
@rake['apartment:rollback'].invoke
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
describe "apartment:seed" do
|
100
|
+
it "should seed all databases" do
|
101
|
+
expect(Apartment::Tenant).to receive(:seed).exactly(company_count).times
|
102
|
+
|
103
|
+
@rake['apartment:seed'].invoke
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
@@ -0,0 +1,81 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'query caching' do
|
4
|
+
describe 'when use_schemas = true' do
|
5
|
+
let(:db_names) { [db1, db2] }
|
6
|
+
|
7
|
+
before do
|
8
|
+
Apartment.configure do |config|
|
9
|
+
config.excluded_models = ["Company"]
|
10
|
+
config.tenant_names = lambda{ Company.pluck(:database) }
|
11
|
+
config.use_schemas = true
|
12
|
+
end
|
13
|
+
|
14
|
+
Apartment::Tenant.reload!(config)
|
15
|
+
|
16
|
+
db_names.each do |db_name|
|
17
|
+
Apartment::Tenant.create(db_name)
|
18
|
+
Company.create database: db_name
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
after do
|
23
|
+
db_names.each{ |db| Apartment::Tenant.drop(db) }
|
24
|
+
Apartment::Tenant.reset
|
25
|
+
Company.delete_all
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'clears the ActiveRecord::QueryCache after switching databases' do
|
29
|
+
db_names.each do |db_name|
|
30
|
+
Apartment::Tenant.switch! db_name
|
31
|
+
User.create! name: db_name
|
32
|
+
end
|
33
|
+
|
34
|
+
ActiveRecord::Base.connection.enable_query_cache!
|
35
|
+
|
36
|
+
Apartment::Tenant.switch! db_names.first
|
37
|
+
expect(User.find_by_name(db_names.first).name).to eq(db_names.first)
|
38
|
+
|
39
|
+
Apartment::Tenant.switch! db_names.last
|
40
|
+
expect(User.find_by_name(db_names.first)).to be_nil
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
describe 'when use_schemas = false' do
|
45
|
+
let(:db_name) { db1 }
|
46
|
+
|
47
|
+
before do
|
48
|
+
Apartment.configure do |config|
|
49
|
+
config.excluded_models = ["Company"]
|
50
|
+
config.tenant_names = lambda{ Company.pluck(:database) }
|
51
|
+
config.use_schemas = false
|
52
|
+
end
|
53
|
+
|
54
|
+
Apartment::Tenant.reload!(config)
|
55
|
+
|
56
|
+
Apartment::Tenant.create(db_name)
|
57
|
+
Company.create database: db_name
|
58
|
+
end
|
59
|
+
|
60
|
+
after do
|
61
|
+
# Avoid cannot drop the currently open database. Maybe there is a better way to handle this.
|
62
|
+
Apartment::Tenant.switch! 'template1'
|
63
|
+
|
64
|
+
Apartment::Tenant.drop(db_name)
|
65
|
+
Apartment::Tenant.reset
|
66
|
+
Company.delete_all
|
67
|
+
end
|
68
|
+
|
69
|
+
it "configuration value is kept after switching databases" do
|
70
|
+
ActiveRecord::Base.connection.enable_query_cache!
|
71
|
+
|
72
|
+
Apartment::Tenant.switch! db_name
|
73
|
+
expect(Apartment.connection.query_cache_enabled).to be true
|
74
|
+
|
75
|
+
ActiveRecord::Base.connection.disable_query_cache!
|
76
|
+
|
77
|
+
Apartment::Tenant.switch! db_name
|
78
|
+
expect(Apartment.connection.query_cache_enabled).to be false
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
describe 'using apartment within an engine' do
|
2
|
+
|
3
|
+
before do
|
4
|
+
engine_path = Pathname.new(File.expand_path('../../dummy_engine', __FILE__))
|
5
|
+
require engine_path.join('test/dummy/config/application')
|
6
|
+
@rake = Rake::Application.new
|
7
|
+
Rake.application = @rake
|
8
|
+
stub_const 'APP_RAKEFILE', engine_path.join('test/dummy/Rakefile')
|
9
|
+
load 'rails/tasks/engine.rake'
|
10
|
+
end
|
11
|
+
|
12
|
+
it 'sucessfully runs rake db:migrate in the engine root' do
|
13
|
+
expect{ Rake::Task['db:migrate'].invoke }.to_not raise_error
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'sucessfully runs rake app:db:migrate in the engine root' do
|
17
|
+
expect{ Rake::Task['app:db:migrate'].invoke }.to_not raise_error
|
18
|
+
end
|
19
|
+
|
20
|
+
context 'when Apartment.db_migrate_tenants is false' do
|
21
|
+
it 'should not enhance tasks' do
|
22
|
+
Apartment.db_migrate_tenants = false
|
23
|
+
expect(Apartment::RakeTaskEnhancer).to_not receive(:enhance_task).with('db:migrate')
|
24
|
+
Rake::Task['db:migrate'].invoke
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|