Skip to content

Instantly share code, notes, and snippets.

@nineinchnick
Last active January 25, 2024 12:19
Show Gist options
  • Save nineinchnick/d3496c05939856c1b0858af99457d85a to your computer and use it in GitHub Desktop.
Save nineinchnick/d3496c05939856c1b0858af99457d85a to your computer and use it in GitHub Desktop.
Test Flyway with Trino
#!/bin/bash
set -euo pipefail
function ensure_container() {
local name=$1
shift
local image=$1
shift
echo
if [ "$(docker ps -q -f name="$name")" ]; then
echo "Container $name already running"
return 0
fi
if [ "$(docker ps -aq -f status=exited -f name="$name")" ]; then
echo "Cleaning up stale $name container..."
docker rm "$name"
fi
echo "Creating $name container from image '$image'..."
docker run --name "$name" -d "$@" "$image"
}
# define defaults
FLYWAY_VERSION=$("$PWD/mvnw" --quiet help:evaluate -Dexpression=project.version -DforceStdout)
ARTIFACT="$PWD/flyway-community-db-support/flyway-database-trino/target/flyway-database-trino-$FLYWAY_VERSION.jar"
TRINO_VERSION=437
TRINO_IMAGE=trinodb/trino:$TRINO_VERSION
CONTAINER_NAME=trino
CMD=${1:-migrate}
shift || true
# check prerequisites
if [ ! -f "$ARTIFACT" ]; then
echo "Build the flyway-community-db-support module using: ./mvnw package -pl :flyway-core,:flyway-community-db-support,:flyway-database-trino"
exit 2
fi
# prepare environment
if [ ! -f catalog/postgres.properties ]; then
mkdir -p catalog
cat <<EOF >catalog/postgres.properties
connector.name=postgresql
connection-url=jdbc:postgresql://postgres:5432/postgres
connection-user=postgres
connection-password=pw
insert.non-transactional-insert.enabled=true
postgresql.array-mapping=AS_ARRAY
EOF
fi
ensure_container postgres postgres:16 -e POSTGRES_PASSWORD=pw
ensure_container $CONTAINER_NAME $TRINO_IMAGE \
--link postgres \
-p 8080:8080 \
-v "$PWD"/catalog:/etc/trino/catalog
echo >&2 "Waiting for Trino to be ready"
until docker inspect "$CONTAINER_NAME" --format "{{json .State.Health.Status }}" | grep -q '"healthy"'; do sleep 1; done
if [ ! -f "$PWD"/trino-jdbc-$TRINO_VERSION.jar ]; then
curl -fLO https://repo1.maven.org/maven2/io/trino/trino-jdbc/$TRINO_VERSION/trino-jdbc-$TRINO_VERSION.jar
fi
if [ ! -f sql/V1__initial.sql ]; then
mkdir -p sql
cat <<EOF >sql/V1__initial.sql
CREATE TABLE car (
id INT NOT NULL,
license_plate VARCHAR NOT NULL,
color VARCHAR NOT NULL
);
CREATE TABLE owner (
id INT NOT NULL,
car_id INT NOT NULL,
name VARCHAR NOT NULL
);
CREATE TABLE brand (
id INT NOT NULL,
name VARCHAR NOT NULL
);
ALTER TABLE owner ADD COLUMN driver_license_id VARCHAR;
INSERT INTO brand (id, name) VALUES (1, 'DeLorean');
EOF
fi
echo
echo "Starting migration of database with flyway"
# enable tracing in case user would like to run a different flyway command
set -x
docker run --rm \
-v "$PWD"/sql:/flyway/sql \
-v "$ARTIFACT":/flyway/lib/flyway-database-trino.jar \
-v "$PWD"/trino-jdbc-$TRINO_VERSION.jar:/flyway/drivers/trino.jar \
--link $CONTAINER_NAME \
flyway/flyway "$CMD" -url="jdbc:trino://trino:8080/postgres/public" -user=admin "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment