From ace6ffad169e63f5876e1503b986ee49ddac1fcb Mon Sep 17 00:00:00 2001 From: Rui Date: Fri, 3 Jan 2020 15:38:14 +0800 Subject: [PATCH 01/50] [Resolves] Basic support for Rails 6 - N/a # Describe the changes made. What does this PR changes that might be critical. If any critical decisions have been made, make sure you explain the rationale for these decisions. - Updated JRuby minor version to fix some tests - Updated travis.yml to latest syntax - Fixed Rakefile to run in rails 6 - Updated gemspec to have URLs matching # How does the implementation addresses the problem After merging this all test suite for apartment gem is passing using rails 6. --- .travis.yml | 19 ++++++++++--------- Gemfile | 2 +- README.md | 4 +++- Rakefile | 10 +++++++++- apartment.gemspec | 38 ++++++++++++++++++++------------------ lib/apartment/version.rb | 2 +- 6 files changed, 44 insertions(+), 31 deletions(-) diff --git a/.travis.yml b/.travis.yml index 78067b58..cefc8560 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,9 +1,10 @@ -sudo: required +os: linux + language: ruby services: - docker rvm: - - jruby-9.1.15.0 + - jruby-9.1.17.0 - 2.1.9 - 2.2.9 - 2.3.6 @@ -29,11 +30,11 @@ before_install: env: RUBY_GC_MALLOC_LIMIT: 90000000 RUBY_GC_HEAP_FREE_SLOTS: 200000 -matrix: +jobs: allow_failures: - rvm: ruby-head - gemfile: gemfiles/rails_master.gemfile - - rvm: jruby-9.1.15.0 + - rvm: jruby-9.1.17.0 gemfile: gemfiles/rails_5_0.gemfile exclude: - rvm: 2.1.9 @@ -46,19 +47,19 @@ matrix: gemfile: gemfiles/rails_6_0.gemfile - rvm: 2.1.9 gemfile: gemfiles/rails_master.gemfile - - rvm: 2.2.9 + - rvm: 2.2.9 gemfile: gemfiles/rails_6_0.gemfile - rvm: 2.3.6 gemfile: gemfiles/rails_6_0.gemfile - rvm: 2.4.3 gemfile: gemfiles/rails_6_0.gemfile - - rvm: jruby-9.1.15.0 + - rvm: jruby-9.1.17.0 gemfile: gemfiles/rails_5_1.gemfile - - rvm: jruby-9.1.15.0 + - rvm: jruby-9.1.17.0 gemfile: gemfiles/rails_5_2.gemfile - - rvm: jruby-9.1.15.0 + - rvm: jruby-9.1.17.0 gemfile: gemfiles/rails_6_0.gemfile - - rvm: jruby-9.1.15.0 + - rvm: jruby-9.1.17.0 gemfile: gemfiles/rails_master.gemfile fast_finish: true cache: bundler diff --git a/Gemfile b/Gemfile index 6c13b34a..bdbf6fe1 100644 --- a/Gemfile +++ b/Gemfile @@ -2,7 +2,7 @@ source 'http://rubygems.org' gemspec -gem 'rails', '>= 3.1.2' +gem 'rails', '>= 3.1.2' group :local do gem 'pry' diff --git a/README.md b/README.md index 6713baaa..65f85886 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,8 @@ [![Code Climate](https://codeclimate.com/github/influitive/apartment/badges/gpa.svg)](https://codeclimate.com/github/influitive/apartment) [![Build Status](https://travis-ci.org/influitive/apartment.svg?branch=development)](https://travis-ci.org/influitive/apartment) +# NOT ACTIVELY MAINTAINED YET. SOLELY FOR TESTING PURPOSES ATM + *Multitenancy for Rails and ActiveRecord* Apartment provides tools to help you deal with multiple tenants in your Rails @@ -484,7 +486,7 @@ Note that you can disable the default migrating of all tenants with `db:migrate` Apartment supports parallelizing migrations into multiple threads when you have a large number of tenants. By default, parallel migrations is -turned off. You can enable this by setting `parallel_migration_threads` to +turned off. You can enable this by setting `parallel_migration_threads` to the number of threads you want to use in your initializer. Keep in mind that because migrations are going to access the database, diff --git a/Rakefile b/Rakefile index df67edc6..8eaec6d0 100644 --- a/Rakefile +++ b/Rakefile @@ -119,10 +119,18 @@ def activerecord_below_5_2? ActiveRecord.version.release < Gem::Version.new('5.2.0') end +def activerecord_below_6_0? + ActiveRecord.version.release < Gem::Version.new('6.0.0') +end + def migrate if activerecord_below_5_2? ActiveRecord::Migrator.migrate('spec/dummy/db/migrate') - else + elsif activerecord_below_6_0? ActiveRecord::MigrationContext.new('spec/dummy/db/migrate').migrate + else + # TODO: Figure out if there is any other possibility that can/should be + # passed here as the second argument for the migration context + ActiveRecord::MigrationContext.new('spec/dummy/db/migrate', ActiveRecord::SchemaMigration).migrate end end diff --git a/apartment.gemspec b/apartment.gemspec index eb59070f..0b51d8a7 100644 --- a/apartment.gemspec +++ b/apartment.gemspec @@ -1,42 +1,44 @@ -# -*- encoding: utf-8 -*- -$: << File.expand_path("../lib", __FILE__) -require "apartment/version" +# frozen_string_literal: true + +$LOAD_PATH << File.expand_path('lib', __dir__) +require 'apartment/version' Gem::Specification.new do |s| - s.name = %q{apartment} + s.name = 'ros-apartment' s.version = Apartment::VERSION - s.authors = ["Ryan Brunner", "Brad Robertson"] - s.summary = %q{A Ruby gem for managing database multitenancy} - s.description = %q{Apartment allows Rack applications to deal with database multitenancy through ActiveRecord} - s.email = ["ryan@influitive.com", "brad@influitive.com"] + s.authors = ['Ryan Brunner', 'Brad Robertson', 'Rui Baltazar'] + s.summary = 'A Ruby gem for managing database multitenancy. Apartment Gem drop in replacement' + s.description = 'Apartment allows Rack applications to deal with database multitenancy through ActiveRecord' + s.email = ['ryan@influitive.com', 'brad@influitive.com', 'rui.p.baltazar@gmail.com'] s.files = `git ls-files`.split($/) - s.executables = s.files.grep(%r{^bin/}).map{ |f| File.basename(f) } + s.executables = s.files.grep(%r{^bin/}).map { |f| File.basename(f) } s.test_files = s.files.grep(%r{^(test|spec|features)/}) - s.require_paths = ["lib"] + s.require_paths = ['lib'] - s.homepage = %q{https://github.com/influitive/apartment} - s.licenses = ["MIT"] + s.homepage = 'https://github.com/rails-on-services/apartment' + s.licenses = ['MIT'] # must be >= 3.1.2 due to bug in prepared_statements s.add_dependency 'activerecord', '>= 3.1.2', '< 6.1' - s.add_dependency 'rack', '>= 1.3.6' - s.add_dependency 'public_suffix', '>= 2' s.add_dependency 'parallel', '>= 0.7.1' + s.add_dependency 'public_suffix', '>= 2' + s.add_dependency 'rack', '>= 1.3.6' s.add_development_dependency 'appraisal' + s.add_development_dependency 'bundler', '~> 2.0' + s.add_development_dependency 'capybara', '~> 2.0' + s.add_development_dependency 'perx-rubocop', '~> 0.0' s.add_development_dependency 'rake', '~> 0.9' s.add_development_dependency 'rspec', '~> 3.4' s.add_development_dependency 'rspec-rails', '~> 3.4' - s.add_development_dependency 'capybara', '~> 2.0' - s.add_development_dependency 'bundler', '>= 1.3', '< 2.0' if defined?(JRUBY_VERSION) s.add_development_dependency 'activerecord-jdbc-adapter' - s.add_development_dependency 'activerecord-jdbcpostgresql-adapter' s.add_development_dependency 'activerecord-jdbcmysql-adapter' - s.add_development_dependency 'jdbc-postgres' + s.add_development_dependency 'activerecord-jdbcpostgresql-adapter' s.add_development_dependency 'jdbc-mysql' + s.add_development_dependency 'jdbc-postgres' s.add_development_dependency 'jruby-openssl' else s.add_development_dependency 'mysql2' diff --git a/lib/apartment/version.rb b/lib/apartment/version.rb index 40b2f751..5e87051d 100644 --- a/lib/apartment/version.rb +++ b/lib/apartment/version.rb @@ -1,3 +1,3 @@ module Apartment - VERSION = "2.2.1" + VERSION = "2.3.0.alpha2" end From 675e6a2b1c9ed1c0eeb1bce4f23950f1b40572f2 Mon Sep 17 00:00:00 2001 From: Rui Baltazar Date: Fri, 3 Jan 2020 16:02:16 +0800 Subject: [PATCH 02/50] [Hotfix] Updated readme --- README.md | 40 +++++++++++++++++++--------------------- 1 file changed, 19 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 65f85886..5afce9dd 100644 --- a/README.md +++ b/README.md @@ -4,34 +4,27 @@ [![Code Climate](https://codeclimate.com/github/influitive/apartment/badges/gpa.svg)](https://codeclimate.com/github/influitive/apartment) [![Build Status](https://travis-ci.org/influitive/apartment.svg?branch=development)](https://travis-ci.org/influitive/apartment) -# NOT ACTIVELY MAINTAINED YET. SOLELY FOR TESTING PURPOSES ATM - *Multitenancy for Rails and ActiveRecord* Apartment provides tools to help you deal with multiple tenants in your Rails application. If you need to have certain data sequestered based on account or company, but still allow some data to exist in a common tenant, Apartment can help. -## HELP! - -In order to help drive the direction of development and clean up the codebase, we'd like to take a poll -on how people are currently using Apartment. If you can take 5 seconds (1 question) to answer -this poll, we'd greatly appreciated it. - -[View Poll](http://www.poll-maker.com/poll391552x4Bfb41a9-15) +## Apartment drop in replacement gem -## Excessive Memory Issues on ActiveRecord 4.x +After having reached out via github issues and email directly, no replies ever +came. Since we wanted to upgrade our application to Rails 6 we decided to fork +and start some development to support Rails 6. Because we don't have access +to the apartment gem itself, the solution was to release it under a different +name but providing the exact same API as it was before. -> If you're noticing ever growing memory issues (ie growing with each tenant you add) -> when using Apartment, that's because there's [an issue](https://github.com/rails/rails/issues/19578) -> with how ActiveRecord maps Postgresql data types into AR data types. -> This has been patched and will be released for AR 4.2.2. It's apparently hard -> to backport to 4.1 unfortunately. -> If you're noticing high memory usage from ActiveRecord with Apartment please upgrade. +## Help wanted -```ruby -gem 'rails', '4.2.1', github: 'influitive/rails', tag: 'v4.2.1.memfix' -``` +We were never involved with the development of Apartment gem in the first place +and this project started out of our own needs. We will be more than happy +to collaborate to maintain the gem alive and supporting the latest versions +of ruby and rails, but your help is appreciated. Either by reporting bugs you +may find or proposing improvements to the gem itself. Feel free to reach out. ## Installation @@ -40,7 +33,7 @@ gem 'rails', '4.2.1', github: 'influitive/rails', tag: 'v4.2.1.memfix' Add the following to your Gemfile: ```ruby -gem 'apartment' +gem 'ros-apartment' ``` Then generate your `Apartment` config file using @@ -533,7 +526,12 @@ end ## Background workers -See [apartment-sidekiq](https://github.com/influitive/apartment-sidekiq) or [apartment-activejob](https://github.com/influitive/apartment-activejob). +Both these gems have been forked as a side consequence of having a new gem name. +You can use them exactly as you were using before. They are, just like this one +a drop-in replacement. + +See [apartment-sidekiq](https://github.com/rails-on-services/apartment-sidekiq) +or [apartment-activejob](https://github.com/rails-on-services/apartment-activejob). ## Callbacks From b5cfd1896bc62034b873b0a8d38357535e30b18a Mon Sep 17 00:00:00 2001 From: Rui Baltazar Date: Fri, 3 Jan 2020 16:21:14 +0800 Subject: [PATCH 03/50] [Hotfix] preparing version 2.3.0 --- 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 5e87051d..25f180b1 100644 --- a/lib/apartment/version.rb +++ b/lib/apartment/version.rb @@ -1,3 +1,3 @@ module Apartment - VERSION = "2.3.0.alpha2" + VERSION = "2.3.0" end From 700c54819f7c9690de720c1accd4db646f22e0b5 Mon Sep 17 00:00:00 2001 From: Rui Baltazar Date: Fri, 3 Jan 2020 16:24:26 +0800 Subject: [PATCH 04/50] [Hotfix] Updated travis config and history.md --- HISTORY.md | 7 +++++++ apartment.gemspec | 3 +-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index c6e1398f..5bf2609c 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,3 +1,10 @@ +# 2.3.0 + * January 3, 2020 + +## Added + - Basic support for rails 6 + - Released different gem name, with same API as apartment + # 2.2.1 * June 19, 2019 diff --git a/apartment.gemspec b/apartment.gemspec index 0b51d8a7..e00a1304 100644 --- a/apartment.gemspec +++ b/apartment.gemspec @@ -26,9 +26,8 @@ Gem::Specification.new do |s| s.add_dependency 'rack', '>= 1.3.6' s.add_development_dependency 'appraisal' - s.add_development_dependency 'bundler', '~> 2.0' + s.add_development_dependency 'bundler', '>= 1.3', '< 2.0' s.add_development_dependency 'capybara', '~> 2.0' - s.add_development_dependency 'perx-rubocop', '~> 0.0' s.add_development_dependency 'rake', '~> 0.9' s.add_development_dependency 'rspec', '~> 3.4' s.add_development_dependency 'rspec-rails', '~> 3.4' From 8aa5b1330837d5862e39cbf816fafd5404250d91 Mon Sep 17 00:00:00 2001 From: Rui Baltazar Date: Tue, 4 Feb 2020 11:03:42 +0800 Subject: [PATCH 05/50] [Hotfix] updated README to include require --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 5afce9dd..f6c6d03b 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,7 @@ may find or proposing improvements to the gem itself. Feel free to reach out. Add the following to your Gemfile: ```ruby -gem 'ros-apartment' +gem 'ros-apartment', require: 'apartment' ``` Then generate your `Apartment` config file using From 428a035fa84dd76bb8c6663df6d2401b624b7c7a Mon Sep 17 00:00:00 2001 From: Rui Baltazar Date: Sun, 16 Feb 2020 15:31:35 +0800 Subject: [PATCH 06/50] [Hotfix] Added story_branch local config --- .story_branch.yml | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 .story_branch.yml diff --git a/.story_branch.yml b/.story_branch.yml new file mode 100644 index 00000000..d9ad6922 --- /dev/null +++ b/.story_branch.yml @@ -0,0 +1,4 @@ +--- +tracker: github +project_id: +- rails-on-services/apartment From 4a4c62c855da330a3adba7ee80c35acaa5656bc9 Mon Sep 17 00:00:00 2001 From: Rui Baltazar Date: Sun, 16 Feb 2020 15:37:24 +0800 Subject: [PATCH 07/50] [Hotfix] added rubocop to the project --- .rubocop.yml | 5 + .rubocop_todo.yml | 755 ++++++++++++++++++ Gemfile | 4 +- apartment.gemspec | 1 + .../apartment_rake_integration_spec.rb | 4 +- 5 files changed, 766 insertions(+), 3 deletions(-) create mode 100644 .rubocop.yml create mode 100644 .rubocop_todo.yml diff --git a/.rubocop.yml b/.rubocop.yml new file mode 100644 index 00000000..902dcf64 --- /dev/null +++ b/.rubocop.yml @@ -0,0 +1,5 @@ +inherit_from: .rubocop_todo.yml + +inherit_gem: + perx-rubocop: + - default.yml diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml new file mode 100644 index 00000000..383eaf02 --- /dev/null +++ b/.rubocop_todo.yml @@ -0,0 +1,755 @@ +# This configuration was generated by +# `rubocop --auto-gen-config` +# on 2020-02-16 15:36:55 +0800 using RuboCop version 0.77.0. +# 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 +# versions of RuboCop, may require this file to be generated again. + +# Offense count: 13 +# Cop supports --auto-correct. +# Configuration parameters: TreatCommentsAsGroupSeparators, Include. +# Include: **/*.gemfile, **/Gemfile, **/gems.rb +Bundler/OrderedGems: + Exclude: + - 'gemfiles/rails_4_2.gemfile' + - 'gemfiles/rails_5_0.gemfile' + - 'gemfiles/rails_5_1.gemfile' + - 'gemfiles/rails_5_2.gemfile' + - 'gemfiles/rails_6_0.gemfile' + - 'gemfiles/rails_master.gemfile' + +# Offense count: 13 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle, IndentationWidth. +# SupportedStyles: outdent, indent +Layout/AccessModifierIndentation: + Exclude: + - 'lib/apartment/adapters/abstract_adapter.rb' + - 'lib/apartment/adapters/abstract_jdbc_adapter.rb' + - 'lib/apartment/adapters/jdbc_postgresql_adapter.rb' + - 'lib/apartment/adapters/mysql2_adapter.rb' + - 'lib/apartment/adapters/postgresql_adapter.rb' + - 'lib/apartment/adapters/sqlite3_adapter.rb' + - 'lib/apartment/elevators/subdomain.rb' + +# Offense count: 4 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle, IndentationWidth. +# SupportedStyles: with_first_argument, with_fixed_indentation +Layout/ArgumentAlignment: + Exclude: + - 'lib/apartment/adapters/sqlite3_adapter.rb' + - 'lib/apartment/elevators/host_hash.rb' + +# Offense count: 1 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyleAlignWith, AutoCorrect, Severity. +# SupportedStylesAlignWith: start_of_line, def +Layout/DefEndAlignment: + Exclude: + - 'lib/apartment/adapters/abstract_adapter.rb' + +# Offense count: 1 +# Cop supports --auto-correct. +Layout/ElseAlignment: + Exclude: + - 'lib/apartment/elevators/subdomain.rb' + +# Offense count: 2 +# Cop supports --auto-correct. +Layout/EmptyLineAfterGuardClause: + Exclude: + - 'lib/apartment.rb' + - 'lib/apartment/elevators/host.rb' + +# Offense count: 3 +# Cop supports --auto-correct. +Layout/EmptyLineAfterMagicComment: + Exclude: + - 'spec/schemas/v1.rb' + - 'spec/schemas/v2.rb' + - 'spec/schemas/v3.rb' + +# Offense count: 2 +# Cop supports --auto-correct. +Layout/EmptyLines: + Exclude: + - 'Appraisals' + +# Offense count: 54 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle. +# SupportedStyles: empty_lines, no_empty_lines +Layout/EmptyLinesAroundBlockBody: + Enabled: false + +# Offense count: 18 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle. +# SupportedStyles: empty_lines, empty_lines_except_namespace, empty_lines_special, no_empty_lines, beginning_only, ending_only +Layout/EmptyLinesAroundClassBody: + Exclude: + - 'lib/apartment.rb' + - 'lib/apartment/adapters/abstract_jdbc_adapter.rb' + - 'lib/apartment/adapters/jdbc_mysql_adapter.rb' + - 'lib/apartment/adapters/jdbc_postgresql_adapter.rb' + - 'lib/apartment/adapters/mysql2_adapter.rb' + - 'lib/apartment/adapters/postgresql_adapter.rb' + - 'lib/apartment/elevators/domain.rb' + - 'lib/apartment/elevators/first_subdomain.rb' + - 'lib/apartment/elevators/generic.rb' + - 'lib/apartment/railtie.rb' + - 'lib/apartment/reloader.rb' + - 'lib/apartment/tasks/enhancements.rb' + - 'lib/generators/apartment/install/install_generator.rb' + +# Offense count: 5 +# Cop supports --auto-correct. +Layout/EmptyLinesAroundExceptionHandlingKeywords: + Exclude: + - 'lib/apartment/adapters/abstract_adapter.rb' + - 'lib/apartment/adapters/jdbc_postgresql_adapter.rb' + - 'lib/apartment/adapters/mysql2_adapter.rb' + - 'lib/apartment/adapters/postgresql_adapter.rb' + +# Offense count: 2 +# Cop supports --auto-correct. +Layout/EmptyLinesAroundMethodBody: + Exclude: + - 'lib/apartment/adapters/postgresql_adapter.rb' + - 'lib/apartment/tasks/enhancements.rb' + +# Offense count: 15 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle. +# SupportedStyles: empty_lines, empty_lines_except_namespace, empty_lines_special, no_empty_lines +Layout/EmptyLinesAroundModuleBody: + Exclude: + - 'lib/apartment.rb' + - 'lib/apartment/adapters/jdbc_mysql_adapter.rb' + - 'lib/apartment/adapters/jdbc_postgresql_adapter.rb' + - 'lib/apartment/adapters/mysql2_adapter.rb' + - 'lib/apartment/adapters/postgis_adapter.rb' + - 'lib/apartment/adapters/postgresql_adapter.rb' + - 'lib/apartment/deprecation.rb' + - 'lib/apartment/migrator.rb' + - 'lib/apartment/tenant.rb' + - 'spec/support/apartment_helpers.rb' + - 'spec/support/config.rb' + - 'spec/support/requirements.rb' + - 'spec/support/setup.rb' + +# Offense count: 1 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyleAlignWith, AutoCorrect, Severity. +# SupportedStylesAlignWith: keyword, variable, start_of_line +Layout/EndAlignment: + Exclude: + - 'lib/apartment/elevators/subdomain.rb' + +# Offense count: 15 +# Cop supports --auto-correct. +# Configuration parameters: AllowForAlignment, AllowBeforeTrailingComments, ForceEqualSignAlignment. +Layout/ExtraSpacing: + Exclude: + - 'lib/apartment/adapters/abstract_adapter.rb' + - 'lib/apartment/adapters/postgresql_adapter.rb' + - 'lib/tasks/apartment.rake' + - 'spec/dummy/config.ru' + - 'spec/dummy/config/environments/test.rb' + - 'spec/dummy/script/rails' + - 'spec/dummy_engine/test/dummy/config.ru' + - 'spec/examples/generic_adapter_examples.rb' + - 'spec/examples/schema_adapter_examples.rb' + - 'spec/integration/apartment_rake_integration_spec.rb' + - 'spec/tasks/apartment_rake_spec.rb' + - 'spec/unit/reloader_spec.rb' + +# Offense count: 3 +# Cop supports --auto-correct. +# Configuration parameters: AllowMultipleStyles, EnforcedHashRocketStyle, EnforcedColonStyle, EnforcedLastArgumentHashStyle. +# SupportedHashRocketStyles: key, separator, table +# SupportedColonStyles: key, separator, table +# SupportedLastArgumentHashStyles: always_inspect, always_ignore, ignore_implicit, ignore_explicit +Layout/HashAlignment: + Exclude: + - 'spec/examples/generic_adapter_custom_configuration_example.rb' + +# Offense count: 1 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle. +# SupportedStyles: normal, indented_internal_methods +Layout/IndentationConsistency: + Exclude: + - 'lib/apartment/adapters/abstract_adapter.rb' + +# Offense count: 6 +# Cop supports --auto-correct. +# Configuration parameters: Width, IgnoredPatterns. +Layout/IndentationWidth: + Exclude: + - 'lib/apartment/adapters/abstract_adapter.rb' + - 'lib/apartment/adapters/abstract_jdbc_adapter.rb' + - 'lib/apartment/adapters/jdbc_postgresql_adapter.rb' + - 'lib/apartment/adapters/postgresql_adapter.rb' + - 'lib/apartment/elevators/subdomain.rb' + +# Offense count: 1 +# Cop supports --auto-correct. +# Configuration parameters: AllowDoxygenCommentStyle. +Layout/LeadingCommentSpace: + Exclude: + - 'spec/dummy_engine/config/initializers/apartment.rb' + +# Offense count: 3 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle, IndentationWidth. +# SupportedStyles: aligned, indented, indented_relative_to_receiver +Layout/MultilineMethodCallIndentation: + Exclude: + - 'spec/examples/generic_adapter_examples.rb' + +# Offense count: 1 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle. +# SupportedStyles: space, no_space +Layout/SpaceAroundEqualsInParameterDefault: + Exclude: + - 'lib/apartment/console.rb' + +# Offense count: 3 +# Cop supports --auto-correct. +# Configuration parameters: AllowForAlignment. +Layout/SpaceAroundOperators: + Exclude: + - 'lib/apartment/adapters/postgresql_adapter.rb' + - 'spec/dummy/config/environments/test.rb' + +# Offense count: 82 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle. +# SupportedStyles: space, no_space +# SupportedStylesForEmptyBraces: space, no_space +Layout/SpaceBeforeBlockBraces: + EnforcedStyleForEmptyBraces: no_space + +# Offense count: 1 +# Cop supports --auto-correct. +# Configuration parameters: AllowForAlignment. +Layout/SpaceBeforeFirstArg: + Exclude: + - 'lib/tasks/apartment.rake' + +# Offense count: 1 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle. +# SupportedStyles: require_no_space, require_space +Layout/SpaceInLambdaLiteral: + Exclude: + - 'lib/apartment/migrator.rb' + +# Offense count: 14 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle, EnforcedStyleForEmptyBraces, SpaceBeforeBlockParameters. +# SupportedStyles: space, no_space +# SupportedStylesForEmptyBraces: space, no_space +Layout/SpaceInsideBlockBraces: + Exclude: + - 'lib/apartment.rb' + - 'lib/apartment/adapters/postgresql_adapter.rb' + - 'spec/dummy/db/seeds/import.rb' + - 'spec/examples/generic_adapter_custom_configuration_example.rb' + - 'spec/support/contexts.rb' + - 'spec/unit/elevators/generic_spec.rb' + +# Offense count: 6 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle. +# SupportedStyles: space, no_space +Layout/SpaceInsideStringInterpolation: + Exclude: + - 'lib/apartment/adapters/abstract_adapter.rb' + +# Offense count: 12 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle. +# SupportedStyles: final_newline, final_blank_line +Layout/TrailingEmptyLines: + Exclude: + - 'lib/apartment/elevators/first_subdomain.rb' + - 'lib/apartment/elevators/host.rb' + - 'spec/dummy/app/models/company.rb' + - 'spec/dummy/app/models/user.rb' + - 'spec/dummy/config/boot.rb' + - 'spec/dummy/config/environments/development.rb' + - 'spec/dummy_engine/test/dummy/config/initializers/cookies_serializer.rb' + - 'spec/support/capybara_sessions.rb' + - 'spec/support/config.rb' + - 'spec/support/contexts.rb' + - 'spec/unit/elevators/first_subdomain_spec.rb' + - 'spec/unit/reloader_spec.rb' + +# Offense count: 6 +# Cop supports --auto-correct. +# Configuration parameters: AllowInHeredoc. +Layout/TrailingWhitespace: + Exclude: + - 'lib/apartment/elevators/domain.rb' + - 'spec/dummy/app/controllers/application_controller.rb' + - 'spec/support/capybara_sessions.rb' + - 'spec/unit/elevators/first_subdomain_spec.rb' + +# Offense count: 7 +# Cop supports --auto-correct. +Lint/DeprecatedClassMethods: + Exclude: + - 'lib/apartment/adapters/abstract_adapter.rb' + - 'lib/apartment/adapters/sqlite3_adapter.rb' + - 'spec/adapters/sqlite3_adapter_spec.rb' + +# Offense count: 3 +Lint/ParenthesesAsGroupedExpression: + Exclude: + - 'spec/adapters/sqlite3_adapter_spec.rb' + +# Offense count: 1 +# Cop supports --auto-correct. +Lint/RedundantStringCoercion: + Exclude: + - 'spec/examples/schema_adapter_examples.rb' + +# Offense count: 2 +# Configuration parameters: AllowComments. +Lint/SuppressedException: + Exclude: + - 'lib/apartment/railtie.rb' + - 'spec/spec_helper.rb' + +# Offense count: 3 +# Cop supports --auto-correct. +# Configuration parameters: AllowUnusedKeywordArguments, IgnoreEmptyMethods. +Lint/UnusedMethodArgument: + Exclude: + - 'lib/apartment/adapters/abstract_adapter.rb' + - 'lib/apartment/elevators/generic.rb' + - 'spec/support/capybara_sessions.rb' + +# Offense count: 5 +Metrics/AbcSize: + Max: 33 + +# Offense count: 11 +# Configuration parameters: CountComments, ExcludedMethods. +# ExcludedMethods: refine +Metrics/BlockLength: + Max: 176 + +# Offense count: 1 +# Configuration parameters: CountComments. +Metrics/ClassLength: + Max: 155 + +# Offense count: 18 +# Cop supports --auto-correct. +# Configuration parameters: AutoCorrect, AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns. +# URISchemes: http, https +Metrics/LineLength: + Max: 237 + +# Offense count: 4 +# Configuration parameters: CountComments, ExcludedMethods. +Metrics/MethodLength: + Max: 24 + +# Offense count: 1 +Naming/AccessorMethodName: + Exclude: + - 'spec/examples/generic_adapter_custom_configuration_example.rb' + +# Offense count: 4 +# Cop supports --auto-correct. +# Configuration parameters: PreferredName. +Naming/RescuedExceptionsVariableName: + Exclude: + - 'lib/apartment/adapters/abstract_adapter.rb' + - 'lib/apartment/adapters/mysql2_adapter.rb' + +# Offense count: 2 +# Cop supports --auto-correct. +Rails/ApplicationRecord: + Exclude: + - 'spec/dummy/app/models/company.rb' + - 'spec/dummy/app/models/user.rb' + +# Offense count: 2 +# Cop supports --auto-correct. +# Configuration parameters: Whitelist. +# Whitelist: find_by_sql +Rails/DynamicFindBy: + Exclude: + - 'spec/integration/query_caching_spec.rb' + +# Offense count: 3 +# Configuration parameters: EnforcedStyle. +# SupportedStyles: slashes, arguments +Rails/FilePath: + Exclude: + - 'lib/apartment.rb' + - 'spec/tenant_spec.rb' + - 'spec/unit/config_spec.rb' + +# Offense count: 1 +# Configuration parameters: Include. +# Include: app/**/*.rb, config/**/*.rb, db/**/*.rb, lib/**/*.rb +Rails/Output: + Exclude: + - '**/rails_helper.rb' + - 'lib/apartment/console.rb' + +# Offense count: 1 +# Cop supports --auto-correct. +Security/YAMLLoad: + Exclude: + - 'spec/support/config.rb' + +# Offense count: 5 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle. +# SupportedStyles: prefer_alias, prefer_alias_method +Style/Alias: + Exclude: + - 'lib/apartment.rb' + - 'lib/apartment/adapters/abstract_adapter.rb' + +# Offense count: 16 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle, ProceduralMethods, FunctionalMethods, IgnoredMethods, AllowBracesOnProceduralOneLiners. +# SupportedStyles: line_count_based, semantic, braces_for_chaining, always_braces +# ProceduralMethods: benchmark, bm, bmbm, create, each_with_object, measure, new, realtime, tap, with_object +# FunctionalMethods: let, let!, subject, watch +# IgnoredMethods: lambda, proc, it +Style/BlockDelimiters: + Exclude: + - '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/tasks/apartment_rake_spec.rb' + - 'spec/tenant_spec.rb' + - 'spec/unit/elevators/generic_spec.rb' + +# Offense count: 1 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle. +# SupportedStyles: braces, no_braces, context_dependent +Style/BracesAroundHashParameters: + Exclude: + - 'lib/apartment/adapters/jdbc_postgresql_adapter.rb' + +# Offense count: 5 +# Cop supports --auto-correct. +# Configuration parameters: Keywords. +# Keywords: TODO, FIXME, OPTIMIZE, HACK, REVIEW +Style/CommentAnnotation: + Exclude: + - 'lib/apartment.rb' + - 'lib/apartment/adapters/abstract_adapter.rb' + - 'spec/tenant_spec.rb' + +# Offense count: 1 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle. +# SupportedStyles: compact, expanded +Style/EmptyMethod: + Exclude: + - 'spec/dummy/app/controllers/application_controller.rb' + +# Offense count: 3 +# Cop supports --auto-correct. +Style/Encoding: + Exclude: + - 'spec/schemas/v1.rb' + - 'spec/schemas/v2.rb' + - 'spec/schemas/v3.rb' + +# Offense count: 14 +# Cop supports --auto-correct. +Style/ExpandPathArguments: + Exclude: + - 'lib/generators/apartment/install/install_generator.rb' + - 'spec/adapters/sqlite3_adapter_spec.rb' + - 'spec/dummy/config/application.rb' + - 'spec/dummy/config/boot.rb' + - 'spec/dummy/config/environment.rb' + - 'spec/dummy/script/rails' + - 'spec/dummy_engine/test/dummy/config/application.rb' + - 'spec/dummy_engine/test/dummy/config/boot.rb' + - 'spec/dummy_engine/test/dummy/config/environment.rb' + - 'spec/integration/use_within_an_engine_spec.rb' + - 'spec/spec_helper.rb' + +# Offense count: 1 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle. +# SupportedStyles: format, sprintf, percent +Style/FormatString: + Exclude: + - 'spec/support/apartment_helpers.rb' + +# Offense count: 1 +# Configuration parameters: . +# SupportedStyles: annotated, template, unannotated +Style/FormatStringToken: + EnforcedStyle: unannotated + +# Offense count: 107 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle. +# SupportedStyles: always, never +Style/FrozenStringLiteralComment: + Enabled: false + +# Offense count: 2 +# Configuration parameters: MinBodyLength. +Style/GuardClause: + Exclude: + - 'lib/apartment/adapters/abstract_adapter.rb' + - 'lib/tasks/apartment.rake' + +# Offense count: 33 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle, UseHashRocketsWithSymbolValues, PreferHashRocketsForNonAlnumEndingSymbols. +# SupportedStyles: ruby19, hash_rockets, no_mixed_keys, ruby19_no_mixed_keys +Style/HashSyntax: + Exclude: + - 'lib/apartment/adapters/jdbc_postgresql_adapter.rb' + - 'spec/dummy/config/initializers/session_store.rb' + - 'spec/dummy/config/routes.rb' + - 'spec/examples/schema_adapter_examples.rb' + - 'spec/integration/apartment_rake_integration_spec.rb' + - 'spec/schemas/v1.rb' + - 'spec/schemas/v2.rb' + - 'spec/schemas/v3.rb' + - 'spec/support/contexts.rb' + - 'spec/tenant_spec.rb' + - 'spec/unit/elevators/first_subdomain_spec.rb' + - 'spec/unit/reloader_spec.rb' + +# Offense count: 6 +# Cop supports --auto-correct. +Style/IfUnlessModifier: + Exclude: + - '.pryrc' + - 'lib/apartment.rb' + - 'lib/apartment/adapters/abstract_adapter.rb' + - 'lib/apartment/adapters/jdbc_postgresql_adapter.rb' + - 'lib/apartment/adapters/postgresql_adapter.rb' + +# Offense count: 7 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle. +# SupportedStyles: line_count_dependent, lambda, literal +Style/Lambda: + Exclude: + - 'lib/generators/apartment/install/templates/apartment.rb' + - 'spec/dummy/config/initializers/apartment.rb' + - 'spec/integration/apartment_rake_integration_spec.rb' + - 'spec/integration/query_caching_spec.rb' + - 'spec/unit/config_spec.rb' + +# Offense count: 1 +Style/MixinUsage: + Exclude: + - '.pryrc' + +# Offense count: 1 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle, Autocorrect. +# SupportedStyles: module_function, extend_self +Style/ModuleFunction: + Exclude: + - 'spec/support/apartment_helpers.rb' + +# Offense count: 5 +# Cop supports --auto-correct. +Style/MultilineIfModifier: + Exclude: + - 'lib/apartment/adapters/sqlite3_adapter.rb' + - 'lib/apartment/elevators/host_hash.rb' + - 'spec/support/requirements.rb' + +# Offense count: 2 +Style/MultilineTernaryOperator: + Exclude: + - 'lib/apartment/adapters/jdbc_postgresql_adapter.rb' + - 'lib/apartment/adapters/mysql2_adapter.rb' + +# Offense count: 7 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle. +# SupportedStyles: literals, strict +Style/MutableConstant: + Exclude: + - 'lib/apartment.rb' + - 'lib/apartment/adapters/postgresql_adapter.rb' + - 'lib/apartment/tasks/enhancements.rb' + - 'lib/apartment/version.rb' + - 'spec/dummy_engine/lib/dummy_engine/version.rb' + +# Offense count: 3 +# Cop supports --auto-correct. +# Configuration parameters: Strict. +Style/NumericLiterals: + MinDigits: 15 + +# Offense count: 2 +# Cop supports --auto-correct. +Style/ParallelAssignment: + Exclude: + - 'lib/apartment/adapters/postgresql_adapter.rb' + +# Offense count: 29 +# Cop supports --auto-correct. +# Configuration parameters: PreferredDelimiters. +Style/PercentLiteralDelimiters: + Exclude: + - 'lib/apartment/adapters/postgresql_adapter.rb' + - 'lib/apartment/tasks/enhancements.rb' + - 'spec/dummy/config/application.rb' + - 'spec/dummy_engine/config/initializers/apartment.rb' + - 'spec/examples/schema_adapter_examples.rb' + - 'spec/integration/apartment_rake_integration_spec.rb' + - 'spec/support/requirements.rb' + - 'spec/unit/elevators/host_spec.rb' + - 'spec/unit/elevators/subdomain_spec.rb' + - 'spec/unit/migrator_spec.rb' + +# Offense count: 1 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle. +# SupportedStyles: short, verbose +Style/PreferredHashMethods: + Exclude: + - 'lib/apartment/elevators/host_hash.rb' + +# Offense count: 13 +# Cop supports --auto-correct. +Style/Proc: + Exclude: + - 'lib/apartment/console.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' + +# Offense count: 2 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle. +# SupportedStyles: compact, exploded +Style/RaiseArgs: + Exclude: + - 'lib/apartment/adapters/jdbc_postgresql_adapter.rb' + - 'lib/apartment/adapters/postgresql_adapter.rb' + +# Offense count: 1 +# Cop supports --auto-correct. +Style/RandomWithOffset: + Exclude: + - 'spec/integration/apartment_rake_integration_spec.rb' + +# Offense count: 1 +# Cop supports --auto-correct. +Style/RedundantBegin: + Exclude: + - 'lib/apartment/adapters/abstract_adapter.rb' + +# Offense count: 3 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle, AllowInnerSlashes. +# SupportedStyles: slashes, percent_r, mixed +Style/RegexpLiteral: + Exclude: + - 'lib/apartment/adapters/abstract_jdbc_adapter.rb' + - 'lib/apartment/adapters/jdbc_postgresql_adapter.rb' + - 'spec/examples/schema_adapter_examples.rb' + +# Offense count: 9 +# Cop supports --auto-correct. +Style/RescueModifier: + Exclude: + - 'lib/apartment/adapters/abstract_adapter.rb' + - 'spec/adapters/sqlite3_adapter_spec.rb' + - 'spec/examples/schema_adapter_examples.rb' + - 'spec/support/apartment_helpers.rb' + - 'spec/support/requirements.rb' + - 'spec/tenant_spec.rb' + +# Offense count: 1 +# Cop supports --auto-correct. +# Configuration parameters: AllowAsExpressionSeparator. +Style/Semicolon: + Exclude: + - 'spec/adapters/postgresql_adapter_spec.rb' + +# Offense count: 1 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle. +# SupportedStyles: use_perl_names, use_english_names +Style/SpecialGlobalVars: + Exclude: + - 'spec/dummy/config/boot.rb' + +# Offense count: 518 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle, ConsistentQuotesInMultiline. +# SupportedStyles: single_quotes, double_quotes +Style/StringLiterals: + Enabled: false + +# Offense count: 2 +# Cop supports --auto-correct. +# Configuration parameters: MinSize. +# SupportedStyles: percent, brackets +Style/SymbolArray: + EnforcedStyle: brackets + +# Offense count: 1 +# Cop supports --auto-correct. +# Configuration parameters: IgnoredMethods. +# IgnoredMethods: respond_to, define_method +Style/SymbolProc: + Exclude: + - 'lib/tasks/apartment.rake' + +# Offense count: 1 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyleForMultiline. +# SupportedStylesForMultiline: comma, consistent_comma, no_comma +Style/TrailingCommaInArrayLiteral: + Exclude: + - 'lib/apartment/adapters/postgresql_adapter.rb' + +# Offense count: 2 +# Cop supports --auto-correct. +# Configuration parameters: ExactNameMatch, AllowPredicates, AllowDSLWriters, IgnoreClassMethods, AllowedMethod. +# AllowedMethod: to_ary, to_a, to_c, to_enum, to_h, to_hash, to_i, to_int, to_io, to_open, to_path, to_proc, to_r, to_regexp, to_str, to_s, to_sym +Style/TrivialAccessors: + Exclude: + - 'lib/apartment/elevators/host.rb' + - 'lib/apartment/elevators/subdomain.rb' + +# Offense count: 1 +# Cop supports --auto-correct. +Style/UnlessElse: + Exclude: + - 'lib/apartment/adapters/abstract_adapter.rb' + +# Offense count: 5 +# Cop supports --auto-correct. +# Configuration parameters: WordRegex. +# SupportedStyles: percent, brackets +Style/WordArray: + EnforcedStyle: percent + MinSize: 3 diff --git a/Gemfile b/Gemfile index bdbf6fe1..d24bee04 100644 --- a/Gemfile +++ b/Gemfile @@ -1,3 +1,5 @@ +# frozen_string_literal: true + source 'http://rubygems.org' gemspec @@ -5,6 +7,6 @@ gemspec gem 'rails', '>= 3.1.2' group :local do - gem 'pry' gem 'guard-rspec', '~> 4.2' + gem 'pry' end diff --git a/apartment.gemspec b/apartment.gemspec index e00a1304..f693da01 100644 --- a/apartment.gemspec +++ b/apartment.gemspec @@ -28,6 +28,7 @@ Gem::Specification.new do |s| s.add_development_dependency 'appraisal' s.add_development_dependency 'bundler', '>= 1.3', '< 2.0' s.add_development_dependency 'capybara', '~> 2.0' + s.add_development_dependency 'perx-rubocop', '~> 0.0.3' s.add_development_dependency 'rake', '~> 0.9' s.add_development_dependency 'rspec', '~> 3.4' s.add_development_dependency 'rspec-rails', '~> 3.4' diff --git a/spec/integration/apartment_rake_integration_spec.rb b/spec/integration/apartment_rake_integration_spec.rb index 4b9b098d..67013069 100644 --- a/spec/integration/apartment_rake_integration_spec.rb +++ b/spec/integration/apartment_rake_integration_spec.rb @@ -1,8 +1,9 @@ +# frozen_string_literal: true + require 'spec_helper' require 'rake' describe "apartment rake tasks", database: :postgresql do - before do @rake = Rake::Application.new Rake.application = @rake @@ -30,7 +31,6 @@ after { Rake.application = nil } context "with x number of databases" do - let(:x){ 1 + rand(5) } # random number of dbs to create let(:db_names){ x.times.map{ Apartment::Test.next_db } } let!(:company_count){ db_names.length } From 3846636b0c1e356d36bbd4a924eb59942e80416c Mon Sep 17 00:00:00 2001 From: Rui Baltazar Date: Mon, 17 Feb 2020 11:05:34 +0800 Subject: [PATCH 08/50] WIP - moved rubocop to gemfile instead of dev dependency --- Gemfile | 1 + README.md | 7 +++++++ apartment.gemspec | 1 - 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/Gemfile b/Gemfile index d24bee04..8bad8db6 100644 --- a/Gemfile +++ b/Gemfile @@ -5,6 +5,7 @@ source 'http://rubygems.org' gemspec gem 'rails', '>= 3.1.2' +gem 'perx-rubocop', '~> 0.0.3' group :local do gem 'guard-rspec', '~> 4.2' diff --git a/README.md b/README.md index f6c6d03b..cb6c82b9 100644 --- a/README.md +++ b/README.md @@ -569,6 +569,13 @@ end * If you're looking to help, check out the TODO file for some upcoming changes I'd like to implement in Apartment. +### Running bundle install + +mysql2 gem in some cases fails to install. +If you face problems running bundle install in OSX, try installing the gem running: + +`gem install mysql2 -v '0.5.3' -- --with-ldflags=-L/usr/local/opt/openssl/lib --with-cppflags=-I/usr/local/opt/openssl/include` + ## License Apartment is released under the [MIT License](http://www.opensource.org/licenses/MIT). diff --git a/apartment.gemspec b/apartment.gemspec index f693da01..e00a1304 100644 --- a/apartment.gemspec +++ b/apartment.gemspec @@ -28,7 +28,6 @@ Gem::Specification.new do |s| s.add_development_dependency 'appraisal' s.add_development_dependency 'bundler', '>= 1.3', '< 2.0' s.add_development_dependency 'capybara', '~> 2.0' - s.add_development_dependency 'perx-rubocop', '~> 0.0.3' s.add_development_dependency 'rake', '~> 0.9' s.add_development_dependency 'rspec', '~> 3.4' s.add_development_dependency 'rspec-rails', '~> 3.4' From 44e86eb602a3b0a6602d851048b8caa750c3f42c Mon Sep 17 00:00:00 2001 From: Rui Baltazar Date: Mon, 17 Feb 2020 11:18:54 +0800 Subject: [PATCH 09/50] WIP - moved rails 5.2 with ruby 2.2.9 to allowed failures --- .travis.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.travis.yml b/.travis.yml index cefc8560..9c634d9a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -36,6 +36,10 @@ jobs: - gemfile: gemfiles/rails_master.gemfile - rvm: jruby-9.1.17.0 gemfile: gemfiles/rails_5_0.gemfile + # Rails 5.2.4.1 is currently broken. Code has been fixed but no release + # for it yet - https://github.com/rails/rails/issues/38137 + - rvm: 2.2.9 + gemfile: gemfiles/rails_5_2.gemfile exclude: - rvm: 2.1.9 gemfile: gemfiles/rails_5_0.gemfile From 812d1292dfb3f6390cb415603b4c0e1157d9223a Mon Sep 17 00:00:00 2001 From: Rui Baltazar Date: Mon, 17 Feb 2020 11:26:53 +0800 Subject: [PATCH 10/50] WIP - added rubocop lint --- .github/workflows/.rubocop-linter.yml | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 .github/workflows/.rubocop-linter.yml diff --git a/.github/workflows/.rubocop-linter.yml b/.github/workflows/.rubocop-linter.yml new file mode 100644 index 00000000..cab9704b --- /dev/null +++ b/.github/workflows/.rubocop-linter.yml @@ -0,0 +1,22 @@ +name: Rubocop Lint + +on: [push] + +jobs: + build: + + runs-on: ubuntu-latest + + container: + image: ruby:2.6.5 + + steps: + - uses: actions/checkout@v1 + - name: Rubocop Linter + uses: andrewmcodes/rubocop-linter-action@v2.0.0 + with: + additional_gems: 'perx-rubocop:0.0.3' + fail_level: 'warning' + version: '0.77.0' + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} From 40d335035d5c9573521bcae1ccc0f5c883913bba Mon Sep 17 00:00:00 2001 From: Rui Baltazar Date: Sun, 16 Feb 2020 15:47:38 +0800 Subject: [PATCH 11/50] WIP - starting to cleanup --- .rubocop_todo.yml | 3 +-- spec/unit/migrator_spec.rb | 30 +++++++++++++++--------------- 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 383eaf02..e31033be 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -426,7 +426,7 @@ Style/Alias: # Cop supports --auto-correct. # Configuration parameters: EnforcedStyle, ProceduralMethods, FunctionalMethods, IgnoredMethods, AllowBracesOnProceduralOneLiners. # SupportedStyles: line_count_based, semantic, braces_for_chaining, always_braces -# ProceduralMethods: benchmark, bm, bmbm, create, each_with_object, measure, new, realtime, tap, with_object +# ProceduralMethods: benchmark, bm, create, each_with_object, measure, new, realtime, tap, with_object # FunctionalMethods: let, let!, subject, watch # IgnoredMethods: lambda, proc, it Style/BlockDelimiters: @@ -623,7 +623,6 @@ Style/PercentLiteralDelimiters: - 'spec/support/requirements.rb' - 'spec/unit/elevators/host_spec.rb' - 'spec/unit/elevators/subdomain_spec.rb' - - 'spec/unit/migrator_spec.rb' # Offense count: 1 # Cop supports --auto-correct. diff --git a/spec/unit/migrator_spec.rb b/spec/unit/migrator_spec.rb index 242b6b97..ade34c73 100644 --- a/spec/unit/migrator_spec.rb +++ b/spec/unit/migrator_spec.rb @@ -3,19 +3,19 @@ describe Apartment::Migrator do - let(:tenant){ Apartment::Test.next_db } + let(:tenant) { Apartment::Test.next_db } # Don't need a real switch here, just testing behaviour before { allow(Apartment::Tenant.adapter).to receive(:connect_to_new) } - context "with ActiveRecord below 5.2.0", skip: ActiveRecord.version >= Gem::Version.new("5.2.0") do + context 'with ActiveRecord below 5.2.0', skip: ActiveRecord.version >= Gem::Version.new('5.2.0') do before do allow(ActiveRecord::Migrator).to receive(:migrations_paths) { %w(spec/dummy/db/migrate) } allow(Apartment::Migrator).to receive(:activerecord_below_5_2?) { true } end - describe "::migrate" do - it "switches and migrates" do + describe '::migrate' do + it 'switches and migrates' do expect(Apartment::Tenant).to receive(:switch).with(tenant).and_call_original expect(ActiveRecord::Migrator).to receive(:migrate) @@ -23,8 +23,8 @@ end end - describe "::run" do - it "switches and runs" do + describe '::run' do + it 'switches and runs' do expect(Apartment::Tenant).to receive(:switch).with(tenant).and_call_original expect(ActiveRecord::Migrator).to receive(:run).with(:up, anything, 1234) @@ -32,8 +32,8 @@ end end - describe "::rollback" do - it "switches and rolls back" do + describe '::rollback' do + it 'switches and rolls back' do expect(Apartment::Tenant).to receive(:switch).with(tenant).and_call_original expect(ActiveRecord::Migrator).to receive(:rollback).with(anything, 2) @@ -42,13 +42,13 @@ end end - context "with ActiveRecord above or equal to 5.2.0", skip: ActiveRecord.version < Gem::Version.new("5.2.0") do + context 'with ActiveRecord above or equal to 5.2.0', skip: ActiveRecord.version < Gem::Version.new('5.2.0') do before do allow(Apartment::Migrator).to receive(:activerecord_below_5_2?) { false } end - describe "::migrate" do - it "switches and migrates" do + describe '::migrate' do + it 'switches and migrates' do expect(Apartment::Tenant).to receive(:switch).with(tenant).and_call_original expect_any_instance_of(ActiveRecord::MigrationContext).to receive(:migrate) @@ -56,8 +56,8 @@ end end - describe "::run" do - it "switches and runs" do + describe '::run' do + it 'switches and runs' do expect(Apartment::Tenant).to receive(:switch).with(tenant).and_call_original expect_any_instance_of(ActiveRecord::MigrationContext).to receive(:run).with(:up, 1234) @@ -65,8 +65,8 @@ end end - describe "::rollback" do - it "switches and rolls back" do + describe '::rollback' do + it 'switches and rolls back' do expect(Apartment::Tenant).to receive(:switch).with(tenant).and_call_original expect_any_instance_of(ActiveRecord::MigrationContext).to receive(:rollback).with(2) From b335c2a97058bfc0acf26e0744b8b047960124c7 Mon Sep 17 00:00:00 2001 From: Rui Baltazar Date: Sun, 16 Feb 2020 15:51:16 +0800 Subject: [PATCH 12/50] WIP - ran autocorrect --- lib/apartment.rb | 2 +- lib/apartment/adapters/abstract_adapter.rb | 4 ++-- lib/apartment/console.rb | 2 +- lib/apartment/tenant.rb | 4 +--- spec/adapters/mysql2_adapter_spec.rb | 2 +- spec/adapters/postgresql_adapter_spec.rb | 8 ++++---- spec/adapters/sqlite3_adapter_spec.rb | 2 +- spec/dummy/config/initializers/apartment.rb | 2 +- spec/examples/connection_adapter_examples.rb | 2 +- spec/examples/generic_adapter_examples.rb | 4 ++-- spec/examples/schema_adapter_examples.rb | 20 +++++++++---------- .../apartment_rake_integration_spec.rb | 10 +++++----- spec/integration/query_caching_spec.rb | 6 +++--- spec/integration/use_within_an_engine_spec.rb | 4 ++-- spec/spec_helper.rb | 2 +- spec/support/apartment_helpers.rb | 2 +- spec/support/contexts.rb | 8 ++++---- spec/support/setup.rb | 6 +++--- spec/tasks/apartment_rake_spec.rb | 12 +++++------ spec/tenant_spec.rb | 20 +++++++++---------- spec/unit/config_spec.rb | 4 ++-- spec/unit/elevators/domain_spec.rb | 2 +- spec/unit/elevators/generic_spec.rb | 6 +++--- spec/unit/elevators/host_hash_spec.rb | 2 +- spec/unit/elevators/host_spec.rb | 2 +- spec/unit/elevators/subdomain_spec.rb | 2 +- spec/unit/migrator_spec.rb | 2 +- spec/unit/reloader_spec.rb | 2 +- 28 files changed, 71 insertions(+), 73 deletions(-) diff --git a/lib/apartment.rb b/lib/apartment.rb index b20a7728..594dc1bd 100644 --- a/lib/apartment.rb +++ b/lib/apartment.rb @@ -84,7 +84,7 @@ def pg_excluded_names # Reset all the config for Apartment def reset - (ACCESSOR_METHODS + WRITER_METHODS).each{|method| remove_instance_variable(:"@#{method}") if instance_variable_defined?(:"@#{method}") } + (ACCESSOR_METHODS + WRITER_METHODS).each {|method| remove_instance_variable(:"@#{method}") if instance_variable_defined?(:"@#{method}") } end def extract_tenant_config diff --git a/lib/apartment/adapters/abstract_adapter.rb b/lib/apartment/adapters/abstract_adapter.rb index 78b844b9..c3982ee9 100644 --- a/lib/apartment/adapters/abstract_adapter.rb +++ b/lib/apartment/adapters/abstract_adapter.rb @@ -93,7 +93,7 @@ def switch(tenant = nil) # def each(tenants = Apartment.tenant_names) tenants.each do |tenant| - switch(tenant){ yield tenant } + switch(tenant) { yield tenant } end end @@ -116,7 +116,7 @@ def reset # def seed_data # Don't log the output of seeding the db - silence_warnings{ load_or_raise(Apartment.seed_data_file) } if Apartment.seed_data_file + silence_warnings { load_or_raise(Apartment.seed_data_file) } if Apartment.seed_data_file end alias_method :seed, :seed_data diff --git a/lib/apartment/console.rb b/lib/apartment/console.rb index 6ce18832..66cefda2 100644 --- a/lib/apartment/console.rb +++ b/lib/apartment/console.rb @@ -5,7 +5,7 @@ def reload!(print=true) puts "Reloading..." if print # This triggers the to_prepare callbacks - ActionDispatch::Callbacks.new(Proc.new {}).call({}) + ActionDispatch::Callbacks.new(Proc.new{}).call({}) # Manually init Apartment again once classes are reloaded Apartment::Tenant.init true diff --git a/lib/apartment/tenant.rb b/lib/apartment/tenant.rb index acabad31..83e3cdd5 100644 --- a/lib/apartment/tenant.rb +++ b/lib/apartment/tenant.rb @@ -40,9 +40,7 @@ def adapter raise "The adapter `#{adapter_method}` is not yet supported" end - unless respond_to?(adapter_method) - raise AdapterNotFound, "database configuration specifies nonexistent #{config[:adapter]} adapter" - end + raise AdapterNotFound, "database configuration specifies nonexistent #{config[:adapter]} adapter" unless respond_to?(adapter_method) send(adapter_method, config) end diff --git a/spec/adapters/mysql2_adapter_spec.rb b/spec/adapters/mysql2_adapter_spec.rb index 78f7b589..831affed 100644 --- a/spec/adapters/mysql2_adapter_spec.rb +++ b/spec/adapters/mysql2_adapter_spec.rb @@ -4,7 +4,7 @@ describe Apartment::Adapters::Mysql2Adapter, database: :mysql do unless defined?(JRUBY_VERSION) - subject(:adapter){ Apartment::Tenant.mysql2_adapter config } + subject(:adapter) { Apartment::Tenant.mysql2_adapter config } def tenant_names ActiveRecord::Base.connection.execute("SELECT schema_name FROM information_schema.schemata").collect { |row| row[0] } diff --git a/spec/adapters/postgresql_adapter_spec.rb b/spec/adapters/postgresql_adapter_spec.rb index e246fd10..16a85eca 100644 --- a/spec/adapters/postgresql_adapter_spec.rb +++ b/spec/adapters/postgresql_adapter_spec.rb @@ -4,11 +4,11 @@ describe Apartment::Adapters::PostgresqlAdapter, database: :postgresql do unless defined?(JRUBY_VERSION) - subject{ Apartment::Tenant.postgresql_adapter config } + subject { Apartment::Tenant.postgresql_adapter config } context "using schemas with schema.rb" do - before{ Apartment.use_schemas = true } + before { Apartment.use_schemas = true } # Not sure why, but somehow using let(:tenant_names) memoizes for the whole example group, not just each test def tenant_names @@ -23,7 +23,7 @@ def tenant_names context "using schemas with SQL dump" do - before{ Apartment.use_schemas = true; Apartment.use_sql = true } + before { Apartment.use_schemas = true; Apartment.use_sql = true } # Not sure why, but somehow using let(:tenant_names) memoizes for the whole example group, not just each test def tenant_names @@ -44,7 +44,7 @@ def tenant_names context "using connections" do - before{ Apartment.use_schemas = false } + before { Apartment.use_schemas = false } # Not sure why, but somehow using let(:tenant_names) memoizes for the whole example group, not just each test def tenant_names diff --git a/spec/adapters/sqlite3_adapter_spec.rb b/spec/adapters/sqlite3_adapter_spec.rb index 55004952..f6efa035 100644 --- a/spec/adapters/sqlite3_adapter_spec.rb +++ b/spec/adapters/sqlite3_adapter_spec.rb @@ -4,7 +4,7 @@ describe Apartment::Adapters::Sqlite3Adapter, database: :sqlite do unless defined?(JRUBY_VERSION) - subject{ Apartment::Tenant.sqlite3_adapter config } + subject { Apartment::Tenant.sqlite3_adapter config } context "using connections" do def tenant_names diff --git a/spec/dummy/config/initializers/apartment.rb b/spec/dummy/config/initializers/apartment.rb index a49dda91..d7d0fcfc 100644 --- a/spec/dummy/config/initializers/apartment.rb +++ b/spec/dummy/config/initializers/apartment.rb @@ -1,4 +1,4 @@ Apartment.configure do |config| config.excluded_models = ["Company"] - config.tenant_names = lambda{ Company.pluck(:database) } + config.tenant_names = lambda { Company.pluck(:database) } end diff --git a/spec/examples/connection_adapter_examples.rb b/spec/examples/connection_adapter_examples.rb index a7d4ac9b..b335e182 100644 --- a/spec/examples/connection_adapter_examples.rb +++ b/spec/examples/connection_adapter_examples.rb @@ -3,7 +3,7 @@ shared_examples_for "a connection based apartment adapter" do include Apartment::Spec::AdapterRequirements - let(:default_tenant){ subject.switch{ ActiveRecord::Base.connection.current_database } } + let(:default_tenant) { subject.switch { ActiveRecord::Base.connection.current_database } } describe "#init" do after do diff --git a/spec/examples/generic_adapter_examples.rb b/spec/examples/generic_adapter_examples.rb index 6c1b32ac..1ba59291 100644 --- a/spec/examples/generic_adapter_examples.rb +++ b/spec/examples/generic_adapter_examples.rb @@ -56,7 +56,7 @@ expect(subject.current).not_to eq(db2) - subject.switch(db2){ expect(User.count).to eq(@count + 1) } + subject.switch(db2) { expect(User.count).to eq(@count + 1) } end it "should raise error when the schema.rb is missing unless Apartment.use_sql is set to true" do @@ -115,7 +115,7 @@ subject.switch!(db2) expect { - subject.switch(db1){ subject.drop(db2) } + subject.switch(db1) { subject.drop(db2) } }.to_not raise_error end end diff --git a/spec/examples/schema_adapter_examples.rb b/spec/examples/schema_adapter_examples.rb index 5eed5091..606033b9 100644 --- a/spec/examples/schema_adapter_examples.rb +++ b/spec/examples/schema_adapter_examples.rb @@ -3,9 +3,9 @@ shared_examples_for "a schema based apartment adapter" do include Apartment::Spec::AdapterRequirements - let(:schema1){ db1 } - let(:schema2){ db2 } - let(:public_schema){ default_tenant } + let(:schema1) { db1 } + let(:schema2) { db2 } + let(:public_schema) { default_tenant } describe "#init" do @@ -75,11 +75,11 @@ expect(connection.schema_search_path).not_to start_with %{"#{schema2}"} - subject.switch(schema2){ expect(User.count).to eq(@count + 1) } + subject.switch(schema2) { expect(User.count).to eq(@count + 1) } end context "numeric database names" do - let(:db){ 1234 } + let(:db) { 1234 } it "should allow them" do expect { subject.create(db) @@ -87,7 +87,7 @@ expect(tenant_names).to include(db.to_s) end - after{ subject.drop(db) } + after { subject.drop(db) } end end @@ -100,7 +100,7 @@ end context "numeric database names" do - let(:db){ 1234 } + let(:db) { 1234 } it "should be able to drop them" do subject.create(db) @@ -176,7 +176,7 @@ end context "numeric databases" do - let(:db){ 1234 } + let(:db) { 1234 } it "should connect to them" do subject.create(db) @@ -187,7 +187,7 @@ expect(connection.schema_search_path).to start_with %{"#{db.to_s}"} end - after{ subject.drop(db) } + after { subject.drop(db) } end describe "with default_schema specified", :default_schema => true do @@ -206,7 +206,7 @@ context "persistent_schemas", :persistent_schemas => true do - before{ subject.switch!(schema1) } + before { subject.switch!(schema1) } it "maintains the persistent schemas in the schema_search_path" do expect(connection.schema_search_path).to end_with persistent_schemas.map { |schema| %{"#{schema}"} }.join(', ') diff --git a/spec/integration/apartment_rake_integration_spec.rb b/spec/integration/apartment_rake_integration_spec.rb index 67013069..c8e822cf 100644 --- a/spec/integration/apartment_rake_integration_spec.rb +++ b/spec/integration/apartment_rake_integration_spec.rb @@ -20,7 +20,7 @@ Apartment.configure do |config| config.use_schemas = true config.excluded_models = ["Company"] - config.tenant_names = lambda{ Company.pluck(:database) } + config.tenant_names = lambda { Company.pluck(:database) } end Apartment::Tenant.reload!(config) @@ -31,9 +31,9 @@ after { Rake.application = nil } context "with x number of databases" do - let(:x){ 1 + rand(5) } # random number of dbs to create - let(:db_names){ x.times.map{ Apartment::Test.next_db } } - let!(:company_count){ db_names.length } + let(:x) { 1 + rand(5) } # random number of dbs to create + let(:db_names) { x.times.map { Apartment::Test.next_db } } + let!(:company_count) { db_names.length } before do db_names.collect do |db_name| @@ -43,7 +43,7 @@ end after do - db_names.each{ |db| Apartment::Tenant.drop(db) } + db_names.each { |db| Apartment::Tenant.drop(db) } Company.delete_all end diff --git a/spec/integration/query_caching_spec.rb b/spec/integration/query_caching_spec.rb index 7bf08996..10cbaa97 100644 --- a/spec/integration/query_caching_spec.rb +++ b/spec/integration/query_caching_spec.rb @@ -7,7 +7,7 @@ before do Apartment.configure do |config| config.excluded_models = ["Company"] - config.tenant_names = lambda{ Company.pluck(:database) } + config.tenant_names = lambda { Company.pluck(:database) } config.use_schemas = true end @@ -20,7 +20,7 @@ end after do - db_names.each{ |db| Apartment::Tenant.drop(db) } + db_names.each { |db| Apartment::Tenant.drop(db) } Apartment::Tenant.reset Company.delete_all end @@ -47,7 +47,7 @@ before do Apartment.configure do |config| config.excluded_models = ["Company"] - config.tenant_names = lambda{ Company.pluck(:database) } + config.tenant_names = lambda { Company.pluck(:database) } config.use_schemas = false end diff --git a/spec/integration/use_within_an_engine_spec.rb b/spec/integration/use_within_an_engine_spec.rb index 904a9238..21873df1 100644 --- a/spec/integration/use_within_an_engine_spec.rb +++ b/spec/integration/use_within_an_engine_spec.rb @@ -10,11 +10,11 @@ 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 }.to_not 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 }.to_not raise_error end context 'when Apartment.db_migrate_tenants is false' do diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 73cca8f4..74aa9c8d 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -22,7 +22,7 @@ begin require 'pry' - silence_warnings{ IRB = Pry } + silence_warnings { IRB = Pry } rescue LoadError end diff --git a/spec/support/apartment_helpers.rb b/spec/support/apartment_helpers.rb index 795f172a..9c4a43bf 100644 --- a/spec/support/apartment_helpers.rb +++ b/spec/support/apartment_helpers.rb @@ -28,7 +28,7 @@ def create_schema(schema) def load_schema(version = 3) file = File.expand_path("../../schemas/v#{version}.rb", __FILE__) - silence_warnings{ load(file) } + silence_warnings { load(file) } end def migrate diff --git a/spec/support/contexts.rb b/spec/support/contexts.rb index ffb10a04..3fbf1a42 100644 --- a/spec/support/contexts.rb +++ b/spec/support/contexts.rb @@ -1,7 +1,7 @@ # Some shared contexts for specs shared_context "with default schema", :default_schema => true do - let(:default_schema){ Apartment::Test.next_db } + let(:default_schema) { Apartment::Test.next_db } before do Apartment::Test.create_schema(default_schema) @@ -38,15 +38,15 @@ end shared_context "persistent_schemas", :persistent_schemas => true do - let(:persistent_schemas){ ['hstore', 'postgis'] } + let(:persistent_schemas) { ['hstore', 'postgis'] } before do - persistent_schemas.map{|schema| subject.create(schema) } + persistent_schemas.map {|schema| subject.create(schema) } Apartment.persistent_schemas = persistent_schemas end after do Apartment.persistent_schemas = [] - persistent_schemas.map{|schema| subject.drop(schema) } + persistent_schemas.map {|schema| subject.drop(schema) } end end \ No newline at end of file diff --git a/spec/support/setup.rb b/spec/support/setup.rb index 8441da3d..d4a68512 100644 --- a/spec/support/setup.rb +++ b/spec/support/setup.rb @@ -4,9 +4,9 @@ module Setup def self.included(base) base.instance_eval do - let(:db1){ Apartment::Test.next_db } - let(:db2){ Apartment::Test.next_db } - let(:connection){ ActiveRecord::Base.connection } + let(:db1) { Apartment::Test.next_db } + let(:db2) { Apartment::Test.next_db } + let(:connection) { ActiveRecord::Base.connection } # This around ensures that we run these hooks before and after # any before/after hooks defined in individual tests diff --git a/spec/tasks/apartment_rake_spec.rb b/spec/tasks/apartment_rake_spec.rb index 0f86aeee..0514676d 100644 --- a/spec/tasks/apartment_rake_spec.rb +++ b/spec/tasks/apartment_rake_spec.rb @@ -27,12 +27,12 @@ Apartment::Test.load_schema end - let(:version){ '1234' } + let(:version) { '1234' } context 'database migration' do - let(:tenant_names){ 3.times.map{ Apartment::Test.next_db } } - let(:tenant_count){ tenant_names.length } + let(:tenant_names) { 3.times.map { Apartment::Test.next_db } } + let(:tenant_count) { tenant_names.length } before do allow(Apartment).to receive(:tenant_names).and_return tenant_names @@ -57,7 +57,7 @@ end it "requires a version to migrate to" do - expect{ + expect { @rake['apartment:migrate:up'].invoke }.to raise_error("VERSION is required") end @@ -84,7 +84,7 @@ end it "requires a version to migrate to" do - expect{ + expect { @rake['apartment:migrate:down'].invoke }.to raise_error("VERSION is required") end @@ -104,7 +104,7 @@ end describe "apartment:rollback" do - let(:step){ '3' } + let(:step) { '3' } it "should rollback dbs" do expect(Apartment::Migrator).to receive(:rollback).exactly(tenant_count).times diff --git a/spec/tenant_spec.rb b/spec/tenant_spec.rb index 261185a6..17dfe801 100644 --- a/spec/tenant_spec.rb +++ b/spec/tenant_spec.rb @@ -18,7 +18,7 @@ before do subject.create db1 end - after{ subject.drop db1 } + after { subject.drop db1 } # it "should recover from incorrect database" do # session = Capybara::Session.new(:rack_test, Capybara.app) @@ -94,7 +94,7 @@ subject.create db1 end - after{ subject.drop db1 } + after { subject.drop db1 } describe "#create" do it "should seed data" do @@ -105,19 +105,19 @@ describe "#switch!" do - let(:x){ rand(3) } + let(:x) { rand(3) } context "creating models" do - before{ subject.create db2 } - after{ subject.drop db2 } + before { subject.create db2 } + after { subject.drop db2 } it "should create a model instance in the current schema" do subject.switch! db2 - db2_count = User.count + x.times{ User.create } + db2_count = User.count + x.times { User.create } subject.switch! db1 - db_count = User.count + x.times{ User.create } + db_count = User.count + x.times { User.create } subject.switch! db2 expect(User.count).to eq(db2_count) @@ -146,10 +146,10 @@ it "should create excluded models in public schema" do subject.reset # ensure we're on public schema - count = Company.count + x.times{ Company.create } + count = Company.count + x.times { Company.create } subject.switch! db1 - x.times{ Company.create } + x.times { Company.create } expect(Company.count).to eq(count + x) subject.reset expect(Company.count).to eq(count + x) @@ -167,7 +167,7 @@ end end - after{ subject.drop db1 } + after { subject.drop db1 } it 'should seed from default path' do subject.create db1 diff --git a/spec/unit/config_spec.rb b/spec/unit/config_spec.rb index 44901952..40320a31 100644 --- a/spec/unit/config_spec.rb +++ b/spec/unit/config_spec.rb @@ -4,8 +4,8 @@ describe "#config" do - let(:excluded_models){ ["Company"] } - let(:seed_data_file_path){ "#{Rails.root}/db/seeds/import.rb" } + let(:excluded_models) { ["Company"] } + let(:seed_data_file_path) { "#{Rails.root}/db/seeds/import.rb" } def tenant_names_from_array(names) names.each_with_object({}) do |tenant, hash| diff --git a/spec/unit/elevators/domain_spec.rb b/spec/unit/elevators/domain_spec.rb index cbdf9fb2..713cd88c 100644 --- a/spec/unit/elevators/domain_spec.rb +++ b/spec/unit/elevators/domain_spec.rb @@ -3,7 +3,7 @@ describe Apartment::Elevators::Domain do - subject(:elevator){ described_class.new(Proc.new{}) } + subject(:elevator) { described_class.new(Proc.new{}) } describe "#parse_tenant_name" do it "parses the host for a domain name" do diff --git a/spec/unit/elevators/generic_spec.rb b/spec/unit/elevators/generic_spec.rb index 6278dfe7..bca84dc9 100644 --- a/spec/unit/elevators/generic_spec.rb +++ b/spec/unit/elevators/generic_spec.rb @@ -9,11 +9,11 @@ def parse_tenant_name(*) end end - subject(:elevator){ described_class.new(Proc.new{}) } + subject(:elevator) { described_class.new(Proc.new{}) } describe "#call" do it "calls the processor if given" do - elevator = described_class.new(Proc.new{}, Proc.new{'tenant1'}) + elevator = described_class.new(Proc.new{}, Proc.new {'tenant1'}) expect(Apartment::Tenant).to receive(:switch).with('tenant1') @@ -35,7 +35,7 @@ def parse_tenant_name(*) end it "calls the block implementation of `switch`" do - elevator = MyElevator.new(Proc.new{}, Proc.new{'tenant2'}) + elevator = MyElevator.new(Proc.new{}, Proc.new {'tenant2'}) expect(Apartment::Tenant).to receive(:switch).with('tenant2').and_yield elevator.call('HTTP_HOST' => 'foo.bar.com') diff --git a/spec/unit/elevators/host_hash_spec.rb b/spec/unit/elevators/host_hash_spec.rb index c748b70d..039912ec 100644 --- a/spec/unit/elevators/host_hash_spec.rb +++ b/spec/unit/elevators/host_hash_spec.rb @@ -3,7 +3,7 @@ describe Apartment::Elevators::HostHash do - subject(:elevator){ Apartment::Elevators::HostHash.new(Proc.new{}, 'example.com' => 'example_tenant') } + subject(:elevator) { Apartment::Elevators::HostHash.new(Proc.new{}, 'example.com' => 'example_tenant') } describe "#parse_tenant_name" do it "parses the host for a domain name" do diff --git a/spec/unit/elevators/host_spec.rb b/spec/unit/elevators/host_spec.rb index 96ea057c..c8daaa37 100644 --- a/spec/unit/elevators/host_spec.rb +++ b/spec/unit/elevators/host_spec.rb @@ -3,7 +3,7 @@ describe Apartment::Elevators::Host do - subject(:elevator){ described_class.new(Proc.new{}) } + subject(:elevator) { described_class.new(Proc.new{}) } describe "#parse_tenant_name" do diff --git a/spec/unit/elevators/subdomain_spec.rb b/spec/unit/elevators/subdomain_spec.rb index 1df11466..71feb6c7 100644 --- a/spec/unit/elevators/subdomain_spec.rb +++ b/spec/unit/elevators/subdomain_spec.rb @@ -3,7 +3,7 @@ describe Apartment::Elevators::Subdomain do - subject(:elevator){ described_class.new(Proc.new{}) } + subject(:elevator) { described_class.new(Proc.new{}) } describe "#parse_tenant_name" do context "assuming one tld" do diff --git a/spec/unit/migrator_spec.rb b/spec/unit/migrator_spec.rb index ade34c73..0022be74 100644 --- a/spec/unit/migrator_spec.rb +++ b/spec/unit/migrator_spec.rb @@ -10,7 +10,7 @@ context 'with ActiveRecord below 5.2.0', skip: ActiveRecord.version >= Gem::Version.new('5.2.0') do before do - allow(ActiveRecord::Migrator).to receive(:migrations_paths) { %w(spec/dummy/db/migrate) } + allow(ActiveRecord::Migrator).to receive(:migrations_paths) { %w[spec/dummy/db/migrate] } allow(Apartment::Migrator).to receive(:activerecord_below_5_2?) { true } end diff --git a/spec/unit/reloader_spec.rb b/spec/unit/reloader_spec.rb index ffe30988..5034f7dc 100644 --- a/spec/unit/reloader_spec.rb +++ b/spec/unit/reloader_spec.rb @@ -13,7 +13,7 @@ Company.reset_table_name # ensure we're clean end - subject{ Apartment::Reloader.new(double("Rack::Application", :call => nil)) } + subject { Apartment::Reloader.new(double("Rack::Application", :call => nil)) } it "should initialize apartment when called" do expect(Company.table_name).not_to include('public.') From 017c7a8d8935b741e96d8fd1b45f8c3eba27d8da Mon Sep 17 00:00:00 2001 From: Rui Baltazar Date: Sun, 16 Feb 2020 16:01:51 +0800 Subject: [PATCH 13/50] [Hotfix] Fixed modifier indentation --- .rubocop_todo.yml | 16 +--------------- lib/apartment/adapters/abstract_adapter.rb | 4 ++-- lib/apartment/adapters/abstract_jdbc_adapter.rb | 2 +- .../adapters/jdbc_postgresql_adapter.rb | 4 ++-- lib/apartment/adapters/mysql2_adapter.rb | 4 ++-- lib/apartment/adapters/postgresql_adapter.rb | 8 ++++---- lib/apartment/adapters/sqlite3_adapter.rb | 4 ++-- lib/apartment/elevators/subdomain.rb | 2 +- 8 files changed, 15 insertions(+), 29 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index e31033be..7f794fec 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -19,20 +19,6 @@ Bundler/OrderedGems: - 'gemfiles/rails_6_0.gemfile' - 'gemfiles/rails_master.gemfile' -# Offense count: 13 -# Cop supports --auto-correct. -# Configuration parameters: EnforcedStyle, IndentationWidth. -# SupportedStyles: outdent, indent -Layout/AccessModifierIndentation: - Exclude: - - 'lib/apartment/adapters/abstract_adapter.rb' - - 'lib/apartment/adapters/abstract_jdbc_adapter.rb' - - 'lib/apartment/adapters/jdbc_postgresql_adapter.rb' - - 'lib/apartment/adapters/mysql2_adapter.rb' - - 'lib/apartment/adapters/postgresql_adapter.rb' - - 'lib/apartment/adapters/sqlite3_adapter.rb' - - 'lib/apartment/elevators/subdomain.rb' - # Offense count: 4 # Cop supports --auto-correct. # Configuration parameters: EnforcedStyle, IndentationWidth. @@ -426,7 +412,7 @@ Style/Alias: # Cop supports --auto-correct. # Configuration parameters: EnforcedStyle, ProceduralMethods, FunctionalMethods, IgnoredMethods, AllowBracesOnProceduralOneLiners. # SupportedStyles: line_count_based, semantic, braces_for_chaining, always_braces -# ProceduralMethods: benchmark, bm, create, each_with_object, measure, new, realtime, tap, with_object +# ProceduralMethods: benchmark, bm, bmbm, create, each_with_object, measure, new, realtime, tap, with_object # FunctionalMethods: let, let!, subject, watch # IgnoredMethods: lambda, proc, it Style/BlockDelimiters: diff --git a/lib/apartment/adapters/abstract_adapter.rb b/lib/apartment/adapters/abstract_adapter.rb index c3982ee9..7974a769 100644 --- a/lib/apartment/adapters/abstract_adapter.rb +++ b/lib/apartment/adapters/abstract_adapter.rb @@ -139,7 +139,7 @@ def environmentify(tenant) end end - protected + protected def process_excluded_model(excluded_model) excluded_model.constantize.establish_connection @config @@ -234,7 +234,7 @@ def db_connection_config(tenant) Apartment.db_config_for(tenant).clone end - def with_neutral_connection(tenant, &block) + def with_neutral_connection(tenant, &block) if Apartment.with_multi_server_setup # neutral connection is necessary whenever you need to create/remove a database from a server. # example: when you use postgresql, you need to connect to the default postgresql database before you create your own. diff --git a/lib/apartment/adapters/abstract_jdbc_adapter.rb b/lib/apartment/adapters/abstract_jdbc_adapter.rb index c4f7f32f..be72414e 100644 --- a/lib/apartment/adapters/abstract_jdbc_adapter.rb +++ b/lib/apartment/adapters/abstract_jdbc_adapter.rb @@ -4,7 +4,7 @@ module Apartment module Adapters class AbstractJDBCAdapter < AbstractAdapter - private + private def multi_tenantify_with_tenant_db_name(config, tenant) config[:url] = "#{config[:url].gsub(/(\S+)\/.+$/, '\1')}/#{environmentify(tenant)}" diff --git a/lib/apartment/adapters/jdbc_postgresql_adapter.rb b/lib/apartment/adapters/jdbc_postgresql_adapter.rb index f0922d8c..4278a2d6 100644 --- a/lib/apartment/adapters/jdbc_postgresql_adapter.rb +++ b/lib/apartment/adapters/jdbc_postgresql_adapter.rb @@ -15,7 +15,7 @@ module Adapters # Default adapter when not using Postgresql Schemas class JDBCPostgresqlAdapter < PostgresqlAdapter - private + private def multi_tenantify_with_tenant_db_name(config, tenant) config[:url] = "#{config[:url].gsub(/(\S+)\/.+$/, '\1')}/#{environmentify(tenant)}" @@ -46,7 +46,7 @@ def connect_to_new(tenant = nil) raise TenantNotFound, "One of the following schema(s) is invalid: #{full_search_path}" end - private + private def rescue_from ActiveRecord::JDBCError diff --git a/lib/apartment/adapters/mysql2_adapter.rb b/lib/apartment/adapters/mysql2_adapter.rb index d1e324ca..780710d3 100644 --- a/lib/apartment/adapters/mysql2_adapter.rb +++ b/lib/apartment/adapters/mysql2_adapter.rb @@ -19,7 +19,7 @@ def initialize(config) @default_tenant = config[:database] end - protected + protected def rescue_from Mysql2::Error @@ -40,7 +40,7 @@ def reset Apartment.connection.execute "use `#{default_tenant}`" end - protected + protected # Connect to new tenant # diff --git a/lib/apartment/adapters/postgresql_adapter.rb b/lib/apartment/adapters/postgresql_adapter.rb index 446b9b5d..cd1a6df5 100644 --- a/lib/apartment/adapters/postgresql_adapter.rb +++ b/lib/apartment/adapters/postgresql_adapter.rb @@ -15,7 +15,7 @@ module Adapters # Default adapter when not using Postgresql Schemas class PostgresqlAdapter < AbstractAdapter - private + private def rescue_from PG::Error @@ -44,7 +44,7 @@ def current @current || default_tenant end - protected + protected def process_excluded_model(excluded_model) excluded_model.constantize.tap do |klass| @@ -79,7 +79,7 @@ def connect_to_new(tenant = nil) raise TenantNotFound, "One of the following schema(s) is invalid: \"#{tenant}\" #{full_search_path}" end - private + private def create_tenant_command(conn, tenant) conn.execute(%{CREATE SCHEMA "#{tenant}"}) @@ -122,7 +122,7 @@ def import_database_schema end end - private + private # Re-set search path after the schema is imported. # Postgres now sets search path to empty before dumping the schema diff --git a/lib/apartment/adapters/sqlite3_adapter.rb b/lib/apartment/adapters/sqlite3_adapter.rb index dff5a21b..e16dd544 100644 --- a/lib/apartment/adapters/sqlite3_adapter.rb +++ b/lib/apartment/adapters/sqlite3_adapter.rb @@ -26,7 +26,7 @@ def current File.basename(Apartment.connection.instance_variable_get(:@config)[:database], '.sqlite3') end - protected + protected def connect_to_new(tenant) raise TenantNotFound, @@ -46,7 +46,7 @@ def create_tenant(tenant) end end - private + private def database_file(tenant) "#{@default_dir}/#{environmentify(tenant)}.sqlite3" diff --git a/lib/apartment/elevators/subdomain.rb b/lib/apartment/elevators/subdomain.rb index c625e29b..b522bfef 100644 --- a/lib/apartment/elevators/subdomain.rb +++ b/lib/apartment/elevators/subdomain.rb @@ -29,7 +29,7 @@ def parse_tenant_name(request) tenant.presence end - protected + protected # *Almost* a direct ripoff of ActionDispatch::Request subdomain methods From 7d66739c33ed67a83eb8094ed432f53d97d8e9b6 Mon Sep 17 00:00:00 2001 From: Rui Baltazar Date: Sun, 16 Feb 2020 16:03:12 +0800 Subject: [PATCH 14/50] WIP - fixed braces around hash params --- .rubocop_todo.yml | 8 -------- lib/apartment/adapters/jdbc_postgresql_adapter.rb | 2 +- 2 files changed, 1 insertion(+), 9 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 7f794fec..6932727c 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -425,14 +425,6 @@ Style/BlockDelimiters: - 'spec/tenant_spec.rb' - 'spec/unit/elevators/generic_spec.rb' -# Offense count: 1 -# Cop supports --auto-correct. -# Configuration parameters: EnforcedStyle. -# SupportedStyles: braces, no_braces, context_dependent -Style/BracesAroundHashParameters: - Exclude: - - 'lib/apartment/adapters/jdbc_postgresql_adapter.rb' - # Offense count: 5 # Cop supports --auto-correct. # Configuration parameters: Keywords. diff --git a/lib/apartment/adapters/jdbc_postgresql_adapter.rb b/lib/apartment/adapters/jdbc_postgresql_adapter.rb index 4278a2d6..6b0f18cb 100644 --- a/lib/apartment/adapters/jdbc_postgresql_adapter.rb +++ b/lib/apartment/adapters/jdbc_postgresql_adapter.rb @@ -22,7 +22,7 @@ def multi_tenantify_with_tenant_db_name(config, tenant) end def create_tenant_command(conn, tenant) - conn.create_database(environmentify(tenant), { :thisisahack => '' }) + conn.create_database(environmentify(tenant), :thisisahack => '') end def rescue_from From 0cadb661207f25bc4f44e1e1d286cc879d4be633 Mon Sep 17 00:00:00 2001 From: Rui Baltazar Date: Sun, 16 Feb 2020 16:05:41 +0800 Subject: [PATCH 15/50] WIP - fixed array of strings --- .rubocop_todo.yml | 14 -------------- lib/apartment/adapters/abstract_adapter.rb | 2 +- spec/support/contexts.rb | 4 ++-- spec/unit/config_spec.rb | 4 ++-- 4 files changed, 5 insertions(+), 19 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 6932727c..9514bce9 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -716,17 +716,3 @@ Style/TrivialAccessors: Exclude: - 'lib/apartment/elevators/host.rb' - 'lib/apartment/elevators/subdomain.rb' - -# Offense count: 1 -# Cop supports --auto-correct. -Style/UnlessElse: - Exclude: - - 'lib/apartment/adapters/abstract_adapter.rb' - -# Offense count: 5 -# Cop supports --auto-correct. -# Configuration parameters: WordRegex. -# SupportedStyles: percent, brackets -Style/WordArray: - EnforcedStyle: percent - MinSize: 3 diff --git a/lib/apartment/adapters/abstract_adapter.rb b/lib/apartment/adapters/abstract_adapter.rb index 7974a769..4ad8f870 100644 --- a/lib/apartment/adapters/abstract_adapter.rb +++ b/lib/apartment/adapters/abstract_adapter.rb @@ -126,7 +126,7 @@ def seed_data # @return {String} tenant name with Rails environment *optionally* prepended # def environmentify(tenant) - unless tenant.include?(Rails.env) + if !tenant.include?(Rails.env) if Apartment.prepend_environment "#{Rails.env}_#{tenant}" elsif Apartment.append_environment diff --git a/spec/support/contexts.rb b/spec/support/contexts.rb index 3fbf1a42..844f4e0a 100644 --- a/spec/support/contexts.rb +++ b/spec/support/contexts.rb @@ -38,7 +38,7 @@ end shared_context "persistent_schemas", :persistent_schemas => true do - let(:persistent_schemas) { ['hstore', 'postgis'] } + let(:persistent_schemas) { %w[hstore postgis] } before do persistent_schemas.map {|schema| subject.create(schema) } @@ -49,4 +49,4 @@ Apartment.persistent_schemas = [] persistent_schemas.map {|schema| subject.drop(schema) } end -end \ No newline at end of file +end diff --git a/spec/unit/config_spec.rb b/spec/unit/config_spec.rb index 40320a31..eea2e2fe 100644 --- a/spec/unit/config_spec.rb +++ b/spec/unit/config_spec.rb @@ -60,7 +60,7 @@ def tenant_names_from_array(names) end context "tenant_names as string array" do - let(:tenant_names) { ['users', 'companies'] } + 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) @@ -72,7 +72,7 @@ def tenant_names_from_array(names) end context "tenant_names as proc returning an array" do - let(:tenant_names) { lambda { ['users', 'companies'] } } + let(:tenant_names) { lambda { %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) From 8ba3245a357c630c929d9a0b48ed7a9ba81e78a9 Mon Sep 17 00:00:00 2001 From: Rui Baltazar Date: Sun, 16 Feb 2020 16:07:43 +0800 Subject: [PATCH 16/50] [Hotfix] fixed array of strings for schemas rb --- .rubocop.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.rubocop.yml b/.rubocop.yml index 902dcf64..50fd0197 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -3,3 +3,7 @@ inherit_from: .rubocop_todo.yml inherit_gem: perx-rubocop: - default.yml + +Style/WordArray: + Exclude: + - spec/schemas/**/*.rb From b4371899893414c8e1019015575e33b9631c5175 Mon Sep 17 00:00:00 2001 From: Rui Baltazar Date: Sun, 16 Feb 2020 16:14:03 +0800 Subject: [PATCH 17/50] WIP - removed a few more broken rules in rubocop --- .rubocop_todo.yml | 17 ----------------- lib/apartment/adapters/postgresql_adapter.rb | 2 +- lib/apartment/elevators/host.rb | 8 +++----- lib/apartment/elevators/subdomain.rb | 6 ++---- 4 files changed, 6 insertions(+), 27 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 9514bce9..f0c67aff 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -699,20 +699,3 @@ Style/SymbolArray: Style/SymbolProc: Exclude: - 'lib/tasks/apartment.rake' - -# Offense count: 1 -# Cop supports --auto-correct. -# Configuration parameters: EnforcedStyleForMultiline. -# SupportedStylesForMultiline: comma, consistent_comma, no_comma -Style/TrailingCommaInArrayLiteral: - Exclude: - - 'lib/apartment/adapters/postgresql_adapter.rb' - -# Offense count: 2 -# Cop supports --auto-correct. -# Configuration parameters: ExactNameMatch, AllowPredicates, AllowDSLWriters, IgnoreClassMethods, AllowedMethod. -# AllowedMethod: to_ary, to_a, to_c, to_enum, to_h, to_hash, to_i, to_int, to_io, to_open, to_path, to_proc, to_r, to_regexp, to_str, to_s, to_sym -Style/TrivialAccessors: - Exclude: - - 'lib/apartment/elevators/host.rb' - - 'lib/apartment/elevators/subdomain.rb' diff --git a/lib/apartment/adapters/postgresql_adapter.rb b/lib/apartment/adapters/postgresql_adapter.rb index cd1a6df5..070415a0 100644 --- a/lib/apartment/adapters/postgresql_adapter.rb +++ b/lib/apartment/adapters/postgresql_adapter.rb @@ -111,7 +111,7 @@ class PostgresqlSchemaFromSqlAdapter < PostgresqlSchemaAdapter /SET row_security/i, # new in postgresql 9.5 /SET idle_in_transaction_session_timeout/i, # new in postgresql 9.6 /CREATE SCHEMA public/i, - /COMMENT ON SCHEMA public/i, + /COMMENT ON SCHEMA public/i ] diff --git a/lib/apartment/elevators/host.rb b/lib/apartment/elevators/host.rb index c8cbd2f8..c62d47e2 100644 --- a/lib/apartment/elevators/host.rb +++ b/lib/apartment/elevators/host.rb @@ -12,14 +12,12 @@ module Elevators # www.a.b.c.d.com => a.b.c.d.com # class Host < Generic + attr_writer ignored_first_subdomains + def self.ignored_first_subdomains @ignored_first_subdomains ||= [] end - def self.ignored_first_subdomains=(arg) - @ignored_first_subdomains = arg - end - def parse_tenant_name(request) return nil if request.host.blank? parts = request.host.split('.') @@ -27,4 +25,4 @@ def parse_tenant_name(request) end end end -end \ No newline at end of file +end diff --git a/lib/apartment/elevators/subdomain.rb b/lib/apartment/elevators/subdomain.rb index b522bfef..8380f758 100644 --- a/lib/apartment/elevators/subdomain.rb +++ b/lib/apartment/elevators/subdomain.rb @@ -7,14 +7,12 @@ module Elevators # Assumes that tenant name should match subdomain # class Subdomain < Generic + attr_writer :excluded_subdomains + def self.excluded_subdomains @excluded_subdomains ||= [] end - def self.excluded_subdomains=(arg) - @excluded_subdomains = arg - end - def parse_tenant_name(request) request_subdomain = subdomain(request.host) From 2bc9f84bc3106371278aeba716efb6b7d745615e Mon Sep 17 00:00:00 2001 From: Rui Baltazar Date: Sun, 16 Feb 2020 16:31:05 +0800 Subject: [PATCH 18/50] WIP - cleanup a few more rubocop --- .rubocop_todo.yml | 72 ------------------- lib/apartment.rb | 9 ++- lib/apartment/adapters/abstract_adapter.rb | 16 +++-- .../adapters/abstract_jdbc_adapter.rb | 2 +- .../adapters/jdbc_postgresql_adapter.rb | 5 +- lib/apartment/adapters/postgresql_adapter.rb | 3 + lib/tasks/apartment.rake | 2 +- spec/adapters/postgresql_adapter_spec.rb | 5 +- spec/adapters/sqlite3_adapter_spec.rb | 26 +++++-- spec/dummy/config/boot.rb | 2 +- spec/examples/schema_adapter_examples.rb | 12 +++- .../apartment_rake_integration_spec.rb | 2 +- spec/support/apartment_helpers.rb | 4 +- spec/support/requirements.rb | 16 ++++- spec/tenant_spec.rb | 8 ++- 15 files changed, 86 insertions(+), 98 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index f0c67aff..de6dcb74 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -621,81 +621,9 @@ Style/Proc: - 'spec/unit/elevators/host_spec.rb' - 'spec/unit/elevators/subdomain_spec.rb' -# Offense count: 2 -# Cop supports --auto-correct. -# Configuration parameters: EnforcedStyle. -# SupportedStyles: compact, exploded -Style/RaiseArgs: - Exclude: - - 'lib/apartment/adapters/jdbc_postgresql_adapter.rb' - - 'lib/apartment/adapters/postgresql_adapter.rb' - -# Offense count: 1 -# Cop supports --auto-correct. -Style/RandomWithOffset: - Exclude: - - 'spec/integration/apartment_rake_integration_spec.rb' - -# Offense count: 1 -# Cop supports --auto-correct. -Style/RedundantBegin: - Exclude: - - 'lib/apartment/adapters/abstract_adapter.rb' - -# Offense count: 3 -# Cop supports --auto-correct. -# Configuration parameters: EnforcedStyle, AllowInnerSlashes. -# SupportedStyles: slashes, percent_r, mixed -Style/RegexpLiteral: - Exclude: - - 'lib/apartment/adapters/abstract_jdbc_adapter.rb' - - 'lib/apartment/adapters/jdbc_postgresql_adapter.rb' - - 'spec/examples/schema_adapter_examples.rb' - -# Offense count: 9 -# Cop supports --auto-correct. -Style/RescueModifier: - Exclude: - - 'lib/apartment/adapters/abstract_adapter.rb' - - 'spec/adapters/sqlite3_adapter_spec.rb' - - 'spec/examples/schema_adapter_examples.rb' - - 'spec/support/apartment_helpers.rb' - - 'spec/support/requirements.rb' - - 'spec/tenant_spec.rb' - -# Offense count: 1 -# Cop supports --auto-correct. -# Configuration parameters: AllowAsExpressionSeparator. -Style/Semicolon: - Exclude: - - 'spec/adapters/postgresql_adapter_spec.rb' - -# Offense count: 1 -# Cop supports --auto-correct. -# Configuration parameters: EnforcedStyle. -# SupportedStyles: use_perl_names, use_english_names -Style/SpecialGlobalVars: - Exclude: - - 'spec/dummy/config/boot.rb' - # Offense count: 518 # Cop supports --auto-correct. # Configuration parameters: EnforcedStyle, ConsistentQuotesInMultiline. # SupportedStyles: single_quotes, double_quotes Style/StringLiterals: Enabled: false - -# Offense count: 2 -# Cop supports --auto-correct. -# Configuration parameters: MinSize. -# SupportedStyles: percent, brackets -Style/SymbolArray: - EnforcedStyle: brackets - -# Offense count: 1 -# Cop supports --auto-correct. -# Configuration parameters: IgnoredMethods. -# IgnoredMethods: respond_to, define_method -Style/SymbolProc: - Exclude: - - 'lib/tasks/apartment.rake' diff --git a/lib/apartment.rb b/lib/apartment.rb index 594dc1bd..e3a6b0cf 100644 --- a/lib/apartment.rb +++ b/lib/apartment.rb @@ -10,8 +10,13 @@ class << self extend Forwardable - ACCESSOR_METHODS = [:use_schemas, :use_sql, :seed_after_create, :prepend_environment, :append_environment, :with_multi_server_setup] - WRITER_METHODS = [:tenant_names, :database_schema_file, :excluded_models, :default_schema, :persistent_schemas, :connection_class, :tld_length, :db_migrate_tenants, :seed_data_file, :parallel_migration_threads, :pg_excluded_names] + ACCESSOR_METHODS = %i[use_schemas use_sql seed_after_create prepend_environment + append_environment with_multi_server_setup] + + WRITER_METHODS = %i[tenant_names database_schema_file excluded_models + default_schema persistent_schemas connection_class + tld_length db_migrate_tenants seed_data_file + parallel_migration_threads pg_excluded_names] attr_accessor(*ACCESSOR_METHODS) attr_writer(*WRITER_METHODS) diff --git a/lib/apartment/adapters/abstract_adapter.rb b/lib/apartment/adapters/abstract_adapter.rb index 4ad8f870..ffac0c43 100644 --- a/lib/apartment/adapters/abstract_adapter.rb +++ b/lib/apartment/adapters/abstract_adapter.rb @@ -1,5 +1,6 @@ module Apartment module Adapters + # rubocop:disable Metrics/ClassLength class AbstractAdapter include ActiveSupport::Callbacks define_callbacks :create, :switch @@ -79,13 +80,15 @@ def switch!(tenant = nil) # @param {String?} tenant to connect to # def switch(tenant = nil) - begin - previous_tenant = current - switch!(tenant) - yield + previous_tenant = current + switch!(tenant) + yield - ensure - switch!(previous_tenant) rescue reset + ensure + begin + switch!(previous_tenant) + rescue StandardError => _e + reset end end @@ -265,5 +268,6 @@ def raise_connect_error!(tenant, exception) class SeparateDbConnectionHandler < ::ActiveRecord::Base end end + # rubocop:enable Metrics/ClassLength end end diff --git a/lib/apartment/adapters/abstract_jdbc_adapter.rb b/lib/apartment/adapters/abstract_jdbc_adapter.rb index be72414e..2d20af95 100644 --- a/lib/apartment/adapters/abstract_jdbc_adapter.rb +++ b/lib/apartment/adapters/abstract_jdbc_adapter.rb @@ -7,7 +7,7 @@ class AbstractJDBCAdapter < AbstractAdapter private def multi_tenantify_with_tenant_db_name(config, tenant) - config[:url] = "#{config[:url].gsub(/(\S+)\/.+$/, '\1')}/#{environmentify(tenant)}" + config[:url] = "#{config[:url].gsub(%r{(\S+)\/.+$}, '\1')}/#{environmentify(tenant)}" end def rescue_from diff --git a/lib/apartment/adapters/jdbc_postgresql_adapter.rb b/lib/apartment/adapters/jdbc_postgresql_adapter.rb index 6b0f18cb..44726d2d 100644 --- a/lib/apartment/adapters/jdbc_postgresql_adapter.rb +++ b/lib/apartment/adapters/jdbc_postgresql_adapter.rb @@ -18,7 +18,7 @@ class JDBCPostgresqlAdapter < PostgresqlAdapter private def multi_tenantify_with_tenant_db_name(config, tenant) - config[:url] = "#{config[:url].gsub(/(\S+)\/.+$/, '\1')}/#{environmentify(tenant)}" + config[:url] = "#{config[:url].gsub(%r{(\S+)\/.+$}, '\1')}/#{environmentify(tenant)}" end def create_tenant_command(conn, tenant) @@ -37,8 +37,11 @@ class JDBCPostgresqlSchemaAdapter < PostgresqlSchemaAdapter # def connect_to_new(tenant = nil) return reset if tenant.nil? + # rubocop:disable Style/RaiseArgs raise ActiveRecord::StatementInvalid.new("Could not find schema #{tenant}") unless Apartment.connection.all_schemas.include? tenant.to_s + # rubocop:enable Style/RaiseArgs + @current = tenant.to_s Apartment.connection.schema_search_path = full_search_path diff --git a/lib/apartment/adapters/postgresql_adapter.rb b/lib/apartment/adapters/postgresql_adapter.rb index 070415a0..c0606509 100644 --- a/lib/apartment/adapters/postgresql_adapter.rb +++ b/lib/apartment/adapters/postgresql_adapter.rb @@ -63,8 +63,11 @@ def drop_command(conn, tenant) # def connect_to_new(tenant = nil) return reset if tenant.nil? + # rubocop:disable Style/RaiseArgs raise ActiveRecord::StatementInvalid.new("Could not find schema #{tenant}") unless Apartment.connection.schema_exists?(tenant.to_s) + # rubocop:enable Style/RaiseArgs + @current = tenant.to_s Apartment.connection.schema_search_path = full_search_path diff --git a/lib/tasks/apartment.rake b/lib/tasks/apartment.rake index bccd9dd2..17032d33 100644 --- a/lib/tasks/apartment.rake +++ b/lib/tasks/apartment.rake @@ -126,7 +126,7 @@ apartment_namespace = namespace :apartment do end def tenants - ENV['DB'] ? ENV['DB'].split(',').map { |s| s.strip } : Apartment.tenant_names || [] + ENV['DB'] ? ENV['DB'].split(',').map(&:strip) : Apartment.tenant_names || [] end def warn_if_tenants_empty diff --git a/spec/adapters/postgresql_adapter_spec.rb b/spec/adapters/postgresql_adapter_spec.rb index 16a85eca..9d5267a3 100644 --- a/spec/adapters/postgresql_adapter_spec.rb +++ b/spec/adapters/postgresql_adapter_spec.rb @@ -23,7 +23,10 @@ def tenant_names context "using schemas with SQL dump" do - before { Apartment.use_schemas = true; Apartment.use_sql = true } + before do + Apartment.use_schemas = true + Apartment.use_sql = true + end # Not sure why, but somehow using let(:tenant_names) memoizes for the whole example group, not just each test def tenant_names diff --git a/spec/adapters/sqlite3_adapter_spec.rb b/spec/adapters/sqlite3_adapter_spec.rb index f6efa035..92246249 100644 --- a/spec/adapters/sqlite3_adapter_spec.rb +++ b/spec/adapters/sqlite3_adapter_spec.rb @@ -35,7 +35,13 @@ def tenant_names end end - after { subject.drop db_name rescue nil } + after do + begin + subject.drop db_name + rescue StandardError => _e + nil + end + end it "should create a new database" do subject.create db_name @@ -50,7 +56,13 @@ def tenant_names Apartment.configure { |config| config.prepend_environment = config.append_environment = false } end - after { subject.drop db_name rescue nil } + after do + begin + subject.drop db_name + rescue StandardError => _e + nil + end + end it "should create a new database" do subject.create db_name @@ -68,7 +80,13 @@ def tenant_names end end - after { subject.drop db_name rescue nil } + after do + begin + subject.drop db_name + rescue StandardError => _e + nil + end + end it "should create a new database" do subject.create db_name @@ -76,8 +94,6 @@ def tenant_names expect(File.exists?("#{default_dir}/#{db_name}_#{Rails.env}.sqlite3")).to eq true end end - end - end end diff --git a/spec/dummy/config/boot.rb b/spec/dummy/config/boot.rb index 490d7917..58dd2d36 100644 --- a/spec/dummy/config/boot.rb +++ b/spec/dummy/config/boot.rb @@ -8,4 +8,4 @@ Bundler.setup end -$:.unshift File.expand_path('../../../../lib', __FILE__) \ No newline at end of file +$LOAD_PATH.unshift File.expand_path('../../../../lib', __FILE__) diff --git a/spec/examples/schema_adapter_examples.rb b/spec/examples/schema_adapter_examples.rb index 606033b9..e70df5d8 100644 --- a/spec/examples/schema_adapter_examples.rb +++ b/spec/examples/schema_adapter_examples.rb @@ -1,5 +1,6 @@ require 'spec_helper' +# rubocop:disable Metrics/BlockLength shared_examples_for "a schema based apartment adapter" do include Apartment::Spec::AdapterRequirements @@ -40,7 +41,7 @@ it 'sets the search_path correctly' do Apartment::Tenant.init - expect(User.connection.schema_search_path).to match(%r|#{default_schema}|) + expect(User.connection.schema_search_path).to match(/|#{default_schema}|/) end end @@ -110,7 +111,13 @@ expect(tenant_names).not_to include(db.to_s) end - after { subject.drop(db) rescue nil } + after do + begin + subject.drop(db) + rescue StandardError => _e + nil + end + end end end @@ -232,3 +239,4 @@ end end end +# rubocop:enable Metrics/BlockLength diff --git a/spec/integration/apartment_rake_integration_spec.rb b/spec/integration/apartment_rake_integration_spec.rb index c8e822cf..d4da2d0a 100644 --- a/spec/integration/apartment_rake_integration_spec.rb +++ b/spec/integration/apartment_rake_integration_spec.rb @@ -31,7 +31,7 @@ after { Rake.application = nil } context "with x number of databases" do - let(:x) { 1 + rand(5) } # random number of dbs to create + let(:x) { rand(1..5) } # random number of dbs to create let(:db_names) { x.times.map { Apartment::Test.next_db } } let!(:company_count) { db_names.length } diff --git a/spec/support/apartment_helpers.rb b/spec/support/apartment_helpers.rb index 9c4a43bf..47909943 100644 --- a/spec/support/apartment_helpers.rb +++ b/spec/support/apartment_helpers.rb @@ -16,7 +16,9 @@ def next_db end def drop_schema(schema) - ActiveRecord::Base.connection.execute("DROP SCHEMA IF EXISTS #{schema} CASCADE") rescue true + ActiveRecord::Base.connection.execute("DROP SCHEMA IF EXISTS #{schema} CASCADE") + rescue StandardError => _e + true end # Use this if you don't want to import schema.rb etc... but need the postgres schema to exist diff --git a/spec/support/requirements.rb b/spec/support/requirements.rb index dc856c9e..f507ab26 100644 --- a/spec/support/requirements.rb +++ b/spec/support/requirements.rb @@ -19,9 +19,19 @@ module AdapterRequirements # Reset before dropping (can't drop a db you're connected to) subject.reset - # sometimes we manually drop these schemas in testing, don't care if we can't drop, hence rescue - subject.drop(db1) rescue true - subject.drop(db2) rescue true + # sometimes we manually drop these schemas in testing, don't care if + # we can't drop, hence rescue + begin + subject.drop(db1) + rescue StandardError => _e + true + end + + begin + subject.drop(db2) + rescue StandardError => _e + true + end end end diff --git a/spec/tenant_spec.rb b/spec/tenant_spec.rb index 17dfe801..92cdc2a2 100644 --- a/spec/tenant_spec.rb +++ b/spec/tenant_spec.rb @@ -42,7 +42,13 @@ subject.reload!(config) end - after { subject.drop "db_with_prefix" rescue nil } + after do + begin + subject.drop "db_with_prefix" + rescue StandardError => _e + nil + end + end it "should create a new database" do subject.create "db_with_prefix" From f6ec07527bc3e9d47ceef66dd51e32655438cd86 Mon Sep 17 00:00:00 2001 From: Rui Baltazar Date: Sun, 16 Feb 2020 16:44:07 +0800 Subject: [PATCH 19/50] WIP - fixed a couple more of rubocops --- .rubocop.yml | 4 ++ .rubocop_todo.yml | 45 ------------------- README.md | 2 +- .../adapters/jdbc_postgresql_adapter.rb | 6 ++- lib/apartment/adapters/mysql2_adapter.rb | 6 ++- lib/apartment/adapters/postgresql_adapter.rb | 12 +++-- lib/apartment/console.rb | 2 +- lib/apartment/elevators/host_hash.rb | 2 +- spec/support/apartment_helpers.rb | 2 + spec/unit/elevators/domain_spec.rb | 2 +- spec/unit/elevators/generic_spec.rb | 12 ++--- spec/unit/elevators/host_hash_spec.rb | 2 +- spec/unit/elevators/host_spec.rb | 2 +- spec/unit/elevators/subdomain_spec.rb | 2 +- 14 files changed, 36 insertions(+), 65 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index 50fd0197..e98d38eb 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -7,3 +7,7 @@ inherit_gem: Style/WordArray: Exclude: - spec/schemas/**/*.rb + +Style/NumericLiterals: + Exclude: + - spec/schemas/**/*.rb diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index de6dcb74..1b12cea7 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -541,14 +541,6 @@ Style/MixinUsage: Exclude: - '.pryrc' -# Offense count: 1 -# Cop supports --auto-correct. -# Configuration parameters: EnforcedStyle, Autocorrect. -# SupportedStyles: module_function, extend_self -Style/ModuleFunction: - Exclude: - - 'spec/support/apartment_helpers.rb' - # Offense count: 5 # Cop supports --auto-correct. Style/MultilineIfModifier: @@ -557,12 +549,6 @@ Style/MultilineIfModifier: - 'lib/apartment/elevators/host_hash.rb' - 'spec/support/requirements.rb' -# Offense count: 2 -Style/MultilineTernaryOperator: - Exclude: - - 'lib/apartment/adapters/jdbc_postgresql_adapter.rb' - - 'lib/apartment/adapters/mysql2_adapter.rb' - # Offense count: 7 # Cop supports --auto-correct. # Configuration parameters: EnforcedStyle. @@ -575,18 +561,6 @@ Style/MutableConstant: - 'lib/apartment/version.rb' - 'spec/dummy_engine/lib/dummy_engine/version.rb' -# Offense count: 3 -# Cop supports --auto-correct. -# Configuration parameters: Strict. -Style/NumericLiterals: - MinDigits: 15 - -# Offense count: 2 -# Cop supports --auto-correct. -Style/ParallelAssignment: - Exclude: - - 'lib/apartment/adapters/postgresql_adapter.rb' - # Offense count: 29 # Cop supports --auto-correct. # Configuration parameters: PreferredDelimiters. @@ -602,25 +576,6 @@ Style/PercentLiteralDelimiters: - 'spec/unit/elevators/host_spec.rb' - 'spec/unit/elevators/subdomain_spec.rb' -# Offense count: 1 -# Cop supports --auto-correct. -# Configuration parameters: EnforcedStyle. -# SupportedStyles: short, verbose -Style/PreferredHashMethods: - Exclude: - - 'lib/apartment/elevators/host_hash.rb' - -# Offense count: 13 -# Cop supports --auto-correct. -Style/Proc: - Exclude: - - 'lib/apartment/console.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' - # Offense count: 518 # Cop supports --auto-correct. # Configuration parameters: EnforcedStyle, ConsistentQuotesInMultiline. diff --git a/README.md b/README.md index cb6c82b9..31e8ca13 100644 --- a/README.md +++ b/README.md @@ -229,7 +229,7 @@ A Generic Elevator exists that allows you to pass a `Proc` (or anything that res module MyApplication class Application < Rails::Application # Obviously not a contrived example - config.middleware.use Apartment::Elevators::Generic, Proc.new { |request| request.host.reverse } + config.middleware.use Apartment::Elevators::Generic, proc { |request| request.host.reverse } end end ``` diff --git a/lib/apartment/adapters/jdbc_postgresql_adapter.rb b/lib/apartment/adapters/jdbc_postgresql_adapter.rb index 44726d2d..3574feb8 100644 --- a/lib/apartment/adapters/jdbc_postgresql_adapter.rb +++ b/lib/apartment/adapters/jdbc_postgresql_adapter.rb @@ -4,9 +4,11 @@ module Apartment module Tenant def self.jdbc_postgresql_adapter(config) - Apartment.use_schemas ? - Adapters::JDBCPostgresqlSchemaAdapter.new(config) : + if Apartment.use_schemas + Adapters::JDBCPostgresqlSchemaAdapter.new(config) + else Adapters::JDBCPostgresqlAdapter.new(config) + end end end diff --git a/lib/apartment/adapters/mysql2_adapter.rb b/lib/apartment/adapters/mysql2_adapter.rb index 780710d3..c3c05e9d 100644 --- a/lib/apartment/adapters/mysql2_adapter.rb +++ b/lib/apartment/adapters/mysql2_adapter.rb @@ -4,9 +4,11 @@ module Apartment module Tenant def self.mysql2_adapter(config) - Apartment.use_schemas ? - Adapters::Mysql2SchemaAdapter.new(config) : + if Apartment.use_schemas + Adapters::Mysql2SchemaAdapter.new(config) + else Adapters::Mysql2Adapter.new(config) + end end end diff --git a/lib/apartment/adapters/postgresql_adapter.rb b/lib/apartment/adapters/postgresql_adapter.rb index c0606509..fc6b60b7 100644 --- a/lib/apartment/adapters/postgresql_adapter.rb +++ b/lib/apartment/adapters/postgresql_adapter.rb @@ -74,7 +74,7 @@ def connect_to_new(tenant = nil) # When the PostgreSQL version is < 9.3, # there is a issue for prepared statement with changing search_path. # https://www.postgresql.org/docs/9.3/static/sql-prepare.html - if postgresql_version < 90300 + if postgresql_version < 90_300 Apartment.connection.clear_cache! end @@ -179,7 +179,10 @@ def pg_dump_schema_migrations_data # Temporary set Postgresql related environment variables if there are in @config # def with_pg_env(&block) - pghost, pgport, pguser, pgpassword = ENV['PGHOST'], ENV['PGPORT'], ENV['PGUSER'], ENV['PGPASSWORD'] + pghost = ENV['PGHOST'] + pgport = ENV['PGPORT'] + pguser = ENV['PGUSER'] + pgpassword = ENV['PGPASSWORD'] ENV['PGHOST'] = @config[:host] if @config[:host] ENV['PGPORT'] = @config[:port].to_s if @config[:port] @@ -188,7 +191,10 @@ def with_pg_env(&block) block.call ensure - ENV['PGHOST'], ENV['PGPORT'], ENV['PGUSER'], ENV['PGPASSWORD'] = pghost, pgport, pguser, pgpassword + ENV['PGHOST'] = pghost + ENV['PGPORT'] = pgport + ENV['PGUSER'] = pguser + ENV['PGPASSWORD'] = pgpassword end # Remove "SET search_path ..." line from SQL dump and prepend search_path set to current tenant diff --git a/lib/apartment/console.rb b/lib/apartment/console.rb index 66cefda2..8393be85 100644 --- a/lib/apartment/console.rb +++ b/lib/apartment/console.rb @@ -5,7 +5,7 @@ def reload!(print=true) puts "Reloading..." if print # This triggers the to_prepare callbacks - ActionDispatch::Callbacks.new(Proc.new{}).call({}) + ActionDispatch::Callbacks.new(proc{}).call({}) # Manually init Apartment again once classes are reloaded Apartment::Tenant.init true diff --git a/lib/apartment/elevators/host_hash.rb b/lib/apartment/elevators/host_hash.rb index 7a0454f1..e3e0e0c1 100644 --- a/lib/apartment/elevators/host_hash.rb +++ b/lib/apartment/elevators/host_hash.rb @@ -13,7 +13,7 @@ def initialize(app, hash = {}, processor = nil) def parse_tenant_name(request) raise TenantNotFound, - "Cannot find tenant for host #{request.host}" unless @hash.has_key?(request.host) + "Cannot find tenant for host #{request.host}" unless @hash.key?(request.host) @hash[request.host] end diff --git a/spec/support/apartment_helpers.rb b/spec/support/apartment_helpers.rb index 47909943..dafbd9d2 100644 --- a/spec/support/apartment_helpers.rb +++ b/spec/support/apartment_helpers.rb @@ -1,7 +1,9 @@ module Apartment module Test + # rubocop:disable Style/ModuleFunction extend self + # rubocop:enable Style/ModuleFunction def reset Apartment.excluded_models = nil diff --git a/spec/unit/elevators/domain_spec.rb b/spec/unit/elevators/domain_spec.rb index 713cd88c..14090c41 100644 --- a/spec/unit/elevators/domain_spec.rb +++ b/spec/unit/elevators/domain_spec.rb @@ -3,7 +3,7 @@ describe Apartment::Elevators::Domain do - subject(:elevator) { described_class.new(Proc.new{}) } + subject(:elevator) { described_class.new(proc) } describe "#parse_tenant_name" do it "parses the host for a domain name" do diff --git a/spec/unit/elevators/generic_spec.rb b/spec/unit/elevators/generic_spec.rb index bca84dc9..f12f8fb4 100644 --- a/spec/unit/elevators/generic_spec.rb +++ b/spec/unit/elevators/generic_spec.rb @@ -9,11 +9,11 @@ def parse_tenant_name(*) end end - subject(:elevator) { described_class.new(Proc.new{}) } + subject(:elevator) { described_class.new(proc) } describe "#call" do it "calls the processor if given" do - elevator = described_class.new(Proc.new{}, Proc.new {'tenant1'}) + elevator = described_class.new(proc{}, proc {'tenant1'}) expect(Apartment::Tenant).to receive(:switch).with('tenant1') @@ -27,7 +27,7 @@ def parse_tenant_name(*) end it "switches to the parsed db_name" do - elevator = MyElevator.new(Proc.new{}) + elevator = MyElevator.new(proc{}) expect(Apartment::Tenant).to receive(:switch).with('tenant2') @@ -35,15 +35,15 @@ def parse_tenant_name(*) end it "calls the block implementation of `switch`" do - elevator = MyElevator.new(Proc.new{}, Proc.new {'tenant2'}) + elevator = MyElevator.new(proc{}, proc {'tenant2'}) expect(Apartment::Tenant).to receive(:switch).with('tenant2').and_yield elevator.call('HTTP_HOST' => 'foo.bar.com') end it "does not call `switch` if no database given" do - app = Proc.new{} - elevator = MyElevator.new(app, Proc.new{}) + app = proc{} + elevator = MyElevator.new(app, proc{}) expect(Apartment::Tenant).not_to receive(:switch) expect(app).to receive :call diff --git a/spec/unit/elevators/host_hash_spec.rb b/spec/unit/elevators/host_hash_spec.rb index 039912ec..af1aa5be 100644 --- a/spec/unit/elevators/host_hash_spec.rb +++ b/spec/unit/elevators/host_hash_spec.rb @@ -3,7 +3,7 @@ describe Apartment::Elevators::HostHash do - subject(:elevator) { Apartment::Elevators::HostHash.new(Proc.new{}, 'example.com' => 'example_tenant') } + subject(:elevator) { Apartment::Elevators::HostHash.new(proc{}, 'example.com' => 'example_tenant') } describe "#parse_tenant_name" do it "parses the host for a domain name" do diff --git a/spec/unit/elevators/host_spec.rb b/spec/unit/elevators/host_spec.rb index c8daaa37..b77fbc49 100644 --- a/spec/unit/elevators/host_spec.rb +++ b/spec/unit/elevators/host_spec.rb @@ -3,7 +3,7 @@ describe Apartment::Elevators::Host do - subject(:elevator) { described_class.new(Proc.new{}) } + subject(:elevator) { described_class.new(proc) } describe "#parse_tenant_name" do diff --git a/spec/unit/elevators/subdomain_spec.rb b/spec/unit/elevators/subdomain_spec.rb index 71feb6c7..ac4e74f3 100644 --- a/spec/unit/elevators/subdomain_spec.rb +++ b/spec/unit/elevators/subdomain_spec.rb @@ -3,7 +3,7 @@ describe Apartment::Elevators::Subdomain do - subject(:elevator) { described_class.new(Proc.new{}) } + subject(:elevator) { described_class.new(proc) } describe "#parse_tenant_name" do context "assuming one tld" do From 8d127c5e691d8dc7dc21eca7c7d1bfd9374d784b Mon Sep 17 00:00:00 2001 From: Rui Baltazar Date: Sun, 16 Feb 2020 16:47:29 +0800 Subject: [PATCH 20/50] WIP - --- .rubocop.yml | 4 +++ .rubocop_todo.yml | 44 ----------------------- Appraisals | 2 -- lib/apartment.rb | 1 + lib/apartment/adapters/sqlite3_adapter.rb | 6 ++-- lib/apartment/elevators/host.rb | 1 + lib/apartment/elevators/host_hash.rb | 2 +- lib/apartment/elevators/subdomain.rb | 8 ++--- 8 files changed, 14 insertions(+), 54 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index e98d38eb..e8cc53e7 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -11,3 +11,7 @@ Style/WordArray: Style/NumericLiterals: Exclude: - spec/schemas/**/*.rb + +Layout/EmptyLineAfterMagicComment: + Exclude: + - spec/schemas/**/*.rb diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 1b12cea7..0ad25d20 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -19,50 +19,6 @@ Bundler/OrderedGems: - 'gemfiles/rails_6_0.gemfile' - 'gemfiles/rails_master.gemfile' -# Offense count: 4 -# Cop supports --auto-correct. -# Configuration parameters: EnforcedStyle, IndentationWidth. -# SupportedStyles: with_first_argument, with_fixed_indentation -Layout/ArgumentAlignment: - Exclude: - - 'lib/apartment/adapters/sqlite3_adapter.rb' - - 'lib/apartment/elevators/host_hash.rb' - -# Offense count: 1 -# Cop supports --auto-correct. -# Configuration parameters: EnforcedStyleAlignWith, AutoCorrect, Severity. -# SupportedStylesAlignWith: start_of_line, def -Layout/DefEndAlignment: - Exclude: - - 'lib/apartment/adapters/abstract_adapter.rb' - -# Offense count: 1 -# Cop supports --auto-correct. -Layout/ElseAlignment: - Exclude: - - 'lib/apartment/elevators/subdomain.rb' - -# Offense count: 2 -# Cop supports --auto-correct. -Layout/EmptyLineAfterGuardClause: - Exclude: - - 'lib/apartment.rb' - - 'lib/apartment/elevators/host.rb' - -# Offense count: 3 -# Cop supports --auto-correct. -Layout/EmptyLineAfterMagicComment: - Exclude: - - 'spec/schemas/v1.rb' - - 'spec/schemas/v2.rb' - - 'spec/schemas/v3.rb' - -# Offense count: 2 -# Cop supports --auto-correct. -Layout/EmptyLines: - Exclude: - - 'Appraisals' - # Offense count: 54 # Cop supports --auto-correct. # Configuration parameters: EnforcedStyle. diff --git a/Appraisals b/Appraisals index f6904095..160e0ada 100644 --- a/Appraisals +++ b/Appraisals @@ -44,7 +44,6 @@ appraise "rails-5-2" do end end - appraise "rails-6-0" do gem "rails", "~> 6.0.0.rc1" platforms :ruby do @@ -57,7 +56,6 @@ appraise "rails-6-0" do end end - appraise "rails-master" do gem "rails", git: 'https://github.com/rails/rails.git' platforms :ruby do diff --git a/lib/apartment.rb b/lib/apartment.rb index e3a6b0cf..eddaa242 100644 --- a/lib/apartment.rb +++ b/lib/apartment.rb @@ -94,6 +94,7 @@ def reset def extract_tenant_config return {} unless @tenant_names + values = @tenant_names.respond_to?(:call) ? @tenant_names.call : @tenant_names unless values.is_a? Hash values = values.each_with_object({}) do |tenant, hash| diff --git a/lib/apartment/adapters/sqlite3_adapter.rb b/lib/apartment/adapters/sqlite3_adapter.rb index e16dd544..f4e169a6 100644 --- a/lib/apartment/adapters/sqlite3_adapter.rb +++ b/lib/apartment/adapters/sqlite3_adapter.rb @@ -17,7 +17,7 @@ def initialize(config) def drop(tenant) raise TenantNotFound, - "The tenant #{environmentify(tenant)} cannot be found." unless File.exists?(database_file(tenant)) + "The tenant #{environmentify(tenant)} cannot be found." unless File.exists?(database_file(tenant)) File.delete(database_file(tenant)) end @@ -30,14 +30,14 @@ def current def connect_to_new(tenant) raise TenantNotFound, - "The tenant #{environmentify(tenant)} cannot be found." unless File.exists?(database_file(tenant)) + "The tenant #{environmentify(tenant)} cannot be found." unless File.exists?(database_file(tenant)) super database_file(tenant) end def create_tenant(tenant) raise TenantExists, - "The tenant #{environmentify(tenant)} already exists." if File.exists?(database_file(tenant)) + "The tenant #{environmentify(tenant)} already exists." if File.exists?(database_file(tenant)) begin f = File.new(database_file(tenant), File::CREAT) diff --git a/lib/apartment/elevators/host.rb b/lib/apartment/elevators/host.rb index c62d47e2..3a9f8da6 100644 --- a/lib/apartment/elevators/host.rb +++ b/lib/apartment/elevators/host.rb @@ -20,6 +20,7 @@ def self.ignored_first_subdomains def parse_tenant_name(request) return nil if request.host.blank? + parts = request.host.split('.') self.class.ignored_first_subdomains.include?(parts[0]) ? parts.drop(1).join('.') : request.host end diff --git a/lib/apartment/elevators/host_hash.rb b/lib/apartment/elevators/host_hash.rb index e3e0e0c1..7838d671 100644 --- a/lib/apartment/elevators/host_hash.rb +++ b/lib/apartment/elevators/host_hash.rb @@ -13,7 +13,7 @@ def initialize(app, hash = {}, processor = nil) def parse_tenant_name(request) raise TenantNotFound, - "Cannot find tenant for host #{request.host}" unless @hash.key?(request.host) + "Cannot find tenant for host #{request.host}" unless @hash.key?(request.host) @hash[request.host] end diff --git a/lib/apartment/elevators/subdomain.rb b/lib/apartment/elevators/subdomain.rb index 8380f758..5716a168 100644 --- a/lib/apartment/elevators/subdomain.rb +++ b/lib/apartment/elevators/subdomain.rb @@ -19,10 +19,10 @@ def parse_tenant_name(request) # If the domain acquired is set to be excluded, set the tenant to whatever is currently # next in line in the schema search path. tenant = if self.class.excluded_subdomains.include?(request_subdomain) - nil - else - request_subdomain - end + nil + else + request_subdomain + end tenant.presence end From a22737d59de2a2ccd8e8f67a91df323822e87600 Mon Sep 17 00:00:00 2001 From: Rui Baltazar Date: Sun, 16 Feb 2020 16:50:22 +0800 Subject: [PATCH 21/50] WIP - removed extra blank lines --- .rubocop_todo.yml | 36 ------------------- lib/apartment.rb | 1 - lib/apartment/adapters/abstract_adapter.rb | 2 -- .../adapters/abstract_jdbc_adapter.rb | 1 - lib/apartment/adapters/jdbc_mysql_adapter.rb | 1 - .../adapters/jdbc_postgresql_adapter.rb | 3 -- lib/apartment/adapters/mysql2_adapter.rb | 2 -- lib/apartment/adapters/postgresql_adapter.rb | 5 --- lib/apartment/elevators/domain.rb | 1 - lib/apartment/elevators/first_subdomain.rb | 1 - lib/apartment/elevators/generic.rb | 1 - lib/apartment/railtie.rb | 1 - lib/apartment/reloader.rb | 1 - lib/apartment/tasks/enhancements.rb | 4 --- .../apartment/install/install_generator.rb | 1 - 15 files changed, 61 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 0ad25d20..0ff1652b 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -26,42 +26,6 @@ Bundler/OrderedGems: Layout/EmptyLinesAroundBlockBody: Enabled: false -# Offense count: 18 -# Cop supports --auto-correct. -# Configuration parameters: EnforcedStyle. -# SupportedStyles: empty_lines, empty_lines_except_namespace, empty_lines_special, no_empty_lines, beginning_only, ending_only -Layout/EmptyLinesAroundClassBody: - Exclude: - - 'lib/apartment.rb' - - 'lib/apartment/adapters/abstract_jdbc_adapter.rb' - - 'lib/apartment/adapters/jdbc_mysql_adapter.rb' - - 'lib/apartment/adapters/jdbc_postgresql_adapter.rb' - - 'lib/apartment/adapters/mysql2_adapter.rb' - - 'lib/apartment/adapters/postgresql_adapter.rb' - - 'lib/apartment/elevators/domain.rb' - - 'lib/apartment/elevators/first_subdomain.rb' - - 'lib/apartment/elevators/generic.rb' - - 'lib/apartment/railtie.rb' - - 'lib/apartment/reloader.rb' - - 'lib/apartment/tasks/enhancements.rb' - - 'lib/generators/apartment/install/install_generator.rb' - -# Offense count: 5 -# Cop supports --auto-correct. -Layout/EmptyLinesAroundExceptionHandlingKeywords: - Exclude: - - 'lib/apartment/adapters/abstract_adapter.rb' - - 'lib/apartment/adapters/jdbc_postgresql_adapter.rb' - - 'lib/apartment/adapters/mysql2_adapter.rb' - - 'lib/apartment/adapters/postgresql_adapter.rb' - -# Offense count: 2 -# Cop supports --auto-correct. -Layout/EmptyLinesAroundMethodBody: - Exclude: - - 'lib/apartment/adapters/postgresql_adapter.rb' - - 'lib/apartment/tasks/enhancements.rb' - # Offense count: 15 # Cop supports --auto-correct. # Configuration parameters: EnforcedStyle. diff --git a/lib/apartment.rb b/lib/apartment.rb index eddaa242..91839452 100644 --- a/lib/apartment.rb +++ b/lib/apartment.rb @@ -7,7 +7,6 @@ module Apartment class << self - extend Forwardable ACCESSOR_METHODS = %i[use_schemas use_sql seed_after_create prepend_environment diff --git a/lib/apartment/adapters/abstract_adapter.rb b/lib/apartment/adapters/abstract_adapter.rb index ffac0c43..ff72bf06 100644 --- a/lib/apartment/adapters/abstract_adapter.rb +++ b/lib/apartment/adapters/abstract_adapter.rb @@ -56,7 +56,6 @@ def drop(tenant) with_neutral_connection(tenant) do |conn| drop_command(conn, tenant) end - rescue *rescuable_exceptions => exception raise_drop_tenant_error!(tenant, exception) end @@ -83,7 +82,6 @@ def switch(tenant = nil) previous_tenant = current switch!(tenant) yield - ensure begin switch!(previous_tenant) diff --git a/lib/apartment/adapters/abstract_jdbc_adapter.rb b/lib/apartment/adapters/abstract_jdbc_adapter.rb index 2d20af95..0f7da013 100644 --- a/lib/apartment/adapters/abstract_jdbc_adapter.rb +++ b/lib/apartment/adapters/abstract_jdbc_adapter.rb @@ -3,7 +3,6 @@ module Apartment module Adapters class AbstractJDBCAdapter < AbstractAdapter - private def multi_tenantify_with_tenant_db_name(config, tenant) diff --git a/lib/apartment/adapters/jdbc_mysql_adapter.rb b/lib/apartment/adapters/jdbc_mysql_adapter.rb index 53fc7dea..49d04c94 100644 --- a/lib/apartment/adapters/jdbc_mysql_adapter.rb +++ b/lib/apartment/adapters/jdbc_mysql_adapter.rb @@ -10,7 +10,6 @@ def self.jdbc_mysql_adapter(config) module Adapters class JDBCMysqlAdapter < AbstractJDBCAdapter - def reset_on_connection_exception? true end diff --git a/lib/apartment/adapters/jdbc_postgresql_adapter.rb b/lib/apartment/adapters/jdbc_postgresql_adapter.rb index 3574feb8..378f4a14 100644 --- a/lib/apartment/adapters/jdbc_postgresql_adapter.rb +++ b/lib/apartment/adapters/jdbc_postgresql_adapter.rb @@ -16,7 +16,6 @@ module Adapters # Default adapter when not using Postgresql Schemas class JDBCPostgresqlAdapter < PostgresqlAdapter - private def multi_tenantify_with_tenant_db_name(config, tenant) @@ -34,7 +33,6 @@ def rescue_from # Separate Adapter for Postgresql when using schemas class JDBCPostgresqlSchemaAdapter < PostgresqlSchemaAdapter - # Set schema search path to new schema # def connect_to_new(tenant = nil) @@ -46,7 +44,6 @@ def connect_to_new(tenant = nil) @current = 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}" end diff --git a/lib/apartment/adapters/mysql2_adapter.rb b/lib/apartment/adapters/mysql2_adapter.rb index c3c05e9d..33365cab 100644 --- a/lib/apartment/adapters/mysql2_adapter.rb +++ b/lib/apartment/adapters/mysql2_adapter.rb @@ -14,7 +14,6 @@ def self.mysql2_adapter(config) module Adapters class Mysql2Adapter < AbstractAdapter - def initialize(config) super @@ -50,7 +49,6 @@ def connect_to_new(tenant) return reset if tenant.nil? Apartment.connection.execute "use `#{environmentify(tenant)}`" - rescue ActiveRecord::StatementInvalid => exception Apartment::Tenant.reset raise_connect_error!(tenant, exception) diff --git a/lib/apartment/adapters/postgresql_adapter.rb b/lib/apartment/adapters/postgresql_adapter.rb index fc6b60b7..aefde151 100644 --- a/lib/apartment/adapters/postgresql_adapter.rb +++ b/lib/apartment/adapters/postgresql_adapter.rb @@ -14,7 +14,6 @@ def self.postgresql_adapter(config) module Adapters # Default adapter when not using Postgresql Schemas class PostgresqlAdapter < AbstractAdapter - private def rescue_from @@ -24,7 +23,6 @@ def rescue_from # Separate Adapter for Postgresql when using schemas class PostgresqlSchemaAdapter < AbstractAdapter - def initialize(config) super @@ -77,7 +75,6 @@ def connect_to_new(tenant = nil) if postgresql_version < 90_300 Apartment.connection.clear_cache! end - rescue *rescuable_exceptions raise TenantNotFound, "One of the following schema(s) is invalid: \"#{tenant}\" #{full_search_path}" end @@ -107,7 +104,6 @@ def postgresql_version # Another Adapter for Postgresql when using schemas and SQL class PostgresqlSchemaFromSqlAdapter < PostgresqlSchemaAdapter - PSQL_DUMP_BLACKLISTED_STATEMENTS= [ /SET search_path/i, # overridden later /SET lock_timeout/i, # new in postgresql 9.3 @@ -156,7 +152,6 @@ def copy_schema_migrations # @return {String} raw SQL contaning only postgres schema dump # def pg_dump_schema - # Skip excluded tables? :/ # excluded_tables = # collect_table_names(Apartment.excluded_models) diff --git a/lib/apartment/elevators/domain.rb b/lib/apartment/elevators/domain.rb index 5b81bb8a..f52f4b40 100644 --- a/lib/apartment/elevators/domain.rb +++ b/lib/apartment/elevators/domain.rb @@ -11,7 +11,6 @@ module Elevators # # class Domain < Generic - def parse_tenant_name(request) return nil if request.host.blank? diff --git a/lib/apartment/elevators/first_subdomain.rb b/lib/apartment/elevators/first_subdomain.rb index 71d0d8d1..052d28c9 100644 --- a/lib/apartment/elevators/first_subdomain.rb +++ b/lib/apartment/elevators/first_subdomain.rb @@ -8,7 +8,6 @@ module Elevators # - example1.domain.com => example1 # - example2.something.domain.com => example2 class FirstSubdomain < Subdomain - def parse_tenant_name(request) super.split('.')[0] unless super.nil? end diff --git a/lib/apartment/elevators/generic.rb b/lib/apartment/elevators/generic.rb index 21349378..716db5dc 100644 --- a/lib/apartment/elevators/generic.rb +++ b/lib/apartment/elevators/generic.rb @@ -6,7 +6,6 @@ module Elevators # Provides a rack based tenant switching solution based on request # class Generic - def initialize(app, processor = nil) @app = app @processor = processor || method(:parse_tenant_name) diff --git a/lib/apartment/railtie.rb b/lib/apartment/railtie.rb index 6a18833f..beba954c 100644 --- a/lib/apartment/railtie.rb +++ b/lib/apartment/railtie.rb @@ -4,7 +4,6 @@ module Apartment class Railtie < Rails::Railtie - # # Set up our default config options # Do this before the app initializers run so we don't override custom settings diff --git a/lib/apartment/reloader.rb b/lib/apartment/reloader.rb index fd315371..164c838e 100644 --- a/lib/apartment/reloader.rb +++ b/lib/apartment/reloader.rb @@ -1,6 +1,5 @@ module Apartment class Reloader - # Middleware used in development to init Apartment for each request # Necessary due to code reload (annoying). When models are reloaded, they no longer have the proper table_name # That is prepended with the schema (if using postgresql schemas) diff --git a/lib/apartment/tasks/enhancements.rb b/lib/apartment/tasks/enhancements.rb index ce93e58a..dff7099b 100644 --- a/lib/apartment/tasks/enhancements.rb +++ b/lib/apartment/tasks/enhancements.rb @@ -3,7 +3,6 @@ module Apartment class RakeTaskEnhancer - module TASKS ENHANCE_BEFORE = %w(db:drop) ENHANCE_AFTER = %w(db:migrate db:rollback db:migrate:up db:migrate:down db:migrate:redo db:seed) @@ -28,7 +27,6 @@ def enhance! task = Rake::Task[name] enhance_after_task(task) end - end def should_enhance? @@ -48,9 +46,7 @@ def enhance_after_task(task) def inserted_task_name(task) task.name.sub(/db:/, 'apartment:') end - end - end end diff --git a/lib/generators/apartment/install/install_generator.rb b/lib/generators/apartment/install/install_generator.rb index eb7cbe66..079a732b 100755 --- a/lib/generators/apartment/install/install_generator.rb +++ b/lib/generators/apartment/install/install_generator.rb @@ -5,6 +5,5 @@ class InstallGenerator < Rails::Generators::Base def copy_files template "apartment.rb", File.join("config", "initializers", "apartment.rb") end - end end From 7f68591d41114192f93e877c928714c96f6152bb Mon Sep 17 00:00:00 2001 From: Rui Baltazar Date: Sun, 16 Feb 2020 16:51:52 +0800 Subject: [PATCH 22/50] WIP - fixed EmptyLinesAroundBlockBody --- .rubocop_todo.yml | 7 ------- lib/generators/apartment/install/templates/apartment.rb | 1 - lib/tasks/apartment.rake | 1 - spec/adapters/jdbc_mysql_adapter_spec.rb | 1 - spec/adapters/jdbc_postgresql_adapter_spec.rb | 4 ---- spec/adapters/postgresql_adapter_spec.rb | 3 --- spec/dummy_engine/config/initializers/apartment.rb | 1 - .../generic_adapter_custom_configuration_example.rb | 4 ---- spec/examples/generic_adapter_examples.rb | 1 - spec/examples/schema_adapter_examples.rb | 5 ----- spec/integration/use_within_an_engine_spec.rb | 2 -- spec/schemas/v1.rb | 1 - spec/schemas/v2.rb | 2 -- spec/schemas/v3.rb | 2 -- spec/spec_helper.rb | 1 - spec/support/setup.rb | 1 - spec/tasks/apartment_rake_spec.rb | 7 ------- spec/tenant_spec.rb | 4 ---- spec/unit/config_spec.rb | 3 --- spec/unit/elevators/domain_spec.rb | 1 - spec/unit/elevators/generic_spec.rb | 1 - spec/unit/elevators/host_hash_spec.rb | 1 - spec/unit/elevators/host_spec.rb | 2 -- spec/unit/elevators/subdomain_spec.rb | 1 - spec/unit/migrator_spec.rb | 1 - spec/unit/reloader_spec.rb | 2 -- 26 files changed, 60 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 0ff1652b..cdd3a8ae 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -19,13 +19,6 @@ Bundler/OrderedGems: - 'gemfiles/rails_6_0.gemfile' - 'gemfiles/rails_master.gemfile' -# Offense count: 54 -# Cop supports --auto-correct. -# Configuration parameters: EnforcedStyle. -# SupportedStyles: empty_lines, no_empty_lines -Layout/EmptyLinesAroundBlockBody: - Enabled: false - # Offense count: 15 # Cop supports --auto-correct. # Configuration parameters: EnforcedStyle. diff --git a/lib/generators/apartment/install/templates/apartment.rb b/lib/generators/apartment/install/templates/apartment.rb index 6c7c52fb..251033bf 100644 --- a/lib/generators/apartment/install/templates/apartment.rb +++ b/lib/generators/apartment/install/templates/apartment.rb @@ -12,7 +12,6 @@ # Apartment Configuration # Apartment.configure do |config| - # Add any models that you do not want to be multi-tenanted, but remain in the global (public) namespace. # A typical example would be a Customer or Tenant model that stores each Tenant's information. # diff --git a/lib/tasks/apartment.rake b/lib/tasks/apartment.rake index 17032d33..7931160f 100644 --- a/lib/tasks/apartment.rake +++ b/lib/tasks/apartment.rake @@ -2,7 +2,6 @@ require 'apartment/migrator' require 'parallel' apartment_namespace = namespace :apartment do - desc "Create all tenants" task :create do tenants.each do |tenant| diff --git a/spec/adapters/jdbc_mysql_adapter_spec.rb b/spec/adapters/jdbc_mysql_adapter_spec.rb index 1fee3e14..1fb5d433 100644 --- a/spec/adapters/jdbc_mysql_adapter_spec.rb +++ b/spec/adapters/jdbc_mysql_adapter_spec.rb @@ -4,7 +4,6 @@ require 'apartment/adapters/jdbc_mysql_adapter' describe Apartment::Adapters::JDBCMysqlAdapter, database: :mysql do - subject { Apartment::Tenant.jdbc_mysql_adapter config.symbolize_keys } def tenant_names diff --git a/spec/adapters/jdbc_postgresql_adapter_spec.rb b/spec/adapters/jdbc_postgresql_adapter_spec.rb index 28cf135f..d595a7d9 100644 --- a/spec/adapters/jdbc_postgresql_adapter_spec.rb +++ b/spec/adapters/jdbc_postgresql_adapter_spec.rb @@ -4,11 +4,9 @@ require 'apartment/adapters/jdbc_postgresql_adapter' describe Apartment::Adapters::JDBCPostgresqlAdapter, database: :postgresql do - subject { Apartment::Tenant.jdbc_postgresql_adapter config.symbolize_keys } context "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 @@ -23,7 +21,6 @@ def tenant_names end context "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 @@ -35,7 +32,6 @@ def tenant_names it_should_behave_like "a generic apartment adapter" it_should_behave_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 9d5267a3..7802a7bd 100644 --- a/spec/adapters/postgresql_adapter_spec.rb +++ b/spec/adapters/postgresql_adapter_spec.rb @@ -7,7 +7,6 @@ subject { Apartment::Tenant.postgresql_adapter config } context "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 @@ -22,7 +21,6 @@ def tenant_names end context "using schemas with SQL dump" do - before do Apartment.use_schemas = true Apartment.use_sql = true @@ -46,7 +44,6 @@ def tenant_names end context "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/dummy_engine/config/initializers/apartment.rb b/spec/dummy_engine/config/initializers/apartment.rb index 611f5322..13d1d152 100644 --- a/spec/dummy_engine/config/initializers/apartment.rb +++ b/spec/dummy_engine/config/initializers/apartment.rb @@ -8,7 +8,6 @@ # Apartment Configuration # Apartment.configure do |config| - # These models will not be multi-tenanted, # but remain in the global (public) namespace # diff --git a/spec/examples/generic_adapter_custom_configuration_example.rb b/spec/examples/generic_adapter_custom_configuration_example.rb index a9f053a3..0f64c64a 100644 --- a/spec/examples/generic_adapter_custom_configuration_example.rb +++ b/spec/examples/generic_adapter_custom_configuration_example.rb @@ -1,7 +1,6 @@ require 'spec_helper' shared_examples_for "a generic apartment adapter able to handle custom configuration" do - let(:custom_tenant_name) { 'test_tenantwwww' } let(:db) { |example| example.metadata[:database]} let(:custom_tenant_names) do @@ -20,7 +19,6 @@ end context "database key taken from specific config" do - let(:expected_args) { get_custom_db_conf } describe "#create" do @@ -45,13 +43,11 @@ end context "database key from tenant name" do - let(:expected_args) { get_custom_db_conf.tap {|args| args.delete(:database) } } describe "#switch!" do - it "should connect to new db" do expect(Apartment).to receive(:establish_connection) do |args| db_name = args.delete(:database) diff --git a/spec/examples/generic_adapter_examples.rb b/spec/examples/generic_adapter_examples.rb index 1ba59291..ead10013 100644 --- a/spec/examples/generic_adapter_examples.rb +++ b/spec/examples/generic_adapter_examples.rb @@ -31,7 +31,6 @@ # Creates happen already in our before_filter # describe "#create" do - it "should create the new databases" do expect(tenant_names).to include(db1) expect(tenant_names).to include(db2) diff --git a/spec/examples/schema_adapter_examples.rb b/spec/examples/schema_adapter_examples.rb index e70df5d8..26ba4a73 100644 --- a/spec/examples/schema_adapter_examples.rb +++ b/spec/examples/schema_adapter_examples.rb @@ -9,7 +9,6 @@ let(:public_schema) { default_tenant } describe "#init" do - before do Apartment.configure do |config| config.excluded_models = ["Company"] @@ -31,7 +30,6 @@ end context "with a default_schema", :default_schema => true do - it "should set the proper table_name on excluded_models" do Apartment::Tenant.init @@ -57,7 +55,6 @@ # Creates happen already in our before_filter # describe "#create" do - it "should load schema.rb to new schema" do connection.schema_search_path = schema1 expect(connection.tables).to include('companies') @@ -90,7 +87,6 @@ after { subject.drop(db) } end - end describe "#drop" do @@ -212,7 +208,6 @@ end context "persistent_schemas", :persistent_schemas => true do - before { subject.switch!(schema1) } it "maintains the persistent schemas in the schema_search_path" do diff --git a/spec/integration/use_within_an_engine_spec.rb b/spec/integration/use_within_an_engine_spec.rb index 21873df1..0e4cfa16 100644 --- a/spec/integration/use_within_an_engine_spec.rb +++ b/spec/integration/use_within_an_engine_spec.rb @@ -1,5 +1,4 @@ describe 'using apartment within an engine' do - before do engine_path = Pathname.new(File.expand_path('../../dummy_engine', __FILE__)) require engine_path.join('test/dummy/config/application') @@ -24,5 +23,4 @@ Rake::Task['db:migrate'].invoke end end - end diff --git a/spec/schemas/v1.rb b/spec/schemas/v1.rb index b5e6a796..e5f626c9 100644 --- a/spec/schemas/v1.rb +++ b/spec/schemas/v1.rb @@ -12,5 +12,4 @@ # It's strongly recommended to check this file into your version control system. ActiveRecord::Schema.define(:version => 0) do - end diff --git a/spec/schemas/v2.rb b/spec/schemas/v2.rb index c6eeaba1..7ace165b 100644 --- a/spec/schemas/v2.rb +++ b/spec/schemas/v2.rb @@ -12,7 +12,6 @@ # It's strongly recommended to check this file into your version control system. ActiveRecord::Schema.define(:version => 20110613152810) do - create_table "companies", :force => true do |t| t.boolean "dummy" t.string "database" @@ -39,5 +38,4 @@ t.datetime "birthdate" t.string "sex" end - end diff --git a/spec/schemas/v3.rb b/spec/schemas/v3.rb index 4dbc011d..7d97bdeb 100644 --- a/spec/schemas/v3.rb +++ b/spec/schemas/v3.rb @@ -12,7 +12,6 @@ # It's strongly recommended to check this file into your version control system. ActiveRecord::Schema.define(:version => 20111202022214) do - create_table "books", :force => true do |t| t.string "name" t.integer "pages" @@ -45,5 +44,4 @@ t.datetime "birthdate" t.string "sex" end - end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 74aa9c8d..9fa79b79 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -36,7 +36,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 diff --git a/spec/support/setup.rb b/spec/support/setup.rb index d4a68512..b7168e4b 100644 --- a/spec/support/setup.rb +++ b/spec/support/setup.rb @@ -12,7 +12,6 @@ def self.included(base) # any before/after hooks defined in individual tests # Otherwise these actually get run after test defined hooks around(:each) do |example| - def config db = RSpec.current_example.metadata.fetch(:database, :postgresql) diff --git a/spec/tasks/apartment_rake_spec.rb b/spec/tasks/apartment_rake_spec.rb index 0514676d..73f0eded 100644 --- a/spec/tasks/apartment_rake_spec.rb +++ b/spec/tasks/apartment_rake_spec.rb @@ -4,7 +4,6 @@ require 'apartment/tenant' describe "apartment rake tasks" do - before do @rake = Rake::Application.new Rake.application = @rake @@ -30,7 +29,6 @@ let(:version) { '1234' } context 'database migration' do - let(:tenant_names) { 3.times.map { Apartment::Test.next_db } } let(:tenant_count) { tenant_names.length } @@ -50,7 +48,6 @@ end describe "apartment:migrate:up" do - context "without a version" do before do ENV['VERSION'] = nil @@ -64,7 +61,6 @@ end context "with version" do - before do ENV['VERSION'] = version end @@ -77,7 +73,6 @@ end describe "apartment:migrate:down" do - context "without a version" do before do ENV['VERSION'] = nil @@ -91,7 +86,6 @@ end context "with version" do - before do ENV['VERSION'] = version end @@ -124,6 +118,5 @@ @rake['apartment:drop'].invoke end end - end end diff --git a/spec/tenant_spec.rb b/spec/tenant_spec.rb index 92cdc2a2..1fdfd88b 100644 --- a/spec/tenant_spec.rb +++ b/spec/tenant_spec.rb @@ -2,7 +2,6 @@ describe Apartment::Tenant do context "using mysql", database: :mysql do - before { subject.reload!(config) } describe "#adapter" do @@ -110,11 +109,9 @@ end describe "#switch!" do - let(:x) { rand(3) } context "creating models" do - before { subject.create db2 } after { subject.drop db2 } @@ -134,7 +131,6 @@ end context "with excluded models" do - before do Apartment.configure do |config| config.excluded_models = ["Company"] diff --git a/spec/unit/config_spec.rb b/spec/unit/config_spec.rb index eea2e2fe..010316ee 100644 --- a/spec/unit/config_spec.rb +++ b/spec/unit/config_spec.rb @@ -1,9 +1,7 @@ require 'spec_helper' describe Apartment do - describe "#config" do - let(:excluded_models) { ["Company"] } let(:seed_data_file_path) { "#{Rails.root}/db/seeds/import.rb" } @@ -107,6 +105,5 @@ def tenant_names_from_array(names) end end end - end end diff --git a/spec/unit/elevators/domain_spec.rb b/spec/unit/elevators/domain_spec.rb index 14090c41..5970e2d4 100644 --- a/spec/unit/elevators/domain_spec.rb +++ b/spec/unit/elevators/domain_spec.rb @@ -2,7 +2,6 @@ require 'apartment/elevators/domain' describe Apartment::Elevators::Domain do - subject(:elevator) { described_class.new(proc) } describe "#parse_tenant_name" do diff --git a/spec/unit/elevators/generic_spec.rb b/spec/unit/elevators/generic_spec.rb index f12f8fb4..727779bf 100644 --- a/spec/unit/elevators/generic_spec.rb +++ b/spec/unit/elevators/generic_spec.rb @@ -2,7 +2,6 @@ require 'apartment/elevators/generic' describe Apartment::Elevators::Generic do - class MyElevator < described_class def parse_tenant_name(*) 'tenant2' diff --git a/spec/unit/elevators/host_hash_spec.rb b/spec/unit/elevators/host_hash_spec.rb index af1aa5be..b47850cd 100644 --- a/spec/unit/elevators/host_hash_spec.rb +++ b/spec/unit/elevators/host_hash_spec.rb @@ -2,7 +2,6 @@ require 'apartment/elevators/host_hash' describe Apartment::Elevators::HostHash do - subject(:elevator) { Apartment::Elevators::HostHash.new(proc{}, 'example.com' => 'example_tenant') } describe "#parse_tenant_name" do diff --git a/spec/unit/elevators/host_spec.rb b/spec/unit/elevators/host_spec.rb index b77fbc49..6338ef32 100644 --- a/spec/unit/elevators/host_spec.rb +++ b/spec/unit/elevators/host_spec.rb @@ -2,11 +2,9 @@ require 'apartment/elevators/host' describe Apartment::Elevators::Host do - subject(:elevator) { described_class.new(proc) } describe "#parse_tenant_name" do - it "should return nil when no host" do request = ActionDispatch::Request.new('HTTP_HOST' => '') expect(elevator.parse_tenant_name(request)).to be_nil diff --git a/spec/unit/elevators/subdomain_spec.rb b/spec/unit/elevators/subdomain_spec.rb index ac4e74f3..87cbc904 100644 --- a/spec/unit/elevators/subdomain_spec.rb +++ b/spec/unit/elevators/subdomain_spec.rb @@ -2,7 +2,6 @@ require 'apartment/elevators/subdomain' describe Apartment::Elevators::Subdomain do - subject(:elevator) { described_class.new(proc) } describe "#parse_tenant_name" do diff --git a/spec/unit/migrator_spec.rb b/spec/unit/migrator_spec.rb index 0022be74..dadc1834 100644 --- a/spec/unit/migrator_spec.rb +++ b/spec/unit/migrator_spec.rb @@ -2,7 +2,6 @@ require 'apartment/migrator' describe Apartment::Migrator do - let(:tenant) { Apartment::Test.next_db } # Don't need a real switch here, just testing behaviour diff --git a/spec/unit/reloader_spec.rb b/spec/unit/reloader_spec.rb index 5034f7dc..16a5f00e 100644 --- a/spec/unit/reloader_spec.rb +++ b/spec/unit/reloader_spec.rb @@ -1,9 +1,7 @@ require 'spec_helper' describe Apartment::Reloader do - context "using postgresql schemas" do - before do Apartment.configure do |config| config.excluded_models = ["Company"] From e7cd59be0a57136bec46f4be20f1272e1a77480e Mon Sep 17 00:00:00 2001 From: Rui Baltazar Date: Sun, 16 Feb 2020 17:00:37 +0800 Subject: [PATCH 23/50] WIP - Fixed EmptyLinesAroundModuleBody --- .rubocop_todo.yml | 20 ------------------- lib/apartment.rb | 1 - lib/apartment/adapters/jdbc_mysql_adapter.rb | 1 - .../adapters/jdbc_postgresql_adapter.rb | 2 -- lib/apartment/adapters/mysql2_adapter.rb | 1 - lib/apartment/adapters/postgis_adapter.rb | 1 - lib/apartment/adapters/postgresql_adapter.rb | 1 - lib/apartment/deprecation.rb | 1 - lib/apartment/migrator.rb | 1 - lib/apartment/tenant.rb | 1 - spec/support/apartment_helpers.rb | 2 -- spec/support/config.rb | 1 - spec/support/requirements.rb | 1 - spec/support/setup.rb | 1 - 14 files changed, 35 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index cdd3a8ae..34bd62d9 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -19,26 +19,6 @@ Bundler/OrderedGems: - 'gemfiles/rails_6_0.gemfile' - 'gemfiles/rails_master.gemfile' -# Offense count: 15 -# Cop supports --auto-correct. -# Configuration parameters: EnforcedStyle. -# SupportedStyles: empty_lines, empty_lines_except_namespace, empty_lines_special, no_empty_lines -Layout/EmptyLinesAroundModuleBody: - Exclude: - - 'lib/apartment.rb' - - 'lib/apartment/adapters/jdbc_mysql_adapter.rb' - - 'lib/apartment/adapters/jdbc_postgresql_adapter.rb' - - 'lib/apartment/adapters/mysql2_adapter.rb' - - 'lib/apartment/adapters/postgis_adapter.rb' - - 'lib/apartment/adapters/postgresql_adapter.rb' - - 'lib/apartment/deprecation.rb' - - 'lib/apartment/migrator.rb' - - 'lib/apartment/tenant.rb' - - 'spec/support/apartment_helpers.rb' - - 'spec/support/config.rb' - - 'spec/support/requirements.rb' - - 'spec/support/setup.rb' - # Offense count: 1 # Cop supports --auto-correct. # Configuration parameters: EnforcedStyleAlignWith, AutoCorrect, Severity. diff --git a/lib/apartment.rb b/lib/apartment.rb index 91839452..e61d93b2 100644 --- a/lib/apartment.rb +++ b/lib/apartment.rb @@ -5,7 +5,6 @@ require 'apartment/tenant' module Apartment - class << self extend Forwardable diff --git a/lib/apartment/adapters/jdbc_mysql_adapter.rb b/lib/apartment/adapters/jdbc_mysql_adapter.rb index 49d04c94..46943176 100644 --- a/lib/apartment/adapters/jdbc_mysql_adapter.rb +++ b/lib/apartment/adapters/jdbc_mysql_adapter.rb @@ -1,7 +1,6 @@ require "apartment/adapters/abstract_jdbc_adapter" module Apartment - module Tenant def self.jdbc_mysql_adapter(config) Adapters::JDBCMysqlAdapter.new config diff --git a/lib/apartment/adapters/jdbc_postgresql_adapter.rb b/lib/apartment/adapters/jdbc_postgresql_adapter.rb index 378f4a14..5ae8c868 100644 --- a/lib/apartment/adapters/jdbc_postgresql_adapter.rb +++ b/lib/apartment/adapters/jdbc_postgresql_adapter.rb @@ -2,7 +2,6 @@ module Apartment module Tenant - def self.jdbc_postgresql_adapter(config) if Apartment.use_schemas Adapters::JDBCPostgresqlSchemaAdapter.new(config) @@ -13,7 +12,6 @@ def self.jdbc_postgresql_adapter(config) end module Adapters - # Default adapter when not using Postgresql Schemas class JDBCPostgresqlAdapter < PostgresqlAdapter private diff --git a/lib/apartment/adapters/mysql2_adapter.rb b/lib/apartment/adapters/mysql2_adapter.rb index 33365cab..30fb2c48 100644 --- a/lib/apartment/adapters/mysql2_adapter.rb +++ b/lib/apartment/adapters/mysql2_adapter.rb @@ -2,7 +2,6 @@ module Apartment module Tenant - def self.mysql2_adapter(config) if Apartment.use_schemas Adapters::Mysql2SchemaAdapter.new(config) diff --git a/lib/apartment/adapters/postgis_adapter.rb b/lib/apartment/adapters/postgis_adapter.rb index 4f5e9176..f9491476 100644 --- a/lib/apartment/adapters/postgis_adapter.rb +++ b/lib/apartment/adapters/postgis_adapter.rb @@ -4,7 +4,6 @@ module Apartment module Tenant - def self.postgis_adapter(config) postgresql_adapter(config) end diff --git a/lib/apartment/adapters/postgresql_adapter.rb b/lib/apartment/adapters/postgresql_adapter.rb index aefde151..b57ccf0f 100644 --- a/lib/apartment/adapters/postgresql_adapter.rb +++ b/lib/apartment/adapters/postgresql_adapter.rb @@ -2,7 +2,6 @@ module Apartment module Tenant - def self.postgresql_adapter(config) adapter = Adapters::PostgresqlAdapter adapter = Adapters::PostgresqlSchemaAdapter if Apartment.use_schemas diff --git a/lib/apartment/deprecation.rb b/lib/apartment/deprecation.rb index 0b225847..3355f802 100644 --- a/lib/apartment/deprecation.rb +++ b/lib/apartment/deprecation.rb @@ -2,7 +2,6 @@ module Apartment module Deprecation - def self.warn(message) ActiveSupport::Deprecation.warn message end diff --git a/lib/apartment/migrator.rb b/lib/apartment/migrator.rb index c99e9a3b..54deea9b 100644 --- a/lib/apartment/migrator.rb +++ b/lib/apartment/migrator.rb @@ -2,7 +2,6 @@ module Apartment module Migrator - extend self # Migrate to latest diff --git a/lib/apartment/tenant.rb b/lib/apartment/tenant.rb index 83e3cdd5..6e1e713b 100644 --- a/lib/apartment/tenant.rb +++ b/lib/apartment/tenant.rb @@ -4,7 +4,6 @@ module Apartment # The main entry point to Apartment functions # module Tenant - extend self extend Forwardable diff --git a/spec/support/apartment_helpers.rb b/spec/support/apartment_helpers.rb index dafbd9d2..1802d49b 100644 --- a/spec/support/apartment_helpers.rb +++ b/spec/support/apartment_helpers.rb @@ -1,6 +1,5 @@ module Apartment module Test - # rubocop:disable Style/ModuleFunction extend self # rubocop:enable Style/ModuleFunction @@ -42,6 +41,5 @@ def migrate def rollback ActiveRecord::Migrator.rollback(Rails.root + ActiveRecord::Migrator.migrations_path) end - end end diff --git a/spec/support/config.rb b/spec/support/config.rb index 999279e5..a19e8e5b 100644 --- a/spec/support/config.rb +++ b/spec/support/config.rb @@ -2,7 +2,6 @@ module Apartment module Test - def self.config @config ||= YAML.load(ERB.new(IO.read('spec/config/database.yml')).result) end diff --git a/spec/support/requirements.rb b/spec/support/requirements.rb index f507ab26..4481fca7 100644 --- a/spec/support/requirements.rb +++ b/spec/support/requirements.rb @@ -1,6 +1,5 @@ module Apartment module Spec - # # Define the interface methods required to # use an adapter shared example diff --git a/spec/support/setup.rb b/spec/support/setup.rb index b7168e4b..e9630dd8 100644 --- a/spec/support/setup.rb +++ b/spec/support/setup.rb @@ -1,7 +1,6 @@ module Apartment module Spec module Setup - def self.included(base) base.instance_eval do let(:db1) { Apartment::Test.next_db } From ea4602748726a58960f7bc67aa075982e825dd70 Mon Sep 17 00:00:00 2001 From: Rui Baltazar Date: Sun, 16 Feb 2020 17:02:52 +0800 Subject: [PATCH 24/50] WIP - ran more auto correct --- .rubocop_todo.yml | 86 ------------------- lib/apartment/adapters/abstract_adapter.rb | 4 +- lib/apartment/adapters/postgresql_adapter.rb | 4 +- lib/apartment/console.rb | 2 +- lib/tasks/apartment.rake | 2 +- spec/dummy/config.ru | 2 +- spec/dummy/config/environments/test.rb | 2 +- spec/dummy/script/rails | 4 +- .../config/initializers/apartment.rb | 2 +- spec/dummy_engine/test/dummy/config.ru | 2 +- ...ic_adapter_custom_configuration_example.rb | 6 +- spec/examples/generic_adapter_examples.rb | 8 +- spec/examples/schema_adapter_examples.rb | 2 +- .../apartment_rake_integration_spec.rb | 2 +- spec/tasks/apartment_rake_spec.rb | 4 +- spec/unit/reloader_spec.rb | 2 +- 16 files changed, 24 insertions(+), 110 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 34bd62d9..0e7836ed 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -19,92 +19,6 @@ Bundler/OrderedGems: - 'gemfiles/rails_6_0.gemfile' - 'gemfiles/rails_master.gemfile' -# Offense count: 1 -# Cop supports --auto-correct. -# Configuration parameters: EnforcedStyleAlignWith, AutoCorrect, Severity. -# SupportedStylesAlignWith: keyword, variable, start_of_line -Layout/EndAlignment: - Exclude: - - 'lib/apartment/elevators/subdomain.rb' - -# Offense count: 15 -# Cop supports --auto-correct. -# Configuration parameters: AllowForAlignment, AllowBeforeTrailingComments, ForceEqualSignAlignment. -Layout/ExtraSpacing: - Exclude: - - 'lib/apartment/adapters/abstract_adapter.rb' - - 'lib/apartment/adapters/postgresql_adapter.rb' - - 'lib/tasks/apartment.rake' - - 'spec/dummy/config.ru' - - 'spec/dummy/config/environments/test.rb' - - 'spec/dummy/script/rails' - - 'spec/dummy_engine/test/dummy/config.ru' - - 'spec/examples/generic_adapter_examples.rb' - - 'spec/examples/schema_adapter_examples.rb' - - 'spec/integration/apartment_rake_integration_spec.rb' - - 'spec/tasks/apartment_rake_spec.rb' - - 'spec/unit/reloader_spec.rb' - -# Offense count: 3 -# Cop supports --auto-correct. -# Configuration parameters: AllowMultipleStyles, EnforcedHashRocketStyle, EnforcedColonStyle, EnforcedLastArgumentHashStyle. -# SupportedHashRocketStyles: key, separator, table -# SupportedColonStyles: key, separator, table -# SupportedLastArgumentHashStyles: always_inspect, always_ignore, ignore_implicit, ignore_explicit -Layout/HashAlignment: - Exclude: - - 'spec/examples/generic_adapter_custom_configuration_example.rb' - -# Offense count: 1 -# Cop supports --auto-correct. -# Configuration parameters: EnforcedStyle. -# SupportedStyles: normal, indented_internal_methods -Layout/IndentationConsistency: - Exclude: - - 'lib/apartment/adapters/abstract_adapter.rb' - -# Offense count: 6 -# Cop supports --auto-correct. -# Configuration parameters: Width, IgnoredPatterns. -Layout/IndentationWidth: - Exclude: - - 'lib/apartment/adapters/abstract_adapter.rb' - - 'lib/apartment/adapters/abstract_jdbc_adapter.rb' - - 'lib/apartment/adapters/jdbc_postgresql_adapter.rb' - - 'lib/apartment/adapters/postgresql_adapter.rb' - - 'lib/apartment/elevators/subdomain.rb' - -# Offense count: 1 -# Cop supports --auto-correct. -# Configuration parameters: AllowDoxygenCommentStyle. -Layout/LeadingCommentSpace: - Exclude: - - 'spec/dummy_engine/config/initializers/apartment.rb' - -# Offense count: 3 -# Cop supports --auto-correct. -# Configuration parameters: EnforcedStyle, IndentationWidth. -# SupportedStyles: aligned, indented, indented_relative_to_receiver -Layout/MultilineMethodCallIndentation: - Exclude: - - 'spec/examples/generic_adapter_examples.rb' - -# Offense count: 1 -# Cop supports --auto-correct. -# Configuration parameters: EnforcedStyle. -# SupportedStyles: space, no_space -Layout/SpaceAroundEqualsInParameterDefault: - Exclude: - - 'lib/apartment/console.rb' - -# Offense count: 3 -# Cop supports --auto-correct. -# Configuration parameters: AllowForAlignment. -Layout/SpaceAroundOperators: - Exclude: - - 'lib/apartment/adapters/postgresql_adapter.rb' - - 'spec/dummy/config/environments/test.rb' - # Offense count: 82 # Cop supports --auto-correct. # Configuration parameters: EnforcedStyle. diff --git a/lib/apartment/adapters/abstract_adapter.rb b/lib/apartment/adapters/abstract_adapter.rb index ff72bf06..ad084b0f 100644 --- a/lib/apartment/adapters/abstract_adapter.rb +++ b/lib/apartment/adapters/abstract_adapter.rb @@ -175,7 +175,7 @@ def connect_to_new(tenant) query_cache_enabled = ActiveRecord::Base.connection.query_cache_enabled Apartment.establish_connection multi_tenantify(tenant) - Apartment.connection.active? # call active? to manually check if this connection is valid + Apartment.connection.active? # call active? to manually check if this connection is valid Apartment.connection.enable_query_cache! if query_cache_enabled rescue *rescuable_exceptions => exception @@ -186,7 +186,7 @@ def connect_to_new(tenant) # Import the database schema # def import_database_schema - ActiveRecord::Schema.verbose = false # do not log schema load output. + ActiveRecord::Schema.verbose = false # do not log schema load output. load_or_raise(Apartment.database_schema_file) if Apartment.database_schema_file end diff --git a/lib/apartment/adapters/postgresql_adapter.rb b/lib/apartment/adapters/postgresql_adapter.rb index b57ccf0f..c584e00c 100644 --- a/lib/apartment/adapters/postgresql_adapter.rb +++ b/lib/apartment/adapters/postgresql_adapter.rb @@ -103,7 +103,7 @@ def postgresql_version # Another Adapter for Postgresql when using schemas and SQL class PostgresqlSchemaFromSqlAdapter < PostgresqlSchemaAdapter - PSQL_DUMP_BLACKLISTED_STATEMENTS= [ + PSQL_DUMP_BLACKLISTED_STATEMENTS = [ /SET search_path/i, # overridden later /SET lock_timeout/i, # new in postgresql 9.3 /SET row_security/i, # new in postgresql 9.5 @@ -173,7 +173,7 @@ def pg_dump_schema_migrations_data # Temporary set Postgresql related environment variables if there are in @config # def with_pg_env(&block) - pghost = ENV['PGHOST'] + pghost = ENV['PGHOST'] pgport = ENV['PGPORT'] pguser = ENV['PGUSER'] pgpassword = ENV['PGPASSWORD'] diff --git a/lib/apartment/console.rb b/lib/apartment/console.rb index 8393be85..390b6ad7 100644 --- a/lib/apartment/console.rb +++ b/lib/apartment/console.rb @@ -2,7 +2,7 @@ # This is unfortunate, but I haven't figured out how to hook into the reload process *after* files are reloaded # reloads the environment -def reload!(print=true) +def reload!(print = true) puts "Reloading..." if print # This triggers the to_prepare callbacks ActionDispatch::Callbacks.new(proc{}).call({}) diff --git a/lib/tasks/apartment.rake b/lib/tasks/apartment.rake index 7931160f..ff736393 100644 --- a/lib/tasks/apartment.rake +++ b/lib/tasks/apartment.rake @@ -106,7 +106,7 @@ apartment_namespace = namespace :apartment do end end - desc 'Rolls back the tenant one migration and re migrate up (options: STEP=x, VERSION=x).' + desc 'Rolls back the tenant one migration and re migrate up (options: STEP=x, VERSION=x).' task :redo do if ENV['VERSION'] apartment_namespace['migrate:down'].invoke diff --git a/spec/dummy/config.ru b/spec/dummy/config.ru index 1989ed8d..cbd74159 100644 --- a/spec/dummy/config.ru +++ b/spec/dummy/config.ru @@ -1,4 +1,4 @@ # This file is used by Rack-based servers to start the application. -require ::File.expand_path('../config/environment', __FILE__) +require ::File.expand_path('../config/environment', __FILE__) run Dummy::Application diff --git a/spec/dummy/config/environments/test.rb b/spec/dummy/config/environments/test.rb index 30879efd..e3634079 100644 --- a/spec/dummy/config/environments/test.rb +++ b/spec/dummy/config/environments/test.rb @@ -17,7 +17,7 @@ config.action_dispatch.show_exceptions = false # Disable request forgery protection in test environment - config.action_controller.allow_forgery_protection = false + config.action_controller.allow_forgery_protection = false # Tell Action Mailer not to deliver emails to the real world. # The :test delivery method accumulates sent emails in the diff --git a/spec/dummy/script/rails b/spec/dummy/script/rails index f8da2cff..bd79dce5 100755 --- a/spec/dummy/script/rails +++ b/spec/dummy/script/rails @@ -1,6 +1,6 @@ #!/usr/bin/env ruby # This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application. -APP_PATH = File.expand_path('../../config/application', __FILE__) -require File.expand_path('../../config/boot', __FILE__) +APP_PATH = File.expand_path('../../config/application', __FILE__) +require File.expand_path('../../config/boot', __FILE__) require 'rails/commands' diff --git a/spec/dummy_engine/config/initializers/apartment.rb b/spec/dummy_engine/config/initializers/apartment.rb index 13d1d152..b94a7f1f 100644 --- a/spec/dummy_engine/config/initializers/apartment.rb +++ b/spec/dummy_engine/config/initializers/apartment.rb @@ -22,7 +22,7 @@ config.use_schemas = true # use raw SQL dumps for creating postgres schemas? (only appies with use_schemas set to true) - #config.use_sql = true + # config.use_sql = true # configure persistent schemas (E.g. hstore ) # config.persistent_schemas = %w{ hstore } diff --git a/spec/dummy_engine/test/dummy/config.ru b/spec/dummy_engine/test/dummy/config.ru index 5bc2a619..bd83b254 100644 --- a/spec/dummy_engine/test/dummy/config.ru +++ b/spec/dummy_engine/test/dummy/config.ru @@ -1,4 +1,4 @@ # This file is used by Rack-based servers to start the application. -require ::File.expand_path('../config/environment', __FILE__) +require ::File.expand_path('../config/environment', __FILE__) run Rails.application diff --git a/spec/examples/generic_adapter_custom_configuration_example.rb b/spec/examples/generic_adapter_custom_configuration_example.rb index 0f64c64a..05328b32 100644 --- a/spec/examples/generic_adapter_custom_configuration_example.rb +++ b/spec/examples/generic_adapter_custom_configuration_example.rb @@ -68,18 +68,18 @@ def specific_connection { postgresql: { - adapter: 'postgresql', + adapter: 'postgresql', database: 'override_database', password: 'override_password', username: 'overridepostgres' }, mysql: { - adapter: 'mysql2', + adapter: 'mysql2', database: 'override_database', username: 'root' }, sqlite: { - adapter: 'sqlite3', + adapter: 'sqlite3', database: 'override_database' } } diff --git a/spec/examples/generic_adapter_examples.rb b/spec/examples/generic_adapter_examples.rb index ead10013..9366cbe5 100644 --- a/spec/examples/generic_adapter_examples.rb +++ b/spec/examples/generic_adapter_examples.rb @@ -18,9 +18,9 @@ Apartment::Railtie.config.to_prepare_blocks.map(&:call) num_available_connections = Apartment.connection_class.connection_pool - .instance_variable_get(:@available) - .instance_variable_get(:@queue) - .size + .instance_variable_get(:@available) + .instance_variable_get(:@queue) + .size expect(num_available_connections).to eq(1) end @@ -45,7 +45,7 @@ it "should yield to block if passed and reset" do subject.drop(db2) # so we don't get errors on creation - @count = 0 # set our variable so its visible in and outside of blocks + @count = 0 # set our variable so its visible in and outside of blocks subject.create(db2) do @count = User.count diff --git a/spec/examples/schema_adapter_examples.rb b/spec/examples/schema_adapter_examples.rb index 26ba4a73..ddd82c1a 100644 --- a/spec/examples/schema_adapter_examples.rb +++ b/spec/examples/schema_adapter_examples.rb @@ -63,7 +63,7 @@ it "should yield to block if passed and reset" do subject.drop(schema2) # so we don't get errors on creation - @count = 0 # set our variable so its visible in and outside of blocks + @count = 0 # set our variable so its visible in and outside of blocks subject.create(schema2) do @count = User.count diff --git a/spec/integration/apartment_rake_integration_spec.rb b/spec/integration/apartment_rake_integration_spec.rb index d4da2d0a..dc819d4e 100644 --- a/spec/integration/apartment_rake_integration_spec.rb +++ b/spec/integration/apartment_rake_integration_spec.rb @@ -31,7 +31,7 @@ after { Rake.application = nil } context "with x number of databases" do - let(:x) { rand(1..5) } # random number of dbs to create + let(:x) { rand(1..5) } # random number of dbs to create let(:db_names) { x.times.map { Apartment::Test.next_db } } let!(:company_count) { db_names.length } diff --git a/spec/tasks/apartment_rake_spec.rb b/spec/tasks/apartment_rake_spec.rb index 73f0eded..2d799b32 100644 --- a/spec/tasks/apartment_rake_spec.rb +++ b/spec/tasks/apartment_rake_spec.rb @@ -19,7 +19,7 @@ after do Rake.application = nil - ENV['VERSION'] = nil # linux users reported env variable carrying on between tests + ENV['VERSION'] = nil # linux users reported env variable carrying on between tests end after(:all) do @@ -38,7 +38,7 @@ describe "apartment:migrate" do before do - allow(ActiveRecord::Migrator).to receive(:migrate) # don't care about this + allow(ActiveRecord::Migrator).to receive(:migrate) # don't care about this end it "should migrate public and all multi-tenant dbs" do diff --git a/spec/unit/reloader_spec.rb b/spec/unit/reloader_spec.rb index 16a5f00e..52b1d80e 100644 --- a/spec/unit/reloader_spec.rb +++ b/spec/unit/reloader_spec.rb @@ -8,7 +8,7 @@ config.use_schemas = true end Apartment::Tenant.reload!(config) - Company.reset_table_name # ensure we're clean + Company.reset_table_name # ensure we're clean end subject { Apartment::Reloader.new(double("Rack::Application", :call => nil)) } From 9495f7964d88d380b2fbd6d16ca8dba828b3f1df Mon Sep 17 00:00:00 2001 From: Rui Baltazar Date: Sun, 16 Feb 2020 17:05:29 +0800 Subject: [PATCH 25/50] WIP - fixed SpaceBeforeBlockBraces --- .rubocop_todo.yml | 8 -------- lib/apartment/console.rb | 2 +- spec/unit/elevators/generic_spec.rb | 10 +++++----- spec/unit/elevators/host_hash_spec.rb | 2 +- 4 files changed, 7 insertions(+), 15 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 0e7836ed..d23854bc 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -19,14 +19,6 @@ Bundler/OrderedGems: - 'gemfiles/rails_6_0.gemfile' - 'gemfiles/rails_master.gemfile' -# Offense count: 82 -# Cop supports --auto-correct. -# Configuration parameters: EnforcedStyle. -# SupportedStyles: space, no_space -# SupportedStylesForEmptyBraces: space, no_space -Layout/SpaceBeforeBlockBraces: - EnforcedStyleForEmptyBraces: no_space - # Offense count: 1 # Cop supports --auto-correct. # Configuration parameters: AllowForAlignment. diff --git a/lib/apartment/console.rb b/lib/apartment/console.rb index 390b6ad7..b84648ed 100644 --- a/lib/apartment/console.rb +++ b/lib/apartment/console.rb @@ -5,7 +5,7 @@ def reload!(print = true) puts "Reloading..." if print # This triggers the to_prepare callbacks - ActionDispatch::Callbacks.new(proc{}).call({}) + ActionDispatch::Callbacks.new(proc {}).call({}) # Manually init Apartment again once classes are reloaded Apartment::Tenant.init true diff --git a/spec/unit/elevators/generic_spec.rb b/spec/unit/elevators/generic_spec.rb index 727779bf..72cd26f0 100644 --- a/spec/unit/elevators/generic_spec.rb +++ b/spec/unit/elevators/generic_spec.rb @@ -12,7 +12,7 @@ def parse_tenant_name(*) describe "#call" do it "calls the processor if given" do - elevator = described_class.new(proc{}, proc {'tenant1'}) + elevator = described_class.new(proc {}, proc {'tenant1'}) expect(Apartment::Tenant).to receive(:switch).with('tenant1') @@ -26,7 +26,7 @@ def parse_tenant_name(*) end it "switches to the parsed db_name" do - elevator = MyElevator.new(proc{}) + elevator = MyElevator.new(proc {}) expect(Apartment::Tenant).to receive(:switch).with('tenant2') @@ -34,15 +34,15 @@ def parse_tenant_name(*) end it "calls the block implementation of `switch`" do - elevator = MyElevator.new(proc{}, proc {'tenant2'}) + elevator = MyElevator.new(proc {}, proc {'tenant2'}) expect(Apartment::Tenant).to receive(:switch).with('tenant2').and_yield elevator.call('HTTP_HOST' => 'foo.bar.com') end it "does not call `switch` if no database given" do - app = proc{} - elevator = MyElevator.new(app, proc{}) + app = proc {} + elevator = MyElevator.new(app, proc {}) expect(Apartment::Tenant).not_to receive(:switch) expect(app).to receive :call diff --git a/spec/unit/elevators/host_hash_spec.rb b/spec/unit/elevators/host_hash_spec.rb index b47850cd..8a8dc344 100644 --- a/spec/unit/elevators/host_hash_spec.rb +++ b/spec/unit/elevators/host_hash_spec.rb @@ -2,7 +2,7 @@ require 'apartment/elevators/host_hash' describe Apartment::Elevators::HostHash do - subject(:elevator) { Apartment::Elevators::HostHash.new(proc{}, 'example.com' => 'example_tenant') } + subject(:elevator) { Apartment::Elevators::HostHash.new(proc {}, 'example.com' => 'example_tenant') } describe "#parse_tenant_name" do it "parses the host for a domain name" do From 127883b16f23365f8ffaa517507b199cd22157f1 Mon Sep 17 00:00:00 2001 From: Rui Baltazar Date: Sun, 16 Feb 2020 17:06:39 +0800 Subject: [PATCH 26/50] WIP - fixed spacing related issues --- .rubocop_todo.yml | 66 ------------------- lib/apartment.rb | 2 +- lib/apartment/adapters/abstract_adapter.rb | 6 +- lib/apartment/adapters/postgresql_adapter.rb | 4 +- lib/apartment/elevators/domain.rb | 2 +- lib/apartment/elevators/first_subdomain.rb | 2 +- lib/apartment/migrator.rb | 2 +- .../app/controllers/application_controller.rb | 2 +- spec/dummy/app/models/company.rb | 2 +- spec/dummy/app/models/user.rb | 2 +- spec/dummy/config/environments/development.rb | 1 - spec/dummy/db/seeds/import.rb | 2 +- .../config/initializers/cookies_serializer.rb | 2 +- ...ic_adapter_custom_configuration_example.rb | 4 +- spec/support/capybara_sessions.rb | 6 +- spec/support/config.rb | 2 +- spec/support/contexts.rb | 4 +- spec/unit/elevators/first_subdomain_spec.rb | 4 +- spec/unit/elevators/generic_spec.rb | 4 +- spec/unit/reloader_spec.rb | 2 +- 20 files changed, 26 insertions(+), 95 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index d23854bc..89f43dfb 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -19,72 +19,6 @@ Bundler/OrderedGems: - 'gemfiles/rails_6_0.gemfile' - 'gemfiles/rails_master.gemfile' -# Offense count: 1 -# Cop supports --auto-correct. -# Configuration parameters: AllowForAlignment. -Layout/SpaceBeforeFirstArg: - Exclude: - - 'lib/tasks/apartment.rake' - -# Offense count: 1 -# Cop supports --auto-correct. -# Configuration parameters: EnforcedStyle. -# SupportedStyles: require_no_space, require_space -Layout/SpaceInLambdaLiteral: - Exclude: - - 'lib/apartment/migrator.rb' - -# Offense count: 14 -# Cop supports --auto-correct. -# Configuration parameters: EnforcedStyle, EnforcedStyleForEmptyBraces, SpaceBeforeBlockParameters. -# SupportedStyles: space, no_space -# SupportedStylesForEmptyBraces: space, no_space -Layout/SpaceInsideBlockBraces: - Exclude: - - 'lib/apartment.rb' - - 'lib/apartment/adapters/postgresql_adapter.rb' - - 'spec/dummy/db/seeds/import.rb' - - 'spec/examples/generic_adapter_custom_configuration_example.rb' - - 'spec/support/contexts.rb' - - 'spec/unit/elevators/generic_spec.rb' - -# Offense count: 6 -# Cop supports --auto-correct. -# Configuration parameters: EnforcedStyle. -# SupportedStyles: space, no_space -Layout/SpaceInsideStringInterpolation: - Exclude: - - 'lib/apartment/adapters/abstract_adapter.rb' - -# Offense count: 12 -# Cop supports --auto-correct. -# Configuration parameters: EnforcedStyle. -# SupportedStyles: final_newline, final_blank_line -Layout/TrailingEmptyLines: - Exclude: - - 'lib/apartment/elevators/first_subdomain.rb' - - 'lib/apartment/elevators/host.rb' - - 'spec/dummy/app/models/company.rb' - - 'spec/dummy/app/models/user.rb' - - 'spec/dummy/config/boot.rb' - - 'spec/dummy/config/environments/development.rb' - - 'spec/dummy_engine/test/dummy/config/initializers/cookies_serializer.rb' - - 'spec/support/capybara_sessions.rb' - - 'spec/support/config.rb' - - 'spec/support/contexts.rb' - - 'spec/unit/elevators/first_subdomain_spec.rb' - - 'spec/unit/reloader_spec.rb' - -# Offense count: 6 -# Cop supports --auto-correct. -# Configuration parameters: AllowInHeredoc. -Layout/TrailingWhitespace: - Exclude: - - 'lib/apartment/elevators/domain.rb' - - 'spec/dummy/app/controllers/application_controller.rb' - - 'spec/support/capybara_sessions.rb' - - 'spec/unit/elevators/first_subdomain_spec.rb' - # Offense count: 7 # Cop supports --auto-correct. Lint/DeprecatedClassMethods: diff --git a/lib/apartment.rb b/lib/apartment.rb index e61d93b2..b48d45fb 100644 --- a/lib/apartment.rb +++ b/lib/apartment.rb @@ -87,7 +87,7 @@ def pg_excluded_names # Reset all the config for Apartment def reset - (ACCESSOR_METHODS + WRITER_METHODS).each {|method| remove_instance_variable(:"@#{method}") if instance_variable_defined?(:"@#{method}") } + (ACCESSOR_METHODS + WRITER_METHODS).each { |method| remove_instance_variable(:"@#{method}") if instance_variable_defined?(:"@#{method}") } end def extract_tenant_config diff --git a/lib/apartment/adapters/abstract_adapter.rb b/lib/apartment/adapters/abstract_adapter.rb index ad084b0f..f3c9feb2 100644 --- a/lib/apartment/adapters/abstract_adapter.rb +++ b/lib/apartment/adapters/abstract_adapter.rb @@ -252,15 +252,15 @@ def reset_on_connection_exception? end def raise_drop_tenant_error!(tenant, exception) - raise TenantNotFound, "Error while dropping tenant #{environmentify(tenant)}: #{ exception.message }" + raise TenantNotFound, "Error while dropping tenant #{environmentify(tenant)}: #{exception.message}" end def raise_create_tenant_error!(tenant, exception) - raise TenantExists, "Error while creating tenant #{environmentify(tenant)}: #{ exception.message }" + raise TenantExists, "Error while creating tenant #{environmentify(tenant)}: #{exception.message}" end def raise_connect_error!(tenant, exception) - raise TenantNotFound, "Error while connecting to tenant #{environmentify(tenant)}: #{ exception.message }" + raise TenantNotFound, "Error while connecting to tenant #{environmentify(tenant)}: #{exception.message}" end class SeparateDbConnectionHandler < ::ActiveRecord::Base diff --git a/lib/apartment/adapters/postgresql_adapter.rb b/lib/apartment/adapters/postgresql_adapter.rb index c584e00c..5c51f70f 100644 --- a/lib/apartment/adapters/postgresql_adapter.rb +++ b/lib/apartment/adapters/postgresql_adapter.rb @@ -200,7 +200,7 @@ def patch_search_path(sql) swap_schema_qualifier(sql) .split("\n") - .select {|line| check_input_against_regexps(line, PSQL_DUMP_BLACKLISTED_STATEMENTS).empty?} + .select { |line| check_input_against_regexps(line, PSQL_DUMP_BLACKLISTED_STATEMENTS).empty? } .prepend(search_path) .join("\n") end @@ -218,7 +218,7 @@ def swap_schema_qualifier(sql) # Checks if any of regexps matches against input # def check_input_against_regexps(input, regexps) - regexps.select {|c| input.match c} + regexps.select { |c| input.match c } end # Collect table names from AR Models diff --git a/lib/apartment/elevators/domain.rb b/lib/apartment/elevators/domain.rb index f52f4b40..9c6862ad 100644 --- a/lib/apartment/elevators/domain.rb +++ b/lib/apartment/elevators/domain.rb @@ -8,7 +8,7 @@ module Elevators # eg. example.com => example # www.example.bc.ca => example # a.example.bc.ca => a - # + # # class Domain < Generic def parse_tenant_name(request) diff --git a/lib/apartment/elevators/first_subdomain.rb b/lib/apartment/elevators/first_subdomain.rb index 052d28c9..e53fa4b9 100644 --- a/lib/apartment/elevators/first_subdomain.rb +++ b/lib/apartment/elevators/first_subdomain.rb @@ -13,4 +13,4 @@ def parse_tenant_name(request) end end end -end \ No newline at end of file +end diff --git a/lib/apartment/migrator.rb b/lib/apartment/migrator.rb index 54deea9b..27f6714c 100644 --- a/lib/apartment/migrator.rb +++ b/lib/apartment/migrator.rb @@ -9,7 +9,7 @@ def migrate(database) Tenant.switch(database) do version = ENV["VERSION"] ? ENV["VERSION"].to_i : nil - migration_scope_block = -> (migration) { ENV["SCOPE"].blank? || (ENV["SCOPE"] == migration.scope) } + migration_scope_block = ->(migration) { ENV["SCOPE"].blank? || (ENV["SCOPE"] == migration.scope) } if activerecord_below_5_2? ActiveRecord::Migrator.migrate(ActiveRecord::Migrator.migrations_paths, version, &migration_scope_block) diff --git a/spec/dummy/app/controllers/application_controller.rb b/spec/dummy/app/controllers/application_controller.rb index 4c655b70..9ec13342 100644 --- a/spec/dummy/app/controllers/application_controller.rb +++ b/spec/dummy/app/controllers/application_controller.rb @@ -1,6 +1,6 @@ class ApplicationController < ActionController::Base protect_from_forgery - + def index end end diff --git a/spec/dummy/app/models/company.rb b/spec/dummy/app/models/company.rb index dd5bf8c7..f97ca639 100644 --- a/spec/dummy/app/models/company.rb +++ b/spec/dummy/app/models/company.rb @@ -1,3 +1,3 @@ class Company < ActiveRecord::Base # Dummy models -end \ No newline at end of file +end diff --git a/spec/dummy/app/models/user.rb b/spec/dummy/app/models/user.rb index 145f27f9..61e0ee5f 100644 --- a/spec/dummy/app/models/user.rb +++ b/spec/dummy/app/models/user.rb @@ -1,3 +1,3 @@ class User < ActiveRecord::Base # Dummy models -end \ No newline at end of file +end diff --git a/spec/dummy/config/environments/development.rb b/spec/dummy/config/environments/development.rb index c143a826..229d545c 100644 --- a/spec/dummy/config/environments/development.rb +++ b/spec/dummy/config/environments/development.rb @@ -25,4 +25,3 @@ # Only use best-standards-support built into browsers config.action_dispatch.best_standards_support = :builtin end - diff --git a/spec/dummy/db/seeds/import.rb b/spec/dummy/db/seeds/import.rb index 8265d6be..4e2eb81b 100644 --- a/spec/dummy/db/seeds/import.rb +++ b/spec/dummy/db/seeds/import.rb @@ -1,5 +1,5 @@ def create_users - 6.times {|x| User.where(name: "Different User #{x}").first_or_create! } + 6.times { |x| User.where(name: "Different User #{x}").first_or_create! } end create_users diff --git a/spec/dummy_engine/test/dummy/config/initializers/cookies_serializer.rb b/spec/dummy_engine/test/dummy/config/initializers/cookies_serializer.rb index 7a06a89f..7f70458d 100644 --- a/spec/dummy_engine/test/dummy/config/initializers/cookies_serializer.rb +++ b/spec/dummy_engine/test/dummy/config/initializers/cookies_serializer.rb @@ -1,3 +1,3 @@ # Be sure to restart your server when you modify this file. -Rails.application.config.action_dispatch.cookies_serializer = :json \ No newline at end of file +Rails.application.config.action_dispatch.cookies_serializer = :json diff --git a/spec/examples/generic_adapter_custom_configuration_example.rb b/spec/examples/generic_adapter_custom_configuration_example.rb index 05328b32..27ca1c84 100644 --- a/spec/examples/generic_adapter_custom_configuration_example.rb +++ b/spec/examples/generic_adapter_custom_configuration_example.rb @@ -2,7 +2,7 @@ shared_examples_for "a generic apartment adapter able to handle custom configuration" do let(:custom_tenant_name) { 'test_tenantwwww' } - let(:db) { |example| example.metadata[:database]} + let(:db) { |example| example.metadata[:database] } let(:custom_tenant_names) do { custom_tenant_name => get_custom_db_conf @@ -44,7 +44,7 @@ context "database key from tenant name" do let(:expected_args) { - get_custom_db_conf.tap {|args| args.delete(:database) } + get_custom_db_conf.tap { |args| args.delete(:database) } } describe "#switch!" do diff --git a/spec/support/capybara_sessions.rb b/spec/support/capybara_sessions.rb index 727d6c07..f9aef29a 100644 --- a/spec/support/capybara_sessions.rb +++ b/spec/support/capybara_sessions.rb @@ -1,15 +1,13 @@ 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 \ No newline at end of file +end diff --git a/spec/support/config.rb b/spec/support/config.rb index a19e8e5b..2111641a 100644 --- a/spec/support/config.rb +++ b/spec/support/config.rb @@ -6,4 +6,4 @@ def self.config @config ||= YAML.load(ERB.new(IO.read('spec/config/database.yml')).result) end end -end \ No newline at end of file +end diff --git a/spec/support/contexts.rb b/spec/support/contexts.rb index 844f4e0a..2e95ffe4 100644 --- a/spec/support/contexts.rb +++ b/spec/support/contexts.rb @@ -41,12 +41,12 @@ let(:persistent_schemas) { %w[hstore postgis] } before do - persistent_schemas.map {|schema| subject.create(schema) } + persistent_schemas.map { |schema| subject.create(schema) } Apartment.persistent_schemas = persistent_schemas end after do Apartment.persistent_schemas = [] - persistent_schemas.map {|schema| subject.drop(schema) } + persistent_schemas.map { |schema| subject.drop(schema) } end end diff --git a/spec/unit/elevators/first_subdomain_spec.rb b/spec/unit/elevators/first_subdomain_spec.rb index 899b7bba..837cb3b3 100644 --- a/spec/unit/elevators/first_subdomain_spec.rb +++ b/spec/unit/elevators/first_subdomain_spec.rb @@ -15,10 +15,10 @@ let(:subdomain) { "test1.test2" } it { is_expected.to eq("test1") } end - + context "no subdomain" do let(:subdomain) { nil } it { is_expected.to eq(nil) } end end -end \ No newline at end of file +end diff --git a/spec/unit/elevators/generic_spec.rb b/spec/unit/elevators/generic_spec.rb index 72cd26f0..9bbe1ad0 100644 --- a/spec/unit/elevators/generic_spec.rb +++ b/spec/unit/elevators/generic_spec.rb @@ -12,7 +12,7 @@ def parse_tenant_name(*) describe "#call" do it "calls the processor if given" do - elevator = described_class.new(proc {}, proc {'tenant1'}) + elevator = described_class.new(proc {}, proc { 'tenant1' }) expect(Apartment::Tenant).to receive(:switch).with('tenant1') @@ -34,7 +34,7 @@ def parse_tenant_name(*) end it "calls the block implementation of `switch`" do - elevator = MyElevator.new(proc {}, proc {'tenant2'}) + elevator = MyElevator.new(proc {}, proc { 'tenant2' }) expect(Apartment::Tenant).to receive(:switch).with('tenant2').and_yield elevator.call('HTTP_HOST' => 'foo.bar.com') diff --git a/spec/unit/reloader_spec.rb b/spec/unit/reloader_spec.rb index 52b1d80e..312aa129 100644 --- a/spec/unit/reloader_spec.rb +++ b/spec/unit/reloader_spec.rb @@ -19,4 +19,4 @@ expect(Company.table_name).to include('public.') end end -end \ No newline at end of file +end From edf9d242a0776c5d1eb73edca9f4dd5d580e8f92 Mon Sep 17 00:00:00 2001 From: Rui Baltazar Date: Sun, 16 Feb 2020 17:16:25 +0800 Subject: [PATCH 27/50] WIP - ran some more auto correct --- .pryrc | 2 + .rubocop_todo.yml | 123 ------------------ Appraisals | 34 ++--- gemfiles/rails_4_2.gemfile | 22 ++-- gemfiles/rails_5_0.gemfile | 20 +-- gemfiles/rails_5_1.gemfile | 20 +-- gemfiles/rails_5_2.gemfile | 18 +-- gemfiles/rails_6_0.gemfile | 20 +-- gemfiles/rails_master.gemfile | 20 +-- lib/apartment.rb | 6 +- lib/apartment/adapters/abstract_adapter.rb | 16 ++- .../adapters/abstract_jdbc_adapter.rb | 2 + lib/apartment/adapters/jdbc_mysql_adapter.rb | 4 +- .../adapters/jdbc_postgresql_adapter.rb | 4 +- lib/apartment/adapters/mysql2_adapter.rb | 6 +- lib/apartment/adapters/postgis_adapter.rb | 4 +- lib/apartment/adapters/postgresql_adapter.rb | 12 +- lib/apartment/adapters/sqlite3_adapter.rb | 8 +- lib/apartment/console.rb | 4 +- lib/apartment/deprecation.rb | 2 + lib/apartment/elevators/domain.rb | 4 +- lib/apartment/elevators/first_subdomain.rb | 2 + lib/apartment/elevators/generic.rb | 4 +- lib/apartment/elevators/host.rb | 2 + lib/apartment/elevators/host_hash.rb | 2 + lib/apartment/elevators/subdomain.rb | 2 + lib/apartment/migrator.rb | 6 +- lib/apartment/railtie.rb | 4 + lib/apartment/reloader.rb | 2 + lib/apartment/tasks/enhancements.rb | 6 +- lib/apartment/tenant.rb | 2 + lib/apartment/version.rb | 4 +- .../apartment/install/install_generator.rb | 4 +- .../apartment/install/templates/apartment.rb | 4 +- lib/tasks/apartment.rake | 14 +- spec/adapters/jdbc_mysql_adapter_spec.rb | 8 +- spec/adapters/jdbc_postgresql_adapter_spec.rb | 18 +-- spec/adapters/mysql2_adapter_spec.rb | 26 ++-- spec/adapters/postgresql_adapter_spec.rb | 28 ++-- spec/adapters/sqlite3_adapter_spec.rb | 36 ++--- spec/apartment_spec.rb | 6 +- .../app/controllers/application_controller.rb | 2 + spec/dummy/app/helpers/application_helper.rb | 2 + spec/dummy/app/models/company.rb | 4 +- spec/dummy/app/models/user.rb | 4 +- spec/dummy/config.ru | 2 + spec/dummy/config/application.rb | 18 +-- spec/dummy/config/boot.rb | 2 + spec/dummy/config/environment.rb | 2 + spec/dummy/config/environments/development.rb | 2 + spec/dummy/config/environments/production.rb | 4 +- spec/dummy/config/environments/test.rb | 2 + spec/dummy/config/initializers/apartment.rb | 6 +- .../initializers/backtrace_silencers.rb | 1 + spec/dummy/config/initializers/inflections.rb | 1 + spec/dummy/config/initializers/mime_types.rb | 1 + .../dummy/config/initializers/secret_token.rb | 2 + .../config/initializers/session_store.rb | 4 +- spec/dummy/config/routes.rb | 4 +- spec/dummy/db/seeds/import.rb | 2 + spec/dummy/script/rails | 2 + .../config/initializers/apartment.rb | 4 +- spec/dummy_engine/lib/dummy_engine.rb | 4 +- spec/dummy_engine/lib/dummy_engine/engine.rb | 2 + spec/dummy_engine/lib/dummy_engine/version.rb | 4 +- spec/dummy_engine/test/dummy/config.ru | 2 + .../test/dummy/config/application.rb | 4 +- spec/dummy_engine/test/dummy/config/boot.rb | 2 + .../test/dummy/config/environment.rb | 2 + .../dummy/config/environments/development.rb | 2 + .../dummy/config/environments/production.rb | 2 + .../test/dummy/config/environments/test.rb | 2 + .../test/dummy/config/initializers/assets.rb | 2 + .../initializers/backtrace_silencers.rb | 1 + .../config/initializers/cookies_serializer.rb | 2 + .../initializers/filter_parameter_logging.rb | 2 + .../dummy/config/initializers/inflections.rb | 1 + .../dummy/config/initializers/mime_types.rb | 1 + .../config/initializers/session_store.rb | 2 + .../config/initializers/wrap_parameters.rb | 2 + spec/dummy_engine/test/dummy/config/routes.rb | 2 + spec/examples/connection_adapter_examples.rb | 18 +-- ...ic_adapter_custom_configuration_example.rb | 28 ++-- spec/examples/generic_adapter_examples.rb | 50 +++---- spec/examples/schema_adapter_examples.rb | 122 ++++++++--------- .../apartment_rake_integration_spec.rb | 36 ++--- spec/integration/query_caching_spec.rb | 12 +- spec/integration/use_within_an_engine_spec.rb | 2 + spec/schemas/v1.rb | 4 +- spec/schemas/v2.rb | 44 +++---- spec/schemas/v3.rb | 52 ++++---- spec/spec_helper.rb | 11 +- spec/support/apartment_helpers.rb | 4 +- spec/support/capybara_sessions.rb | 2 + spec/support/config.rb | 2 + spec/support/contexts.rb | 8 +- spec/support/requirements.rb | 4 +- spec/support/setup.rb | 2 + spec/tasks/apartment_rake_spec.rb | 42 +++--- spec/tenant_spec.rb | 52 ++++---- spec/unit/config_spec.rb | 48 +++---- spec/unit/elevators/domain_spec.rb | 14 +- spec/unit/elevators/first_subdomain_spec.rb | 22 ++-- spec/unit/elevators/generic_spec.rb | 14 +- spec/unit/elevators/host_hash_spec.rb | 14 +- spec/unit/elevators/host_spec.rb | 48 +++---- spec/unit/elevators/subdomain_spec.rb | 44 ++++--- spec/unit/migrator_spec.rb | 2 + spec/unit/reloader_spec.rb | 10 +- 109 files changed, 718 insertions(+), 636 deletions(-) diff --git a/.pryrc b/.pryrc index 22e16cde..6d4a3df8 100644 --- a/.pryrc +++ b/.pryrc @@ -1,3 +1,5 @@ +# frozen_string_literal: true + if defined?(Rails) && Rails.env extend Rails::ConsoleMethods end diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 89f43dfb..f397e797 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -19,32 +19,6 @@ Bundler/OrderedGems: - 'gemfiles/rails_6_0.gemfile' - 'gemfiles/rails_master.gemfile' -# Offense count: 7 -# Cop supports --auto-correct. -Lint/DeprecatedClassMethods: - Exclude: - - 'lib/apartment/adapters/abstract_adapter.rb' - - 'lib/apartment/adapters/sqlite3_adapter.rb' - - 'spec/adapters/sqlite3_adapter_spec.rb' - -# Offense count: 3 -Lint/ParenthesesAsGroupedExpression: - Exclude: - - 'spec/adapters/sqlite3_adapter_spec.rb' - -# Offense count: 1 -# Cop supports --auto-correct. -Lint/RedundantStringCoercion: - Exclude: - - 'spec/examples/schema_adapter_examples.rb' - -# Offense count: 2 -# Configuration parameters: AllowComments. -Lint/SuppressedException: - Exclude: - - 'lib/apartment/railtie.rb' - - 'spec/spec_helper.rb' - # Offense count: 3 # Cop supports --auto-correct. # Configuration parameters: AllowUnusedKeywordArguments, IgnoreEmptyMethods. @@ -81,26 +55,6 @@ Metrics/LineLength: Metrics/MethodLength: Max: 24 -# Offense count: 1 -Naming/AccessorMethodName: - Exclude: - - 'spec/examples/generic_adapter_custom_configuration_example.rb' - -# Offense count: 4 -# Cop supports --auto-correct. -# Configuration parameters: PreferredName. -Naming/RescuedExceptionsVariableName: - Exclude: - - 'lib/apartment/adapters/abstract_adapter.rb' - - 'lib/apartment/adapters/mysql2_adapter.rb' - -# Offense count: 2 -# Cop supports --auto-correct. -Rails/ApplicationRecord: - Exclude: - - 'spec/dummy/app/models/company.rb' - - 'spec/dummy/app/models/user.rb' - # Offense count: 2 # Cop supports --auto-correct. # Configuration parameters: Whitelist. @@ -109,15 +63,6 @@ Rails/DynamicFindBy: Exclude: - 'spec/integration/query_caching_spec.rb' -# Offense count: 3 -# Configuration parameters: EnforcedStyle. -# SupportedStyles: slashes, arguments -Rails/FilePath: - Exclude: - - 'lib/apartment.rb' - - 'spec/tenant_spec.rb' - - 'spec/unit/config_spec.rb' - # Offense count: 1 # Configuration parameters: Include. # Include: app/**/*.rb, config/**/*.rb, db/**/*.rb, lib/**/*.rb @@ -176,14 +121,6 @@ Style/EmptyMethod: Exclude: - 'spec/dummy/app/controllers/application_controller.rb' -# Offense count: 3 -# Cop supports --auto-correct. -Style/Encoding: - Exclude: - - 'spec/schemas/v1.rb' - - 'spec/schemas/v2.rb' - - 'spec/schemas/v3.rb' - # Offense count: 14 # Cop supports --auto-correct. Style/ExpandPathArguments: @@ -214,13 +151,6 @@ Style/FormatString: Style/FormatStringToken: EnforcedStyle: unannotated -# Offense count: 107 -# Cop supports --auto-correct. -# Configuration parameters: EnforcedStyle. -# SupportedStyles: always, never -Style/FrozenStringLiteralComment: - Enabled: false - # Offense count: 2 # Configuration parameters: MinBodyLength. Style/GuardClause: @@ -228,25 +158,6 @@ Style/GuardClause: - 'lib/apartment/adapters/abstract_adapter.rb' - 'lib/tasks/apartment.rake' -# Offense count: 33 -# Cop supports --auto-correct. -# Configuration parameters: EnforcedStyle, UseHashRocketsWithSymbolValues, PreferHashRocketsForNonAlnumEndingSymbols. -# SupportedStyles: ruby19, hash_rockets, no_mixed_keys, ruby19_no_mixed_keys -Style/HashSyntax: - Exclude: - - 'lib/apartment/adapters/jdbc_postgresql_adapter.rb' - - 'spec/dummy/config/initializers/session_store.rb' - - 'spec/dummy/config/routes.rb' - - 'spec/examples/schema_adapter_examples.rb' - - 'spec/integration/apartment_rake_integration_spec.rb' - - 'spec/schemas/v1.rb' - - 'spec/schemas/v2.rb' - - 'spec/schemas/v3.rb' - - 'spec/support/contexts.rb' - - 'spec/tenant_spec.rb' - - 'spec/unit/elevators/first_subdomain_spec.rb' - - 'spec/unit/reloader_spec.rb' - # Offense count: 6 # Cop supports --auto-correct. Style/IfUnlessModifier: @@ -257,18 +168,6 @@ Style/IfUnlessModifier: - 'lib/apartment/adapters/jdbc_postgresql_adapter.rb' - 'lib/apartment/adapters/postgresql_adapter.rb' -# Offense count: 7 -# Cop supports --auto-correct. -# Configuration parameters: EnforcedStyle. -# SupportedStyles: line_count_dependent, lambda, literal -Style/Lambda: - Exclude: - - 'lib/generators/apartment/install/templates/apartment.rb' - - 'spec/dummy/config/initializers/apartment.rb' - - 'spec/integration/apartment_rake_integration_spec.rb' - - 'spec/integration/query_caching_spec.rb' - - 'spec/unit/config_spec.rb' - # Offense count: 1 Style/MixinUsage: Exclude: @@ -293,25 +192,3 @@ Style/MutableConstant: - 'lib/apartment/tasks/enhancements.rb' - 'lib/apartment/version.rb' - 'spec/dummy_engine/lib/dummy_engine/version.rb' - -# Offense count: 29 -# Cop supports --auto-correct. -# Configuration parameters: PreferredDelimiters. -Style/PercentLiteralDelimiters: - Exclude: - - 'lib/apartment/adapters/postgresql_adapter.rb' - - 'lib/apartment/tasks/enhancements.rb' - - 'spec/dummy/config/application.rb' - - 'spec/dummy_engine/config/initializers/apartment.rb' - - 'spec/examples/schema_adapter_examples.rb' - - 'spec/integration/apartment_rake_integration_spec.rb' - - 'spec/support/requirements.rb' - - 'spec/unit/elevators/host_spec.rb' - - 'spec/unit/elevators/subdomain_spec.rb' - -# Offense count: 518 -# Cop supports --auto-correct. -# Configuration parameters: EnforcedStyle, ConsistentQuotesInMultiline. -# SupportedStyles: single_quotes, double_quotes -Style/StringLiterals: - Enabled: false diff --git a/Appraisals b/Appraisals index 160e0ada..9a5ad27a 100644 --- a/Appraisals +++ b/Appraisals @@ -1,8 +1,10 @@ -appraise "rails-4-2" do - gem "rails", "~> 4.2.0" +# frozen_string_literal: true + +appraise 'rails-4-2' do + gem 'rails', '~> 4.2.0' platforms :ruby do - gem "pg", "< 1.0.0" - gem "mysql2", "~> 0.4.0" + gem 'pg', '< 1.0.0' + gem 'mysql2', '~> 0.4.0' end platforms :jruby do gem 'activerecord-jdbc-adapter', '~> 1.3' @@ -11,10 +13,10 @@ appraise "rails-4-2" do end end -appraise "rails-5-0" do - gem "rails", "~> 5.0.0" +appraise 'rails-5-0' do + gem 'rails', '~> 5.0.0' platforms :ruby do - gem "pg", "< 1.0.0" + gem 'pg', '< 1.0.0' end platforms :jruby do gem 'activerecord-jdbc-adapter', '~> 50.0' @@ -23,10 +25,10 @@ appraise "rails-5-0" do end end -appraise "rails-5-1" do - gem "rails", "~> 5.1.0" +appraise 'rails-5-1' do + gem 'rails', '~> 5.1.0' platforms :ruby do - gem "pg", "< 1.0.0" + gem 'pg', '< 1.0.0' end platforms :jruby do gem 'activerecord-jdbc-adapter', '~> 51.0' @@ -35,8 +37,8 @@ appraise "rails-5-1" do end end -appraise "rails-5-2" do - gem "rails", "~> 5.2.0" +appraise 'rails-5-2' do + gem 'rails', '~> 5.2.0' platforms :jruby do gem 'activerecord-jdbc-adapter', '~> 52.0' gem 'activerecord-jdbcpostgresql-adapter', '~> 52.0' @@ -44,8 +46,8 @@ appraise "rails-5-2" do end end -appraise "rails-6-0" do - gem "rails", "~> 6.0.0.rc1" +appraise 'rails-6-0' do + gem 'rails', '~> 6.0.0.rc1' platforms :ruby do gem 'sqlite3', '~> 1.4' end @@ -56,8 +58,8 @@ appraise "rails-6-0" do end end -appraise "rails-master" do - gem "rails", git: 'https://github.com/rails/rails.git' +appraise 'rails-master' do + gem 'rails', git: 'https://github.com/rails/rails.git' platforms :ruby do gem 'sqlite3', '~> 1.4' end diff --git a/gemfiles/rails_4_2.gemfile b/gemfiles/rails_4_2.gemfile index ef31a500..acec9a0d 100644 --- a/gemfiles/rails_4_2.gemfile +++ b/gemfiles/rails_4_2.gemfile @@ -1,23 +1,25 @@ +# frozen_string_literal: true + # This file was generated by Appraisal -source "http://rubygems.org" +source 'http://rubygems.org' -gem "rails", "~> 4.2.0" +gem 'rails', '~> 4.2.0' group :local do - gem "pry" - gem "guard-rspec", "~> 4.2" + gem 'pry' + gem 'guard-rspec', '~> 4.2' end platforms :ruby do - gem "pg", "< 1.0.0" - gem "mysql2", "~> 0.4.0" + gem 'pg', '< 1.0.0' + gem 'mysql2', '~> 0.4.0' end platforms :jruby do - gem "activerecord-jdbc-adapter", "~> 1.3" - gem "activerecord-jdbcpostgresql-adapter", "~> 1.3" - gem "activerecord-jdbcmysql-adapter", "~> 1.3" + gem 'activerecord-jdbc-adapter', '~> 1.3' + gem 'activerecord-jdbcpostgresql-adapter', '~> 1.3' + gem 'activerecord-jdbcmysql-adapter', '~> 1.3' end -gemspec path: "../" +gemspec path: '../' diff --git a/gemfiles/rails_5_0.gemfile b/gemfiles/rails_5_0.gemfile index 07d99a14..c1188bc2 100644 --- a/gemfiles/rails_5_0.gemfile +++ b/gemfiles/rails_5_0.gemfile @@ -1,22 +1,24 @@ +# frozen_string_literal: true + # This file was generated by Appraisal -source "http://rubygems.org" +source 'http://rubygems.org' -gem "rails", "~> 5.0.0" +gem 'rails', '~> 5.0.0' group :local do - gem "pry" - gem "guard-rspec", "~> 4.2" + gem 'pry' + gem 'guard-rspec', '~> 4.2' end platforms :ruby do - gem "pg", "< 1.0.0" + gem 'pg', '< 1.0.0' end platforms :jruby do - gem "activerecord-jdbc-adapter", "~> 50.0" - gem "activerecord-jdbcpostgresql-adapter", "~> 50.0" - gem "activerecord-jdbcmysql-adapter", "~> 50.0" + gem 'activerecord-jdbc-adapter', '~> 50.0' + gem 'activerecord-jdbcpostgresql-adapter', '~> 50.0' + gem 'activerecord-jdbcmysql-adapter', '~> 50.0' end -gemspec path: "../" +gemspec path: '../' diff --git a/gemfiles/rails_5_1.gemfile b/gemfiles/rails_5_1.gemfile index 28353198..3e738bf1 100644 --- a/gemfiles/rails_5_1.gemfile +++ b/gemfiles/rails_5_1.gemfile @@ -1,22 +1,24 @@ +# frozen_string_literal: true + # This file was generated by Appraisal -source "http://rubygems.org" +source 'http://rubygems.org' -gem "rails", "~> 5.1.0" +gem 'rails', '~> 5.1.0' group :local do - gem "pry" - gem "guard-rspec", "~> 4.2" + gem 'pry' + gem 'guard-rspec', '~> 4.2' end platforms :ruby do - gem "pg", "< 1.0.0" + gem 'pg', '< 1.0.0' end platforms :jruby do - gem "activerecord-jdbc-adapter", "~> 51.0" - gem "activerecord-jdbcpostgresql-adapter", "~> 51.0" - gem "activerecord-jdbcmysql-adapter", "~> 51.0" + gem 'activerecord-jdbc-adapter', '~> 51.0' + gem 'activerecord-jdbcpostgresql-adapter', '~> 51.0' + gem 'activerecord-jdbcmysql-adapter', '~> 51.0' end -gemspec path: "../" +gemspec path: '../' diff --git a/gemfiles/rails_5_2.gemfile b/gemfiles/rails_5_2.gemfile index 5a18c669..57d1aac4 100644 --- a/gemfiles/rails_5_2.gemfile +++ b/gemfiles/rails_5_2.gemfile @@ -1,18 +1,20 @@ +# frozen_string_literal: true + # This file was generated by Appraisal -source "http://rubygems.org" +source 'http://rubygems.org' -gem "rails", "~> 5.2.0" +gem 'rails', '~> 5.2.0' group :local do - gem "pry" - gem "guard-rspec", "~> 4.2" + gem 'pry' + gem 'guard-rspec', '~> 4.2' end platforms :jruby do - gem "activerecord-jdbc-adapter", "~> 52.0" - gem "activerecord-jdbcpostgresql-adapter", "~> 52.0" - gem "activerecord-jdbcmysql-adapter", "~> 52.0" + gem 'activerecord-jdbc-adapter', '~> 52.0' + gem 'activerecord-jdbcpostgresql-adapter', '~> 52.0' + gem 'activerecord-jdbcmysql-adapter', '~> 52.0' end -gemspec path: "../" +gemspec path: '../' diff --git a/gemfiles/rails_6_0.gemfile b/gemfiles/rails_6_0.gemfile index 20ed8a37..58cb5b6e 100644 --- a/gemfiles/rails_6_0.gemfile +++ b/gemfiles/rails_6_0.gemfile @@ -1,22 +1,24 @@ +# frozen_string_literal: true + # This file was generated by Appraisal -source "http://rubygems.org" +source 'http://rubygems.org' -gem "rails", "~> 6.0.0.rc1" +gem 'rails', '~> 6.0.0.rc1' group :local do - gem "pry" - gem "guard-rspec", "~> 4.2" + gem 'pry' + gem 'guard-rspec', '~> 4.2' end platforms :ruby do - gem "sqlite3", "~> 1.4" + gem 'sqlite3', '~> 1.4' end platforms :jruby do - gem "activerecord-jdbc-adapter", "~> 60.0.rc1" - gem "activerecord-jdbcpostgresql-adapter", "~> 60.0.rc1" - gem "activerecord-jdbcmysql-adapter", "~> 60.0.rc1" + gem 'activerecord-jdbc-adapter', '~> 60.0.rc1' + gem 'activerecord-jdbcpostgresql-adapter', '~> 60.0.rc1' + gem 'activerecord-jdbcmysql-adapter', '~> 60.0.rc1' end -gemspec path: "../" +gemspec path: '../' diff --git a/gemfiles/rails_master.gemfile b/gemfiles/rails_master.gemfile index edc30465..a2eed35f 100644 --- a/gemfiles/rails_master.gemfile +++ b/gemfiles/rails_master.gemfile @@ -1,22 +1,24 @@ +# frozen_string_literal: true + # This file was generated by Appraisal -source "http://rubygems.org" +source 'http://rubygems.org' -gem "rails", git: "https://github.com/rails/rails.git" +gem 'rails', git: 'https://github.com/rails/rails.git' group :local do - gem "pry" - gem "guard-rspec", "~> 4.2" + gem 'pry' + gem 'guard-rspec', '~> 4.2' end platforms :ruby do - gem "sqlite3", "~> 1.4" + gem 'sqlite3', '~> 1.4' end platforms :jruby do - gem "activerecord-jdbc-adapter", "~> 52.0" - gem "activerecord-jdbcpostgresql-adapter", "~> 52.0" - gem "activerecord-jdbcmysql-adapter", "~> 52.0" + gem 'activerecord-jdbc-adapter', '~> 52.0' + gem 'activerecord-jdbcpostgresql-adapter', '~> 52.0' + gem 'activerecord-jdbcmysql-adapter', '~> 52.0' end -gemspec path: "../" +gemspec path: '../' diff --git a/lib/apartment.rb b/lib/apartment.rb index b48d45fb..0b5f774e 100644 --- a/lib/apartment.rb +++ b/lib/apartment.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'apartment/railtie' if defined?(Rails) require 'active_support/core_ext/object/blank' require 'forwardable' @@ -52,7 +54,7 @@ def excluded_models end def default_schema - @default_schema || "public" # TODO 'public' is postgres specific + @default_schema || 'public' # TODO 'public' is postgres specific end def parallel_migration_threads @@ -78,7 +80,7 @@ def database_schema_file def seed_data_file return @seed_data_file if defined?(@seed_data_file) - @seed_data_file = "#{Rails.root}/db/seeds.rb" + @seed_data_file = Rails.root.join('db', 'seeds.rb') end def pg_excluded_names diff --git a/lib/apartment/adapters/abstract_adapter.rb b/lib/apartment/adapters/abstract_adapter.rb index f3c9feb2..fe23552f 100644 --- a/lib/apartment/adapters/abstract_adapter.rb +++ b/lib/apartment/adapters/abstract_adapter.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Apartment module Adapters # rubocop:disable Metrics/ClassLength @@ -56,8 +58,8 @@ def drop(tenant) with_neutral_connection(tenant) do |conn| drop_command(conn, tenant) end - rescue *rescuable_exceptions => exception - raise_drop_tenant_error!(tenant, exception) + rescue *rescuable_exceptions => e + raise_drop_tenant_error!(tenant, e) end # Switch to a new tenant @@ -159,8 +161,8 @@ def create_tenant(tenant) with_neutral_connection(tenant) do |conn| create_tenant_command(conn, tenant) end - rescue *rescuable_exceptions => exception - raise_create_tenant_error!(tenant, exception) + rescue *rescuable_exceptions => e + raise_create_tenant_error!(tenant, e) end def create_tenant_command(conn, tenant) @@ -178,9 +180,9 @@ def connect_to_new(tenant) Apartment.connection.active? # call active? to manually check if this connection is valid Apartment.connection.enable_query_cache! if query_cache_enabled - rescue *rescuable_exceptions => exception + rescue *rescuable_exceptions => e Apartment::Tenant.reset if reset_on_connection_exception? - raise_connect_error!(tenant, exception) + raise_connect_error!(tenant, e) end # Import the database schema @@ -210,7 +212,7 @@ def multi_tenantify_with_tenant_db_name(config, tenant) # Load a file or raise error if it doesn't exists # def load_or_raise(file) - if File.exists?(file) + if File.exist?(file) load(file) else raise FileNotFound, "#{file} doesn't exist yet" diff --git a/lib/apartment/adapters/abstract_jdbc_adapter.rb b/lib/apartment/adapters/abstract_jdbc_adapter.rb index 0f7da013..d37aa79d 100644 --- a/lib/apartment/adapters/abstract_jdbc_adapter.rb +++ b/lib/apartment/adapters/abstract_jdbc_adapter.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'apartment/adapters/abstract_adapter' module Apartment diff --git a/lib/apartment/adapters/jdbc_mysql_adapter.rb b/lib/apartment/adapters/jdbc_mysql_adapter.rb index 46943176..90c1bfeb 100644 --- a/lib/apartment/adapters/jdbc_mysql_adapter.rb +++ b/lib/apartment/adapters/jdbc_mysql_adapter.rb @@ -1,4 +1,6 @@ -require "apartment/adapters/abstract_jdbc_adapter" +# frozen_string_literal: true + +require 'apartment/adapters/abstract_jdbc_adapter' module Apartment module Tenant diff --git a/lib/apartment/adapters/jdbc_postgresql_adapter.rb b/lib/apartment/adapters/jdbc_postgresql_adapter.rb index 5ae8c868..4353cfbf 100644 --- a/lib/apartment/adapters/jdbc_postgresql_adapter.rb +++ b/lib/apartment/adapters/jdbc_postgresql_adapter.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'apartment/adapters/postgresql_adapter' module Apartment @@ -21,7 +23,7 @@ def multi_tenantify_with_tenant_db_name(config, tenant) end def create_tenant_command(conn, tenant) - conn.create_database(environmentify(tenant), :thisisahack => '') + conn.create_database(environmentify(tenant), thisisahack: '') end def rescue_from diff --git a/lib/apartment/adapters/mysql2_adapter.rb b/lib/apartment/adapters/mysql2_adapter.rb index 30fb2c48..d35427ff 100644 --- a/lib/apartment/adapters/mysql2_adapter.rb +++ b/lib/apartment/adapters/mysql2_adapter.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'apartment/adapters/abstract_adapter' module Apartment @@ -48,9 +50,9 @@ def connect_to_new(tenant) return reset if tenant.nil? Apartment.connection.execute "use `#{environmentify(tenant)}`" - rescue ActiveRecord::StatementInvalid => exception + rescue ActiveRecord::StatementInvalid => e Apartment::Tenant.reset - raise_connect_error!(tenant, exception) + raise_connect_error!(tenant, e) end def process_excluded_model(model) diff --git a/lib/apartment/adapters/postgis_adapter.rb b/lib/apartment/adapters/postgis_adapter.rb index f9491476..bcf67be1 100644 --- a/lib/apartment/adapters/postgis_adapter.rb +++ b/lib/apartment/adapters/postgis_adapter.rb @@ -1,6 +1,8 @@ +# frozen_string_literal: true + # handle postgis adapter as if it were postgresql, # only override the adapter_method used for initialization -require "apartment/adapters/postgresql_adapter" +require 'apartment/adapters/postgresql_adapter' module Apartment module Tenant diff --git a/lib/apartment/adapters/postgresql_adapter.rb b/lib/apartment/adapters/postgresql_adapter.rb index 5c51f70f..73a1643e 100644 --- a/lib/apartment/adapters/postgresql_adapter.rb +++ b/lib/apartment/adapters/postgresql_adapter.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'apartment/adapters/abstract_adapter' module Apartment @@ -53,7 +55,7 @@ def process_excluded_model(excluded_model) end def drop_command(conn, tenant) - conn.execute(%{DROP SCHEMA "#{tenant}" CASCADE}) + conn.execute(%(DROP SCHEMA "#{tenant}" CASCADE)) end # Set schema search path to new schema @@ -81,13 +83,13 @@ def connect_to_new(tenant = nil) private def create_tenant_command(conn, tenant) - conn.execute(%{CREATE SCHEMA "#{tenant}"}) + conn.execute(%(CREATE SCHEMA "#{tenant}")) end # Generate the final search path to set including persistent_schemas # def full_search_path - persistent_schemas.map(&:inspect).join(", ") + persistent_schemas.map(&:inspect).join(', ') end def persistent_schemas @@ -127,7 +129,7 @@ def import_database_schema # and it mut be reset # def preserving_search_path - search_path = Apartment.connection.execute("show search_path").first["search_path"] + search_path = Apartment.connection.execute('show search_path').first['search_path'] yield Apartment.connection.execute("set search_path = #{search_path}") end @@ -210,7 +212,7 @@ def swap_schema_qualifier(sql) if Apartment.pg_excluded_names.any? { |name| match.include? name } match else - match.gsub("#{default_tenant}.", %{"#{current}".}) + match.gsub("#{default_tenant}.", %("#{current}".)) end end end diff --git a/lib/apartment/adapters/sqlite3_adapter.rb b/lib/apartment/adapters/sqlite3_adapter.rb index f4e169a6..78f26adc 100644 --- a/lib/apartment/adapters/sqlite3_adapter.rb +++ b/lib/apartment/adapters/sqlite3_adapter.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'apartment/adapters/abstract_adapter' module Apartment @@ -17,7 +19,7 @@ def initialize(config) def drop(tenant) raise TenantNotFound, - "The tenant #{environmentify(tenant)} cannot be found." unless File.exists?(database_file(tenant)) + "The tenant #{environmentify(tenant)} cannot be found." unless File.exist?(database_file(tenant)) File.delete(database_file(tenant)) end @@ -30,14 +32,14 @@ def current def connect_to_new(tenant) raise TenantNotFound, - "The tenant #{environmentify(tenant)} cannot be found." unless File.exists?(database_file(tenant)) + "The tenant #{environmentify(tenant)} cannot be found." unless File.exist?(database_file(tenant)) super database_file(tenant) end def create_tenant(tenant) raise TenantExists, - "The tenant #{environmentify(tenant)} already exists." if File.exists?(database_file(tenant)) + "The tenant #{environmentify(tenant)} already exists." if File.exist?(database_file(tenant)) begin f = File.new(database_file(tenant), File::CREAT) diff --git a/lib/apartment/console.rb b/lib/apartment/console.rb index b84648ed..ee1220a8 100644 --- a/lib/apartment/console.rb +++ b/lib/apartment/console.rb @@ -1,9 +1,11 @@ +# frozen_string_literal: true + # A workaraound to get `reload!` to also call Apartment::Tenant.init # This is unfortunate, but I haven't figured out how to hook into the reload process *after* files are reloaded # reloads the environment def reload!(print = true) - puts "Reloading..." if print + puts 'Reloading...' if print # This triggers the to_prepare callbacks ActionDispatch::Callbacks.new(proc {}).call({}) # Manually init Apartment again once classes are reloaded diff --git a/lib/apartment/deprecation.rb b/lib/apartment/deprecation.rb index 3355f802..db73dd5d 100644 --- a/lib/apartment/deprecation.rb +++ b/lib/apartment/deprecation.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'active_support/deprecation' module Apartment diff --git a/lib/apartment/elevators/domain.rb b/lib/apartment/elevators/domain.rb index 9c6862ad..8915289a 100644 --- a/lib/apartment/elevators/domain.rb +++ b/lib/apartment/elevators/domain.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'apartment/elevators/generic' module Apartment @@ -14,7 +16,7 @@ class Domain < Generic def parse_tenant_name(request) return nil if request.host.blank? - request.host.match(/(www\.)?(?[^.]*)/)["sld"] + request.host.match(/(www\.)?(?[^.]*)/)['sld'] end end end diff --git a/lib/apartment/elevators/first_subdomain.rb b/lib/apartment/elevators/first_subdomain.rb index e53fa4b9..20ef3e68 100644 --- a/lib/apartment/elevators/first_subdomain.rb +++ b/lib/apartment/elevators/first_subdomain.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'apartment/elevators/subdomain' module Apartment diff --git a/lib/apartment/elevators/generic.rb b/lib/apartment/elevators/generic.rb index 716db5dc..8d321aaa 100644 --- a/lib/apartment/elevators/generic.rb +++ b/lib/apartment/elevators/generic.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'rack/request' require 'apartment/tenant' @@ -24,7 +26,7 @@ def call(env) end def parse_tenant_name(request) - raise "Override" + raise 'Override' end end end diff --git a/lib/apartment/elevators/host.rb b/lib/apartment/elevators/host.rb index 3a9f8da6..283ad316 100644 --- a/lib/apartment/elevators/host.rb +++ b/lib/apartment/elevators/host.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'apartment/elevators/generic' module Apartment diff --git a/lib/apartment/elevators/host_hash.rb b/lib/apartment/elevators/host_hash.rb index 7838d671..306f0319 100644 --- a/lib/apartment/elevators/host_hash.rb +++ b/lib/apartment/elevators/host_hash.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'apartment/elevators/generic' module Apartment diff --git a/lib/apartment/elevators/subdomain.rb b/lib/apartment/elevators/subdomain.rb index 5716a168..60e9d022 100644 --- a/lib/apartment/elevators/subdomain.rb +++ b/lib/apartment/elevators/subdomain.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'apartment/elevators/generic' require 'public_suffix' diff --git a/lib/apartment/migrator.rb b/lib/apartment/migrator.rb index 27f6714c..089109df 100644 --- a/lib/apartment/migrator.rb +++ b/lib/apartment/migrator.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'apartment/tenant' module Apartment @@ -7,9 +9,9 @@ module Migrator # Migrate to latest def migrate(database) Tenant.switch(database) do - version = ENV["VERSION"] ? ENV["VERSION"].to_i : nil + version = ENV['VERSION'] ? ENV['VERSION'].to_i : nil - migration_scope_block = ->(migration) { ENV["SCOPE"].blank? || (ENV["SCOPE"] == migration.scope) } + migration_scope_block = ->(migration) { ENV['SCOPE'].blank? || (ENV['SCOPE'] == migration.scope) } if activerecord_below_5_2? ActiveRecord::Migrator.migrate(ActiveRecord::Migrator.migrations_paths, version, &migration_scope_block) diff --git a/lib/apartment/railtie.rb b/lib/apartment/railtie.rb index beba954c..43bc54a5 100644 --- a/lib/apartment/railtie.rb +++ b/lib/apartment/railtie.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'rails' require 'apartment/tenant' require 'apartment/reloader' @@ -32,10 +34,12 @@ class Railtie < Rails::Railtie Apartment.connection_class.connection_pool.with_connection do Apartment::Tenant.init end + # rubocop:disable Lint/SuppressedException rescue ::ActiveRecord::NoDatabaseError # Since `db:create` and other tasks invoke this block from Rails 5.2.0, # we need to swallow the error to execute `db:create` properly. end + # rubocop:enable Lint/SuppressedException end # diff --git a/lib/apartment/reloader.rb b/lib/apartment/reloader.rb index 164c838e..cd8b6861 100644 --- a/lib/apartment/reloader.rb +++ b/lib/apartment/reloader.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Apartment class Reloader # Middleware used in development to init Apartment for each request diff --git a/lib/apartment/tasks/enhancements.rb b/lib/apartment/tasks/enhancements.rb index dff7099b..95e32ab7 100644 --- a/lib/apartment/tasks/enhancements.rb +++ b/lib/apartment/tasks/enhancements.rb @@ -1,11 +1,13 @@ +# frozen_string_literal: true + # Require this file to append Apartment rake tasks to ActiveRecord db rake tasks # Enabled by default in the initializer module Apartment class RakeTaskEnhancer module TASKS - ENHANCE_BEFORE = %w(db:drop) - ENHANCE_AFTER = %w(db:migrate db:rollback db:migrate:up db:migrate:down db:migrate:redo db:seed) + ENHANCE_BEFORE = %w[db:drop] + ENHANCE_AFTER = %w[db:migrate db:rollback db:migrate:up db:migrate:down db:migrate:redo db:seed] freeze end diff --git a/lib/apartment/tenant.rb b/lib/apartment/tenant.rb index 6e1e713b..93e755a0 100644 --- a/lib/apartment/tenant.rb +++ b/lib/apartment/tenant.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'forwardable' module Apartment diff --git a/lib/apartment/version.rb b/lib/apartment/version.rb index 25f180b1..8f84b610 100644 --- a/lib/apartment/version.rb +++ b/lib/apartment/version.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Apartment - VERSION = "2.3.0" + VERSION = '2.3.0' end diff --git a/lib/generators/apartment/install/install_generator.rb b/lib/generators/apartment/install/install_generator.rb index 079a732b..509cf2e9 100755 --- a/lib/generators/apartment/install/install_generator.rb +++ b/lib/generators/apartment/install/install_generator.rb @@ -1,9 +1,11 @@ +# frozen_string_literal: true + module Apartment class InstallGenerator < Rails::Generators::Base source_root File.expand_path('../templates', __FILE__) def copy_files - template "apartment.rb", File.join("config", "initializers", "apartment.rb") + template 'apartment.rb', File.join('config', 'initializers', 'apartment.rb') end end end diff --git a/lib/generators/apartment/install/templates/apartment.rb b/lib/generators/apartment/install/templates/apartment.rb index 251033bf..2a774737 100644 --- a/lib/generators/apartment/install/templates/apartment.rb +++ b/lib/generators/apartment/install/templates/apartment.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + # You can have Apartment route to the appropriate Tenant by adding some Rack middleware. # Apartment can support many different "Elevators" that can take care of this routing to your data. # Require whichever Elevator you're using below or none if you have a custom one. @@ -47,7 +49,7 @@ # end # end # - config.tenant_names = lambda { ToDo_Tenant_Or_User_Model.pluck :database } + config.tenant_names = -> { ToDo_Tenant_Or_User_Model.pluck :database } # PostgreSQL: # Specifies whether to use PostgreSQL schemas or create a new database per Tenant. diff --git a/lib/tasks/apartment.rake b/lib/tasks/apartment.rake index ff736393..10c4613a 100644 --- a/lib/tasks/apartment.rake +++ b/lib/tasks/apartment.rake @@ -1,8 +1,10 @@ +# frozen_string_literal: true + require 'apartment/migrator' require 'parallel' apartment_namespace = namespace :apartment do - desc "Create all tenants" + desc 'Create all tenants' task :create do tenants.each do |tenant| begin @@ -14,7 +16,7 @@ apartment_namespace = namespace :apartment do end end - desc "Drop all tenants" + desc 'Drop all tenants' task :drop do tenants.each do |tenant| begin @@ -26,7 +28,7 @@ apartment_namespace = namespace :apartment do end end - desc "Migrate all tenants" + desc 'Migrate all tenants' task :migrate do warn_if_tenants_empty each_tenant do |tenant| @@ -39,7 +41,7 @@ apartment_namespace = namespace :apartment do end end - desc "Seed all tenants" + desc 'Seed all tenants' task :seed do warn_if_tenants_empty @@ -55,7 +57,7 @@ apartment_namespace = namespace :apartment do end end - desc "Rolls the migration back to the previous version (specify steps w/ STEP=n) across all tenants." + desc 'Rolls the migration back to the previous version (specify steps w/ STEP=n) across all tenants.' task :rollback do warn_if_tenants_empty @@ -129,7 +131,7 @@ apartment_namespace = namespace :apartment do end def warn_if_tenants_empty - if tenants.empty? && ENV['IGNORE_EMPTY_TENANTS'] != "true" + if tenants.empty? && ENV['IGNORE_EMPTY_TENANTS'] != 'true' puts <<-WARNING [WARNING] - The list of tenants to migrate appears to be empty. This could mean a few things: diff --git a/spec/adapters/jdbc_mysql_adapter_spec.rb b/spec/adapters/jdbc_mysql_adapter_spec.rb index 1fb5d433..4833f93d 100644 --- a/spec/adapters/jdbc_mysql_adapter_spec.rb +++ b/spec/adapters/jdbc_mysql_adapter_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + if defined?(JRUBY_VERSION) require 'spec_helper' @@ -7,12 +9,12 @@ subject { Apartment::Tenant.jdbc_mysql_adapter config.symbolize_keys } def tenant_names - ActiveRecord::Base.connection.execute("SELECT schema_name FROM information_schema.schemata").collect { |row| row['schema_name'] } + ActiveRecord::Base.connection.execute('SELECT schema_name FROM information_schema.schemata').collect { |row| row['schema_name'] } end 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_should_behave_like 'a generic apartment adapter' + it_should_behave_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 d595a7d9..49dfaf1a 100644 --- a/spec/adapters/jdbc_postgresql_adapter_spec.rb +++ b/spec/adapters/jdbc_postgresql_adapter_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + if defined?(JRUBY_VERSION) require 'spec_helper' @@ -6,32 +8,32 @@ describe Apartment::Adapters::JDBCPostgresqlAdapter, database: :postgresql do subject { Apartment::Tenant.jdbc_postgresql_adapter config.symbolize_keys } - context "using schemas" do + context '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 def tenant_names - ActiveRecord::Base.connection.execute("SELECT nspname FROM pg_namespace;").collect { |row| row['nspname'] } + 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('"', '') } } - it_should_behave_like "a generic apartment adapter" - it_should_behave_like "a schema based apartment adapter" + it_should_behave_like 'a generic apartment adapter' + it_should_behave_like 'a schema based apartment adapter' end - context "using databases" do + context '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 def tenant_names - connection.execute("select datname from pg_database;").collect { |row| row['datname'] } + connection.execute('select datname from pg_database;').collect { |row| row['datname'] } end 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_should_behave_like 'a generic apartment adapter' + it_should_behave_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 831affed..29529514 100644 --- a/spec/adapters/mysql2_adapter_spec.rb +++ b/spec/adapters/mysql2_adapter_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' require 'apartment/adapters/mysql2_adapter' @@ -7,28 +9,28 @@ subject(:adapter) { Apartment::Tenant.mysql2_adapter config } def tenant_names - ActiveRecord::Base.connection.execute("SELECT schema_name FROM information_schema.schemata").collect { |row| row[0] } + ActiveRecord::Base.connection.execute('SELECT schema_name FROM information_schema.schemata').collect { |row| row[0] } end let(:default_tenant) { subject.switch { ActiveRecord::Base.connection.current_database } } - context "using - the equivalent of - schemas" do + context 'using - the equivalent of - schemas' do before { Apartment.use_schemas = true } - it_should_behave_like "a generic apartment adapter" + it_should_behave_like 'a generic apartment adapter' - describe "#default_tenant" do - it "is set to the original db from config" do + describe '#default_tenant' do + it 'is set to the original db from config' do expect(subject.default_tenant).to eq(config[:database]) end end - describe "#init" do + describe '#init' do include Apartment::Spec::AdapterRequirements before do Apartment.configure do |config| - config.excluded_models = ["Company"] + config.excluded_models = ['Company'] end end @@ -40,7 +42,7 @@ def tenant_names end end - it "should process model exclusions" do + it 'should process model exclusions' do Apartment::Tenant.init expect(Company.table_name).to eq("#{default_tenant}.companies") @@ -48,12 +50,12 @@ def tenant_names end end - context "using connections" do + 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_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' end end end diff --git a/spec/adapters/postgresql_adapter_spec.rb b/spec/adapters/postgresql_adapter_spec.rb index 7802a7bd..d553ff8b 100644 --- a/spec/adapters/postgresql_adapter_spec.rb +++ b/spec/adapters/postgresql_adapter_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' require 'apartment/adapters/postgresql_adapter' @@ -6,21 +8,21 @@ subject { Apartment::Tenant.postgresql_adapter config } - context "using schemas with schema.rb" do + context '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 def tenant_names - ActiveRecord::Base.connection.execute("SELECT nspname FROM pg_namespace;").collect { |row| row['nspname'] } + 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('"', '') } } - it_should_behave_like "a generic apartment adapter" - it_should_behave_like "a schema based apartment adapter" + it_should_behave_like 'a generic apartment adapter' + it_should_behave_like 'a schema based apartment adapter' end - context "using schemas with SQL dump" do + context 'using schemas with SQL dump' do before do Apartment.use_schemas = true Apartment.use_sql = true @@ -28,13 +30,13 @@ def tenant_names # 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'] } + 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('"', '') } } - it_should_behave_like "a generic apartment adapter" - it_should_behave_like "a schema based apartment adapter" + it_should_behave_like 'a generic apartment adapter' + it_should_behave_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 @@ -43,19 +45,19 @@ def tenant_names after { Apartment::Tenant.drop('has-dashes') if Apartment.connection.schema_exists? 'has-dashes' } end - context "using connections" do + context '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 def tenant_names - connection.execute("select datname from pg_database;").collect { |row| row['datname'] } + connection.execute('select datname from pg_database;').collect { |row| row['datname'] } end 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_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' end end end diff --git a/spec/adapters/sqlite3_adapter_spec.rb b/spec/adapters/sqlite3_adapter_spec.rb index 92246249..40388acd 100644 --- a/spec/adapters/sqlite3_adapter_spec.rb +++ b/spec/adapters/sqlite3_adapter_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' require 'apartment/adapters/sqlite3_adapter' @@ -6,9 +8,9 @@ subject { Apartment::Tenant.sqlite3_adapter config } - context "using connections" do + context 'using connections' do def tenant_names - db_dir = File.expand_path("../../dummy/db", __FILE__) + db_dir = File.expand_path('../../dummy/db', __FILE__) Dir.glob("#{db_dir}/*.sqlite3").map { |file| File.basename(file, '.sqlite3') } end @@ -16,18 +18,18 @@ 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_should_behave_like 'a generic apartment adapter' + it_should_behave_like 'a connection based apartment adapter' after(:all) do File.delete(Apartment::Test.config['connections']['sqlite']['database']) end end - context "with prepend and append" do + context 'with prepend and append' do let(:default_dir) { File.expand_path(File.dirname(config[:database])) } - describe "#prepend" do - let (:db_name) { "db_with_prefix" } + describe '#prepend' do + let(:db_name) { 'db_with_prefix' } before do Apartment.configure do |config| config.prepend_environment = true @@ -43,15 +45,15 @@ def tenant_names end end - it "should create a new database" do + it 'should create a new database' do subject.create db_name - expect(File.exists?("#{default_dir}/#{Rails.env}_#{db_name}.sqlite3")).to eq true + expect(File.exist?("#{default_dir}/#{Rails.env}_#{db_name}.sqlite3")).to eq true end end - describe "#neither" do - let (:db_name) { "db_without_prefix_suffix" } + describe '#neither' do + let(:db_name) { 'db_without_prefix_suffix' } before do Apartment.configure { |config| config.prepend_environment = config.append_environment = false } end @@ -64,15 +66,15 @@ def tenant_names end end - it "should create a new database" do + it 'should create a new database' do subject.create db_name - expect(File.exists?("#{default_dir}/#{db_name}.sqlite3")).to eq true + expect(File.exist?("#{default_dir}/#{db_name}.sqlite3")).to eq true end end - describe "#append" do - let (:db_name) { "db_with_suffix" } + describe '#append' do + let(:db_name) { 'db_with_suffix' } before do Apartment.configure do |config| config.prepend_environment = false @@ -88,10 +90,10 @@ def tenant_names end end - it "should create a new database" do + it 'should create a new database' do subject.create db_name - expect(File.exists?("#{default_dir}/#{db_name}_#{Rails.env}.sqlite3")).to eq true + expect(File.exist?("#{default_dir}/#{db_name}_#{Rails.env}.sqlite3")).to eq true end end end diff --git a/spec/apartment_spec.rb b/spec/apartment_spec.rb index b00e0b2d..f90a09f2 100644 --- a/spec/apartment_spec.rb +++ b/spec/apartment_spec.rb @@ -1,11 +1,13 @@ +# frozen_string_literal: true + require 'spec_helper' describe Apartment do - it "should be valid" do + it 'should be valid' do expect(Apartment).to be_a(Module) end - it "should be a valid app" do + it 'should be a valid app' do expect(::Rails.application).to be_a(Dummy::Application) end end diff --git a/spec/dummy/app/controllers/application_controller.rb b/spec/dummy/app/controllers/application_controller.rb index 9ec13342..bfa2456c 100644 --- a/spec/dummy/app/controllers/application_controller.rb +++ b/spec/dummy/app/controllers/application_controller.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class ApplicationController < ActionController::Base protect_from_forgery diff --git a/spec/dummy/app/helpers/application_helper.rb b/spec/dummy/app/helpers/application_helper.rb index de6be794..15b06f0f 100644 --- a/spec/dummy/app/helpers/application_helper.rb +++ b/spec/dummy/app/helpers/application_helper.rb @@ -1,2 +1,4 @@ +# frozen_string_literal: true + module ApplicationHelper end diff --git a/spec/dummy/app/models/company.rb b/spec/dummy/app/models/company.rb index f97ca639..96986b7d 100644 --- a/spec/dummy/app/models/company.rb +++ b/spec/dummy/app/models/company.rb @@ -1,3 +1,5 @@ -class Company < ActiveRecord::Base +# frozen_string_literal: true + +class Company < ApplicationRecord # Dummy models end diff --git a/spec/dummy/app/models/user.rb b/spec/dummy/app/models/user.rb index 61e0ee5f..463ce352 100644 --- a/spec/dummy/app/models/user.rb +++ b/spec/dummy/app/models/user.rb @@ -1,3 +1,5 @@ -class User < ActiveRecord::Base +# frozen_string_literal: true + +class User < ApplicationRecord # Dummy models end diff --git a/spec/dummy/config.ru b/spec/dummy/config.ru index cbd74159..989f7c64 100644 --- a/spec/dummy/config.ru +++ b/spec/dummy/config.ru @@ -1,3 +1,5 @@ +# frozen_string_literal: true + # This file is used by Rack-based servers to start the application. require ::File.expand_path('../config/environment', __FILE__) diff --git a/spec/dummy/config/application.rb b/spec/dummy/config/application.rb index e8f51ed9..c633c115 100644 --- a/spec/dummy/config/application.rb +++ b/spec/dummy/config/application.rb @@ -1,13 +1,15 @@ +# frozen_string_literal: true + require File.expand_path('../boot', __FILE__) -require "active_model/railtie" -require "active_record/railtie" -require "action_controller/railtie" -require "action_view/railtie" -require "action_mailer/railtie" +require 'active_model/railtie' +require 'active_record/railtie' +require 'action_controller/railtie' +require 'action_view/railtie' +require 'action_mailer/railtie' Bundler.require -require "apartment" +require 'apartment' module Dummy class Application < Rails::Application @@ -20,7 +22,7 @@ class Application < Rails::Application config.middleware.use Apartment::Elevators::Subdomain # Custom directories with classes and modules you want to be autoloadable. - config.autoload_paths += %W(#{config.root}/lib) + config.autoload_paths += %W[#{config.root}/lib] # Only load the plugins named here, in the order given (default is alphabetical). # :all can be used as a placeholder for all plugins not explicitly named. @@ -41,7 +43,7 @@ class Application < Rails::Application # config.action_view.javascript_expansions[:defaults] = %w(jquery rails) # Configure the default encoding used in templates for Ruby 1.9. - config.encoding = "utf-8" + config.encoding = 'utf-8' # Configure sensitive parameters which will be filtered from the log file. config.filter_parameters += [:password] diff --git a/spec/dummy/config/boot.rb b/spec/dummy/config/boot.rb index 58dd2d36..1f976a16 100644 --- a/spec/dummy/config/boot.rb +++ b/spec/dummy/config/boot.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'rubygems' gemfile = File.expand_path('../../../../Gemfile', __FILE__) diff --git a/spec/dummy/config/environment.rb b/spec/dummy/config/environment.rb index 3da5eb91..1a1e336f 100644 --- a/spec/dummy/config/environment.rb +++ b/spec/dummy/config/environment.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + # Load the rails application require File.expand_path('../application', __FILE__) diff --git a/spec/dummy/config/environments/development.rb b/spec/dummy/config/environments/development.rb index 229d545c..77567be6 100644 --- a/spec/dummy/config/environments/development.rb +++ b/spec/dummy/config/environments/development.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + Dummy::Application.configure do # Settings specified here will take precedence over those in config/application.rb diff --git a/spec/dummy/config/environments/production.rb b/spec/dummy/config/environments/production.rb index a71b06c4..1d9b71e1 100644 --- a/spec/dummy/config/environments/production.rb +++ b/spec/dummy/config/environments/production.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + Dummy::Application.configure do # Settings specified here will take precedence over those in config/application.rb @@ -12,7 +14,7 @@ config.action_controller.perform_caching = true # Specifies the header that your server uses for sending files - config.action_dispatch.x_sendfile_header = "X-Sendfile" + config.action_dispatch.x_sendfile_header = 'X-Sendfile' # For nginx: # config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' diff --git a/spec/dummy/config/environments/test.rb b/spec/dummy/config/environments/test.rb index e3634079..3044c445 100644 --- a/spec/dummy/config/environments/test.rb +++ b/spec/dummy/config/environments/test.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + Dummy::Application.configure do # Settings specified here will take precedence over those in config/application.rb diff --git a/spec/dummy/config/initializers/apartment.rb b/spec/dummy/config/initializers/apartment.rb index d7d0fcfc..7db9fd26 100644 --- a/spec/dummy/config/initializers/apartment.rb +++ b/spec/dummy/config/initializers/apartment.rb @@ -1,4 +1,6 @@ +# frozen_string_literal: true + Apartment.configure do |config| - config.excluded_models = ["Company"] - config.tenant_names = lambda { Company.pluck(:database) } + config.excluded_models = ['Company'] + config.tenant_names = -> { Company.pluck(:database) } end diff --git a/spec/dummy/config/initializers/backtrace_silencers.rb b/spec/dummy/config/initializers/backtrace_silencers.rb index 59385cdf..d0f0d3b5 100644 --- a/spec/dummy/config/initializers/backtrace_silencers.rb +++ b/spec/dummy/config/initializers/backtrace_silencers.rb @@ -1,3 +1,4 @@ +# frozen_string_literal: true # Be sure to restart your server when you modify this file. # You can add backtrace silencers for libraries that you're using but don't wish to see in your backtraces. diff --git a/spec/dummy/config/initializers/inflections.rb b/spec/dummy/config/initializers/inflections.rb index 9e8b0131..8138cabc 100644 --- a/spec/dummy/config/initializers/inflections.rb +++ b/spec/dummy/config/initializers/inflections.rb @@ -1,3 +1,4 @@ +# frozen_string_literal: true # Be sure to restart your server when you modify this file. # Add new inflection rules using the following format diff --git a/spec/dummy/config/initializers/mime_types.rb b/spec/dummy/config/initializers/mime_types.rb index 72aca7e4..f75864f9 100644 --- a/spec/dummy/config/initializers/mime_types.rb +++ b/spec/dummy/config/initializers/mime_types.rb @@ -1,3 +1,4 @@ +# frozen_string_literal: true # Be sure to restart your server when you modify this file. # Add new mime types for use in respond_to blocks: diff --git a/spec/dummy/config/initializers/secret_token.rb b/spec/dummy/config/initializers/secret_token.rb index 1c651c86..2c7f52bd 100644 --- a/spec/dummy/config/initializers/secret_token.rb +++ b/spec/dummy/config/initializers/secret_token.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + # Be sure to restart your server when you modify this file. # Your secret key for verifying the integrity of signed cookies. diff --git a/spec/dummy/config/initializers/session_store.rb b/spec/dummy/config/initializers/session_store.rb index aa2f5129..66099cf5 100644 --- a/spec/dummy/config/initializers/session_store.rb +++ b/spec/dummy/config/initializers/session_store.rb @@ -1,6 +1,8 @@ +# frozen_string_literal: true + # Be sure to restart your server when you modify this file. -Dummy::Application.config.session_store :cookie_store, :key => '_dummy_session' +Dummy::Application.config.session_store :cookie_store, key: '_dummy_session' # Use the database for sessions instead of the cookie-based default, # which shouldn't be used to store highly confidential information diff --git a/spec/dummy/config/routes.rb b/spec/dummy/config/routes.rb index 99d3e5c1..d0be0e6d 100644 --- a/spec/dummy/config/routes.rb +++ b/spec/dummy/config/routes.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + Dummy::Application.routes.draw do - root :to => 'application#index' + root to: 'application#index' end diff --git a/spec/dummy/db/seeds/import.rb b/spec/dummy/db/seeds/import.rb index 4e2eb81b..10b479b9 100644 --- a/spec/dummy/db/seeds/import.rb +++ b/spec/dummy/db/seeds/import.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + def create_users 6.times { |x| User.where(name: "Different User #{x}").first_or_create! } end diff --git a/spec/dummy/script/rails b/spec/dummy/script/rails index bd79dce5..1a08aed0 100755 --- a/spec/dummy/script/rails +++ b/spec/dummy/script/rails @@ -1,4 +1,6 @@ #!/usr/bin/env ruby +# frozen_string_literal: true + # This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application. APP_PATH = File.expand_path('../../config/application', __FILE__) diff --git a/spec/dummy_engine/config/initializers/apartment.rb b/spec/dummy_engine/config/initializers/apartment.rb index b94a7f1f..466f3d87 100644 --- a/spec/dummy_engine/config/initializers/apartment.rb +++ b/spec/dummy_engine/config/initializers/apartment.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + # Require whichever elevator you're using below here... # # require 'apartment/elevators/generic' @@ -16,7 +18,7 @@ # # config.excluded_models = %w{Tenant} # - config.excluded_models = %w{} + config.excluded_models = %w[] # use postgres schemas? config.use_schemas = true diff --git a/spec/dummy_engine/lib/dummy_engine.rb b/spec/dummy_engine/lib/dummy_engine.rb index b1a14145..8f9c8111 100644 --- a/spec/dummy_engine/lib/dummy_engine.rb +++ b/spec/dummy_engine/lib/dummy_engine.rb @@ -1,4 +1,6 @@ -require "dummy_engine/engine" +# frozen_string_literal: true + +require 'dummy_engine/engine' module DummyEngine end diff --git a/spec/dummy_engine/lib/dummy_engine/engine.rb b/spec/dummy_engine/lib/dummy_engine/engine.rb index cc8821c3..d308ec0d 100644 --- a/spec/dummy_engine/lib/dummy_engine/engine.rb +++ b/spec/dummy_engine/lib/dummy_engine/engine.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module DummyEngine class Engine < ::Rails::Engine end diff --git a/spec/dummy_engine/lib/dummy_engine/version.rb b/spec/dummy_engine/lib/dummy_engine/version.rb index 82d2c8b8..76d025df 100644 --- a/spec/dummy_engine/lib/dummy_engine/version.rb +++ b/spec/dummy_engine/lib/dummy_engine/version.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module DummyEngine - VERSION = "0.0.1" + VERSION = '0.0.1' end diff --git a/spec/dummy_engine/test/dummy/config.ru b/spec/dummy_engine/test/dummy/config.ru index bd83b254..61c04e13 100644 --- a/spec/dummy_engine/test/dummy/config.ru +++ b/spec/dummy_engine/test/dummy/config.ru @@ -1,3 +1,5 @@ +# frozen_string_literal: true + # This file is used by Rack-based servers to start the application. require ::File.expand_path('../config/environment', __FILE__) diff --git a/spec/dummy_engine/test/dummy/config/application.rb b/spec/dummy_engine/test/dummy/config/application.rb index 5ef001e9..2562455c 100644 --- a/spec/dummy_engine/test/dummy/config/application.rb +++ b/spec/dummy_engine/test/dummy/config/application.rb @@ -1,9 +1,11 @@ +# frozen_string_literal: true + require File.expand_path('../boot', __FILE__) require 'rails/all' Bundler.require(*Rails.groups) -require "dummy_engine" +require 'dummy_engine' module Dummy class Application < Rails::Application diff --git a/spec/dummy_engine/test/dummy/config/boot.rb b/spec/dummy_engine/test/dummy/config/boot.rb index 6266cfc5..18536532 100644 --- a/spec/dummy_engine/test/dummy/config/boot.rb +++ b/spec/dummy_engine/test/dummy/config/boot.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + # Set up gems listed in the Gemfile. ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../../../Gemfile', __FILE__) diff --git a/spec/dummy_engine/test/dummy/config/environment.rb b/spec/dummy_engine/test/dummy/config/environment.rb index ee8d90dc..3fa66218 100644 --- a/spec/dummy_engine/test/dummy/config/environment.rb +++ b/spec/dummy_engine/test/dummy/config/environment.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + # Load the Rails application. require File.expand_path('../application', __FILE__) diff --git a/spec/dummy_engine/test/dummy/config/environments/development.rb b/spec/dummy_engine/test/dummy/config/environments/development.rb index ddf0e90c..8296624e 100644 --- a/spec/dummy_engine/test/dummy/config/environments/development.rb +++ b/spec/dummy_engine/test/dummy/config/environments/development.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + Rails.application.configure do # Settings specified here will take precedence over those in config/application.rb. diff --git a/spec/dummy_engine/test/dummy/config/environments/production.rb b/spec/dummy_engine/test/dummy/config/environments/production.rb index b93a877c..1bd152f1 100644 --- a/spec/dummy_engine/test/dummy/config/environments/production.rb +++ b/spec/dummy_engine/test/dummy/config/environments/production.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + Rails.application.configure do # Settings specified here will take precedence over those in config/application.rb. diff --git a/spec/dummy_engine/test/dummy/config/environments/test.rb b/spec/dummy_engine/test/dummy/config/environments/test.rb index 053f5b66..bd942389 100644 --- a/spec/dummy_engine/test/dummy/config/environments/test.rb +++ b/spec/dummy_engine/test/dummy/config/environments/test.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + Rails.application.configure do # Settings specified here will take precedence over those in config/application.rb. diff --git a/spec/dummy_engine/test/dummy/config/initializers/assets.rb b/spec/dummy_engine/test/dummy/config/initializers/assets.rb index d2f4ec33..761905a7 100644 --- a/spec/dummy_engine/test/dummy/config/initializers/assets.rb +++ b/spec/dummy_engine/test/dummy/config/initializers/assets.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + # Be sure to restart your server when you modify this file. # Version of your assets, change this if you want to expire all your assets. diff --git a/spec/dummy_engine/test/dummy/config/initializers/backtrace_silencers.rb b/spec/dummy_engine/test/dummy/config/initializers/backtrace_silencers.rb index 59385cdf..d0f0d3b5 100644 --- a/spec/dummy_engine/test/dummy/config/initializers/backtrace_silencers.rb +++ b/spec/dummy_engine/test/dummy/config/initializers/backtrace_silencers.rb @@ -1,3 +1,4 @@ +# frozen_string_literal: true # Be sure to restart your server when you modify this file. # You can add backtrace silencers for libraries that you're using but don't wish to see in your backtraces. diff --git a/spec/dummy_engine/test/dummy/config/initializers/cookies_serializer.rb b/spec/dummy_engine/test/dummy/config/initializers/cookies_serializer.rb index 7f70458d..0a23b25e 100644 --- a/spec/dummy_engine/test/dummy/config/initializers/cookies_serializer.rb +++ b/spec/dummy_engine/test/dummy/config/initializers/cookies_serializer.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + # Be sure to restart your server when you modify this file. Rails.application.config.action_dispatch.cookies_serializer = :json diff --git a/spec/dummy_engine/test/dummy/config/initializers/filter_parameter_logging.rb b/spec/dummy_engine/test/dummy/config/initializers/filter_parameter_logging.rb index 4a994e1e..7a4f47b4 100644 --- a/spec/dummy_engine/test/dummy/config/initializers/filter_parameter_logging.rb +++ b/spec/dummy_engine/test/dummy/config/initializers/filter_parameter_logging.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + # Be sure to restart your server when you modify this file. # Configure sensitive parameters which will be filtered from the log file. diff --git a/spec/dummy_engine/test/dummy/config/initializers/inflections.rb b/spec/dummy_engine/test/dummy/config/initializers/inflections.rb index ac033bf9..aa7435fb 100644 --- a/spec/dummy_engine/test/dummy/config/initializers/inflections.rb +++ b/spec/dummy_engine/test/dummy/config/initializers/inflections.rb @@ -1,3 +1,4 @@ +# frozen_string_literal: true # Be sure to restart your server when you modify this file. # Add new inflection rules using the following format. Inflections diff --git a/spec/dummy_engine/test/dummy/config/initializers/mime_types.rb b/spec/dummy_engine/test/dummy/config/initializers/mime_types.rb index dc189968..6e1d16f0 100644 --- a/spec/dummy_engine/test/dummy/config/initializers/mime_types.rb +++ b/spec/dummy_engine/test/dummy/config/initializers/mime_types.rb @@ -1,3 +1,4 @@ +# frozen_string_literal: true # Be sure to restart your server when you modify this file. # Add new mime types for use in respond_to blocks: diff --git a/spec/dummy_engine/test/dummy/config/initializers/session_store.rb b/spec/dummy_engine/test/dummy/config/initializers/session_store.rb index e766b67b..969d977f 100644 --- a/spec/dummy_engine/test/dummy/config/initializers/session_store.rb +++ b/spec/dummy_engine/test/dummy/config/initializers/session_store.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + # Be sure to restart your server when you modify this file. Rails.application.config.session_store :cookie_store, key: '_dummy_session' diff --git a/spec/dummy_engine/test/dummy/config/initializers/wrap_parameters.rb b/spec/dummy_engine/test/dummy/config/initializers/wrap_parameters.rb index 33725e95..246168a4 100644 --- a/spec/dummy_engine/test/dummy/config/initializers/wrap_parameters.rb +++ b/spec/dummy_engine/test/dummy/config/initializers/wrap_parameters.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + # Be sure to restart your server when you modify this file. # This file contains settings for ActionController::ParamsWrapper which diff --git a/spec/dummy_engine/test/dummy/config/routes.rb b/spec/dummy_engine/test/dummy/config/routes.rb index 3f66539d..189947fc 100644 --- a/spec/dummy_engine/test/dummy/config/routes.rb +++ b/spec/dummy_engine/test/dummy/config/routes.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + Rails.application.routes.draw do # The priority is based upon order of creation: first created -> highest priority. # See how all your routes lay out with "rake routes". diff --git a/spec/examples/connection_adapter_examples.rb b/spec/examples/connection_adapter_examples.rb index b335e182..033ded4f 100644 --- a/spec/examples/connection_adapter_examples.rb +++ b/spec/examples/connection_adapter_examples.rb @@ -1,11 +1,13 @@ +# frozen_string_literal: true + require 'spec_helper' -shared_examples_for "a connection based apartment adapter" do +shared_examples_for 'a connection based apartment adapter' do include Apartment::Spec::AdapterRequirements let(:default_tenant) { subject.switch { ActiveRecord::Base.connection.current_database } } - describe "#init" do + describe '#init' do after do # Apartment::Tenant.init creates per model connection. # Remove the connection after testing not to unintentionally keep the connection across tests. @@ -14,9 +16,9 @@ end end - it "should process model exclusions" do + it 'should process model exclusions' do Apartment.configure do |config| - config.excluded_models = ["Company"] + config.excluded_models = ['Company'] end Apartment::Tenant.init @@ -24,16 +26,16 @@ end end - describe "#drop" do - it "should raise an error for unknown database" do + describe '#drop' do + it 'should raise an error for unknown database' do expect { subject.drop 'unknown_database' }.to raise_error(Apartment::TenantNotFound) end end - describe "#switch!" do - it "should raise an error if database is invalid" do + describe '#switch!' do + it 'should raise an error if database is invalid' do expect { subject.switch! 'unknown_database' }.to raise_error(Apartment::TenantNotFound) diff --git a/spec/examples/generic_adapter_custom_configuration_example.rb b/spec/examples/generic_adapter_custom_configuration_example.rb index 27ca1c84..a5fece4c 100644 --- a/spec/examples/generic_adapter_custom_configuration_example.rb +++ b/spec/examples/generic_adapter_custom_configuration_example.rb @@ -1,11 +1,13 @@ +# frozen_string_literal: true + require 'spec_helper' -shared_examples_for "a generic apartment adapter able to handle custom configuration" do +shared_examples_for 'a generic apartment adapter able to handle custom configuration' do let(:custom_tenant_name) { 'test_tenantwwww' } let(:db) { |example| example.metadata[:database] } let(:custom_tenant_names) do { - custom_tenant_name => get_custom_db_conf + custom_tenant_name => custom_db_conf } end @@ -18,11 +20,11 @@ Apartment.with_multi_server_setup = false end - context "database key taken from specific config" do - let(:expected_args) { get_custom_db_conf } + context 'database key taken from specific config' do + let(:expected_args) { custom_db_conf } - describe "#create" do - it "should establish_connection with the separate connection with expected args" do + describe '#create' do + it 'should establish_connection with the separate connection with expected args' do expect(Apartment::Adapters::AbstractAdapter::SeparateDbConnectionHandler).to receive(:establish_connection).with(expected_args).and_call_original # because we dont have another server to connect to it errors @@ -31,8 +33,8 @@ end end - describe "#drop" do - it "should establish_connection with the separate connection with expected args" do + describe '#drop' do + it 'should establish_connection with the separate connection with expected args' do expect(Apartment::Adapters::AbstractAdapter::SeparateDbConnectionHandler).to receive(:establish_connection).with(expected_args).and_call_original # because we dont have another server to connect to it errors @@ -42,13 +44,13 @@ end end - context "database key from tenant name" do + context 'database key from tenant name' do let(:expected_args) { - get_custom_db_conf.tap { |args| args.delete(:database) } + custom_db_conf.tap { |args| args.delete(:database) } } - describe "#switch!" do - it "should connect to new db" do + describe '#switch!' do + it 'should connect to new db' do expect(Apartment).to receive(:establish_connection) do |args| db_name = args.delete(:database) @@ -85,7 +87,7 @@ def specific_connection } end - def get_custom_db_conf + def custom_db_conf specific_connection[db.to_sym].with_indifferent_access end end diff --git a/spec/examples/generic_adapter_examples.rb b/spec/examples/generic_adapter_examples.rb index 9366cbe5..6da9bd56 100644 --- a/spec/examples/generic_adapter_examples.rb +++ b/spec/examples/generic_adapter_examples.rb @@ -1,6 +1,8 @@ +# frozen_string_literal: true + require 'spec_helper' -shared_examples_for "a generic apartment adapter" do +shared_examples_for 'a generic apartment adapter' do include Apartment::Spec::AdapterRequirements before { @@ -8,8 +10,8 @@ Apartment.append_environment = false } - describe "#init" do - it "should not retain a connection after railtie" do + describe '#init' do + it 'should not retain a connection after railtie' do # this test should work on rails >= 4, the connection pool code is # completely different for 3.2 so we'd have to have a messy conditional.. unless Rails::VERSION::MAJOR < 4 @@ -30,19 +32,19 @@ # # Creates happen already in our before_filter # - describe "#create" do - it "should create the new databases" do + describe '#create' do + it 'should create the new databases' do expect(tenant_names).to include(db1) expect(tenant_names).to include(db2) end - it "should load schema.rb to new schema" do + it 'should load schema.rb to new schema' do subject.switch(db1) do expect(connection.tables).to include('companies') end end - it "should yield to block if passed and reset" do + it 'should yield to block if passed and reset' do subject.drop(db2) # so we don't get errors on creation @count = 0 # set our variable so its visible in and outside of blocks @@ -58,7 +60,7 @@ subject.switch(db2) { expect(User.count).to eq(@count + 1) } end - it "should raise error when the schema.rb is missing unless Apartment.use_sql is set to true" do + it 'should raise error when the schema.rb is missing unless Apartment.use_sql is set to true' do next if Apartment.use_sql subject.drop(db1) @@ -75,33 +77,33 @@ end end - describe "#drop" do - it "should remove the db" do + describe '#drop' do + it 'should remove the db' do subject.drop db1 expect(tenant_names).not_to include(db1) end end - describe "#switch!" do - it "should connect to new db" do + describe '#switch!' do + it 'should connect to new db' do subject.switch!(db1) expect(subject.current).to eq(db1) end - it "should reset connection if database is nil" do + it 'should reset connection if database is nil' do subject.switch! expect(subject.current).to eq(default_tenant) end - it "should raise an error if database is invalid" do + it 'should raise an error if database is invalid' do expect { subject.switch! 'unknown_database' }.to raise_error(Apartment::ApartmentError) end end - describe "#switch" do - it "connects and resets the tenant" do + describe '#switch' do + it 'connects and resets the tenant' do subject.switch(db1) do expect(subject.current).to eq(db1) end @@ -110,7 +112,7 @@ # We're often finding when using Apartment in tests, the `current` (ie the previously connect to db) # gets dropped, but switch will try to return to that db in a test. We should just reset if it doesn't exist - it "should not throw exception if current is no longer accessible" do + it 'should not throw exception if current is no longer accessible' do subject.switch!(db2) expect { @@ -119,23 +121,23 @@ end end - describe "#reset" do - it "should reset connection" do + describe '#reset' do + it 'should reset connection' do subject.switch!(db1) subject.reset expect(subject.current).to eq(default_tenant) end end - describe "#current" do - it "should return the current db name" do + describe '#current' do + it 'should return the current db name' do subject.switch!(db1) expect(subject.current).to eq(db1) end end - describe "#each" do - it "iterates over each tenant by default" do + describe '#each' do + it 'iterates over each tenant by default' do result = [] Apartment.tenant_names = [db2, db1] @@ -147,7 +149,7 @@ expect(result).to eq([db2, db1]) end - it "iterates over the given tenants" do + it 'iterates over the given tenants' do result = [] Apartment.tenant_names = [db2] diff --git a/spec/examples/schema_adapter_examples.rb b/spec/examples/schema_adapter_examples.rb index ddd82c1a..c96f7038 100644 --- a/spec/examples/schema_adapter_examples.rb +++ b/spec/examples/schema_adapter_examples.rb @@ -1,17 +1,19 @@ +# frozen_string_literal: true + require 'spec_helper' # rubocop:disable Metrics/BlockLength -shared_examples_for "a schema based apartment adapter" do +shared_examples_for 'a schema based apartment adapter' do include Apartment::Spec::AdapterRequirements let(:schema1) { db1 } let(:schema2) { db2 } let(:public_schema) { default_tenant } - describe "#init" do + describe '#init' do before do Apartment.configure do |config| - config.excluded_models = ["Company"] + config.excluded_models = ['Company'] end end @@ -23,14 +25,14 @@ end end - it "should process model exclusions" do + it 'should process model exclusions' do Apartment::Tenant.init - expect(Company.table_name).to eq("public.companies") + expect(Company.table_name).to eq('public.companies') end - context "with a default_schema", :default_schema => true do - it "should set the proper table_name on excluded_models" do + context 'with a default_schema', default_schema: true do + it 'should set the proper table_name on excluded_models' do Apartment::Tenant.init expect(Company.table_name).to eq("#{default_schema}.companies") @@ -43,10 +45,10 @@ end end - context "persistent_schemas", :persistent_schemas => true do - it "sets the persistent schemas in the schema_search_path" do + context 'persistent_schemas', persistent_schemas: true do + it 'sets the persistent schemas in the schema_search_path' do Apartment::Tenant.init - expect(connection.schema_search_path).to end_with persistent_schemas.map { |schema| %{"#{schema}"} }.join(', ') + expect(connection.schema_search_path).to end_with persistent_schemas.map { |schema| %("#{schema}") }.join(', ') end end end @@ -54,31 +56,31 @@ # # Creates happen already in our before_filter # - describe "#create" do - it "should load schema.rb to new schema" do + describe '#create' do + it 'should load schema.rb to new schema' do connection.schema_search_path = schema1 expect(connection.tables).to include('companies') end - it "should yield to block if passed and reset" do + it 'should yield to block if passed and reset' do subject.drop(schema2) # so we don't get errors on creation @count = 0 # set our variable so its visible in and outside of blocks subject.create(schema2) do @count = User.count - expect(connection.schema_search_path).to start_with %{"#{schema2}"} + expect(connection.schema_search_path).to start_with %("#{schema2}") User.create end - expect(connection.schema_search_path).not_to start_with %{"#{schema2}"} + expect(connection.schema_search_path).not_to start_with %("#{schema2}") subject.switch(schema2) { expect(User.count).to eq(@count + 1) } end - context "numeric database names" do + context 'numeric database names' do let(:db) { 1234 } - it "should allow them" do + it 'should allow them' do expect { subject.create(db) }.to_not raise_error @@ -89,17 +91,17 @@ end end - describe "#drop" do - it "should raise an error for unknown database" do + describe '#drop' do + it 'should raise an error for unknown database' do expect { - subject.drop "unknown_database" + subject.drop 'unknown_database' }.to raise_error(Apartment::TenantNotFound) end - context "numeric database names" do + context 'numeric database names' do let(:db) { 1234 } - it "should be able to drop them" do + it 'should be able to drop them' do subject.create(db) expect { subject.drop(db) @@ -117,117 +119,117 @@ end end - describe "#switch" do - it "connects and resets" do + describe '#switch' do + it 'connects and resets' do subject.switch(schema1) do - expect(connection.schema_search_path).to start_with %{"#{schema1}"} + expect(connection.schema_search_path).to start_with %("#{schema1}") end - expect(connection.schema_search_path).to start_with %{"#{public_schema}"} + expect(connection.schema_search_path).to start_with %("#{public_schema}") end end - describe "#reset" do - it "should reset connection" do + describe '#reset' do + it 'should reset connection' do subject.switch!(schema1) subject.reset - expect(connection.schema_search_path).to start_with %{"#{public_schema}"} + expect(connection.schema_search_path).to start_with %("#{public_schema}") end - context "with default_schema", :default_schema => true do - it "should reset to the default schema" do + context 'with default_schema', default_schema: true do + it 'should reset to the default schema' do subject.switch!(schema1) subject.reset - expect(connection.schema_search_path).to start_with %{"#{default_schema}"} + expect(connection.schema_search_path).to start_with %("#{default_schema}") end end - context "persistent_schemas", :persistent_schemas => true do + context 'persistent_schemas', persistent_schemas: true do before do subject.switch!(schema1) subject.reset end - it "maintains the persistent schemas in the schema_search_path" do - expect(connection.schema_search_path).to end_with persistent_schemas.map { |schema| %{"#{schema}"} }.join(', ') + it 'maintains the persistent schemas in the schema_search_path' do + expect(connection.schema_search_path).to end_with persistent_schemas.map { |schema| %("#{schema}") }.join(', ') end - context "with default_schema", :default_schema => true do - it "prioritizes the switched schema to front of schema_search_path" do + context 'with default_schema', default_schema: true do + it 'prioritizes the switched schema to front of schema_search_path' do subject.reset # need to re-call this as the default_schema wasn't set at the time that the above reset ran - expect(connection.schema_search_path).to start_with %{"#{default_schema}"} + expect(connection.schema_search_path).to start_with %("#{default_schema}") end end end end - describe "#switch!" do - it "should connect to new schema" do + describe '#switch!' do + it 'should connect to new schema' do subject.switch!(schema1) - expect(connection.schema_search_path).to start_with %{"#{schema1}"} + expect(connection.schema_search_path).to start_with %("#{schema1}") end - it "should reset connection if database is nil" do + it 'should reset connection if database is nil' do subject.switch! - expect(connection.schema_search_path).to eq(%{"#{public_schema}"}) + expect(connection.schema_search_path).to eq(%("#{public_schema}")) end - it "should raise an error if schema is invalid" do + it 'should raise an error if schema is invalid' do expect { subject.switch! 'unknown_schema' }.to raise_error(Apartment::TenantNotFound) end - context "numeric databases" do + context 'numeric databases' do let(:db) { 1234 } - it "should connect to them" do + it 'should connect to them' do subject.create(db) expect { subject.switch!(db) }.to_not raise_error - expect(connection.schema_search_path).to start_with %{"#{db.to_s}"} + expect(connection.schema_search_path).to start_with %("#{db}") end after { subject.drop(db) } end - describe "with default_schema specified", :default_schema => true do + describe 'with default_schema specified', default_schema: true do before do subject.switch!(schema1) end - it "should switch out the default schema rather than public" do + it 'should switch out the default schema rather than public' do expect(connection.schema_search_path).not_to include default_schema end - it "should still switch to the switched schema" do - expect(connection.schema_search_path).to start_with %{"#{schema1}"} + it 'should still switch to the switched schema' do + expect(connection.schema_search_path).to start_with %("#{schema1}") end end - context "persistent_schemas", :persistent_schemas => true do + context 'persistent_schemas', persistent_schemas: true do before { subject.switch!(schema1) } - it "maintains the persistent schemas in the schema_search_path" do - expect(connection.schema_search_path).to end_with persistent_schemas.map { |schema| %{"#{schema}"} }.join(', ') + it 'maintains the persistent schemas in the schema_search_path' do + expect(connection.schema_search_path).to end_with persistent_schemas.map { |schema| %("#{schema}") }.join(', ') end - it "prioritizes the switched schema to front of schema_search_path" do - expect(connection.schema_search_path).to start_with %{"#{schema1}"} + it 'prioritizes the switched schema to front of schema_search_path' do + expect(connection.schema_search_path).to start_with %("#{schema1}") end end end - describe "#current" do - it "should return the current schema name" do + describe '#current' do + it 'should return the current schema name' do subject.switch!(schema1) expect(subject.current).to eq(schema1) end - context "persistent_schemas", :persistent_schemas => true do - it "should exlude persistent_schemas" do + context 'persistent_schemas', persistent_schemas: true do + it 'should exlude persistent_schemas' do subject.switch!(schema1) expect(subject.current).to eq(schema1) end diff --git a/spec/integration/apartment_rake_integration_spec.rb b/spec/integration/apartment_rake_integration_spec.rb index dc819d4e..fbfd0474 100644 --- a/spec/integration/apartment_rake_integration_spec.rb +++ b/spec/integration/apartment_rake_integration_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' require 'rake' -describe "apartment rake tasks", database: :postgresql do +describe 'apartment rake tasks', database: :postgresql do before do @rake = Rake::Application.new Rake.application = @rake @@ -19,8 +19,8 @@ Apartment.configure do |config| config.use_schemas = true - config.excluded_models = ["Company"] - config.tenant_names = lambda { Company.pluck(:database) } + config.excluded_models = ['Company'] + config.tenant_names = -> { Company.pluck(:database) } end Apartment::Tenant.reload!(config) @@ -30,7 +30,7 @@ after { Rake.application = nil } - context "with x number of databases" do + 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!(:company_count) { db_names.length } @@ -38,7 +38,7 @@ before do db_names.collect do |db_name| Apartment::Tenant.create(db_name) - Company.create :database => db_name + Company.create database: db_name end end @@ -47,22 +47,22 @@ Company.delete_all end - context "with ActiveRecord below 5.2.0" do + context 'with ActiveRecord below 5.2.0' do before do - allow(ActiveRecord::Migrator).to receive(:migrations_paths) { %w(spec/dummy/db/migrate) } + allow(ActiveRecord::Migrator).to receive(:migrations_paths) { %w[spec/dummy/db/migrate] } allow(Apartment::Migrator).to receive(:activerecord_below_5_2?) { true } end - describe "#migrate" do - it "should migrate all databases" do + describe '#migrate' do + it 'should migrate all databases' do expect(ActiveRecord::Migrator).to receive(:migrate).exactly(company_count).times @rake['apartment:migrate'].invoke end end - describe "#rollback" do - it "should rollback all dbs" do + describe '#rollback' do + it 'should rollback all dbs' do expect(ActiveRecord::Migrator).to receive(:rollback).exactly(company_count).times @rake['apartment:rollback'].invoke @@ -70,15 +70,15 @@ end end - context "with ActiveRecord above or equal to 5.2.0" do + context 'with ActiveRecord above or equal to 5.2.0' do let(:migration_context_double) { double(:migration_context) } before do allow(Apartment::Migrator).to receive(:activerecord_below_5_2?) { false } end - describe "#migrate" do - it "should migrate all databases" do + describe '#migrate' do + it 'should migrate all databases' do allow(ActiveRecord::Base.connection).to receive(:migration_context) { migration_context_double } expect(migration_context_double).to receive(:migrate).exactly(company_count).times @@ -86,8 +86,8 @@ end end - describe "#rollback" do - it "should rollback all dbs" do + describe '#rollback' do + it 'should rollback all dbs' do allow(ActiveRecord::Base.connection).to receive(:migration_context) { migration_context_double } expect(migration_context_double).to receive(:rollback).exactly(company_count).times @@ -96,8 +96,8 @@ end end - describe "apartment:seed" do - it "should seed all databases" do + describe 'apartment:seed' do + it 'should seed all databases' do expect(Apartment::Tenant).to receive(:seed).exactly(company_count).times @rake['apartment:seed'].invoke diff --git a/spec/integration/query_caching_spec.rb b/spec/integration/query_caching_spec.rb index 10cbaa97..29a5fa8e 100644 --- a/spec/integration/query_caching_spec.rb +++ b/spec/integration/query_caching_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe 'query caching' do @@ -6,8 +8,8 @@ before do Apartment.configure do |config| - config.excluded_models = ["Company"] - config.tenant_names = lambda { Company.pluck(:database) } + config.excluded_models = ['Company'] + config.tenant_names = -> { Company.pluck(:database) } config.use_schemas = true end @@ -46,8 +48,8 @@ before do Apartment.configure do |config| - config.excluded_models = ["Company"] - config.tenant_names = lambda { Company.pluck(:database) } + config.excluded_models = ['Company'] + config.tenant_names = -> { Company.pluck(:database) } config.use_schemas = false end @@ -66,7 +68,7 @@ Company.delete_all end - it "configuration value is kept after switching databases" do + it 'configuration value is kept after switching databases' do ActiveRecord::Base.connection.enable_query_cache! Apartment::Tenant.switch! db_name diff --git a/spec/integration/use_within_an_engine_spec.rb b/spec/integration/use_within_an_engine_spec.rb index 0e4cfa16..d9592108 100644 --- a/spec/integration/use_within_an_engine_spec.rb +++ b/spec/integration/use_within_an_engine_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + describe 'using apartment within an engine' do before do engine_path = Pathname.new(File.expand_path('../../dummy_engine', __FILE__)) diff --git a/spec/schemas/v1.rb b/spec/schemas/v1.rb index e5f626c9..052f5663 100644 --- a/spec/schemas/v1.rb +++ b/spec/schemas/v1.rb @@ -1,4 +1,4 @@ -# encoding: UTF-8 +# frozen_string_literal: true # This file is auto-generated from the current state of the database. Instead # of editing this file, please use the migrations feature of Active Record to # incrementally modify your database, and then regenerate this schema definition. @@ -11,5 +11,5 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 0) do +ActiveRecord::Schema.define(version: 0) do end diff --git a/spec/schemas/v2.rb b/spec/schemas/v2.rb index 7ace165b..baf7b998 100644 --- a/spec/schemas/v2.rb +++ b/spec/schemas/v2.rb @@ -1,4 +1,4 @@ -# encoding: UTF-8 +# frozen_string_literal: true # This file is auto-generated from the current state of the database. Instead # of editing this file, please use the migrations feature of Active Record to # incrementally modify your database, and then regenerate this schema definition. @@ -11,31 +11,31 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20110613152810) do - create_table "companies", :force => true do |t| - t.boolean "dummy" - t.string "database" +ActiveRecord::Schema.define(version: 20110613152810) do + create_table 'companies', force: true do |t| + t.boolean 'dummy' + t.string 'database' end - create_table "delayed_jobs", :force => true do |t| - t.integer "priority", :default => 0 - t.integer "attempts", :default => 0 - t.text "handler" - t.text "last_error" - t.datetime "run_at" - t.datetime "locked_at" - t.datetime "failed_at" - t.string "locked_by" - t.datetime "created_at" - t.datetime "updated_at" - t.string "queue" + create_table 'delayed_jobs', force: true do |t| + t.integer 'priority', default: 0 + t.integer 'attempts', default: 0 + t.text 'handler' + t.text 'last_error' + t.datetime 'run_at' + t.datetime 'locked_at' + t.datetime 'failed_at' + t.string 'locked_by' + t.datetime 'created_at' + t.datetime 'updated_at' + t.string 'queue' end - add_index "delayed_jobs", ["priority", "run_at"], :name => "delayed_jobs_priority" + add_index 'delayed_jobs', ['priority', 'run_at'], name: 'delayed_jobs_priority' - create_table "users", :force => true do |t| - t.string "name" - t.datetime "birthdate" - t.string "sex" + create_table 'users', force: true do |t| + t.string 'name' + t.datetime 'birthdate' + t.string 'sex' end end diff --git a/spec/schemas/v3.rb b/spec/schemas/v3.rb index 7d97bdeb..2cd09a57 100644 --- a/spec/schemas/v3.rb +++ b/spec/schemas/v3.rb @@ -1,4 +1,4 @@ -# encoding: UTF-8 +# frozen_string_literal: true # This file is auto-generated from the current state of the database. Instead # of editing this file, please use the migrations feature of Active Record to # incrementally modify your database, and then regenerate this schema definition. @@ -11,37 +11,37 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20111202022214) do - create_table "books", :force => true do |t| - t.string "name" - t.integer "pages" - t.datetime "published" +ActiveRecord::Schema.define(version: 20111202022214) do + create_table 'books', force: true do |t| + t.string 'name' + t.integer 'pages' + t.datetime 'published' end - create_table "companies", :force => true do |t| - t.boolean "dummy" - t.string "database" + create_table 'companies', force: true do |t| + t.boolean 'dummy' + t.string 'database' end - create_table "delayed_jobs", :force => true do |t| - t.integer "priority", :default => 0 - t.integer "attempts", :default => 0 - t.text "handler" - t.text "last_error" - t.datetime "run_at" - t.datetime "locked_at" - t.datetime "failed_at" - t.string "locked_by" - t.datetime "created_at" - t.datetime "updated_at" - t.string "queue" + create_table 'delayed_jobs', force: true do |t| + t.integer 'priority', default: 0 + t.integer 'attempts', default: 0 + t.text 'handler' + t.text 'last_error' + t.datetime 'run_at' + t.datetime 'locked_at' + t.datetime 'failed_at' + t.string 'locked_by' + t.datetime 'created_at' + t.datetime 'updated_at' + t.string 'queue' end - add_index "delayed_jobs", ["priority", "run_at"], :name => "delayed_jobs_priority" + add_index 'delayed_jobs', ['priority', 'run_at'], name: 'delayed_jobs_priority' - create_table "users", :force => true do |t| - t.string "name" - t.datetime "birthdate" - t.string "sex" + create_table 'users', force: true do |t| + t.string 'name' + t.datetime 'birthdate' + t.string 'sex' end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 9fa79b79..e16b2af8 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,9 +1,11 @@ +# frozen_string_literal: true + $LOAD_PATH.unshift(File.dirname(__FILE__)) # Configure Rails Environment -ENV["RAILS_ENV"] = "test" +ENV['RAILS_ENV'] = 'test' -require File.expand_path("../dummy/config/environment.rb", __FILE__) +require File.expand_path('../dummy/config/environment.rb', __FILE__) # Loading dummy applications affects table_name of each excluded models # defined in `spec/dummy/config/initializers/apartment.rb`. @@ -16,7 +18,7 @@ klass.reset_table_name end -require "rspec/rails" +require 'rspec/rails' require 'capybara/rspec' require 'capybara/rails' @@ -24,11 +26,12 @@ require 'pry' silence_warnings { IRB = Pry } rescue LoadError + nil end ActionMailer::Base.delivery_method = :test ActionMailer::Base.perform_deliveries = true -ActionMailer::Base.default_url_options[:host] = "test.com" +ActionMailer::Base.default_url_options[:host] = 'test.com' Rails.backtrace_cleaner.remove_silencers! diff --git a/spec/support/apartment_helpers.rb b/spec/support/apartment_helpers.rb index 1802d49b..485143ac 100644 --- a/spec/support/apartment_helpers.rb +++ b/spec/support/apartment_helpers.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Apartment module Test # rubocop:disable Style/ModuleFunction @@ -13,7 +15,7 @@ def reset def next_db @x ||= 0 - "db%d" % @x += 1 + 'db%d' % @x += 1 end def drop_schema(schema) diff --git a/spec/support/capybara_sessions.rb b/spec/support/capybara_sessions.rb index f9aef29a..61c3d8f2 100644 --- a/spec/support/capybara_sessions.rb +++ b/spec/support/capybara_sessions.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module RSpec module Integration module CapybaraSessions diff --git a/spec/support/config.rb b/spec/support/config.rb index 2111641a..f4c46c7a 100644 --- a/spec/support/config.rb +++ b/spec/support/config.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'yaml' module Apartment diff --git a/spec/support/contexts.rb b/spec/support/contexts.rb index 2e95ffe4..9c63f0a1 100644 --- a/spec/support/contexts.rb +++ b/spec/support/contexts.rb @@ -1,6 +1,8 @@ +# frozen_string_literal: true + # Some shared contexts for specs -shared_context "with default schema", :default_schema => true do +shared_context 'with default schema', default_schema: true do let(:default_schema) { Apartment::Test.next_db } before do @@ -16,7 +18,7 @@ end # Some default setup for elevator specs -shared_context "elevators", elevator: true do +shared_context 'elevators', elevator: true do let(:company1) { mock_model(Company, database: db1).as_null_object } let(:company2) { mock_model(Company, database: db2).as_null_object } @@ -37,7 +39,7 @@ end end -shared_context "persistent_schemas", :persistent_schemas => true do +shared_context 'persistent_schemas', persistent_schemas: true do let(:persistent_schemas) { %w[hstore postgis] } before do diff --git a/spec/support/requirements.rb b/spec/support/requirements.rb index 4481fca7..cbfbe5ea 100644 --- a/spec/support/requirements.rb +++ b/spec/support/requirements.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Apartment module Spec # @@ -34,7 +36,7 @@ module AdapterRequirements end end - %w{subject tenant_names default_tenant}.each do |method| + %w[subject tenant_names default_tenant].each do |method| define_method method do raise "You must define a `#{method}` method in your host group" end unless defined?(method) diff --git a/spec/support/setup.rb b/spec/support/setup.rb index e9630dd8..ef650383 100644 --- a/spec/support/setup.rb +++ b/spec/support/setup.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Apartment module Spec module Setup diff --git a/spec/tasks/apartment_rake_spec.rb b/spec/tasks/apartment_rake_spec.rb index 2d799b32..451517b8 100644 --- a/spec/tasks/apartment_rake_spec.rb +++ b/spec/tasks/apartment_rake_spec.rb @@ -1,9 +1,11 @@ +# frozen_string_literal: true + require 'spec_helper' require 'rake' require 'apartment/migrator' require 'apartment/tenant' -describe "apartment rake tasks" do +describe 'apartment rake tasks' do before do @rake = Rake::Application.new Rake.application = @rake @@ -36,84 +38,84 @@ allow(Apartment).to receive(:tenant_names).and_return tenant_names end - describe "apartment:migrate" do + describe 'apartment:migrate' do before do allow(ActiveRecord::Migrator).to receive(:migrate) # don't care about this end - it "should migrate public and all multi-tenant dbs" do + it 'should migrate public and all multi-tenant dbs' do expect(Apartment::Migrator).to receive(:migrate).exactly(tenant_count).times @rake['apartment:migrate'].invoke end end - describe "apartment:migrate:up" do - context "without a version" do + describe 'apartment:migrate:up' do + context 'without a version' do before do ENV['VERSION'] = nil end - it "requires a version to migrate to" do + it 'requires a version to migrate to' do expect { @rake['apartment:migrate:up'].invoke - }.to raise_error("VERSION is required") + }.to raise_error('VERSION is required') end end - context "with version" do + context 'with version' do before do ENV['VERSION'] = version end - it "migrates up to a specific version" do + it 'migrates up to a specific version' do expect(Apartment::Migrator).to receive(:run).with(:up, anything, version.to_i).exactly(tenant_count).times @rake['apartment:migrate:up'].invoke end end end - describe "apartment:migrate:down" do - context "without a version" do + describe 'apartment:migrate:down' do + context 'without a version' do before do ENV['VERSION'] = nil end - it "requires a version to migrate to" do + it 'requires a version to migrate to' do expect { @rake['apartment:migrate:down'].invoke - }.to raise_error("VERSION is required") + }.to raise_error('VERSION is required') end end - context "with version" do + context 'with version' do before do ENV['VERSION'] = version end - it "migrates up to a specific version" do + it 'migrates up to a specific version' do expect(Apartment::Migrator).to receive(:run).with(:down, anything, version.to_i).exactly(tenant_count).times @rake['apartment:migrate:down'].invoke end end end - describe "apartment:rollback" do + describe 'apartment:rollback' do let(:step) { '3' } - it "should rollback dbs" do + it 'should rollback dbs' do expect(Apartment::Migrator).to receive(:rollback).exactly(tenant_count).times @rake['apartment:rollback'].invoke end - it "should rollback dbs STEP amt" do + it 'should rollback dbs STEP amt' do expect(Apartment::Migrator).to receive(:rollback).with(anything, step.to_i).exactly(tenant_count).times ENV['STEP'] = step @rake['apartment:rollback'].invoke end end - describe "apartment:drop" do - it "should migrate public and all multi-tenant dbs" do + describe 'apartment:drop' do + it 'should migrate public and all multi-tenant dbs' do expect(Apartment::Tenant).to receive(:drop).exactly(tenant_count).times @rake['apartment:drop'].invoke end diff --git a/spec/tenant_spec.rb b/spec/tenant_spec.rb index 1fdfd88b..d98325ec 100644 --- a/spec/tenant_spec.rb +++ b/spec/tenant_spec.rb @@ -1,11 +1,13 @@ +# frozen_string_literal: true + require 'spec_helper' describe Apartment::Tenant do - context "using mysql", database: :mysql do + context 'using mysql', database: :mysql do before { subject.reload!(config) } - describe "#adapter" do - it "should load mysql adapter" do + describe '#adapter' do + it 'should load mysql adapter' do subject.adapter expect(Apartment::Adapters::Mysql2Adapter).to be_a(Class) end @@ -13,7 +15,7 @@ # TODO this doesn't belong here, but there aren't integration tests currently for mysql # where to put??? - describe "exception recovery", :type => :request do + describe 'exception recovery', type: :request do before do subject.create db1 end @@ -30,8 +32,8 @@ end # TODO re-organize these tests - context "with prefix and schemas" do - describe "#create" do + context 'with prefix and schemas' do + describe '#create' do before do Apartment.configure do |config| config.prepend_environment = true @@ -43,31 +45,31 @@ after do begin - subject.drop "db_with_prefix" + subject.drop 'db_with_prefix' rescue StandardError => _e nil end end - it "should create a new database" do - subject.create "db_with_prefix" + it 'should create a new database' do + subject.create 'db_with_prefix' end end end end - context "using postgresql", database: :postgresql do + context 'using postgresql', database: :postgresql do before do Apartment.use_schemas = true subject.reload!(config) end - describe "#adapter" do - it "should load postgresql adapter" do + describe '#adapter' do + it 'should load postgresql adapter' do expect(subject.adapter).to be_a(Apartment::Adapters::PostgresqlSchemaAdapter) end - it "raises exception with invalid adapter specified" do + it 'raises exception with invalid adapter specified' do subject.reload!(config.merge(adapter: 'unknown')) expect { @@ -75,7 +77,7 @@ }.to raise_error(RuntimeError) end - context "threadsafety" do + context 'threadsafety' do before { subject.create db1 } after { subject.drop db1 } @@ -89,7 +91,7 @@ end # TODO above spec are also with use_schemas=true - context "with schemas" do + context 'with schemas' do before do Apartment.configure do |config| config.excluded_models = [] @@ -101,21 +103,21 @@ after { subject.drop db1 } - describe "#create" do - it "should seed data" do + describe '#create' do + it 'should seed data' do subject.switch! db1 expect(User.count).to be > 0 end end - describe "#switch!" do + describe '#switch!' do let(:x) { rand(3) } - context "creating models" do + context 'creating models' do before { subject.create db2 } after { subject.drop db2 } - it "should create a model instance in the current schema" do + it 'should create a model instance in the current schema' do subject.switch! db2 db2_count = User.count + x.times { User.create } @@ -130,10 +132,10 @@ end end - context "with excluded models" do + context 'with excluded models' do before do Apartment.configure do |config| - config.excluded_models = ["Company"] + config.excluded_models = ['Company'] end subject.init end @@ -146,7 +148,7 @@ end end - it "should create excluded models in public schema" do + it 'should create excluded models in public schema' do subject.reset # ensure we're on public schema count = Company.count + x.times { Company.create } @@ -160,7 +162,7 @@ end end - context "seed paths" do + context 'seed paths' do before do Apartment.configure do |config| config.excluded_models = [] @@ -180,7 +182,7 @@ it 'should seed from custom path' do Apartment.configure do |config| - config.seed_data_file = "#{Rails.root}/db/seeds/import.rb" + config.seed_data_file = Rails.root.join('db', 'seeds', 'import.rb') end subject.create db1 subject.switch! db1 diff --git a/spec/unit/config_spec.rb b/spec/unit/config_spec.rb index 010316ee..cceaa7e4 100644 --- a/spec/unit/config_spec.rb +++ b/spec/unit/config_spec.rb @@ -1,9 +1,11 @@ +# frozen_string_literal: true + require 'spec_helper' describe Apartment do - describe "#config" do - let(:excluded_models) { ["Company"] } - let(:seed_data_file_path) { "#{Rails.root}/db/seeds/import.rb" } + describe '#config' do + let(:excluded_models) { ['Company'] } + 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| @@ -11,21 +13,21 @@ def tenant_names_from_array(names) end.with_indifferent_access end - it "should yield the Apartment object" do + it 'should yield the Apartment object' do Apartment.configure do |config| config.excluded_models = [] expect(config).to eq(Apartment) end end - it "should set excluded models" do + it 'should set excluded models' do Apartment.configure do |config| config.excluded_models = excluded_models end expect(Apartment.excluded_models).to eq(excluded_models) end - it "should set use_schemas" do + it 'should set use_schemas' do Apartment.configure do |config| config.excluded_models = [] config.use_schemas = false @@ -33,14 +35,14 @@ def tenant_names_from_array(names) expect(Apartment.use_schemas).to be false end - it "should set seed_data_file" do + it 'should set seed_data_file' do Apartment.configure do |config| config.seed_data_file = seed_data_file_path end expect(Apartment.seed_data_file).to eq(seed_data_file_path) end - it "should set seed_after_create" do + it 'should set seed_after_create' do Apartment.configure do |config| config.excluded_models = [] config.seed_after_create = true @@ -48,7 +50,7 @@ def tenant_names_from_array(names) expect(Apartment.seed_after_create).to be true end - context "databases" do + context 'databases' do let(:users_conf_hash) { { port: 5444 } } before do @@ -57,50 +59,50 @@ def tenant_names_from_array(names) end end - context "tenant_names as string array" do + context 'tenant_names as string array' do let(:tenant_names) { %w[users companies] } - it "should return object if it doesnt respond_to call" do + it 'should return object if it doesnt respond_to call' do expect(Apartment.tenant_names).to eq(tenant_names_from_array(tenant_names).keys) end - it "should set tenants_with_config" do + it 'should set tenants_with_config' do expect(Apartment.tenants_with_config).to eq(tenant_names_from_array(tenant_names)) end end - context "tenant_names as proc returning an array" do - let(:tenant_names) { lambda { %w[users companies] } } + context '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 + 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) end - it "should set tenants_with_config" do + it 'should set tenants_with_config' do expect(Apartment.tenants_with_config).to eq(tenant_names_from_array(tenant_names.call)) end end - context "tenant_names as Hash" do + context '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 + it 'should return object if it doesnt respond_to call' do expect(Apartment.tenant_names).to eq(tenant_names.keys) end - it "should set tenants_with_config" do + it 'should set tenants_with_config' do expect(Apartment.tenants_with_config).to eq(tenant_names) end end - context "tenant_names as proc returning a Hash" do - let(:tenant_names) { lambda { { users: users_conf_hash }.with_indifferent_access } } + context '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 + it 'should return object if it doesnt respond_to call' do expect(Apartment.tenant_names).to eq(tenant_names.call.keys) end - it "should set tenants_with_config" do + it 'should set tenants_with_config' do expect(Apartment.tenants_with_config).to eq(tenant_names.call) end end diff --git a/spec/unit/elevators/domain_spec.rb b/spec/unit/elevators/domain_spec.rb index 5970e2d4..fc0ade75 100644 --- a/spec/unit/elevators/domain_spec.rb +++ b/spec/unit/elevators/domain_spec.rb @@ -1,28 +1,30 @@ +# frozen_string_literal: true + require 'spec_helper' require 'apartment/elevators/domain' describe Apartment::Elevators::Domain do subject(:elevator) { described_class.new(proc) } - describe "#parse_tenant_name" do - it "parses the host for a domain name" do + describe '#parse_tenant_name' do + it 'parses the host for a domain name' do request = ActionDispatch::Request.new('HTTP_HOST' => 'example.com') expect(elevator.parse_tenant_name(request)).to eq('example') end - it "ignores a www prefix and domain suffix" do + it 'ignores a www prefix and domain suffix' do request = ActionDispatch::Request.new('HTTP_HOST' => 'www.example.bc.ca') expect(elevator.parse_tenant_name(request)).to eq('example') end - it "returns nil if there is no host" do + it 'returns nil if there is no host' do request = ActionDispatch::Request.new('HTTP_HOST' => '') expect(elevator.parse_tenant_name(request)).to be_nil end end - describe "#call" do - it "switches to the proper tenant" do + describe '#call' do + it 'switches to the proper tenant' do expect(Apartment::Tenant).to receive(:switch).with('example') elevator.call('HTTP_HOST' => 'www.example.com') diff --git a/spec/unit/elevators/first_subdomain_spec.rb b/spec/unit/elevators/first_subdomain_spec.rb index 837cb3b3..f608d834 100644 --- a/spec/unit/elevators/first_subdomain_spec.rb +++ b/spec/unit/elevators/first_subdomain_spec.rb @@ -1,22 +1,24 @@ +# frozen_string_literal: true + require 'spec_helper' require 'apartment/elevators/first_subdomain' 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") } + describe 'subdomain' do + subject { described_class.new('test').parse_tenant_name(request) } + let(:request) { double(:request, host: "#{subdomain}.example.com") } - context "one subdomain" do - let(:subdomain) { "test" } - it { is_expected.to eq("test") } + context 'one subdomain' do + let(:subdomain) { 'test' } + it { is_expected.to eq('test') } end - context "nested subdomains" do - let(:subdomain) { "test1.test2" } - it { is_expected.to eq("test1") } + context 'nested subdomains' do + let(:subdomain) { 'test1.test2' } + it { is_expected.to eq('test1') } end - context "no subdomain" do + context 'no subdomain' do let(:subdomain) { nil } it { is_expected.to eq(nil) } end diff --git a/spec/unit/elevators/generic_spec.rb b/spec/unit/elevators/generic_spec.rb index 9bbe1ad0..1b12044a 100644 --- a/spec/unit/elevators/generic_spec.rb +++ b/spec/unit/elevators/generic_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' require 'apartment/elevators/generic' @@ -10,8 +12,8 @@ def parse_tenant_name(*) subject(:elevator) { described_class.new(proc) } - describe "#call" do - it "calls the processor if given" do + describe '#call' do + it 'calls the processor if given' do elevator = described_class.new(proc {}, proc { 'tenant1' }) expect(Apartment::Tenant).to receive(:switch).with('tenant1') @@ -19,13 +21,13 @@ def parse_tenant_name(*) elevator.call('HTTP_HOST' => 'foo.bar.com') end - it "raises if parse_tenant_name not implemented" do + it 'raises if parse_tenant_name not implemented' do expect { elevator.call('HTTP_HOST' => 'foo.bar.com') }.to raise_error(RuntimeError) end - it "switches to the parsed db_name" do + it 'switches to the parsed db_name' do elevator = MyElevator.new(proc {}) expect(Apartment::Tenant).to receive(:switch).with('tenant2') @@ -33,14 +35,14 @@ def parse_tenant_name(*) elevator.call('HTTP_HOST' => 'foo.bar.com') end - it "calls the block implementation of `switch`" do + it 'calls the block implementation of `switch`' do elevator = MyElevator.new(proc {}, proc { 'tenant2' }) expect(Apartment::Tenant).to receive(:switch).with('tenant2').and_yield elevator.call('HTTP_HOST' => 'foo.bar.com') end - it "does not call `switch` if no database given" do + it 'does not call `switch` if no database given' do app = proc {} elevator = MyElevator.new(app, proc {}) diff --git a/spec/unit/elevators/host_hash_spec.rb b/spec/unit/elevators/host_hash_spec.rb index 8a8dc344..6fc2f72b 100644 --- a/spec/unit/elevators/host_hash_spec.rb +++ b/spec/unit/elevators/host_hash_spec.rb @@ -1,28 +1,30 @@ +# frozen_string_literal: true + require 'spec_helper' require 'apartment/elevators/host_hash' describe Apartment::Elevators::HostHash do subject(:elevator) { Apartment::Elevators::HostHash.new(proc {}, 'example.com' => 'example_tenant') } - describe "#parse_tenant_name" do - it "parses the host for a domain name" do + describe '#parse_tenant_name' do + it 'parses the host for a domain name' do request = ActionDispatch::Request.new('HTTP_HOST' => 'example.com') expect(elevator.parse_tenant_name(request)).to eq('example_tenant') end - it "raises TenantNotFound exception if there is no host" do + it 'raises TenantNotFound exception if there is no host' do request = ActionDispatch::Request.new('HTTP_HOST' => '') expect { elevator.parse_tenant_name(request) }.to raise_error(Apartment::TenantNotFound) end - it "raises TenantNotFound exception if there is no database associated to current host" do + it 'raises TenantNotFound exception if there is no database associated to current host' do request = ActionDispatch::Request.new('HTTP_HOST' => 'example2.com') expect { elevator.parse_tenant_name(request) }.to raise_error(Apartment::TenantNotFound) end end - describe "#call" do - it "switches to the proper tenant" do + describe '#call' do + it 'switches to the proper tenant' do expect(Apartment::Tenant).to receive(:switch).with('example_tenant') elevator.call('HTTP_HOST' => 'example.com') diff --git a/spec/unit/elevators/host_spec.rb b/spec/unit/elevators/host_spec.rb index 6338ef32..f480cbbb 100644 --- a/spec/unit/elevators/host_spec.rb +++ b/spec/unit/elevators/host_spec.rb @@ -1,85 +1,87 @@ +# frozen_string_literal: true + require 'spec_helper' require 'apartment/elevators/host' describe Apartment::Elevators::Host do subject(:elevator) { described_class.new(proc) } - describe "#parse_tenant_name" do - it "should return nil when no host" do + describe '#parse_tenant_name' do + it 'should return 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 '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 + context 'with 3 parts' do + it 'should return 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 + context 'with 6 parts' do + it 'should return 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 - before { allow(described_class).to receive(:ignored_first_subdomains).and_return(%w{www foo}) } + context '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 + context 'with 3 parts' do + it 'should return 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 'should return 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 + 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') end - it "should return host without www" do + 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') end end end - context "assuming localhost" do - it "should return localhost" do + context 'assuming localhost' do + it 'should return 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 'assuming ip address' do + it 'should return 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 end end - describe "#call" do - it "switches to the proper tenant" do + describe '#call' do + it 'switches to the proper tenant' do allow(described_class).to receive(:ignored_first_subdomains).and_return([]) expect(Apartment::Tenant).to receive(:switch).with('foo.bar.com') elevator.call('HTTP_HOST' => 'foo.bar.com') end - it "ignores ignored_first_subdomains" do - allow(described_class).to receive(:ignored_first_subdomains).and_return(%w{foo}) + it 'ignores ignored_first_subdomains' do + allow(described_class).to receive(:ignored_first_subdomains).and_return(%w[foo]) expect(Apartment::Tenant).to receive(:switch).with('bar.com') elevator.call('HTTP_HOST' => 'foo.bar.com') end diff --git a/spec/unit/elevators/subdomain_spec.rb b/spec/unit/elevators/subdomain_spec.rb index 87cbc904..42d9c96a 100644 --- a/spec/unit/elevators/subdomain_spec.rb +++ b/spec/unit/elevators/subdomain_spec.rb @@ -1,69 +1,71 @@ +# frozen_string_literal: true + require 'spec_helper' require 'apartment/elevators/subdomain' describe Apartment::Elevators::Subdomain do subject(:elevator) { described_class.new(proc) } - describe "#parse_tenant_name" do - context "assuming one tld" do - it "should parse subdomain" do + describe '#parse_tenant_name' do + context 'assuming one tld' do + it 'should parse 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 'should return 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 'assuming two tlds' do + it 'should parse 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") + expect(elevator.parse_tenant_name(request)).to eq('foo') end - it "should return nil when no subdomain in the third level domain" do + it 'should return 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 'assuming two subdomains' do + it 'should parse 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") + expect(elevator.parse_tenant_name(request)).to eq('foo') end - it "should parse two subdomains in the third level domain" do + it 'should parse 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") + expect(elevator.parse_tenant_name(request)).to eq('foo') end end - context "assuming localhost" do - it "should return nil for localhost" do + context 'assuming localhost' do + it 'should return 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 'assuming ip address' do + it 'should return 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 end end - describe "#call" do - it "switches to the proper tenant" do + describe '#call' do + it 'switches to the proper tenant' do expect(Apartment::Tenant).to receive(:switch).with('tenant1') elevator.call('HTTP_HOST' => 'tenant1.example.com') end - it "ignores excluded subdomains" do - described_class.excluded_subdomains = %w{foo} + it 'ignores excluded subdomains' do + described_class.excluded_subdomains = %w[foo] expect(Apartment::Tenant).not_to receive(:switch) diff --git a/spec/unit/migrator_spec.rb b/spec/unit/migrator_spec.rb index dadc1834..8cbcb9e2 100644 --- a/spec/unit/migrator_spec.rb +++ b/spec/unit/migrator_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' require 'apartment/migrator' diff --git a/spec/unit/reloader_spec.rb b/spec/unit/reloader_spec.rb index 312aa129..54bd2ff7 100644 --- a/spec/unit/reloader_spec.rb +++ b/spec/unit/reloader_spec.rb @@ -1,19 +1,21 @@ +# frozen_string_literal: true + require 'spec_helper' describe Apartment::Reloader do - context "using postgresql schemas" do + context 'using postgresql schemas' do before do Apartment.configure do |config| - config.excluded_models = ["Company"] + config.excluded_models = ['Company'] config.use_schemas = true end Apartment::Tenant.reload!(config) Company.reset_table_name # ensure we're clean end - subject { Apartment::Reloader.new(double("Rack::Application", :call => nil)) } + subject { Apartment::Reloader.new(double('Rack::Application', call: nil)) } - it "should initialize apartment when called" do + it 'should initialize apartment when called' do expect(Company.table_name).not_to include('public.') subject.call(double('env')) expect(Company.table_name).to include('public.') From bacd6188b67744d27f3461cb872e4c3e1ed7e223 Mon Sep 17 00:00:00 2001 From: Rui Baltazar Date: Sun, 16 Feb 2020 17:18:20 +0800 Subject: [PATCH 28/50] WIP - fixed dynamic find by --- .rubocop_todo.yml | 8 -------- spec/integration/query_caching_spec.rb | 4 ++-- 2 files changed, 2 insertions(+), 10 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index f397e797..ad509725 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -55,14 +55,6 @@ Metrics/LineLength: Metrics/MethodLength: Max: 24 -# Offense count: 2 -# Cop supports --auto-correct. -# Configuration parameters: Whitelist. -# Whitelist: find_by_sql -Rails/DynamicFindBy: - Exclude: - - 'spec/integration/query_caching_spec.rb' - # Offense count: 1 # Configuration parameters: Include. # Include: app/**/*.rb, config/**/*.rb, db/**/*.rb, lib/**/*.rb diff --git a/spec/integration/query_caching_spec.rb b/spec/integration/query_caching_spec.rb index 29a5fa8e..6026e5a1 100644 --- a/spec/integration/query_caching_spec.rb +++ b/spec/integration/query_caching_spec.rb @@ -36,10 +36,10 @@ ActiveRecord::Base.connection.enable_query_cache! Apartment::Tenant.switch! db_names.first - expect(User.find_by_name(db_names.first).name).to eq(db_names.first) + expect(User.find_by(name: db_names.first).name).to eq(db_names.first) Apartment::Tenant.switch! db_names.last - expect(User.find_by_name(db_names.first)).to be_nil + expect(User.find_by(name: db_names.first)).to be_nil end end From d2e8248ff0c48f8065c2e6358994c7ca8d569509 Mon Sep 17 00:00:00 2001 From: Rui Baltazar Date: Sun, 16 Feb 2020 17:20:27 +0800 Subject: [PATCH 29/50] WIP - fixed rails output and preferred alias method --- .rubocop_todo.yml | 17 ----------------- lib/apartment.rb | 4 ++-- lib/apartment/adapters/abstract_adapter.rb | 6 +++--- lib/apartment/console.rb | 3 +++ 4 files changed, 8 insertions(+), 22 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index ad509725..b22db18e 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -55,29 +55,12 @@ Metrics/LineLength: Metrics/MethodLength: Max: 24 -# Offense count: 1 -# Configuration parameters: Include. -# Include: app/**/*.rb, config/**/*.rb, db/**/*.rb, lib/**/*.rb -Rails/Output: - Exclude: - - '**/rails_helper.rb' - - 'lib/apartment/console.rb' - # Offense count: 1 # Cop supports --auto-correct. Security/YAMLLoad: Exclude: - 'spec/support/config.rb' -# Offense count: 5 -# Cop supports --auto-correct. -# Configuration parameters: EnforcedStyle. -# SupportedStyles: prefer_alias, prefer_alias_method -Style/Alias: - Exclude: - - 'lib/apartment.rb' - - 'lib/apartment/adapters/abstract_adapter.rb' - # Offense count: 16 # Cop supports --auto-correct. # Configuration parameters: EnforcedStyle, ProceduralMethods, FunctionalMethods, IgnoredMethods, AllowBracesOnProceduralOneLiners. diff --git a/lib/apartment.rb b/lib/apartment.rb index 0b5f774e..2f1eceb4 100644 --- a/lib/apartment.rb +++ b/lib/apartment.rb @@ -60,8 +60,8 @@ def default_schema def parallel_migration_threads @parallel_migration_threads || 0 end - alias :default_tenant :default_schema - alias :default_tenant= :default_schema= + alias default_tenant default_schema + alias default_tenant= default_schema= def persistent_schemas @persistent_schemas || [] diff --git a/lib/apartment/adapters/abstract_adapter.rb b/lib/apartment/adapters/abstract_adapter.rb index fe23552f..7598af1b 100644 --- a/lib/apartment/adapters/abstract_adapter.rb +++ b/lib/apartment/adapters/abstract_adapter.rb @@ -48,7 +48,7 @@ def current def default_tenant @default_tenant || Apartment.default_tenant end - alias :default_schema :default_tenant # TODO deprecate default_schema + alias default_schema default_tenant # TODO deprecate default_schema # Drop the tenant # @@ -121,7 +121,7 @@ def seed_data # Don't log the output of seeding the db silence_warnings { load_or_raise(Apartment.seed_data_file) } if Apartment.seed_data_file end - alias_method :seed, :seed_data + alias seed seed_data # Prepend the environment if configured and the environment isn't already there # @@ -219,7 +219,7 @@ def load_or_raise(file) end end # Backward compatibility - alias_method :load_or_abort, :load_or_raise + alias load_or_abort load_or_raise # Exceptions to rescue from on db operations # diff --git a/lib/apartment/console.rb b/lib/apartment/console.rb index ee1220a8..d5a45d57 100644 --- a/lib/apartment/console.rb +++ b/lib/apartment/console.rb @@ -5,7 +5,10 @@ # reloads the environment def reload!(print = true) + # rubocop:disable Rails/Output puts 'Reloading...' if print + # rubocop:enable Rails/Output + # This triggers the to_prepare callbacks ActionDispatch::Callbacks.new(proc {}).call({}) # Manually init Apartment again once classes are reloaded From 7fc7d339ad3dac4524541d1c8454d7f3bc47965b Mon Sep 17 00:00:00 2001 From: Rui Baltazar Date: Sun, 16 Feb 2020 17:23:39 +0800 Subject: [PATCH 30/50] WIP - sorted gemfiels --- .rubocop_todo.yml | 13 ------------- gemfiles/rails_4_2.gemfile | 6 +++--- gemfiles/rails_5_0.gemfile | 4 ++-- gemfiles/rails_5_1.gemfile | 4 ++-- gemfiles/rails_5_2.gemfile | 4 ++-- gemfiles/rails_6_0.gemfile | 4 ++-- gemfiles/rails_master.gemfile | 4 ++-- 7 files changed, 13 insertions(+), 26 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index b22db18e..b738842e 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -6,19 +6,6 @@ # Note that changes in the inspected code, or installation of new # versions of RuboCop, may require this file to be generated again. -# Offense count: 13 -# Cop supports --auto-correct. -# Configuration parameters: TreatCommentsAsGroupSeparators, Include. -# Include: **/*.gemfile, **/Gemfile, **/gems.rb -Bundler/OrderedGems: - Exclude: - - 'gemfiles/rails_4_2.gemfile' - - 'gemfiles/rails_5_0.gemfile' - - 'gemfiles/rails_5_1.gemfile' - - 'gemfiles/rails_5_2.gemfile' - - 'gemfiles/rails_6_0.gemfile' - - 'gemfiles/rails_master.gemfile' - # Offense count: 3 # Cop supports --auto-correct. # Configuration parameters: AllowUnusedKeywordArguments, IgnoreEmptyMethods. diff --git a/gemfiles/rails_4_2.gemfile b/gemfiles/rails_4_2.gemfile index acec9a0d..33048072 100644 --- a/gemfiles/rails_4_2.gemfile +++ b/gemfiles/rails_4_2.gemfile @@ -7,19 +7,19 @@ source 'http://rubygems.org' gem 'rails', '~> 4.2.0' group :local do - gem 'pry' gem 'guard-rspec', '~> 4.2' + gem 'pry' end platforms :ruby do - gem 'pg', '< 1.0.0' gem 'mysql2', '~> 0.4.0' + gem 'pg', '< 1.0.0' end platforms :jruby do gem 'activerecord-jdbc-adapter', '~> 1.3' - gem 'activerecord-jdbcpostgresql-adapter', '~> 1.3' gem 'activerecord-jdbcmysql-adapter', '~> 1.3' + gem 'activerecord-jdbcpostgresql-adapter', '~> 1.3' end gemspec path: '../' diff --git a/gemfiles/rails_5_0.gemfile b/gemfiles/rails_5_0.gemfile index c1188bc2..3d1c50a4 100644 --- a/gemfiles/rails_5_0.gemfile +++ b/gemfiles/rails_5_0.gemfile @@ -7,8 +7,8 @@ source 'http://rubygems.org' gem 'rails', '~> 5.0.0' group :local do - gem 'pry' gem 'guard-rspec', '~> 4.2' + gem 'pry' end platforms :ruby do @@ -17,8 +17,8 @@ end platforms :jruby do gem 'activerecord-jdbc-adapter', '~> 50.0' - gem 'activerecord-jdbcpostgresql-adapter', '~> 50.0' gem 'activerecord-jdbcmysql-adapter', '~> 50.0' + gem 'activerecord-jdbcpostgresql-adapter', '~> 50.0' end gemspec path: '../' diff --git a/gemfiles/rails_5_1.gemfile b/gemfiles/rails_5_1.gemfile index 3e738bf1..fd285b95 100644 --- a/gemfiles/rails_5_1.gemfile +++ b/gemfiles/rails_5_1.gemfile @@ -7,8 +7,8 @@ source 'http://rubygems.org' gem 'rails', '~> 5.1.0' group :local do - gem 'pry' gem 'guard-rspec', '~> 4.2' + gem 'pry' end platforms :ruby do @@ -17,8 +17,8 @@ end platforms :jruby do gem 'activerecord-jdbc-adapter', '~> 51.0' - gem 'activerecord-jdbcpostgresql-adapter', '~> 51.0' gem 'activerecord-jdbcmysql-adapter', '~> 51.0' + gem 'activerecord-jdbcpostgresql-adapter', '~> 51.0' end gemspec path: '../' diff --git a/gemfiles/rails_5_2.gemfile b/gemfiles/rails_5_2.gemfile index 57d1aac4..fb42246f 100644 --- a/gemfiles/rails_5_2.gemfile +++ b/gemfiles/rails_5_2.gemfile @@ -7,14 +7,14 @@ source 'http://rubygems.org' gem 'rails', '~> 5.2.0' group :local do - gem 'pry' gem 'guard-rspec', '~> 4.2' + gem 'pry' end platforms :jruby do gem 'activerecord-jdbc-adapter', '~> 52.0' - gem 'activerecord-jdbcpostgresql-adapter', '~> 52.0' gem 'activerecord-jdbcmysql-adapter', '~> 52.0' + gem 'activerecord-jdbcpostgresql-adapter', '~> 52.0' end gemspec path: '../' diff --git a/gemfiles/rails_6_0.gemfile b/gemfiles/rails_6_0.gemfile index 58cb5b6e..ec151217 100644 --- a/gemfiles/rails_6_0.gemfile +++ b/gemfiles/rails_6_0.gemfile @@ -7,8 +7,8 @@ source 'http://rubygems.org' gem 'rails', '~> 6.0.0.rc1' group :local do - gem 'pry' gem 'guard-rspec', '~> 4.2' + gem 'pry' end platforms :ruby do @@ -17,8 +17,8 @@ end platforms :jruby do gem 'activerecord-jdbc-adapter', '~> 60.0.rc1' - gem 'activerecord-jdbcpostgresql-adapter', '~> 60.0.rc1' gem 'activerecord-jdbcmysql-adapter', '~> 60.0.rc1' + gem 'activerecord-jdbcpostgresql-adapter', '~> 60.0.rc1' end gemspec path: '../' diff --git a/gemfiles/rails_master.gemfile b/gemfiles/rails_master.gemfile index a2eed35f..af85bb04 100644 --- a/gemfiles/rails_master.gemfile +++ b/gemfiles/rails_master.gemfile @@ -7,8 +7,8 @@ source 'http://rubygems.org' gem 'rails', git: 'https://github.com/rails/rails.git' group :local do - gem 'pry' gem 'guard-rspec', '~> 4.2' + gem 'pry' end platforms :ruby do @@ -17,8 +17,8 @@ end platforms :jruby do gem 'activerecord-jdbc-adapter', '~> 52.0' - gem 'activerecord-jdbcpostgresql-adapter', '~> 52.0' gem 'activerecord-jdbcmysql-adapter', '~> 52.0' + gem 'activerecord-jdbcpostgresql-adapter', '~> 52.0' end gemspec path: '../' From ea8f97cbf39ab4459a6aeb13f4693d02e2ccc732 Mon Sep 17 00:00:00 2001 From: Rui Baltazar Date: Sun, 16 Feb 2020 17:25:32 +0800 Subject: [PATCH 31/50] WIP - fixed unused argument --- .rubocop_todo.yml | 9 --------- lib/apartment/adapters/abstract_adapter.rb | 2 +- lib/apartment/elevators/generic.rb | 2 +- spec/support/capybara_sessions.rb | 2 +- 4 files changed, 3 insertions(+), 12 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index b738842e..b0913ea8 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -6,15 +6,6 @@ # Note that changes in the inspected code, or installation of new # versions of RuboCop, may require this file to be generated again. -# Offense count: 3 -# Cop supports --auto-correct. -# Configuration parameters: AllowUnusedKeywordArguments, IgnoreEmptyMethods. -Lint/UnusedMethodArgument: - Exclude: - - 'lib/apartment/adapters/abstract_adapter.rb' - - 'lib/apartment/elevators/generic.rb' - - 'spec/support/capybara_sessions.rb' - # Offense count: 5 Metrics/AbcSize: Max: 33 diff --git a/lib/apartment/adapters/abstract_adapter.rb b/lib/apartment/adapters/abstract_adapter.rb index 7598af1b..79e49c08 100644 --- a/lib/apartment/adapters/abstract_adapter.rb +++ b/lib/apartment/adapters/abstract_adapter.rb @@ -237,7 +237,7 @@ def db_connection_config(tenant) Apartment.db_config_for(tenant).clone end - def with_neutral_connection(tenant, &block) + def with_neutral_connection(tenant, &_block) if Apartment.with_multi_server_setup # neutral connection is necessary whenever you need to create/remove a database from a server. # example: when you use postgresql, you need to connect to the default postgresql database before you create your own. diff --git a/lib/apartment/elevators/generic.rb b/lib/apartment/elevators/generic.rb index 8d321aaa..a765486e 100644 --- a/lib/apartment/elevators/generic.rb +++ b/lib/apartment/elevators/generic.rb @@ -25,7 +25,7 @@ def call(env) end end - def parse_tenant_name(request) + def parse_tenant_name(_request) raise 'Override' end end diff --git a/spec/support/capybara_sessions.rb b/spec/support/capybara_sessions.rb index 61c3d8f2..def4985c 100644 --- a/spec/support/capybara_sessions.rb +++ b/spec/support/capybara_sessions.rb @@ -3,7 +3,7 @@ module RSpec module Integration module CapybaraSessions - def in_new_session(&block) + def in_new_session(&_block) yield new_session end From 6c7d1fecd7149acbb7f43ba72d9228d12f7df960 Mon Sep 17 00:00:00 2001 From: Rui Baltazar Date: Sun, 16 Feb 2020 17:26:49 +0800 Subject: [PATCH 32/50] WIP - fixed yaml safe load --- .rubocop_todo.yml | 6 ------ spec/support/config.rb | 2 ++ 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index b0913ea8..ad620566 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -33,12 +33,6 @@ Metrics/LineLength: Metrics/MethodLength: Max: 24 -# Offense count: 1 -# Cop supports --auto-correct. -Security/YAMLLoad: - Exclude: - - 'spec/support/config.rb' - # Offense count: 16 # Cop supports --auto-correct. # Configuration parameters: EnforcedStyle, ProceduralMethods, FunctionalMethods, IgnoredMethods, AllowBracesOnProceduralOneLiners. diff --git a/spec/support/config.rb b/spec/support/config.rb index f4c46c7a..ce91ad1a 100644 --- a/spec/support/config.rb +++ b/spec/support/config.rb @@ -5,7 +5,9 @@ module Apartment module Test def self.config + # rubocop:disable Security/YAMLLoad @config ||= YAML.load(ERB.new(IO.read('spec/config/database.yml')).result) + # rubocop:enable Security/YAMLLoad end end end From 6efd1e52ae95e715b94b63b9949d1fc74e60391a Mon Sep 17 00:00:00 2001 From: Rui Baltazar Date: Sun, 16 Feb 2020 17:27:35 +0800 Subject: [PATCH 33/50] WIP - fixed block delimiters --- .rubocop_todo.yml | 17 ---------------- spec/examples/connection_adapter_examples.rb | 8 ++++---- ...ic_adapter_custom_configuration_example.rb | 4 ++-- spec/examples/generic_adapter_examples.rb | 16 +++++++-------- spec/examples/schema_adapter_examples.rb | 20 +++++++++---------- spec/tasks/apartment_rake_spec.rb | 8 ++++---- spec/tenant_spec.rb | 4 ++-- spec/unit/elevators/generic_spec.rb | 4 ++-- 8 files changed, 32 insertions(+), 49 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index ad620566..460928d9 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -33,23 +33,6 @@ Metrics/LineLength: Metrics/MethodLength: Max: 24 -# Offense count: 16 -# Cop supports --auto-correct. -# Configuration parameters: EnforcedStyle, ProceduralMethods, FunctionalMethods, IgnoredMethods, AllowBracesOnProceduralOneLiners. -# SupportedStyles: line_count_based, semantic, braces_for_chaining, always_braces -# ProceduralMethods: benchmark, bm, bmbm, create, each_with_object, measure, new, realtime, tap, with_object -# FunctionalMethods: let, let!, subject, watch -# IgnoredMethods: lambda, proc, it -Style/BlockDelimiters: - Exclude: - - '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/tasks/apartment_rake_spec.rb' - - 'spec/tenant_spec.rb' - - 'spec/unit/elevators/generic_spec.rb' - # Offense count: 5 # Cop supports --auto-correct. # Configuration parameters: Keywords. diff --git a/spec/examples/connection_adapter_examples.rb b/spec/examples/connection_adapter_examples.rb index 033ded4f..973ed1fa 100644 --- a/spec/examples/connection_adapter_examples.rb +++ b/spec/examples/connection_adapter_examples.rb @@ -28,17 +28,17 @@ describe '#drop' do it 'should raise an error for unknown database' do - expect { + expect do subject.drop 'unknown_database' - }.to raise_error(Apartment::TenantNotFound) + end.to raise_error(Apartment::TenantNotFound) end end describe '#switch!' do it 'should raise an error if database is invalid' do - expect { + expect do subject.switch! 'unknown_database' - }.to raise_error(Apartment::TenantNotFound) + end.to raise_error(Apartment::TenantNotFound) end end end diff --git a/spec/examples/generic_adapter_custom_configuration_example.rb b/spec/examples/generic_adapter_custom_configuration_example.rb index a5fece4c..07e2c719 100644 --- a/spec/examples/generic_adapter_custom_configuration_example.rb +++ b/spec/examples/generic_adapter_custom_configuration_example.rb @@ -45,9 +45,9 @@ end context 'database key from tenant name' do - let(:expected_args) { + let(:expected_args) do custom_db_conf.tap { |args| args.delete(:database) } - } + end describe '#switch!' do it 'should connect to new db' do diff --git a/spec/examples/generic_adapter_examples.rb b/spec/examples/generic_adapter_examples.rb index 6da9bd56..252ac47c 100644 --- a/spec/examples/generic_adapter_examples.rb +++ b/spec/examples/generic_adapter_examples.rb @@ -5,10 +5,10 @@ shared_examples_for 'a generic apartment adapter' do include Apartment::Spec::AdapterRequirements - before { + before do Apartment.prepend_environment = false Apartment.append_environment = false - } + end describe '#init' do it 'should not retain a connection after railtie' do @@ -67,9 +67,9 @@ begin Dir.mktmpdir do |tmpdir| Apartment.database_schema_file = "#{tmpdir}/schema.rb" - expect { + expect do subject.create(db1) - }.to raise_error(Apartment::FileNotFound) + end.to raise_error(Apartment::FileNotFound) end ensure Apartment.remove_instance_variable(:@database_schema_file) @@ -96,9 +96,9 @@ end it 'should raise an error if database is invalid' do - expect { + expect do subject.switch! 'unknown_database' - }.to raise_error(Apartment::ApartmentError) + end.to raise_error(Apartment::ApartmentError) end end @@ -115,9 +115,9 @@ it 'should not throw exception if current is no longer accessible' do subject.switch!(db2) - expect { + expect do subject.switch(db1) { subject.drop(db2) } - }.to_not raise_error + end.to_not raise_error end end diff --git a/spec/examples/schema_adapter_examples.rb b/spec/examples/schema_adapter_examples.rb index c96f7038..d253ced9 100644 --- a/spec/examples/schema_adapter_examples.rb +++ b/spec/examples/schema_adapter_examples.rb @@ -81,9 +81,9 @@ context 'numeric database names' do let(:db) { 1234 } it 'should allow them' do - expect { + expect do subject.create(db) - }.to_not raise_error + end.to_not raise_error expect(tenant_names).to include(db.to_s) end @@ -93,9 +93,9 @@ describe '#drop' do it 'should raise an error for unknown database' do - expect { + expect do subject.drop 'unknown_database' - }.to raise_error(Apartment::TenantNotFound) + end.to raise_error(Apartment::TenantNotFound) end context 'numeric database names' do @@ -103,9 +103,9 @@ it 'should be able to drop them' do subject.create(db) - expect { + expect do subject.drop(db) - }.to_not raise_error + end.to_not raise_error expect(tenant_names).not_to include(db.to_s) end @@ -175,9 +175,9 @@ end it 'should raise an error if schema is invalid' do - expect { + expect do subject.switch! 'unknown_schema' - }.to raise_error(Apartment::TenantNotFound) + end.to raise_error(Apartment::TenantNotFound) end context 'numeric databases' do @@ -185,9 +185,9 @@ it 'should connect to them' do subject.create(db) - expect { + expect do subject.switch!(db) - }.to_not raise_error + end.to_not raise_error expect(connection.schema_search_path).to start_with %("#{db}") end diff --git a/spec/tasks/apartment_rake_spec.rb b/spec/tasks/apartment_rake_spec.rb index 451517b8..f71727a9 100644 --- a/spec/tasks/apartment_rake_spec.rb +++ b/spec/tasks/apartment_rake_spec.rb @@ -56,9 +56,9 @@ end it 'requires a version to migrate to' do - expect { + expect do @rake['apartment:migrate:up'].invoke - }.to raise_error('VERSION is required') + end.to raise_error('VERSION is required') end end @@ -81,9 +81,9 @@ end it 'requires a version to migrate to' do - expect { + expect do @rake['apartment:migrate:down'].invoke - }.to raise_error('VERSION is required') + end.to raise_error('VERSION is required') end end diff --git a/spec/tenant_spec.rb b/spec/tenant_spec.rb index d98325ec..f2b756b5 100644 --- a/spec/tenant_spec.rb +++ b/spec/tenant_spec.rb @@ -72,9 +72,9 @@ it 'raises exception with invalid adapter specified' do subject.reload!(config.merge(adapter: 'unknown')) - expect { + expect do Apartment::Tenant.adapter - }.to raise_error(RuntimeError) + end.to raise_error(RuntimeError) end context 'threadsafety' do diff --git a/spec/unit/elevators/generic_spec.rb b/spec/unit/elevators/generic_spec.rb index 1b12044a..ae54fca1 100644 --- a/spec/unit/elevators/generic_spec.rb +++ b/spec/unit/elevators/generic_spec.rb @@ -22,9 +22,9 @@ def parse_tenant_name(*) end it 'raises if parse_tenant_name not implemented' do - expect { + expect do elevator.call('HTTP_HOST' => 'foo.bar.com') - }.to raise_error(RuntimeError) + end.to raise_error(RuntimeError) end it 'switches to the parsed db_name' do From edb6ced566fb5a779dc8866d39964c38193b2bfb Mon Sep 17 00:00:00 2001 From: Rui Baltazar Date: Sun, 16 Feb 2020 17:29:52 +0800 Subject: [PATCH 34/50] WIP - fixed keyword todo and empty method definition --- .rubocop_todo.yml | 18 ------------------ lib/apartment.rb | 2 +- lib/apartment/adapters/abstract_adapter.rb | 2 +- .../app/controllers/application_controller.rb | 3 +-- spec/tenant_spec.rb | 6 +++--- 5 files changed, 6 insertions(+), 25 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 460928d9..1dc2448e 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -33,24 +33,6 @@ Metrics/LineLength: Metrics/MethodLength: Max: 24 -# Offense count: 5 -# Cop supports --auto-correct. -# Configuration parameters: Keywords. -# Keywords: TODO, FIXME, OPTIMIZE, HACK, REVIEW -Style/CommentAnnotation: - Exclude: - - 'lib/apartment.rb' - - 'lib/apartment/adapters/abstract_adapter.rb' - - 'spec/tenant_spec.rb' - -# Offense count: 1 -# Cop supports --auto-correct. -# Configuration parameters: EnforcedStyle. -# SupportedStyles: compact, expanded -Style/EmptyMethod: - Exclude: - - 'spec/dummy/app/controllers/application_controller.rb' - # Offense count: 14 # Cop supports --auto-correct. Style/ExpandPathArguments: diff --git a/lib/apartment.rb b/lib/apartment.rb index 2f1eceb4..ac0e3320 100644 --- a/lib/apartment.rb +++ b/lib/apartment.rb @@ -54,7 +54,7 @@ def excluded_models end def default_schema - @default_schema || 'public' # TODO 'public' is postgres specific + @default_schema || 'public' # TODO: 'public' is postgres specific end def parallel_migration_threads diff --git a/lib/apartment/adapters/abstract_adapter.rb b/lib/apartment/adapters/abstract_adapter.rb index 79e49c08..ced5c69b 100644 --- a/lib/apartment/adapters/abstract_adapter.rb +++ b/lib/apartment/adapters/abstract_adapter.rb @@ -48,7 +48,7 @@ def current def default_tenant @default_tenant || Apartment.default_tenant end - alias default_schema default_tenant # TODO deprecate default_schema + alias default_schema default_tenant # TODO: deprecate default_schema # Drop the tenant # diff --git a/spec/dummy/app/controllers/application_controller.rb b/spec/dummy/app/controllers/application_controller.rb index bfa2456c..3615e013 100644 --- a/spec/dummy/app/controllers/application_controller.rb +++ b/spec/dummy/app/controllers/application_controller.rb @@ -3,6 +3,5 @@ class ApplicationController < ActionController::Base protect_from_forgery - def index - end + def index; end end diff --git a/spec/tenant_spec.rb b/spec/tenant_spec.rb index f2b756b5..aa32a0ff 100644 --- a/spec/tenant_spec.rb +++ b/spec/tenant_spec.rb @@ -13,7 +13,7 @@ end end - # TODO this doesn't belong here, but there aren't integration tests currently for mysql + # 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 @@ -31,7 +31,7 @@ # end end - # TODO re-organize these tests + # TODO: re-organize these tests context 'with prefix and schemas' do describe '#create' do before do @@ -90,7 +90,7 @@ end end - # TODO above spec are also with use_schemas=true + # TODO: above spec are also with use_schemas=true context 'with schemas' do before do Apartment.configure do |config| From e6f3e44836133a2f6557f0cf933f4c8243b567af Mon Sep 17 00:00:00 2001 From: Rui Baltazar Date: Sun, 16 Feb 2020 17:31:01 +0800 Subject: [PATCH 35/50] WIP - fixed expand path --- .rubocop_todo.yml | 16 ---------------- .../apartment/install/install_generator.rb | 2 +- spec/adapters/sqlite3_adapter_spec.rb | 2 +- spec/dummy/config/application.rb | 2 +- spec/dummy/config/boot.rb | 4 ++-- spec/dummy/config/environment.rb | 2 +- spec/dummy/script/rails | 4 ++-- .../test/dummy/config/application.rb | 2 +- spec/dummy_engine/test/dummy/config/boot.rb | 4 ++-- .../test/dummy/config/environment.rb | 2 +- spec/integration/use_within_an_engine_spec.rb | 2 +- spec/spec_helper.rb | 2 +- 12 files changed, 14 insertions(+), 30 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 1dc2448e..e1fcf700 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -33,22 +33,6 @@ Metrics/LineLength: Metrics/MethodLength: Max: 24 -# Offense count: 14 -# Cop supports --auto-correct. -Style/ExpandPathArguments: - Exclude: - - 'lib/generators/apartment/install/install_generator.rb' - - 'spec/adapters/sqlite3_adapter_spec.rb' - - 'spec/dummy/config/application.rb' - - 'spec/dummy/config/boot.rb' - - 'spec/dummy/config/environment.rb' - - 'spec/dummy/script/rails' - - 'spec/dummy_engine/test/dummy/config/application.rb' - - 'spec/dummy_engine/test/dummy/config/boot.rb' - - 'spec/dummy_engine/test/dummy/config/environment.rb' - - 'spec/integration/use_within_an_engine_spec.rb' - - 'spec/spec_helper.rb' - # Offense count: 1 # Cop supports --auto-correct. # Configuration parameters: EnforcedStyle. diff --git a/lib/generators/apartment/install/install_generator.rb b/lib/generators/apartment/install/install_generator.rb index 509cf2e9..9fe7152e 100755 --- a/lib/generators/apartment/install/install_generator.rb +++ b/lib/generators/apartment/install/install_generator.rb @@ -2,7 +2,7 @@ module Apartment class InstallGenerator < Rails::Generators::Base - source_root File.expand_path('../templates', __FILE__) + source_root File.expand_path('templates', __dir__) def copy_files template 'apartment.rb', File.join('config', 'initializers', 'apartment.rb') diff --git a/spec/adapters/sqlite3_adapter_spec.rb b/spec/adapters/sqlite3_adapter_spec.rb index 40388acd..252cdb66 100644 --- a/spec/adapters/sqlite3_adapter_spec.rb +++ b/spec/adapters/sqlite3_adapter_spec.rb @@ -10,7 +10,7 @@ context 'using connections' do def tenant_names - db_dir = File.expand_path('../../dummy/db', __FILE__) + db_dir = File.expand_path('../dummy/db', __dir__) Dir.glob("#{db_dir}/*.sqlite3").map { |file| File.basename(file, '.sqlite3') } end diff --git a/spec/dummy/config/application.rb b/spec/dummy/config/application.rb index c633c115..49b5d459 100644 --- a/spec/dummy/config/application.rb +++ b/spec/dummy/config/application.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -require File.expand_path('../boot', __FILE__) +require File.expand_path('boot', __dir__) require 'active_model/railtie' require 'active_record/railtie' diff --git a/spec/dummy/config/boot.rb b/spec/dummy/config/boot.rb index 1f976a16..0c68bf2c 100644 --- a/spec/dummy/config/boot.rb +++ b/spec/dummy/config/boot.rb @@ -2,7 +2,7 @@ require 'rubygems' -gemfile = File.expand_path('../../../../Gemfile', __FILE__) +gemfile = File.expand_path('../../../Gemfile', __dir__) if File.exist?(gemfile) ENV['BUNDLE_GEMFILE'] = gemfile @@ -10,4 +10,4 @@ Bundler.setup end -$LOAD_PATH.unshift File.expand_path('../../../../lib', __FILE__) +$LOAD_PATH.unshift File.expand_path('../../../lib', __dir__) diff --git a/spec/dummy/config/environment.rb b/spec/dummy/config/environment.rb index 1a1e336f..65c03fc7 100644 --- a/spec/dummy/config/environment.rb +++ b/spec/dummy/config/environment.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true # Load the rails application -require File.expand_path('../application', __FILE__) +require File.expand_path('application', __dir__) # Initialize the rails application Dummy::Application.initialize! diff --git a/spec/dummy/script/rails b/spec/dummy/script/rails index 1a08aed0..e1068b28 100755 --- a/spec/dummy/script/rails +++ b/spec/dummy/script/rails @@ -3,6 +3,6 @@ # This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application. -APP_PATH = File.expand_path('../../config/application', __FILE__) -require File.expand_path('../../config/boot', __FILE__) +APP_PATH = File.expand_path('../config/application', __dir__) +require File.expand_path('../config/boot', __dir__) require 'rails/commands' diff --git a/spec/dummy_engine/test/dummy/config/application.rb b/spec/dummy_engine/test/dummy/config/application.rb index 2562455c..0984f6ce 100644 --- a/spec/dummy_engine/test/dummy/config/application.rb +++ b/spec/dummy_engine/test/dummy/config/application.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -require File.expand_path('../boot', __FILE__) +require File.expand_path('boot', __dir__) require 'rails/all' diff --git a/spec/dummy_engine/test/dummy/config/boot.rb b/spec/dummy_engine/test/dummy/config/boot.rb index 18536532..6d2cba07 100644 --- a/spec/dummy_engine/test/dummy/config/boot.rb +++ b/spec/dummy_engine/test/dummy/config/boot.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true # Set up gems listed in the Gemfile. -ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../../../Gemfile', __FILE__) +ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../../Gemfile', __dir__) require 'bundler/setup' if File.exist?(ENV['BUNDLE_GEMFILE']) -$LOAD_PATH.unshift File.expand_path('../../../../lib', __FILE__) +$LOAD_PATH.unshift File.expand_path('../../../lib', __dir__) diff --git a/spec/dummy_engine/test/dummy/config/environment.rb b/spec/dummy_engine/test/dummy/config/environment.rb index 3fa66218..32d57aa4 100644 --- a/spec/dummy_engine/test/dummy/config/environment.rb +++ b/spec/dummy_engine/test/dummy/config/environment.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true # Load the Rails application. -require File.expand_path('../application', __FILE__) +require File.expand_path('application', __dir__) # Initialize the Rails application. Rails.application.initialize! diff --git a/spec/integration/use_within_an_engine_spec.rb b/spec/integration/use_within_an_engine_spec.rb index d9592108..072efac6 100644 --- a/spec/integration/use_within_an_engine_spec.rb +++ b/spec/integration/use_within_an_engine_spec.rb @@ -2,7 +2,7 @@ describe 'using apartment within an engine' do before do - engine_path = Pathname.new(File.expand_path('../../dummy_engine', __FILE__)) + engine_path = Pathname.new(File.expand_path('../dummy_engine', __dir__)) require engine_path.join('test/dummy/config/application') @rake = Rake::Application.new Rake.application = @rake diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index e16b2af8..68b9c680 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -5,7 +5,7 @@ # Configure Rails Environment ENV['RAILS_ENV'] = 'test' -require File.expand_path('../dummy/config/environment.rb', __FILE__) +require File.expand_path('dummy/config/environment.rb', __dir__) # Loading dummy applications affects table_name of each excluded models # defined in `spec/dummy/config/initializers/apartment.rb`. From 8f55e25736f893cbea13a741c62d9e3d7983dc9f Mon Sep 17 00:00:00 2001 From: Rui Baltazar Date: Sun, 16 Feb 2020 17:35:07 +0800 Subject: [PATCH 36/50] WIP - fixed a couple more rules --- .rubocop_todo.yml | 21 --------------------- lib/apartment/adapters/abstract_adapter.rb | 10 +++------- lib/tasks/apartment.rake | 18 +++++++++--------- spec/support/apartment_helpers.rb | 2 +- 4 files changed, 13 insertions(+), 38 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index e1fcf700..b2abff96 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -33,27 +33,6 @@ Metrics/LineLength: Metrics/MethodLength: Max: 24 -# Offense count: 1 -# Cop supports --auto-correct. -# Configuration parameters: EnforcedStyle. -# SupportedStyles: format, sprintf, percent -Style/FormatString: - Exclude: - - 'spec/support/apartment_helpers.rb' - -# Offense count: 1 -# Configuration parameters: . -# SupportedStyles: annotated, template, unannotated -Style/FormatStringToken: - EnforcedStyle: unannotated - -# Offense count: 2 -# Configuration parameters: MinBodyLength. -Style/GuardClause: - Exclude: - - 'lib/apartment/adapters/abstract_adapter.rb' - - 'lib/tasks/apartment.rake' - # Offense count: 6 # Cop supports --auto-correct. Style/IfUnlessModifier: diff --git a/lib/apartment/adapters/abstract_adapter.rb b/lib/apartment/adapters/abstract_adapter.rb index ced5c69b..ca686ff0 100644 --- a/lib/apartment/adapters/abstract_adapter.rb +++ b/lib/apartment/adapters/abstract_adapter.rb @@ -2,7 +2,6 @@ module Apartment module Adapters - # rubocop:disable Metrics/ClassLength class AbstractAdapter include ActiveSupport::Callbacks define_callbacks :create, :switch @@ -212,11 +211,9 @@ def multi_tenantify_with_tenant_db_name(config, tenant) # Load a file or raise error if it doesn't exists # def load_or_raise(file) - if File.exist?(file) - load(file) - else - raise FileNotFound, "#{file} doesn't exist yet" - end + raise FileNotFound, "#{file} doesn't exist yet" unless File.exist?(file) + + load(file) end # Backward compatibility alias load_or_abort load_or_raise @@ -268,6 +265,5 @@ def raise_connect_error!(tenant, exception) class SeparateDbConnectionHandler < ::ActiveRecord::Base end end - # rubocop:enable Metrics/ClassLength end end diff --git a/lib/tasks/apartment.rake b/lib/tasks/apartment.rake index 10c4613a..437deda7 100644 --- a/lib/tasks/apartment.rake +++ b/lib/tasks/apartment.rake @@ -131,16 +131,16 @@ apartment_namespace = namespace :apartment do end def warn_if_tenants_empty - if tenants.empty? && ENV['IGNORE_EMPTY_TENANTS'] != 'true' - puts <<-WARNING - [WARNING] - The list of tenants to migrate appears to be empty. This could mean a few things: + return unless tenants.empty? && ENV['IGNORE_EMPTY_TENANTS'] != 'true' - 1. You may not have created any, in which case you can ignore this message - 2. You've run `apartment:migrate` directly without loading the Rails environment - * `apartment:migrate` is now deprecated. Tenants will automatically be migrated with `db:migrate` + puts <<-WARNING + [WARNING] - The list of tenants to migrate appears to be empty. This could mean a few things: - Note that your tenants currently haven't been migrated. You'll need to run `db:migrate` to rectify this. - WARNING - end + 1. You may not have created any, in which case you can ignore this message + 2. You've run `apartment:migrate` directly without loading the Rails environment + * `apartment:migrate` is now deprecated. Tenants will automatically be migrated with `db:migrate` + + Note that your tenants currently haven't been migrated. You'll need to run `db:migrate` to rectify this. + WARNING end end diff --git a/spec/support/apartment_helpers.rb b/spec/support/apartment_helpers.rb index 485143ac..257185b9 100644 --- a/spec/support/apartment_helpers.rb +++ b/spec/support/apartment_helpers.rb @@ -15,7 +15,7 @@ def reset def next_db @x ||= 0 - 'db%d' % @x += 1 + format('db%d', @x += 1) end def drop_schema(schema) From 5b04164688066e68fb40e003f6edb538b38a3776 Mon Sep 17 00:00:00 2001 From: Rui Baltazar Date: Sun, 16 Feb 2020 17:36:53 +0800 Subject: [PATCH 37/50] WIP - --- spec/support/apartment_helpers.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/support/apartment_helpers.rb b/spec/support/apartment_helpers.rb index 257185b9..d45ea1b3 100644 --- a/spec/support/apartment_helpers.rb +++ b/spec/support/apartment_helpers.rb @@ -15,7 +15,7 @@ def reset def next_db @x ||= 0 - format('db%d', @x += 1) + format('db%d', db_idx: @x += 1) end def drop_schema(schema) From 0dcea87fba9d5deb8b8126228005174698ffcf56 Mon Sep 17 00:00:00 2001 From: Rui Baltazar Date: Sun, 16 Feb 2020 17:38:36 +0800 Subject: [PATCH 38/50] WIP - fixed if unless modifier --- .pryrc | 4 +--- .rubocop_todo.yml | 10 ---------- lib/apartment/adapters/abstract_adapter.rb | 4 +--- lib/apartment/adapters/postgresql_adapter.rb | 4 +--- 4 files changed, 3 insertions(+), 19 deletions(-) diff --git a/.pryrc b/.pryrc index 6d4a3df8..a6e67e41 100644 --- a/.pryrc +++ b/.pryrc @@ -1,5 +1,3 @@ # frozen_string_literal: true -if defined?(Rails) && Rails.env - extend Rails::ConsoleMethods -end +extend Rails::ConsoleMethods if defined?(Rails) && Rails.env diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index b2abff96..17d735d5 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -33,16 +33,6 @@ Metrics/LineLength: Metrics/MethodLength: Max: 24 -# Offense count: 6 -# Cop supports --auto-correct. -Style/IfUnlessModifier: - Exclude: - - '.pryrc' - - 'lib/apartment.rb' - - 'lib/apartment/adapters/abstract_adapter.rb' - - 'lib/apartment/adapters/jdbc_postgresql_adapter.rb' - - 'lib/apartment/adapters/postgresql_adapter.rb' - # Offense count: 1 Style/MixinUsage: Exclude: diff --git a/lib/apartment/adapters/abstract_adapter.rb b/lib/apartment/adapters/abstract_adapter.rb index ca686ff0..171a7ef4 100644 --- a/lib/apartment/adapters/abstract_adapter.rb +++ b/lib/apartment/adapters/abstract_adapter.rb @@ -198,9 +198,7 @@ def import_database_schema # if false, use the default db name from the db def multi_tenantify(tenant, with_database = true) db_connection_config(tenant).tap do |config| - if with_database - multi_tenantify_with_tenant_db_name(config, tenant) - end + multi_tenantify_with_tenant_db_name(config, tenant) if with_database end end diff --git a/lib/apartment/adapters/postgresql_adapter.rb b/lib/apartment/adapters/postgresql_adapter.rb index 73a1643e..88459afb 100644 --- a/lib/apartment/adapters/postgresql_adapter.rb +++ b/lib/apartment/adapters/postgresql_adapter.rb @@ -73,9 +73,7 @@ def connect_to_new(tenant = nil) # When the PostgreSQL version is < 9.3, # there is a issue for prepared statement with changing search_path. # https://www.postgresql.org/docs/9.3/static/sql-prepare.html - if postgresql_version < 90_300 - Apartment.connection.clear_cache! - end + Apartment.connection.clear_cache! if postgresql_version < 90_300 rescue *rescuable_exceptions raise TenantNotFound, "One of the following schema(s) is invalid: \"#{tenant}\" #{full_search_path}" end From 0af178428f95ffa20df518e50723eb5075391040 Mon Sep 17 00:00:00 2001 From: Rui Baltazar Date: Sun, 16 Feb 2020 17:39:17 +0800 Subject: [PATCH 39/50] WIP - fixed multiline modifier --- .rubocop_todo.yml | 8 -------- lib/apartment/adapters/sqlite3_adapter.rb | 18 ++++++++++++------ lib/apartment/elevators/host_hash.rb | 6 ++++-- spec/support/requirements.rb | 4 +++- 4 files changed, 19 insertions(+), 17 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 17d735d5..919d6203 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -38,14 +38,6 @@ Style/MixinUsage: Exclude: - '.pryrc' -# Offense count: 5 -# Cop supports --auto-correct. -Style/MultilineIfModifier: - Exclude: - - 'lib/apartment/adapters/sqlite3_adapter.rb' - - 'lib/apartment/elevators/host_hash.rb' - - 'spec/support/requirements.rb' - # Offense count: 7 # Cop supports --auto-correct. # Configuration parameters: EnforcedStyle. diff --git a/lib/apartment/adapters/sqlite3_adapter.rb b/lib/apartment/adapters/sqlite3_adapter.rb index 78f26adc..44896ef4 100644 --- a/lib/apartment/adapters/sqlite3_adapter.rb +++ b/lib/apartment/adapters/sqlite3_adapter.rb @@ -18,8 +18,10 @@ def initialize(config) end def drop(tenant) - raise TenantNotFound, - "The tenant #{environmentify(tenant)} cannot be found." unless File.exist?(database_file(tenant)) + unless File.exist?(database_file(tenant)) + raise TenantNotFound, + "The tenant #{environmentify(tenant)} cannot be found." + end File.delete(database_file(tenant)) end @@ -31,15 +33,19 @@ def current protected def connect_to_new(tenant) - raise TenantNotFound, - "The tenant #{environmentify(tenant)} cannot be found." unless File.exist?(database_file(tenant)) + unless File.exist?(database_file(tenant)) + raise TenantNotFound, + "The tenant #{environmentify(tenant)} cannot be found." + end super database_file(tenant) end def create_tenant(tenant) - raise TenantExists, - "The tenant #{environmentify(tenant)} already exists." if File.exist?(database_file(tenant)) + if File.exist?(database_file(tenant)) + raise TenantExists, + "The tenant #{environmentify(tenant)} already exists." + end begin f = File.new(database_file(tenant), File::CREAT) diff --git a/lib/apartment/elevators/host_hash.rb b/lib/apartment/elevators/host_hash.rb index 306f0319..f68c10cb 100644 --- a/lib/apartment/elevators/host_hash.rb +++ b/lib/apartment/elevators/host_hash.rb @@ -14,8 +14,10 @@ def initialize(app, hash = {}, processor = nil) end def parse_tenant_name(request) - raise TenantNotFound, - "Cannot find tenant for host #{request.host}" unless @hash.key?(request.host) + unless @hash.key?(request.host) + raise TenantNotFound, + "Cannot find tenant for host #{request.host}" + end @hash[request.host] end diff --git a/spec/support/requirements.rb b/spec/support/requirements.rb index cbfbe5ea..0a5fdf7d 100644 --- a/spec/support/requirements.rb +++ b/spec/support/requirements.rb @@ -37,9 +37,11 @@ module AdapterRequirements end %w[subject tenant_names default_tenant].each do |method| + next if defined?(method) + define_method method do raise "You must define a `#{method}` method in your host group" - end unless defined?(method) + end end end end From af4dd0ca06b7bd0af7829acb9556615f5d05d3ea Mon Sep 17 00:00:00 2001 From: Rui Baltazar Date: Sun, 16 Feb 2020 17:40:20 +0800 Subject: [PATCH 40/50] WIP - fixed mutable constants --- .rubocop_todo.yml | 12 ------------ lib/apartment.rb | 4 ++-- lib/apartment/adapters/postgresql_adapter.rb | 2 +- lib/apartment/tasks/enhancements.rb | 4 ++-- 4 files changed, 5 insertions(+), 17 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 919d6203..805f9c54 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -37,15 +37,3 @@ Metrics/MethodLength: Style/MixinUsage: Exclude: - '.pryrc' - -# Offense count: 7 -# Cop supports --auto-correct. -# Configuration parameters: EnforcedStyle. -# SupportedStyles: literals, strict -Style/MutableConstant: - Exclude: - - 'lib/apartment.rb' - - 'lib/apartment/adapters/postgresql_adapter.rb' - - 'lib/apartment/tasks/enhancements.rb' - - 'lib/apartment/version.rb' - - 'spec/dummy_engine/lib/dummy_engine/version.rb' diff --git a/lib/apartment.rb b/lib/apartment.rb index ac0e3320..b0d49758 100644 --- a/lib/apartment.rb +++ b/lib/apartment.rb @@ -11,12 +11,12 @@ class << self extend Forwardable ACCESSOR_METHODS = %i[use_schemas use_sql seed_after_create prepend_environment - append_environment with_multi_server_setup] + append_environment with_multi_server_setup].freeze WRITER_METHODS = %i[tenant_names database_schema_file excluded_models default_schema persistent_schemas connection_class tld_length db_migrate_tenants seed_data_file - parallel_migration_threads pg_excluded_names] + parallel_migration_threads pg_excluded_names].freeze attr_accessor(*ACCESSOR_METHODS) attr_writer(*WRITER_METHODS) diff --git a/lib/apartment/adapters/postgresql_adapter.rb b/lib/apartment/adapters/postgresql_adapter.rb index 88459afb..3e98b627 100644 --- a/lib/apartment/adapters/postgresql_adapter.rb +++ b/lib/apartment/adapters/postgresql_adapter.rb @@ -111,7 +111,7 @@ class PostgresqlSchemaFromSqlAdapter < PostgresqlSchemaAdapter /CREATE SCHEMA public/i, /COMMENT ON SCHEMA public/i - ] + ].freeze def import_database_schema preserving_search_path do diff --git a/lib/apartment/tasks/enhancements.rb b/lib/apartment/tasks/enhancements.rb index 95e32ab7..f9a13206 100644 --- a/lib/apartment/tasks/enhancements.rb +++ b/lib/apartment/tasks/enhancements.rb @@ -6,8 +6,8 @@ module Apartment class RakeTaskEnhancer module TASKS - ENHANCE_BEFORE = %w[db:drop] - ENHANCE_AFTER = %w[db:migrate db:rollback db:migrate:up db:migrate:down db:migrate:redo db:seed] + ENHANCE_BEFORE = %w[db:drop].freeze + ENHANCE_AFTER = %w[db:migrate db:rollback db:migrate:up db:migrate:down db:migrate:redo db:seed].freeze freeze end From 9adea7fe953345ec05b196ff93e2403d13dc2186 Mon Sep 17 00:00:00 2001 From: Rui Baltazar Date: Sun, 16 Feb 2020 17:42:12 +0800 Subject: [PATCH 41/50] WIP - fixed all immediatly fixable issues --- .pryrc | 2 ++ .rubocop_todo.yml | 5 ----- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/.pryrc b/.pryrc index a6e67e41..33d98970 100644 --- a/.pryrc +++ b/.pryrc @@ -1,3 +1,5 @@ # frozen_string_literal: true +# rubocop:disable Style/MixinUsage extend Rails::ConsoleMethods if defined?(Rails) && Rails.env +# rubocop:enable Style/MixinUsage diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 805f9c54..d61a071d 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -32,8 +32,3 @@ Metrics/LineLength: # Configuration parameters: CountComments, ExcludedMethods. Metrics/MethodLength: Max: 24 - -# Offense count: 1 -Style/MixinUsage: - Exclude: - - '.pryrc' From 1b1476ed0be49dcf8540936d933f2ec9f4f08977 Mon Sep 17 00:00:00 2001 From: Rui Baltazar Date: Mon, 17 Feb 2020 12:51:59 +0800 Subject: [PATCH 42/50] WIP - added base application record class --- spec/dummy/app/models/application_record.rb | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 spec/dummy/app/models/application_record.rb diff --git a/spec/dummy/app/models/application_record.rb b/spec/dummy/app/models/application_record.rb new file mode 100644 index 00000000..ba931a20 --- /dev/null +++ b/spec/dummy/app/models/application_record.rb @@ -0,0 +1,5 @@ +# frozen_string_literal: true + +# NOTE: Dummy model base +class ApplicationRecord < ActiveRecord::Base +end From 6e89883cbeae47cb4c581a887e0feb38ffe937a0 Mon Sep 17 00:00:00 2001 From: Rui Baltazar Date: Mon, 17 Feb 2020 12:58:20 +0800 Subject: [PATCH 43/50] WIP - fixed ignored_first_subdomains --- lib/apartment/elevators/host.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/apartment/elevators/host.rb b/lib/apartment/elevators/host.rb index 283ad316..1984341e 100644 --- a/lib/apartment/elevators/host.rb +++ b/lib/apartment/elevators/host.rb @@ -14,12 +14,14 @@ module Elevators # www.a.b.c.d.com => a.b.c.d.com # class Host < Generic - attr_writer ignored_first_subdomains - def self.ignored_first_subdomains @ignored_first_subdomains ||= [] end + def self.ignored_first_subdomains=(arg) + @ignored_first_subdomains = arg + end + def parse_tenant_name(request) return nil if request.host.blank? From d2cf4b908081be488c5d4df73e8cf7d131c3c901 Mon Sep 17 00:00:00 2001 From: Rui Baltazar Date: Mon, 17 Feb 2020 14:14:00 +0800 Subject: [PATCH 44/50] WIP - fixed failing specs after rubocop cleanup --- apartment.gemspec | 2 +- lib/apartment/elevators/subdomain.rb | 6 ++++-- spec/dummy/app/models/application_record.rb | 1 + spec/unit/elevators/domain_spec.rb | 2 +- spec/unit/elevators/generic_spec.rb | 2 +- spec/unit/elevators/host_spec.rb | 2 +- spec/unit/elevators/subdomain_spec.rb | 2 +- 7 files changed, 10 insertions(+), 7 deletions(-) diff --git a/apartment.gemspec b/apartment.gemspec index e00a1304..2a834523 100644 --- a/apartment.gemspec +++ b/apartment.gemspec @@ -42,6 +42,6 @@ Gem::Specification.new do |s| else s.add_development_dependency 'mysql2' s.add_development_dependency 'pg' - s.add_development_dependency 'sqlite3', '~> 1.3.6' + s.add_development_dependency 'sqlite3', '~> 1.4' end end diff --git a/lib/apartment/elevators/subdomain.rb b/lib/apartment/elevators/subdomain.rb index 60e9d022..2f4eb3bc 100644 --- a/lib/apartment/elevators/subdomain.rb +++ b/lib/apartment/elevators/subdomain.rb @@ -9,12 +9,14 @@ module Elevators # Assumes that tenant name should match subdomain # class Subdomain < Generic - attr_writer :excluded_subdomains - def self.excluded_subdomains @excluded_subdomains ||= [] end + def self.excluded_subdomains=(arg) + @excluded_subdomains = arg + end + def parse_tenant_name(request) request_subdomain = subdomain(request.host) diff --git a/spec/dummy/app/models/application_record.rb b/spec/dummy/app/models/application_record.rb index ba931a20..1d405ba4 100644 --- a/spec/dummy/app/models/application_record.rb +++ b/spec/dummy/app/models/application_record.rb @@ -2,4 +2,5 @@ # NOTE: Dummy model base class ApplicationRecord < ActiveRecord::Base + self.abstract_class = true end diff --git a/spec/unit/elevators/domain_spec.rb b/spec/unit/elevators/domain_spec.rb index fc0ade75..9fd1159e 100644 --- a/spec/unit/elevators/domain_spec.rb +++ b/spec/unit/elevators/domain_spec.rb @@ -4,7 +4,7 @@ require 'apartment/elevators/domain' describe Apartment::Elevators::Domain do - subject(:elevator) { described_class.new(proc) } + subject(:elevator) { described_class.new(Proc.new{}) } describe '#parse_tenant_name' do it 'parses the host for a domain name' do diff --git a/spec/unit/elevators/generic_spec.rb b/spec/unit/elevators/generic_spec.rb index ae54fca1..b39d1598 100644 --- a/spec/unit/elevators/generic_spec.rb +++ b/spec/unit/elevators/generic_spec.rb @@ -10,7 +10,7 @@ def parse_tenant_name(*) end end - subject(:elevator) { described_class.new(proc) } + subject(:elevator) { described_class.new(Proc.new{}) } describe '#call' do it 'calls the processor if given' do diff --git a/spec/unit/elevators/host_spec.rb b/spec/unit/elevators/host_spec.rb index f480cbbb..9655611b 100644 --- a/spec/unit/elevators/host_spec.rb +++ b/spec/unit/elevators/host_spec.rb @@ -4,7 +4,7 @@ require 'apartment/elevators/host' describe Apartment::Elevators::Host do - subject(:elevator) { described_class.new(proc) } + subject(:elevator) { described_class.new(Proc.new{}) } describe '#parse_tenant_name' do it 'should return nil when no host' do diff --git a/spec/unit/elevators/subdomain_spec.rb b/spec/unit/elevators/subdomain_spec.rb index 42d9c96a..ff5d5375 100644 --- a/spec/unit/elevators/subdomain_spec.rb +++ b/spec/unit/elevators/subdomain_spec.rb @@ -4,7 +4,7 @@ require 'apartment/elevators/subdomain' describe Apartment::Elevators::Subdomain do - subject(:elevator) { described_class.new(proc) } + subject(:elevator) { described_class.new(Proc.new{}) } describe '#parse_tenant_name' do context 'assuming one tld' do From 7dd413b6c5dd3e09453605341d6a4dae44272a71 Mon Sep 17 00:00:00 2001 From: Rui Baltazar Date: Mon, 17 Feb 2020 14:25:30 +0800 Subject: [PATCH 45/50] WIP - reverted sqlite3 as it is not an issue atm --- apartment.gemspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apartment.gemspec b/apartment.gemspec index 2a834523..e00a1304 100644 --- a/apartment.gemspec +++ b/apartment.gemspec @@ -42,6 +42,6 @@ Gem::Specification.new do |s| else s.add_development_dependency 'mysql2' s.add_development_dependency 'pg' - s.add_development_dependency 'sqlite3', '~> 1.4' + s.add_development_dependency 'sqlite3', '~> 1.3.6' end end From daad79bfca897a0fb38957df5f0e1255acfaca0c Mon Sep 17 00:00:00 2001 From: Rui Baltazar Date: Mon, 17 Feb 2020 14:36:47 +0800 Subject: [PATCH 46/50] WIP - cleaning up rubocop --- .rubocop_todo.yml | 2 +- lib/apartment/elevators/host.rb | 2 ++ lib/apartment/elevators/subdomain.rb | 2 ++ spec/unit/elevators/domain_spec.rb | 2 +- spec/unit/elevators/generic_spec.rb | 2 +- spec/unit/elevators/host_spec.rb | 2 +- spec/unit/elevators/subdomain_spec.rb | 2 +- 7 files changed, 9 insertions(+), 5 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index d61a071d..aaac295c 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -25,7 +25,7 @@ Metrics/ClassLength: # Cop supports --auto-correct. # Configuration parameters: AutoCorrect, AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns. # URISchemes: http, https -Metrics/LineLength: +Layout/LineLength: Max: 237 # Offense count: 4 diff --git a/lib/apartment/elevators/host.rb b/lib/apartment/elevators/host.rb index 1984341e..2db8dfd0 100644 --- a/lib/apartment/elevators/host.rb +++ b/lib/apartment/elevators/host.rb @@ -18,9 +18,11 @@ def self.ignored_first_subdomains @ignored_first_subdomains ||= [] end + # rubocop:disable Style/TrivialAccessors def self.ignored_first_subdomains=(arg) @ignored_first_subdomains = arg end + # rubocop:enable Style/TrivialAccessors def parse_tenant_name(request) return nil if request.host.blank? diff --git a/lib/apartment/elevators/subdomain.rb b/lib/apartment/elevators/subdomain.rb index 2f4eb3bc..38604d60 100644 --- a/lib/apartment/elevators/subdomain.rb +++ b/lib/apartment/elevators/subdomain.rb @@ -13,9 +13,11 @@ def self.excluded_subdomains @excluded_subdomains ||= [] end + # rubocop:disable Style/TrivialAccessors def self.excluded_subdomains=(arg) @excluded_subdomains = arg end + # rubocop:enable Style/TrivialAccessors def parse_tenant_name(request) request_subdomain = subdomain(request.host) diff --git a/spec/unit/elevators/domain_spec.rb b/spec/unit/elevators/domain_spec.rb index 9fd1159e..520b315c 100644 --- a/spec/unit/elevators/domain_spec.rb +++ b/spec/unit/elevators/domain_spec.rb @@ -4,7 +4,7 @@ require 'apartment/elevators/domain' describe Apartment::Elevators::Domain do - subject(:elevator) { described_class.new(Proc.new{}) } + subject(:elevator) { described_class.new(proc {}) } describe '#parse_tenant_name' do it 'parses the host for a domain name' do diff --git a/spec/unit/elevators/generic_spec.rb b/spec/unit/elevators/generic_spec.rb index b39d1598..b282a5af 100644 --- a/spec/unit/elevators/generic_spec.rb +++ b/spec/unit/elevators/generic_spec.rb @@ -10,7 +10,7 @@ def parse_tenant_name(*) end end - subject(:elevator) { described_class.new(Proc.new{}) } + subject(:elevator) { described_class.new(proc {}) } describe '#call' do it 'calls the processor if given' do diff --git a/spec/unit/elevators/host_spec.rb b/spec/unit/elevators/host_spec.rb index 9655611b..e0cb9c3c 100644 --- a/spec/unit/elevators/host_spec.rb +++ b/spec/unit/elevators/host_spec.rb @@ -4,7 +4,7 @@ require 'apartment/elevators/host' describe Apartment::Elevators::Host do - subject(:elevator) { described_class.new(Proc.new{}) } + subject(:elevator) { described_class.new(proc {}) } describe '#parse_tenant_name' do it 'should return nil when no host' do diff --git a/spec/unit/elevators/subdomain_spec.rb b/spec/unit/elevators/subdomain_spec.rb index ff5d5375..4c66ce67 100644 --- a/spec/unit/elevators/subdomain_spec.rb +++ b/spec/unit/elevators/subdomain_spec.rb @@ -4,7 +4,7 @@ require 'apartment/elevators/subdomain' describe Apartment::Elevators::Subdomain do - subject(:elevator) { described_class.new(Proc.new{}) } + subject(:elevator) { described_class.new(proc {}) } describe '#parse_tenant_name' do context 'assuming one tld' do From 4aa741b3e4f28cbd8fa3e4245542354ef91f65fe Mon Sep 17 00:00:00 2001 From: Rui Baltazar Date: Mon, 17 Feb 2020 14:45:10 +0800 Subject: [PATCH 47/50] WIP - --- .rubocop_todo.yml | 7 +------ lib/apartment/adapters/abstract_adapter.rb | 2 ++ lib/apartment/railtie.rb | 9 ++++++--- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index aaac295c..0f709629 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -16,16 +16,11 @@ Metrics/AbcSize: Metrics/BlockLength: Max: 176 -# Offense count: 1 -# Configuration parameters: CountComments. -Metrics/ClassLength: - Max: 155 - # Offense count: 18 # Cop supports --auto-correct. # Configuration parameters: AutoCorrect, AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns. # URISchemes: http, https -Layout/LineLength: +Metrics/LineLength: Max: 237 # Offense count: 4 diff --git a/lib/apartment/adapters/abstract_adapter.rb b/lib/apartment/adapters/abstract_adapter.rb index 171a7ef4..3890d661 100644 --- a/lib/apartment/adapters/abstract_adapter.rb +++ b/lib/apartment/adapters/abstract_adapter.rb @@ -2,6 +2,7 @@ module Apartment module Adapters + # rubocop:disable Metrics/ClassLength class AbstractAdapter include ActiveSupport::Callbacks define_callbacks :create, :switch @@ -264,4 +265,5 @@ class SeparateDbConnectionHandler < ::ActiveRecord::Base end end end + # rubocop:enable Metrics/ClassLength end diff --git a/lib/apartment/railtie.rb b/lib/apartment/railtie.rb index 43bc54a5..b33cab2d 100644 --- a/lib/apartment/railtie.rb +++ b/lib/apartment/railtie.rb @@ -51,8 +51,10 @@ class Railtie < Rails::Railtie end # - # The following initializers are a workaround to the fact that I can't properly hook into the rails reloader - # Note this is technically valid for any environment where cache_classes is false, for us, it's just development + # The following initializers are a workaround to the fact that I can't + # properly hook into the rails reloader + # Note this is technically valid for any environment where cache_classes + # is false, for us, it's just development # if Rails.env.development? @@ -61,7 +63,8 @@ class Railtie < Rails::Railtie app.config.middleware.use Apartment::Reloader end - # Overrides reload! to also call Apartment::Tenant.init as well so that the reloaded classes have the proper table_names + # Overrides reload! to also call Apartment::Tenant.init as well so that the + # reloaded classes have the proper table_names console do require 'apartment/console' end From 68b6cbc4b0e2de0a2a000b05aecba274d620b3de Mon Sep 17 00:00:00 2001 From: Rui Baltazar Date: Mon, 17 Feb 2020 15:43:05 +0800 Subject: [PATCH 48/50] WIP - working on travis config --- .travis.yml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.travis.yml b/.travis.yml index 9c634d9a..bcecef19 100644 --- a/.travis.yml +++ b/.travis.yml @@ -12,6 +12,11 @@ rvm: - 2.5.0 - 2.6.2 - ruby-head + +branches: + only: + - development + gemfile: - gemfiles/rails_4_2.gemfile - gemfiles/rails_5_0.gemfile From e6133862e0c58f7ec7b2ca163c9c6a6362dfa699 Mon Sep 17 00:00:00 2001 From: Rui Baltazar Date: Mon, 17 Feb 2020 15:45:29 +0800 Subject: [PATCH 49/50] WIP - including master in branches to test --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index bcecef19..3fe43feb 100644 --- a/.travis.yml +++ b/.travis.yml @@ -15,6 +15,7 @@ rvm: branches: only: + - master - development gemfile: From 4eb1681658d9eed8f78dd04a4ce34d38211eb2d4 Mon Sep 17 00:00:00 2001 From: Arthur Dingemans Date: Fri, 13 Mar 2020 14:27:06 +0100 Subject: [PATCH 50/50] Don't crash when no database connection is present --- lib/apartment/railtie.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/apartment/railtie.rb b/lib/apartment/railtie.rb index b33cab2d..e082ab9e 100644 --- a/lib/apartment/railtie.rb +++ b/lib/apartment/railtie.rb @@ -35,9 +35,12 @@ class Railtie < Rails::Railtie Apartment::Tenant.init end # rubocop:disable Lint/SuppressedException - rescue ::ActiveRecord::NoDatabaseError + rescue ::ActiveRecord::NoDatabaseError, PG::ConnectionBad # Since `db:create` and other tasks invoke this block from Rails 5.2.0, # we need to swallow the error to execute `db:create` properly. + Rails.logger.warn do + 'Failed to initialize Apartment because a database connection could not be established.' + end end # rubocop:enable Lint/SuppressedException end