migration_helper.rb 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class MigrationHelper
  3. =begin
  4. MigrationHelper.rename_custom_object_attribute('Organization', 'vip')
  5. =end
  6. def self.rename_custom_object_attribute(object, name)
  7. return if !custom_object_attribute(object, name)
  8. sanitized_name = "_#{name}"
  9. custom_object_attribute(object, name).update!(name: sanitized_name)
  10. rename_table_column(object.constantize, name, sanitized_name)
  11. end
  12. =begin
  13. object_attribute = MigrationHelper.custom_object_attribute('Organization', 'vip')
  14. returns ObjectManager::Attribute
  15. =end
  16. def self.custom_object_attribute(object, name)
  17. ObjectManager::Attribute.get(object: object, name: name)
  18. end
  19. =begin
  20. MigrationHelper.rename_table_column(Organization, 'vip', '_vip')
  21. =end
  22. def self.rename_table_column(model, name, sanitized_name)
  23. return if ActiveRecord::Base.connection.columns(model.table_name).map(&:name).exclude?(name)
  24. ActiveRecord::Migration.rename_column(model.table_name.to_sym, name.to_sym, sanitized_name.to_sym)
  25. model.connection.schema_cache.clear!
  26. model.reset_column_information
  27. end
  28. end