From 13d56eb989ac1270f6537b76df0216830846a071 Mon Sep 17 00:00:00 2001 From: Kane York Date: Mon, 27 Jul 2015 16:41:31 -0700 Subject: [PATCH] FEATURE: Add CommentMigration for db column comments For documenting the database (will be used in discourse-data-explorer) --- ...50727230537_add_example_column_comments.rb | 26 +++++++++ lib/comment_migration.rb | 58 +++++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 db/migrate/20150727230537_add_example_column_comments.rb create mode 100644 lib/comment_migration.rb diff --git a/db/migrate/20150727230537_add_example_column_comments.rb b/db/migrate/20150727230537_add_example_column_comments.rb new file mode 100644 index 00000000000..46e359d26c3 --- /dev/null +++ b/db/migrate/20150727230537_add_example_column_comments.rb @@ -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 diff --git a/lib/comment_migration.rb b/lib/comment_migration.rb new file mode 100644 index 00000000000..60065d7507d --- /dev/null +++ b/lib/comment_migration.rb @@ -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