Resetting Hasura migrations

Introduction

This page explains how to reset the state of migrations and create new migrations from the state that is on the server. This can be useful if the current migration state on your local machine is corrupted.

Step 1: Delete the local migration files

Move all your migrations to a backup folder, in case you may need them later.

You can use the following command:

# move the contents of the migrations folder into the migrations_backup folder

mv migrations migrations_backup

Step 2: Reset the migration history on the server

To reset the migrations status on the server only, use:

# reset migrations on server only

hasura migrate delete --all --server --database-name <database-name>

To reset the migrations status on server and clean up the local project migration files

# reset migrations on server and on local project, use:

hasura migrate delete --all --database-name <database-name>

Step 3: Pull the schema and metadata from the server

If the migrations were reset, then we will set up fresh migrations by pulling the schema and metadata from the server using the following commands:

## create migration files - "this will only export public schema from postgres"

hasura migrate create "init" --from-server --database-name <database-name>
## note down the version
## mark the migration as applied on this server

hasura migrate apply --version "<version>" --skip-execution --database-name <database-name>
## to also export the Hasura metadata and save it in the ``/metadata`` directory

hasura metadata export

Note

If you are using schemas other than public, use the --schema <schema_name> flag to indicate each one of them in the create command. This flag can be used multiple times.

Step 4: Verify the status of the migrations

Run the following command to verify the migration status:

hasura migrate status  --database-name <database-name>

You should see the new migrations!