20190724000001_rename_reserved_words.rb 1.0 KB

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