Cloud Control Plane
In the previous section, we demonstrated how to use the Atlas CLI to manage migrations for a database-per-tenant architecture. Next, we will see how to use the Atlas Cloud Control Plane to manage migrations across multiple databases.
Setting up
In this section, we will be continuing our minimal example from before, so if you are just joining us, please follow the steps in the previous section to set up your project.
Additionally, you will need an Atlas Cloud account. If you don't have one, you can sign up for free by running the following command and following the instructions on the screen:
atlas login
Pushing our project to Atlas Cloud
In order to manage our migrations across multiple databases, we need push our project to the Atlas Cloud
Schema Registry. But first, let's set up a local env
block in our atlas.hcl
file. Append the following
to the file:
env "local" {
dev = "sqlite://?mode=memory"
migration {
dir = "file://migrations"
}
}
Next, push the project to the Atlas Cloud Schema Registry by running the following command:
atlas migrate push --env prod db-per-tenant
Atlas will push our migration directory to the Schema Registry and print the URL of the project, for example:
https://rotemtam85.atlasgo.cloud/dirs/4294967396
Working with Atlas Cloud
Deploying from the Registry
Once we have successfully pushed our project to the Schema Registry, we can deploy from it to our target
databases. To do this, let's make a small change to our prod
env in atlas.hcl
:
env "prod" {
for_each = toset(local.tenant)
url = "sqlite://${each.value}.db"
migration {
dir = "atlas://db-per-tenant"
}
}
Now, we can deploy the migrations to our target databases by running:
atlas migrate apply --env prod
Atlas will read the most recent version of our migration directory from the schema registry, apply the migrations to each target database, report the results to Atlas Cloud, and print the results:
No migration files to execute
No migration files to execute
https://rotemtam85.atlasgo.cloud/deployments/sets/94489280593
In this case, we see that there were no new migrations to apply to the target databases. Let's show how this flow works when there is work to be done in the next section.
Another migration
Let's plan another migration to our project. Create a new migration file by running:
atlas migrate new --edit seed_users
In the editor, add the following SQL statements:
INSERT INTO users (id, name) VALUES (1, "a8m");
INSERT INTO users (id, name) VALUES (2, "rotemtam");
Save the file and exit the editor. Let's push the new migration to the Schema Registry:
atlas migrate push --env prod db-per-tenant