I'd like to insert COMMENT, which is part of SQL the command, in my migration files.
As far as I know, I can add COMMENT to each table and column.
I can't remember a plugin name that lets me to write as follows:
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"
And that statement magically is translated into SQL, which is like
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 anybody kno开发者_开发知识库w the plug in name?
- I'm not looking for Annotate Models plugins by Dave Thomas. What I mean by comments is inside MySQL queries.
Shameless plug - there is now a 'migration_comments' gem that works for commenting MySQL, SQLite, and PostgreSQL. It supports Rails 2.3 and higher at this time. It also works together with the annotate gem (v2.5.0 or higher) to generate these comments in your Model/Fixture/Spec files.
I don't know of any plugin that will accomplish what you're asking for. You might be able to hack in what you want by looking at ActiveRecord::ConnectionAdapters::ColumnDefinition
. (See active_record/connection_adapters/abstract/schema_definitions.rb
.)
As you can see the Struct
defines the various column options (like :limit
and :default
.) You could extended that struct with a :comment
and then modify #to_sql
to generate the required SQL. You would 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
After a failure getting migration_comments gem to work for Oracle, I just tried the following with activerecord -v 4.1.1 and the comment was added correctly. So no more need for extra gems.
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
With Rails 5, you can now directly add comments to your migration without using any plugin.
You can add comments for table, column and index.
You can view these comments in schema.rb plus from 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.
精彩评论