20190724000001_rename_reserved_words.rb 984 B

1234567891011121314151617181920212223242526272829
  1. class RenameReservedWords < ActiveRecord::Migration[5.1]
  2. def up
  3. return if !Setting.find_by(name: 'system_init_done')
  4. models = ObjectManager.list_objects.map(&:underscore).map { |object| object.tr('_', '/') }.map(&:classify).map(&:constantize)
  5. reserved_words = %w[url icon initials avatar permission validate subscribe unsubscribe translate search]
  6. models.each do |model|
  7. reserved_words.each do |reserved_word|
  8. next if ActiveRecord::Base.connection.columns(model.table_name).map(&:name).exclude?(reserved_word)
  9. sanitized_name = "_#{reserved_word}"
  10. ActiveRecord::Migration.rename_column(model.table_name.to_sym, reserved_word.to_sym, sanitized_name.to_sym)
  11. model.reset_column_information
  12. attribute = ObjectManager::Attribute.get(
  13. object: model.to_app_model,
  14. name: reserved_word,
  15. )
  16. next if !attribute
  17. attribute.update!(name: sanitized_name)
  18. end
  19. end
  20. end
  21. end