June, 09 2020

Altering Tables In Production Database

As a developer, I have experienced changes in a product requirement after an app has been deployed on the production server. Most times, these changes require creating or altering an existing table in the database.

Laravel migration solves these two problems. If you want to add a new table to a Laravel application, you just have to generate a migration file for the table, add table columns, push the new file to the server and then run the artisan migrate command. This will create the desired table and add this migration file to the list of migration files that have been executed.

Check the migrations table in your database, that's where Laravel saves all migration files that have been executed.

Altering A Table Column

When altering a database table in production laravel also provides a way to do this.

Let's take for example we want to modify a slug column on the posts table to be nullable. First, we have to create a migration file for that.

php artisan make:migration update_slug_on_posts_table --table=posts

This will generate a migration file with update_slug_on_posts_table attached to the file name. Just like other migration files every change we are going to make goes inside the up() method.

...
public function up(){
   Schema::table('posts', function (Blueprint $table) {
    $table->string('slug')->nullable()->change();
   });
}

The change() method is what does the magic. Now you can commit your code, push to the production server and run the artisan migrate command.

Adding New Table Column

Adding a new column to a database table in production can also be achieved by following the same steps we discussed above.

Let's take for example we want to add an image_url column on the posts table.

Lets create a migration file for that:

php artisan make:migration add_image_url_to_posts_table --table=posts

This will generate a migration file with add_image_url_on_posts_table attached to the file name.

Now let's add our new image_url column to the up() method of the new migration file.

...
public function up(){
   Schema::table('posts', function (Blueprint $table) {
    $table->string('image_url')->after('slug');
   });
}

The after('slug') method will move our new column next to the slug column on the posts table.

Renaming A Table Column

Just like we discussed above you just have to update the up() method of your new migration file and used the renameColumn() method.

...
Schema::table('posts', function (Blueprint $table) {
   $table->renameColumn('slug', 'slugs');
});

Dropping A Table Column

...
Schema::table('posts', function (Blueprint $table) {
   $table->dropColumn('slug');
});

You can also drop multiple columns

$table->dropColumn(['slug', 'image_url']);

Now you can play around your database tables in production. You can visit the Laravel docs for more details.

. You can follow me on twitter.

Join my inner circle newsletter

Be the first to hear about anything I publish, launch, or think is helpful for you. Subscribe here

Hey, have you tried Litehost lately ?

Litehost is a web hosting platform for PHP & Laravel developers with Composer, Git, PHP & CLI pre-installed. Try it now