check_postgres_array_columns.rb 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #!/usr/bin/env ruby
  2. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  3. require File.expand_path('../config/environment', __dir__)
  4. class CheckPostgresArrayColumns
  5. def self.run
  6. if Rails.configuration.database_configuration[Rails.env]['adapter'] != 'postgresql'
  7. puts 'Error: This script works only with postgresql adapter!'
  8. exit 1
  9. end
  10. puts 'Checking database array columns:'
  11. check_columns_type
  12. check_columns_array
  13. puts 'done.'
  14. end
  15. def self.check_columns_type
  16. puts ' (type == :string):'
  17. smime_certificates.concat(pgp_keys).concat(public_links).concat(object_manager_attributes).each do |item|
  18. print " #{item[:table]}.#{item[:column]} ... "
  19. type = column(item[:table], item[:column]).type
  20. if type == :string
  21. puts 'OK'
  22. else
  23. puts 'Not OK!'
  24. puts " Expected type :string, but got: #{type}"
  25. exit 1
  26. end
  27. end
  28. end
  29. def self.check_columns_array
  30. puts ' (array == true):'
  31. smime_certificates.concat(pgp_keys).concat(public_links).concat(object_manager_attributes).each do |item|
  32. print " #{item[:table]}.#{item[:column]} ... "
  33. array = column(item[:table], item[:column]).array
  34. if array
  35. puts 'OK'
  36. else
  37. puts 'Not OK!'
  38. puts " Expected array true, but got: #{array}"
  39. exit 1
  40. end
  41. end
  42. end
  43. def self.object_manager_attributes
  44. ObjectManager::Attribute.where(data_type: %w[multiselect multi_tree_select]).map do |field|
  45. { table: field.object_lookup.name.constantize.table_name, column: field.name }
  46. end
  47. end
  48. def self.smime_certificates
  49. [{ table: SMIMECertificate.table_name, column: 'email_addresses' }]
  50. end
  51. def self.pgp_keys
  52. [{ table: PGPKey.table_name, column: 'email_addresses' }]
  53. end
  54. def self.public_links
  55. [{ table: PublicLink.table_name, column: 'screen' }]
  56. end
  57. def self.column(table, column)
  58. ActiveRecord::Base.connection.columns(table.to_sym).find { |c| c.name == column }
  59. end
  60. end
  61. CheckPostgresArrayColumns.run