Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When adding a new column to a table, should I create a new migration or edit an old one?

If I create a table with rails generate migration, I can add an extra column to it later by creating a new migration. I could also rollback the original migration and then edit it to include the extra column.

Method 1: New Migration

//Create the model including the migration
$ rails generate model Foo bar:string

//Perform the migration
$ rake db:migrate

//Create the add column migration
$ rails generate migration add_foobar_to_foos foobar:string

//Perform the new migration
$ rake db:migrate

Method 2: Rollback

//Create the model including the migration
$ rails generate model Foo bar:string

//Perform the migration
$ rake db:migrate

//Rollback the migration
$ rake db:rollback

//Edit the original migration file

//Perform the new migration
$ rake db:migrate

What is the correct/best way to accomplish this task and why?

like image 754
Rupert Madden-Abbott Avatar asked Jan 04 '11 02:01

Rupert Madden-Abbott


People also ask

What is migration table?

Migration tables are files that are created by an administrator. They are used to implement the advanced capabilities of Group Policy object (GPO) import and copy operations. A migration table can contain information about references to security principals, to Universal Naming Convention (UNC) paths, or to both.

How do I add a column in laravel migration without losing data?

database\migration\add_new_column_to_products_table.php Now you can run migrate command to add this new field. Simply you can run below command to auto add a new field. Hope it can help you. Laravel Migration example tutorial, in this tutorial you have learned how to add new column to a table without losing data.


1 Answers

Ill go with method 1. why? because if other developers/machines are working with this environment you can get an inconstant state due to the fact that they might need to rollback at the right time in order to maintain correct db structure.

like image 195
ddayan Avatar answered Oct 01 '22 05:10

ddayan