2020-01-14 15:57:45 +03:00
|
|
|
.. meta::
|
|
|
|
:description: How Hasura migrations work
|
|
|
|
:keywords: hasura, docs, migration, how it works
|
|
|
|
|
2020-03-11 22:42:36 +03:00
|
|
|
.. _migrations_how_it_works:
|
|
|
|
|
2019-09-11 10:17:14 +03:00
|
|
|
How Hasura migrations work
|
|
|
|
==========================
|
2019-03-28 13:51:58 +03:00
|
|
|
|
|
|
|
.. contents:: Table of contents
|
|
|
|
:backlinks: none
|
|
|
|
:depth: 1
|
|
|
|
:local:
|
|
|
|
|
2019-09-11 10:17:14 +03:00
|
|
|
This is an explanation on how the Hasura migration system works. To understand how
|
2020-04-29 11:00:26 +03:00
|
|
|
to use the system, refer to :ref:`Migrations & Metadata <migrations>`.
|
2019-03-28 13:51:58 +03:00
|
|
|
|
|
|
|
Metadata
|
|
|
|
--------
|
|
|
|
|
|
|
|
Let's first talk about metadata. Whenever you do certain actions on the console
|
2020-03-11 22:42:36 +03:00
|
|
|
or via the API, Hasura records it in the :ref:`metadata catalogue <hasura_metadata_schema>`
|
2019-10-17 15:02:01 +03:00
|
|
|
which is a schema called ``hdb_catalog`` in your Postgres database. For example, if you track
|
|
|
|
a table, a new entry is created in the ``hdb_catalog.hdb_table`` table in Postgres.
|
|
|
|
Similarly, there are more tables in this schema to track relationships, event triggers,
|
|
|
|
functions and remote schemas.
|
2019-03-28 13:51:58 +03:00
|
|
|
|
2020-04-29 11:00:26 +03:00
|
|
|
All information in this schema can be exported as files. Export
|
|
|
|
options are available on the console, CLI and via the API. These files when
|
2019-09-11 10:17:14 +03:00
|
|
|
imported to an existing or new Hasura instance, will clear out the
|
2019-03-28 13:51:58 +03:00
|
|
|
``hdb_catalog`` schema on that instance and populates it again with the imported
|
|
|
|
data. One thing to note is that all the Postgres resources the metadata refers
|
|
|
|
to should already exist when the import happens, otherwise Hasura will throw an
|
|
|
|
error.
|
|
|
|
|
|
|
|
Migrations
|
|
|
|
----------
|
|
|
|
|
2020-04-29 11:00:26 +03:00
|
|
|
While metadata can be exported as files as a representation of the state
|
2019-09-11 10:17:14 +03:00
|
|
|
of Hasura, you might want more granular step-by-step checkpoints on the
|
2019-03-28 13:51:58 +03:00
|
|
|
evolution of the state. You might also want to track the Postgres schema changes
|
|
|
|
through Hasura's migration system.
|
|
|
|
|
|
|
|
Migrations are stored and applied as steps (or versions). A migration step (or
|
2020-04-29 11:00:26 +03:00
|
|
|
version) contains changes to the Postgres schema. The
|
2019-03-28 13:51:58 +03:00
|
|
|
migration version can also store the ``up`` migration (creating resources) and
|
|
|
|
the ``down`` migration (deleting resources). For example, migration version
|
|
|
|
``1`` can include the SQL statements required to create a table called
|
2020-04-29 11:00:26 +03:00
|
|
|
``profile`` as the ``up`` migration and SQL statements to drop this table as
|
|
|
|
the ``down`` migration.
|
2019-03-28 13:51:58 +03:00
|
|
|
|
2019-09-11 10:17:14 +03:00
|
|
|
The migration versions can be automatically generated by the Hasura console or
|
2020-04-29 11:00:26 +03:00
|
|
|
can be written by hand. They are stored as SQL files in a directory
|
2019-03-28 13:51:58 +03:00
|
|
|
called ``migrations``.
|
|
|
|
|
|
|
|
For more details on the format of these files, refer to
|
2020-04-29 11:00:26 +03:00
|
|
|
:ref:`migration_file_format_v2`.
|
2019-03-28 13:51:58 +03:00
|
|
|
|
2019-09-11 10:17:14 +03:00
|
|
|
When someone executes ``migrate apply`` using the Hasura CLI, the CLI will first
|
2019-03-28 13:51:58 +03:00
|
|
|
read the migration files present in the designated directory. The CLI would then
|
2019-09-11 10:17:14 +03:00
|
|
|
contact the Hasura Server and get the status of all migrations applied to the
|
|
|
|
server by reading the ``hdb_catalog.schema_migrations`` table. Each row in this
|
2019-03-28 13:51:58 +03:00
|
|
|
table denotes a migration version that is already applied on the server.
|
|
|
|
|
|
|
|
By comparing these two sets of versions, the CLI derives which versions are
|
2019-09-11 10:17:14 +03:00
|
|
|
already applied and which are not. The CLI would then go ahead and apply the
|
2019-03-28 13:51:58 +03:00
|
|
|
migrations on the server. This is done by executing the actions against the
|
2019-09-11 10:17:14 +03:00
|
|
|
database through the Hasura metadata APIs. Whenever the ``apply`` command is
|
2019-03-28 13:51:58 +03:00
|
|
|
used, all migrations that are to be applied are executed in a Postgres
|
2019-09-11 10:17:14 +03:00
|
|
|
transaction (through a ``bulk`` API call). The advantage of doing this is that if
|
2019-03-28 13:51:58 +03:00
|
|
|
there are any errors, all actions are rolled back and the user can properly
|
|
|
|
debug the error without worrying about partial changes.
|
|
|
|
|
2019-09-11 10:17:14 +03:00
|
|
|
The default action of the ``migrate apply`` command is to execute all the ``up``
|
2019-03-28 13:51:58 +03:00
|
|
|
migrations. In order to roll back changes, you would need to execute ``down``
|
2019-09-11 10:17:14 +03:00
|
|
|
migrations using the ``--down`` flag on the CLI.
|
2019-03-28 13:51:58 +03:00
|
|
|
|
2019-09-11 10:17:14 +03:00
|
|
|
This guide provides an overall idea of how the system works. For more details
|
2020-04-29 11:00:26 +03:00
|
|
|
on how to actually use the system, refer to :ref:`migrations`.
|