Header

How do you handle database changes during a deployment?

Tips & Tricks

Whilst deployment services like DeployHQ will generally only handle file changes, it can be necessary, particularly when deploying a major release of your app, that changes might need to be made to the underlying database structure.

To be able to keep database changes version controlled in the same way as your files, a useful technique can be to organise "migrations".

What are Database migrations?

Database migrations are a way to control updates to a database's underlying structure in a more granular way, allowing ease of changes as part of a deployment strategy, and the potential to easily rollback if required.

Normally, this happens by the use of a series of files that define what those changes are, which can both created, and then referenced by a specific tool during the actual migration process.

Common tools include Laravel's Artisan, Delicious Brains' dbi migrate tool (for migrations of WordPress based sites) and the Rails Rake tool.

Creating a migration

One particularly big advantage of migration techniques such as the above, is that they can be controlled by, and live within the same repository as the rest of your app, and can therefore be easily tracked in your version control software of choice.

For example, using Artisan, you'd run a command like the following:

php artisan make:migration create_users_table

This'll normally populate a new file in app/database/migrations named as above and including a timestamp of when it was created. This is then used later on by the migration process to determine which files actually need to be checked and actioned as necessary.

Then, within the migration file, you can further define actions for the process to take:


class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->id();
            $table->string('first_name');
            $table->string('last_name');
            $table->string('email'); 
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('users');
    }
}

This allows you to define specific columns to create as part of the migration, as well as add a command to drop the table, i.e. rollback the change if required. We'd recommend taking a look at Laravel's full guide to the Artisan tool if you want to explore further.

The Rails Rake migration process works very similarly, and you can find more information about that process specifically in this guide.

And further information on using the Delicious Brains dbi migrate tool for migrations within your WordPress site, can be found here.

Setting up Migrations in DeployHQ

Once you have migrations defined, committed to your version control system and ready to you, you'll then want to start using them during a deployment. This is best achieved using our SSH commands feature.

Within your project, you can define SSH Commands using the option on the left-hand side, and simply enter a command, in just the same way it'd be run on your server, and ensure it's set to run after changes have been uploaded (i.e. so that your migrations files exist on the server).

Screenshot

This will then automatically run on every deployment. The process should run as such that if there are no migrations to complete, it'll just skip.

For additional robustness, you could also look to set this up as part of an atomic deployment strategy.

If you have any questions about how DeployHQ might be able to help with your database changes, please don't hesitate to ask.

A little bit about the author

I'm Adam W, or as everyone prefers to call me - Batman. I get a lot of satisfaction out of helping our customers and am committed to making their customer experience as positive as possible. When not offering support to our customers, you'll find me gaming, listening to music or running.

Proudly powered by Katapult. Running on 100% renewable energy.