2020-01-14 15:57:45 +03:00
|
|
|
.. meta::
|
|
|
|
:description: Manage Hasura metadata
|
|
|
|
:keywords: hasura, docs, metadata
|
|
|
|
|
2019-03-28 13:51:58 +03:00
|
|
|
.. _manage_hasura_metadata:
|
|
|
|
|
2019-09-11 10:17:14 +03:00
|
|
|
Managing Hasura metadata
|
2019-03-28 13:51:58 +03:00
|
|
|
========================
|
|
|
|
|
|
|
|
.. contents:: Table of contents
|
|
|
|
:backlinks: none
|
|
|
|
:depth: 1
|
|
|
|
:local:
|
|
|
|
|
|
|
|
If your Postgres schema is already managed with a tool like knex, TypeORM,
|
2019-09-11 10:17:14 +03:00
|
|
|
Django/Rails migrations, you will still need a way to export the actions you
|
2019-03-28 13:51:58 +03:00
|
|
|
performed on the Hasura console to apply it later on another Hasura instance.
|
|
|
|
|
|
|
|
All the actions performed on the console, like tracking tables/views/functions,
|
|
|
|
creating relationships, configuring permissions, creating event triggers and remote
|
|
|
|
schemas, etc. can be exported as a JSON file which can be version
|
2019-09-11 10:17:14 +03:00
|
|
|
controlled. The content of this JSON file is called "Hasura metadata". The
|
2019-03-28 13:51:58 +03:00
|
|
|
metadata file can be later imported to another Hasura instance to get the same
|
|
|
|
configuration. You can also manually edit the JSON file to add more objects to
|
2019-09-11 10:17:14 +03:00
|
|
|
it and then use it to update the instance.
|
2019-03-28 13:51:58 +03:00
|
|
|
|
|
|
|
Exporting Hasura metadata
|
|
|
|
-------------------------
|
|
|
|
|
|
|
|
.. rst-class:: api_tabs
|
|
|
|
.. tabs::
|
|
|
|
|
|
|
|
.. tab:: Console
|
|
|
|
|
2019-09-11 10:17:14 +03:00
|
|
|
1. Click on the settings (⚙) icon at the top right corner of the console screen.
|
|
|
|
2. In the Hasura metadata actions page that opens, click on the ``Export Metadata`` button.
|
2020-03-18 17:57:18 +03:00
|
|
|
3. This will prompt a file download for ``hasura_metadata_<timestamp>.json``. Save the file.
|
2019-03-28 13:51:58 +03:00
|
|
|
|
|
|
|
.. tab:: API
|
|
|
|
|
2020-03-11 22:42:36 +03:00
|
|
|
The export can be done via the :ref:`Metadata API
|
|
|
|
<api_manage_metadata>`.
|
2019-11-12 16:09:00 +03:00
|
|
|
Response will be a JSON object with the Hasura metadata. Here is an example
|
2019-03-28 13:51:58 +03:00
|
|
|
using ``curl`` to save this as a file:
|
|
|
|
|
|
|
|
.. code-block:: bash
|
|
|
|
|
2020-03-18 17:57:18 +03:00
|
|
|
curl -d'{"type": "export_metadata", "args": {}}' http://localhost:8080/v1/query -o hasura_metadata.json
|
2019-03-28 13:51:58 +03:00
|
|
|
|
2020-03-18 17:57:18 +03:00
|
|
|
This command will create a ``hasura_metadata.json`` file.
|
2019-11-12 16:09:00 +03:00
|
|
|
If an admin secret is set, add ``-H 'X-Hasura-Admin-Secret: <your-admin-secret>'`` as the API is an
|
2019-03-28 13:51:58 +03:00
|
|
|
admin-only API.
|
|
|
|
|
2019-09-11 10:17:14 +03:00
|
|
|
Importing Hasura metadata
|
2019-03-28 13:51:58 +03:00
|
|
|
-------------------------
|
|
|
|
|
2019-09-11 10:17:14 +03:00
|
|
|
You can apply exported metadata from one Hasura GraphQL engine instance to another. You can also apply an older or
|
|
|
|
modified version of an instance's metadata onto itself.
|
|
|
|
|
2019-11-12 16:09:00 +03:00
|
|
|
Importing completely replaces the metadata on that instance, i.e. you lose any metadata that was already present
|
|
|
|
before.
|
2019-03-28 13:51:58 +03:00
|
|
|
|
|
|
|
.. rst-class:: api_tabs
|
|
|
|
.. tabs::
|
|
|
|
|
|
|
|
.. tab:: Console
|
|
|
|
|
2019-09-11 10:17:14 +03:00
|
|
|
1. Click on the settings (⚙) icon at the top right corner of the console screen.
|
2019-03-28 13:51:58 +03:00
|
|
|
2. Click on ``Import Metadata`` button.
|
2020-03-18 17:57:18 +03:00
|
|
|
3. Choose a ``hasura_metadata.json`` file that was exported earlier.
|
2019-03-28 13:51:58 +03:00
|
|
|
4. A notification should appear indicating the success or error.
|
|
|
|
|
|
|
|
.. tab:: API
|
|
|
|
|
2020-03-11 22:42:36 +03:00
|
|
|
The exported JSON can be imported via the :ref:`Metadata API
|
|
|
|
<api_manage_metadata>`.
|
2019-11-12 16:09:00 +03:00
|
|
|
Here is an example using ``curl``:
|
2019-03-28 13:51:58 +03:00
|
|
|
|
|
|
|
.. code-block:: bash
|
|
|
|
|
2020-03-18 17:57:18 +03:00
|
|
|
curl -d'{"type":"replace_metadata", "args":'$(cat hasura_metadata.json)'}' http://localhost:8080/v1/query
|
2019-03-28 13:51:58 +03:00
|
|
|
|
2020-03-18 17:57:18 +03:00
|
|
|
This command reads the ``hasura_metadata.json`` file and makes a POST request to
|
2019-11-12 16:09:00 +03:00
|
|
|
replace the metadata.
|
|
|
|
If an admin secret is set, add ``-H 'X-Hasura-Admin-Secret: <your-admin-secret>'`` as the API is an
|
|
|
|
admin-only API.
|
2019-03-28 13:51:58 +03:00
|
|
|
|
|
|
|
.. note::
|
|
|
|
|
|
|
|
All the dependent objects, like tables, views, functions etc. should exist on
|
|
|
|
Postgres before importing the metadata. Otherwise, it will result in an error
|
|
|
|
saying the object does not exist. So, apply the Postgres schema first, before
|
|
|
|
importing the metadata.
|
|
|
|
|
|
|
|
|
2019-11-12 16:09:00 +03:00
|
|
|
.. _reload_metadata_manual:
|
|
|
|
|
|
|
|
Reloading Hasura metadata
|
|
|
|
-------------------------
|
|
|
|
|
|
|
|
In some cases, the metadata can be out of sync with the Postgres schema. For example,
|
|
|
|
when a new column has been added to a table via an external tool such as ``psql``.
|
|
|
|
|
|
|
|
.. rst-class:: api_tabs
|
|
|
|
.. tabs::
|
|
|
|
|
|
|
|
.. tab:: Console
|
|
|
|
|
|
|
|
1. Click on the settings (⚙) icon at the top right corner of the console screen.
|
|
|
|
2. Click on ``Reload`` button.
|
|
|
|
3. A notification should appear indicating the success.
|
|
|
|
|
|
|
|
.. tab:: API
|
|
|
|
|
2020-03-11 22:42:36 +03:00
|
|
|
The reload of metadata can be done via the :ref:`Metadata API
|
|
|
|
<api_manage_metadata>`.
|
2019-11-12 16:09:00 +03:00
|
|
|
Here is an example using ``curl``:
|
|
|
|
|
|
|
|
.. code-block:: bash
|
|
|
|
|
|
|
|
curl -d'{"type": "reload_metadata", "args": {}}' http://localhost:8080/v1/query
|
|
|
|
|
|
|
|
If an admin secret is set, add ``-H 'X-Hasura-Admin-Secret: <your-admin-secret>'`` as the API is an
|
|
|
|
admin-only API.
|
|
|
|
|
|
|
|
.. note::
|
|
|
|
|
|
|
|
Reloading may result in inconsistent metadata status. You may need to resolve
|
|
|
|
all inconsistent objects manually or delete them. After that, you need to reload
|
|
|
|
metadata again.
|
|
|
|
|
|
|
|
Managing Hasura metadata in CI/CD
|
|
|
|
---------------------------------
|
|
|
|
|
|
|
|
Using tools like ``curl`` you can easily integrate the metadata API requests for the above metadata management
|
|
|
|
actions with your CI/CD workflows.
|
|
|
|
|
2019-03-28 13:51:58 +03:00
|
|
|
In case you need an automated way of applying/importing the metadata, take a
|
2020-03-11 22:42:36 +03:00
|
|
|
look at the :ref:`CLI-Migrations <auto_apply_migrations>` Docker image, which
|
2019-11-12 16:09:00 +03:00
|
|
|
can start the GraphQL engine after automatically importing a mounted metadata file.
|