3

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'
Dinesh Saini
  • 2,917
  • 2
  • 30
  • 48

1 Answers1

17

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 == '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
Carlos Castillo
  • 3,218
  • 2
  • 14
  • 10
rdubya
  • 2,916
  • 1
  • 16
  • 20
  • 1
    but how can we run seed command from model? Lets say in user module there is a function after_create: create_schema def create_schema #create tenant command #>> How can I run seed command here ????? end – Dinesh Saini Dec 08 '14 at 02:21
  • 1
    Just added info about how to do that – rdubya Dec 08 '14 at 03:03
  • Can you add the actual error to the question and the pertinent code that is causing the error? – rdubya Dec 10 '14 at 01:57
  • @rdubya 's answer works perfectly fine.But I need some thing more like to run the seed on background. Because I need to seed all the countries in world with all their state and few other things. it is taking almost 3 mints to complete the seed and the user doesn't get notification till all the seed completes. so I need to run it background . any suggestion on that. – thedudecodes Sep 28 '17 at 09:54
  • The correct way of writing the condition is actually `unless Apartment::Tenant.current == 'public'` (skip _tenant) – Marek Dlugos Mar 16 '19 at 22:16