From da7880b73f017fa8f844a14fe6e839eebc736325 Mon Sep 17 00:00:00 2001 From: Mark Siemers Date: Tue, 13 Apr 2021 14:01:15 -0400 Subject: [PATCH 01/42] Allow a list of schemas when switching using schemas --- README.md | 10 ++++++++++ lib/apartment/adapters/jdbc_postgresql_adapter.rb | 6 ++---- lib/apartment/adapters/postgresql_adapter.rb | 12 ++++++++---- spec/examples/schema_adapter_examples.rb | 7 +++++++ 4 files changed, 27 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index fa033c09..4ffc9231 100644 --- a/README.md +++ b/README.md @@ -101,6 +101,16 @@ switched back at the end of the block to what it was before. There is also `switch!` which doesn't take a block, but it's recommended to use `switch`. To return to the default tenant, you can call `switch` with no arguments. +#### Multiple Tenants + +When using schemas, you can also pass in a list of schemas if desired. Any tables defined in a schema earlier in the chain will be referenced first, so this is only useful if you have a schema with only some of the tables defined: + +```ruby +Apartment::Tenant.switch(['tenant_1', 'tenant_2']) do + # ... +end +``` + ### Switching Tenants per request You can have Apartment route to the appropriate tenant by adding some Rack middleware. diff --git a/lib/apartment/adapters/jdbc_postgresql_adapter.rb b/lib/apartment/adapters/jdbc_postgresql_adapter.rb index 70dbadf3..3e9494b0 100644 --- a/lib/apartment/adapters/jdbc_postgresql_adapter.rb +++ b/lib/apartment/adapters/jdbc_postgresql_adapter.rb @@ -38,11 +38,9 @@ class JDBCPostgresqlSchemaAdapter < PostgresqlSchemaAdapter # def connect_to_new(tenant = nil) return reset if tenant.nil? + raise ActiveRecord::StatementInvalid, "Could not find schema #{tenant}" unless schema_exists?(tenant) - tenant = tenant.to_s - raise ActiveRecord::StatementInvalid, "Could not find schema #{tenant}" unless tenant_exists?(tenant) - - @current = tenant + @current = tenant.is_a?(Array) ? tenant.map(&:to_s) : tenant.to_s Apartment.connection.schema_search_path = full_search_path rescue ActiveRecord::StatementInvalid, ActiveRecord::JDBCError raise TenantNotFound, "One of the following schema(s) is invalid: #{full_search_path}" diff --git a/lib/apartment/adapters/postgresql_adapter.rb b/lib/apartment/adapters/postgresql_adapter.rb index 9540f016..17ad4599 100644 --- a/lib/apartment/adapters/postgresql_adapter.rb +++ b/lib/apartment/adapters/postgresql_adapter.rb @@ -72,11 +72,9 @@ def drop_command(conn, tenant) # def connect_to_new(tenant = nil) return reset if tenant.nil? + raise ActiveRecord::StatementInvalid, "Could not find schema #{tenant}" unless schema_exists?(tenant) - tenant = tenant.to_s - raise ActiveRecord::StatementInvalid, "Could not find schema #{tenant}" unless tenant_exists?(tenant) - - @current = tenant + @current = tenant.is_a?(Array) ? tenant.map(&:to_s) : tenant.to_s Apartment.connection.schema_search_path = full_search_path # When the PostgreSQL version is < 9.3, @@ -149,6 +147,12 @@ def reset_sequence_names c.remove_instance_variable :@sequence_name if c.instance_variable_defined?(:@sequence_name) end end + + def schema_exists?(schemas) + return true unless Apartment.tenant_presence_check + + Array(schemas).all? { |schema| Apartment.connection.schema_exists?(schema.to_s) } + end end # Another Adapter for Postgresql when using schemas and SQL diff --git a/spec/examples/schema_adapter_examples.rb b/spec/examples/schema_adapter_examples.rb index 447322e3..0eaaa250 100644 --- a/spec/examples/schema_adapter_examples.rb +++ b/spec/examples/schema_adapter_examples.rb @@ -127,6 +127,13 @@ expect(connection.schema_search_path).to start_with %("#{public_schema}") expect(User.sequence_name).to eq "#{public_schema}.#{User.table_name}_id_seq" end + + it 'allows a list of schemas' do + subject.switch([schema1, schema2]) do + expect(connection.schema_search_path).to include %("#{schema1}") + expect(connection.schema_search_path).to include %("#{schema2}") + end + end end describe '#reset' do From b3937d6c7b900065a1e92c3f721566af426c8787 Mon Sep 17 00:00:00 2001 From: Mark Siemers Date: Tue, 13 Apr 2021 14:09:00 -0400 Subject: [PATCH 02/42] Remove unnecessary rubocop disable directive --- lib/apartment/railtie.rb | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/apartment/railtie.rb b/lib/apartment/railtie.rb index 649c6fcd..20826e62 100644 --- a/lib/apartment/railtie.rb +++ b/lib/apartment/railtie.rb @@ -74,11 +74,9 @@ class Railtie < Rails::Railtie # Overrides reload! to also call Apartment::Tenant.init as well # so that the reloaded classes have the proper table_names - # rubocop:disable Lint/Debugger console do require 'apartment/console' end - # rubocop:enable Lint/Debugger end end end From af01032abee7c7cd96de05dd7f9b82bd2421434d Mon Sep 17 00:00:00 2001 From: Rui Baltazar Date: Sat, 1 May 2021 16:30:28 +0800 Subject: [PATCH 03/42] Fixed rubocop version and fixed reported violations --- Gemfile | 2 +- lib/apartment/adapters/abstract_adapter.rb | 2 - lib/apartment/console.rb | 2 - lib/apartment/log_subscriber.rb | 2 - lib/tasks/apartment.rake | 44 ++++++++----------- spec/adapters/sqlite3_adapter_spec.rb | 24 ++++------ .../generic_adapters_callbacks_examples.rb | 2 - spec/examples/schema_adapter_examples.rb | 8 ++-- spec/spec_helper.rb | 2 - spec/tenant_spec.rb | 8 ++-- spec/unit/elevators/generic_spec.rb | 2 - 11 files changed, 34 insertions(+), 64 deletions(-) diff --git a/Gemfile b/Gemfile index 0949319d..696e58dc 100644 --- a/Gemfile +++ b/Gemfile @@ -5,7 +5,7 @@ source 'http://rubygems.org' gemspec gem 'rails', '>= 3.1.2' -gem 'rubocop' +gem 'rubocop', '~> 0.93' group :local do gem 'guard-rspec', '~> 4.2' diff --git a/lib/apartment/adapters/abstract_adapter.rb b/lib/apartment/adapters/abstract_adapter.rb index e59ba523..8ca4f308 100644 --- a/lib/apartment/adapters/abstract_adapter.rb +++ b/lib/apartment/adapters/abstract_adapter.rb @@ -201,13 +201,11 @@ def import_database_schema # @param {String} tenant: Database name # @param {Boolean} with_database: if true, use the actual tenant's db name # if false, use the default db name from the db - # rubocop:disable Style/OptionalBooleanParameter def multi_tenantify(tenant, with_database = true) db_connection_config(tenant).tap do |config| multi_tenantify_with_tenant_db_name(config, tenant) if with_database end end - # rubocop:enable Style/OptionalBooleanParameter def multi_tenantify_with_tenant_db_name(config, tenant) config[:database] = environmentify(tenant) diff --git a/lib/apartment/console.rb b/lib/apartment/console.rb index 91721222..d1b83baf 100644 --- a/lib/apartment/console.rb +++ b/lib/apartment/console.rb @@ -4,7 +4,6 @@ # This is unfortunate, but I haven't figured out how to hook into the reload process *after* files are reloaded # reloads the environment -# rubocop:disable Style/OptionalBooleanParameter def reload!(print = true) puts 'Reloading...' if print @@ -14,7 +13,6 @@ def reload!(print = true) Apartment::Tenant.init true end -# rubocop:enable Style/OptionalBooleanParameter def st(schema_name = nil) if schema_name.nil? diff --git a/lib/apartment/log_subscriber.rb b/lib/apartment/log_subscriber.rb index 119acbee..43c6a601 100644 --- a/lib/apartment/log_subscriber.rb +++ b/lib/apartment/log_subscriber.rb @@ -6,11 +6,9 @@ module Apartment # Custom Log subscriber to include database name and schema name in sql logs class LogSubscriber < ActiveRecord::LogSubscriber # NOTE: for some reason, if the method definition is not here, then the custom debug method is not called - # rubocop:disable Lint/UselessMethodDefinition def sql(event) super(event) end - # rubocop:enable Lint/UselessMethodDefinition private diff --git a/lib/tasks/apartment.rake b/lib/tasks/apartment.rake index 5ae47c0f..d911ce31 100644 --- a/lib/tasks/apartment.rake +++ b/lib/tasks/apartment.rake @@ -39,15 +39,13 @@ apartment_namespace = namespace :apartment do Apartment::TaskHelper.warn_if_tenants_empty Apartment::TaskHelper.each_tenant do |tenant| - begin - Apartment::TaskHelper.create_tenant(tenant) - puts("Seeding #{tenant} tenant") - Apartment::Tenant.switch(tenant) do - Apartment::Tenant.seed - end - rescue Apartment::TenantNotFound => e - puts e.message + Apartment::TaskHelper.create_tenant(tenant) + puts("Seeding #{tenant} tenant") + Apartment::Tenant.switch(tenant) do + Apartment::Tenant.seed end + rescue Apartment::TenantNotFound => e + puts e.message end end @@ -58,12 +56,10 @@ apartment_namespace = namespace :apartment do step = ENV['STEP'] ? ENV['STEP'].to_i : 1 Apartment::TaskHelper.each_tenant do |tenant| - begin - puts("Rolling back #{tenant} tenant") - Apartment::Migrator.rollback tenant, step - rescue Apartment::TenantNotFound => e - puts e.message - end + puts("Rolling back #{tenant} tenant") + Apartment::Migrator.rollback tenant, step + rescue Apartment::TenantNotFound => e + puts e.message end end @@ -76,12 +72,10 @@ apartment_namespace = namespace :apartment do raise 'VERSION is required' unless version Apartment::TaskHelper.each_tenant do |tenant| - begin - puts("Migrating #{tenant} tenant up") - Apartment::Migrator.run :up, tenant, version - rescue Apartment::TenantNotFound => e - puts e.message - end + puts("Migrating #{tenant} tenant up") + Apartment::Migrator.run :up, tenant, version + rescue Apartment::TenantNotFound => e + puts e.message end end @@ -93,12 +87,10 @@ apartment_namespace = namespace :apartment do raise 'VERSION is required' unless version Apartment::TaskHelper.each_tenant do |tenant| - begin - puts("Migrating #{tenant} tenant down") - Apartment::Migrator.run :down, tenant, version - rescue Apartment::TenantNotFound => e - puts e.message - end + puts("Migrating #{tenant} tenant down") + Apartment::Migrator.run :down, tenant, version + rescue Apartment::TenantNotFound => e + puts e.message end end diff --git a/spec/adapters/sqlite3_adapter_spec.rb b/spec/adapters/sqlite3_adapter_spec.rb index f05c76ba..0e778621 100644 --- a/spec/adapters/sqlite3_adapter_spec.rb +++ b/spec/adapters/sqlite3_adapter_spec.rb @@ -40,11 +40,9 @@ def tenant_names end after do - begin - subject.drop db_name - rescue StandardError => _e - nil - end + subject.drop db_name + rescue StandardError => _e + nil end it 'should create a new database' do @@ -61,11 +59,9 @@ def tenant_names end after do - begin - subject.drop db_name - rescue StandardError => _e - nil - end + subject.drop db_name + rescue StandardError => _e + nil end it 'should create a new database' do @@ -85,11 +81,9 @@ def tenant_names end after do - begin - subject.drop db_name - rescue StandardError => _e - nil - end + subject.drop db_name + rescue StandardError => _e + nil end it 'should create a new database' do diff --git a/spec/examples/generic_adapters_callbacks_examples.rb b/spec/examples/generic_adapters_callbacks_examples.rb index 3184a4a1..5d878d38 100644 --- a/spec/examples/generic_adapters_callbacks_examples.rb +++ b/spec/examples/generic_adapters_callbacks_examples.rb @@ -3,11 +3,9 @@ require 'spec_helper' shared_examples_for 'a generic apartment adapter callbacks' do - # rubocop:disable Lint/ConstantDefinitionInBlock class MyProc def self.call(tenant_name); end end - # rubocop:enable Lint/ConstantDefinitionInBlock include Apartment::Spec::AdapterRequirements diff --git a/spec/examples/schema_adapter_examples.rb b/spec/examples/schema_adapter_examples.rb index 0eaaa250..fe87a699 100644 --- a/spec/examples/schema_adapter_examples.rb +++ b/spec/examples/schema_adapter_examples.rb @@ -108,11 +108,9 @@ end after do - begin - subject.drop(db) - rescue StandardError => _e - nil - end + subject.drop(db) + rescue StandardError => _e + nil end end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index f5ba6bbe..68b9c680 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -22,14 +22,12 @@ require 'capybara/rspec' require 'capybara/rails' -# rubocop:disable Lint/ConstantDefinitionInBlock begin require 'pry' silence_warnings { IRB = Pry } rescue LoadError nil end -# rubocop:enable Lint/ConstantDefinitionInBlock ActionMailer::Base.delivery_method = :test ActionMailer::Base.perform_deliveries = true diff --git a/spec/tenant_spec.rb b/spec/tenant_spec.rb index e3aacf04..9fdc8b33 100644 --- a/spec/tenant_spec.rb +++ b/spec/tenant_spec.rb @@ -44,11 +44,9 @@ end after do - begin - subject.drop 'db_with_prefix' - rescue StandardError => _e - nil - end + subject.drop 'db_with_prefix' + rescue StandardError => _e + nil end it 'should create a new database' do diff --git a/spec/unit/elevators/generic_spec.rb b/spec/unit/elevators/generic_spec.rb index f4112e78..b282a5af 100644 --- a/spec/unit/elevators/generic_spec.rb +++ b/spec/unit/elevators/generic_spec.rb @@ -4,13 +4,11 @@ require 'apartment/elevators/generic' describe Apartment::Elevators::Generic do - # rubocop:disable Lint/ConstantDefinitionInBlock class MyElevator < described_class def parse_tenant_name(*) 'tenant2' end end - # rubocop:enable Lint/ConstantDefinitionInBlock subject(:elevator) { described_class.new(proc {}) } From 2a8c359a329cb53d36474e49f29c29a57d83fa02 Mon Sep 17 00:00:00 2001 From: Rui Baltazar Date: Sat, 1 May 2021 16:33:15 +0800 Subject: [PATCH 04/42] added ruby version and reviewdog --- .github/workflows/reviewdog.yml | 21 +++++++++++++++++++++ .ruby-version | 1 + 2 files changed, 22 insertions(+) create mode 100644 .github/workflows/reviewdog.yml create mode 100644 .ruby-version diff --git a/.github/workflows/reviewdog.yml b/.github/workflows/reviewdog.yml new file mode 100644 index 00000000..97074b4d --- /dev/null +++ b/.github/workflows/reviewdog.yml @@ -0,0 +1,21 @@ +name: reviewdog +on: [push, pull_request] +jobs: + rubocop: + name: runner / rubocop + runs-on: ubuntu-latest + steps: + - name: Check out code + uses: actions/checkout@v2 + - name: Read ruby version + run: echo ::set-output name=RUBY_VERSION::$(cat .ruby-version | cut -f 1,2 -d .) + id: rv + - uses: ruby/setup-ruby@v1 + with: + ruby-version: "${{ steps.rv.outputs.RUBY_VERSION }}" + - uses: reviewdog/action-rubocop@v1 + with: + reporter: github-check + rubocop_version: gemfile + rubocop_extensions: rubocop-rails:gemfile rubocop-rspec:gemfile rubocop-performance:gemfile + github_token: ${{ secrets.github_token }} diff --git a/.ruby-version b/.ruby-version new file mode 100644 index 00000000..2c9b4ef4 --- /dev/null +++ b/.ruby-version @@ -0,0 +1 @@ +2.7.3 From 9f838ae6b00b531ae7c3042bccd6a8df90f8a320 Mon Sep 17 00:00:00 2001 From: Rui Baltazar Date: Sat, 1 May 2021 16:34:13 +0800 Subject: [PATCH 05/42] removed rubocop from circleci --- .circleci/config.yml | 8 -------- 1 file changed, 8 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 2f3e3c8b..40a42681 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -1,8 +1,5 @@ version: 2.1 -orbs: - rubocop: hanachin/rubocop@0.0.6 - jobs: build: docker: @@ -71,8 +68,3 @@ workflows: parameters: ruby_version: ["ruby:2.6-buster", "ruby:2.7-buster"] gemfile: ["gemfiles/rails_5_2.gemfile", "gemfiles/rails_6_0.gemfile", "gemfiles/rails_6_1.gemfile"] - - rubocop: - jobs: - - rubocop/rubocop: - version: 0.88.0 From f79ec201e93c2517a2c1dcde92ffa155ebf41dd4 Mon Sep 17 00:00:00 2001 From: Rui Baltazar Date: Mon, 3 May 2021 12:13:41 +0800 Subject: [PATCH 06/42] update rubocop local config and fix remaining broken rules --- .rubocop.yml | 2 ++ Gemfile | 3 +++ lib/apartment/adapters/abstract_adapter.rb | 2 ++ lib/apartment/console.rb | 2 ++ lib/apartment/log_subscriber.rb | 2 ++ lib/tasks/apartment.rake | 10 ++++------ spec/examples/generic_adapters_callbacks_examples.rb | 2 ++ spec/spec_helper.rb | 2 ++ spec/unit/elevators/generic_spec.rb | 2 ++ 9 files changed, 21 insertions(+), 6 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index b7c5c8b6..f7d039b6 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -6,6 +6,8 @@ AllCops: - 'gemfiles/vendor/**/*' - 'spec/dummy_engine/dummy_engine.gemspec' + NewCops: enable + Gemspec/RequiredRubyVersion: Exclude: - 'ros-apartment.gemspec' diff --git a/Gemfile b/Gemfile index 696e58dc..c53bc19e 100644 --- a/Gemfile +++ b/Gemfile @@ -6,6 +6,9 @@ gemspec gem 'rails', '>= 3.1.2' gem 'rubocop', '~> 0.93' +gem 'rubocop-performance' +gem 'rubocop-rails' +gem 'rubocop-rspec' group :local do gem 'guard-rspec', '~> 4.2' diff --git a/lib/apartment/adapters/abstract_adapter.rb b/lib/apartment/adapters/abstract_adapter.rb index 8ca4f308..e59ba523 100644 --- a/lib/apartment/adapters/abstract_adapter.rb +++ b/lib/apartment/adapters/abstract_adapter.rb @@ -201,11 +201,13 @@ def import_database_schema # @param {String} tenant: Database name # @param {Boolean} with_database: if true, use the actual tenant's db name # if false, use the default db name from the db + # rubocop:disable Style/OptionalBooleanParameter def multi_tenantify(tenant, with_database = true) db_connection_config(tenant).tap do |config| multi_tenantify_with_tenant_db_name(config, tenant) if with_database end end + # rubocop:enable Style/OptionalBooleanParameter def multi_tenantify_with_tenant_db_name(config, tenant) config[:database] = environmentify(tenant) diff --git a/lib/apartment/console.rb b/lib/apartment/console.rb index d1b83baf..91721222 100644 --- a/lib/apartment/console.rb +++ b/lib/apartment/console.rb @@ -4,6 +4,7 @@ # This is unfortunate, but I haven't figured out how to hook into the reload process *after* files are reloaded # reloads the environment +# rubocop:disable Style/OptionalBooleanParameter def reload!(print = true) puts 'Reloading...' if print @@ -13,6 +14,7 @@ def reload!(print = true) Apartment::Tenant.init true end +# rubocop:enable Style/OptionalBooleanParameter def st(schema_name = nil) if schema_name.nil? diff --git a/lib/apartment/log_subscriber.rb b/lib/apartment/log_subscriber.rb index 43c6a601..119acbee 100644 --- a/lib/apartment/log_subscriber.rb +++ b/lib/apartment/log_subscriber.rb @@ -6,9 +6,11 @@ module Apartment # Custom Log subscriber to include database name and schema name in sql logs class LogSubscriber < ActiveRecord::LogSubscriber # NOTE: for some reason, if the method definition is not here, then the custom debug method is not called + # rubocop:disable Lint/UselessMethodDefinition def sql(event) super(event) end + # rubocop:enable Lint/UselessMethodDefinition private diff --git a/lib/tasks/apartment.rake b/lib/tasks/apartment.rake index d911ce31..6cb74393 100644 --- a/lib/tasks/apartment.rake +++ b/lib/tasks/apartment.rake @@ -17,12 +17,10 @@ apartment_namespace = namespace :apartment do desc 'Drop all tenants' task :drop do Apartment::TaskHelper.tenants.each do |tenant| - begin - puts("Dropping #{tenant} tenant") - Apartment::Tenant.drop(tenant) - rescue Apartment::TenantNotFound, ActiveRecord::NoDatabaseError => e - puts e.message - end + puts("Dropping #{tenant} tenant") + Apartment::Tenant.drop(tenant) + rescue Apartment::TenantNotFound, ActiveRecord::NoDatabaseError => e + puts e.message end end diff --git a/spec/examples/generic_adapters_callbacks_examples.rb b/spec/examples/generic_adapters_callbacks_examples.rb index 5d878d38..3184a4a1 100644 --- a/spec/examples/generic_adapters_callbacks_examples.rb +++ b/spec/examples/generic_adapters_callbacks_examples.rb @@ -3,9 +3,11 @@ require 'spec_helper' shared_examples_for 'a generic apartment adapter callbacks' do + # rubocop:disable Lint/ConstantDefinitionInBlock class MyProc def self.call(tenant_name); end end + # rubocop:enable Lint/ConstantDefinitionInBlock include Apartment::Spec::AdapterRequirements diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 68b9c680..a7bd4195 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -24,7 +24,9 @@ begin require 'pry' + # rubocop:disable Lint/ConstantDefinitionInBlock silence_warnings { IRB = Pry } + # rubocop:enable Lint/ConstantDefinitionInBlock rescue LoadError nil end diff --git a/spec/unit/elevators/generic_spec.rb b/spec/unit/elevators/generic_spec.rb index b282a5af..f4112e78 100644 --- a/spec/unit/elevators/generic_spec.rb +++ b/spec/unit/elevators/generic_spec.rb @@ -4,11 +4,13 @@ require 'apartment/elevators/generic' describe Apartment::Elevators::Generic do + # rubocop:disable Lint/ConstantDefinitionInBlock class MyElevator < described_class def parse_tenant_name(*) 'tenant2' end end + # rubocop:enable Lint/ConstantDefinitionInBlock subject(:elevator) { described_class.new(proc {}) } From 0ed15c8eed53307b4b52ab0b9139fdae0d1038a7 Mon Sep 17 00:00:00 2001 From: Marcelo Lauxen Date: Tue, 25 May 2021 18:28:59 -0300 Subject: [PATCH 07/42] Add Ruby 3 to build matrix --- .circleci/config.yml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 40a42681..d8411d98 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -66,5 +66,8 @@ workflows: - build: matrix: parameters: - ruby_version: ["ruby:2.6-buster", "ruby:2.7-buster"] + ruby_version: ["ruby:2.6-buster", "ruby:2.7-buster", "ruby:3.0-buster"] gemfile: ["gemfiles/rails_5_2.gemfile", "gemfiles/rails_6_0.gemfile", "gemfiles/rails_6_1.gemfile"] + exclude: + - ruby_version: "ruby:3.0-buster" + gemfile: "gemfiles/rails_5_2.gemfile" From 8031e4447986496c26ab850306b63195b33feda9 Mon Sep 17 00:00:00 2001 From: Rui Baltazar Date: Thu, 6 May 2021 10:44:19 +0800 Subject: [PATCH 08/42] moved dependency to development depenedncy --- .rubocop.yml | 5 +++++ Gemfile | 4 ---- ros-apartment.gemspec | 5 +++++ 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index f7d039b6..ca4d6cb6 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -1,5 +1,10 @@ inherit_from: .rubocop_todo.yml +# require: +# - rubocop-rails +# - rubocop-performance +# - rubocop-rspec + AllCops: Exclude: - 'gemfiles/**/*.gemfile' diff --git a/Gemfile b/Gemfile index c53bc19e..d24bee04 100644 --- a/Gemfile +++ b/Gemfile @@ -5,10 +5,6 @@ source 'http://rubygems.org' gemspec gem 'rails', '>= 3.1.2' -gem 'rubocop', '~> 0.93' -gem 'rubocop-performance' -gem 'rubocop-rails' -gem 'rubocop-rspec' group :local do gem 'guard-rspec', '~> 4.2' diff --git a/ros-apartment.gemspec b/ros-apartment.gemspec index 95a60c35..f3a5a6a6 100644 --- a/ros-apartment.gemspec +++ b/ros-apartment.gemspec @@ -36,6 +36,11 @@ Gem::Specification.new do |s| s.add_development_dependency 'bundler', '>= 1.3', '< 3.0' s.add_development_dependency 'capybara', '~> 2.0' s.add_development_dependency 'rake', '~> 13.0' + s.add_development_dependency 'rubocop', '~> 0.93' + s.add_development_dependency 'rubocop-performance' + s.add_development_dependency 'rubocop-rails' + s.add_development_dependency 'rubocop-rspec' + s.add_development_dependency 'rspec', '~> 3.4' s.add_development_dependency 'rspec-rails', '~> 3.4' From c79245bdbb4d7dd0b68ffeca5eb84a23dc74c98e Mon Sep 17 00:00:00 2001 From: Rui Baltazar Date: Thu, 6 May 2021 10:48:20 +0800 Subject: [PATCH 09/42] moved gems to development dependencies --- Gemfile | 7 ------- ros-apartment.gemspec | 3 ++- 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/Gemfile b/Gemfile index d24bee04..f7200f13 100644 --- a/Gemfile +++ b/Gemfile @@ -3,10 +3,3 @@ source 'http://rubygems.org' gemspec - -gem 'rails', '>= 3.1.2' - -group :local do - gem 'guard-rspec', '~> 4.2' - gem 'pry' -end diff --git a/ros-apartment.gemspec b/ros-apartment.gemspec index f3a5a6a6..e66b87f7 100644 --- a/ros-apartment.gemspec +++ b/ros-apartment.gemspec @@ -34,7 +34,8 @@ Gem::Specification.new do |s| s.add_development_dependency 'appraisal', '~> 2.2' s.add_development_dependency 'bundler', '>= 1.3', '< 3.0' - s.add_development_dependency 'capybara', '~> 2.0' + s.add_development_dependency 'guard-rspec', '~> 4.2' + s.add_development_dependency 'pry' s.add_development_dependency 'rake', '~> 13.0' s.add_development_dependency 'rubocop', '~> 0.93' s.add_development_dependency 'rubocop-performance' From b0622b99b88c4faff5f53f357d9f11bd6a7a5f51 Mon Sep 17 00:00:00 2001 From: Rui Baltazar Date: Thu, 6 May 2021 10:52:42 +0800 Subject: [PATCH 10/42] trying to specify rubocop version so it does not default to latest --- .github/workflows/reviewdog.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/reviewdog.yml b/.github/workflows/reviewdog.yml index 97074b4d..beb4ccbd 100644 --- a/.github/workflows/reviewdog.yml +++ b/.github/workflows/reviewdog.yml @@ -16,6 +16,6 @@ jobs: - uses: reviewdog/action-rubocop@v1 with: reporter: github-check - rubocop_version: gemfile + rubocop_version: '~> 0.93' rubocop_extensions: rubocop-rails:gemfile rubocop-rspec:gemfile rubocop-performance:gemfile github_token: ${{ secrets.github_token }} From 5793f38a3be2f7fbe634998431cdcbec2bf38e34 Mon Sep 17 00:00:00 2001 From: Rui Baltazar Date: Thu, 6 May 2021 10:54:28 +0800 Subject: [PATCH 11/42] do not skip any file --- .github/workflows/reviewdog.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/reviewdog.yml b/.github/workflows/reviewdog.yml index beb4ccbd..4df74d11 100644 --- a/.github/workflows/reviewdog.yml +++ b/.github/workflows/reviewdog.yml @@ -15,6 +15,7 @@ jobs: ruby-version: "${{ steps.rv.outputs.RUBY_VERSION }}" - uses: reviewdog/action-rubocop@v1 with: + filter_mode: nofilter reporter: github-check rubocop_version: '~> 0.93' rubocop_extensions: rubocop-rails:gemfile rubocop-rspec:gemfile rubocop-performance:gemfile From 6dd1e6df266e42ddff8449f45c0cc6feaafa7cc7 Mon Sep 17 00:00:00 2001 From: Rui Baltazar Date: Thu, 6 May 2021 10:55:34 +0800 Subject: [PATCH 12/42] specify version --- .github/workflows/reviewdog.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/reviewdog.yml b/.github/workflows/reviewdog.yml index 4df74d11..f970db03 100644 --- a/.github/workflows/reviewdog.yml +++ b/.github/workflows/reviewdog.yml @@ -17,6 +17,6 @@ jobs: with: filter_mode: nofilter reporter: github-check - rubocop_version: '~> 0.93' + rubocop_version: 0.93.1 rubocop_extensions: rubocop-rails:gemfile rubocop-rspec:gemfile rubocop-performance:gemfile github_token: ${{ secrets.github_token }} From 0a10581d45e8bce37315e90db25ff00f0a46333d Mon Sep 17 00:00:00 2001 From: Rui Baltazar Date: Thu, 6 May 2021 10:59:30 +0800 Subject: [PATCH 13/42] removed rubocop performance rails and rspec --- .github/workflows/reviewdog.yml | 1 - ros-apartment.gemspec | 4 ---- 2 files changed, 5 deletions(-) diff --git a/.github/workflows/reviewdog.yml b/.github/workflows/reviewdog.yml index f970db03..aa6e8b25 100644 --- a/.github/workflows/reviewdog.yml +++ b/.github/workflows/reviewdog.yml @@ -18,5 +18,4 @@ jobs: filter_mode: nofilter reporter: github-check rubocop_version: 0.93.1 - rubocop_extensions: rubocop-rails:gemfile rubocop-rspec:gemfile rubocop-performance:gemfile github_token: ${{ secrets.github_token }} diff --git a/ros-apartment.gemspec b/ros-apartment.gemspec index e66b87f7..4014701c 100644 --- a/ros-apartment.gemspec +++ b/ros-apartment.gemspec @@ -38,10 +38,6 @@ Gem::Specification.new do |s| s.add_development_dependency 'pry' s.add_development_dependency 'rake', '~> 13.0' s.add_development_dependency 'rubocop', '~> 0.93' - s.add_development_dependency 'rubocop-performance' - s.add_development_dependency 'rubocop-rails' - s.add_development_dependency 'rubocop-rspec' - s.add_development_dependency 'rspec', '~> 3.4' s.add_development_dependency 'rspec-rails', '~> 3.4' From 8001d5759a61782207fcfccaa589a30984100429 Mon Sep 17 00:00:00 2001 From: Rui Baltazar Date: Thu, 6 May 2021 11:08:36 +0800 Subject: [PATCH 14/42] addressed rubocop gems out of order --- ros-apartment.gemspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ros-apartment.gemspec b/ros-apartment.gemspec index 4014701c..caf58b6d 100644 --- a/ros-apartment.gemspec +++ b/ros-apartment.gemspec @@ -37,9 +37,9 @@ Gem::Specification.new do |s| s.add_development_dependency 'guard-rspec', '~> 4.2' s.add_development_dependency 'pry' s.add_development_dependency 'rake', '~> 13.0' - s.add_development_dependency 'rubocop', '~> 0.93' s.add_development_dependency 'rspec', '~> 3.4' s.add_development_dependency 'rspec-rails', '~> 3.4' + s.add_development_dependency 'rubocop', '~> 0.93' if defined?(JRUBY_VERSION) s.add_development_dependency 'activerecord-jdbc-adapter' From 82c35797792f78588c007aad092c97689717defb Mon Sep 17 00:00:00 2001 From: Rui Baltazar Date: Fri, 25 Jun 2021 14:33:05 +0800 Subject: [PATCH 15/42] specify rubocop extensions versions --- .github/workflows/reviewdog.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.github/workflows/reviewdog.yml b/.github/workflows/reviewdog.yml index aa6e8b25..5944c121 100644 --- a/.github/workflows/reviewdog.yml +++ b/.github/workflows/reviewdog.yml @@ -19,3 +19,7 @@ jobs: reporter: github-check rubocop_version: 0.93.1 github_token: ${{ secrets.github_token }} + rubocop_extensions: + - rubocop-performance:1.10.2 + - rubocop-rails:2.9.1 + - rubocop-rspec:1.44.1 From 8d41888fbe9157665a0325bd80480402cdb8d7af Mon Sep 17 00:00:00 2001 From: Rui Baltazar Date: Fri, 25 Jun 2021 14:35:10 +0800 Subject: [PATCH 16/42] string with all extensions --- .github/workflows/reviewdog.yml | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/.github/workflows/reviewdog.yml b/.github/workflows/reviewdog.yml index 5944c121..6b1b315a 100644 --- a/.github/workflows/reviewdog.yml +++ b/.github/workflows/reviewdog.yml @@ -19,7 +19,4 @@ jobs: reporter: github-check rubocop_version: 0.93.1 github_token: ${{ secrets.github_token }} - rubocop_extensions: - - rubocop-performance:1.10.2 - - rubocop-rails:2.9.1 - - rubocop-rspec:1.44.1 + rubocop_extensions: rubocop-performance:1.10.2 rubocop-rails:2.9.1 rubocop-rspec:1.44.1 From 8312f131407c962a7c50d7cbef5d46d74baad258 Mon Sep 17 00:00:00 2001 From: Rui Baltazar Date: Fri, 25 Jun 2021 14:36:39 +0800 Subject: [PATCH 17/42] put back the extensions in rubocop yml --- .rubocop.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index ca4d6cb6..8416678b 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -1,9 +1,9 @@ inherit_from: .rubocop_todo.yml -# require: -# - rubocop-rails -# - rubocop-performance -# - rubocop-rspec +require: + - rubocop-rails + - rubocop-performance + - rubocop-rspec AllCops: Exclude: From 5483c59ab321d8ff64b0792771ca3235c168154b Mon Sep 17 00:00:00 2001 From: Rui Baltazar Date: Fri, 25 Jun 2021 14:42:41 +0800 Subject: [PATCH 18/42] commented capybara references --- spec/spec_helper.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index a7bd4195..e5078ba8 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -19,8 +19,8 @@ end require 'rspec/rails' -require 'capybara/rspec' -require 'capybara/rails' +# require 'capybara/rspec' +# require 'capybara/rails' begin require 'pry' @@ -41,7 +41,7 @@ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f } RSpec.configure do |config| - config.include RSpec::Integration::CapybaraSessions, type: :request + # config.include RSpec::Integration::CapybaraSessions, type: :request config.include Apartment::Spec::Setup # Somewhat brutal hack so that rails 4 postgres extensions don't modify this file From 85e6472256786349a25a38b04969dc736c11a217 Mon Sep 17 00:00:00 2001 From: Rui Baltazar Date: Fri, 25 Jun 2021 14:49:29 +0800 Subject: [PATCH 19/42] removed unused capybara --- spec/spec_helper.rb | 3 --- 1 file changed, 3 deletions(-) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index e5078ba8..b125d041 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -19,8 +19,6 @@ end require 'rspec/rails' -# require 'capybara/rspec' -# require 'capybara/rails' begin require 'pry' @@ -41,7 +39,6 @@ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f } RSpec.configure do |config| - # config.include RSpec::Integration::CapybaraSessions, type: :request config.include Apartment::Spec::Setup # Somewhat brutal hack so that rails 4 postgres extensions don't modify this file From d8334e5d0538527eb2e806d810ef34145f574ffc Mon Sep 17 00:00:00 2001 From: Rui Baltazar Date: Fri, 25 Jun 2021 15:07:03 +0800 Subject: [PATCH 20/42] added rubocop extensions versions to development dependencies --- ros-apartment.gemspec | 3 +++ spec/support/capybara_sessions.rb | 15 --------------- spec/tenant_spec.rb | 18 ------------------ 3 files changed, 3 insertions(+), 33 deletions(-) delete mode 100644 spec/support/capybara_sessions.rb diff --git a/ros-apartment.gemspec b/ros-apartment.gemspec index caf58b6d..2a72d0ba 100644 --- a/ros-apartment.gemspec +++ b/ros-apartment.gemspec @@ -40,6 +40,9 @@ Gem::Specification.new do |s| s.add_development_dependency 'rspec', '~> 3.4' s.add_development_dependency 'rspec-rails', '~> 3.4' s.add_development_dependency 'rubocop', '~> 0.93' + s.add_development_dependency 'rubocop-performance', '~> 1.10' + s.add_development_dependency 'rubocop-rails','~> 2.1' + s.add_development_dependency 'rubocop-rspec', '~> 1.44' if defined?(JRUBY_VERSION) s.add_development_dependency 'activerecord-jdbc-adapter' diff --git a/spec/support/capybara_sessions.rb b/spec/support/capybara_sessions.rb deleted file mode 100644 index def4985c..00000000 --- a/spec/support/capybara_sessions.rb +++ /dev/null @@ -1,15 +0,0 @@ -# frozen_string_literal: true - -module RSpec - module Integration - module CapybaraSessions - def in_new_session(&_block) - yield new_session - end - - def new_session - Capybara::Session.new(Capybara.current_driver, Capybara.app) - end - end - end -end diff --git a/spec/tenant_spec.rb b/spec/tenant_spec.rb index 9fdc8b33..7d541814 100644 --- a/spec/tenant_spec.rb +++ b/spec/tenant_spec.rb @@ -13,24 +13,6 @@ end end - # TODO: this doesn't belong here, but there aren't integration tests currently for mysql - # where to put??? - describe 'exception recovery', type: :request do - before do - subject.create db1 - end - after { subject.drop db1 } - - # it "should recover from incorrect database" do - # session = Capybara::Session.new(:rack_test, Capybara.app) - # session.visit("http://#{db1}.com") - # expect { - # session.visit("http://this-database-should-not-exist.com") - # }.to raise_error - # session.visit("http://#{db1}.com") - # end - end - # TODO: re-organize these tests context 'with prefix and schemas' do describe '#create' do From 8d934b011e1b042a33fef464a0087c92eb962993 Mon Sep 17 00:00:00 2001 From: Rui Baltazar Date: Fri, 25 Jun 2021 15:16:17 +0800 Subject: [PATCH 21/42] exclude rake environment from rakefile --- .rubocop.yml | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index 8416678b..6b4889b0 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -7,9 +7,10 @@ require: AllCops: Exclude: - - 'gemfiles/**/*.gemfile' - - 'gemfiles/vendor/**/*' - - 'spec/dummy_engine/dummy_engine.gemspec' + - gemfiles/**/*.gemfile + - gemfiles/vendor/**/* + - spec/dummy_engine/dummy_engine.gemspec + - spec/schemas/**/*.rb NewCops: enable @@ -17,22 +18,14 @@ Gemspec/RequiredRubyVersion: Exclude: - 'ros-apartment.gemspec' -Style/WordArray: - Exclude: - - spec/schemas/**/*.rb - -Style/NumericLiterals: - Exclude: - - spec/schemas/**/*.rb - -Layout/EmptyLineAfterMagicComment: - Exclude: - - spec/schemas/**/*.rb - Metrics/BlockLength: Exclude: - spec/**/*.rb +Rails/RakeEnvironment: + Exclude: + - Rakefile + Layout/EmptyLinesAroundAttributeAccessor: Enabled: true From 3b3f9d5531a28e42b076acbbe0d52156419f46fe Mon Sep 17 00:00:00 2001 From: Rui Baltazar Date: Fri, 25 Jun 2021 15:29:34 +0800 Subject: [PATCH 22/42] disabled application record cop because apartment is an engine. --- .rubocop.yml | 70 ++-------------------------------------------------- 1 file changed, 2 insertions(+), 68 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index 6b4889b0..3b963ef1 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -26,71 +26,5 @@ Rails/RakeEnvironment: Exclude: - Rakefile -Layout/EmptyLinesAroundAttributeAccessor: - Enabled: true - -Layout/SpaceAroundMethodCallOperator: - Enabled: true - -Lint/DeprecatedOpenSSLConstant: - Enabled: true - -Lint/DuplicateElsifCondition: - Enabled: true - -Lint/MixedRegexpCaptureTypes: - Enabled: true - -Lint/RaiseException: - Enabled: true - -Lint/StructNewOverride: - Enabled: true - -Style/AccessorGrouping: - Enabled: true - -Style/ArrayCoercion: - Enabled: true - -Style/BisectedAttrAccessor: - Enabled: true - -Style/CaseLikeIf: - Enabled: true - -Style/ExponentialNotation: - Enabled: true - -Style/HashAsLastArrayItem: - Enabled: true - -Style/HashEachMethods: - Enabled: true - -Style/HashLikeCase: - Enabled: true - -Style/HashTransformKeys: - Enabled: true - -Style/HashTransformValues: - Enabled: true - -Style/RedundantAssignment: - Enabled: true - -Style/RedundantFetchBlock: - Enabled: true - -Style/RedundantFileExtensionInRequire: - Enabled: true - -Style/RedundantRegexpCharacterClass: - Enabled: true - -Style/RedundantRegexpEscape: - Enabled: true - -Style/SlicingWithRange: - Enabled: true +Rails/ApplicationRecord: + Enabled: false From 24e2803486e3c920e5c19ee1595fd3e043797c42 Mon Sep 17 00:00:00 2001 From: Rui Baltazar Date: Fri, 25 Jun 2021 15:29:47 +0800 Subject: [PATCH 23/42] fixed filepath rule --- lib/apartment.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/apartment.rb b/lib/apartment.rb index a9506fd9..8a774b5b 100644 --- a/lib/apartment.rb +++ b/lib/apartment.rb @@ -107,13 +107,13 @@ def connection_class def database_schema_file return @database_schema_file if defined?(@database_schema_file) - @database_schema_file = Rails.root.join('db', 'schema.rb') + @database_schema_file = Rails.root.join('db/schema.rb') end def seed_data_file return @seed_data_file if defined?(@seed_data_file) - @seed_data_file = Rails.root.join('db', 'seeds.rb') + @seed_data_file = Rails.root.join('db/seeds.rb') end def pg_excluded_names From 0067ee8db8fdba64b9840345a172510f2f9a7662 Mon Sep 17 00:00:00 2001 From: Rui Baltazar Date: Fri, 25 Jun 2021 15:36:22 +0800 Subject: [PATCH 24/42] replace it_should_behave_like with it_behaves_like --- spec/adapters/jdbc_mysql_adapter_spec.rb | 6 +++--- spec/adapters/jdbc_postgresql_adapter_spec.rb | 10 +++++----- spec/adapters/mysql2_adapter_spec.rb | 10 +++++----- spec/adapters/postgresql_adapter_spec.rb | 16 ++++++++-------- spec/adapters/sqlite3_adapter_spec.rb | 6 +++--- 5 files changed, 24 insertions(+), 24 deletions(-) diff --git a/spec/adapters/jdbc_mysql_adapter_spec.rb b/spec/adapters/jdbc_mysql_adapter_spec.rb index 2d0fb975..b757c96b 100644 --- a/spec/adapters/jdbc_mysql_adapter_spec.rb +++ b/spec/adapters/jdbc_mysql_adapter_spec.rb @@ -16,8 +16,8 @@ def tenant_names let(:default_tenant) { subject.switch { ActiveRecord::Base.connection.current_database } } - it_should_behave_like 'a generic apartment adapter callbacks' - it_should_behave_like 'a generic apartment adapter' - it_should_behave_like 'a connection based apartment adapter' + it_behaves_like 'a generic apartment adapter callbacks' + it_behaves_like 'a generic apartment adapter' + it_behaves_like 'a connection based apartment adapter' end end diff --git a/spec/adapters/jdbc_postgresql_adapter_spec.rb b/spec/adapters/jdbc_postgresql_adapter_spec.rb index 67d3c981..549a38be 100644 --- a/spec/adapters/jdbc_postgresql_adapter_spec.rb +++ b/spec/adapters/jdbc_postgresql_adapter_spec.rb @@ -8,7 +8,7 @@ describe Apartment::Adapters::JDBCPostgresqlAdapter, database: :postgresql do subject { Apartment::Tenant.jdbc_postgresql_adapter config.symbolize_keys } - it_should_behave_like 'a generic apartment adapter callbacks' + it_behaves_like 'a generic apartment adapter callbacks' context 'using schemas' do before { Apartment.use_schemas = true } @@ -20,8 +20,8 @@ def tenant_names let(:default_tenant) { subject.switch { ActiveRecord::Base.connection.schema_search_path.gsub('"', '') } } - it_should_behave_like 'a generic apartment adapter' - it_should_behave_like 'a schema based apartment adapter' + it_behaves_like 'a generic apartment adapter' + it_behaves_like 'a schema based apartment adapter' end context 'using databases' do @@ -34,8 +34,8 @@ def tenant_names let(:default_tenant) { subject.switch { ActiveRecord::Base.connection.current_database } } - it_should_behave_like 'a generic apartment adapter' - it_should_behave_like 'a connection based apartment adapter' + it_behaves_like 'a generic apartment adapter' + it_behaves_like 'a connection based apartment adapter' end end end diff --git a/spec/adapters/mysql2_adapter_spec.rb b/spec/adapters/mysql2_adapter_spec.rb index 505b7d6b..86ce2ee0 100644 --- a/spec/adapters/mysql2_adapter_spec.rb +++ b/spec/adapters/mysql2_adapter_spec.rb @@ -16,12 +16,12 @@ def tenant_names let(:default_tenant) { subject.switch { ActiveRecord::Base.connection.current_database } } - it_should_behave_like 'a generic apartment adapter callbacks' + it_behaves_like 'a generic apartment adapter callbacks' context 'using - the equivalent of - schemas' do before { Apartment.use_schemas = true } - it_should_behave_like 'a generic apartment adapter' + it_behaves_like 'a generic apartment adapter' describe '#default_tenant' do it 'is set to the original db from config' do @@ -57,9 +57,9 @@ def tenant_names context 'using connections' do before { Apartment.use_schemas = false } - it_should_behave_like 'a generic apartment adapter' - it_should_behave_like 'a generic apartment adapter able to handle custom configuration' - it_should_behave_like 'a connection based apartment adapter' + it_behaves_like 'a generic apartment adapter' + it_behaves_like 'a generic apartment adapter able to handle custom configuration' + it_behaves_like 'a connection based apartment adapter' end end end diff --git a/spec/adapters/postgresql_adapter_spec.rb b/spec/adapters/postgresql_adapter_spec.rb index d7689d26..58888fea 100644 --- a/spec/adapters/postgresql_adapter_spec.rb +++ b/spec/adapters/postgresql_adapter_spec.rb @@ -8,7 +8,7 @@ subject { Apartment::Tenant.postgresql_adapter config } - it_should_behave_like 'a generic apartment adapter callbacks' + it_behaves_like 'a generic apartment adapter callbacks' context 'using schemas with schema.rb' do before { Apartment.use_schemas = true } @@ -20,8 +20,8 @@ def tenant_names let(:default_tenant) { subject.switch { ActiveRecord::Base.connection.schema_search_path.gsub('"', '') } } - it_should_behave_like 'a generic apartment adapter' - it_should_behave_like 'a schema based apartment adapter' + it_behaves_like 'a generic apartment adapter' + it_behaves_like 'a schema based apartment adapter' end context 'using schemas with SQL dump' do @@ -37,8 +37,8 @@ def tenant_names let(:default_tenant) { subject.switch { ActiveRecord::Base.connection.schema_search_path.gsub('"', '') } } - it_should_behave_like 'a generic apartment adapter' - it_should_behave_like 'a schema based apartment adapter' + it_behaves_like 'a generic apartment adapter' + it_behaves_like 'a schema based apartment adapter' it 'allows for dashes in the schema name' do expect { Apartment::Tenant.create('has-dashes') }.to_not raise_error @@ -59,9 +59,9 @@ def tenant_names let(:default_tenant) { subject.switch { ActiveRecord::Base.connection.current_database } } - it_should_behave_like 'a generic apartment adapter' - it_should_behave_like 'a generic apartment adapter able to handle custom configuration' - it_should_behave_like 'a connection based apartment adapter' + it_behaves_like 'a generic apartment adapter' + it_behaves_like 'a generic apartment adapter able to handle custom configuration' + it_behaves_like 'a connection based apartment adapter' end end end diff --git a/spec/adapters/sqlite3_adapter_spec.rb b/spec/adapters/sqlite3_adapter_spec.rb index 0e778621..1581a3e3 100644 --- a/spec/adapters/sqlite3_adapter_spec.rb +++ b/spec/adapters/sqlite3_adapter_spec.rb @@ -8,7 +8,7 @@ subject { Apartment::Tenant.sqlite3_adapter config } - it_should_behave_like 'a generic apartment adapter callbacks' + it_behaves_like 'a generic apartment adapter callbacks' context 'using connections' do def tenant_names @@ -20,8 +20,8 @@ def tenant_names subject.switch { File.basename(Apartment::Test.config['connections']['sqlite']['database'], '.sqlite3') } end - it_should_behave_like 'a generic apartment adapter' - it_should_behave_like 'a connection based apartment adapter' + it_behaves_like 'a generic apartment adapter' + it_behaves_like 'a connection based apartment adapter' after(:all) do File.delete(Apartment::Test.config['connections']['sqlite']['database']) From 794f527cfdae7c9cab85ce79501df2d7fdd50a72 Mon Sep 17 00:00:00 2001 From: Rui Baltazar Date: Fri, 25 Jun 2021 15:37:21 +0800 Subject: [PATCH 25/42] cleanup spacings in gemspec --- ros-apartment.gemspec | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/ros-apartment.gemspec b/ros-apartment.gemspec index 2a72d0ba..4b78bfb7 100644 --- a/ros-apartment.gemspec +++ b/ros-apartment.gemspec @@ -27,21 +27,21 @@ Gem::Specification.new do |s| s.homepage = 'https://github.com/rails-on-services/apartment' s.licenses = ['MIT'] - s.add_dependency 'activerecord', '>= 5.0.0', '< 6.2' - s.add_dependency 'parallel', '< 2.0' - s.add_dependency 'public_suffix', '>= 2.0.5', '< 5.0' - s.add_dependency 'rack', '>= 1.3.6', '< 3.0' + s.add_dependency 'activerecord', '>= 5.0.0', '< 6.2' + s.add_dependency 'parallel', '< 2.0' + s.add_dependency 'public_suffix', '>= 2.0.5', '< 5.0' + s.add_dependency 'rack', '>= 1.3.6', '< 3.0' - s.add_development_dependency 'appraisal', '~> 2.2' - s.add_development_dependency 'bundler', '>= 1.3', '< 3.0' + s.add_development_dependency 'appraisal', '~> 2.2' + s.add_development_dependency 'bundler', '>= 1.3', '< 3.0' s.add_development_dependency 'guard-rspec', '~> 4.2' s.add_development_dependency 'pry' - s.add_development_dependency 'rake', '~> 13.0' - s.add_development_dependency 'rspec', '~> 3.4' - s.add_development_dependency 'rspec-rails', '~> 3.4' - s.add_development_dependency 'rubocop', '~> 0.93' + s.add_development_dependency 'rake', '~> 13.0' + s.add_development_dependency 'rspec', '~> 3.4' + s.add_development_dependency 'rspec-rails', '~> 3.4' + s.add_development_dependency 'rubocop', '~> 0.93' s.add_development_dependency 'rubocop-performance', '~> 1.10' - s.add_development_dependency 'rubocop-rails','~> 2.1' + s.add_development_dependency 'rubocop-rails', '~> 2.1' s.add_development_dependency 'rubocop-rspec', '~> 1.44' if defined?(JRUBY_VERSION) From adfa41f8a3acf6552473e90ba625de877658811c Mon Sep 17 00:00:00 2001 From: Rui Baltazar Date: Sat, 26 Jun 2021 10:52:44 +0800 Subject: [PATCH 26/42] addressed Performance/RedundantBlockCall --- lib/apartment/adapters/postgresql_adapter.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/apartment/adapters/postgresql_adapter.rb b/lib/apartment/adapters/postgresql_adapter.rb index 17ad4599..a8d3d4f8 100644 --- a/lib/apartment/adapters/postgresql_adapter.rb +++ b/lib/apartment/adapters/postgresql_adapter.rb @@ -239,7 +239,7 @@ def with_pg_env(&block) ENV['PGUSER'] = @config[:username].to_s if @config[:username] ENV['PGPASSWORD'] = @config[:password].to_s if @config[:password] - block.call + yield ensure ENV['PGHOST'] = pghost ENV['PGPORT'] = pgport From 3fdd2989115e65de5c3243891d8a5c7c96359418 Mon Sep 17 00:00:00 2001 From: Rui Baltazar Date: Sat, 26 Jun 2021 10:57:53 +0800 Subject: [PATCH 27/42] addressed Performance/RedundantEqualityComparisonBlock --- lib/apartment/railtie.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/apartment/railtie.rb b/lib/apartment/railtie.rb index 20826e62..ba4e5728 100644 --- a/lib/apartment/railtie.rb +++ b/lib/apartment/railtie.rb @@ -32,7 +32,7 @@ class Railtie < Rails::Railtie # config.to_prepare do next if ARGV.any? { |arg| arg =~ /\Aassets:(?:precompile|clean)\z/ } - next if ARGV.any? { |arg| arg == 'webpacker:compile' } + next if ARGV.any?('webpacker:compile') next if ENV['APARTMENT_DISABLE_INIT'] begin From 05ad40dcff6382c0f5507bc51dbb6213cadcec3a Mon Sep 17 00:00:00 2001 From: Rui Baltazar Date: Sat, 26 Jun 2021 10:58:52 +0800 Subject: [PATCH 28/42] disabled rake environment from rubocop --- .rubocop.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index 3b963ef1..3007eb68 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -23,8 +23,7 @@ Metrics/BlockLength: - spec/**/*.rb Rails/RakeEnvironment: - Exclude: - - Rakefile + Enabled: false Rails/ApplicationRecord: Enabled: false From b4d5015ecec5cfea29502077b581433492075ac0 Mon Sep 17 00:00:00 2001 From: Rui Baltazar Date: Sat, 26 Jun 2021 11:15:19 +0800 Subject: [PATCH 29/42] address a bunch of breaking rules --- lib/apartment/adapters/postgresql_adapter.rb | 2 +- spec/adapters/jdbc_postgresql_adapter_spec.rb | 6 +- spec/adapters/mysql2_adapter_spec.rb | 6 +- spec/unit/config_spec.rb | 88 +++++++++---------- spec/unit/elevators/first_subdomain_spec.rb | 10 ++- spec/unit/elevators/host_spec.rb | 42 +++++---- spec/unit/elevators/subdomain_spec.rb | 26 +++--- spec/unit/reloader_spec.rb | 4 +- 8 files changed, 98 insertions(+), 86 deletions(-) diff --git a/lib/apartment/adapters/postgresql_adapter.rb b/lib/apartment/adapters/postgresql_adapter.rb index a8d3d4f8..c9ffcd2e 100644 --- a/lib/apartment/adapters/postgresql_adapter.rb +++ b/lib/apartment/adapters/postgresql_adapter.rb @@ -228,7 +228,7 @@ def pg_dump_schema_migrations_data # Temporary set Postgresql related environment variables if there are in @config # - def with_pg_env(&block) + def with_pg_env pghost = ENV['PGHOST'] pgport = ENV['PGPORT'] pguser = ENV['PGUSER'] diff --git a/spec/adapters/jdbc_postgresql_adapter_spec.rb b/spec/adapters/jdbc_postgresql_adapter_spec.rb index 549a38be..d1deabf3 100644 --- a/spec/adapters/jdbc_postgresql_adapter_spec.rb +++ b/spec/adapters/jdbc_postgresql_adapter_spec.rb @@ -10,7 +10,7 @@ it_behaves_like 'a generic apartment adapter callbacks' - context 'using schemas' do + context 'when using schemas' do before { Apartment.use_schemas = true } # Not sure why, but somehow using let(:tenant_names) memoizes for the whole example group, not just each test @@ -18,13 +18,13 @@ def tenant_names ActiveRecord::Base.connection.execute('SELECT nspname FROM pg_namespace;').collect { |row| row['nspname'] } end - let(:default_tenant) { subject.switch { ActiveRecord::Base.connection.schema_search_path.gsub('"', '') } } + let(:default_tenant) { subject.switch { ActiveRecord::Base.connection.schema_search_path.delete('"') } } it_behaves_like 'a generic apartment adapter' it_behaves_like 'a schema based apartment adapter' end - context 'using databases' do + context 'when using databases' do before { Apartment.use_schemas = false } # Not sure why, but somehow using let(:tenant_names) memoizes for the whole example group, not just each test diff --git a/spec/adapters/mysql2_adapter_spec.rb b/spec/adapters/mysql2_adapter_spec.rb index 86ce2ee0..fea994f4 100644 --- a/spec/adapters/mysql2_adapter_spec.rb +++ b/spec/adapters/mysql2_adapter_spec.rb @@ -18,7 +18,7 @@ def tenant_names it_behaves_like 'a generic apartment adapter callbacks' - context 'using - the equivalent of - schemas' do + context 'when using - the equivalent of - schemas' do before { Apartment.use_schemas = true } it_behaves_like 'a generic apartment adapter' @@ -46,7 +46,7 @@ def tenant_names end end - it 'should process model exclusions' do + it 'processes model exclusions' do Apartment::Tenant.init expect(Company.table_name).to eq("#{default_tenant}.companies") @@ -54,7 +54,7 @@ def tenant_names end end - context 'using connections' do + context 'when using connections' do before { Apartment.use_schemas = false } it_behaves_like 'a generic apartment adapter' diff --git a/spec/unit/config_spec.rb b/spec/unit/config_spec.rb index 9adaf84c..c15514da 100644 --- a/spec/unit/config_spec.rb +++ b/spec/unit/config_spec.rb @@ -5,7 +5,7 @@ describe Apartment do describe '#config' do let(:excluded_models) { ['Company'] } - let(:seed_data_file_path) { Rails.root.join('db', 'seeds', 'import.rb') } + let(:seed_data_file_path) { Rails.root.join('db/seeds/import.rb') } def tenant_names_from_array(names) names.each_with_object({}) do |tenant, hash| @@ -13,111 +13,111 @@ def tenant_names_from_array(names) end.with_indifferent_access end - it 'should yield the Apartment object' do - Apartment.configure do |config| + it 'yields the Apartment object' do + described_class.configure do |config| config.excluded_models = [] - expect(config).to eq(Apartment) + expect(config).to eq(described_class) end end - it 'should set excluded models' do - Apartment.configure do |config| + it 'sets excluded models' do + described_class.configure do |config| config.excluded_models = excluded_models end - expect(Apartment.excluded_models).to eq(excluded_models) + expect(described_class.excluded_models).to eq(excluded_models) end - it 'should set use_schemas' do - Apartment.configure do |config| + it 'sets use_schemas' do + described_class.configure do |config| config.excluded_models = [] config.use_schemas = false end - expect(Apartment.use_schemas).to be false + expect(described_class.use_schemas).to be false end - it 'should set seed_data_file' do - Apartment.configure do |config| + it 'sets seed_data_file' do + described_class.configure do |config| config.seed_data_file = seed_data_file_path end - expect(Apartment.seed_data_file).to eq(seed_data_file_path) + expect(described_class.seed_data_file).to eq(seed_data_file_path) end - it 'should set seed_after_create' do - Apartment.configure do |config| + it 'sets seed_after_create' do + described_class.configure do |config| config.excluded_models = [] config.seed_after_create = true end - expect(Apartment.seed_after_create).to be true + expect(described_class.seed_after_create).to be true end - it 'should set tenant_presence_check' do - Apartment.configure do |config| + it 'sets tenant_presence_check' do + described_class.configure do |config| config.tenant_presence_check = true end - expect(Apartment.tenant_presence_check).to be true + expect(described_class.tenant_presence_check).to be true end - it 'should set active_record_log' do - Apartment.configure do |config| + it 'sets active_record_log' do + described_class.configure do |config| config.active_record_log = true end - expect(Apartment.active_record_log).to be true + expect(described_class.active_record_log).to be true end - context 'databases' do + context 'when databases' do let(:users_conf_hash) { { port: 5444 } } before do - Apartment.configure do |config| + described_class.configure do |config| config.tenant_names = tenant_names end end - context 'tenant_names as string array' do + context 'when tenant_names as string array' do let(:tenant_names) { %w[users companies] } - it 'should return object if it doesnt respond_to call' do - expect(Apartment.tenant_names).to eq(tenant_names_from_array(tenant_names).keys) + it 'returns object if it doesnt respond_to call' do + expect(described_class.tenant_names).to eq(tenant_names_from_array(tenant_names).keys) end - it 'should set tenants_with_config' do - expect(Apartment.tenants_with_config).to eq(tenant_names_from_array(tenant_names)) + it 'sets tenants_with_config' do + expect(described_class.tenants_with_config).to eq(tenant_names_from_array(tenant_names)) end end - context 'tenant_names as proc returning an array' do + context 'when tenant_names as proc returning an array' do let(:tenant_names) { -> { %w[users companies] } } - it 'should return object if it doesnt respond_to call' do - expect(Apartment.tenant_names).to eq(tenant_names_from_array(tenant_names.call).keys) + it 'returns object if it doesnt respond_to call' do + expect(described_class.tenant_names).to eq(tenant_names_from_array(tenant_names.call).keys) end - it 'should set tenants_with_config' do - expect(Apartment.tenants_with_config).to eq(tenant_names_from_array(tenant_names.call)) + it 'sets tenants_with_config' do + expect(described_class.tenants_with_config).to eq(tenant_names_from_array(tenant_names.call)) end end - context 'tenant_names as Hash' do + context 'when tenant_names as Hash' do let(:tenant_names) { { users: users_conf_hash }.with_indifferent_access } - it 'should return object if it doesnt respond_to call' do - expect(Apartment.tenant_names).to eq(tenant_names.keys) + it 'returns object if it doesnt respond_to call' do + expect(described_class.tenant_names).to eq(tenant_names.keys) end - it 'should set tenants_with_config' do - expect(Apartment.tenants_with_config).to eq(tenant_names) + it 'sets tenants_with_config' do + expect(described_class.tenants_with_config).to eq(tenant_names) end end - context 'tenant_names as proc returning a Hash' do + context 'when tenant_names as proc returning a Hash' do let(:tenant_names) { -> { { users: users_conf_hash }.with_indifferent_access } } - it 'should return object if it doesnt respond_to call' do - expect(Apartment.tenant_names).to eq(tenant_names.call.keys) + it 'returns object if it doesnt respond_to call' do + expect(described_class.tenant_names).to eq(tenant_names.call.keys) end - it 'should set tenants_with_config' do - expect(Apartment.tenants_with_config).to eq(tenant_names.call) + it 'sets tenants_with_config' do + expect(described_class.tenants_with_config).to eq(tenant_names.call) end end end diff --git a/spec/unit/elevators/first_subdomain_spec.rb b/spec/unit/elevators/first_subdomain_spec.rb index f608d834..fc36a109 100644 --- a/spec/unit/elevators/first_subdomain_spec.rb +++ b/spec/unit/elevators/first_subdomain_spec.rb @@ -6,20 +6,24 @@ describe Apartment::Elevators::FirstSubdomain do describe 'subdomain' do subject { described_class.new('test').parse_tenant_name(request) } + let(:request) { double(:request, host: "#{subdomain}.example.com") } - context 'one subdomain' do + context 'when one subdomain' do let(:subdomain) { 'test' } + it { is_expected.to eq('test') } end - context 'nested subdomains' do + context 'when nested subdomains' do let(:subdomain) { 'test1.test2' } + it { is_expected.to eq('test1') } end - context 'no subdomain' do + context 'when no subdomain' do let(:subdomain) { nil } + it { is_expected.to eq(nil) } end end diff --git a/spec/unit/elevators/host_spec.rb b/spec/unit/elevators/host_spec.rb index e0cb9c3c..f8d77949 100644 --- a/spec/unit/elevators/host_spec.rb +++ b/spec/unit/elevators/host_spec.rb @@ -7,66 +7,74 @@ subject(:elevator) { described_class.new(proc {}) } describe '#parse_tenant_name' do - it 'should return nil when no host' do + it 'returns nil when no host' do request = ActionDispatch::Request.new('HTTP_HOST' => '') expect(elevator.parse_tenant_name(request)).to be_nil end - context 'assuming no ignored_first_subdomains' do + context 'when assuming no ignored_first_subdomains' do before { allow(described_class).to receive(:ignored_first_subdomains).and_return([]) } context 'with 3 parts' do - it 'should return the whole host' do + it 'returns the whole host' do request = ActionDispatch::Request.new('HTTP_HOST' => 'foo.bar.com') expect(elevator.parse_tenant_name(request)).to eq('foo.bar.com') end end context 'with 6 parts' do - it 'should return the whole host' do + it 'returns the whole host' do request = ActionDispatch::Request.new('HTTP_HOST' => 'one.two.three.foo.bar.com') expect(elevator.parse_tenant_name(request)).to eq('one.two.three.foo.bar.com') end end end - context 'assuming ignored_first_subdomains is set' do + context 'when assuming ignored_first_subdomains is set' do before { allow(described_class).to receive(:ignored_first_subdomains).and_return(%w[www foo]) } context 'with 3 parts' do - it 'should return host without www' do + it 'returns host without www' do request = ActionDispatch::Request.new('HTTP_HOST' => 'www.bar.com') expect(elevator.parse_tenant_name(request)).to eq('bar.com') end - it 'should return host without foo' do + it 'returns host without foo' do request = ActionDispatch::Request.new('HTTP_HOST' => 'foo.bar.com') expect(elevator.parse_tenant_name(request)).to eq('bar.com') end end context 'with 6 parts' do - it 'should return host without www' do - request = ActionDispatch::Request.new('HTTP_HOST' => 'www.one.two.three.foo.bar.com') - expect(elevator.parse_tenant_name(request)).to eq('one.two.three.foo.bar.com') + context 'when ignored subdomains do not match in the begining' do + let(:http_host) { 'www.one.two.three.foo.bar.com' } + + it 'returns host without www' do + request = ActionDispatch::Request.new('HTTP_HOST' => http_host) + expect(elevator.parse_tenant_name(request)).to eq('one.two.three.foo.bar.com') + end end - it 'should return host without www' do - request = ActionDispatch::Request.new('HTTP_HOST' => 'foo.one.two.three.bar.com') - expect(elevator.parse_tenant_name(request)).to eq('one.two.three.bar.com') + context 'when ignored subdomains match in the begining' do + let(:http_host) { 'foo.one.two.three.bar.com' } + + it 'returns host without matching subdomain' do + request = ActionDispatch::Request.new('HTTP_HOST' => http_host) + expect(elevator.parse_tenant_name(request)).to eq('one.two.three.bar.com') + end end end end - context 'assuming localhost' do - it 'should return localhost' do + context 'when assuming localhost' do + it 'returns localhost' do request = ActionDispatch::Request.new('HTTP_HOST' => 'localhost') expect(elevator.parse_tenant_name(request)).to eq('localhost') end end - context 'assuming ip address' do - it 'should return the ip address' do + context 'when assuming ip address' do + it 'returns the ip address' do request = ActionDispatch::Request.new('HTTP_HOST' => '127.0.0.1') expect(elevator.parse_tenant_name(request)).to eq('127.0.0.1') end diff --git a/spec/unit/elevators/subdomain_spec.rb b/spec/unit/elevators/subdomain_spec.rb index 4c66ce67..b1cd8f4b 100644 --- a/spec/unit/elevators/subdomain_spec.rb +++ b/spec/unit/elevators/subdomain_spec.rb @@ -7,51 +7,51 @@ subject(:elevator) { described_class.new(proc {}) } describe '#parse_tenant_name' do - context 'assuming one tld' do - it 'should parse subdomain' do + context 'when assuming one tld' do + it 'parses subdomain' do request = ActionDispatch::Request.new('HTTP_HOST' => 'foo.bar.com') expect(elevator.parse_tenant_name(request)).to eq('foo') end - it 'should return nil when no subdomain' do + it 'returns nil when no subdomain' do request = ActionDispatch::Request.new('HTTP_HOST' => 'bar.com') expect(elevator.parse_tenant_name(request)).to be_nil end end - context 'assuming two tlds' do - it 'should parse subdomain in the third level domain' do + context 'when assuming two tlds' do + it 'parses subdomain in the third level domain' do request = ActionDispatch::Request.new('HTTP_HOST' => 'foo.bar.co.uk') expect(elevator.parse_tenant_name(request)).to eq('foo') end - it 'should return nil when no subdomain in the third level domain' do + it 'returns nil when no subdomain in the third level domain' do request = ActionDispatch::Request.new('HTTP_HOST' => 'bar.co.uk') expect(elevator.parse_tenant_name(request)).to be_nil end end - context 'assuming two subdomains' do - it 'should parse two subdomains in the two level domain' do + context 'when assuming two subdomains' do + it 'parses two subdomains in the two level domain' do request = ActionDispatch::Request.new('HTTP_HOST' => 'foo.xyz.bar.com') expect(elevator.parse_tenant_name(request)).to eq('foo') end - it 'should parse two subdomains in the third level domain' do + it 'parses two subdomains in the third level domain' do request = ActionDispatch::Request.new('HTTP_HOST' => 'foo.xyz.bar.co.uk') expect(elevator.parse_tenant_name(request)).to eq('foo') end end - context 'assuming localhost' do - it 'should return nil for localhost' do + context 'when assuming localhost' do + it 'returns nil for localhost' do request = ActionDispatch::Request.new('HTTP_HOST' => 'localhost') expect(elevator.parse_tenant_name(request)).to be_nil end end - context 'assuming ip address' do - it 'should return nil for an ip address' do + context 'when assuming ip address' do + it 'returns nil for an ip address' do request = ActionDispatch::Request.new('HTTP_HOST' => '127.0.0.1') expect(elevator.parse_tenant_name(request)).to be_nil end diff --git a/spec/unit/reloader_spec.rb b/spec/unit/reloader_spec.rb index 54bd2ff7..b194a46c 100644 --- a/spec/unit/reloader_spec.rb +++ b/spec/unit/reloader_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' describe Apartment::Reloader do - context 'using postgresql schemas' do + context 'when using postgresql schemas' do before do Apartment.configure do |config| config.excluded_models = ['Company'] @@ -15,7 +15,7 @@ subject { Apartment::Reloader.new(double('Rack::Application', call: nil)) } - it 'should initialize apartment when called' do + it 'initializes apartment when called' do expect(Company.table_name).not_to include('public.') subject.call(double('env')) expect(Company.table_name).to include('public.') From 2fb4b1b79a93a5f21d736dc3729fb58f62ce21e0 Mon Sep 17 00:00:00 2001 From: Rui Baltazar Date: Sat, 26 Jun 2021 11:17:31 +0800 Subject: [PATCH 30/42] use logger info instead of puts --- lib/apartment/console.rb | 10 +++++----- lib/apartment/custom_console.rb | 2 +- lib/apartment/tasks/task_helper.rb | 10 +++++----- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/lib/apartment/console.rb b/lib/apartment/console.rb index 91721222..a4dd0e58 100644 --- a/lib/apartment/console.rb +++ b/lib/apartment/console.rb @@ -6,7 +6,7 @@ # reloads the environment # rubocop:disable Style/OptionalBooleanParameter def reload!(print = true) - puts 'Reloading...' if print + logger.info 'Reloading...' if print # This triggers the to_prepare callbacks ActionDispatch::Callbacks.new(proc {}).call({}) @@ -18,12 +18,12 @@ def reload!(print = true) def st(schema_name = nil) if schema_name.nil? - tenant_list.each { |t| puts t } + tenant_list.each { |t| logger.info t } elsif tenant_list.include? schema_name Apartment::Tenant.switch!(schema_name) else - puts "Tenant #{schema_name} is not part of the tenant list" + logger.info "Tenant #{schema_name} is not part of the tenant list" end end @@ -35,6 +35,6 @@ def tenant_list end def tenant_info_msg - puts "Available Tenants: #{tenant_list}\n" - puts "Use `st 'tenant'` to switch tenants & `tenant_list` to see list\n" + logger.info "Available Tenants: #{tenant_list}\n" + logger.info "Use `st 'tenant'` to switch tenants & `tenant_list` to see list\n" end diff --git a/lib/apartment/custom_console.rb b/lib/apartment/custom_console.rb index 7b32a5b5..f74a012b 100644 --- a/lib/apartment/custom_console.rb +++ b/lib/apartment/custom_console.rb @@ -8,7 +8,7 @@ module CustomConsole require 'pry-rails' rescue LoadError # rubocop:disable Layout/LineLength - puts '[Failed to load pry-rails] If you want to use Apartment custom prompt you need to add pry-rails to your gemfile' + logger.info '[Failed to load pry-rails] If you want to use Apartment custom prompt you need to add pry-rails to your gemfile' # rubocop:enable Layout/LineLength end diff --git a/lib/apartment/tasks/task_helper.rb b/lib/apartment/tasks/task_helper.rb index 32cd908e..d3203633 100644 --- a/lib/apartment/tasks/task_helper.rb +++ b/lib/apartment/tasks/task_helper.rb @@ -19,7 +19,7 @@ def self.tenants def self.warn_if_tenants_empty return unless tenants.empty? && ENV['IGNORE_EMPTY_TENANTS'] != 'true' - puts <<-WARNING + logger.info <<-WARNING [WARNING] - The list of tenants to migrate appears to be empty. This could mean a few things: 1. You may not have created any, in which case you can ignore this message @@ -31,22 +31,22 @@ def self.warn_if_tenants_empty end def self.create_tenant(tenant_name) - puts("Creating #{tenant_name} tenant") + logger.info("Creating #{tenant_name} tenant") Apartment::Tenant.create(tenant_name) rescue Apartment::TenantExists => e - puts "Tried to create already existing tenant: #{e}" + logger.info "Tried to create already existing tenant: #{e}" end def self.migrate_tenant(tenant_name) strategy = Apartment.db_migrate_tenant_missing_strategy create_tenant(tenant_name) if strategy == :create_tenant - puts("Migrating #{tenant_name} tenant") + logger.info("Migrating #{tenant_name} tenant") Apartment::Migrator.migrate tenant_name rescue Apartment::TenantNotFound => e raise e if strategy == :raise_exception - puts e.message + logger.info e.message end end end From c0fbc94a1618410407470712dfc13a9dee96fcf7 Mon Sep 17 00:00:00 2001 From: Rui Baltazar Date: Sat, 26 Jun 2021 11:20:50 +0800 Subject: [PATCH 31/42] addressed Performance/StringReplacement --- spec/adapters/postgresql_adapter_spec.rb | 20 +++++++++---------- spec/examples/generic_adapter_examples.rb | 2 +- spec/examples/schema_adapter_examples.rb | 8 ++++---- spec/integration/use_within_an_engine_spec.rb | 6 +++--- 4 files changed, 18 insertions(+), 18 deletions(-) diff --git a/spec/adapters/postgresql_adapter_spec.rb b/spec/adapters/postgresql_adapter_spec.rb index 58888fea..7944c862 100644 --- a/spec/adapters/postgresql_adapter_spec.rb +++ b/spec/adapters/postgresql_adapter_spec.rb @@ -10,7 +10,7 @@ it_behaves_like 'a generic apartment adapter callbacks' - context 'using schemas with schema.rb' do + context 'when using schemas with schema.rb' do before { Apartment.use_schemas = true } # Not sure why, but somehow using let(:tenant_names) memoizes for the whole example group, not just each test @@ -18,38 +18,38 @@ def tenant_names ActiveRecord::Base.connection.execute('SELECT nspname FROM pg_namespace;').collect { |row| row['nspname'] } end - let(:default_tenant) { subject.switch { ActiveRecord::Base.connection.schema_search_path.gsub('"', '') } } + let(:default_tenant) { subject.switch { ActiveRecord::Base.connection.schema_search_path.delete('"') } } it_behaves_like 'a generic apartment adapter' it_behaves_like 'a schema based apartment adapter' end - context 'using schemas with SQL dump' do + context 'when using schemas with SQL dump' do before do Apartment.use_schemas = true Apartment.use_sql = true end + after do + Apartment::Tenant.drop('has-dashes') if Apartment.connection.schema_exists? 'has-dashes' + end + # Not sure why, but somehow using let(:tenant_names) memoizes for the whole example group, not just each test def tenant_names ActiveRecord::Base.connection.execute('SELECT nspname FROM pg_namespace;').collect { |row| row['nspname'] } end - let(:default_tenant) { subject.switch { ActiveRecord::Base.connection.schema_search_path.gsub('"', '') } } + let(:default_tenant) { subject.switch { ActiveRecord::Base.connection.schema_search_path.delete('"') } } it_behaves_like 'a generic apartment adapter' it_behaves_like 'a schema based apartment adapter' it 'allows for dashes in the schema name' do - expect { Apartment::Tenant.create('has-dashes') }.to_not raise_error - end - - after do - Apartment::Tenant.drop('has-dashes') if Apartment.connection.schema_exists? 'has-dashes' + expect { Apartment::Tenant.create('has-dashes') }.not_to raise_error end end - context 'using connections' do + context 'when using connections' do before { Apartment.use_schemas = false } # Not sure why, but somehow using let(:tenant_names) memoizes for the whole example group, not just each test diff --git a/spec/examples/generic_adapter_examples.rb b/spec/examples/generic_adapter_examples.rb index cbe6013f..8289b3f9 100644 --- a/spec/examples/generic_adapter_examples.rb +++ b/spec/examples/generic_adapter_examples.rb @@ -132,7 +132,7 @@ expect do subject.switch(db1) { subject.drop(db2) } - end.to_not raise_error + end.not_to raise_error end end diff --git a/spec/examples/schema_adapter_examples.rb b/spec/examples/schema_adapter_examples.rb index fe87a699..586dce62 100644 --- a/spec/examples/schema_adapter_examples.rb +++ b/spec/examples/schema_adapter_examples.rb @@ -81,7 +81,7 @@ it 'should allow them' do expect do subject.create(db) - end.to_not raise_error + end.not_to raise_error expect(tenant_names).to include(db.to_s) end @@ -103,7 +103,7 @@ subject.create(db) expect do subject.drop(db) - end.to_not raise_error + end.not_to raise_error expect(tenant_names).not_to include(db.to_s) end @@ -197,7 +197,7 @@ it 'should not raise any errors' do expect do subject.switch! 'unknown_schema' - end.to_not raise_error(Apartment::TenantNotFound) + end.not_to raise_error(Apartment::TenantNotFound) end end @@ -208,7 +208,7 @@ subject.create(db) expect do subject.switch!(db) - end.to_not raise_error + end.not_to raise_error expect(connection.schema_search_path).to start_with %("#{db}") end diff --git a/spec/integration/use_within_an_engine_spec.rb b/spec/integration/use_within_an_engine_spec.rb index 072efac6..f3269ba4 100644 --- a/spec/integration/use_within_an_engine_spec.rb +++ b/spec/integration/use_within_an_engine_spec.rb @@ -11,17 +11,17 @@ end it 'sucessfully runs rake db:migrate in the engine root' do - expect { Rake::Task['db:migrate'].invoke }.to_not raise_error + expect { Rake::Task['db:migrate'].invoke }.not_to raise_error end it 'sucessfully runs rake app:db:migrate in the engine root' do - expect { Rake::Task['app:db:migrate'].invoke }.to_not raise_error + expect { Rake::Task['app:db:migrate'].invoke }.not_to raise_error end context 'when Apartment.db_migrate_tenants is false' do it 'should not enhance tasks' do Apartment.db_migrate_tenants = false - expect(Apartment::RakeTaskEnhancer).to_not receive(:enhance_task).with('db:migrate') + expect(Apartment::RakeTaskEnhancer).not_to receive(:enhance_task).with('db:migrate') Rake::Task['db:migrate'].invoke end end From d6a2de6049496eab147dbb8696453f52df2d9d99 Mon Sep 17 00:00:00 2001 From: Rui Baltazar Date: Sat, 26 Jun 2021 11:27:09 +0800 Subject: [PATCH 32/42] Updated rubocop todo --- .rubocop_todo.yml | 226 +++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 213 insertions(+), 13 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index c72ac883..16267ccf 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -1,6 +1,6 @@ # This configuration was generated by # `rubocop --auto-gen-config` -# on 2020-07-16 04:15:41 UTC using RuboCop version 0.88.0. +# on 2021-06-26 03:25:28 UTC using RuboCop version 0.93.1. # The point is for the user to remove these configuration records # one by one as the offenses are removed from the code base. # Note that changes in the inspected code, or installation of new @@ -17,16 +17,16 @@ Lint/NonDeterministicRequireOrder: Exclude: - 'spec/spec_helper.rb' -# Offense count: 7 +# Offense count: 3 # Configuration parameters: IgnoredMethods. Metrics/AbcSize: - Max: 33 + Max: 28 -# Offense count: 3 +# Offense count: 5 # Configuration parameters: CountComments, CountAsOne, ExcludedMethods. # ExcludedMethods: refine Metrics/BlockLength: - Max: 102 + Max: 83 # Offense count: 1 # Configuration parameters: CountComments, CountAsOne. @@ -38,6 +38,214 @@ Metrics/ClassLength: Metrics/MethodLength: Max: 24 +# Offense count: 2 +# Cop supports --auto-correct. +# Configuration parameters: AutoCorrect. +Performance/TimesMap: + Exclude: + - 'spec/integration/apartment_rake_integration_spec.rb' + - 'spec/tasks/apartment_rake_spec.rb' + +# Offense count: 3 +RSpec/AnyInstance: + Exclude: + - 'spec/unit/migrator_spec.rb' + +# Offense count: 2 +RSpec/BeforeAfterAll: + Exclude: + - 'spec/spec_helper.rb' + - 'spec/rails_helper.rb' + - 'spec/support/**/*.rb' + - 'spec/adapters/sqlite3_adapter_spec.rb' + - 'spec/tasks/apartment_rake_spec.rb' + +# Offense count: 18 +# Configuration parameters: Prefixes. +# Prefixes: when, with, without +RSpec/ContextWording: + Exclude: + - 'spec/adapters/sqlite3_adapter_spec.rb' + - 'spec/examples/generic_adapter_custom_configuration_example.rb' + - 'spec/examples/schema_adapter_examples.rb' + - 'spec/support/contexts.rb' + - 'spec/tasks/apartment_rake_spec.rb' + - 'spec/tenant_spec.rb' + +# Offense count: 4 +# Configuration parameters: IgnoredMetadata. +RSpec/DescribeClass: + Exclude: + - 'spec/integration/apartment_rake_integration_spec.rb' + - 'spec/integration/query_caching_spec.rb' + - 'spec/integration/use_within_an_engine_spec.rb' + - 'spec/tasks/apartment_rake_spec.rb' + +# Offense count: 12 +# Cop supports --auto-correct. +# Configuration parameters: SkipBlocks, EnforcedStyle. +# SupportedStyles: described_class, explicit +RSpec/DescribedClass: + Exclude: + - 'spec/apartment_spec.rb' + - 'spec/tenant_spec.rb' + - 'spec/unit/elevators/host_hash_spec.rb' + - 'spec/unit/migrator_spec.rb' + - 'spec/unit/reloader_spec.rb' + +# Offense count: 5 +# Cop supports --auto-correct. +RSpec/EmptyLineAfterFinalLet: + Exclude: + - 'spec/adapters/sqlite3_adapter_spec.rb' + - 'spec/examples/schema_adapter_examples.rb' + +# Offense count: 2 +# Cop supports --auto-correct. +RSpec/EmptyLineAfterHook: + Exclude: + - 'spec/tenant_spec.rb' + +# Offense count: 14 +# Configuration parameters: Max. +RSpec/ExampleLength: + Exclude: + - 'spec/examples/generic_adapter_custom_configuration_example.rb' + - 'spec/examples/generic_adapter_examples.rb' + - 'spec/examples/schema_adapter_examples.rb' + - 'spec/integration/query_caching_spec.rb' + - 'spec/tenant_spec.rb' + +# Offense count: 60 +# Cop supports --auto-correct. +# Configuration parameters: CustomTransform, IgnoredWords. +RSpec/ExampleWording: + Exclude: + - 'spec/adapters/sqlite3_adapter_spec.rb' + - 'spec/apartment_spec.rb' + - 'spec/examples/connection_adapter_examples.rb' + - 'spec/examples/generic_adapter_custom_configuration_example.rb' + - 'spec/examples/generic_adapter_examples.rb' + - 'spec/examples/schema_adapter_examples.rb' + - 'spec/integration/apartment_rake_integration_spec.rb' + - 'spec/integration/use_within_an_engine_spec.rb' + - 'spec/tasks/apartment_rake_spec.rb' + - 'spec/tenant_spec.rb' + +# Offense count: 13 +# Configuration parameters: CustomTransform, IgnoreMethods, SpecSuffixOnly. +RSpec/FilePath: + Exclude: + - 'spec/adapters/mysql2_adapter_spec.rb' + - 'spec/adapters/postgresql_adapter_spec.rb' + - 'spec/adapters/sqlite3_adapter_spec.rb' + - 'spec/tenant_spec.rb' + - 'spec/unit/config_spec.rb' + - 'spec/unit/elevators/domain_spec.rb' + - 'spec/unit/elevators/first_subdomain_spec.rb' + - 'spec/unit/elevators/generic_spec.rb' + - 'spec/unit/elevators/host_hash_spec.rb' + - 'spec/unit/elevators/host_spec.rb' + - 'spec/unit/elevators/subdomain_spec.rb' + - 'spec/unit/migrator_spec.rb' + - 'spec/unit/reloader_spec.rb' + +# Offense count: 1 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle. +# SupportedStyles: implicit, each, example +RSpec/HookArgument: + Exclude: + - 'spec/support/setup.rb' + +# Offense count: 4 +# Cop supports --auto-correct. +RSpec/HooksBeforeExamples: + Exclude: + - 'spec/adapters/sqlite3_adapter_spec.rb' + - 'spec/examples/schema_adapter_examples.rb' + +# Offense count: 18 +# Configuration parameters: AssignmentOnly. +RSpec/InstanceVariable: + Exclude: + - 'spec/examples/generic_adapter_examples.rb' + - 'spec/examples/schema_adapter_examples.rb' + - 'spec/integration/apartment_rake_integration_spec.rb' + - 'spec/integration/use_within_an_engine_spec.rb' + - 'spec/tasks/apartment_rake_spec.rb' + +# Offense count: 1 +# Cop supports --auto-correct. +RSpec/LeadingSubject: + Exclude: + - 'spec/unit/reloader_spec.rb' + +# Offense count: 2 +RSpec/LeakyConstantDeclaration: + Exclude: + - 'spec/examples/generic_adapters_callbacks_examples.rb' + - 'spec/unit/elevators/generic_spec.rb' + +# Offense count: 35 +# Configuration parameters: EnforcedStyle. +# SupportedStyles: have_received, receive +RSpec/MessageSpies: + Exclude: + - 'spec/examples/generic_adapter_custom_configuration_example.rb' + - 'spec/integration/apartment_rake_integration_spec.rb' + - 'spec/integration/use_within_an_engine_spec.rb' + - 'spec/tasks/apartment_rake_spec.rb' + - 'spec/unit/elevators/domain_spec.rb' + - 'spec/unit/elevators/generic_spec.rb' + - 'spec/unit/elevators/host_hash_spec.rb' + - 'spec/unit/elevators/host_spec.rb' + - 'spec/unit/elevators/subdomain_spec.rb' + - 'spec/unit/migrator_spec.rb' + +# Offense count: 29 +RSpec/MultipleExpectations: + Max: 4 + +# Offense count: 47 +# Configuration parameters: IgnoreSharedExamples. +RSpec/NamedSubject: + Exclude: + - 'spec/adapters/mysql2_adapter_spec.rb' + - 'spec/adapters/sqlite3_adapter_spec.rb' + - 'spec/support/contexts.rb' + - 'spec/support/requirements.rb' + - 'spec/tenant_spec.rb' + - 'spec/unit/reloader_spec.rb' + +# Offense count: 24 +RSpec/NestedGroups: + Max: 5 + +# Offense count: 6 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle. +# SupportedStyles: and_return, block +RSpec/ReturnFromStub: + Exclude: + - 'spec/integration/apartment_rake_integration_spec.rb' + - 'spec/unit/migrator_spec.rb' + +# Offense count: 4 +# Configuration parameters: IgnoreNameless, IgnoreSymbolicNames. +RSpec/VerifiedDoubles: + Exclude: + - 'spec/integration/apartment_rake_integration_spec.rb' + - 'spec/unit/elevators/first_subdomain_spec.rb' + - 'spec/unit/reloader_spec.rb' + +# Offense count: 1 +# Configuration parameters: EnforcedStyle. +# SupportedStyles: slashes, arguments +Rails/FilePath: + Exclude: + - 'spec/tenant_spec.rb' + # Offense count: 17 Style/Documentation: Exclude: @@ -56,11 +264,3 @@ Style/Documentation: - 'lib/apartment/tasks/enhancements.rb' - 'lib/apartment/tasks/task_helper.rb' - 'lib/generators/apartment/install/install_generator.rb' - -# Offense count: 3 -# Cop supports --auto-correct. -Style/IfUnlessModifier: - Exclude: - - 'Rakefile' - - 'lib/apartment.rb' - - 'lib/apartment/tenant.rb' From c9a36188b01a8c6adfb2ea71e4142bb7e0b4df27 Mon Sep 17 00:00:00 2001 From: Rui Baltazar Date: Sat, 26 Jun 2021 11:30:55 +0800 Subject: [PATCH 33/42] addressed Performance/TimesMap --- .rubocop_todo.yml | 8 -------- spec/integration/apartment_rake_integration_spec.rb | 2 +- spec/tasks/apartment_rake_spec.rb | 2 +- 3 files changed, 2 insertions(+), 10 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 16267ccf..4802fc7e 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -38,14 +38,6 @@ Metrics/ClassLength: Metrics/MethodLength: Max: 24 -# Offense count: 2 -# Cop supports --auto-correct. -# Configuration parameters: AutoCorrect. -Performance/TimesMap: - Exclude: - - 'spec/integration/apartment_rake_integration_spec.rb' - - 'spec/tasks/apartment_rake_spec.rb' - # Offense count: 3 RSpec/AnyInstance: Exclude: diff --git a/spec/integration/apartment_rake_integration_spec.rb b/spec/integration/apartment_rake_integration_spec.rb index fbfd0474..64fcd644 100644 --- a/spec/integration/apartment_rake_integration_spec.rb +++ b/spec/integration/apartment_rake_integration_spec.rb @@ -32,7 +32,7 @@ context 'with x number of databases' do let(:x) { rand(1..5) } # random number of dbs to create - let(:db_names) { x.times.map { Apartment::Test.next_db } } + let(:db_names) { Array.new(x).map { Apartment::Test.next_db } } let!(:company_count) { db_names.length } before do diff --git a/spec/tasks/apartment_rake_spec.rb b/spec/tasks/apartment_rake_spec.rb index f71727a9..e75c8c92 100644 --- a/spec/tasks/apartment_rake_spec.rb +++ b/spec/tasks/apartment_rake_spec.rb @@ -31,7 +31,7 @@ let(:version) { '1234' } context 'database migration' do - let(:tenant_names) { 3.times.map { Apartment::Test.next_db } } + let(:tenant_names) { Array(3).map { Apartment::Test.next_db } } let(:tenant_count) { tenant_names.length } before do From 3a961679c34cd3c44f97890987345b9a84c905c3 Mon Sep 17 00:00:00 2001 From: Rui Baltazar Date: Sat, 26 Jun 2021 11:31:48 +0800 Subject: [PATCH 34/42] addressed Rails/FilePath --- .rubocop_todo.yml | 7 ------- spec/tenant_spec.rb | 2 +- 2 files changed, 1 insertion(+), 8 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 4802fc7e..063d6845 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -231,13 +231,6 @@ RSpec/VerifiedDoubles: - 'spec/unit/elevators/first_subdomain_spec.rb' - 'spec/unit/reloader_spec.rb' -# Offense count: 1 -# Configuration parameters: EnforcedStyle. -# SupportedStyles: slashes, arguments -Rails/FilePath: - Exclude: - - 'spec/tenant_spec.rb' - # Offense count: 17 Style/Documentation: Exclude: diff --git a/spec/tenant_spec.rb b/spec/tenant_spec.rb index 7d541814..8ed3139b 100644 --- a/spec/tenant_spec.rb +++ b/spec/tenant_spec.rb @@ -162,7 +162,7 @@ it 'should seed from custom path' do Apartment.configure do |config| - config.seed_data_file = Rails.root.join('db', 'seeds', 'import.rb') + config.seed_data_file = Rails.root.join('db/seeds/import.rb') end subject.create db1 subject.switch! db1 From 7e0c06dda23585f6dcfef52341be9a00d4e227f3 Mon Sep 17 00:00:00 2001 From: Rui Baltazar Date: Sat, 26 Jun 2021 11:33:26 +0800 Subject: [PATCH 35/42] addressed RSpec/EmptyLineAfterHook --- .rubocop_todo.yml | 6 ------ spec/tenant_spec.rb | 2 ++ 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 063d6845..417e3a5f 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -92,12 +92,6 @@ RSpec/EmptyLineAfterFinalLet: - 'spec/adapters/sqlite3_adapter_spec.rb' - 'spec/examples/schema_adapter_examples.rb' -# Offense count: 2 -# Cop supports --auto-correct. -RSpec/EmptyLineAfterHook: - Exclude: - - 'spec/tenant_spec.rb' - # Offense count: 14 # Configuration parameters: Max. RSpec/ExampleLength: diff --git a/spec/tenant_spec.rb b/spec/tenant_spec.rb index 8ed3139b..13363b9a 100644 --- a/spec/tenant_spec.rb +++ b/spec/tenant_spec.rb @@ -59,6 +59,7 @@ context 'threadsafety' do before { subject.create db1 } + after { subject.drop db1 } it 'has a threadsafe adapter' do @@ -95,6 +96,7 @@ context 'creating models' do before { subject.create db2 } + after { subject.drop db2 } it 'should create a model instance in the current schema' do From 8f32e7866442b0e68653471ef7ea82939b5c7504 Mon Sep 17 00:00:00 2001 From: Rui Baltazar Date: Sat, 26 Jun 2021 11:35:24 +0800 Subject: [PATCH 36/42] addressed Lint/NonDeterministicRequireOrder --- .rubocop_todo.yml | 6 ------ spec/spec_helper.rb | 4 ++-- 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 417e3a5f..3650e125 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -11,12 +11,6 @@ Lint/MixedRegexpCaptureTypes: Exclude: - 'lib/apartment/elevators/domain.rb' -# Offense count: 2 -# Cop supports --auto-correct. -Lint/NonDeterministicRequireOrder: - Exclude: - - 'spec/spec_helper.rb' - # Offense count: 3 # Configuration parameters: IgnoredMethods. Metrics/AbcSize: diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index b125d041..138f719e 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -36,7 +36,7 @@ Rails.backtrace_cleaner.remove_silencers! # Load support files -Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f } +Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].sort.each { |f| require f } RSpec.configure do |config| config.include Apartment::Spec::Setup @@ -59,4 +59,4 @@ end # Load shared examples, must happen after configure for RSpec 3 -Dir["#{File.dirname(__FILE__)}/examples/**/*.rb"].each { |f| require f } +Dir["#{File.dirname(__FILE__)}/examples/**/*.rb"].sort.each { |f| require f } From 08e10412a7eb6cb6584fe72a812bcd44fe7a229d Mon Sep 17 00:00:00 2001 From: Rui Baltazar Date: Sat, 26 Jun 2021 11:39:25 +0800 Subject: [PATCH 37/42] allow puts instead of logger --- .rubocop.yml | 3 +++ lib/apartment/console.rb | 10 +++++----- lib/apartment/custom_console.rb | 2 +- lib/apartment/tasks/task_helper.rb | 10 +++++----- 4 files changed, 14 insertions(+), 11 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index 3007eb68..998c5846 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -27,3 +27,6 @@ Rails/RakeEnvironment: Rails/ApplicationRecord: Enabled: false + +Rails/Output: + Enabled: false diff --git a/lib/apartment/console.rb b/lib/apartment/console.rb index a4dd0e58..91721222 100644 --- a/lib/apartment/console.rb +++ b/lib/apartment/console.rb @@ -6,7 +6,7 @@ # reloads the environment # rubocop:disable Style/OptionalBooleanParameter def reload!(print = true) - logger.info 'Reloading...' if print + puts 'Reloading...' if print # This triggers the to_prepare callbacks ActionDispatch::Callbacks.new(proc {}).call({}) @@ -18,12 +18,12 @@ def reload!(print = true) def st(schema_name = nil) if schema_name.nil? - tenant_list.each { |t| logger.info t } + tenant_list.each { |t| puts t } elsif tenant_list.include? schema_name Apartment::Tenant.switch!(schema_name) else - logger.info "Tenant #{schema_name} is not part of the tenant list" + puts "Tenant #{schema_name} is not part of the tenant list" end end @@ -35,6 +35,6 @@ def tenant_list end def tenant_info_msg - logger.info "Available Tenants: #{tenant_list}\n" - logger.info "Use `st 'tenant'` to switch tenants & `tenant_list` to see list\n" + puts "Available Tenants: #{tenant_list}\n" + puts "Use `st 'tenant'` to switch tenants & `tenant_list` to see list\n" end diff --git a/lib/apartment/custom_console.rb b/lib/apartment/custom_console.rb index f74a012b..7b32a5b5 100644 --- a/lib/apartment/custom_console.rb +++ b/lib/apartment/custom_console.rb @@ -8,7 +8,7 @@ module CustomConsole require 'pry-rails' rescue LoadError # rubocop:disable Layout/LineLength - logger.info '[Failed to load pry-rails] If you want to use Apartment custom prompt you need to add pry-rails to your gemfile' + puts '[Failed to load pry-rails] If you want to use Apartment custom prompt you need to add pry-rails to your gemfile' # rubocop:enable Layout/LineLength end diff --git a/lib/apartment/tasks/task_helper.rb b/lib/apartment/tasks/task_helper.rb index d3203633..32cd908e 100644 --- a/lib/apartment/tasks/task_helper.rb +++ b/lib/apartment/tasks/task_helper.rb @@ -19,7 +19,7 @@ def self.tenants def self.warn_if_tenants_empty return unless tenants.empty? && ENV['IGNORE_EMPTY_TENANTS'] != 'true' - logger.info <<-WARNING + puts <<-WARNING [WARNING] - The list of tenants to migrate appears to be empty. This could mean a few things: 1. You may not have created any, in which case you can ignore this message @@ -31,22 +31,22 @@ def self.warn_if_tenants_empty end def self.create_tenant(tenant_name) - logger.info("Creating #{tenant_name} tenant") + puts("Creating #{tenant_name} tenant") Apartment::Tenant.create(tenant_name) rescue Apartment::TenantExists => e - logger.info "Tried to create already existing tenant: #{e}" + puts "Tried to create already existing tenant: #{e}" end def self.migrate_tenant(tenant_name) strategy = Apartment.db_migrate_tenant_missing_strategy create_tenant(tenant_name) if strategy == :create_tenant - logger.info("Migrating #{tenant_name} tenant") + puts("Migrating #{tenant_name} tenant") Apartment::Migrator.migrate tenant_name rescue Apartment::TenantNotFound => e raise e if strategy == :raise_exception - logger.info e.message + puts e.message end end end From c733f35bf8da44fb4d9dda8499066f1409aa4b99 Mon Sep 17 00:00:00 2001 From: Oleksii Leonov Date: Wed, 28 Apr 2021 17:01:13 +0300 Subject: [PATCH 38/42] Fix create schema between different versions of DB `default_table_access_method` (https://postgresqlco.nf/doc/en/param/default_table_access_method/) was introduced in PostreSQL 12. In some edge cases, we may need to take schema from PostgreSQL 12+, but create new schemas in an earlier version. In this case we need to clear `SET default_table_access_method=head` line from dump. Similar to https://github.com/rails-on-services/apartment/commit/163961baad5a661ec1a48c84a72c71ea1dcf3be6. --- lib/apartment/adapters/postgresql_adapter.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/apartment/adapters/postgresql_adapter.rb b/lib/apartment/adapters/postgresql_adapter.rb index c9ffcd2e..5d7f9e21 100644 --- a/lib/apartment/adapters/postgresql_adapter.rb +++ b/lib/apartment/adapters/postgresql_adapter.rb @@ -162,6 +162,7 @@ class PostgresqlSchemaFromSqlAdapter < PostgresqlSchemaAdapter /SET lock_timeout/i, # new in postgresql 9.3 /SET row_security/i, # new in postgresql 9.5 /SET idle_in_transaction_session_timeout/i, # new in postgresql 9.6 + /SET default_table_access_method/i, # new in postgresql 12 /CREATE SCHEMA public/i, /COMMENT ON SCHEMA public/i From 049198c8052d3c39c92852ed1ac2e7743f3fc6ca Mon Sep 17 00:00:00 2001 From: Rui Baltazar Date: Sun, 12 Sep 2021 16:19:15 +0800 Subject: [PATCH 39/42] added junit formatter, output and save the spec results --- .circleci/config.yml | 5 ++++- ros-apartment.gemspec | 1 + 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index d8411d98..6ae44ff4 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -58,7 +58,10 @@ jobs: - run: name: Run tests - command: bundle exec rspec + command: bundle exec rspec --format progress --format RspecJunitFormatter -o ~/test-results/rspec/rspec.xml + + - store_test_results: + path: ~/test-results/rspec/ workflows: tests: diff --git a/ros-apartment.gemspec b/ros-apartment.gemspec index 4b78bfb7..2f18eed3 100644 --- a/ros-apartment.gemspec +++ b/ros-apartment.gemspec @@ -39,6 +39,7 @@ Gem::Specification.new do |s| s.add_development_dependency 'rake', '~> 13.0' s.add_development_dependency 'rspec', '~> 3.4' s.add_development_dependency 'rspec-rails', '~> 3.4' + s.add_development_dependency 'rspec_junit_formatter' s.add_development_dependency 'rubocop', '~> 0.93' s.add_development_dependency 'rubocop-performance', '~> 1.10' s.add_development_dependency 'rubocop-rails', '~> 2.1' From 0938d582c22c4306ce31f086124e347e5244dd1a Mon Sep 17 00:00:00 2001 From: Rui Baltazar Date: Sun, 12 Sep 2021 16:26:33 +0800 Subject: [PATCH 40/42] alphabetical sorting of gem --- ros-apartment.gemspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ros-apartment.gemspec b/ros-apartment.gemspec index 2f18eed3..169865c4 100644 --- a/ros-apartment.gemspec +++ b/ros-apartment.gemspec @@ -38,8 +38,8 @@ Gem::Specification.new do |s| s.add_development_dependency 'pry' s.add_development_dependency 'rake', '~> 13.0' s.add_development_dependency 'rspec', '~> 3.4' - s.add_development_dependency 'rspec-rails', '~> 3.4' s.add_development_dependency 'rspec_junit_formatter' + s.add_development_dependency 'rspec-rails', '~> 3.4' s.add_development_dependency 'rubocop', '~> 0.93' s.add_development_dependency 'rubocop-performance', '~> 1.10' s.add_development_dependency 'rubocop-rails', '~> 2.1' From 06ad2d6adf0a5343037464ca37ac08af91da3d1e Mon Sep 17 00:00:00 2001 From: Rui Baltazar Date: Sun, 12 Sep 2021 00:29:34 +0800 Subject: [PATCH 41/42] cache key breaks when receiving a belongs to reflection --- lib/apartment/model.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/apartment/model.rb b/lib/apartment/model.rb index 5a75a140..8401cd64 100644 --- a/lib/apartment/model.rb +++ b/lib/apartment/model.rb @@ -17,7 +17,9 @@ def cached_find_by_statement(key, &block) cache_key = if key.is_a? String "#{Apartment::Tenant.current}_#{key}" else - [Apartment::Tenant.current] + key + # NOTE: In Rails 6.0.4 we start receiving an ActiveRecord::Reflection::BelongsToReflection + # as the key, which wouldn't work well with an array. + [Apartment::Tenant.current] + Array.wrap(key) end cache = @find_by_statement_cache[connection.prepared_statements] cache.compute_if_absent(cache_key) { ActiveRecord::StatementCache.create(connection, &block) } From 1929f5f76bd654bb0023243d2ca30341761aadb1 Mon Sep 17 00:00:00 2001 From: Rui Baltazar Date: Mon, 13 Sep 2021 16:53:49 +0800 Subject: [PATCH 42/42] version bump --- lib/apartment/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/apartment/version.rb b/lib/apartment/version.rb index 7ec53121..31deaf81 100644 --- a/lib/apartment/version.rb +++ b/lib/apartment/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module Apartment - VERSION = '2.9.0' + VERSION = '2.10.0' end