Skip to content

sort table columns by type, as hinted by name conventions #51

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion lib/fix_db_schema_conflicts/schema_dumper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions spec/integration/integration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down