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.
Files changed (160) hide show
  1. checksums.yaml +4 -4
  2. data/.pryrc +5 -3
  3. data/.rubocop.yml +36 -0
  4. data/.rubocop_todo.yml +439 -0
  5. data/.ruby-version +1 -0
  6. data/Appraisals +31 -52
  7. data/CHANGELOG.md +965 -0
  8. data/Gemfile +2 -7
  9. data/Guardfile +3 -16
  10. data/README.md +121 -44
  11. data/Rakefile +42 -31
  12. data/lib/apartment/active_record/connection_handling.rb +31 -0
  13. data/lib/apartment/active_record/internal_metadata.rb +9 -0
  14. data/lib/apartment/active_record/postgresql_adapter.rb +43 -0
  15. data/lib/apartment/active_record/schema_migration.rb +11 -0
  16. data/lib/apartment/adapters/abstract_adapter.rb +52 -46
  17. data/lib/apartment/adapters/abstract_jdbc_adapter.rb +5 -3
  18. data/lib/apartment/adapters/jdbc_mysql_adapter.rb +3 -3
  19. data/lib/apartment/adapters/jdbc_postgresql_adapter.rb +19 -13
  20. data/lib/apartment/adapters/mysql2_adapter.rb +15 -9
  21. data/lib/apartment/adapters/postgis_adapter.rb +3 -2
  22. data/lib/apartment/adapters/postgresql_adapter.rb +79 -31
  23. data/lib/apartment/adapters/sqlite3_adapter.rb +18 -8
  24. data/lib/apartment/adapters/trilogy_adapter.rb +29 -0
  25. data/lib/apartment/console.rb +23 -11
  26. data/lib/apartment/custom_console.rb +42 -0
  27. data/lib/apartment/deprecation.rb +2 -1
  28. data/lib/apartment/elevators/domain.rb +4 -3
  29. data/lib/apartment/elevators/first_subdomain.rb +3 -2
  30. data/lib/apartment/elevators/generic.rb +4 -3
  31. data/lib/apartment/elevators/host.rb +6 -1
  32. data/lib/apartment/elevators/host_hash.rb +6 -2
  33. data/lib/apartment/elevators/subdomain.rb +9 -5
  34. data/lib/apartment/log_subscriber.rb +45 -0
  35. data/lib/apartment/migrator.rb +7 -24
  36. data/lib/apartment/model.rb +29 -0
  37. data/lib/apartment/railtie.rb +21 -20
  38. data/lib/apartment/tasks/enhancements.rb +4 -6
  39. data/lib/apartment/tasks/task_helper.rb +52 -0
  40. data/lib/apartment/tenant.rb +7 -10
  41. data/lib/apartment/version.rb +3 -1
  42. data/lib/apartment.rb +46 -15
  43. data/lib/generators/apartment/install/install_generator.rb +4 -3
  44. data/lib/generators/apartment/install/templates/apartment.rb +10 -3
  45. data/lib/tasks/apartment.rake +48 -87
  46. data/ros-apartment.gemspec +65 -0
  47. metadata +181 -264
  48. data/.github/ISSUE_TEMPLATE.md +0 -21
  49. data/.travis.yml +0 -65
  50. data/HISTORY.md +0 -398
  51. data/TODO.md +0 -51
  52. data/apartment.gemspec +0 -46
  53. data/docker-compose.yml +0 -33
  54. data/gemfiles/rails_4_2.gemfile +0 -23
  55. data/gemfiles/rails_5_0.gemfile +0 -22
  56. data/gemfiles/rails_5_1.gemfile +0 -22
  57. data/gemfiles/rails_5_2.gemfile +0 -18
  58. data/gemfiles/rails_6_0.gemfile +0 -22
  59. data/gemfiles/rails_master.gemfile +0 -22
  60. data/lib/apartment/reloader.rb +0 -21
  61. data/spec/adapters/jdbc_mysql_adapter_spec.rb +0 -19
  62. data/spec/adapters/jdbc_postgresql_adapter_spec.rb +0 -41
  63. data/spec/adapters/mysql2_adapter_spec.rb +0 -59
  64. data/spec/adapters/postgresql_adapter_spec.rb +0 -61
  65. data/spec/adapters/sqlite3_adapter_spec.rb +0 -83
  66. data/spec/apartment_spec.rb +0 -11
  67. data/spec/config/database.yml.sample +0 -49
  68. data/spec/dummy/Rakefile +0 -7
  69. data/spec/dummy/app/controllers/application_controller.rb +0 -6
  70. data/spec/dummy/app/helpers/application_helper.rb +0 -2
  71. data/spec/dummy/app/models/company.rb +0 -3
  72. data/spec/dummy/app/models/user.rb +0 -3
  73. data/spec/dummy/app/views/application/index.html.erb +0 -1
  74. data/spec/dummy/app/views/layouts/application.html.erb +0 -14
  75. data/spec/dummy/config/application.rb +0 -49
  76. data/spec/dummy/config/boot.rb +0 -11
  77. data/spec/dummy/config/database.yml.sample +0 -44
  78. data/spec/dummy/config/environment.rb +0 -5
  79. data/spec/dummy/config/environments/development.rb +0 -28
  80. data/spec/dummy/config/environments/production.rb +0 -51
  81. data/spec/dummy/config/environments/test.rb +0 -34
  82. data/spec/dummy/config/initializers/apartment.rb +0 -4
  83. data/spec/dummy/config/initializers/backtrace_silencers.rb +0 -7
  84. data/spec/dummy/config/initializers/inflections.rb +0 -10
  85. data/spec/dummy/config/initializers/mime_types.rb +0 -5
  86. data/spec/dummy/config/initializers/secret_token.rb +0 -7
  87. data/spec/dummy/config/initializers/session_store.rb +0 -8
  88. data/spec/dummy/config/locales/en.yml +0 -5
  89. data/spec/dummy/config/routes.rb +0 -3
  90. data/spec/dummy/config.ru +0 -4
  91. data/spec/dummy/db/migrate/20110613152810_create_dummy_models.rb +0 -39
  92. data/spec/dummy/db/migrate/20111202022214_create_table_books.rb +0 -14
  93. data/spec/dummy/db/migrate/20180415260934_create_public_tokens.rb +0 -13
  94. data/spec/dummy/db/schema.rb +0 -55
  95. data/spec/dummy/db/seeds/import.rb +0 -5
  96. data/spec/dummy/db/seeds.rb +0 -5
  97. data/spec/dummy/db/test.sqlite3 +0 -0
  98. data/spec/dummy/public/404.html +0 -26
  99. data/spec/dummy/public/422.html +0 -26
  100. data/spec/dummy/public/500.html +0 -26
  101. data/spec/dummy/public/favicon.ico +0 -0
  102. data/spec/dummy/public/stylesheets/.gitkeep +0 -0
  103. data/spec/dummy/script/rails +0 -6
  104. data/spec/dummy_engine/.gitignore +0 -8
  105. data/spec/dummy_engine/Gemfile +0 -15
  106. data/spec/dummy_engine/Rakefile +0 -34
  107. data/spec/dummy_engine/bin/rails +0 -12
  108. data/spec/dummy_engine/config/initializers/apartment.rb +0 -51
  109. data/spec/dummy_engine/dummy_engine.gemspec +0 -24
  110. data/spec/dummy_engine/lib/dummy_engine/engine.rb +0 -4
  111. data/spec/dummy_engine/lib/dummy_engine/version.rb +0 -3
  112. data/spec/dummy_engine/lib/dummy_engine.rb +0 -4
  113. data/spec/dummy_engine/test/dummy/Rakefile +0 -6
  114. data/spec/dummy_engine/test/dummy/config/application.rb +0 -22
  115. data/spec/dummy_engine/test/dummy/config/boot.rb +0 -5
  116. data/spec/dummy_engine/test/dummy/config/database.yml +0 -25
  117. data/spec/dummy_engine/test/dummy/config/environment.rb +0 -5
  118. data/spec/dummy_engine/test/dummy/config/environments/development.rb +0 -37
  119. data/spec/dummy_engine/test/dummy/config/environments/production.rb +0 -78
  120. data/spec/dummy_engine/test/dummy/config/environments/test.rb +0 -39
  121. data/spec/dummy_engine/test/dummy/config/initializers/assets.rb +0 -8
  122. data/spec/dummy_engine/test/dummy/config/initializers/backtrace_silencers.rb +0 -7
  123. data/spec/dummy_engine/test/dummy/config/initializers/cookies_serializer.rb +0 -3
  124. data/spec/dummy_engine/test/dummy/config/initializers/filter_parameter_logging.rb +0 -4
  125. data/spec/dummy_engine/test/dummy/config/initializers/inflections.rb +0 -16
  126. data/spec/dummy_engine/test/dummy/config/initializers/mime_types.rb +0 -4
  127. data/spec/dummy_engine/test/dummy/config/initializers/session_store.rb +0 -3
  128. data/spec/dummy_engine/test/dummy/config/initializers/wrap_parameters.rb +0 -14
  129. data/spec/dummy_engine/test/dummy/config/locales/en.yml +0 -23
  130. data/spec/dummy_engine/test/dummy/config/routes.rb +0 -56
  131. data/spec/dummy_engine/test/dummy/config/secrets.yml +0 -22
  132. data/spec/dummy_engine/test/dummy/config.ru +0 -4
  133. data/spec/examples/connection_adapter_examples.rb +0 -42
  134. data/spec/examples/generic_adapter_custom_configuration_example.rb +0 -95
  135. data/spec/examples/generic_adapter_examples.rb +0 -163
  136. data/spec/examples/schema_adapter_examples.rb +0 -234
  137. data/spec/integration/apartment_rake_integration_spec.rb +0 -107
  138. data/spec/integration/query_caching_spec.rb +0 -81
  139. data/spec/integration/use_within_an_engine_spec.rb +0 -28
  140. data/spec/schemas/v1.rb +0 -16
  141. data/spec/schemas/v2.rb +0 -43
  142. data/spec/schemas/v3.rb +0 -49
  143. data/spec/spec_helper.rb +0 -61
  144. data/spec/support/apartment_helpers.rb +0 -43
  145. data/spec/support/capybara_sessions.rb +0 -15
  146. data/spec/support/config.rb +0 -10
  147. data/spec/support/contexts.rb +0 -52
  148. data/spec/support/requirements.rb +0 -35
  149. data/spec/support/setup.rb +0 -46
  150. data/spec/tasks/apartment_rake_spec.rb +0 -129
  151. data/spec/tenant_spec.rb +0 -190
  152. data/spec/unit/config_spec.rb +0 -112
  153. data/spec/unit/elevators/domain_spec.rb +0 -32
  154. data/spec/unit/elevators/first_subdomain_spec.rb +0 -24
  155. data/spec/unit/elevators/generic_spec.rb +0 -54
  156. data/spec/unit/elevators/host_hash_spec.rb +0 -32
  157. data/spec/unit/elevators/host_spec.rb +0 -89
  158. data/spec/unit/elevators/subdomain_spec.rb +0 -76
  159. data/spec/unit/migrator_spec.rb +0 -77
  160. 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
- module Apartment
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 = [: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]
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, :connection_config, :establish_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).with_indifferent_access
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', 'schema.rb')
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 = "#{Rails.root}/db/seeds.rb"
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{|method| remove_instance_variable(:"@#{method}") if instance_variable_defined?(:"@#{method}") }
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('../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,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