influitive / apartment

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

Pending Migration Error if default schema is not Public #501

Closed mri-dula closed 6 years ago

mri-dula commented 6 years ago

Steps to reproduce

1) Add the apartment gem to your rails app and change the default schema to internal in apartment.rb 2) Add a new migration rails g migration SomeNewMigration. Add some schema changes in the new migration. Like adding a new table. 3) Run rails db:migrate 4) Start the Rails Server and browse to any route in the app (http://somedomain.lvh.me/something).

Expected behavior

The schema_migrations table in the internal and any other tenant schemas must have the new migration no. inserted. The schema_migrations table of the public schema must be ignored. The app should work fine.

Actual behavior

The schema_migrations table in the internal and any other tenant schemas do have the new migration no. inserted. However, upon browsing the app, I get the ActiveRecord::PendingMigrationError. If I insert the new migration no. into the public.schema_migrations table, it works.

System configuration

OS: Mac OS High Sierra

mri-dula commented 6 years ago

Fixed this by changing the schema_search_path in my database.yml

development:
    adapter: postgresql
    encoding: unicode
    database: myapp
    pool: 5
    username: postgres
    password: ***
    host: localhost
    schema_search_path: 'internal'
mikecmpbll commented 6 years ago

curious. thank you for investigating and providing a solution ⭐️

mikecmpbll commented 6 years ago

ah yes: https://github.com/influitive/apartment#2-ensure-the-schema-is-in-rails-default-connection