Skip to content

Commit

Permalink
[Chore] Removed references to allow prepend tenant name
Browse files Browse the repository at this point in the history
  • Loading branch information
rpbaltazar committed Apr 15, 2020
1 parent b27311d commit 3fa374a
Show file tree
Hide file tree
Showing 7 changed files with 1 addition and 107 deletions.
22 changes: 0 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 1 addition & 2 deletions lib/apartment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
32 changes: 0 additions & 32 deletions lib/apartment/arel/visitors/postgresql.rb

This file was deleted.

1 change: 0 additions & 1 deletion lib/apartment/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 0 additions & 21 deletions spec/adapters/jdbc_postgresql_adapter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 0 additions & 21 deletions spec/adapters/postgresql_adapter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 0 additions & 8 deletions spec/unit/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3fa374a

Please sign in to comment.