Set up Hasura Migrations (config v2)
Introduction
If you don’t already use any tool to manage your Postgres schema, you can use Hasura to do that for you. Hasura has a CLI which will help you save each action that you do on the Console, including creating tables/views and schema modifying SQL statements, as SQL files. These files are called Migrations and they can be applied and rolled back step-by-step. These files can be version controlled and can be used with your CI/CD system to make incremental updates.
Let's say we have the following two tables in our schema:
author (id uuid, name text, rating integer)
article (id uuid, title text, content text, author_id uuid)
Now we want to set up Migrations starting with this schema.
Step 1: Disable the Console on the server
To use Migrations effectively, the Console on the server (which is served at /console
) should be disabled and all
changes must go through the Console served by the CLI. Otherwise, changes could be made through the server Console and
they will not be tracked by Migrations.
So, the first step is to disable the Console served by the GraphQL Engine server. In order to do that, remove the
--enable-console
flag from the command that starts the server or set the following environment variable to false
:
HASURA_GRAPHQL_ENABLE_CONSOLE=false
If this is set in YAML, make sure you quote the word false, i.e. HASURA_GRAPHQL_ENABLE_CONSOLE: "false"
.
Step 2: Install the Hasura CLI
Follow the instructions in Installing the Hasura CLI.
Step 3: Set up a project directory
For the endpoint referred here, let's say you've deployed the GraphQL Engine on Hasura Cloud, then this endpoint is:
https://my-graphql.hasura.app
. In case you've deployed Hasura using Docker, the URL might be
http://xx.xx.xx.xx:8080
. In any case, the endpoint should not contain the v1/graphql
API path. It should just be
the hostname and any sub-path if it is configured that way.
Let's set up a project directory by executing the following command:
hasura init my-project --endpoint http://my-graphql.hasura.app
cd my-project
This will create a new directory called my-project
with a config.yaml
file, a migrations
directory and a
metadata
directory. This directory structure is mandatory to use Hasura Migrations.
In case there is an admin secret set, you can set it as an environment variable
HASURA_GRAPHQL_ADMIN_SECRET=<your-admin-secret>
on your local machine and the CLI will use it. You can also use it as
a flag to CLI commands: --admin-secret '<your-admin-secret>'
.
Step 3.1: Set up version control for your project directory
The project directory created above can be committed to version control.
Set up version control and commit the project status:
# in project dir
# initialize version control
git init
# commit initial project status
git add .
git commit -m "hasura project init"
Step 4: Initialize the Migrations and Metadata as per your current state
If you have already set up your database and GraphQL API, you need to initialize your database Migrations and Hasura Metadata with the current state of the database.
Step 4.1: Initialize database Migrations
Create a migration called init
by exporting the current Postgres schema from the server:
# create migration files (note that this will only export the public schema from postgres)
hasura migrate create "init" --from-server
# note down the version
# mark the migration as applied on this server
hasura migrate apply --version "<version>" --skip-execution
This command will create a new directory named <timestamp>_init
inside the migrations
directory. In the newly
created directory, there's a file named up.sql
. This file will contain the required information to reproduce the
current state of the server including the Postgres (public) schema. If you'd like to read more about the format of
migration files, check out the
Migration file format reference (config v2)
The apply command will mark this migration as "applied" on the server.
If you need to export other schemas along with public
, you can name them using the --schema
flag.
For example, to export schemas public
, schema1
and schema2
, execute the following command:
hasura migrate create "init" --from-server --schema "public" --schema "schema1" --schema "schema2"
Step 4.2: Initialize Hasura Metadata
Export the Hasura Metadata from the server:
# export the metadata
hasura metadata export
This command will export the current Hasura Metadata as a bunch of YAML files in the metadata
directory.
If you'd like to read more about the format of Metadata files, check out the Metadata format reference (config v2)
Step 4.3: Add a checkpoint to version control
Commit the current project state to version control:
# in project dir
git add .
git commit -m "initialize migrations and metadata"
The version control set up should typically be done right after Step 3
Step 5: Use the Console from the CLI
From this point onwards, instead of using the Console at http://my-graphql.hasura.app/console
you should use the
console from the CLI by running:
# in project dir
hasura console
Step 6: Add a new table and see how Migrations and Metadata is updated
As you use the Hasura Console UI served by the CLI to make changes to your schema, database migration files are
automatically generated in the migrations/
directory and the Metadata is exported in the metadata/
directory of your
project.
Let's create the following table address (id uuid, street text, zip text, city text, country text, author_id int)
and
then create a foreign-key to the author
table via the author_id -> id
columns.
In the migrations
directory, we can find new directories called <timestamp>_create_table_public_address
and
<timestamp>_set_fk_public_address_author_id
containing an up.sql
and a down.sql
migration files for the changes we
made.
You can also go ahead and add permissions and create relationships for the address table. The related Metadata changes
will automatically be exported into the metadata
directory.
Migrations are only created when using the Console through the CLI.
Step 7: Squash Migrations and add checkpoints to version control
As you keep using the Console via the CLI to make changes to the schema, new migration files will keep getting generated and the Metadata files will keep getting updated automatically.
Typically while adding a feature a lot of incremental migration files get created for each of the small tasks that you did to achieve the feature. To improve maintainability of the migration files and to ensure you can go back to a particular version of the metadata, it is recommended that you squash your migration files and commit the project status in version control whenever you reach a logical checkpoint in your feature development.
The following command will squash all migration files from the given migration to the latest migration into a single migration file.
hasura migrate squash --name "<feature-name>" --from <start-migration-version>
# note down the version
# mark the squashed migration as applied on this server
hasura migrate apply --version "<squash-migration-version>" --skip-execution
Commit the project status into version control.
# in project dir
git add .
git commit -m "<feature-name>"
The version control set up should typically be done right after Step 3
Step 8: Apply the Migrations and Metadata on another instance of the GraphQL Engine
Apply all Migrations present in the migrations/
directory and the Metadata present in the metadata/
directory on a
new instance at http://another-graphql-instance.hasura.app
:
# in project dir
hasura migrate apply --endpoint http://another-graphql-instance.hasura.app
hasura metadata apply --endpoint http://another-graphql-instance.hasura.app
In case you need an automated way of applying the Migrations and metadata, take a look at the cli-migrations Docker image, which can start the GraphQL Engine after automatically applying the Migrations and Metadata which are mounted onto directories.
If you now open the Console of the new instance, you can see that the three tables have been created and are tracked:
Step 9: Check the status of Migrations
# in project dir
hasura migrate status
This command will print out each migration version present in the migrations
directory along with its name, source
status and database status.
For example,
$ hasura migrate status
VERSION NAME SOURCE STATUS DATABASE STATUS
1590493510167 init Present Present
1590497881360 create_table_public_address Present Present
Such a migration status indicates that there are 2 migration versions in the local directory and both of them are applied on the database.
If SOURCE STATUS
indicates Not Present
, it means that the migration version is present on the server, but not on the
current user's local directory. This typically happens if multiple people are collaborating on a project and one of the
collaborators forgot to pull the latest changes which included the latest migration files, or another collaborator
forgot to push the latest migration files that were applied on the database. Syncing of the files would fix the issue.
If DATABASE STATUS
indicates Not Present
, it denotes that there are new migration versions in the local directory
which are not applied on the database yet. Executing hasura migrate apply
will resolve this.