任何Rails插件添加有关ActiveRecord迁移文件中每列的注释?

TK.*_*TK. 6 activerecord ruby-on-rails

我想在我的迁移文件中插入COMMENT,它是SQL命令的一部分.

据我所知,我可以为每个表和列添加COMMENT.

我记不起一个允许我写的插件名称如下:

    t.string  :name, :comment => "A user's fullname"
    t.string  :label, :comment => "name of color"
    t.text  :value, :comment => "self intro"
    t.integer  :position, :comment => "1 is left, 2 is right"
Run Code Online (Sandbox Code Playgroud)

神奇的语句被翻译成SQL,就像

create table test (
  name varchar(255) not null COMMENT 'blahblah',
  label varchar(255) null COMMENT 'hahaha'
  text varchar(255) not null,
  position int(11)
);
Run Code Online (Sandbox Code Playgroud)

有人知道插件的名字吗?


Pin*_*nyM 6

无耻的插件 - 现在有一个' migration_comments '宝石,用于评论MySQL,SQLite和PostgreSQL.它目前支持Rails 2.3及更高版本.它还与annotate gem(v2.5.0或更高版本)一起在Model/Fixture/Spec文件中生成这些注释.


rjk*_*rjk 5

我不知道任何能完成你要求的插件.你可以通过观察来破解你想要的东西ActiveRecord::ConnectionAdapters::ColumnDefinition.(见active_record/connection_adapters/abstract/schema_definitions.rb.)

正如您所看到的那样Struct定义了各种列选项(比如:limit:default.)您可以使用a扩展该结构,:comment然后进行修改#to_sql以生成所需的SQL.您还需要修改TableDefinition#column以设置:comment属性.

以下内容已经过测试并且有效(适用于MySQL):

module ActiveRecord
  module ConnectionAdapters
    class ColumnDefinition
      attr_accessor :comment

      def to_sql_with_comment
        column_sql = to_sql_without_comment
        return column_sql if comment.nil?
       "#{column_sql} COMMENT '#{base.quote_string(comment)}'"
      end

      alias_method_chain :to_sql, :comment
    end

    class TableDefinition
      # Completely replaces (and duplicates the existing code, but there's
      # no place to really hook into the middle of this.)
      def column(name, type, options = {})
        column = self[name] || ColumnDefinition.new(@base, name, type)
        if options[:limit]
          column.limit = options[:limit]
        elsif native[type.to_sym].is_a?(Hash)
          column.limit = native[type.to_sym][:limit]
        end
        column.precision = options[:precision]
        column.scale = options[:scale]
        column.default = options[:default]
        column.null = options[:null]
        column.comment = options[:comment]
        @columns << column unless @columns.include? column
        self
      end
    end
  end
end
Run Code Online (Sandbox Code Playgroud)