forked from pingcap/discourse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomment_migration.rb
58 lines (51 loc) · 1.39 KB
/
comment_migration.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
52
53
54
55
56
57
58
# frozen_string_literal: true
class CommentMigration < ActiveRecord::Migration[4.2]
def comments_up
raise "Not implemented"
end
def up
comments_up.each do |table|
table[1].each do |column|
table_name = table[0]
column_name = column[0]
comment = column[1]
if column_name == :_table
DB.exec "COMMENT ON TABLE #{table_name} IS ?", comment
puts " COMMENT ON TABLE #{table_name}"
else
DB.exec "COMMENT ON COLUMN #{table_name}.#{column_name} IS ?", comment
puts " COMMENT ON COLUMN #{table_name}.#{column_name}"
end
end
end
end
def comments_down
{}
end
def down
replace_nils(comments_up).deep_merge(comments_down).each do |table|
table[1].each do |column|
table_name = table[0]
column_name = column[0]
comment = column[1]
if column_name == :_table
DB.exec "COMMENT ON TABLE #{table_name} IS ?", comment
puts " COMMENT ON TABLE #{table_name}"
else
DB.exec "COMMENT ON COLUMN #{table_name}.#{column_name} IS ?", comment
puts " COMMENT ON COLUMN #{table_name}.#{column_name}"
end
end
end
end
private
def replace_nils(hash)
hash.each do |key, value|
if Hash === value
hash[key] = replace_nils value
else
hash[key] = nil
end
end
end
end