This project is archived and is in readonly mode.

#3286 ✓committed
Ben Marini

[PATCH] Add support for Mysql column positioning to migrations

Reported by Ben Marini | September 28th, 2009 @ 12:48 AM

This patch allows some extra options to be passed for #add_column and #change_column

From the mysql docs for alter table (http://dev.mysql.com/doc/refman/5.1/en/alter-table.html):
"To add a column at a specific position within a table row, use FIRST or AFTER col_name."

Here are the new options:
:first => true # Put the column in front of all the columns :after => column_name # Put the colmn after 'column_name'

Here is an example:

# Inside of a migration ...
add_column :new_col, :string, :first => true
add_column :another_col, :integer, :default => 0, :after => :new_col

Patch contains tests and applies cleanly to 2-3-stable and master. This is my first patch so any pointers would be appreciated, thanks!

Comments and changes to this ticket

  • Ben Marini

    Ben Marini September 28th, 2009 @ 03:09 AM

    • Tag changed from active_record, add_column, master, migrations, mysql, patch to 2-3-stable, active_record, add_column, master, migrations, mysql, patch
  • Peter Wagenet

    Peter Wagenet October 4th, 2009 @ 10:21 PM

    • Tag changed from 2-3-stable, active_record, add_column, master, migrations, mysql, patch to 2-3-stable, active_record, add_column, master, migrations, mysql, patch, tested

    This looks good to me. Check out #rails-contrib on IRC to see if you can get some more people to review this and maybe get it into Core.

  • luniki

    luniki December 3rd, 2009 @ 12:27 PM

    What's the state of this ticket?

  • Ben Marini

    Ben Marini December 4th, 2009 @ 05:28 AM

    Patch should apply cleanly still. Please test it out and give it a +1 if you like it, or feedback if you don't. I'm still motivated to get this patch accepted.

  • Jeremy Kemper

    Jeremy Kemper December 4th, 2009 @ 07:12 AM

    Hey Ben - curious why you added this. Aesthetics only?

  • Repository

    Repository December 4th, 2009 @ 07:12 AM

    • State changed from “new” to “committed”

    (from [e55284e8256461fc2440c41548ee9b4216f96b47]) Add support for Mysql column positioning via #add_column and #change_column

    add_column and change_column in the Mysql adapter now accept some
    additional options:
    :first => true # Put the column in front of all the columns :after => column_name # Put the colmn after 'column_name'

    add_column :new_col, :string, :first => true
    add_column :another_col, :integer, :default => 0, :after => :new_col

    [#3286 state:committed]

    Signed-off-by: Jeremy Kemper jeremy@bitsweat.net
    http://github.com/rails/rails/commit/e55284e8256461fc2440c41548ee9b...

  • Repository

    Repository December 4th, 2009 @ 07:12 AM

    (from [ccb197b2e6c2e773f908d3b00f5debe57c22311a]) Add support for Mysql column positioning via #add_column and #change_column

    add_column and change_column in the Mysql adapter now accept some
    additional options:
    :first => true # Put the column in front of all the columns :after => column_name # Put the colmn after 'column_name'

    add_column :new_col, :string, :first => true
    add_column :another_col, :integer, :default => 0, :after => :new_col

    [#3286 state:committed]

    Signed-off-by: Jeremy Kemper jeremy@bitsweat.net
    http://github.com/rails/rails/commit/ccb197b2e6c2e773f908d3b00f5deb...

  • Ben Marini

    Ben Marini December 4th, 2009 @ 10:32 PM

    Thanks for approving the patch Jeremy!

    I find it valuable to have my columns organized, just like any other parts of my code. It's nice when I'm scanning the columns of a table in my schema.rb file for reference, to see them in some sensible order. I've always been annoyed that my updated_at and created_at columns are no longer last on the list after adding more columns to a table with a migration. If I add a new foreign key, I'd like it to be clustered with any other foreign keys for that table. It also makes accessing the database via other applications like Mysql Query Browser a little nicer, since I can just use "select * from ..." and the columns are ordered in some sensible way.

    There also are some apparent performance benefits to gain by optimizing your column ordering in MySQL, although this was not a motivator for me.
    http://stackoverflow.com/questions/894522/is-there-any-reason-to-wo...

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Attachments