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
29
30
31
32
33
34
35
36
# 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 = "#{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}."

    # Unless the res is already prefixed with the default_tenant_prefix
    # we should delete the schema_prefix and add the default_tenant_prefix
    unless res&.starts_with?(default_tenant_prefix)
      res&.delete_prefix!(schema_prefix)
      res = default_tenant_prefix + res
    end

    return res
  end

  # Delete the schema_prefix from the res if it is present
  res&.delete_prefix!(schema_prefix)

  res
end