代码之家  ›  专栏  ›  技术社区  ›  TK.

有没有Rails插件可以为ActiveRecord迁移文件中的每一列添加注释?

  •  6
  • TK.  · 技术社区  · 15 年前

    我想在迁移文件中插入注释,这是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)
    );
    

    有人知道插件的名字吗?


    4 回复  |  直到 15 年前
        1
  •  5
  •   rjk    15 年前

    可以 能够通过查看 ActiveRecord::ConnectionAdapters::ColumnDefinition . (见 active_record/connection_adapters/abstract/schema_definitions.rb

    你可以看到 Struct 定义各种列选项(如 :limit :default )你可以用一个 :comment #to_sql 生成所需的SQL。您还需要修改 TableDefinition#column :注释 属性。

    以下内容已经过测试并可以正常工作(针对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
    
        2
  •  6
  •   PinnyM    13 年前

    migration_comments '用于注释MySQL、SQLite和PostgreSQL的gem。此时它支持Rails 2.3及更高版本。它还与 annotate gem(v2.5.0或更高版本)在模型/夹具/规范文件中生成这些注释。

        3
  •  1
  •   peter    10 年前

    在让gem为Oracle工作失败之后,我用activerecord-v4.1.1尝试了以下操作,并且正确添加了注释。 所以不再需要额外的宝石。

      ActiveRecord::Schema.define do
        create_table TABLENAME do |table|
          table.column :status, :integer, :comment => "Keeps status for this signal"
          table.column :rowversion, :integer, :comment => "Increments with each change of this record"
          etc..
        end
      end
    
        4
  •  1
  •   Prajakta    9 年前

    有了Rails5,您现在可以直接向迁移添加注释,而无需使用任何插件。

    您可以在中查看这些评论架构.rb加上从数据库工具。

    例子:

    class CreateProducts < ActiveRecord::Migration[5.0]
      def change
        create_table :products, comment: 'Products table' do |t|
          t.string :name, comment: 'Name of the product'
          t.string :barcode, comment: 'Barcode of the product'
          t.string :description, comment: 'Product details'
          t.float :msrp, comment: 'Maximum Retail Price'
          t.float :our_price, comment: 'Selling price'
    
          t.timestamps
        end
    
        add_index :products, :name, name: 'index_products_on_name', unique: true, comment: 'Index used to lookup product by name.'
      end
    end