ros-apartment 2.3.0 → 2.11.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (163) hide show
  1. checksums.yaml +4 -4
  2. data/.circleci/config.yml +78 -0
  3. data/.github/workflows/changelog.yml +63 -0
  4. data/.github/workflows/reviewdog.yml +22 -0
  5. data/.pryrc +5 -3
  6. data/.rubocop.yml +32 -0
  7. data/.rubocop_todo.yml +233 -0
  8. data/.ruby-version +1 -0
  9. data/.story_branch.yml +5 -0
  10. data/Appraisals +29 -42
  11. data/CHANGELOG.md +963 -0
  12. data/Gemfile +2 -7
  13. data/Guardfile +3 -16
  14. data/HISTORY.md +159 -68
  15. data/README.md +101 -21
  16. data/Rakefile +39 -22
  17. data/TODO.md +0 -1
  18. data/gemfiles/rails_5_2.gemfile +0 -5
  19. data/gemfiles/rails_6_0.gemfile +4 -9
  20. data/gemfiles/rails_6_1.gemfile +17 -0
  21. data/gemfiles/rails_7_0.gemfile +17 -0
  22. data/gemfiles/rails_master.gemfile +3 -8
  23. data/lib/apartment/active_record/connection_handling.rb +20 -0
  24. data/lib/apartment/active_record/internal_metadata.rb +9 -0
  25. data/lib/apartment/active_record/postgresql_adapter.rb +39 -0
  26. data/lib/apartment/active_record/schema_migration.rb +11 -0
  27. data/lib/apartment/adapters/abstract_adapter.rb +52 -46
  28. data/lib/apartment/adapters/abstract_jdbc_adapter.rb +5 -3
  29. data/lib/apartment/adapters/jdbc_mysql_adapter.rb +3 -3
  30. data/lib/apartment/adapters/jdbc_postgresql_adapter.rb +19 -13
  31. data/lib/apartment/adapters/mysql2_adapter.rb +15 -9
  32. data/lib/apartment/adapters/postgis_adapter.rb +3 -2
  33. data/lib/apartment/adapters/postgresql_adapter.rb +79 -31
  34. data/lib/apartment/adapters/sqlite3_adapter.rb +18 -8
  35. data/lib/apartment/console.rb +23 -11
  36. data/lib/apartment/custom_console.rb +42 -0
  37. data/lib/apartment/deprecation.rb +2 -1
  38. data/lib/apartment/elevators/domain.rb +4 -3
  39. data/lib/apartment/elevators/first_subdomain.rb +3 -2
  40. data/lib/apartment/elevators/generic.rb +4 -3
  41. data/lib/apartment/elevators/host.rb +6 -1
  42. data/lib/apartment/elevators/host_hash.rb +6 -2
  43. data/lib/apartment/elevators/subdomain.rb +9 -5
  44. data/lib/apartment/log_subscriber.rb +33 -0
  45. data/lib/apartment/migrator.rb +4 -3
  46. data/lib/apartment/model.rb +29 -0
  47. data/lib/apartment/railtie.rb +16 -20
  48. data/lib/apartment/tasks/enhancements.rb +4 -6
  49. data/lib/apartment/tasks/task_helper.rb +52 -0
  50. data/lib/apartment/tenant.rb +7 -10
  51. data/lib/apartment/version.rb +3 -1
  52. data/lib/apartment.rb +56 -15
  53. data/lib/generators/apartment/install/install_generator.rb +4 -3
  54. data/lib/generators/apartment/install/templates/apartment.rb +10 -3
  55. data/lib/tasks/apartment.rake +48 -87
  56. data/ros-apartment.gemspec +59 -0
  57. metadata +148 -240
  58. data/.travis.yml +0 -65
  59. data/apartment.gemspec +0 -47
  60. data/gemfiles/rails_4_2.gemfile +0 -23
  61. data/gemfiles/rails_5_0.gemfile +0 -22
  62. data/gemfiles/rails_5_1.gemfile +0 -22
  63. data/lib/apartment/reloader.rb +0 -21
  64. data/spec/adapters/jdbc_mysql_adapter_spec.rb +0 -19
  65. data/spec/adapters/jdbc_postgresql_adapter_spec.rb +0 -41
  66. data/spec/adapters/mysql2_adapter_spec.rb +0 -59
  67. data/spec/adapters/postgresql_adapter_spec.rb +0 -61
  68. data/spec/adapters/sqlite3_adapter_spec.rb +0 -83
  69. data/spec/apartment_spec.rb +0 -11
  70. data/spec/config/database.yml.sample +0 -49
  71. data/spec/dummy/Rakefile +0 -7
  72. data/spec/dummy/app/controllers/application_controller.rb +0 -6
  73. data/spec/dummy/app/helpers/application_helper.rb +0 -2
  74. data/spec/dummy/app/models/company.rb +0 -3
  75. data/spec/dummy/app/models/user.rb +0 -3
  76. data/spec/dummy/app/views/application/index.html.erb +0 -1
  77. data/spec/dummy/app/views/layouts/application.html.erb +0 -14
  78. data/spec/dummy/config/application.rb +0 -49
  79. data/spec/dummy/config/boot.rb +0 -11
  80. data/spec/dummy/config/database.yml.sample +0 -44
  81. data/spec/dummy/config/environment.rb +0 -5
  82. data/spec/dummy/config/environments/development.rb +0 -28
  83. data/spec/dummy/config/environments/production.rb +0 -51
  84. data/spec/dummy/config/environments/test.rb +0 -34
  85. data/spec/dummy/config/initializers/apartment.rb +0 -4
  86. data/spec/dummy/config/initializers/backtrace_silencers.rb +0 -7
  87. data/spec/dummy/config/initializers/inflections.rb +0 -10
  88. data/spec/dummy/config/initializers/mime_types.rb +0 -5
  89. data/spec/dummy/config/initializers/secret_token.rb +0 -7
  90. data/spec/dummy/config/initializers/session_store.rb +0 -8
  91. data/spec/dummy/config/locales/en.yml +0 -5
  92. data/spec/dummy/config/routes.rb +0 -3
  93. data/spec/dummy/config.ru +0 -4
  94. data/spec/dummy/db/migrate/20110613152810_create_dummy_models.rb +0 -39
  95. data/spec/dummy/db/migrate/20111202022214_create_table_books.rb +0 -14
  96. data/spec/dummy/db/migrate/20180415260934_create_public_tokens.rb +0 -13
  97. data/spec/dummy/db/schema.rb +0 -55
  98. data/spec/dummy/db/seeds/import.rb +0 -5
  99. data/spec/dummy/db/seeds.rb +0 -5
  100. data/spec/dummy/db/test.sqlite3 +0 -0
  101. data/spec/dummy/public/404.html +0 -26
  102. data/spec/dummy/public/422.html +0 -26
  103. data/spec/dummy/public/500.html +0 -26
  104. data/spec/dummy/public/favicon.ico +0 -0
  105. data/spec/dummy/public/stylesheets/.gitkeep +0 -0
  106. data/spec/dummy/script/rails +0 -6
  107. data/spec/dummy_engine/.gitignore +0 -8
  108. data/spec/dummy_engine/Gemfile +0 -15
  109. data/spec/dummy_engine/Rakefile +0 -34
  110. data/spec/dummy_engine/bin/rails +0 -12
  111. data/spec/dummy_engine/config/initializers/apartment.rb +0 -51
  112. data/spec/dummy_engine/dummy_engine.gemspec +0 -24
  113. data/spec/dummy_engine/lib/dummy_engine/engine.rb +0 -4
  114. data/spec/dummy_engine/lib/dummy_engine/version.rb +0 -3
  115. data/spec/dummy_engine/lib/dummy_engine.rb +0 -4
  116. data/spec/dummy_engine/test/dummy/Rakefile +0 -6
  117. data/spec/dummy_engine/test/dummy/config/application.rb +0 -22
  118. data/spec/dummy_engine/test/dummy/config/boot.rb +0 -5
  119. data/spec/dummy_engine/test/dummy/config/database.yml +0 -25
  120. data/spec/dummy_engine/test/dummy/config/environment.rb +0 -5
  121. data/spec/dummy_engine/test/dummy/config/environments/development.rb +0 -37
  122. data/spec/dummy_engine/test/dummy/config/environments/production.rb +0 -78
  123. data/spec/dummy_engine/test/dummy/config/environments/test.rb +0 -39
  124. data/spec/dummy_engine/test/dummy/config/initializers/assets.rb +0 -8
  125. data/spec/dummy_engine/test/dummy/config/initializers/backtrace_silencers.rb +0 -7
  126. data/spec/dummy_engine/test/dummy/config/initializers/cookies_serializer.rb +0 -3
  127. data/spec/dummy_engine/test/dummy/config/initializers/filter_parameter_logging.rb +0 -4
  128. data/spec/dummy_engine/test/dummy/config/initializers/inflections.rb +0 -16
  129. data/spec/dummy_engine/test/dummy/config/initializers/mime_types.rb +0 -4
  130. data/spec/dummy_engine/test/dummy/config/initializers/session_store.rb +0 -3
  131. data/spec/dummy_engine/test/dummy/config/initializers/wrap_parameters.rb +0 -14
  132. data/spec/dummy_engine/test/dummy/config/locales/en.yml +0 -23
  133. data/spec/dummy_engine/test/dummy/config/routes.rb +0 -56
  134. data/spec/dummy_engine/test/dummy/config/secrets.yml +0 -22
  135. data/spec/dummy_engine/test/dummy/config.ru +0 -4
  136. data/spec/examples/connection_adapter_examples.rb +0 -42
  137. data/spec/examples/generic_adapter_custom_configuration_example.rb +0 -95
  138. data/spec/examples/generic_adapter_examples.rb +0 -163
  139. data/spec/examples/schema_adapter_examples.rb +0 -234
  140. data/spec/integration/apartment_rake_integration_spec.rb +0 -107
  141. data/spec/integration/query_caching_spec.rb +0 -81
  142. data/spec/integration/use_within_an_engine_spec.rb +0 -28
  143. data/spec/schemas/v1.rb +0 -16
  144. data/spec/schemas/v2.rb +0 -43
  145. data/spec/schemas/v3.rb +0 -49
  146. data/spec/spec_helper.rb +0 -61
  147. data/spec/support/apartment_helpers.rb +0 -43
  148. data/spec/support/capybara_sessions.rb +0 -15
  149. data/spec/support/config.rb +0 -10
  150. data/spec/support/contexts.rb +0 -52
  151. data/spec/support/requirements.rb +0 -35
  152. data/spec/support/setup.rb +0 -46
  153. data/spec/tasks/apartment_rake_spec.rb +0 -129
  154. data/spec/tenant_spec.rb +0 -190
  155. data/spec/unit/config_spec.rb +0 -112
  156. data/spec/unit/elevators/domain_spec.rb +0 -32
  157. data/spec/unit/elevators/first_subdomain_spec.rb +0 -24
  158. data/spec/unit/elevators/generic_spec.rb +0 -54
  159. data/spec/unit/elevators/host_hash_spec.rb +0 -32
  160. data/spec/unit/elevators/host_spec.rb +0 -89
  161. data/spec/unit/elevators/subdomain_spec.rb +0 -76
  162. data/spec/unit/migrator_spec.rb +0 -77
  163. data/spec/unit/reloader_spec.rb +0 -24
