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>
This commit is contained in:
Gabriel Jablonski 2026-04-12 18:14:50 -03:00 committed by GitHub
parent 23f9cc3740
commit 6ea19c0b9f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 34 additions and 0 deletions

View File

@ -0,0 +1,27 @@
# 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)

View File

@ -19,6 +19,13 @@ ActiveRecord::Schema[7.1].define(version: 2026_04_10_170003) do
enable_extension "unaccent"
enable_extension "vector"
# Custom SQL functions (required before index creation)
execute <<~SQL
CREATE OR REPLACE FUNCTION f_unaccent(text)
RETURNS text LANGUAGE sql IMMUTABLE PARALLEL SAFE STRICT
AS $func$ SELECT public.unaccent('public.unaccent', $1) $func$
SQL
create_table "access_tokens", force: :cascade do |t|
t.string "owner_type"
t.bigint "owner_id"