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.
- checksums.yaml +4 -4
- data/.rubocop.yml +5 -0
- data/.rubocop_todo.yml +1 -1
- data/.story_branch.yml +1 -0
- data/.travis.yml +13 -30
- data/Appraisals +4 -17
- data/Gemfile +2 -2
- data/Guardfile +3 -1
- data/HISTORY.md +90 -0
- data/README.md +31 -16
- data/Rakefile +34 -22
- data/TODO.md +0 -1
- data/gemfiles/rails_5_0.gemfile +10 -11
- data/gemfiles/rails_5_1.gemfile +10 -11
- data/gemfiles/rails_5_2.gemfile +9 -10
- data/gemfiles/rails_6_0.gemfile +10 -11
- data/gemfiles/rails_master.gemfile +10 -11
- data/lib/apartment.rb +31 -13
- data/lib/apartment/active_record/connection_handling.rb +17 -0
- data/lib/apartment/active_record/internal_metadata.rb +11 -0
- data/lib/apartment/active_record/log_subscriber.rb +41 -0
- data/lib/apartment/active_record/schema_migration.rb +13 -0
- data/lib/apartment/adapters/abstract_adapter.rb +17 -13
- data/lib/apartment/adapters/jdbc_postgresql_adapter.rb +9 -4
- data/lib/apartment/adapters/mysql2_adapter.rb +2 -0
- data/lib/apartment/adapters/postgresql_adapter.rb +33 -4
- data/lib/apartment/adapters/sqlite3_adapter.rb +2 -0
- data/lib/apartment/console.rb +7 -0
- data/lib/apartment/custom_console.rb +23 -7
- data/lib/apartment/model.rb +27 -0
- data/lib/apartment/railtie.rb +19 -15
- data/lib/apartment/tasks/task_helper.rb +35 -0
- data/lib/apartment/tenant.rb +16 -5
- data/lib/apartment/version.rb +1 -1
- data/lib/generators/apartment/install/templates/apartment.rb +5 -0
- data/lib/tasks/apartment.rake +16 -37
- data/{apartment.gemspec → ros-apartment.gemspec} +12 -6
- metadata +16 -213
- data/.github/workflows/.rubocop-linter.yml +0 -22
- data/spec/adapters/jdbc_mysql_adapter_spec.rb +0 -20
- data/spec/adapters/jdbc_postgresql_adapter_spec.rb +0 -39
- data/spec/adapters/mysql2_adapter_spec.rb +0 -61
- data/spec/adapters/postgresql_adapter_spec.rb +0 -63
- data/spec/adapters/sqlite3_adapter_spec.rb +0 -101
- data/spec/apartment_spec.rb +0 -13
- data/spec/config/database.yml.sample +0 -49
- data/spec/dummy/Rakefile +0 -7
- data/spec/dummy/app/controllers/application_controller.rb +0 -7
- data/spec/dummy/app/helpers/application_helper.rb +0 -4
- data/spec/dummy/app/models/application_record.rb +0 -6
- data/spec/dummy/app/models/company.rb +0 -5
- data/spec/dummy/app/models/user.rb +0 -5
- data/spec/dummy/app/views/application/index.html.erb +0 -1
- data/spec/dummy/app/views/layouts/application.html.erb +0 -14
- data/spec/dummy/config.ru +0 -6
- data/spec/dummy/config/application.rb +0 -51
- data/spec/dummy/config/boot.rb +0 -13
- data/spec/dummy/config/database.yml.sample +0 -44
- data/spec/dummy/config/environment.rb +0 -7
- data/spec/dummy/config/environments/development.rb +0 -29
- data/spec/dummy/config/environments/production.rb +0 -53
- data/spec/dummy/config/environments/test.rb +0 -36
- data/spec/dummy/config/initializers/apartment.rb +0 -6
- data/spec/dummy/config/initializers/backtrace_silencers.rb +0 -8
- data/spec/dummy/config/initializers/inflections.rb +0 -11
- data/spec/dummy/config/initializers/mime_types.rb +0 -6
- data/spec/dummy/config/initializers/secret_token.rb +0 -9
- data/spec/dummy/config/initializers/session_store.rb +0 -10
- data/spec/dummy/config/locales/en.yml +0 -5
- data/spec/dummy/config/routes.rb +0 -5
- data/spec/dummy/db/migrate/20110613152810_create_dummy_models.rb +0 -39
- data/spec/dummy/db/migrate/20111202022214_create_table_books.rb +0 -14
- data/spec/dummy/db/migrate/20180415260934_create_public_tokens.rb +0 -13
- data/spec/dummy/db/schema.rb +0 -55
- data/spec/dummy/db/seeds.rb +0 -5
- data/spec/dummy/db/seeds/import.rb +0 -7
- data/spec/dummy/public/404.html +0 -26
- data/spec/dummy/public/422.html +0 -26
- data/spec/dummy/public/500.html +0 -26
- data/spec/dummy/public/favicon.ico +0 -0
- data/spec/dummy/public/stylesheets/.gitkeep +0 -0
- data/spec/dummy/script/rails +0 -8
- data/spec/dummy_engine/.gitignore +0 -8
- data/spec/dummy_engine/Gemfile +0 -15
- data/spec/dummy_engine/Rakefile +0 -34
- data/spec/dummy_engine/bin/rails +0 -12
- data/spec/dummy_engine/config/initializers/apartment.rb +0 -52
- data/spec/dummy_engine/dummy_engine.gemspec +0 -24
- data/spec/dummy_engine/lib/dummy_engine.rb +0 -6
- data/spec/dummy_engine/lib/dummy_engine/engine.rb +0 -6
- data/spec/dummy_engine/lib/dummy_engine/version.rb +0 -5
- data/spec/dummy_engine/test/dummy/Rakefile +0 -6
- data/spec/dummy_engine/test/dummy/config.ru +0 -6
- data/spec/dummy_engine/test/dummy/config/application.rb +0 -24
- data/spec/dummy_engine/test/dummy/config/boot.rb +0 -7
- data/spec/dummy_engine/test/dummy/config/database.yml +0 -25
- data/spec/dummy_engine/test/dummy/config/environment.rb +0 -7
- data/spec/dummy_engine/test/dummy/config/environments/development.rb +0 -39
- data/spec/dummy_engine/test/dummy/config/environments/production.rb +0 -80
- data/spec/dummy_engine/test/dummy/config/environments/test.rb +0 -41
- data/spec/dummy_engine/test/dummy/config/initializers/assets.rb +0 -10
- data/spec/dummy_engine/test/dummy/config/initializers/backtrace_silencers.rb +0 -8
- data/spec/dummy_engine/test/dummy/config/initializers/cookies_serializer.rb +0 -5
- data/spec/dummy_engine/test/dummy/config/initializers/filter_parameter_logging.rb +0 -6
- data/spec/dummy_engine/test/dummy/config/initializers/inflections.rb +0 -17
- data/spec/dummy_engine/test/dummy/config/initializers/mime_types.rb +0 -5
- data/spec/dummy_engine/test/dummy/config/initializers/session_store.rb +0 -5
- data/spec/dummy_engine/test/dummy/config/initializers/wrap_parameters.rb +0 -16
- data/spec/dummy_engine/test/dummy/config/locales/en.yml +0 -23
- data/spec/dummy_engine/test/dummy/config/routes.rb +0 -58
- data/spec/dummy_engine/test/dummy/config/secrets.yml +0 -22
- data/spec/examples/connection_adapter_examples.rb +0 -44
- data/spec/examples/generic_adapter_custom_configuration_example.rb +0 -93
- data/spec/examples/generic_adapter_examples.rb +0 -164
- data/spec/examples/schema_adapter_examples.rb +0 -239
- data/spec/integration/apartment_rake_integration_spec.rb +0 -107
- data/spec/integration/query_caching_spec.rb +0 -83
- data/spec/integration/use_within_an_engine_spec.rb +0 -28
- data/spec/schemas/v1.rb +0 -15
- data/spec/schemas/v2.rb +0 -41
- data/spec/schemas/v3.rb +0 -47
- data/spec/spec_helper.rb +0 -63
- data/spec/support/apartment_helpers.rb +0 -47
- data/spec/support/capybara_sessions.rb +0 -15
- data/spec/support/config.rb +0 -13
- data/spec/support/contexts.rb +0 -54
- data/spec/support/requirements.rb +0 -48
- data/spec/support/setup.rb +0 -46
- data/spec/tasks/apartment_rake_spec.rb +0 -124
- data/spec/tenant_spec.rb +0 -194
- data/spec/unit/config_spec.rb +0 -111
- data/spec/unit/elevators/domain_spec.rb +0 -33
- data/spec/unit/elevators/first_subdomain_spec.rb +0 -26
- data/spec/unit/elevators/generic_spec.rb +0 -55
- data/spec/unit/elevators/host_hash_spec.rb +0 -33
- data/spec/unit/elevators/host_spec.rb +0 -89
- data/spec/unit/elevators/subdomain_spec.rb +0 -77
- data/spec/unit/migrator_spec.rb +0 -78
- data/spec/unit/reloader_spec.rb +0 -24
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f2dcb62b879236f8522148786e9ef35dc10b59be747f5e4cd0e9b0d515add9de
|
4
|
+
data.tar.gz: 33cc4f75fbe99a1ec29e6370cee9a83cd9a8ee59b0657ada7ce6f2b0cbd87524
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3b4fbc349f3d9198c85914667335662c7d3bc8c64a1c6fa0397b417e4d451247eeb18e44a6949d2f79647889a20542f64c0b25de2d700cb9cf6459e2a2120d80
|
7
|
+
data.tar.gz: 4b5057afb829f973b0234a04136f0f470b38f7b52e2dc96ccdc7d88f70c1d949c06b5195d41da5a74b6460cd23c5a3b0393dc38be21eb5d69d66caa3507746b6
|
data/.rubocop.yml
CHANGED
data/.rubocop_todo.yml
CHANGED
@@ -21,7 +21,7 @@ Metrics/BlockLength:
|
|
21
21
|
# Configuration parameters: AutoCorrect, AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
|
22
22
|
# URISchemes: http, https
|
23
23
|
Metrics/LineLength:
|
24
|
-
Max:
|
24
|
+
Max: 200
|
25
25
|
|
26
26
|
# Offense count: 4
|
27
27
|
# Configuration parameters: CountComments, ExcludedMethods.
|
data/.story_branch.yml
CHANGED
data/.travis.yml
CHANGED
@@ -5,12 +5,11 @@ services:
|
|
5
5
|
- docker
|
6
6
|
rvm:
|
7
7
|
- jruby-9.2.11.0
|
8
|
-
- 2.
|
9
|
-
- 2.
|
10
|
-
- 2.
|
11
|
-
- 2.
|
12
|
-
-
|
13
|
-
- 2.6.2
|
8
|
+
- 2.4.10
|
9
|
+
- 2.5.8
|
10
|
+
- 2.6.6
|
11
|
+
- 2.7.1
|
12
|
+
- jruby-head
|
14
13
|
- ruby-head
|
15
14
|
|
16
15
|
branches:
|
@@ -19,48 +18,32 @@ branches:
|
|
19
18
|
- development
|
20
19
|
|
21
20
|
gemfile:
|
22
|
-
- gemfiles/rails_4_2.gemfile
|
23
21
|
- gemfiles/rails_5_0.gemfile
|
24
22
|
- gemfiles/rails_5_1.gemfile
|
25
23
|
- gemfiles/rails_5_2.gemfile
|
26
24
|
- gemfiles/rails_6_0.gemfile
|
27
25
|
- gemfiles/rails_master.gemfile
|
26
|
+
|
28
27
|
bundler_args: --without local
|
29
28
|
before_install:
|
30
29
|
- sudo /etc/init.d/mysql stop
|
31
30
|
- sudo /etc/init.d/postgresql stop
|
32
31
|
- docker-compose up -d
|
33
|
-
|
34
|
-
- gem uninstall bundler -v '>= 2' -x || true
|
35
|
-
- gem install bundler -v '< 2'
|
32
|
+
|
36
33
|
env:
|
37
34
|
RUBY_GC_MALLOC_LIMIT: 90000000
|
38
35
|
RUBY_GC_HEAP_FREE_SLOTS: 200000
|
39
36
|
jobs:
|
37
|
+
include:
|
38
|
+
- name: Rubocop Lint
|
39
|
+
script: gem install perx-rubocop && rubocop
|
40
|
+
|
40
41
|
allow_failures:
|
41
42
|
- rvm: ruby-head
|
43
|
+
- rvm: jruby-head
|
42
44
|
- gemfile: gemfiles/rails_master.gemfile
|
43
|
-
- rvm: 2.2.9
|
44
|
-
gemfile: gemfiles/rails_5_2.gemfile
|
45
|
-
# JRuby does not provide support for anything below rails 5
|
46
|
-
- rvm: jruby-9.2.11.0
|
47
|
-
gemfile: gemfiles/rails_4_2.gemfile
|
48
45
|
exclude:
|
49
|
-
- rvm: 2.
|
50
|
-
gemfile: gemfiles/rails_5_0.gemfile
|
51
|
-
- rvm: 2.1.9
|
52
|
-
gemfile: gemfiles/rails_5_1.gemfile
|
53
|
-
- rvm: 2.1.9
|
54
|
-
gemfile: gemfiles/rails_5_2.gemfile
|
55
|
-
- rvm: 2.1.9
|
56
|
-
gemfile: gemfiles/rails_6_0.gemfile
|
57
|
-
- rvm: 2.1.9
|
58
|
-
gemfile: gemfiles/rails_master.gemfile
|
59
|
-
- rvm: 2.2.9
|
60
|
-
gemfile: gemfiles/rails_6_0.gemfile
|
61
|
-
- rvm: 2.3.6
|
62
|
-
gemfile: gemfiles/rails_6_0.gemfile
|
63
|
-
- rvm: 2.4.3
|
46
|
+
- rvm: 2.4.10
|
64
47
|
gemfile: gemfiles/rails_6_0.gemfile
|
65
48
|
fast_finish: true
|
66
49
|
cache: bundler
|
data/Appraisals
CHANGED
@@ -1,18 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
appraise 'rails-4-2' do
|
4
|
-
gem 'rails', '~> 4.2.0'
|
5
|
-
platforms :ruby do
|
6
|
-
gem 'pg', '< 1.0.0'
|
7
|
-
gem 'mysql2', '~> 0.4.0'
|
8
|
-
end
|
9
|
-
platforms :jruby do
|
10
|
-
gem 'activerecord-jdbc-adapter', '~> 1.3'
|
11
|
-
gem 'activerecord-jdbcpostgresql-adapter', '~> 1.3'
|
12
|
-
gem 'activerecord-jdbcmysql-adapter', '~> 1.3'
|
13
|
-
end
|
14
|
-
end
|
15
|
-
|
16
3
|
appraise 'rails-5-0' do
|
17
4
|
gem 'rails', '~> 5.0.0'
|
18
5
|
platforms :ruby do
|
@@ -47,14 +34,14 @@ appraise 'rails-5-2' do
|
|
47
34
|
end
|
48
35
|
|
49
36
|
appraise 'rails-6-0' do
|
50
|
-
gem 'rails', '~> 6.0.0
|
37
|
+
gem 'rails', '~> 6.0.0'
|
51
38
|
platforms :ruby do
|
52
39
|
gem 'sqlite3', '~> 1.4'
|
53
40
|
end
|
54
41
|
platforms :jruby do
|
55
|
-
gem 'activerecord-jdbc-adapter', '~> 60.0
|
56
|
-
gem 'activerecord-jdbcpostgresql-adapter', '~> 60.0
|
57
|
-
gem 'activerecord-jdbcmysql-adapter', '~> 60.0
|
42
|
+
gem 'activerecord-jdbc-adapter', '~> 60.0'
|
43
|
+
gem 'activerecord-jdbcpostgresql-adapter', '~> 60.0'
|
44
|
+
gem 'activerecord-jdbcmysql-adapter', '~> 60.0'
|
58
45
|
end
|
59
46
|
end
|
60
47
|
|
data/Gemfile
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
source '
|
3
|
+
source 'https://my.diffend.io/protect/gems'
|
4
4
|
|
5
5
|
gemspec
|
6
6
|
|
7
|
-
gem 'rails', '>= 3.1.2'
|
8
7
|
gem 'perx-rubocop', '~> 0.0.3'
|
8
|
+
gem 'rails', '>= 3.1.2'
|
9
9
|
|
10
10
|
group :local do
|
11
11
|
gem 'guard-rspec', '~> 4.2'
|
data/Guardfile
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
# A sample Guardfile
|
2
4
|
# More info at https://github.com/guard/guard#readme
|
3
5
|
|
@@ -5,7 +7,7 @@ guard :rspec do
|
|
5
7
|
watch(%r{^spec/.+_spec\.rb$})
|
6
8
|
watch(%r{^lib/apartment/(.+)\.rb$}) { |m| "spec/unit/#{m[1]}_spec.rb" }
|
7
9
|
watch(%r{^lib/apartment/(.+)\.rb$}) { |m| "spec/integration/#{m[1]}_spec.rb" }
|
8
|
-
watch('spec/spec_helper.rb')
|
10
|
+
watch('spec/spec_helper.rb') { 'spec' }
|
9
11
|
|
10
12
|
# # Rails example
|
11
13
|
# watch(%r{^app/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
|
data/HISTORY.md
CHANGED
@@ -1,3 +1,93 @@
|
|
1
|
+
# 2.7.1
|
2
|
+
|
3
|
+
# Enhancements
|
4
|
+
|
5
|
+
- N/a
|
6
|
+
|
7
|
+
# Bugfixes
|
8
|
+
|
9
|
+
- [Resolves #82] Enhanced db:create breaks plugin compatibility - <https://github.com/rails-on-services/apartment/pull/83>
|
10
|
+
|
11
|
+
# Chores
|
12
|
+
|
13
|
+
- Update rake version in development
|
14
|
+
- Renamed gemspec to match gem name
|
15
|
+
|
16
|
+
# 2.7.0
|
17
|
+
|
18
|
+
# Enhancements
|
19
|
+
|
20
|
+
- [Resolves #70] Rake tasks define methods on main - <https://github.com/rails-on-services/apartment/pull/75>
|
21
|
+
- Add database and schema to active record log. Configurable, defaults to false to keep current behavior - <https://github.com/rails-on-services/apartment/pull/55>
|
22
|
+
|
23
|
+
# Bugfixes
|
24
|
+
|
25
|
+
- [Fixes #61] Fix database create in mysql - <https://github.com/rails-on-services/apartment/pull/76>
|
26
|
+
|
27
|
+
# Chores
|
28
|
+
|
29
|
+
- Remove deprecated tld_length config option: tld_length was removed in influitive#309, this configuration option doesn't have any effect now. - <https://github.com/rails-on-services/apartment/pull/72>
|
30
|
+
- Using [diffend.io proxy](https://diffend.io) to safely check required gems
|
31
|
+
- Added [story branch](https://github.com/story-branch/story_branch) to the configuration
|
32
|
+
- Using travis-ci to run rubocop as well, replacing github actions: github actions do not work in fork's PRs
|
33
|
+
|
34
|
+
# 2.6.1
|
35
|
+
|
36
|
+
## Enhancements
|
37
|
+
- N/a
|
38
|
+
|
39
|
+
## Bugfixes
|
40
|
+
- [Resolves influitive#607] Avoid early connection
|
41
|
+
- <https://github.com/rails-on-services/apartment/pull/39>
|
42
|
+
- <https://github.com/rails-on-services/apartment/pull/53>
|
43
|
+
- <https://github.com/rails-on-services/apartment/pull/51>
|
44
|
+
- [Resolves #52] Rake db:setup tries to seed non existent tenant - <https://github.com/rails-on-services/apartment/pull/54>
|
45
|
+
- [Resolves #56] DB rollback uses second last migration - <https://github.com/rails-on-services/apartment/pull/57>
|
46
|
+
|
47
|
+
## Chores
|
48
|
+
- N/a
|
49
|
+
|
50
|
+
# 2.6.0
|
51
|
+
|
52
|
+
## Enhancements
|
53
|
+
- [Resolves #26] Support configuration for skip checking of schema existence before switching
|
54
|
+
- [Resolves #41] Add tenant info to console boot
|
55
|
+
|
56
|
+
## Bugfixes
|
57
|
+
- [Resolves #37] Custom Console deprecation warning
|
58
|
+
- [Resolves #42] After switch callback not working with nil argument
|
59
|
+
|
60
|
+
## Chores
|
61
|
+
- Updated github actions configuration to run on PRs as well
|
62
|
+
|
63
|
+
# 2.5.0
|
64
|
+
|
65
|
+
## Enhancements
|
66
|
+
- [Resolves #6] Adds support for rails 6.1
|
67
|
+
- [Resolves #27] Adds support to not rely on set search path, but instead prepends the schema name to the table name when using postgresql with schemas.
|
68
|
+
- [Resolves #35] Cache keys are now tenant dependent
|
69
|
+
|
70
|
+
## Bugfixes
|
71
|
+
- [Resolves #27] Manually switching connection between read and write forgets the schema
|
72
|
+
|
73
|
+
## Chores
|
74
|
+
- [Resolves #31] Add latest ruby versions to test matrix
|
75
|
+
|
76
|
+
# 2.4.0
|
77
|
+
|
78
|
+
## Enhancements
|
79
|
+
- [Resolves #14] Add console info about tenants and fast switches #17
|
80
|
+
- Skip init if we're running webpacker:compile #18
|
81
|
+
|
82
|
+
## Bugfixes
|
83
|
+
- Don't crash when no database connection is present #16
|
84
|
+
- Rescuing ActiveRecord::NoDatabaseError when dropping tenants #19
|
85
|
+
|
86
|
+
## Chores
|
87
|
+
- Rakefile should use mysql port from configuration #5
|
88
|
+
- [Resolves #9] Cleanup rubocop todo #8
|
89
|
+
- Cleanup travis matrix #23
|
90
|
+
|
1
91
|
# 2.3.0
|
2
92
|
* January 3, 2020
|
3
93
|
|
data/README.md
CHANGED
@@ -324,6 +324,20 @@ Apartment.configure do |config|
|
|
324
324
|
end
|
325
325
|
```
|
326
326
|
|
327
|
+
### Skip tenant schema check
|
328
|
+
|
329
|
+
This is configurable by setting: `tenant_presence_check`. It defaults to true
|
330
|
+
in order to maintain the original gem behavior. This is only checked when using one of the PostgreSQL adapters.
|
331
|
+
The original gem behavior, when running `switch` would look for the existence of the schema before switching. This adds an extra query on every context switch. While in the default simple scenarios this is a valid check, in high volume platforms this adds some unnecessary overhead which can be detected in some other ways on the application level.
|
332
|
+
|
333
|
+
Setting this configuration value to `false` will disable the schema presence check before trying to switch the context.
|
334
|
+
|
335
|
+
```ruby
|
336
|
+
Apartment.configure do |config|
|
337
|
+
tenant_presence_check = false
|
338
|
+
end
|
339
|
+
```
|
340
|
+
|
327
341
|
### Excluding models
|
328
342
|
|
329
343
|
If you have some models that should always access the 'public' tenant, you can specify this by configuring Apartment using `Apartment.configure`. This will yield a config object for you. You can set excluded models like so:
|
@@ -341,17 +355,30 @@ Rails will always access the 'public' tenant when accessing these models, but no
|
|
341
355
|
|
342
356
|
### Postgresql Schemas
|
343
357
|
|
344
|
-
|
358
|
+
#### Alternative: Creating new schemas by using raw SQL dumps
|
359
|
+
|
360
|
+
Apartment can be forced to use raw SQL dumps insted of `schema.rb` for creating new schemas. Use this when you are using some extra features in postgres that can't be represented in `schema.rb`, like materialized views etc.
|
361
|
+
|
362
|
+
This only applies while using postgres adapter and `config.use_schemas` is set to `true`.
|
363
|
+
(Note: this option doesn't use `db/structure.sql`, it creates SQL dump by executing `pg_dump`)
|
364
|
+
|
365
|
+
Enable this option with:
|
366
|
+
|
367
|
+
```ruby
|
368
|
+
config.use_sql = true
|
369
|
+
```
|
370
|
+
|
371
|
+
### Providing a Different default_tenant
|
345
372
|
|
346
373
|
By default, ActiveRecord will use `"$user", public` as the default `schema_search_path`. This can be modified if you wish to use a different default schema be setting:
|
347
374
|
|
348
375
|
```ruby
|
349
|
-
config.
|
376
|
+
config.default_tenant = "some_other_schema"
|
350
377
|
```
|
351
378
|
|
352
379
|
With that set, all excluded models will use this schema as the table name prefix instead of `public` and `reset` on `Apartment::Tenant` will return to this schema as well.
|
353
380
|
|
354
|
-
|
381
|
+
### Persistent Schemas
|
355
382
|
|
356
383
|
Apartment will normally just switch the `schema_search_path` whole hog to the one passed in. This can lead to problems if you want other schemas to always be searched as well. Enter `persistent_schemas`. You can configure a list of other schemas that will always remain in the search path, while the default gets swapped out:
|
357
384
|
|
@@ -419,7 +446,7 @@ schema_search_path: "public,shared_extensions"
|
|
419
446
|
...
|
420
447
|
```
|
421
448
|
|
422
|
-
This would be for a config with `
|
449
|
+
This would be for a config with `default_tenant` set to `public` and `persistent_schemas` set to `['shared_extensions']`. **Note**: This only works on Heroku with [Rails 4.1+](https://devcenter.heroku.com/changelog-items/426). For apps that use older Rails versions hosted on Heroku, the only way to properly setup is to start with a fresh PostgreSQL instance:
|
423
450
|
|
424
451
|
1. Append `?schema_search_path=public,hstore` to your `DATABASE_URL` environment variable, by this you don't have to revise the `database.yml` file (which is impossible since Heroku regenerates a completely different and immutable `database.yml` of its own on each deploy)
|
425
452
|
2. Run `heroku pg:psql` from your command line
|
@@ -457,18 +484,6 @@ schema in the `search_path` at all times. We won't be able to do this though unt
|
|
457
484
|
also contain the tenanted tables, which is an open issue with no real milestone to be completed.
|
458
485
|
Happy to accept PR's on the matter.
|
459
486
|
|
460
|
-
#### Alternative: Creating new schemas by using raw SQL dumps
|
461
|
-
|
462
|
-
Apartment can be forced to use raw SQL dumps insted of `schema.rb` for creating new schemas. Use this when you are using some extra features in postgres that can't be represented in `schema.rb`, like materialized views etc.
|
463
|
-
|
464
|
-
This only applies while using postgres adapter and `config.use_schemas` is set to `true`.
|
465
|
-
(Note: this option doesn't use `db/structure.sql`, it creates SQL dump by executing `pg_dump`)
|
466
|
-
|
467
|
-
Enable this option with:
|
468
|
-
```ruby
|
469
|
-
config.use_sql = true
|
470
|
-
```
|
471
|
-
|
472
487
|
### Managing Migrations
|
473
488
|
|
474
489
|
In order to migrate all of your tenants (or postgresql schemas) you need to provide a list
|
data/Rakefile
CHANGED
@@ -1,19 +1,25 @@
|
|
1
|
-
|
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
|
8
|
-
require
|
13
|
+
require 'rspec'
|
14
|
+
require 'rspec/core/rake_task'
|
9
15
|
|
10
|
-
RSpec::Core::RakeTask.new(:
|
11
|
-
spec.pattern =
|
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
|
-
[
|
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 :
|
36
|
+
task default: :spec
|
31
37
|
|
32
38
|
namespace :db do
|
33
39
|
namespace :test do
|
34
|
-
task :
|
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, :
|
44
|
-
FileUtils.copy(rails_db_file + '.sample', 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
|
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 <<
|
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
|
-
|
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
|
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
|
-
|
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
|
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
|
@@ -86,12 +95,16 @@ namespace :mysql do
|
|
86
95
|
params << "-u #{my_config['username']}" if my_config['username']
|
87
96
|
params << "-p#{my_config['password']}" if my_config['password']
|
88
97
|
params << "--port #{my_config['port']}" if my_config['port']
|
89
|
-
|
98
|
+
begin
|
99
|
+
`mysqladmin #{params.join(' ')} create #{my_config['database']}`
|
100
|
+
rescue StandardError
|
101
|
+
'test db already exists'
|
102
|
+
end
|
90
103
|
ActiveRecord::Base.establish_connection my_config
|
91
104
|
migrate
|
92
105
|
end
|
93
106
|
|
94
|
-
desc
|
107
|
+
desc 'drop the MySQL test database'
|
95
108
|
task :drop_db do
|
96
109
|
puts "dropping database #{my_config['database']}"
|
97
110
|
params = []
|
@@ -99,12 +112,11 @@ namespace :mysql do
|
|
99
112
|
params << "-u #{my_config['username']}" if my_config['username']
|
100
113
|
params << "-p#{my_config['password']}" if my_config['password']
|
101
114
|
params << "--port #{my_config['port']}" if my_config['port']
|
102
|
-
|
115
|
+
`mysqladmin #{params.join(' ')} drop #{my_config['database']} --force`
|
103
116
|
end
|
104
|
-
|
105
117
|
end
|
106
118
|
|
107
|
-
# TODO clean this up
|
119
|
+
# TODO: clean this up
|
108
120
|
def config
|
109
121
|
Apartment::Test.config['connections']
|
110
122
|
end
|