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-01-08 12:05:03 +03:00
|
|
|
# check server port and ser default as 8080
|
|
|
|
if [ -z ${HASURA_GRAPHQL_SERVER_PORT+x} ]; then
|
|
|
|
log "port env var is not set, defaulting to 8080"
|
|
|
|
HASURA_GRAPHQL_SERVER_PORT=8080
|
|
|
|
fi
|
|
|
|
|
2018-11-02 17:08:25 +03:00
|
|
|
# wait for a port to be ready
|
|
|
|
wait_for_port() {
|
|
|
|
local PORT=$1
|
2019-01-08 12:05:03 +03:00
|
|
|
log "waiting 30s for $PORT to be ready"
|
2018-11-02 17:08:25 +03:00
|
|
|
for i in `seq 1 30`;
|
|
|
|
do
|
2019-01-08 12:05:03 +03:00
|
|
|
nc 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-01-08 12:05:03 +03:00
|
|
|
log "starting graphql engine temporarily on port $HASURA_GRAPHQL_SERVER_PORT"
|
2018-11-02 17:08:25 +03:00
|
|
|
|
|
|
|
# start graphql engine
|
|
|
|
graphql-engine serve &
|
|
|
|
# store the pid to kill it later
|
|
|
|
PID=$!
|
|
|
|
|
2019-01-08 12:05:03 +03:00
|
|
|
# wait for port to be ready
|
|
|
|
wait_for_port $HASURA_GRAPHQL_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-01-08 12:05:03 +03:00
|
|
|
echo "endpoint: http://localhost:$HASURA_GRAPHQL_SERVER_PORT" > config.yaml
|
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 "$@"
|