This allows applying Flyway operations (migrate, validate, etc) to a Google Cloud SQL Postgres instance. In essence, it's a recreation of the Flyway CLI tailored for use with the Cloud SQL JDBC socket factory.

It's possible that you could use the existing Flyway CLI, but the Cloud SQL adapter has a deep dependency tree that would be quite a pain to maintain in your local flyway CLI install.


Prepare a properties file like so:

DB_NAME = ...
DB_USER = ...

You'll also need a Google credentials json file. You can get one by creating a Service Account with suitable privileges in the Project containing the Cloud SQL instance you want to interact with.


First, build the tool. Since gradle doesn't make it easy to pass CLI args and env vars to forked jvms, just package it as an application and use that:

./gradlew distTar

Then, untar that somewhere handy, like tmp in this project:

mkdir -p tmp && \
    rm -rf tmp/flyway-gcp-cloud-sql-cli && \
    tar -xf build/distributions/flyway-gcp-cloud-sql-cli.tar -C tmp

Now, you could execute the newly untarred executable (tmp/flyway-gcp-cloud-sql-cli/bin/flyway-gcp-cloud-sql-cli), but it won't work, of course, because you haven't configured anything yet...

This uses the Google Cloud Sql JDBC SocketFactory to allow accessing a Cloud Sql instance from outside GCP. Unlike other GCP clients, the JDBC adapter only works with Application Default Credentials, which requires setting the GOOGLE_APPLICATION_CREDENTIALS env var to point to a credentials json file.

Together with the necessary CLI args, an invocation would look something like this:

GOOGLE_APPLICATION_CREDENTIALS=path/to/credentials.json \
    tmp/flyway-gcp-cloud-sql-cli/bin/flyway-gcp-cloud-sql-cli \
    -c \
    -l path-to-parent-of-migrations-dir \
    -m mode

mode is one of migrate, info, etc.

Note that if using clean, you also have to pass --enable-clean as a safety measure.