加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 任何Rails插件,用于添加有关ActiveRecord迁移

发布时间:2020-12-16 21:43:21 所属栏目:百科 来源:网络整理
导读:我想在我的迁移文件中插入COMMENT,它是SQL命令的一部分. As far as I know,I can add COMMENT to each table and column. 我记不起一个允许我写的插件名称如下: t.string :name,:comment = "A user's fullname" t.string :label,:comment = "name of color"
我想在我的迁移文件中插入COMMENT,它是SQL命令的一部分.

As far as I know,I can add COMMENT to each table and column.

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

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"

神奇的语句被翻译成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)
);

有人知道插件的名字吗?

>我不是在寻找Dave Thomas的Annotate Models plugins.我的意思是评论是在MySQL查询中.

解决方法

我不知道任何能完成你要求的插件.您可以通过查看ActiveRecord :: ConnectionAdapters :: ColumnDefinition来破解您想要的内容. (请参阅active_record / connection_adapters / abstract / schema_definitions.rb.)

正如您所看到的,Struct定义了各种列选项(例如:limit和:default.)您可以使用:comment扩展该结构,然后修改#to_sql以生成所需的SQL.您还需要修改TableDefinition#列以设置: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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读