Skip to content

Schema dump writes create_collation after tables #2

@jeremysmithco

Description

@jeremysmithco

Hey, thanks for this gem! I just had this issue on a project and started looking for a solution yesterday, so this was good timing! 😊

I noticed when running rails db:schema:dump that the create_collation calls will get added near the end of the schema.rb, after the table definitions:

ActiveRecord::Schema[8.2].define(version: 2025_10_13_182247) do
  enable_extension "fuzzystrmatch"
  enable_extension "pg_catalog.plpgsql"
  enable_extension "pg_stat_statements"
  enable_extension "pg_trgm"
  enable_extension "pgcrypto"
  enable_extension "tablefunc"
  
  # …

  create_table "xxx", force: :cascade do |t|
    # …
    
    t.string "xxx_number", collation: "numeric"
    
    # …
  end

  # …

  create_collation("numeric", "en-u-kn")
end

And so when I try to load the schema again, I get an error at the first point where I try to use a named collation, because it doesn't exist yet:

bin/rails aborted!
ActiveRecord::StatementInvalid: PG::UndefinedObject: ERROR:  collation "numeric" for encoding "UTF8" does not exist (ActiveRecord::StatementInvalid)
LINE 1: ...cter varying, "xxx_number" character varying COLLATE "n...
                                                             ^
/Users/jeremy/Code/xxx/db/schema.rb:618:in 'block in <main>'
/Users/jeremy/Code/xxx/db/schema.rb:13:in '<main>'

Caused by:
PG::UndefinedObject: ERROR:  collation "numeric" for encoding "UTF8" does not exist (PG::UndefinedObject)
LINE 1: ...cter varying, "xxx_number" character varying COLLATE "n...
                                                             ^
/Users/jeremy/Code/xxx/db/schema.rb:618:in 'block in <main>'
/Users/jeremy/Code/xxx/db/schema.rb:13:in '<main>'
Tasks: TOP => db:setup => db:schema:load

There's another gem out there (older, apparently unmaintained) called activerecord-pg_collation and I noticed that it creates collations right after extensions, which makes sense to me:

module SchemaDumper
  private

  def extensions(stream)
    super
    collations(stream)
  end

  # …
end

I also noticed it uses prepend instead of include:

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

What would you think of that change? Am I missing anything important? Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions