ros-apartment 2.6.1 → 2.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e57016e050d57190e3baa0693ccd7e2e094db1cdb864f9c75e193ce06fcdddbb
4
- data.tar.gz: 65b587e5c59e03e957e42c8ca0c0cf4d078859e078eebdcda4594e69284c3175
3
+ metadata.gz: 7c5ea61531091303bdf3ab60326eadb7ffda6ea92b03079fd8a93583067037c7
4
+ data.tar.gz: 95c482e8304a914d6098f1f4ea498f93e1a4827d851e4ea4a283f18ca51079ae
5
5
  SHA512:
6
- metadata.gz: d1ebfaf0a5e4da01bceed2f3cec330aefec6bbfdb5e98dd706ec7b0305cb8bc820e58106ebb60854fc7f5d8735c02d426c87a17a9f16c4b12f34104059644419
7
- data.tar.gz: 1a72b322cf5913313aedfb1f84d3117c5cbbd5ba36a7b62ab1cf10d2e3987d8eca8b465cfc38538b86482a08fd8412e43e1d39708d3a952e4fad8c946b0b0203
6
+ metadata.gz: '09f36cbd0d164f3f692aaf9b2a60b55aa5289537b07c299e49d651a1f1fe6aba6a8d67578076ccad94c2ad62bcaf46e74900c532664f696d1dd4ae673fb49d1d'
7
+ data.tar.gz: b38b9708487fa0e64c93d8349f922dafe2678b09020f5c5a5e77aca5acff4510254737094636d3c25a03e57e76280a20c403fe4021b6e64bcb3e9956b4a77acb
@@ -7,6 +7,7 @@ inherit_gem:
7
7
  AllCops:
8
8
  Exclude:
9
9
  - 'gemfiles/**/*.gemfile'
10
+ - 'gemfiles/vendor/**/*'
10
11
 
11
12
  Style/WordArray:
12
13
  Exclude:
@@ -1,4 +1,5 @@
1
1
  ---
2
2
  tracker: github
3
+ issue_placement: beginning
3
4
  project_id:
4
5
  - rails-on-services/apartment
@@ -29,10 +29,15 @@ before_install:
29
29
  - sudo /etc/init.d/mysql stop
30
30
  - sudo /etc/init.d/postgresql stop
31
31
  - docker-compose up -d
32
+
32
33
  env:
33
34
  RUBY_GC_MALLOC_LIMIT: 90000000
34
35
  RUBY_GC_HEAP_FREE_SLOTS: 200000
35
36
  jobs:
37
+ include:
38
+ - name: Rubocop Lint
39
+ script: gem install perx-rubocop && rubocop
40
+
36
41
  allow_failures:
37
42
  - rvm: ruby-head
38
43
  - rvm: jruby-head
data/Gemfile CHANGED
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- source 'http://rubygems.org'
3
+ source 'https://my.diffend.io/protect/gems'
4
4
 
5
5
  gemspec
6
6
 
