This article was originally published on bmf-tech.com.
Overview
I used golang-migrate for database migration with Spanner, so here's a note.
Usage
Assuming usage with Docker.
I was running it as a binary instead of Docker, but it might not work depending on the version of OpenSSL on the host machine, so running it in a container seems safer.
MIGRATE_VERSION='v4.14.1'
docker run -v /migrations:/migrations -v ~/.config/gcloud/:/root/.config/gcloud --network host migrate/migrate:${MIGRATE_VERSION} -path=/migrations/ -database spanner://projects/<PROJECT ID>/instances/<INSTANCE>/databases/<DATABASE>?x-clean-statements=True <COMMAND>
Specify commands like up, down, version, etc., in COMMAND.
cf. github.com - golang/migrate/migrate/tree/master/cmd/migrate#usage
I wrote the execution command as a one-liner, so it might be hard to read, but there shouldn't be anything particularly difficult.
The two mounts are as follows:
-v /migrations:/migrations -v ~/.config/gcloud/:/root/.config/gcloud
Prepare the SQL files for migration in /migrations. Mount them to the container's /migrations.
~/.config/gcloud/:/root/.config/gcloud is for passing gcloud authentication.
You can also pass authentication by mounting the credential file and setting the environment variable GOOGLE_APPLICATION_CREDENTIALS, but this way is easier...
In the latest version of golang-migrate, query parameters are required for Spanner connection information.
spanner://projects/<PROJECT ID>/instances/<INSTANCE>/databases/<DATABASE>?x-clean-statements=True
Here's the background:
Top comments (0)