Sha256: 19b54c850205578e37a8201c8ecfb7d3c3b67041271c4c692803fe83d67f4368

Contents?: true

Size: 1.63 KB

Versions: 7

Compression:

Stored size: 1.63 KB

Contents

# frozen_string_literal: true

require 'rails_helper'

RSpec.describe PandaPal::Organization, type: :model do
  context "across multiple DBs" do
    with_multiple_shards(
      shards: { alt: "panda_pal_test2" },
      tenant_names: %w[on_primary alt:on_alt],
      transactional: false,
      determine_shard: ->(org) { org.name.include?("alt") ? "alt" : nil }
    )

    let!(:org1) { create(:panda_pal_organization, name: "on_primary") }
    let!(:org2) { create(:panda_pal_organization, name: "on_alt") }

    it "successfully creates tenants on multiple shards" do
      expect(1).to eql(1)
    end

    it "only creates schemas on the target shard" do
      org1.switch_tenant do
        schemas = PandaPal::ApiCall.connection.exec_query("SELECT schema_name FROM information_schema.schemata").pluck("schema_name")
        expect(schemas).to include("on_primary")
        expect(schemas).to_not include("on_alt")
      end
      org2.switch_tenant do
        schemas = PandaPal::ApiCall.connection.exec_query("SELECT schema_name FROM information_schema.schemata").pluck("schema_name")
        expect(schemas).to include("on_alt")
        expect(schemas).to_not include("on_primary")
      end
    end

    context "load_async" do
      it "works across shards" do
        qs = []
        PandaPal::Organization.all.each do |org|
          org.switch_tenant do
            PandaPal::ApiCall.create!(logic: org.name)
            qs << PandaPal::ApiCall.select("logic, pg_sleep(0.5)").load_async
          end
        end
        calls = qs.map(&:to_a).flatten
        expect(calls.pluck(:logic)).to match_array(["on_primary", "on_alt"])
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
panda_pal-5.12.7 spec/core/apartment_multidb_spec.rb
panda_pal-5.12.6 spec/core/apartment_multidb_spec.rb
panda_pal-5.12.5 spec/core/apartment_multidb_spec.rb
panda_pal-5.12.4 spec/core/apartment_multidb_spec.rb
panda_pal-5.12.3 spec/core/apartment_multidb_spec.rb
panda_pal-5.12.2 spec/core/apartment_multidb_spec.rb
panda_pal-5.12.1 spec/core/apartment_multidb_spec.rb