Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?


  • I'm not looking for Annotate Models plugins by Dave Thomas. What I mean by comments is inside MySQL queries.
like image 714
TK. Avatar asked Sep 02 '10 14:09

TK.


People also ask

What is are the default column columns that Rails will generate while migrating?

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

What does Add_index do in Rails?

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.

How do you write migration in Rails?

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.

How do I add a reference column in rails?

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.


2 Answers

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.

like image 57
PinnyM Avatar answered Nov 15 '22 00:11

PinnyM


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
like image 27
rjk Avatar answered Nov 14 '22 23:11

rjk