Any Rails plugin to add comments to each column in ActiveRecord migration files?

I would like to insert COMMENT, which is part of the SQL command, into my migration files.

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

I can’t remember the name of the plugin, which allows me to write as follows:

t.string :name, :comment => "A user fullname" t.string :label, :comment => "name of color" t.text :value, :comment => "self intro" t.integer :position, :comment => "1 is left, 2 is right" 

And that expression is magically translated into SQL, which is similar to

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

Does anyone know the name of the plugin?


+6
ruby-on-rails activerecord
source share
4 answers

I do not know a single plugin that will do what you ask for. You can hack what you want by looking at ActiveRecord::ConnectionAdapters::ColumnDefinition . (See active_record/connection_adapters/abstract/schema_definitions.rb .)

As you can see, Struct defines various column parameters (for example :limit and :default .). You could expand this structure with :comment , and then modify #to_sql to generate the required SQL. You also need to modify TableDefinition#column to set the :comment attribute.

The following has been tested and works (for 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 
+5
source share

Shameless plugin - now there is migration_comments ' that works for MySQL, SQLite and PostgreSQL comments. It currently supports Rails 2.3 and higher. It also works along with annotate gem (v2.5.0 or later) to create these comments in Model / Fixture / Spec files.

+6
source share

After the crash obtained using the migration_comments file for Oracle, I just tried the following with activerecord -v 4.1.1 and the comment was added correctly. Therefore, additional gems are no longer needed.

  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 
+1
source share

With Rails 5, you can now directly add comments to your migration without using any plugin.

You can add comments to the table, column and index.

You can view these comments in schema.rb plus from the DBA tool.

Example:

 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 

Note. This is only supported for PostgreSQL and MySQL.

0
source share

All Articles