data/lib/apartment.rb CHANGED
@@ -1,22 +1,47 @@
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
- module Apartment
9
+ require_relative 'apartment/log_subscriber'
8
10
 
9
- class << self
11
+ if ActiveRecord.version.release >= Gem::Version.new('6.0')
12
+ require_relative 'apartment/active_record/connection_handling'
13
+ end
14
+
15
+ if ActiveRecord.version.release >= Gem::Version.new('6.1')
16
+ require_relative 'apartment/active_record/schema_migration'
17
+ require_relative 'apartment/active_record/internal_metadata'
18
+ end
10
19
 
20
+ # Apartment main definitions
21
+ module Apartment
22
+ class << self
11
23
  extend Forwardable
12
24
 
13
- ACCESSOR_METHODS = [:use_schemas, :use_sql, :seed_after_create, :prepend_environment, :append_environment, :with_multi_server_setup]
14
- WRITER_METHODS = [:tenant_names, :database_schema_file, :excluded_models, :default_schema, :persistent_schemas, :connection_class, :tld_length, :db_migrate_tenants, :seed_data_file, :parallel_migration_threads, :pg_excluded_names]
25
+ ACCESSOR_METHODS = %i[use_schemas use_sql seed_after_create prepend_environment default_tenant
26
+ append_environment with_multi_server_setup tenant_presence_check active_record_log].freeze
27
+
28
+ WRITER_METHODS = %i[tenant_names database_schema_file excluded_models
29
+ persistent_schemas connection_class
30
+ db_migrate_tenants db_migrate_tenant_missing_strategy seed_data_file
31
+ parallel_migration_threads pg_excluded_names].freeze
15
32
 
