hasura / hasura/graphql-engine

Invalid graphql identifier in unique constraint name in tracked table prevents server from starting

Open
#7,850 3 comments 1 reaction 0 assignees View on GitHub
c/server k/bug p/high t/native-dbs
Dominant language
TypeScript
Stars
32.1k
Forks
3k
PR merge metrics
PR metrics pending

Description

### Version Information

Server Version: 2.0.10

### Environment

All

### What is the expected behaviour?

Starting in 2.0, inconsistent metadata should no longer prevent server start. Instead, hasura will still start with reduced functionality depending on the metadata issue, allowing the user to use the console to diagnose and possibly solve the problem.

This can be seen when for example a table has been dropped since last server start, causing inconsistent metadata.
The user will still be able to start the server, and be shown a list of inconsistent metadata object, with the ability to delete such metadata objects to solve the problem.

This bug report details an issue that _should_ be a metadata problem only, but instead prevents server start altogether.
This will soft lock the user out of their hasura instance until they access their database to fix the problem. This is especially concerning on hasura cloud.

### What is the current behaviour?

Server start fails when the following conditions are met:
1. At table is tracked and thus included in hasura metadata
2. This table has a unique constraint with a custom name that is not a [valid graphql identifier](https://spec.graphql.org/June2018/#sec-Names)

The issue won't present itself if the table is not tracked, and the user won't be able to track such a table.
Therefore the constraint must be added after the table has already been tracked, or the metadata must be written manually.

This can happen when using third party tools to manage database schema.

### How to reproduce the issue?

1. create a table by running the following SQL, or the equivalent in the console UI:
```sql
CREATE TABLE test (id INT);
```
2. Track the table (not required if you used the console UI for step 1)
3. Run the following SQL (warning: this will softlock you out of your instance once the server restarts)
```sql
-- note "invalid identifier" is an invalid graphql identifier because it contains a space
ALTER TABLE test ADD CONSTRAINT "invalid identifier" UNIQUE (id)
```
4. If you run this in the console and attempt to track the change you will get a warning, but things will otherwise keep working
5. Restart the instance. Startup will fail.
6. To restore access to the instance use a third party tool to run the following SQL and restart the server
```sql
ALTER TABLE test DROP CONSTRAINT "invalid identifier"
```

### Screenshots or Screencast

The console ui tries really hard to prevent you from doing this. Here is the tooltip you get when attempting to track a table with an invalid identifier name, things work as expected:
![image](https://user-images.githubusercontent.com/25712958/143460905-d25bfed1-1833-49e5-93f4-704504e3921e.png)

And here is the error when you try to track a table that has a unique constraint with an invalid identifier name:
![image](https://user-images.githubusercontent.com/25712958/143461014-66a8aebe-1f94-488c-94d6-69a5498a3d12.png)

### Keywords

Invalid graphql identifier

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

The failed startup generates a single relevant log line (note "invalid identifier" is literally the name of the constrain in this case):
```json
{
"path": "$",
"error": "cannot include \"invalid identifier\" in the GraphQL schema because it is not a valid GraphQL identifier",
"code": "validation-failed"
}
```

### Any possible solutions?

Currently it is possible to work around this by connecting to the database directly using a third party tool, and either dropping or renaming the offending constraints before attempting to restart the hasura server.
If using hasura cloud, you can trigger a restart by modifying environment variables.

Contributor guide

Open the contributing guide

Research direction

Reproduce the failure by tracking a table, adding the quoted unique constraint "invalid identifier" with SQL, and restarting the server. Start at server startup metadata validation and follow how the constraint enters schema generation; done means the server starts with reduced functionality and exposes the metadata problem for diagnosis instead of failing startup.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.