Search code examples
ruby-on-railspostgresqlmulti-tenantsearchkickapartment-gem

How can I use searchkick with pgSQL schemas/apartment and cycle through schemas?


I currently am trying to use searchkick within my multi-tenant Rails 4 app that uses Apartment and pgSQL schemas to handle the tenancy. However, I am unable to get searchkick to work. I am not exactly sure what I am doing wrong, but I am thinking that it might be that when I reindex, it only reindexes on one schema.

I am thinking that I need to cycle through each schema, but I am unsure how to do it in Apartment. They seem to do it automatically for migrations, but I am trying to get it to work with searchkick. I want to edit the below rake task from searchkick to do so:

require 'rake'

namespace :searchkick do 

    task :reindex => :environment do
    if ENV["CLASS"]
      klass = ENV["CLASS"].constantize rescue nil
      if klass
        klass.reindex
      else
        abort "Could not find class: #{ENV["CLASS"]}"
      end
    else
      abort "USAGE: rake searchkick:reindex CLASS=Product"
    end
  end

end

Please let me know if you have any suggestions


Solution

  • I was able to figure it out. I had to do the following and add in the Apartment task to here:

    require 'rake'
    require 'apartment/migrator'
    
    namespace :searchkick do 
        task :reindex => :environment do
            tenants.each do |tenant|
    
                Apartment::Tenant.switch(tenant)
                p 'switched'
            if ENV["CLASS"]
              klass = ENV["CLASS"].constantize rescue nil
              if klass
                klass.reindex
              else
                abort "Could not find class: #{ENV["CLASS"]}"
              end
            else
              abort "USAGE: rake searchkick:reindex CLASS=Product"
            end
        end
        end
    
    end