scinote-web/db/migrate/20160513165000_rename_duplicate_columns.rb

35 lines
890 B
Ruby
Raw Normal View History

2017-06-30 16:23:28 +08:00
class RenameDuplicateColumns < ActiveRecord::Migration[4.2]
2017-01-26 23:30:31 +08:00
require 'set'
2016-07-21 19:11:15 +08:00
def up
2017-01-26 23:30:31 +08:00
# Update duplicate or prohibited column names with extensions
2016-07-21 19:11:15 +08:00
2017-01-26 23:30:31 +08:00
Team.find_each do |team|
2016-07-21 19:11:15 +08:00
names = Set.new ["Assigned", "Sample name", "Sample type", "Sample group", "Added on", "Added by"]
2017-01-26 23:30:31 +08:00
CustomField.where(team: team).find_each do |column|
2016-07-21 19:11:15 +08:00
if names.include?(column.name)
name = column.name
i = 0
while names.include?(name)
name = column.name
i = i+1
suffix = "(" + i.to_s + ")"
2017-01-26 23:30:31 +08:00
if (suffix.length + name.length > 50)
2016-07-21 19:11:15 +08:00
name = name[0..(49-suffix.length)]
end
name = name + suffix
end
column.update(name: name);
end
names.add(column.name)
end
end
end
def down
# We can't really rollback this change
end
2017-01-26 23:30:31 +08:00
end