Search code examples
node.jsgoogle-app-enginegoogle-cloud-sqlknex.jsgoogle-cloud-platform

Running Knex Migrations in Cloud SQL


I'm using Google App Engine and Cloud SQL to run an application.

I've successfully set up local and prod environments and their respective DB's. Just now, I've managed to set up some migrations in my local environment using the Knex migrate CLI and everything works just fine locally.

How does one go about running these migrations (and seeds!) after (or before?) doing a gcloud app deploy? I don't see anyway to access my project files from within cloud shell.

What am I missing?


Solution

  • We run migrations every time before starting the server in npm start. Prestart should work the same way that our system is doing it (we have separate starter shell script, which is called from package.json start).

    Have you setup your Cloud SQL connection correctly and tested that you actually have working DB connection? Check logs from Cloud SQL proxy container that is running in your app engine instance.

    You can do it by opening ssh connection to app engine instance and check logs from proxy container:

    sudo docker ps --no-trunc 
    sudo docker logs stoic_heisenberg
    

    My sql proxy container was called stoic_heisenberg. Also you should check that the unix socket is created correclty in gaeapp container.

    Entering container you can do

    container_exec gaeapp /bin/bash
    ls -la /cloudsql
    

    If there are no files, then cloud sql is not setup correctly.