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"