Module: Apartment::SchemaDumperPatch

Defined in:
lib/apartment/schema_dumper_patch.rb

Defined Under Namespace

Modules: DumperOverride

Class Method Summary collapse

Class Method Details

.apply!Object



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/apartment/schema_dumper_patch.rb', line 17

def self.apply!
  return unless should_patch?

  # Rails 8.1+ adds schema-qualified names via `relation_name` in the
  # PG-specific SchemaDumper (PR #50020). The prefix is applied to tables,
  # foreign keys, enums, and indexes — all through `relation_name`. We
  # intercept that single method rather than patching each call site.
  return unless defined?(ActiveRecord::ConnectionAdapters::PostgreSQL::SchemaDumper)

  ActiveRecord::ConnectionAdapters::PostgreSQL::SchemaDumper.prepend(DumperOverride)
end

.should_patch?Boolean

Returns:

  • (Boolean)


29
30
31
32
33
# File 'lib/apartment/schema_dumper_patch.rb', line 29

def self.should_patch?
  return false unless defined?(ActiveRecord::SchemaDumper)

  ActiveRecord.gem_version >= Gem::Version.new('8.1.0')
end

.strip_public_prefix(table_name, include_schemas: []) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/apartment/schema_dumper_patch.rb', line 5

def self.strip_public_prefix(table_name, include_schemas: [])
  schema, name = table_name.split('.', 2)

  return table_name unless name

  return table_name if schema != 'public' && include_schemas.include?(schema)

  return name if schema == 'public'

  table_name
end