20230728090212_pgp_key_email_addresses.rb 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class PGPKeyEmailAddresses < ActiveRecord::Migration[6.1]
  3. def change
  4. # return if it's a new setup
  5. return if !Setting.exists?(name: 'system_init_done')
  6. migrate_db_schema_pre
  7. migrate_db_data
  8. migrate_db_schema_post
  9. end
  10. private
  11. def migrate_db_schema_pre
  12. add_column :pgp_keys, :name, :string, limit: 3000, null: true
  13. if Rails.application.config.db_column_array
  14. add_column :pgp_keys, :email_addresses, :string, null: true, array: true
  15. else
  16. add_column :pgp_keys, :email_addresses, :json, null: true
  17. end
  18. PGPKey.reset_column_information
  19. end
  20. def migrate_db_data
  21. PGPKey.all.each do |key|
  22. next if key.name.present?
  23. key.name = key.uids.split(',').join(', ')
  24. key.prepare_email_addresses
  25. key.save!
  26. end
  27. end
  28. def migrate_db_schema_post
  29. change_column_null :pgp_keys, :name, false
  30. remove_index :pgp_keys, [:uids]
  31. remove_column :pgp_keys, :uids
  32. PGPKey.reset_column_information
  33. end
  34. end