16
33
  attr_accessor(*ACCESSOR_METHODS)
17
34
  attr_writer(*WRITER_METHODS)
18
35
 
19
- def_delegators :connection_class, :connection, :connection_config, :establish_connection
36
+ if ActiveRecord.version.release >= Gem::Version.new('6.1')
37
+ def_delegators :connection_class, :connection, :connection_db_config, :establish_connection
38
+
39
+ def connection_config
40
+ connection_db_config.configuration_hash
41
+ end
42
+ else
43
+ def_delegators :connection_class, :connection, :connection_config, :establish_connection
44
+ end
20
45
 
21
46
  # configure apartment with available options
22
47
  def configure
@@ -31,8 +56,12 @@ module Apartment
31
56
  extract_tenant_config
32
57
  end
33
58
 
59
+ def tld_length=(_)
60
+ Apartment::Deprecation.warn('`config.tld_length` have no effect because it was removed in https://github.com/influitive/apartment/pull/309')
61
+ end
62
+
34
63
  def db_config_for(tenant)
35
- (tenants_with_config[tenant] || connection_config).with_indifferent_access
64
+ (tenants_with_config[tenant] || connection_config)
36
65
  end
37
66
 
38
67
  # Whether or not db:migrate should also migrate tenants
@@ -43,20 +72,29 @@ module Apartment
43
72
  @db_migrate_tenants = true
