influitive / apartment

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

excluded_models from migrations in engines are run for each tenant #634

Open bjensen opened 4 years ago

bjensen commented 4 years ago

Steps to reproduce

Make a engine and add the following lets call it Project

Add the following to its lib/project/engine.rb:

  class Engine < ::Rails::Engine
    isolate_namespace Project

    initializer :append_migrations do |app|
      unless app.root.to_s.match root.to_s
        config.paths["db/migrate"].expanded.each do |expanded_path|
          app.config.paths["db/migrate"] << expanded_path
        end
      end
    end
  end
end

Add some models in the rails engine e.g. User

now put the rails engine in your Gemfile and do a bundle install

and Add the following to your appartment.rb initializer:

project_models = Project.constants.map{|c| Project.const_get(c) }.select do |const|
  const.class == Class && const < ActiveRecord::Base
end

config.excluded_models = project_models

Expected behavior

The migration for Project::User is run once and located in the public search path

Actual behavior

The migration for Project::User located in the public search path but run for every tenant, so you get this error: PG::DuplicateTable: ERROR: relation "project_users" already exists

System configuration