Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatically manage database when running scripts/test.sh #3389

Merged
merged 16 commits into from
Jun 30, 2023
Merged
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,7 @@ pictrs/

# The generated typescript bindings
bindings

# Database cluster and sockets for testing
dev_pgdata/
*.PGSQL.*
24 changes: 24 additions & 0 deletions scripts/start_dev_db.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# This script is meant to be run with `source` so it can set environment variables.

export PGDATA="$PWD/dev_pgdata"
export PGHOST=$PWD
export LEMMY_DATABASE_URL="postgresql://lemmy:password@/lemmy?host=$PWD"

# If cluster exists, stop the server and delete the cluster
if [ -d $PGDATA ]
then
# Prevent `stop` from failing if server already stopped
pg_ctl restart > /dev/null
pg_ctl stop
rm -rf $PGDATA
fi

# Create cluster
initdb --username=postgres --auth=trust --no-instructions

# Start server that only listens to socket in current directory
pg_ctl start --options="-c listen_addresses= -c unix_socket_directories=$PWD" > /dev/null

# Setup database
psql -c "CREATE USER lemmy WITH PASSWORD 'password' SUPERUSER;" -U postgres
psql -c "CREATE DATABASE lemmy WITH OWNER lemmy;" -U postgres
11 changes: 8 additions & 3 deletions scripts/test.sh
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
#!/usr/bin/env bash
set -e

CWD="$(cd -P -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd -P)"

cd $CWD/../

PACKAGE="$1"
echo "$PACKAGE"

psql -U lemmy -d postgres -c "DROP DATABASE lemmy;"
psql -U lemmy -d postgres -c "CREATE DATABASE lemmy;"
source scripts/start_dev_db.sh

export LEMMY_DATABASE_URL=postgres://lemmy:password@localhost:5432/lemmy
# tests are executed in working directory crates/api (or similar),
# so to load the config we need to traverse to the repo root
export LEMMY_CONFIG_LOCATION=../../config/config.hjson
Expand All @@ -21,3 +23,6 @@ else
fi

# Add this to do printlns: -- --nocapture

pg_ctl stop
rm -rf $PGDATA