How to define seed file for Rails Apartment

2019-04-02 05:04发布

I have set up the schema file but unable to define seed file for tenant such that it can run only for tenant migration only. Also I ma trying to create schema once a user has been created and its tenant is created.

    require 'apartment/elevators/subdomain'

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

      config.excluded_models = ["Admin","Contractor", "ContractorPackage","ContractorTransaction","Country","Currency","Faq","FaqCategory","Language","Package","Page","PaymentType","Setting","TempTransaction","Testimonial","Timezone","Tutorial"]

      # use postgres schemas?
      config.use_schemas = true

       config.tenant_names = lambda{ Contractor.pluck("CONCAT('contractor_',id)") }
    end

    # overriding module schema file here
    module Apartment

      class << self
            def database_schema_file
                @database_schema_file=Rails.root.join('db', 'contractor_schema.rb')
            end
        end

    end


    Rails.application.config.middleware.use 'Apartment::Elevators::Subdomain'

1条回答
够拽才男人
2楼-- · 2019-04-02 06:01

In your seeds.rb file, wrap your code in a check for the current tenant. I don't have anywhere to test this right now, but the following code should get you close:

unless Apartment::Tenant.current_tenant == 'public'
    #Insert seed data
end

If you want to seed a tenant manually you should be able to run Apartment::Tenant.seed

To get the seeds.rb file to run when a tenant is created add:

config.seed_after_create = true

to your apartment initializer file.

For your example:

Apartment.configure do |config|

  config.excluded_models = ["Admin","Contractor", "ContractorPackage","ContractorTransaction","Country","Currency","Faq","FaqCategory","Language","Package","Page","PaymentType","Setting","TempTransaction","Testimonial","Timezone","Tutorial"]

  # use postgres schemas?
  config.use_schemas = true

  config.tenant_names = lambda{ Contractor.pluck("CONCAT('contractor_',id)") }

  config.seed_after_create = true
end
查看更多
登录 后发表回答