From 3fa374ad9e4920eb61da779c78b998c2f6e54d19 Mon Sep 17 00:00:00 2001 From: Rui Baltazar Date: Wed, 15 Apr 2020 11:03:56 +0800 Subject: [PATCH] [Chore] Removed references to allow prepend tenant name --- README.md | 22 ------------- lib/apartment.rb | 3 +- lib/apartment/arel/visitors/postgresql.rb | 32 ------------------- lib/apartment/railtie.rb | 1 - spec/adapters/jdbc_postgresql_adapter_spec.rb | 21 ------------ spec/adapters/postgresql_adapter_spec.rb | 21 ------------ spec/unit/config_spec.rb | 8 ----- 7 files changed, 1 insertion(+), 107 deletions(-) delete mode 100644 lib/apartment/arel/visitors/postgresql.rb diff --git a/README.md b/README.md index 81d9aa95..3ac0b6fc 100644 --- a/README.md +++ b/README.md @@ -341,28 +341,6 @@ Rails will always access the 'public' tenant when accessing these models, but no ### Postgresql Schemas -#### Allow prepend tenant name - -`config.allow_prepend_tenant_name` currently defaults to `false`. - -This configuration only applies while using postgres adapter and `config.use_schemas` is set to `true`. -This is also intended to be used in combination with `Apartment::Model` module. What this module -does is to overwrite the `arel_table` method, used internally by Rails to create the arel_table -object and build the final SQL query to include the apartment's current tenant schema name. -This becomes particularly helpful when we have read/write replicas in our rails application and -we need to switch the connection between read and write DB. -When in our application we run `SomeModel.connected_to(role: :reading)` this will switch the connection but also reset the schema that it is pointing to. - -The alternative to this is whenever you switch the connection manually, ensure that you switch to -the expected schema as well. E.g. - -```ruby -ActiveRecord::Base.connected_to(role: :reading) do - Apartment::Tenant.switch!(your_schema) - do_your_logic -end -``` - #### Alternative: Creating new schemas by using raw SQL dumps Apartment can be forced to use raw SQL dumps insted of `schema.rb` for creating new schemas. Use this when you are using some extra features in postgres that can't be represented in `schema.rb`, like materialized views etc. diff --git a/lib/apartment.rb b/lib/apartment.rb index 42c85398..9c0d664e 100644 --- a/lib/apartment.rb +++ b/lib/apartment.rb @@ -20,8 +20,7 @@ class << self extend Forwardable ACCESSOR_METHODS = %i[use_schemas use_sql seed_after_create prepend_environment - append_environment with_multi_server_setup - allow_prepend_tenant_name].freeze + append_environment with_multi_server_setup].freeze WRITER_METHODS = %i[tenant_names database_schema_file excluded_models default_schema persistent_schemas connection_class diff --git a/lib/apartment/arel/visitors/postgresql.rb b/lib/apartment/arel/visitors/postgresql.rb deleted file mode 100644 index 43d98257..00000000 --- a/lib/apartment/arel/visitors/postgresql.rb +++ /dev/null @@ -1,32 +0,0 @@ -# frozen_string_literal: true - -module Arel # :nodoc: all - module Visitors - class PostgreSQL < Arel::Visitors::ToSql - private - - # rubocop:disable Naming/MethodName - # rubocop:disable Naming/MethodParameterName - def visit_Arel_Table(o, collector) - if o.table_alias - collector << quoted_table_name_with_tenant(o.name) << ' ' << quote_table_name(o.table_alias) - else - collector << quoted_table_name_with_tenant(o.name) - end - end - # rubocop:enable Naming/MethodParameterName - # rubocop:enable Naming/MethodName - - def quoted_table_name_with_tenant(table_name) - # NOTE: Only postgres supports schemas, so prepending tenant name - # as part of the table name is only available if configuration - # specifies use_schemas - if Apartment.allow_prepend_tenant_name && Apartment.use_schemas && !table_name.include?('.') - quote_table_name("#{Apartment::Tenant.current}.#{table_name}") - else - quote_table_name(table_name) - end - end - end - end -end diff --git a/lib/apartment/railtie.rb b/lib/apartment/railtie.rb index 7ff83b1a..647b221c 100644 --- a/lib/apartment/railtie.rb +++ b/lib/apartment/railtie.rb @@ -18,7 +18,6 @@ class Railtie < Rails::Railtie config.seed_after_create = false config.prepend_environment = false config.append_environment = false - config.allow_prepend_tenant_name = false end ActiveRecord::Migrator.migrations_paths = Rails.application.paths['db/migrate'].to_a diff --git a/spec/adapters/jdbc_postgresql_adapter_spec.rb b/spec/adapters/jdbc_postgresql_adapter_spec.rb index c78c351e..49dfaf1a 100644 --- a/spec/adapters/jdbc_postgresql_adapter_spec.rb +++ b/spec/adapters/jdbc_postgresql_adapter_spec.rb @@ -20,27 +20,6 @@ def tenant_names it_should_behave_like 'a generic apartment adapter' it_should_behave_like 'a schema based apartment adapter' - - context 'using allow_prepend_tenant_name' do - let(:api) { Apartment::Tenant } - - before do - Apartment.allow_prepend_tenant_name = true - api.create(db1) - api.create(db2) - end - - it 'prepends the tenant schema name to the table name when building the query' do - Apartment::Tenant.switch!(db1) - sql = "SELECT \"users\".* FROM \"#{db1}\".\"users\" LIMIT 10" - # NOTE: for some reason there is an extra space in the output of to_sql - expect(UserWithTenantModel.all.limit(10).to_sql.gsub(/\s+/, ' ')).to eq(sql) - - Apartment::Tenant.switch!(db2) - sql = "SELECT \"users\".* FROM \"#{db2}\".\"users\" LIMIT 10" - expect(UserWithTenantModel.all.limit(10).to_sql.gsub(/\s+/, ' ')).to eq(sql) - end - end end context 'using databases' do diff --git a/spec/adapters/postgresql_adapter_spec.rb b/spec/adapters/postgresql_adapter_spec.rb index f1adcaec..d553ff8b 100644 --- a/spec/adapters/postgresql_adapter_spec.rb +++ b/spec/adapters/postgresql_adapter_spec.rb @@ -20,27 +20,6 @@ def tenant_names it_should_behave_like 'a generic apartment adapter' it_should_behave_like 'a schema based apartment adapter' - - context 'using allow_prepend_tenant_name' do - let(:api) { Apartment::Tenant } - - before do - Apartment.allow_prepend_tenant_name = true - api.create(db1) - api.create(db2) - end - - it 'prepends the tenant schema name to the table name when building the query' do - Apartment::Tenant.switch!(db1) - sql = "SELECT \"users\".* FROM \"#{db1}\".\"users\" LIMIT 10" - # NOTE: for some reason there is an extra space in the output of to_sql - expect(UserWithTenantModel.all.limit(10).to_sql.gsub(/\s+/, ' ')).to eq(sql) - - Apartment::Tenant.switch!(db2) - sql = "SELECT \"users\".* FROM \"#{db2}\".\"users\" LIMIT 10" - expect(UserWithTenantModel.all.limit(10).to_sql.gsub(/\s+/, ' ')).to eq(sql) - end - end end context 'using schemas with SQL dump' do diff --git a/spec/unit/config_spec.rb b/spec/unit/config_spec.rb index e486dac9..cceaa7e4 100644 --- a/spec/unit/config_spec.rb +++ b/spec/unit/config_spec.rb @@ -35,14 +35,6 @@ def tenant_names_from_array(names) expect(Apartment.use_schemas).to be false end - it 'should set allow_prepend_tenant_name' do - Apartment.configure do |config| - config.excluded_models = [] - config.allow_prepend_tenant_name = true - end - expect(Apartment.allow_prepend_tenant_name).to be true - end - it 'should set seed_data_file' do Apartment.configure do |config| config.seed_data_file = seed_data_file_path