Module: Apartment::PostgreSqlAdapterPatch

Included in:
ActiveRecord::ConnectionAdapters::PostgreSQLAdapter
Defined in:
lib/apartment/active_record/postgresql_adapter.rb

Overview

NOTE: This patch is meant to remove any schema_prefix appart from the ones for excluded models. The schema_prefix would be resolved by apartment’s setting of search path

Instance Method Summary collapse

Instance Method Details

#default_sequence_name(table, _column) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/apartment/active_record/postgresql_adapter.rb', line 9

def default_sequence_name(table, _column)
  res = super

  # 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}."

  # NOTE: Excluded models should always access the sequence from the default
  # tenant schema
  if excluded_model?(table)
    res.sub!(schema_prefix, default_tenant_prefix) if schema_prefix != default_tenant_prefix
    return res
  end

  res.delete_prefix!(schema_prefix) if res&.starts_with?(schema_prefix)

  res
end