44
73
  end
45
74
 
75
+ # How to handle tenant missing on db:migrate
76
+ # defaults to :rescue_exception
77
+ # available options: rescue_exception, raise_exception, create_tenant
78
+ def db_migrate_tenant_missing_strategy
79
+ valid = %i[rescue_exception raise_exception create_tenant]
80
+ value = @db_migrate_tenant_missing_strategy || :rescue_exception
81
+
82
+ return value if valid.include?(value)
83
+
84
+ key_name = 'config.db_migrate_tenant_missing_strategy'
85
+ opt_names = valid.join(', ')
86
+
87
+ raise ApartmentError, "Option #{value} not valid for `#{key_name}`. Use one of #{opt_names}"
88
+ end
89
+
46
90
  # Default to empty array
47
91
  def excluded_models
48
92
  @excluded_models || []
49
93
  end
50
94
 
51
- def default_schema
52
- @default_schema || "public" # TODO 'public' is postgres specific
53
- end
54
-
55
95
  def parallel_migration_threads
56
96
  @parallel_migration_threads || 0
57
97
  end
58
- alias :default_tenant :default_schema
59
- alias :default_tenant= :default_schema=
60
98
 
61
99
  def persistent_schemas
62
100
  @persistent_schemas || []
@@ -69,13 +107,13 @@ module Apartment
69
107
  def database_schema_file
70
108
  return @database_schema_file if defined?(@database_schema_file)
71
109
 
72
- @database_schema_file = Rails.root.join('db', 'schema.rb')
110
+ @database_schema_file = Rails.root.join('db/schema.rb')
73
111
  end
74
112
 
75
113
  def seed_data_file
76
114
  return @seed_data_file if defined?(@seed_data_file)
77
115
 
78
- @seed_data_file = "#{Rails.root}/db/seeds.rb"
116
+ @seed_data_file = Rails.root.join('db/seeds.rb')
79
117
  end
80
118
 
81
119
  def pg_excluded_names
@@ -84,11 +122,14 @@ module Apartment
84
122
 
85
123
  # Reset all the config for Apartment
86
124
  def reset
87
- (ACCESSOR_METHODS + WRITER_METHODS).each{|method| remove_instance_variable(:"@#{method}") if instance_variable_defined?(:"@#{method}") }
125
+ (ACCESSOR_METHODS + WRITER_METHODS).each do |method|
126
+ remove_instance_variable(:"@#{method}") if instance_variable_defined?(:"@#{method}")
127
+ end
88
128
  end
89
129
 
90
130
  def extract_tenant_config
