iachat/config/initializers/schema_dumper_functions.rb
Gabriel Jablonski 6ea19c0b9f
fix(db): include f_unaccent function in schema.rb for test DB setup (#263)
The f_unaccent SQL function created by the internal chat migration is
not natively captured by schema.rb, causing db:test:prepare to fail
when creating indices that depend on it. Add the function definition
to schema.rb and extend SchemaDumper to preserve it across future dumps.

Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-12 18:14:50 -03:00

28 lines
1020 B
Ruby

# Extends the Rails schema dumper to include custom SQL functions that are
# required by indices but not natively supported by schema.rb format.
#
# Without this, db:schema:load (used by db:test:prepare) fails because
# schema.rb references indices that depend on f_unaccent() but the function
# definition is lost during the dump (schema.rb only captures tables,
# indices, and extensions, not custom functions).
module SchemaDumperFunctions
private
def extensions(stream)
super
dump_custom_functions(stream)
end
def dump_custom_functions(stream)
stream.puts
stream.puts ' # Custom SQL functions (required before index creation)'
stream.puts ' execute <<~SQL'
stream.puts ' CREATE OR REPLACE FUNCTION f_unaccent(text)'
stream.puts ' RETURNS text LANGUAGE sql IMMUTABLE PARALLEL SAFE STRICT'
stream.puts " AS $func$ SELECT public.unaccent('public.unaccent', $1) $func$"
stream.puts ' SQL'
end
end
ActiveRecord::SchemaDumper.prepend(SchemaDumperFunctions)