Skip to content

Commit

Permalink
Properly reset sequence if switching with multiple schemas (#203)
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanswood authored Aug 14, 2024
1 parent 973828f commit 87247aa
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
12 changes: 10 additions & 2 deletions lib/apartment/active_record/postgresql_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
30 changes: 26 additions & 4 deletions spec/examples/schema_adapter_examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down

0 comments on commit 87247aa

Please sign in to comment.