data/HISTORY.md CHANGED
@@ -1,3 +1,60 @@
1
+ # 2.6.1
2
+
3
+ ## Enhancements
4
+ - N/a
5
+
6
+ ## Bugfixes
7
+ - [Resolves influitive#607] Avoid early connection
8
+ - <https://github.com/rails-on-services/apartment/pull/39>
9
+ - <https://github.com/rails-on-services/apartment/pull/53>
10
+ - <https://github.com/rails-on-services/apartment/pull/51>
11
+ - [Resolves #52] Rake db:setup tries to seed non existent tenant - <https://github.com/rails-on-services/apartment/pull/54>
12
+ - [Resolves #56] DB rollback uses second last migration - <https://github.com/rails-on-services/apartment/pull/57>
13
+
14
+ ## Chores
15
+ - N/a
16
+
17
+ # 2.6.0
18
+
19
+ ## Enhancements
20
+ - [Resolves #26] Support configuration for skip checking of schema existence before switching
21
+ - [Resolves #41] Add tenant info to console boot
22
+
23
+ ## Bugfixes
24
+ - [Resolves #37] Custom Console deprecation warning
25
+ - [Resolves #42] After switch callback not working with nil argument
26
+
27
+ ## Chores
28
+ - Updated github actions configuration to run on PRs as well
29
+
30
+ # 2.5.0
31
+
32
+ ## Enhancements
33
+ - [Resolves #6] Adds support for rails 6.1
34
+ - [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.
35
+ - [Resolves #35] Cache keys are now tenant dependent
36
+
37
+ ## Bugfixes
38
+ - [Resolves #27] Manually switching connection between read and write forgets the schema
39
+
40
+ ## Chores
41
+ - [Resolves #31] Add latest ruby versions to test matrix
42
+
43
+ # 2.4.0
44
+
45
+ ## Enhancements
46
+ - [Resolves #14] Add console info about tenants and fast switches #17
47
+ - Skip init if we're running webpacker:compile #18
48
+
49
+ ## Bugfixes
50
+ - Don't crash when no database connection is present #16
51
+ - Rescuing ActiveRecord::NoDatabaseError when dropping tenants #19
52
+
53
+ ## Chores
54
+ - Rakefile should use mysql port from configuration #5
55
+ - [Resolves #9] Cleanup rubocop todo #8
56
+ - Cleanup travis matrix #23
57
+
1
58
  # 2.3.0
2
59
  * January 3, 2020
3
60
 
data/README.md CHANGED
@@ -368,12 +368,12 @@ Enable this option with:
368
368
  config.use_sql = true
369
369
  ```
370
370
 
371
- ### Providing a Different default_schema
371
+ ### Providing a Different default_tenant
372
372
 
373
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:
374
374
 
375
375
  ```ruby
376
- config.default_schema = "some_other_schema"
376
+ config.default_tenant = "some_other_schema"
377
377
  ```
378
378
 
379
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.
@@ -446,7 +446,7 @@ schema_search_path: "public,shared_extensions"
446
446
  ...
447
447
  ```
448
448
 
449
- This would be for a config with `default_schema` 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:
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:
450
450
 
451
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)
452
452
  2. Run `heroku pg:psql` from your command line
data/TODO.md CHANGED
@@ -46,6 +46,5 @@
46
46
 
47
47
  Quick TODOs
48
48
 
49
- 1. `default_tenant` should be up to the adapter, not the Apartment class, deprecate `default_schema`
50
49
  2. deprecation.rb rescues everything, we have a hard dependency on ActiveSupport so this is unnecessary
51
50
  3.
@@ -8,6 +8,7 @@ require 'apartment/tenant'
8
8
 
9
9
  # require_relative 'apartment/arel/visitors/postgresql'
10
10
 
11
+ require_relative 'apartment/active_record/log_subscriber'
11
12
  require_relative 'apartment/active_record/connection_handling' if ActiveRecord.version.release >= Gem::Version.new('6.0')
12
13
 
13
14
  if ActiveRecord.version.release >= Gem::Version.new('6.1')
@@ -19,12 +20,12 @@ module Apartment
19
20
  class << self
20
21
  extend Forwardable
21
22
 
22
- ACCESSOR_METHODS = %i[use_schemas use_sql seed_after_create prepend_environment
23
- append_environment with_multi_server_setup tenant_presence_check].freeze
23
+ ACCESSOR_METHODS = %i[use_schemas use_sql seed_after_create prepend_environment default_tenant
24
+ append_environment with_multi_server_setup tenant_presence_check active_record_log].freeze
24
25
 
25
26
  WRITER_METHODS = %i[tenant_names database_schema_file excluded_models
26
- default_schema persistent_schemas connection_class
27
- tld_length db_migrate_tenants seed_data_file
27
+ persistent_schemas connection_class
28
+ db_migrate_tenants seed_data_file
28
29
  parallel_migration_threads pg_excluded_names].freeze
29
30
 
30
31
  attr_accessor(*ACCESSOR_METHODS)
@@ -53,6 +54,10 @@ module Apartment
53
54
  extract_tenant_config
54
55
  end
55
56
 
57
+ def tld_length=(_)
58
+ Apartment::Deprecation.warn('`config.tld_length` have no effect because it was removed in https://github.com/influitive/apartment/pull/309')
59
+ end
60
+
56
61
  def db_config_for(tenant)
57
62
  (tenants_with_config[tenant] || connection_config)
58
63
  end
@@ -70,15 +75,9 @@ module Apartment
70
75
  @excluded_models || []
71
76
  end
72
77
 
73
- def default_schema
74
- @default_schema || 'public' # TODO: 'public' is postgres specific
75
- end
76
-
77
78
  def parallel_migration_threads
78
79
  @parallel_migration_threads || 0
79
80
  end
80
- alias default_tenant default_schema
81
- alias default_tenant= default_schema=
82
81
 
83
82
  def persistent_schemas
84
83
  @persistent_schemas || []
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveRecord
4
+ class LogSubscriber
5
+ def apartment_log
6
+ return unless Apartment.active_record_log
7
+
8
+ database = color("[#{Apartment.connection.current_database}] ", ActiveSupport::LogSubscriber::MAGENTA, true)
9
+ schema = nil
10
+ schema = color("[#{Apartment.connection.schema_search_path.tr('"', '')}] ", ActiveSupport::LogSubscriber::YELLOW, true) unless Apartment.connection.schema_search_path.nil?
11
+ "#{database}#{schema}"
12
+ end
13
+
14
+ def payload_binds(binds, type_casted_binds)
15
+ return unless (binds || []).empty?
16
+
17
+ casted_params = type_casted_binds(type_casted_binds)
18
+ binds = ' ' + binds.zip(casted_params).map { |attr, value| render_bind(attr, value) }.inspect
19
+ binds
20
+ end
21
+
22
+ def sql(event)
23
+ self.class.runtime += event.duration
24
+ return unless logger.debug?
25
+
26
+ payload = event.payload
27
+
28
+ return if IGNORE_PAYLOAD_NAMES.include?(payload[:name])
29
+
30
+ name = "#{payload[:name]} (#{event.duration.round(1)}ms)"
31
+ name = "CACHE #{name}" if payload[:cached]
32
+ sql = payload[:sql]
33
+ binds = payload_binds(payload[:binds], payload[:type_casted_binds])
34
+
35
+ name = colorize_payload_name(name, payload[:name])
36
+ sql = color(sql, sql_color(sql), true) if colorize_logging
37
+
38
+ debug " #{apartment_log}#{name} #{sql}#{binds}"
39
+ end
40
+ end
41
+ end
@@ -54,7 +54,6 @@ module Apartment
54
54
  def default_tenant
55
55
  @default_tenant || Apartment.default_tenant
56
56
  end
57
- alias default_schema default_tenant # TODO: deprecate default_schema
58
57
 
59
58
  # Drop the tenant
60
59
  #
@@ -39,6 +39,8 @@ module Apartment
39
39
  # Reset current tenant to the default_tenant
40
40
  #
41
41
  def reset
42
+ return unless default_tenant
43
+
42
44
  Apartment.connection.execute "use `#{default_tenant}`"
43
45
  end
44
46
 
@@ -30,6 +30,10 @@ module Apartment
30
30
  reset
31
31
  end
32
32
 
33
+ def default_tenant
34
+ @default_tenant = Apartment.default_tenant || 'public'
35
+ end
36
+
33
37
  # Reset schema search path to the default schema_search_path
34
38
  #
35
39
  # @return {String} default schema search path
@@ -19,6 +19,7 @@ module Apartment
19
19
  config.prepend_environment = false
20
20
  config.append_environment = false
21
21
  config.tenant_presence_check = true
22
+ config.active_record_log = false
22
23
  end
23
24
 
24
25
  ActiveRecord::Migrator.migrations_paths = Rails.application.paths['db/migrate'].to_a
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Apartment
4
+ module TaskHelper
5
+ def self.each_tenant(&block)
6
+ Parallel.each(tenants_without_default, in_threads: Apartment.parallel_migration_threads) do |tenant|
7
+ block.call(tenant)
8
+ end
9
+ end
10
+
11
+ def self.tenants_without_default
12
+ tenants - [Apartment.default_tenant]
13
+ end
14
+
15
+ def self.tenants
16
+ ENV['DB'] ? ENV['DB'].split(',').map(&:strip) : Apartment.tenant_names || []
17
+ end
18
+
19
+ def self.warn_if_tenants_empty
20
+ return unless tenants.empty? && ENV['IGNORE_EMPTY_TENANTS'] != 'true'
21
+
22
+ # rubocop:disable Rails/Output
23
+ puts <<-WARNING
24
+ [WARNING] - The list of tenants to migrate appears to be empty. This could mean a few things:
25
+
26
+ 1. You may not have created any, in which case you can ignore this message
27
+ 2. You've run `apartment:migrate` directly without loading the Rails environment
28
+ * `apartment:migrate` is now deprecated. Tenants will automatically be migrated with `db:migrate`
29
+
30
+ Note that your tenants currently haven't been migrated. You'll need to run `db:migrate` to rectify this.
31
+ WARNING
32
+ # rubocop:enable Rails/Output
33
+ end
34
+ end
35
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Apartment
4
- VERSION = '2.6.1'
4
+ VERSION = '2.7.0'
5
5
  end
@@ -96,6 +96,11 @@ Apartment.configure do |config|
96
96
  # the new tenant
97
97
  #
98
98
  # config.pg_excluded_names = ["uuid_generate_v4"]
99
+
100
+ # Specifies whether the database and schema (when using PostgreSQL schemas) will prepend in ActiveRecord log.
101
+ # Uncomment the line below if you want to enable this behavior.
102
+ #
103
+ # config.active_record_log = true
99
104
  end
100
105
 
101
106
  # Setup a custom Tenant switching middleware. The Proc should return the name of the Tenant that
@@ -1,12 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'apartment/migrator'
4
+ require 'apartment/tasks/task_helper'
4
5
  require 'parallel'
5
6
 
6
7
  apartment_namespace = namespace :apartment do
7
8
  desc 'Create all tenants'
8
9
  task :create do
9
- tenants.each do |tenant|
10
+ Apartment::TaskHelper.tenants.each do |tenant|
10
11
  begin
11
12
  puts("Creating #{tenant} tenant")
12
13
  Apartment::Tenant.create(tenant)
@@ -18,7 +19,7 @@ apartment_namespace = namespace :apartment do
18
19
 
19
20
  desc 'Drop all tenants'
20
21
  task :drop do
21
- tenants.each do |tenant|
22
+ Apartment::TaskHelper.tenants.each do |tenant|
22
23
  begin
23
24
  puts("Dropping #{tenant} tenant")
24
25
  Apartment::Tenant.drop(tenant)
@@ -30,8 +31,8 @@ apartment_namespace = namespace :apartment do
30
31
 
31
32
  desc 'Migrate all tenants'
32
33
  task :migrate do
33
- warn_if_tenants_empty
34
- each_tenant do |tenant|
34
+ Apartment::TaskHelper.warn_if_tenants_empty
35
+ Apartment::TaskHelper.each_tenant do |tenant|
35
36
  begin
36
37
  puts("Migrating #{tenant} tenant")
37
38
  Apartment::Migrator.migrate tenant
@@ -43,9 +44,9 @@ apartment_namespace = namespace :apartment do
43
44
 
44
45
  desc 'Seed all tenants'
45
46
  task :seed do
46
- warn_if_tenants_empty
47
+ Apartment::TaskHelper.warn_if_tenants_empty
47
48
 
48
- each_tenant do |tenant|
49
+ Apartment::TaskHelper.each_tenant do |tenant|
49
50
  begin
50
51
  puts("Seeding #{tenant} tenant")
51
52
  Apartment::Tenant.switch(tenant) do
@@ -59,11 +60,11 @@ apartment_namespace = namespace :apartment do
59
60
 
60
61
  desc 'Rolls the migration back to the previous version (specify steps w/ STEP=n) across all tenants.'
61
62
  task :rollback do
62
- warn_if_tenants_empty
63
+ Apartment::TaskHelper.warn_if_tenants_empty
63
64
 
64
65
  step = ENV['STEP'] ? ENV['STEP'].to_i : 1
65
66
 
66
- each_tenant do |tenant|
67
+ Apartment::TaskHelper.each_tenant do |tenant|
67
68
  begin
68
69
  puts("Rolling back #{tenant} tenant")
69
70
  Apartment::Migrator.rollback tenant, step
@@ -76,12 +77,12 @@ apartment_namespace = namespace :apartment do
76
77
  namespace :migrate do
77
78
  desc 'Runs the "up" for a given migration VERSION across all tenants.'
78
79
  task :up do
79
- warn_if_tenants_empty
80
+ Apartment::TaskHelper.warn_if_tenants_empty
80
81
 
81
82
  version = ENV['VERSION'] ? ENV['VERSION'].to_i : nil
82
83
  raise 'VERSION is required' unless version
83
84
 
84
- each_tenant do |tenant|
85
+ Apartment::TaskHelper.each_tenant do |tenant|
85
86
  begin
86
87
  puts("Migrating #{tenant} tenant up")
87
88
  Apartment::Migrator.run :up, tenant, version
@@ -93,12 +94,12 @@ apartment_namespace = namespace :apartment do
93
94
 
94
95
  desc 'Runs the "down" for a given migration VERSION across all tenants.'
95
96
  task :down do
96
- warn_if_tenants_empty
97
+ Apartment::TaskHelper.warn_if_tenants_empty
97
98
 
98
99
  version = ENV['VERSION'] ? ENV['VERSION'].to_i : nil
99
100
  raise 'VERSION is required' unless version
100
101
 
101
- each_tenant do |tenant|
102
+ Apartment::TaskHelper.each_tenant do |tenant|
102
103
  begin
103
104
  puts("Migrating #{tenant} tenant down")
104
105
  Apartment::Migrator.run :down, tenant, version
@@ -119,32 +120,4 @@ apartment_namespace = namespace :apartment do
119
120
  end
120
121
  end
121
122
  end
122
-
123
- def each_tenant(&block)
124
- Parallel.each(tenants_without_default, in_threads: Apartment.parallel_migration_threads) do |tenant|
125
- block.call(tenant)
126
- end
127
- end
128
-
129
- def tenants_without_default
130
- tenants - [Apartment.default_schema]
131
- end
132
-
133
- def tenants
134
- ENV['DB'] ? ENV['DB'].split(',').map(&:strip) : Apartment.tenant_names || []
135
- end
136
-
137
- def warn_if_tenants_empty
138
- return unless tenants.empty? && ENV['IGNORE_EMPTY_TENANTS'] != 'true'
139
-
140
- puts <<-WARNING
141
- [WARNING] - The list of tenants to migrate appears to be empty. This could mean a few things:
142
-
143
- 1. You may not have created any, in which case you can ignore this message
144
- 2. You've run `apartment:migrate` directly without loading the Rails environment
145
- * `apartment:migrate` is now deprecated. Tenants will automatically be migrated with `db:migrate`
146
-
147
- Note that your tenants currently haven't been migrated. You'll need to run `db:migrate` to rectify this.
148
- WARNING
149
- end
150
123
  end
metadata CHANGED
@@ -1,16 +1,16 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ros-apartment
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.6.1
4
+ version: 2.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan Brunner
8
8
  - Brad Robertson
9
9
  - Rui Baltazar
10
- autorequire:
10
+ autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2020-06-02 00:00:00.000000000 Z
13
+ date: 2020-06-26 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activerecord
@@ -229,7 +229,6 @@ extensions: []
229
229
  extra_rdoc_files: []
230
230
  files:
231
231
  - ".github/ISSUE_TEMPLATE.md"
232
- - ".github/workflows/.rubocop-linter.yml"
233
232
  - ".gitignore"
234
233
  - ".pryrc"
235
234
  - ".rspec"
@@ -255,6 +254,7 @@ files:
255
254
  - lib/apartment.rb
256
255
  - lib/apartment/active_record/connection_handling.rb
257
256
  - lib/apartment/active_record/internal_metadata.rb
257
+ - lib/apartment/active_record/log_subscriber.rb
258
258
  - lib/apartment/active_record/schema_migration.rb
259
259
  - lib/apartment/adapters/abstract_adapter.rb
260
260
  - lib/apartment/adapters/abstract_jdbc_adapter.rb
@@ -278,6 +278,7 @@ files:
278
278
  - lib/apartment/railtie.rb
279
279
  - lib/apartment/reloader.rb
280
280
  - lib/apartment/tasks/enhancements.rb
281
+ - lib/apartment/tasks/task_helper.rb
281
282
  - lib/apartment/tenant.rb
282
283
  - lib/apartment/version.rb
283
284
  - lib/generators/apartment/install/USAGE
@@ -288,7 +289,7 @@ homepage: https://github.com/rails-on-services/apartment
288
289
  licenses:
289
290
  - MIT
290
291
  metadata: {}
291
- post_install_message:
292
+ post_install_message:
292
293
  rdoc_options: []
293
294
  require_paths:
294
295
  - lib
@@ -304,7 +305,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
304
305
  version: '0'
305
306
  requirements: []
306
307
  rubygems_version: 3.0.3
307
- signing_key:
308
+ signing_key:
308
309
  specification_version: 4
309
310
  summary: A Ruby gem for managing database multitenancy. Apartment Gem drop in replacement
310
311
  test_files: []
@@ -1,22 +0,0 @@
1
- name: Rubocop Lint
2
-
3
- on: [push, pull_request]
4
-
5
- jobs:
6
- build:
7
-
8
- runs-on: ubuntu-latest
9
-
10
- container:
11
- image: ruby:2.6.5
12
-
13
- steps:
14
- - uses: actions/checkout@v1
15
- - name: Rubocop Linter
16
- uses: andrewmcodes/rubocop-linter-action@v2.0.0
17
- with:
18
- additional_gems: 'perx-rubocop:0.0.3'
19
- fail_level: 'warning'
20
- version: '0.77.0'
21
- env:
22
- GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}