mirror of
https://github.com/discourse/discourse.git
synced 2025-05-22 16:34:31 +08:00
FEATURE: Add CommentMigration for db column comments
For documenting the database (will be used in discourse-data-explorer)
This commit is contained in:
58
lib/comment_migration.rb
Normal file
58
lib/comment_migration.rb
Normal file
@ -0,0 +1,58 @@
|
||||
|
||||
|
||||
class CommentMigration < ActiveRecord::Migration
|
||||
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
|
||||
ActiveRecord::Base.exec_sql "COMMENT ON TABLE #{table_name} IS ?", comment
|
||||
puts " COMMENT ON TABLE #{table_name}"
|
||||
else
|
||||
ActiveRecord::Base.exec_sql "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
|
||||
ActiveRecord::Base.exec_sql "COMMENT ON TABLE #{table_name} IS ?", comment
|
||||
puts " COMMENT ON TABLE #{table_name}"
|
||||
else
|
||||
ActiveRecord::Base.exec_sql "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
|
Reference in New Issue
Block a user