diff --git a/lib/fix_db_schema_conflicts/schema_dumper.rb b/lib/fix_db_schema_conflicts/schema_dumper.rb index 1bcd458..5cc269f 100644 --- a/lib/fix_db_schema_conflicts/schema_dumper.rb +++ b/lib/fix_db_schema_conflicts/schema_dumper.rb @@ -12,12 +12,31 @@ def columns(table) end def indexes(table) - __getobj__.indexes(table).sort_by(&:name) + indexes_string_sort(table) + # __getobj__.indexes(table).sort_by(&:name) end def foreign_keys(table) __getobj__.indexes(table).sort_by(&:name) end + + def indexes_string_sort(table) + cols = __getobj__.indexes(table) + cols.reduce({ids:[],datetimes:[],other:[]}) do |h,col| + case col.name + when /_id$"/ + h[:ids] << col + when /_at$"/ + h[:datetimes] << col + else + h[:other] << col + end + h + end.tap {|h| h[:ids ] = h[:ids ].sort{|a,b| a.name <=> b.name }} + .tap {|h| h[:datetimes] = h[:datetimes].sort{|a,b| a.name <=> b.name }} + .tap {|h| h[:other ] = h[:other ].sort{|a,b| a.name <=> b.name }} + .values.flatten + end end def extensions(*args) diff --git a/spec/integration/integration_spec.rb b/spec/integration/integration_spec.rb index 20f1885..f9d67ec 100644 --- a/spec/integration/integration_spec.rb +++ b/spec/integration/integration_spec.rb @@ -43,6 +43,8 @@ def reference_db_schema t.string "state" t.datetime "updated_at", null: false t.string "zip" + t.bigint "entity_registry_id" + t.integer "corporation_type_id" end add_index "companies", ["city"], name: "index_companies_on_city"