forked from zammad/zammad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_postgres_array_columns.rb
executable file
·51 lines (39 loc) · 1.39 KB
/
check_postgres_array_columns.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env ruby
# Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
require File.expand_path('../config/environment', __dir__)
class CheckPostgresArrayColumns
def self.run
if Rails.configuration.database_configuration[Rails.env]['adapter'] != 'postgresql'
puts 'Error: This script works only with postgresql adapter!'
exit 1
end
puts 'Checking data type of array columns:'
check_columns
puts 'done.'
end
def self.check_columns
public_links.concat(object_manager_attributes).each do |item|
print " #{item[:table]}.#{item[:column]} ... "
type = data_type(item[:table], item[:column])
if type == 'ARRAY'
puts 'OK'
else
puts 'Not OK!'
puts " Expected type ARRAY, but got: #{type}"
exit 1
end
end
end
def self.object_manager_attributes
ObjectManager::Attribute.where(data_type: %w[multiselect multi_tree_select]).map do |field|
{ table: field.object_lookup.name.constantize.table_name, column: field.name }
end
end
def self.public_links
[{ table: PublicLink.table_name, column: 'screen' }]
end
def self.data_type(table, column)
ActiveRecord::Base.connection.execute("select data_type from information_schema.columns where table_name = '#{table}' and column_name = '#{column}' limit 1")[0]['data_type']
end
end
CheckPostgresArrayColumns.run