]> Untitled Git - lemmy.git/commitdiff
Automatically manage database when running scripts/test.sh (#3389)
authordullbananas <dull.bananas0@gmail.com>
Fri, 30 Jun 2023 07:50:30 +0000 (00:50 -0700)
committerGitHub <noreply@github.com>
Fri, 30 Jun 2023 07:50:30 +0000 (09:50 +0200)
* Update .gitignore

* Create start-dev-db.sh

* Rename start-dev-db.sh to start_dev_db.sh

* Update .gitignore

* Update start_dev_db.sh

* Update start_dev_db.sh

* Update start_dev_db.sh

* Update start_dev_db.sh

* h

* Update test.sh

* Update start_dev_db.sh

* made it work

* Make test.sh work when run from scripts dir

.gitignore
scripts/start_dev_db.sh [new file with mode: 0644]
scripts/test.sh

index 0461784f02508109a75aa6601655a011688121f9..e482488366d43f794beb235d66ff59bc86a2e8bb 100644 (file)
@@ -26,3 +26,7 @@ pictrs/
 
 # The generated typescript bindings
 bindings
+
+# Database cluster and sockets for testing
+dev_pgdata/
+*.PGSQL.*
diff --git a/scripts/start_dev_db.sh b/scripts/start_dev_db.sh
new file mode 100644 (file)
index 0000000..f192def
--- /dev/null
@@ -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
index f117238bd3154857c6da5a9dbf0e49fe11fd873d..2a5efb30d10d5a825cc7ff8fa23e53107a39d744 100755 (executable)
@@ -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
@@ -21,3 +23,6 @@ else
 fi
 
 # Add this to do printlns: -- --nocapture
+
+pg_ctl stop
+rm -rf $PGDATA