def with_multiple_shards(shards:, tenant_names: [], determine_shard: nil, transactional: true)
self.use_transactional_tests = transactional
original_settings = {}
before :all do
if defined?(DatabaseCleaner)
original_settings[:cleaner_strategy] = DatabaseCleaner.strategy
DatabaseCleaner.strategy = :truncation
end
Apartment.configure do |config|
original_settings[:with_multi_server_setup] = config.with_multi_server_setup
config.with_multi_server_setup = true
end
shards.each do |k, v|
Apartment.shard_configurations[k.to_s] = Apartment.connection_config.merge(database: v)
end
@existing_org_ids = PandaPal::Organization.pluck(:id)
Apartment::Tenant.reload!
clean_schemas(tenant_names)
end
after :all do
Apartment::Tenant.reset
Apartment.configure do |config|
config.with_multi_server_setup = false
end
shards.each do |k, _|
Apartment.shard_configurations.delete(k.to_s)
end
PandaPal::Organization.where.not(id: @existing_org_ids).delete_all
if defined?(DatabaseCleaner)
DatabaseCleaner.strategy = original_settings[:cleaner_strategy]
end
end
before :each do
clean_schemas(tenant_names)
@each_existing_org_ids = PandaPal::Organization.pluck(:id)
if determine_shard
allow_any_instance_of(PandaPal::Organization).to receive(:tenant_name) do |org|
shard = determine_shard.call(org)
tenant = org.name
tenant = "#{shard}:#{tenant}" if shard.present?
tenant
end
end
end
after :each do
Apartment::Tenant.reset
PandaPal::Organization.where.not(id: @each_existing_org_ids).destroy_all
end
define_method :clean_schemas do |schemas|
schema_to_fqt = schemas.index_by { |s| Apartment::Tenant.split_tenant(s)[1] }
schemas_to_clean = schema_to_fqt.keys - PandaPal::Organization.pluck(:name)
schemas_to_clean.each do |schema|
Apartment::Tenant.drop(schema) rescue nil
shards.each do |k, v|
Apartment::Tenant.drop("#{k}:#{schema}") rescue nil
end
end
end
end