2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
2015-07-27 19:41:31 -04:00
|
|
|
|
2017-08-31 00:06:56 -04:00
|
|
|
class CommentMigration < ActiveRecord::Migration[4.2]
|
2015-07-27 19:41:31 -04:00
|
|
|
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
|
2018-06-19 02:13:14 -04:00
|
|
|
DB.exec "COMMENT ON TABLE #{table_name} IS ?", comment
|
2015-07-27 19:41:31 -04:00
|
|
|
puts " COMMENT ON TABLE #{table_name}"
|
|
|
|
else
|
2018-06-19 02:13:14 -04:00
|
|
|
DB.exec "COMMENT ON COLUMN #{table_name}.#{column_name} IS ?", comment
|
2015-07-27 19:41:31 -04:00
|
|
|
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
|
2018-06-19 02:13:14 -04:00
|
|
|
DB.exec "COMMENT ON TABLE #{table_name} IS ?", comment
|
2015-07-27 19:41:31 -04:00
|
|
|
puts " COMMENT ON TABLE #{table_name}"
|
|
|
|
else
|
2018-06-19 02:13:14 -04:00
|
|
|
DB.exec "COMMENT ON COLUMN #{table_name}.#{column_name} IS ?", comment
|
2015-07-27 19:41:31 -04:00
|
|
|
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
|