#!/usr/bin/env bash CLASS_NAME="MJB::DB" if [ -z $CLASS_NAME ]; then echo "Please call this script as $0 Your::Schema" exit 1 fi # Generate a random 8 character name for the docker container that holds the PSQL database. PSQL_NAME=$(LC_ALL=C tr -dc 'a-zA-Z' < /dev/urandom | fold -w 8 | head -n 1) if [ -z "$PSQL_NAME" ]; then echo 'Command failed: PSQL_NAME=$(LC_ALL=C tr -dc 'a-zA-Z' < /dev/urandom | fold -w 8 | head -n 1)' exit 1 fi # Launch a PSQL Instance PSQL_DOCKER=$(docker run --rm --name "$PSQL_NAME" -e POSTGRES_PASSWORD=dbic -e POSTGRES_USER=dbic -e POSTGRES_DB=dbic -d \ --mount "type=bind,src=$PWD/etc/schema.sql,dst=/docker-entrypoint-initdb.d/schema.sql" postgres:13) if [ -z "$PSQL_DOCKER" ]; then echo "Failed to get id for PSQL docker container." exit 1 fi echo "Loading DB schema...." # Give a few seconds for the database to come up, then if it's existed there is likely an issue # with the schema. sleep 5; PSQL_DOCKER_COUNT=$(docker ps --filter id=$PSQL_DOCKER | wc -l) if [ $PSQL_DOCKER_COUNT -ne 2 ]; then echo 'Failed to load schema, view error with the following:' echo 'docker run --rm -e POSTGRES_PASSWORD=password --mount "type=bind,src=$PWD/etc/schema.sql,dst=/docker-entrypoint-initdb.d/schema.sql" postgres:13' exit -1; fi docker run --rm --link "$PSQL_NAME:psqldb" --mount "type=bind,src=$PWD,dst=/app" symkat/schema_builder:3 /bin/build-schema "$CLASS_NAME" docker kill "$PSQL_DOCKER" if [ -d 'lib' ]; then sudo chown -R "$USER:$USER" lib else echo 'Failed to find lib directory. This should have been created automatically.' fi