You want to add anew column or table to an existing database? Or do you want to add a new value to an existing table? Whichever it is, you definitely dont want migrate:rollback. That undoes the last migration and whatever data was stored in those columns will be gone. Id say never use migrate:rollback in production.
What is the typical process to add a field to a production database without loosing existing data
What is the typical process to add a field to a production database without loosing existing data?
Is migrate:rollback meant for this situation? If yes, how is this done?
Thanks
For the sake of demonstration, lets say you're adding that age field to your users table...
php artisan make:migration add_age_field_to_users_table
Edit the resulting file, and fill it thusly:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
class AddAgeFieldToUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->integer('age')->unsigned()->nullable();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('users', function (Blueprint $table) {
$table->dropColumn('age');
});
}
}
Run the migration:
php artisan migrate
If you don't make the field nullable, or at least provide a default value, you might run into issues next time a user record is updated.. but if it's nullable, it can remain empty until it's populated by the user.. so that's probably the best bet in this case.
Please or to participate in this conversation.