From 6ea19c0b9f435bf3ab2d6bc357612bcf130bb6d6 Mon Sep 17 00:00:00 2001 From: Gabriel Jablonski Date: Sun, 12 Apr 2026 18:14:50 -0300 Subject: [PATCH] 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) --- .../initializers/schema_dumper_functions.rb | 27 +++++++++++++++++++ db/schema.rb | 7 +++++ 2 files changed, 34 insertions(+) create mode 100644 config/initializers/schema_dumper_functions.rb diff --git a/config/initializers/schema_dumper_functions.rb b/config/initializers/schema_dumper_functions.rb new file mode 100644 index 000000000..944022070 --- /dev/null +++ b/config/initializers/schema_dumper_functions.rb @@ -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) diff --git a/db/schema.rb b/db/schema.rb index 439b19349..7bd0ec04a 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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"