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 know the plug in name?
By default, the generated migration will include t. timestamps (which creates the updated_at and created_at columns that are automatically populated by Active Record).
add_index(table_name, column_name, **options) LinkAdds a new index to the table. column_name can be a single Symbol , or an Array of Symbols. The index will be named after the table and the column name(s), unless you pass :name as an option.
2 Creating a Migrationrb , that is to say a UTC timestamp identifying the migration followed by an underscore followed by the name of the migration. The name of the migration class (CamelCased version) should match the latter part of the file name. For example 20080906120000_create_products.
When you already have users and uploads tables and wish to add a new relationship between them. Then, run the migration using rake db:migrate . This migration will take care of adding a new column named user_id to uploads table (referencing id column in users table), PLUS it will also add an index on the new column.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With