This repo contains a service designed to keep a Hasura instance in sync with a Postgres database.
You already have a Postgres instance and a Hasura instance and you want something to do the hard work of keeping Hasura aware of your changing database schema.
Enter hasura-postgres-schema-sync, a small Python service that regularly walks the schema of the given Postgres database, converts it to
Hasura metadata and updates Hasura with it.
- Docker w/ Docker Compose
Use the helper script that orchestrates the Docker Compose interactions:
./run.shThis will spin up the following Docker Compose services:
postgres- A Postgres instance w/ PostGIS
postgres-bootstrap- A one-shot container to restore some contrived SQL to
postgresfor testing
- A one-shot container to restore some contrived SQL to
hasura-bootstrap- A one-shot container to prepare the Hasura metadata and migrations volumes
hasura- A Hasura instance
hasura-postgres-schema-sync- The service this repo is all about
hasura-postgres-schema-sync-test- A container for integration testing
hasura-postgres-schema-sync
- A container for integration testing
If you like, you can then run the integration tests:
./test.shYou should see pytest output reflecting some passed tests.
Finally, navigate to http://localhost:8080, login with "Password1" and attempt a query; e.g.:
query ThingLocationStatByThing {
thing {
thing_location_stat_by_thing_uuid(limit: 10, order_by: {timestamp: desc}) {
location
timestamp
}
uuid
}
}- A Postgres database
- A Hasura instance
- Docker
We publish hasura-postgres-schema-sync to the FTP Solutions Docker Hub organisation, so that's
the easiest way to get started.
Use docker run to run a temporary container pointed at your Postgres database and Hasura instance:
docker run --rm -it \
-e POSTGRES_SCHEMA=public \
-e POSTGRES_HOST=your-postgres-database.org.au \
-e POSTGRES_PORT=5432 \
-e POSTGRES_USER=postgres \
-e POSTGRES_PASSWORD=Password1 \
-e POSTGRES_DB=some_database \
-e HASURA_HOST=your-hasura-instance.org.au \
-e HASURA_PORT=8080 \
-e HASURA_USER=admin \
-e HASURA_PASSWORD=Password1 \
ftpsolutions/hasura-postgres-schema-sync:mainThis will leave you with an attached container, attempting to sync your Postgres database with your Hasura instance on a 10 second schedule.