check_postgres_array_columns.rb 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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 data type of array columns:'
  11. check_columns
  12. puts 'done.'
  13. end
  14. def self.check_columns
  15. public_links.concat(object_manager_attributes).each do |item|
  16. print " #{item[:table]}.#{item[:column]} ... "
  17. type = data_type(item[:table], item[:column])
  18. if type == 'ARRAY'
  19. puts 'OK'
  20. else
  21. puts 'Not OK!'
  22. puts " Expected type ARRAY, but got: #{type}"
  23. exit 1
  24. end
  25. end
  26. end
  27. def self.object_manager_attributes
  28. ObjectManager::Attribute.where(data_type: %w[multiselect multi_tree_select]).map do |field|
  29. { table: field.object_lookup.name.constantize.table_name, column: field.name }
  30. end
  31. end
  32. def self.public_links
  33. [{ table: PublicLink.table_name, column: 'screen' }]
  34. end
  35. def self.data_type(table, column)
  36. ActiveRecord::Base.connection.execute("select data_type from information_schema.columns where table_name = '#{table}' and column_name = '#{column}' limit 1")[0]['data_type']
  37. end
  38. end
  39. CheckPostgresArrayColumns.run