Automatic Google Cloud Spanner Schema Migrations with Atlas
Spanner is a fully managed, horizontally scalable, globally distributed, and strongly consistent database service offered by Google Cloud. It is designed to handle large-scale applications with high availability and low latency.
However, managing a large database schema with Spanner can be challenging due to the complexity of related data structures and the need for coordinated schema changes across multiple teams and applications.
Enter: Atlas
Atlas helps developers manage their database schema as code, abstracting away the intricacies of database schema management. With Atlas, users provide the desired state of the database schema and Atlas automatically plans the required migrations.
In this guide, we will dive into setting up Atlas for Spanner schema migrations, and introduce the different workflows available.
Prerequisites
- Docker
- Google Cloud Environment set up on your machine:
- Atlas installed on your machine:
- macOS + Linux
- Docker
- Windows
To download and install the custom release of the Atlas CLI, simply run the following in your terminal:
curl -sSf https://atlasgo.sh | ATLAS_FLAVOR="spanner" sh
To pull the Atlas image and run it as a Docker container:
docker pull arigaio/atlas:latest-extended
docker run --rm arigaio/atlas:latest-extended --help
If the container needs access to the host network or a local directory, use the --net=host flag and mount the desired
directory:
docker run --rm --net=host \
-v $(pwd)/migrations:/migrations \
arigaio/atlas:latest-extended migrate apply \
--url "oracle://PDBADMIN:Pssw0rd0995@localhost:1521/FREEPDB1"
Download the custom release and move the atlas binary to a file location on your system PATH.
- An Atlas Pro account. To use the Spanner driver, run:
$ atlas login
Creating a Spanner Instance and Database
If you want to use an existing Spanner instance and database, you can skip this step.
Let's start off by spinning up a Spanner instance using the Google Cloud Console or the gcloud command line tool.
gcloud spanner instances create my-instance \
--config=regional-us-central1 \
--description="My Spanner Instance" \
--nodes=1
Next, create a Spanner database within the instance:
gcloud spanner databases create my-database \
--instance=my-instance \
--ddl="CREATE TABLE users (id INT64 NOT NULL, email STRING(255), display_name STRING(255)) PRIMARY KEY(id);"
This command creates a new Spanner database named my-database with a users table.
Inspecting the Schema
The atlas schema inspect command supports reading the database description provided by a URL and outputting it in
different formats, including Atlas DDL (default), SQL, and JSON. In this guide, we will
demonstrate the flow using both the Atlas DDL and SQL formats, as the JSON format is often used for processing the
output using jq.
- Atlas DDL (HCL)
- SQL
To inspect our Spanner database, use the -u flag and write the output to a file named schema.hcl:
atlas schema inspect -u "spanner://projects/YOUR_PROJECT_ID/instances/my-instance/databases/my-database" \
> schema.hcl
Open the schema.hcl file to view the Atlas schema that describes our database.
table "users" {
schema = schema.default
column "id" {
null = false
type = INT64
}
column "email" {
null = true
type = STRING(255)
}
column "display_name" {
null = true
type = STRING(255)
}
primary_key {
columns = [column.id]
}
}
schema "default" {
}
To inspect our Spanner database, use the -u flag and write the output to a file named schema.sql:
atlas schema inspect -u "spanner://projects/YOUR_PROJECT_ID/instances/my-instance/databases/my-database" \
--format '{{ sql . }}' > schema.sql
Open the schema.sql file to view the inspected SQL schema that describes our database.
-- Create "users" table
CREATE TABLE `users` (
`id` INT64 NOT NULL,
`email` STRING(255),
`display_name` STRING(255)
) PRIMARY KEY (`id`);
For in-depth details on the atlas schema inspect command, covering aspects like inspecting specific schemas,
handling multiple schemas concurrently, excluding tables, and more, refer to our documentation
here.