Module: UnsortDbSchemaColumns::SchemaDumperPatch

Defined in:
lib/unsort_db_schema_columns/schema_dumper_patch.rb

Overview

Restores the pre-#53281 behavior: dump columns in the order the database returns them (ordinal position), instead of alphabetically by name.

We override the entire #table method because Rails sorts inline:

columns.sort_by(&:name).each do |column|

The body below is copied from activerecord/lib/active_record/schema_dumper.rb at the Rails 8.x main branch, with that single ‘.sort_by(&:name)` removed. Verified against Rails 8.0.x. If you upgrade Rails and this gem hasn’t, watch for an UnexpectedRailsVersion warning at boot.

Instance Method Summary collapse

Instance Method Details

#table(table, stream) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/unsort_db_schema_columns/schema_dumper_patch.rb', line 16

def table(table, stream)
  columns = @connection.columns(table)
  begin
    self.table_name = table

    tbl = StringIO.new

    # first dump primary key column
    if @connection.respond_to?(:primary_keys)
      pk = @connection.primary_keys(table)
      pk = pk.first unless pk.size > 1
    else
      pk = @connection.primary_key(table)
    end

    tbl.print "  create_table #{remove_prefix_and_suffix(table).inspect}"

    case pk
    when String
      tbl.print ", primary_key: #{pk.inspect}" unless pk == "id"
      pkcol = columns.detect { |c| c.name == pk }
      if pkcol
        pkcolspec = column_spec_for_primary_key(pkcol)
        if pkcolspec.present?
          if pkcolspec != pkcolspec.slice(:id, :default)
            pkcolspec = { id: { type: pkcolspec.delete(:id), **pkcolspec }.compact }
          end
          tbl.print ", #{format_colspec(pkcolspec)}"
        end
      end
    when Array
      tbl.print ", primary_key: #{pk.inspect}"
    else
      tbl.print ", id: false"
    end

    table_options = @connection.table_options(table)
    if table_options.present?
      tbl.print ", #{format_options(table_options)}"
    end

    tbl.puts ", force: :cascade do |t|"

    # then dump all non-primary key columns -- in NATURAL DB order, not sorted
    columns.each do |column|
      raise StandardError, "Unknown type '#{column.sql_type}' for column '#{column.name}'" unless @connection.valid_type?(column.type)
      next if column.name == pk

      type, colspec = column_spec(column)
      if type.is_a?(Symbol)
        tbl.print "    t.#{type} #{column.name.inspect}"
      else
        tbl.print "    t.column #{column.name.inspect}, #{type.inspect}"
      end
      tbl.print ", #{format_colspec(colspec)}" if colspec.present?
      tbl.puts
    end

    indexes_in_create(table, tbl)
    remaining = check_constraints_in_create(table, tbl) if @connection.supports_check_constraints?
    exclusion_constraints_in_create(table, tbl) if @connection.supports_exclusion_constraints?
    unique_constraints_in_create(table, tbl) if @connection.supports_unique_constraints?

    tbl.puts "  end"

    if remaining
      tbl.puts
      tbl.print remaining.string
    end

    stream.print tbl.string
  rescue => e
    stream.puts "# Could not dump table #{table.inspect} because of following #{e.class}"
    stream.puts "#   #{e.message}"
    stream.puts
  ensure
    self.table_name = nil
  end
end