Merge pull request #3633 from riking/patch-5

FEATURE: Add CommentMigration for db column comments
This commit is contained in:
Robin Ward 2015-07-28 16:18:01 -04:00
commit e09e5016ce
2 changed files with 84 additions and 0 deletions

View File

@ -0,0 +1,26 @@
require 'comment_migration'
class AddExampleColumnComments < CommentMigration
def comments_up
{
posts: {
_table: 'If you want to query public posts only, use the badge_posts view.',
post_number: 'The position of this post in the topic. The pair (topic_id, post_number) forms a natural key on the posts table.',
raw: 'The raw Markdown that the user entered into the composer.',
cooked: 'The processed HTML that is presented in a topic.',
reply_to_post_number: "If this post is a reply to another, this column is the post_number of the post it's replying to. [FKEY posts.topic_id, posts.post_number]",
reply_quoted: 'This column is true if the post contains a quote-reply, which causes the in-reply-to indicator to be absent.',
},
topics: {
_table: "To query public topics only: SELECT ... FROM topics t LEFT INNER JOIN categories c ON (t.category_id = c.id AND c.read_restricted = false)"
},
}
end
def comments_down
{
}
end
end

58
lib/comment_migration.rb Normal file
View 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