ros-apartment 2.3.0.alpha1 → 3.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.pryrc +5 -3
- data/.rubocop.yml +36 -0
- data/.rubocop_todo.yml +439 -0
- data/.ruby-version +1 -0
- data/Appraisals +31 -52
- data/CHANGELOG.md +965 -0
- data/Gemfile +2 -7
- data/Guardfile +3 -16
- data/README.md +121 -44
- data/Rakefile +42 -31
- data/lib/apartment/active_record/connection_handling.rb +31 -0
- data/lib/apartment/active_record/internal_metadata.rb +9 -0
- data/lib/apartment/active_record/postgresql_adapter.rb +43 -0
- data/lib/apartment/active_record/schema_migration.rb +11 -0
- data/lib/apartment/adapters/abstract_adapter.rb +52 -46
- data/lib/apartment/adapters/abstract_jdbc_adapter.rb +5 -3
- data/lib/apartment/adapters/jdbc_mysql_adapter.rb +3 -3
- data/lib/apartment/adapters/jdbc_postgresql_adapter.rb +19 -13
- data/lib/apartment/adapters/mysql2_adapter.rb +15 -9
- data/lib/apartment/adapters/postgis_adapter.rb +3 -2
- data/lib/apartment/adapters/postgresql_adapter.rb +79 -31
- data/lib/apartment/adapters/sqlite3_adapter.rb +18 -8
- data/lib/apartment/adapters/trilogy_adapter.rb +29 -0
- data/lib/apartment/console.rb +23 -11
- data/lib/apartment/custom_console.rb +42 -0
- data/lib/apartment/deprecation.rb +2 -1
- data/lib/apartment/elevators/domain.rb +4 -3
- data/lib/apartment/elevators/first_subdomain.rb +3 -2
- data/lib/apartment/elevators/generic.rb +4 -3
- data/lib/apartment/elevators/host.rb +6 -1
- data/lib/apartment/elevators/host_hash.rb +6 -2
- data/lib/apartment/elevators/subdomain.rb +9 -5
- data/lib/apartment/log_subscriber.rb +45 -0
- data/lib/apartment/migrator.rb +7 -24
- data/lib/apartment/model.rb +29 -0
- data/lib/apartment/railtie.rb +21 -20
- data/lib/apartment/tasks/enhancements.rb +4 -6
- data/lib/apartment/tasks/task_helper.rb +52 -0
- data/lib/apartment/tenant.rb +7 -10
- data/lib/apartment/version.rb +3 -1
- data/lib/apartment.rb +46 -15
- data/lib/generators/apartment/install/install_generator.rb +4 -3
- data/lib/generators/apartment/install/templates/apartment.rb +10 -3
- data/lib/tasks/apartment.rake +48 -87
- data/ros-apartment.gemspec +65 -0
- metadata +181 -264
- data/.github/ISSUE_TEMPLATE.md +0 -21
- data/.travis.yml +0 -65
- data/HISTORY.md +0 -398
- data/TODO.md +0 -51
- data/apartment.gemspec +0 -46
- data/docker-compose.yml +0 -33
- data/gemfiles/rails_4_2.gemfile +0 -23
- data/gemfiles/rails_5_0.gemfile +0 -22
- data/gemfiles/rails_5_1.gemfile +0 -22
- data/gemfiles/rails_5_2.gemfile +0 -18
- data/gemfiles/rails_6_0.gemfile +0 -22
- data/gemfiles/rails_master.gemfile +0 -22
- data/lib/apartment/reloader.rb +0 -21
- data/spec/adapters/jdbc_mysql_adapter_spec.rb +0 -19
- data/spec/adapters/jdbc_postgresql_adapter_spec.rb +0 -41
- data/spec/adapters/mysql2_adapter_spec.rb +0 -59
- data/spec/adapters/postgresql_adapter_spec.rb +0 -61
- data/spec/adapters/sqlite3_adapter_spec.rb +0 -83
- data/spec/apartment_spec.rb +0 -11
- data/spec/config/database.yml.sample +0 -49
- data/spec/dummy/Rakefile +0 -7
- data/spec/dummy/app/controllers/application_controller.rb +0 -6
- data/spec/dummy/app/helpers/application_helper.rb +0 -2
- data/spec/dummy/app/models/company.rb +0 -3
- data/spec/dummy/app/models/user.rb +0 -3
- 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/application.rb +0 -49
- data/spec/dummy/config/boot.rb +0 -11
- data/spec/dummy/config/database.yml.sample +0 -44
- data/spec/dummy/config/environment.rb +0 -5
- data/spec/dummy/config/environments/development.rb +0 -28
- data/spec/dummy/config/environments/production.rb +0 -51
- data/spec/dummy/config/environments/test.rb +0 -34
- data/spec/dummy/config/initializers/apartment.rb +0 -4
- data/spec/dummy/config/initializers/backtrace_silencers.rb +0 -7
- data/spec/dummy/config/initializers/inflections.rb +0 -10
- data/spec/dummy/config/initializers/mime_types.rb +0 -5
- data/spec/dummy/config/initializers/secret_token.rb +0 -7
- data/spec/dummy/config/initializers/session_store.rb +0 -8
- data/spec/dummy/config/locales/en.yml +0 -5
- data/spec/dummy/config/routes.rb +0 -3
- data/spec/dummy/config.ru +0 -4
- 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/import.rb +0 -5
- data/spec/dummy/db/seeds.rb +0 -5
- data/spec/dummy/db/test.sqlite3 +0 -0
- 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 -6
- 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 -51
- data/spec/dummy_engine/dummy_engine.gemspec +0 -24
- data/spec/dummy_engine/lib/dummy_engine/engine.rb +0 -4
- data/spec/dummy_engine/lib/dummy_engine/version.rb +0 -3
- data/spec/dummy_engine/lib/dummy_engine.rb +0 -4
- data/spec/dummy_engine/test/dummy/Rakefile +0 -6
- data/spec/dummy_engine/test/dummy/config/application.rb +0 -22
- data/spec/dummy_engine/test/dummy/config/boot.rb +0 -5
- data/spec/dummy_engine/test/dummy/config/database.yml +0 -25
- data/spec/dummy_engine/test/dummy/config/environment.rb +0 -5
- data/spec/dummy_engine/test/dummy/config/environments/development.rb +0 -37
- data/spec/dummy_engine/test/dummy/config/environments/production.rb +0 -78
- data/spec/dummy_engine/test/dummy/config/environments/test.rb +0 -39
- data/spec/dummy_engine/test/dummy/config/initializers/assets.rb +0 -8
- data/spec/dummy_engine/test/dummy/config/initializers/backtrace_silencers.rb +0 -7
- data/spec/dummy_engine/test/dummy/config/initializers/cookies_serializer.rb +0 -3
- data/spec/dummy_engine/test/dummy/config/initializers/filter_parameter_logging.rb +0 -4
- data/spec/dummy_engine/test/dummy/config/initializers/inflections.rb +0 -16
- data/spec/dummy_engine/test/dummy/config/initializers/mime_types.rb +0 -4
- data/spec/dummy_engine/test/dummy/config/initializers/session_store.rb +0 -3
- data/spec/dummy_engine/test/dummy/config/initializers/wrap_parameters.rb +0 -14
- data/spec/dummy_engine/test/dummy/config/locales/en.yml +0 -23
- data/spec/dummy_engine/test/dummy/config/routes.rb +0 -56
- data/spec/dummy_engine/test/dummy/config/secrets.yml +0 -22
- data/spec/dummy_engine/test/dummy/config.ru +0 -4
- data/spec/examples/connection_adapter_examples.rb +0 -42
- data/spec/examples/generic_adapter_custom_configuration_example.rb +0 -95
- data/spec/examples/generic_adapter_examples.rb +0 -163
- data/spec/examples/schema_adapter_examples.rb +0 -234
- data/spec/integration/apartment_rake_integration_spec.rb +0 -107
- data/spec/integration/query_caching_spec.rb +0 -81
- data/spec/integration/use_within_an_engine_spec.rb +0 -28
- data/spec/schemas/v1.rb +0 -16
- data/spec/schemas/v2.rb +0 -43
- data/spec/schemas/v3.rb +0 -49
- data/spec/spec_helper.rb +0 -61
- data/spec/support/apartment_helpers.rb +0 -43
- data/spec/support/capybara_sessions.rb +0 -15
- data/spec/support/config.rb +0 -10
- data/spec/support/contexts.rb +0 -52
- data/spec/support/requirements.rb +0 -35
- data/spec/support/setup.rb +0 -46
- data/spec/tasks/apartment_rake_spec.rb +0 -129
- data/spec/tenant_spec.rb +0 -190
- data/spec/unit/config_spec.rb +0 -112
- data/spec/unit/elevators/domain_spec.rb +0 -32
- data/spec/unit/elevators/first_subdomain_spec.rb +0 -24
- data/spec/unit/elevators/generic_spec.rb +0 -54
- data/spec/unit/elevators/host_hash_spec.rb +0 -32
- data/spec/unit/elevators/host_spec.rb +0 -89
- data/spec/unit/elevators/subdomain_spec.rb +0 -76
- data/spec/unit/migrator_spec.rb +0 -77
- data/spec/unit/reloader_spec.rb +0 -24
data/lib/apartment.rb
CHANGED
@@ -1,22 +1,37 @@
|
|
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
|
-
|
9
|
+
require_relative 'apartment/log_subscriber'
|
10
|
+
require_relative 'apartment/active_record/connection_handling'
|
11
|
+
require_relative 'apartment/active_record/schema_migration'
|
12
|
+
require_relative 'apartment/active_record/internal_metadata'
|
8
13
|
|
14
|
+
# Apartment main definitions
|
15
|
+
module Apartment
|
9
16
|
class << self
|
10
|
-
|
11
17
|
extend Forwardable
|
12
18
|
|
13
|
-
ACCESSOR_METHODS
|
14
|
-
|
19
|
+
ACCESSOR_METHODS = %i[use_schemas use_sql seed_after_create prepend_environment default_tenant
|
20
|
+
append_environment with_multi_server_setup tenant_presence_check active_record_log].freeze
|
21
|
+
|
22
|
+
WRITER_METHODS = %i[tenant_names database_schema_file excluded_models
|
23
|
+
persistent_schemas connection_class
|
24
|
+
db_migrate_tenants db_migrate_tenant_missing_strategy seed_data_file
|
25
|
+
parallel_migration_threads pg_excluded_names].freeze
|
15
26
|
|
16
27
|
attr_accessor(*ACCESSOR_METHODS)
|
17
28
|
attr_writer(*WRITER_METHODS)
|
18
29
|
|
19
|
-
def_delegators :connection_class, :connection, :
|
30
|
+
def_delegators :connection_class, :connection, :connection_db_config, :establish_connection
|
31
|
+
|
32
|
+
def connection_config
|
33
|
+
connection_db_config.configuration_hash
|
34
|
+
end
|
20
35
|
|
21
36
|
# configure apartment with available options
|
22
37
|
def configure
|
@@ -31,8 +46,12 @@ module Apartment
|
|
31
46
|
extract_tenant_config
|
32
47
|
end
|
33
48
|
|
49
|
+
def tld_length=(_)
|
50
|
+
Apartment::Deprecation.warn('`config.tld_length` have no effect because it was removed in https://github.com/influitive/apartment/pull/309')
|
51
|
+
end
|
52
|
+
|
34
53
|
def db_config_for(tenant)
|
35
|
-
(tenants_with_config[tenant] || connection_config)
|
54
|
+
(tenants_with_config[tenant] || connection_config)
|
36
55
|
end
|
37
56
|
|
38
57
|
# Whether or not db:migrate should also migrate tenants
|
@@ -43,20 +62,29 @@ module Apartment
|
|
43
62
|
@db_migrate_tenants = true
|
44
63
|
end
|
45
64
|
|
65
|
+
# How to handle tenant missing on db:migrate
|
66
|
+
# defaults to :rescue_exception
|
67
|
+
# available options: rescue_exception, raise_exception, create_tenant
|
68
|
+
def db_migrate_tenant_missing_strategy
|
69
|
+
valid = %i[rescue_exception raise_exception create_tenant]
|
70
|
+
value = @db_migrate_tenant_missing_strategy || :rescue_exception
|
71
|
+
|
72
|
+
return value if valid.include?(value)
|
73
|
+
|
74
|
+
key_name = 'config.db_migrate_tenant_missing_strategy'
|
75
|
+
opt_names = valid.join(', ')
|
76
|
+
|
77
|
+
raise ApartmentError, "Option #{value} not valid for `#{key_name}`. Use one of #{opt_names}"
|
78
|
+
end
|
79
|
+
|
46
80
|
# Default to empty array
|
47
81
|
def excluded_models
|
48
82
|
@excluded_models || []
|
49
83
|
end
|
50
84
|
|
51
|
-
def default_schema
|
52
|
-
@default_schema || "public" # TODO 'public' is postgres specific
|
53
|
-
end
|
54
|
-
|
55
85
|
def parallel_migration_threads
|
56
86
|
@parallel_migration_threads || 0
|
57
87
|
end
|
58
|
-
alias :default_tenant :default_schema
|
59
|
-
alias :default_tenant= :default_schema=
|
60
88
|
|
61
89
|
def persistent_schemas
|
62
90
|
@persistent_schemas || []
|
@@ -69,13 +97,13 @@ module Apartment
|
|
69
97
|
def database_schema_file
|
70
98
|
return @database_schema_file if defined?(@database_schema_file)
|
71
99
|
|
72
|
-
@database_schema_file = Rails.root.join('db
|
100
|
+
@database_schema_file = Rails.root.join('db/schema.rb')
|
73
101
|
end
|
74
102
|
|
75
103
|
def seed_data_file
|
76
104
|
return @seed_data_file if defined?(@seed_data_file)
|
77
105
|
|
78
|
-
@seed_data_file =
|
106
|
+
@seed_data_file = Rails.root.join('db/seeds.rb')
|
79
107
|
end
|
80
108
|
|
81
109
|
def pg_excluded_names
|
@@ -84,11 +112,14 @@ module Apartment
|
|
84
112
|
|
85
113
|
# Reset all the config for Apartment
|
86
114
|
def reset
|
87
|
-
(ACCESSOR_METHODS + WRITER_METHODS).each
|
115
|
+
(ACCESSOR_METHODS + WRITER_METHODS).each do |method|
|
116
|
+
remove_instance_variable(:"@#{method}") if instance_variable_defined?(:"@#{method}")
|
117
|
+
end
|
88
118
|
end
|
89
119
|
|
90
120
|
def extract_tenant_config
|
91
121
|
return {} unless @tenant_names
|
122
|
+
|
92
123
|
values = @tenant_names.respond_to?(:call) ? @tenant_names.call : @tenant_names
|
93
124
|
unless values.is_a? Hash
|
94
125
|
values = values.each_with_object({}) do |tenant, hash|
|
@@ -1,10 +1,11 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Apartment
|
2
4
|
class InstallGenerator < Rails::Generators::Base
|
3
|
-
source_root File.expand_path('
|
5
|
+
source_root File.expand_path('templates', __dir__)
|
4
6
|
|
5
7
|
def copy_files
|
6
|
-
template
|
8
|
+
template 'apartment.rb', File.join('config', 'initializers', 'apartment.rb')
|
7
9
|
end
|
8
|
-
|
9
10
|
end
|
10
11
|
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
# You can have Apartment route to the appropriate Tenant by adding some Rack middleware.
|
2
4
|
# Apartment can support many different "Elevators" that can take care of this routing to your data.
|
3
5
|
# Require whichever Elevator you're using below or none if you have a custom one.
|
@@ -12,7 +14,6 @@ require 'apartment/elevators/subdomain'
|
|
12
14
|
# Apartment Configuration
|
13
15
|
#
|
14
16
|
Apartment.configure do |config|
|
15
|
-
|
16
17
|
# Add any models that you do not want to be multi-tenanted, but remain in the global (public) namespace.
|
17
18
|
# A typical example would be a Customer or Tenant model that stores each Tenant's information.
|
18
19
|
#
|
@@ -22,7 +23,8 @@ Apartment.configure do |config|
|
|
22
23
|
# You can make this dynamic by providing a Proc object to be called on migrations.
|
23
24
|
# This object should yield either:
|
24
25
|
# - an array of strings representing each Tenant name.
|
25
|
-
# - a hash which keys are tenant names, and values custom db config
|
26
|
+
# - a hash which keys are tenant names, and values custom db config
|
27
|
+
# (must contain all key/values required in database.yml)
|
26
28
|
#
|
27
29
|
# config.tenant_names = lambda{ Customer.pluck(:tenant_name) }
|
28
30
|
# config.tenant_names = ['tenant1', 'tenant2']
|
@@ -48,7 +50,7 @@ Apartment.configure do |config|
|
|
48
50
|
# end
|
49
51
|
# end
|
50
52
|
#
|
51
|
-
config.tenant_names =
|
53
|
+
config.tenant_names = -> { ToDo_Tenant_Or_User_Model.pluck :database }
|
52
54
|
|
53
55
|
# PostgreSQL:
|
54
56
|
# Specifies whether to use PostgreSQL schemas or create a new database per Tenant.
|
@@ -95,6 +97,11 @@ Apartment.configure do |config|
|
|
95
97
|
# the new tenant
|
96
98
|
#
|
97
99
|
# config.pg_excluded_names = ["uuid_generate_v4"]
|
100
|
+
|
101
|
+
# Specifies whether the database and schema (when using PostgreSQL schemas) will prepend in ActiveRecord log.
|
102
|
+
# Uncomment the line below if you want to enable this behavior.
|
103
|
+
#
|
104
|
+
# config.active_record_log = true
|
98
105
|
end
|
99
106
|
|
100
107
|
# Setup a custom Tenant switching middleware. The Proc should return the name of the Tenant that
|
data/lib/tasks/apartment.rake
CHANGED
@@ -1,113 +1,98 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'apartment/migrator'
|
4
|
+
require 'apartment/tasks/task_helper'
|
2
5
|
require 'parallel'
|
3
6
|
|
4
7
|
apartment_namespace = namespace :apartment do
|
5
|
-
|
6
|
-
desc "Create all tenants"
|
8
|
+
desc 'Create all tenants'
|
7
9
|
task :create do
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
rescue Apartment::TenantExists => e
|
13
|
-
puts e.message
|
14
|
-
end
|
10
|
+
Apartment::TaskHelper.warn_if_tenants_empty
|
11
|
+
|
12
|
+
Apartment::TaskHelper.tenants.each do |tenant|
|
13
|
+
Apartment::TaskHelper.create_tenant(tenant)
|
15
14
|
end
|
16
15
|
end
|
17
16
|
|
18
|
-
desc
|
17
|
+
desc 'Drop all tenants'
|
19
18
|
task :drop do
|
20
|
-
tenants.each do |tenant|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
puts e.message
|
26
|
-
end
|
19
|
+
Apartment::TaskHelper.tenants.each do |tenant|
|
20
|
+
puts("Dropping #{tenant} tenant")
|
21
|
+
Apartment::Tenant.drop(tenant)
|
22
|
+
rescue Apartment::TenantNotFound, ActiveRecord::NoDatabaseError => e
|
23
|
+
puts e.message
|
27
24
|
end
|
28
25
|
end
|
29
26
|
|
30
|
-
desc
|
27
|
+
desc 'Migrate all tenants'
|
31
28
|
task :migrate do
|
32
|
-
warn_if_tenants_empty
|
33
|
-
each_tenant do |tenant|
|
34
|
-
|
35
|
-
puts("Migrating #{tenant} tenant")
|
36
|
-
Apartment::Migrator.migrate tenant
|
37
|
-
rescue Apartment::TenantNotFound => e
|
38
|
-
puts e.message
|
39
|
-
end
|
29
|
+
Apartment::TaskHelper.warn_if_tenants_empty
|
30
|
+
Apartment::TaskHelper.each_tenant do |tenant|
|
31
|
+
Apartment::TaskHelper.migrate_tenant(tenant)
|
40
32
|
end
|
41
33
|
end
|
42
34
|
|
43
|
-
desc
|
35
|
+
desc 'Seed all tenants'
|
44
36
|
task :seed do
|
45
|
-
warn_if_tenants_empty
|
46
|
-
|
47
|
-
each_tenant do |tenant|
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
end
|
53
|
-
rescue Apartment::TenantNotFound => e
|
54
|
-
puts e.message
|
37
|
+
Apartment::TaskHelper.warn_if_tenants_empty
|
38
|
+
|
39
|
+
Apartment::TaskHelper.each_tenant do |tenant|
|
40
|
+
Apartment::TaskHelper.create_tenant(tenant)
|
41
|
+
puts("Seeding #{tenant} tenant")
|
42
|
+
Apartment::Tenant.switch(tenant) do
|
43
|
+
Apartment::Tenant.seed
|
55
44
|
end
|
45
|
+
rescue Apartment::TenantNotFound => e
|
46
|
+
puts e.message
|
56
47
|
end
|
57
48
|
end
|
58
49
|
|
59
|
-
desc
|
50
|
+
desc 'Rolls the migration back to the previous version (specify steps w/ STEP=n) across all tenants.'
|
60
51
|
task :rollback do
|
61
|
-
warn_if_tenants_empty
|
52
|
+
Apartment::TaskHelper.warn_if_tenants_empty
|
62
53
|
|
63
54
|
step = ENV['STEP'] ? ENV['STEP'].to_i : 1
|
64
55
|
|
65
|
-
each_tenant do |tenant|
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
puts e.message
|
71
|
-
end
|
56
|
+
Apartment::TaskHelper.each_tenant do |tenant|
|
57
|
+
puts("Rolling back #{tenant} tenant")
|
58
|
+
Apartment::Migrator.rollback tenant, step
|
59
|
+
rescue Apartment::TenantNotFound => e
|
60
|
+
puts e.message
|
72
61
|
end
|
73
62
|
end
|
74
63
|
|
75
64
|
namespace :migrate do
|
76
65
|
desc 'Runs the "up" for a given migration VERSION across all tenants.'
|
77
66
|
task :up do
|
78
|
-
warn_if_tenants_empty
|
67
|
+
Apartment::TaskHelper.warn_if_tenants_empty
|
79
68
|
|
80
69
|
version = ENV['VERSION'] ? ENV['VERSION'].to_i : nil
|
81
70
|
raise 'VERSION is required' unless version
|
82
71
|
|
83
|
-
each_tenant do |tenant|
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
puts e.message
|
89
|
-
end
|
72
|
+
Apartment::TaskHelper.each_tenant do |tenant|
|
73
|
+
puts("Migrating #{tenant} tenant up")
|
74
|
+
Apartment::Migrator.run :up, tenant, version
|
75
|
+
rescue Apartment::TenantNotFound => e
|
76
|
+
puts e.message
|
90
77
|
end
|
91
78
|
end
|
92
79
|
|
93
80
|
desc 'Runs the "down" for a given migration VERSION across all tenants.'
|
94
81
|
task :down do
|
95
|
-
warn_if_tenants_empty
|
82
|
+
Apartment::TaskHelper.warn_if_tenants_empty
|
96
83
|
|
97
84
|
version = ENV['VERSION'] ? ENV['VERSION'].to_i : nil
|
98
85
|
raise 'VERSION is required' unless version
|
99
86
|
|
100
|
-
each_tenant do |tenant|
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
puts e.message
|
106
|
-
end
|
87
|
+
Apartment::TaskHelper.each_tenant do |tenant|
|
88
|
+
puts("Migrating #{tenant} tenant down")
|
89
|
+
Apartment::Migrator.run :down, tenant, version
|
90
|
+
rescue Apartment::TenantNotFound => e
|
91
|
+
puts e.message
|
107
92
|
end
|
108
93
|
end
|
109
94
|
|
110
|
-
desc
|
95
|
+
desc 'Rolls back the tenant one migration and re migrate up (options: STEP=x, VERSION=x).'
|
111
96
|
task :redo do
|
112
97
|
if ENV['VERSION']
|
113
98
|
apartment_namespace['migrate:down'].invoke
|
@@ -118,28 +103,4 @@ apartment_namespace = namespace :apartment do
|
|
118
103
|
end
|
119
104
|
end
|
120
105
|
end
|
121
|
-
|
122
|
-
def each_tenant(&block)
|
123
|
-
Parallel.each(tenants, in_threads: Apartment.parallel_migration_threads) do |tenant|
|
124
|
-
block.call(tenant)
|
125
|
-
end
|
126
|
-
end
|
127
|
-
|
128
|
-
def tenants
|
129
|
-
ENV['DB'] ? ENV['DB'].split(',').map { |s| s.strip } : Apartment.tenant_names || []
|
130
|
-
end
|
131
|
-
|
132
|
-
def warn_if_tenants_empty
|
133
|
-
if tenants.empty? && ENV['IGNORE_EMPTY_TENANTS'] != "true"
|
134
|
-
puts <<-WARNING
|
135
|
-
[WARNING] - The list of tenants to migrate appears to be empty. This could mean a few things:
|
136
|
-
|
137
|
-
1. You may not have created any, in which case you can ignore this message
|
138
|
-
2. You've run `apartment:migrate` directly without loading the Rails environment
|
139
|
-
* `apartment:migrate` is now deprecated. Tenants will automatically be migrated with `db:migrate`
|
140
|
-
|
141
|
-
Note that your tenants currently haven't been migrated. You'll need to run `db:migrate` to rectify this.
|
142
|
-
WARNING
|
143
|
-
end
|
144
|
-
end
|
145
106
|
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
$LOAD_PATH << File.expand_path('lib', __dir__)
|
4
|
+
require 'apartment/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = 'ros-apartment'
|
8
|
+
s.version = Apartment::VERSION
|
9
|
+
|
10
|
+
s.authors = ['Ryan Brunner', 'Brad Robertson', 'Rui Baltazar']
|
11
|
+
s.summary = 'A Ruby gem for managing database multitenancy. Apartment Gem drop in replacement'
|
12
|
+
s.description = 'Apartment allows Rack applications to deal with database multitenancy through ActiveRecord'
|
13
|
+
s.email = ['ryan@influitive.com', 'brad@influitive.com', 'rui.p.baltazar@gmail.com']
|
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|documentation|gemfiles|.github)/})
|
21
|
+
end
|
22
|
+
end
|
23
|
+
s.executables = s.files.grep(%r{^bin/}).map { |f| File.basename(f) }
|
24
|
+
s.test_files = s.files.grep(%r{^(test|spec|features)/})
|
25
|
+
s.require_paths = ['lib']
|
26
|
+
|
27
|
+
s.homepage = 'https://github.com/rails-on-services/apartment'
|
28
|
+
s.licenses = ['MIT']
|
29
|
+
s.metadata = {
|
30
|
+
'github_repo' => 'ssh://github.com/rails-on-services/apartment'
|
31
|
+
}
|
32
|
+
|
33
|
+
s.required_ruby_version = '>= 3.0', '< 3.4'
|
34
|
+
|
35
|
+
s.add_dependency 'activerecord', '>= 6.1.0', '< 7.2'
|
36
|
+
s.add_dependency 'parallel', '< 2.0'
|
37
|
+
s.add_dependency 'public_suffix', '>= 2.0.5', '< 6.0'
|
38
|
+
s.add_dependency 'rack', '>= 1.3.6', '< 4.0'
|
39
|
+
|
40
|
+
s.add_development_dependency 'appraisal', '~> 2.2'
|
41
|
+
s.add_development_dependency 'bundler', '>= 1.3', '< 3.0'
|
42
|
+
s.add_development_dependency 'guard-rspec', '~> 4.2'
|
43
|
+
s.add_development_dependency 'pry'
|
44
|
+
s.add_development_dependency 'rake', '~> 13.0'
|
45
|
+
s.add_development_dependency 'rspec', '~> 3.4'
|
46
|
+
s.add_development_dependency 'rspec_junit_formatter'
|
47
|
+
s.add_development_dependency 'rspec-rails', '~> 6.1'
|
48
|
+
s.add_development_dependency 'rubocop', '~> 1.63'
|
49
|
+
s.add_development_dependency 'rubocop-performance', '~> 1.21'
|
50
|
+
s.add_development_dependency 'rubocop-rails', '~> 2.24'
|
51
|
+
s.add_development_dependency 'rubocop-rspec', '~> 2.29'
|
52
|
+
|
53
|
+
if defined?(JRUBY_VERSION)
|
54
|
+
s.add_development_dependency 'activerecord-jdbc-adapter'
|
55
|
+
s.add_development_dependency 'activerecord-jdbcmysql-adapter'
|
56
|
+
s.add_development_dependency 'activerecord-jdbcpostgresql-adapter'
|
57
|
+
s.add_development_dependency 'jdbc-mysql'
|
58
|
+
s.add_development_dependency 'jdbc-postgres'
|
59
|
+
else
|
60
|
+
s.add_development_dependency 'mysql2', '~> 0.5'
|
61
|
+
s.add_development_dependency 'pg', '~> 1.5'
|
62
|
+
s.add_development_dependency 'sqlite3', '< 2.0'
|
63
|
+
s.add_development_dependency 'trilogy', '< 3.0'
|
64
|
+
end
|
65
|
+
end
|