#!/bin/bash


if [ -n "$CLICKHOUSE_URL_DIRECT" ]; then
  export GOOSE_DBSTRING=$CLICKHOUSE_URL_DIRECT
elif [ -z "$CLICKHOUSE_URL" ]; then
  echo "Neither CLICKHOUSE_URL_DIRECT nor CLICKHOUSE_URL is set"
  exit 1
else
  export GOOSE_DBSTRING=$CLICKHOUSE_URL
fi

echo "Clickhouse migration script"
echo ""
echo "================="
echo "Selected database: $GOOSE_DBSTRING"
echo "================="
echo ""
if [ "$1" != "create" ] && [ -z "$CI" ]; then
  read -p "Are you sure you want to run migrations on this database? (y/n) " -n 1 -r
  echo
  if [[ ! $REPLY =~ ^[Yy]$ ]]; then
    echo "Migration cancelled."
    exit 0
  fi
fi

goose clickhouse --dir ./migrations $@