Automatic Aurora DSQL Schema Migrations with Atlas
Amazon Aurora DSQL is a serverless, distributed relational database service optimized for transactional workloads. Aurora DSQL is PostgreSQL-compatible (version 16), offering ACID transactions with strong consistency and snapshot isolation. It provides 99.99% single-Region and 99.999% multi-Region availability with automatic scaling and self-healing architecture.
Support for Aurora DSQL is available exclusively to Pro users. To use this feature, run:
atlas login
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 Aurora DSQL schema migrations, and introduce the different workflows available.
Prerequisites
- An AWS account with the necessary permissions to create an Aurora DSQL cluster.
- Atlas installed on your machine:
- macOS + Linux
- Homebrew
- Docker
- Windows
- CI
- Manual Installation
To download and install the latest release of the Atlas CLI, simply run the following in your terminal:
curl -sSf https://atlasgo.sh | sh
Get the latest release with Homebrew:
brew install ariga/tap/atlas
To pull the Atlas image and run it as a Docker container:
docker pull arigaio/atlas
docker run --rm arigaio/atlas --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 migrate apply \
--url "mysql://root:pass@:3306/test"
Download the latest release and move the atlas binary to a file location on your system PATH.
GitHub Actions
Use the setup-atlas action to install Atlas in your GitHub Actions workflow:
- uses: ariga/setup-atlas@v0
with:
cloud-token: ${{ secrets.ATLAS_CLOUD_TOKEN }}
Other CI Platforms
For other CI/CD platforms, use the installation script. See the CI/CD integrations for more details.
Logging in to Atlas
Aurora DSQL is only available on the Pro plan or during a Trial. Upgrade your plan to get started.
To use Aurora DSQL with Atlas, you'll need to log in to Atlas. If it's your first time, you will be prompted to create both an account and a workspace (organization):
- Via Web
- Via Token
- Via Environment Variable
atlas login
atlas login --token "ATLAS_TOKEN"
ATLAS_TOKEN="ATLAS_TOKEN" atlas login
Connecting to Aurora DSQL
Atlas uses the dsql:// scheme for connecting to Aurora DSQL clusters:
- Connect to Database
- Connect to Schema
Connect to a database (all schemas):
dsql://admin:<password>@<cluster-endpoint>/?sslmode=require
Example:
dsql://admin:mypassword@abc123xyz.dsql.us-east-1.on.aws/?sslmode=require
Connect to a specific schema using the search_path query parameter:
dsql://admin:<password>@<cluster-endpoint>/?sslmode=require&search_path=<schema>
Example:
dsql://admin:mypassword@abc123xyz.dsql.us-east-1.on.aws/?sslmode=require&search_path=public
- Aurora DSQL requires SSL connections (
sslmode=require) - The
adminuser is the default DSQL user - Password is obtained from an Aurora DSQL auth token
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 your Aurora DSQL cluster, use the -u flag and write the output to a file named schema.hcl:
atlas schema inspect \
-u "dsql://admin:pass@cluster.dsql.us-east-1.on.aws/?sslmode=require" > schema.hcl
Open the schema.hcl file to view the Atlas schema that describes your database.
table "users" {
schema = schema.public
column "id" {
null = false
type = bigint
}
column "name" {
null = false
type = character_varying(255)
}
primary_key {
columns = [column.id]
}
}
schema "public" {
}
To inspect your Aurora DSQL cluster, use the -u flag and write the output to a file named schema.sql:
atlas schema inspect \
-u "dsql://admin:pass@cluster.dsql.us-east-1.on.aws/?sslmode=require" \
--format '{{ sql . }}' > schema.sql
Open the schema.sql file to view the inspected SQL schema that describes your database.
-- Create "users" table
CREATE TABLE "users" (
"id" bigint NOT NULL,
"name" character varying(255) NOT NULL,
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.
Declarative Migrations
The declarative approach, sometimes called "state-based migrations", lets users manage schemas by defining the desired state of the database as code. Atlas then inspects the target database and calculates an execution plan to reconcile the difference between the desired and actual states. Let's see this in action.
We will start off by making a change to our schema file, such as adding a repos table:
- Atlas DDL (HCL)
- SQL
table "users" {
schema = schema.public
column "id" {
null = false
type = bigint
}
column "name" {
null = false
type = character_varying(255)
}
primary_key {
columns = [column.id]
}
}
table "repos" {
schema = schema.public
column "id" {
type = bigint
null = false
}
column "name" {
type = character_varying(255)
null = false
}
column "owner_id" {
type = bigint
null = false
}
primary_key {
columns = [column.id]
}
}
schema "public" {
}
-- Create "users" table
CREATE TABLE "users" (
"id" bigint NOT NULL,
"name" character varying(255) NOT NULL,
PRIMARY KEY ("id")
);
-- Create "repos" table
CREATE TABLE "repos" (
"id" bigint NOT NULL,
"name" character varying(255) NOT NULL,
"owner_id" bigint NOT NULL,
PRIMARY KEY ("id")
);
Now that our desired state has changed, to apply these changes to our database, Atlas will plan a migration for us
by running the atlas schema apply command:
- Atlas DDL (HCL)
- SQL
atlas schema apply \
-u "dsql://admin:pass@cluster.dsql.us-east-1.on.aws/?sslmode=require" \
--to file://schema.hcl \
--dev-url "docker://dsql/16"
atlas schema apply \
-u "dsql://admin:pass@cluster.dsql.us-east-1.on.aws/?sslmode=require" \
--to file://schema.sql \
--dev-url "docker://dsql/16"
Approve the proposed changes, and that's it! You have successfully run a declarative migration.
For a more detailed description of the atlas schema apply command refer to our documentation
here.