Rails 4 Migration: How to Reorder Columns

Rails 4 migration: how to reorder columns

When using MySQL, you can call change_column, but you have to repeat the column type (just copy and paste it from your other migration):

def up
change_column :your_table, :some_column, :integer, after: :other_column
end

Or if you have to reorder multiple columns in one table:

def up
change_table :your_table do |t|
t.change :some_column, :integer, after: :other_column
# ...
end
end

change_column calls ALTER TABLE under the hood. From the MySQL documentation:

You can also use FIRST and AFTER in CHANGE or MODIFY operations to
reorder columns within a table.

Note that this approach doesn't work with PostgreSQL. (see Alter column positions)

How do I re-order table columns in a Rails app?

You need to reorder columns in ActiveAdmin? Let's do it in corresponding admin/student_detail.rb file

index do
selectable_column
column :exact_length
column :returned_home
column :has_spouse
end

show do
attributes_table do
row :title
row :returned_home
row :has_spouse
end
end

More info about customizing index and show views you can find in the docs

Add_column migration column order

Well, SQLite does not handle AFTER syntax so in this situation the best solution is leave unchanged order of columns or create new table.

Is it possible to re-order columns when doing a migration?

Changing the original migrations is not considered as a best practice especially if multiple people are working on the same project, and every one has to reset their database each time you change existing migrations.

If you still feel strongly about the order of columns, one of the ways is to write a sql queries as part of migration and the support of ordering columns depends on the database.

For mySQL (https://dev.mysql.com/doc/refman/5.7/en/alter-table.html):

To add a column at a specific position within a table row, use FIRST or AFTER col_name. The default is to add the column last. You can also use FIRST and AFTER in CHANGE or MODIFY operations to reorder columns within a table.`

Change Position of Existing column in Rails

You can call change_column migration, like shown below:

def up
change_column :your_table, :some_column, :integer, after: :other_column
end

For more information have look into this: Rails 4 migration: how to reorder columns

Make a new column (migration) between two other columns?

If you haven't commited your changes to production you can reorder migrations by rolling them back and then changing the timestamps in the file name.

If thats not an alternative you can actually re-order the columns on some databases directly with SQL even if its not part of the migrations DSL. Migrations are after all really just a DSL to create SQL strings and run them in a repeatable way across environments.

If you can't generate the SQL you want with the DSL you can always use execute to execute a raw DSL string.

# MySQL example
class ReorderYourTableName < ActiveRecord::Migration[5.2]
def change
execute "alter table yourTableName change column yourColumnName yourColumnName dataType after yourSpecificColumnName;"
end
end

However on some DBs you can't actually reorder the columns without extensive steps of creating new columns and shuffling the data around.

Reorder/change timestamp on migration files

When you run rake db:migrate command it compares schema_migrations table and migration files located in db/migrate folder. All the migrations which were not executed receive MigrationClass#up call then.

So starting from the point when your code is already published and/or migrations are run by other users, changing your migrations timestamps/names may lead to unprocessable migration procedure (as schema_migrations will treat a migration with changed timestamp as new, unprocessed one, and try to process it "again"). Possible workaround for this would be to comment the contents of up method for a while and uncomment it back after migrations are done. For fun you can also manipulate schema_migrations table directly from your db console (adding or removing necessary records). Both of these ways smells like a hack though.

Until then... Everything should work flawlessly.



Related Topics



Leave a reply



Submit