apply metadata also in cli migrations image (close #1316) (#1319)

This commit is contained in:
Shahidh K Muhammed 2019-01-08 14:35:03 +05:30 committed by GitHub
parent a2b40de8a0
commit feaeccba71
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 54 additions and 24 deletions

View File

@ -1,5 +1,5 @@
Auto-apply migrations when server starts
========================================
Auto-apply migrations or metadata when server starts
====================================================
.. contents:: Table of contents
:backlinks: none
@ -7,23 +7,26 @@ Auto-apply migrations when server starts
:local:
Hasura ships a special docker container which can be used to
automatically apply migrations when the server starts:
automatically apply migrations/metadata when the server starts:
.. code-block:: bash
hasura/graphql-engine:<version>.cli-migrations
.. note::
This container image includes Hasura CLI at ``/bin/hasura-cli`` and can be
used for running any other CI/CD scripts in your workflow.
Applying migrations
-------------------
The ``migrations`` directory created by Hasura CLI (the one next to
``config.yaml``) can be mounted at ``/hasura-migrations`` path of this docker
container and the container's entrypoint script will apply the migrations before
starting the server. If no directory is mounted at the designated path, server
will start ignoring migrations.
.. note::
This container image includes Hasura CLI at ``/bin/hasura-cli`` and can be
used for running any other CI/CD scripts in your workflow.
If you want to mount the migrations directory at some location other than
``/hasura-migrations``, set the following environment variable:
@ -43,3 +46,11 @@ Example:
-v /home/me/my-project/migrations:/hasura-migrations \
-e HASURA_GRAPHQL_DATABASE_URL=postgres://postgres:@postgres:5432/postgres \
hasura/graphql-engine:v1.0.0-alpha27.cli-migrations
Applying only metadata
----------------------
If you're managing migrations with a different tool and want to use this image to apply only the
metadata, mount a directory with just a ``metadata.yaml`` file and the script will
apply the meatadata.

View File

@ -34,7 +34,7 @@ Follow the guide that best fits your scenario:
Advanced
--------
- :doc:`Auto-apply migrations when server starts <auto-apply-migrations>`
- :doc:`Auto-apply migrations or metadata when server starts <auto-apply-migrations>`
.. toctree::
:hidden:
@ -42,4 +42,4 @@ Advanced
For new project <new-project>
For existing project <existing-project>
With a database with an existing migration system <database-with-migrations>
Auto-apply migrations when server starts <auto-apply-migrations>
Auto-apply migrations or metadata when server starts <auto-apply-migrations>

View File

@ -1,6 +1,8 @@
FROM hasura/graphql-engine:v1.0.0-alpha34
ENV HASURA_GRAPHQL_ENABLE_CONSOLE=true
# set an env var to let the cli know that
# it is running in server environment
ENV HASURA_GRAPHQL_CLI_ENVIRONMENT=server-on-docker
COPY docker-entrypoint.sh /bin/
COPY cli-hasura-linux-amd64 /bin/hasura-cli

View File

@ -2,54 +2,71 @@
set -e
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\"}}"
}
DEFAULT_MIGRATIONS_DIR="/hasura-migrations"
TEMP_MIGRATIONS_DIR="/tmp/hasura-migrations"
# 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
# wait for a port to be ready
wait_for_port() {
local PORT=$1
echo "waiting for $PORT"
log "waiting 30s for $PORT to be ready"
for i in `seq 1 30`;
do
nc localhost $PORT > /dev/null 2>&1 && echo "port $PORT is ready" && return
echo -n .
nc localhost $PORT > /dev/null 2>&1 && log "port $PORT is ready" && return
sleep 1
done
echo "failed waiting for $PORT" && exit 1
log "failed waiting for $PORT" && exit 1
}
echo "starting graphql engine on port 8080..."
log "starting graphql engine temporarily on port $HASURA_GRAPHQL_SERVER_PORT"
# start graphql engine
graphql-engine serve &
# store the pid to kill it later
PID=$!
# wait for port 8080 to be ready
wait_for_port 8080
# wait for port to be ready
wait_for_port $HASURA_GRAPHQL_SERVER_PORT
# check if migration directory is set, default otherwise
log "checking for migrations directory"
if [ -z ${HASURA_GRAPHQL_MIGRATIONS_DIR+x} ]; then
echo "env var HASURA_GRAPHQL_MIGRATIONS_DIR is not set"
echo "defaulting to $DEFAULT_MIGRATIONS_DIR"
log "env var HASURA_GRAPHQL_MIGRATIONS_DIR is not set, defaulting to $DEFAULT_MIGRATIONS_DIR"
HASURA_GRAPHQL_MIGRATIONS_DIR="$DEFAULT_MIGRATIONS_DIR"
fi
# apply migrations if the directory exist
if [ -d "$HASURA_GRAPHQL_MIGRATIONS_DIR" ]; then
echo "applying migrations in $HASURA_GRAPHQL_MIGRATIONS_DIR..."
log "applying migrations from $HASURA_GRAPHQL_MIGRATIONS_DIR"
mkdir -p "$TEMP_MIGRATIONS_DIR"
cp -a "$HASURA_GRAPHQL_MIGRATIONS_DIR/." "$TEMP_MIGRATIONS_DIR/migrations/"
cd "$TEMP_MIGRATIONS_DIR"
echo "endpoint: http://localhost:8080" > config.yaml
echo "endpoint: http://localhost:$HASURA_GRAPHQL_SERVER_PORT" > config.yaml
hasura-cli migrate apply
# check if metadata.yaml exist and apply
if [ -f migrations/metadata.yaml ]; then
log "applying metadata from $HASURA_GRAPHQL_MIGRATIONS_DIR/metadata.yaml"
hasura-cli metadata apply
fi
else
echo "directory $HASURA_GRAPHQL_MIGRATIONS_DIR does not exist"
echo "skipping migration apply"
log "directory $HASURA_GRAPHQL_MIGRATIONS_DIR does not exist, skipping migrations"
fi
# kill graphql engine that we started earlier
log "killing temporary server"
kill $PID
# pass control to CMD
log "graphql-engine will now start in normal mode"
exec "$@"