From 9e0ad449b090939ef695e74b19e90bcebf721d4b Mon Sep 17 00:00:00 2001 From: Raminder Singh Date: Tue, 30 Jul 2024 14:56:00 +0530 Subject: [PATCH] run tests in CI --- .github/workflows/general.yml | 24 ++++++++++++- api/scripts/init_db.sh | 63 +++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+), 1 deletion(-) create mode 100755 api/scripts/init_db.sh diff --git a/.github/workflows/general.yml b/.github/workflows/general.yml index 54f3838..fc14b7f 100644 --- a/.github/workflows/general.yml +++ b/.github/workflows/general.yml @@ -8,12 +8,34 @@ env: jobs: test: - name: Test + name: Api Tests runs-on: ubuntu-latest + services: + postgres: + image: postgres:15 + env: + POSTGRES_USER: postgres + POSTGRES_PASSWORD: postgres + POSTGRES_DB: postgres + ports: + - 5432:5432 steps: - uses: actions/checkout@v3 - uses: dtolnay/rust-toolchain@stable - uses: Swatinem/rust-cache@v2 + with: + key: sqlx-${{ env.SQLX_VERSION }} + - name: Install sqlx-cli + run: cargo install sqlx-cli + --version=${{ env.SQLX_VERSION }} + --features ${{ env.SQLX_FEATURES }} + --no-default-features + --locked + - name: Migrate database + run: | + sudo apt-get install libpq-dev -y + cd api + SKIP_DOCKER=true ./scripts/init_db.sh - name: Run tests run: cargo test diff --git a/api/scripts/init_db.sh b/api/scripts/init_db.sh new file mode 100755 index 0000000..30f91f1 --- /dev/null +++ b/api/scripts/init_db.sh @@ -0,0 +1,63 @@ +#!/usr/bin/env bash +set -x +set -eo pipefail + +if ! [ -x "$(command -v psql)" ]; then + echo >&2 "Error: psql is not installed." + exit 1 +fi + +if ! [ -x "$(command -v sqlx)" ]; then + echo >&2 "Error: sqlx is not installed." + echo >&2 "Use:" + echo >&2 " cargo install --version='~0.7' sqlx-cli --no-default-features --features rustls,postgres" + echo >&2 "to install it." + exit 1 +fi + +# Check if a custom user has been set, otherwise default to 'postgres' +DB_USER="${POSTGRES_USER:=postgres}" +# Check if a custom password has been set, otherwise default to 'postgres' +DB_PASSWORD="${POSTGRES_PASSWORD:=postgres}" +# Check if a custom database name has been set, otherwise default to 'postgres' +DB_NAME="${POSTGRES_DB:=postgres}" +# Check if a custom port has been set, otherwise default to '5432' +DB_PORT="${POSTGRES_PORT:=5432}" +# Check if a custom host has been set, otherwise default to 'localhost' +DB_HOST="${POSTGRES_HOST:=localhost}" + +# Allow to skip Docker if a dockerized Postgres database is already running +if [[ -z "${SKIP_DOCKER}" ]] +then + # if a postgres container is running, print instructions to kill it and exit + RUNNING_POSTGRES_CONTAINER=$(docker ps --filter 'name=postgres' --format '{{.ID}}') + if [[ -n $RUNNING_POSTGRES_CONTAINER ]]; then + echo >&2 "there is a postgres container already running, kill it with" + echo >&2 " docker kill ${RUNNING_POSTGRES_CONTAINER}" + exit 1 + fi + # Launch postgres using Docker + docker run \ + -e POSTGRES_USER=${DB_USER} \ + -e POSTGRES_PASSWORD=${DB_PASSWORD} \ + -e POSTGRES_DB=${DB_NAME} \ + -p "${DB_PORT}":5432 \ + -d \ + --name "postgres_$(date '+%s')" \ + postgres -N 1000 + # ^ Increased maximum number of connections for testing purposes +fi + +# Keep pinging Postgres until it's ready to accept commands +until PGPASSWORD="${DB_PASSWORD}" psql -h "${DB_HOST}" -U "${DB_USER}" -p "${DB_PORT}" -d "postgres" -c '\q'; do + >&2 echo "Postgres is still unavailable - sleeping" + sleep 1 +done + +>&2 echo "Postgres is up and running on port ${DB_PORT} - running migrations now!" + +export DATABASE_URL=postgres://${DB_USER}:${DB_PASSWORD}@${DB_HOST}:${DB_PORT}/${DB_NAME} +sqlx database create +sqlx migrate run + +>&2 echo "Postgres has been migrated, ready to go!"