influitive / apartment

Database multi-tenancy for Rack (and Rails) applications
2.67k stars 463 forks source link

Incompatible with Postgresql 11.1 #571

Open ArnisL opened 5 years ago

ArnisL commented 5 years ago

Steps to reproduce

Apartment::Tenant.create('test')

Expected behavior

create a new tenant named test

Actual behavior

apparently "forgets" the name and tries to create another "public" schema

System configuration

arch linux something

require 'middleware/my_elevator'

#
# Apartment Configuration
#
Apartment.configure do |config|

  # Exclude the following models from the tenant.
  config.excluded_models = %w(Subdomain SubdomainUser Permission User OnlineUser)

  # Use postgres schemas
  config.use_schemas = true

  # Ensure the seeds.rb file is run when a tenant is created.
  config.seed_after_create = true

  # The list of tenants is driven from the subdomain model, based on sub domain.
  config.tenant_names = lambda { Subdomain.pluck :subdomain }

  # Use the SQL generation for the schemas, rather than schema.rb
  config.use_sql = true

end

# Switch the apartment based on sub domain.
Rails.application.config.middleware.use MyElevator
toao commented 5 years ago

Same issue with Rails 5.2.2 and Ruby 2.5.3

tappleby commented 5 years ago

It seems like newer versions of pg_dump will always include CREATE SCHEMA public; if specifying a schema on the command line.

I ran into something similar while working on an external application for provisioning tenants where create schema command would be included for non public schemas, I ended up replacing the create schema calls in swap_schema_qualifier

new_sql.gsub!(/CREATE SCHEMA #{@source_schema}\;/, '')

I could be worth adding CREATE SCHEMA to the pg dump block list, but there might be some setups where creating more then one schema per tenant is a valid use case? https://github.com/influitive/apartment/blob/a4332c3222210b11f5e8c27fa4c1a18b492e9629/lib/apartment/adapters/postgresql_adapter.rb#L108-L113

toao commented 5 years ago

@tappleby thanks for your feedback, I just used the latter approach for a lokal money patch.

toao commented 5 years ago

does 927ba21a3092b8e4525ccd3ce8f9d7a3ba2afdd1 wich is included in v2.2.1 fixes this finally?