Skip to content

Commit

Permalink
update the cli-migrations docker images so that their entrypoints all…
Browse files Browse the repository at this point in the history
…ow use of the --disallow-inconsistent-metadata flag when applying metadata (fixes hasura#10599)
  • Loading branch information
chardo committed Nov 15, 2024
1 parent bf61f3e commit 7388f1c
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 2 deletions.
7 changes: 7 additions & 0 deletions docs/docs/migrations-metadata-seeds/auto-apply-migrations.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,10 @@ If you're managing Migrations with a different tool and want to use this image t
`metadata` directory of your Hasura Project at the `/hasura-metadata`
[path of this Docker container the container's entry point script](https://github.com/hasura/graphql-engine/blob/master/packaging/cli-migrations/v3/docker-entrypoint.sh#L13)
will apply the Metadata before starting the server.

## Disallowing Inconsistent Metadata

If you set the `HASURA_GRAPHQL_DISALLOW_INCONSISTENT_METADATA` environment variable to `true`, the entrypoint script will
reject inconsistent metadata and exit with a non-zero exit code. This has the same behavior as the
`--disallow-inconsistent-metadata` flag in the `hasura metadata apply` command documented
[here](/hasura-cli/commands/hasura_metadata_apply.mdx).
8 changes: 8 additions & 0 deletions packaging/cli-migrations/v2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,11 @@ At least one of these **must** be configured to migrate the database successfull
- `HASURA_GRAPHQL_MIGRATIONS_SERVER_TIMEOUT` (default=`30s`)

Specify the server timeout threshold.

### Disallow Inconsistent Metadata (Optional)

- `HASURA_GRAPHQL_DISALLOW_INCONSISTENT_METADATA` (default=`false`)

If set to true, inconsistent metadata will be rejected, causing the entrypoint script to exit with a non-zero exit code.
Has the same behavior as the `--disallow-inconsistent-metadata` flag in the `hasura metadata apply` command documented
[here](https://hasura.io/docs/latest/hasura-cli/commands/hasura_metadata_apply).
12 changes: 11 additions & 1 deletion packaging/cli-migrations/v2/docker-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ log() {

DEFAULT_MIGRATIONS_DIR="/hasura-migrations"
DEFAULT_METADATA_DIR="/hasura-metadata"
DEFAULT_DISALLOW_INCONSISTENT_METADATA_FLAG=""
TEMP_PROJECT_DIR="/tmp/hasura-project"

# configure the target database for migrations
Expand Down Expand Up @@ -72,6 +73,15 @@ if [ -z ${HASURA_GRAPHQL_METADATA_DIR+x} ]; then
HASURA_GRAPHQL_METADATA_DIR="$DEFAULT_METADATA_DIR"
fi

# check if disallow metadata inconsistency is set to true (case insensitive), default otherwise
if [ "$(echo "$HASURA_GRAPHQL_DISALLOW_INCONSISTENT_METADATA" | tr '[:upper:]' '[:lower:]')" = "true" ]; then
log "migrations-startup" "env var HASURA_GRAPHQL_DISALLOW_INCONSISTENT_METADATA is set to true, disallowing inconsistent metadata"
HASURA_GRAPHQL_DISALLOW_INCONSISTENT_METADATA="--disallow-inconsistent-metadata"
else
log "migrations-startup" "env var HASURA_GRAPHQL_DISALLOW_INCONSISTENT_METADATA is not true, falling back to default CLI behavior"
HASURA_GRAPHQL_DISALLOW_INCONSISTENT_METADATA="$DEFAULT_DISALLOW_INCONSISTENT_METADATA_FLAG"
fi

# apply migrations if the directory exist
if [ -d "$HASURA_GRAPHQL_MIGRATIONS_DIR" ]; then
log "migrations-apply" "applying migrations from $HASURA_GRAPHQL_MIGRATIONS_DIR"
Expand All @@ -95,7 +105,7 @@ if [ -d "$HASURA_GRAPHQL_METADATA_DIR" ]; then
echo "version: 2" > config.yaml
echo "endpoint: http://localhost:$HASURA_GRAPHQL_MIGRATIONS_SERVER_PORT" >> config.yaml
echo "metadata_directory: metadata" >> config.yaml
hasura-cli metadata apply
hasura-cli metadata apply $HASURA_GRAPHQL_DISALLOW_INCONSISTENT_METADATA
else
log "migrations-apply" "directory $HASURA_GRAPHQL_METADATA_DIR does not exist, skipping metadata"
fi
Expand Down
8 changes: 8 additions & 0 deletions packaging/cli-migrations/v3/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,11 @@ Optional configuration for the server which boots during migrations.
- `HASURA_GRAPHQL_MIGRATIONS_SERVER_TIMEOUT` (default=`30s`)

Specify the server timeout threshold.

### Disallow Inconsistent Metadata (Optional)

- `HASURA_GRAPHQL_DISALLOW_INCONSISTENT_METADATA` (default=`false`)

If set to true, inconsistent metadata will be rejected, causing the entrypoint script to exit with a non-zero exit code.
Has the same behavior as the `--disallow-inconsistent-metadata` flag in the `hasura metadata apply` command documented
[here](https://hasura.io/docs/latest/hasura-cli/commands/hasura_metadata_apply).
12 changes: 11 additions & 1 deletion packaging/cli-migrations/v3/docker-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ log() {

DEFAULT_MIGRATIONS_DIR="/hasura-migrations"
DEFAULT_METADATA_DIR="/hasura-metadata"
DEFAULT_DISALLOW_INCONSISTENT_METADATA_FLAG=""
TEMP_PROJECT_DIR="/tmp/hasura-project"

if [ -z ${HGE_BINARY+x} ]; then
Expand Down Expand Up @@ -67,6 +68,15 @@ if [ -z ${HASURA_GRAPHQL_METADATA_DIR+x} ]; then
HASURA_GRAPHQL_METADATA_DIR="$DEFAULT_METADATA_DIR"
fi

# check if disallow metadata inconsistency is set to true (case insensitive), default otherwise
if [ "$(echo "$HASURA_GRAPHQL_DISALLOW_INCONSISTENT_METADATA" | tr '[:upper:]' '[:lower:]')" = "true" ]; then
log "migrations-startup" "env var HASURA_GRAPHQL_DISALLOW_INCONSISTENT_METADATA is set to true, disallowing inconsistent metadata"
HASURA_GRAPHQL_DISALLOW_INCONSISTENT_METADATA="--disallow-inconsistent-metadata"
else
log "migrations-startup" "env var HASURA_GRAPHQL_DISALLOW_INCONSISTENT_METADATA is not true, falling back to default CLI behavior"
HASURA_GRAPHQL_DISALLOW_INCONSISTENT_METADATA="$DEFAULT_DISALLOW_INCONSISTENT_METADATA_FLAG"
fi

# apply metadata if the directory exist
if [ -d "$HASURA_GRAPHQL_METADATA_DIR" ]; then
rm -rf "$TEMP_PROJECT_DIR"
Expand All @@ -77,7 +87,7 @@ if [ -d "$HASURA_GRAPHQL_METADATA_DIR" ]; then
echo "version: 3" > config.yaml
echo "endpoint: http://localhost:$HASURA_GRAPHQL_MIGRATIONS_SERVER_PORT" >> config.yaml
echo "metadata_directory: metadata" >> config.yaml
hasura-cli metadata apply
hasura-cli metadata apply $HASURA_GRAPHQL_DISALLOW_INCONSISTENT_METADATA
else
log "migrations-apply" "directory $HASURA_GRAPHQL_METADATA_DIR does not exist, skipping metadata"
fi
Expand Down

0 comments on commit 7388f1c

Please sign in to comment.