diff --git a/lib/apartment/active_record/postgresql_adapter.rb b/lib/apartment/active_record/postgresql_adapter.rb index 3d1d2043..ca0adbcb 100644 --- a/lib/apartment/active_record/postgresql_adapter.rb +++ b/lib/apartment/active_record/postgresql_adapter.rb @@ -12,12 +12,13 @@ def default_sequence_name(table, _column) # for JDBC driver, if rescued in super_method, trim leading and trailing quotes res.delete!('"') if defined?(JRUBY_VERSION) - schema_prefix = "#{Apartment::Tenant.current}." - default_tenant_prefix = "#{Apartment::Tenant.default_tenant}." + schema_prefix = "#{sequence_schema(res)}." # NOTE: Excluded models should always access the sequence from the default # tenant schema if excluded_model?(table) + default_tenant_prefix = "#{Apartment::Tenant.default_tenant}." + res.sub!(schema_prefix, default_tenant_prefix) if schema_prefix != default_tenant_prefix return res end @@ -29,6 +30,13 @@ def default_sequence_name(table, _column) private + def sequence_schema(sequence_name) + current = Apartment::Tenant.current + return current unless current.is_a?(Array) + + current.find { |schema| sequence_name.starts_with?("#{schema}.") } + end + def excluded_model?(table) Apartment.excluded_models.any? { |m| m.constantize.table_name == table } end diff --git a/spec/examples/schema_adapter_examples.rb b/spec/examples/schema_adapter_examples.rb index aa1a1340..35623cde 100644 --- a/spec/examples/schema_adapter_examples.rb +++ b/spec/examples/schema_adapter_examples.rb @@ -130,6 +130,10 @@ it 'connects and resets' do subject.switch(schema1) do + # Ensure sequence is not cached + Company.reset_sequence_name + User.reset_sequence_name + expect(connection.schema_search_path).to start_with %("#{schema1}") expect(User.sequence_name).to eq "#{User.table_name}_id_seq" expect(Company.sequence_name).to eq "#{public_schema}.#{Company.table_name}_id_seq" @@ -140,10 +144,28 @@ expect(Company.sequence_name).to eq "#{public_schema}.#{Company.table_name}_id_seq" end - it 'allows a list of schemas' do - subject.switch([schema1, schema2]) do - expect(connection.schema_search_path).to include %("#{schema1}") - expect(connection.schema_search_path).to include %("#{schema2}") + describe 'multiple schemas' do + it 'allows a list of schemas' do + subject.switch([schema1, schema2]) do + expect(connection.schema_search_path).to include %("#{schema1}") + expect(connection.schema_search_path).to include %("#{schema2}") + end + end + + it 'connects and resets' do + subject.switch([schema1, schema2]) do + # Ensure sequence is not cached + Company.reset_sequence_name + User.reset_sequence_name + + expect(connection.schema_search_path).to start_with %("#{schema1}") + expect(User.sequence_name).to eq "#{User.table_name}_id_seq" + expect(Company.sequence_name).to eq "#{public_schema}.#{Company.table_name}_id_seq" + end + + expect(connection.schema_search_path).to start_with %("#{public_schema}") + expect(User.sequence_name).to eq "#{User.table_name}_id_seq" + expect(Company.sequence_name).to eq "#{public_schema}.#{Company.table_name}_id_seq" end end end