hasura / hasura/graphql-engine

Hasura possibly needs to be smarter about creating the pgcrypto extension

Open
#8,128 1 comment 0 reactions 0 assignees View on GitHub
k/bug
Dominant language
TypeScript
Stars
32.1k
Forks
3k
PR merge metrics
PR metrics pending

Description

### Version Information

Server Version: 2.1.1
CLI Version (for CLI related issue):

### Environment

OSS

### What is the expected behaviour?

Migrations should issue `CREATE EXTENSION IF NOT EXISTS pgcrypto` _before_ DDL that uses the `gen_random_uuid()` function, not _after_.

### Keywords

pgcrypto
gen_random_uuid

### What is the current behaviour?

In v2.1.1 at least, Hasura creates migrations in `up.sql` that issue DDL that reference the `gen_random_uuid()` function _first_ and then issue the `CREATE EXTENSION IF NOT EXISTS pgcrypto` statement _second_. This will fail in PostgreSQL versions 12 and before, because the `pgcrypto` extension supplies the `gen_random_uuid` function. If this is a bug, it's subtle however, because it will silently not fail in versions of PostgreSQL 13 and after, since `gen_random_uuid` was moved into PostgreSQL core and it's no longer necessary to add the `pgcrypto` extension to get that function.

### How to reproduce the issue?

1. Iterate on a local Hasura instance to generate migrations.
2. Deploy those migrations to another Hasura instance in the cloud in Hasura HQ
3. Make sure that cloud Hasura instance is connected to a version of PostgreSQL v12 or earlier.
4. Note that the migrations fail leaving the metadata in an inconsistent state.
5. Repeat steps 2 & 3 but for a cloud Hasura instance connected to a version of PostgreSQL v 13 or later.
6. Note that the migrations succeed

### Screenshots or Screencast

https://youtu.be/CYEYbplbyv8

### Please provide any traces or logs that could help here.

### Any possible solutions?

There are at least two possible solutions:

* Install `pgcrypto` during DB init, like is promised [in the docs](https://hasura.io/docs/latest/graphql/core/deployment/postgres-requirements.html#pgcrypto-in-pg-search-path)
* Alternatively, ensure that `CREATE EXTENSION IF NOT EXISTS pgcrypto` is always added to migration files first

### Can you identify the location in the source code where the problem exists?

I'm afraid I don't know the source code or even Haskell at all.

### If the bug is confirmed, would you be willing to submit a PR?

I suppose I could try.

Contributor guide

Open the contributing guide

Research direction

Start by reproducing migration generation against PostgreSQL 12 or earlier, where gen_random_uuid() requires pgcrypto, and compare it with PostgreSQL 13 or later. Trace the migration-generation entry point that orders the DDL and extension statement. Done means generated up.sql creates pgcrypto before any DDL using gen_random_uuid(), while migrations still succeed on both PostgreSQL version ranges.

Written by the indexing model from the issue text.

Assessment

Tech stack
postgresql
Domain
databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.