91
131
  return {} unless @tenant_names
132
+
92
133
  values = @tenant_names.respond_to?(:call) ? @tenant_names.call : @tenant_names
93
134
  unless values.is_a? Hash
94
135
  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('../templates', __FILE__)
5
+ source_root File.expand_path('templates', __dir__)
4
6
 
5
7
  def copy_files
6
- template "apartment.rb", File.join("config", "initializers", "apartment.rb")
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 (must contain all key/values required in database.yml)
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 = lambda { ToDo_Tenant_Or_User_Model.pluck :database }
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
@@ -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
- tenants.each do |tenant|
9
- begin
10
- puts("Creating #{tenant} tenant")
11
- Apartment::Tenant.create(tenant)
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 "Drop all tenants"
17
+ desc 'Drop all tenants'
19
18
  task :drop do
20
- tenants.each do |tenant|
21
- begin
22
- puts("Dropping #{tenant} tenant")
23
- Apartment::Tenant.drop(tenant)
24
- rescue Apartment::TenantNotFound => e
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 "Migrate all tenants"
27
+ desc 'Migrate all tenants'
31
28
  task :migrate do
32
- warn_if_tenants_empty
33
- each_tenant do |tenant|
34
- begin
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 "Seed all tenants"
35
+ desc 'Seed all tenants'
44
36
  task :seed do
45
- warn_if_tenants_empty
46
-
47
- each_tenant do |tenant|
48
- begin
49
- puts("Seeding #{tenant} tenant")
50
- Apartment::Tenant.switch(tenant) do
51
- Apartment::Tenant.seed
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 "Rolls the migration back to the previous version (specify steps w/ STEP=n) across all tenants."
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
- begin
67
- puts("Rolling back #{tenant} tenant")
68
- Apartment::Migrator.rollback tenant, step
69
- rescue Apartment::TenantNotFound => e
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
- begin
85
- puts("Migrating #{tenant} tenant up")
86
- Apartment::Migrator.run :up, tenant, version
87
- rescue Apartment::TenantNotFound => e
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
- begin
102
- puts("Migrating #{tenant} tenant down")
103
- Apartment::Migrator.run :down, tenant, version
104
- rescue Apartment::TenantNotFound => e
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 'Rolls back the tenant one migration and re migrate up (options: STEP=x, VERSION=x).'
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,59 @@
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)/})
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
+
30
+ s.add_dependency 'activerecord', '>= 5.0.0', '< 7.1'
31
+ s.add_dependency 'parallel', '< 2.0'
32
+ s.add_dependency 'public_suffix', '>= 2.0.5', '< 5.0'
33
+ s.add_dependency 'rack', '>= 1.3.6', '< 3.0'
34
+
35
+ s.add_development_dependency 'appraisal', '~> 2.2'
36
+ s.add_development_dependency 'bundler', '>= 1.3', '< 3.0'
37
+ s.add_development_dependency 'guard-rspec', '~> 4.2'
38
+ s.add_development_dependency 'pry'
39
+ s.add_development_dependency 'rake', '~> 13.0'
40
+ s.add_development_dependency 'rspec', '~> 3.4'
41
+ s.add_development_dependency 'rspec_junit_formatter'
42
+ s.add_development_dependency 'rspec-rails', '~> 3.4'
43
+ s.add_development_dependency 'rubocop', '~> 0.93'
44
+ s.add_development_dependency 'rubocop-performance', '~> 1.10'
45
+ s.add_development_dependency 'rubocop-rails', '~> 2.1'
46
+ s.add_development_dependency 'rubocop-rspec', '~> 1.44'
47
+
48
+ if defined?(JRUBY_VERSION)
49
+ s.add_development_dependency 'activerecord-jdbc-adapter'
50
+ s.add_development_dependency 'activerecord-jdbcmysql-adapter'
51
+ s.add_development_dependency 'activerecord-jdbcpostgresql-adapter'
52
+ s.add_development_dependency 'jdbc-mysql'
53
+ s.add_development_dependency 'jdbc-postgres'
54
+ else
55
+ s.add_development_dependency 'mysql2', '~> 0.5'
56
+ s.add_development_dependency 'pg', '~> 1.2'
57
+ s.add_development_dependency 'sqlite3', '~> 1.3.6'
58
+ end
59
+ end