2010-09-02 22 views
6

我想插入註釋,這是SQL的一部分的命令,在我的移民文件。任何Rails插件可以添加有關ActiveRecord遷移文件中每列的註釋?

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) 
); 

有人知道插頭的名字嗎?


回答

5

我不知道會完成你問任何插件。您可能能在你想通過看ActiveRecord::ConnectionAdapters::ColumnDefinition什麼破解。 (見active_record/connection_adapters/abstract/schema_definitions.rb。)

正如你可以看到Struct定義各種列選項(如:limit:default。)你可以擴展該結構具有: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 
+0

謝謝!你的答案很棒。我還沒有測試過你的代碼。但我會採取你的方法。 – 2010-09-03 15:08:56

+0

看來代碼不起作用。 – 2010-09-06 03:04:41

+0

哪部分不工作?您是否添加了任何日誌記錄以查看方法是否被調用? – rjk 2010-09-06 21:50:43

6

無恥的插件 - 現在有一個 'migration_comments' 的寶石,爲MySQL的評論,SQLite的和PostgreSQL的作品。目前它支持Rails 2.3及更高版本。它還與annotate gem(v2.5.0或更高版本)一起使用,以在您的Model/Fixture/Spec文件中生成這些註釋。

+0

太棒了!我更喜歡用寶石來編寫我自己的代碼。 – 2013-01-07 09:41:59

1

在獲得migration_comments gem for Oracle的工作失敗後,我剛剛嘗試了以下activerecord -v 4.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 
0

使用Rails 5,您現在可以在不使用任何插件的情況下直接向您的遷移添加註釋。

您可以添加對錶,列和索引的意見。

您可以schema.rb加上從DBA工具來查看這些評論。

例子:

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 

注意:這是隻支持PostgreSQL和MySQL。

相關問題