Setting up a New Project Database in Homestead with Envoy

If you’ve just started using Laravel Homestead you know just how easy it makes getting started on a new project. On my first attempt I was up and running in under five minutes…very cool!

While adding a new project today I realized there isn’t a built-in way to do project-specific provisioning. There are a few tasks I do on nearly every project and I’d prefer to not have to jump into the VM to do them (mostly because I have to always look up the commands). Enter Envoy.

Nearly every site has a database. Why not automate its creation? Envoy can take care of that in a snap.

MySQL

@servers(['vm' => '-p 2222 vagrant@127.0.0.1'])

@task('init-local', ['on' => 'vm'])
    mysql -u homestead -psecret -e "create database :db_name
@endtask

Postgres

@servers(['vm' => '-p 2222 vagrant@127.0.0.1'])

@task('init-local', ['on' => 'vm'])
    echo "*:*:*:*:secret" > ~/.pgpass
    chmod 600 ~/.pgpass
    createdb -h 127.0.0.1 -U homestead -O homestead :db_name
    rm ~/.pgpass
@endtask

This is just the beginning of what you could do for a new project within Homestead. Other obvious tasks might be migrating and seeding the database, running custom Artisan commands, or even other sync’d shell scripts. It’s a breeze!