2018-11-02 17:08:25 +03:00
|
|
|
#!/bin/sh
|
|
|
|
|
|
|
|
set -e
|
|
|
|
|
2019-01-08 12:05:03 +03:00
|
|
|
log() {
|
|
|
|
TIMESTAMP=$(date -u "+%Y-%m-%dT%H:%M:%S.000+0000")
|
|
|
|
MESSAGE=$1
|
|
|
|
echo "{\"timestamp\":\"$TIMESTAMP\",\"level\":\"info\",\"type\":\"startup\",\"detail\":{\"kind\":\"migration-apply\",\"info\":\"$MESSAGE\"}}"
|
|
|
|
}
|
|
|
|
|
2018-11-02 17:08:25 +03:00
|
|
|
DEFAULT_MIGRATIONS_DIR="/hasura-migrations"
|
|
|
|
TEMP_MIGRATIONS_DIR="/tmp/hasura-migrations"
|
|
|
|
|
2019-12-27 07:30:06 +03:00
|
|
|
# configure the target database for migrations
|
|
|
|
if [ ${HASURA_GRAPHQL_MIGRATIONS_DATABASE_ENV_VAR} ]; then
|
|
|
|
log "database url for migrations is set by $HASURA_GRAPHQL_MIGRATIONS_DATABASE_ENV_VAR"
|
|
|
|
HASURA_GRAPHQL_MIGRATIONS_DATABASE_URL=$(printenv $HASURA_GRAPHQL_MIGRATIONS_DATABASE_ENV_VAR)
|
|
|
|
elif [ -z ${HASURA_GRAPHQL_MIGRATIONS_DATABASE_URL+x} ]; then
|
|
|
|
HASURA_GRAPHQL_MIGRATIONS_DATABASE_URL=$HASURA_GRAPHQL_DATABASE_URL
|
|
|
|
fi
|
|
|
|
log "database url for migrations is set by HASURA_GRAPHQL_DATABASE_URL"
|
|
|
|
|
2019-12-27 07:05:29 +03:00
|
|
|
# Use 9691 port for running temporary instance.
|
|
|
|
# In case 9691 is occupied (according to docker networking), then this will fail.
|
2019-12-27 07:30:06 +03:00
|
|
|
# override with another port in that case
|
|
|
|
# TODO: Find a proper random port
|
|
|
|
if [ -z ${HASURA_GRAPHQL_MIGRATIONS_SERVER_PORT+x} ]; then
|
|
|
|
log "migrations server port env var is not set, defaulting to 9691"
|
|
|
|
HASURA_GRAPHQL_MIGRATIONS_SERVER_PORT=9691
|
|
|
|
fi
|
2019-12-27 07:05:29 +03:00
|
|
|
|
2019-05-31 13:37:59 +03:00
|
|
|
if [ -z ${HASURA_GRAPHQL_MIGRATIONS_SERVER_TIMEOUT+x} ]; then
|
2019-12-27 07:30:06 +03:00
|
|
|
log "server timeout is not set, defaulting to 30 seconds"
|
2019-05-31 13:37:59 +03:00
|
|
|
HASURA_GRAPHQL_MIGRATIONS_SERVER_TIMEOUT=30
|
|
|
|
fi
|
2019-01-08 12:05:03 +03:00
|
|
|
|
2018-11-02 17:08:25 +03:00
|
|
|
# wait for a port to be ready
|
|
|
|
wait_for_port() {
|
|
|
|
local PORT=$1
|
2019-05-31 13:37:59 +03:00
|
|
|
log "waiting $HASURA_GRAPHQL_MIGRATIONS_SERVER_TIMEOUT for $PORT to be ready"
|
|
|
|
for i in `seq 1 $HASURA_GRAPHQL_MIGRATIONS_SERVER_TIMEOUT`;
|
2018-11-02 17:08:25 +03:00
|
|
|
do
|
2019-07-31 08:18:03 +03:00
|
|
|
nc -z localhost $PORT > /dev/null 2>&1 && log "port $PORT is ready" && return
|
2018-11-02 17:08:25 +03:00
|
|
|
sleep 1
|
|
|
|
done
|
2019-01-08 12:05:03 +03:00
|
|
|
log "failed waiting for $PORT" && exit 1
|
2018-11-02 17:08:25 +03:00
|
|
|
}
|
|
|
|
|
2019-12-27 07:05:29 +03:00
|
|
|
log "starting graphql engine temporarily on port $HASURA_GRAPHQL_MIGRATIONS_SERVER_PORT"
|
2018-11-02 17:08:25 +03:00
|
|
|
|
2019-10-17 12:58:27 +03:00
|
|
|
# start graphql engine with metadata api enabled
|
2019-12-27 07:30:06 +03:00
|
|
|
graphql-engine --database-url "$HASURA_GRAPHQL_MIGRATIONS_DATABASE_URL" \
|
|
|
|
serve --enabled-apis="metadata" \
|
|
|
|
--server-port=${HASURA_GRAPHQL_MIGRATIONS_SERVER_PORT} &
|
2018-11-02 17:08:25 +03:00
|
|
|
# store the pid to kill it later
|
|
|
|
PID=$!
|
|
|
|
|
2019-01-08 12:05:03 +03:00
|
|
|
# wait for port to be ready
|
2019-12-27 07:05:29 +03:00
|
|
|
wait_for_port $HASURA_GRAPHQL_MIGRATIONS_SERVER_PORT
|
2018-11-02 17:08:25 +03:00
|
|
|
|
|
|
|
# check if migration directory is set, default otherwise
|
2019-01-08 12:05:03 +03:00
|
|
|
log "checking for migrations directory"
|
2018-11-02 17:08:25 +03:00
|
|
|
if [ -z ${HASURA_GRAPHQL_MIGRATIONS_DIR+x} ]; then
|
2019-01-08 12:05:03 +03:00
|
|
|
log "env var HASURA_GRAPHQL_MIGRATIONS_DIR is not set, defaulting to $DEFAULT_MIGRATIONS_DIR"
|
2018-11-02 17:08:25 +03:00
|
|
|
HASURA_GRAPHQL_MIGRATIONS_DIR="$DEFAULT_MIGRATIONS_DIR"
|
|
|
|
fi
|
|
|
|
|
|
|
|
# apply migrations if the directory exist
|
|
|
|
if [ -d "$HASURA_GRAPHQL_MIGRATIONS_DIR" ]; then
|
2019-01-08 12:05:03 +03:00
|
|
|
log "applying migrations from $HASURA_GRAPHQL_MIGRATIONS_DIR"
|
2018-11-02 17:08:25 +03:00
|
|
|
mkdir -p "$TEMP_MIGRATIONS_DIR"
|
|
|
|
cp -a "$HASURA_GRAPHQL_MIGRATIONS_DIR/." "$TEMP_MIGRATIONS_DIR/migrations/"
|
|
|
|
cd "$TEMP_MIGRATIONS_DIR"
|
2019-12-27 07:05:29 +03:00
|
|
|
echo "endpoint: http://localhost:$HASURA_GRAPHQL_MIGRATIONS_SERVER_PORT" > config.yaml
|
2020-03-05 09:01:22 +03:00
|
|
|
# set HASURA_GRAPHQL_SHOW_UPDATE_NOTIFICATION to disable update notification
|
|
|
|
export HASURA_GRAPHQL_SHOW_UPDATE_NOTIFICATION=false
|
2018-11-02 17:08:25 +03:00
|
|
|
hasura-cli migrate apply
|
2019-04-11 06:41:44 +03:00
|
|
|
# check if metadata.[yaml|json] exist and apply
|
2019-01-08 12:05:03 +03:00
|
|
|
if [ -f migrations/metadata.yaml ]; then
|
|
|
|
log "applying metadata from $HASURA_GRAPHQL_MIGRATIONS_DIR/metadata.yaml"
|
|
|
|
hasura-cli metadata apply
|
2019-04-11 06:41:44 +03:00
|
|
|
elif [ -f migrations/metadata.json ]; then
|
|
|
|
log "applying metadata from $HASURA_GRAPHQL_MIGRATIONS_DIR/metadata.json"
|
|
|
|
hasura-cli metadata apply
|
2019-01-08 12:05:03 +03:00
|
|
|
fi
|
2018-11-02 17:08:25 +03:00
|
|
|
else
|
2019-01-08 12:05:03 +03:00
|
|
|
log "directory $HASURA_GRAPHQL_MIGRATIONS_DIR does not exist, skipping migrations"
|
2018-11-02 17:08:25 +03:00
|
|
|
fi
|
|
|
|
|
|
|
|
# kill graphql engine that we started earlier
|
2019-01-08 12:05:03 +03:00
|
|
|
log "killing temporary server"
|
2018-11-02 17:08:25 +03:00
|
|
|
kill $PID
|
|
|
|
|
|
|
|
# pass control to CMD
|
2019-01-08 12:05:03 +03:00
|
|
|
log "graphql-engine will now start in normal mode"
|
2018-11-02 17:08:25 +03:00
|
|
|
exec "$@"
|