class ZuoraConnect::AppInstanceBase

def apartment_switch(method = nil, migrate = false)

def apartment_switch(method = nil, migrate = false)
  switch_count ||= 0
  if self.persisted?
    begin
      Apartment::Tenant.switch!(self.id) 
    rescue Apartment::TenantNotFound => ex
      sleep(2)
      begin
        Apartment::Tenant.create(self.id.to_s)
      rescue Apartment::TenantExists => ex
      end
      if (switch_count += 1) < 2
        retry 
      else
        raise 
      end
    end
    if migrate && ActiveRecord::Migrator.needs_migration?
      Apartment::Migrator.migrate(self.id)
    end
  end
  Thread.current[:appinstance] = self
end