Managing migration files

Managing the database schema to perform incremental, reversible updates that are version controlled is a critical component for apps running in production. Hasura comes with a built-in migration system to manage the database schema.

More importantly, migration files are generated automatically by the Console UI, when served through the CLI. This makes it easy to make changes to the schema like creating tables, columns, functions, views by simply using the UI. Read more about the architecture of how Hasura built the UI to autogenerate database migrations.

Of course, the UI is not the only way to generate migration files, albeit convenient. The hasura CLI lets you create migration files manually. That's the option we are going to use below to initialise our schema for this tutorial.

Download the SQL file and execute the following command.

hasura migrate create init --sql-from-file `/path/to/schema.sql` --database-name default

Update the path to the SQL file appropriately. This would create a migration file with the contents of the above SQL file. Check the newly created migration files in the migrations directory of your hasura project.

Next up, execute the following command:

hasura migrate apply --database-name default

This would apply the migration files present in the migrations directory, following the order of files.

Now head to http://localhost:9695/console/data (the Data tab) to see the list of untracked tables. These are the tables that were part of the SQL file used in the initial migration.

If you are looking to reset migration, follow this blog post Resetting Hasura Migrations

Note: Using Hasura's migration system is optional. In case you are comfortable or familiar using other database migration tooling, you can continue to use that to manage your database schema. To disable Hasura's migration, you can do so via the Console served by the CLI. Head to to the Data -> Migrations tab and switch off the toggle Allow Postgres schema changes via console.


Get Started with GraphQL Now

Hasura Cloud gives you a fully managed, production ready GraphQL API as a service to help you build modern apps faster.
Ready to get started?
Start for free on Hasura Cloud or you could contact our sales team for a detailed walk-through on how Hasura may benefit your business.
Stay in the know
Sign up for full access to our community highlights, new features, and occasional baby animal gifs! Oh, and we have a strict no-spam rule. โœŒ๏ธ