Big update to deploy scripts, and deploy prosecss

This commit is contained in:
Max committed 2026-10-28 12:17:09 -07:00
1 parent 98261207d6
commit 42287395fe
1 file changed
+56 -4
+56 -4
View File
@@ -21,6 +21,36 @@ pkgs.mkShell {
MYSQL_SOCKET="$PWD/.mysql/mysql.sock"
MYSQL_PIDFILE="$PWD/.mysql/mysql.pid"
#
# Work out what database situation we are in BEFORE touching anything.
# This shell is used on the production server too, where a system MariaDB
# is already running -- we must not initialise a datadir, start a second
# server on port 3306, or inject dev credentials over the real ones.
#
# ours - the project-local server is already up (another nix-shell)
# existing - some other server is listening; not ours to manage
# start - nothing is running; this is a dev box, bring one up
#
SS_DB_MODE="start"
if mysqladmin --socket="$MYSQL_SOCKET" ping &>/dev/null 2>&1; then
SS_DB_MODE="ours"
elif (echo >/dev/tcp/127.0.0.1/3306) &>/dev/null \
|| [ -S /run/mysqld/mysqld.sock ] \
|| [ -S /var/run/mysqld/mysqld.sock ] \
|| [ -S /tmp/mysql.sock ]; then
SS_DB_MODE="existing"
fi
if [ "$SS_DB_MODE" = "existing" ]; then
echo "Existing database detected -- not starting the project one."
echo "Leaving DB_* and JSON_KEY alone so the server's own config wins."
fi
STARTED_MYSQL=0
if [ "$SS_DB_MODE" = "start" ]; then
mkdir -p "$MYSQL_DATADIR"
# Initialize data directory on first run
@@ -34,9 +64,6 @@ pkgs.mkShell {
echo "Initialization complete."
fi
# Start mysqld if not already running
STARTED_MYSQL=0
if ! mysqladmin --socket="$MYSQL_SOCKET" ping &>/dev/null 2>&1; then
STARTED_MYSQL=1
echo "Starting MySQL..."
mysqld --no-defaults \
@@ -56,7 +83,17 @@ pkgs.mkShell {
-e "CREATE DATABASE IF NOT EXISTS application;" 2>/dev/null
fi
# Set app DB env vars — dotenv won't override these since they're already in process.env
#
# Dev credentials, injected ONLY when this shell is providing the database
# and there is no ~/.env supplying real config.
#
# This matters because dotenv does NOT override variables already present in
# process.env (server/config/database.js loads ~/.env). So anything exported
# here silently beats the server's real configuration -- including JSON_KEY,
# which would invalidate every existing user session.
#
if [ "$SS_DB_MODE" != "existing" ] && [ ! -f "$HOME/.env" ]; then
export DB_HOST=127.0.0.1
export DB_USER=root
export DB_PASS=
@@ -72,6 +109,10 @@ pkgs.mkShell {
fi
fi
elif [ -f "$HOME/.env" ]; then
echo "~/.env found -- using its DB_* and JSON_KEY, not dev defaults."
fi
# Restore a SQL dump — handles both plain .sql and .gz files
restore-db() {
local file="''${1:-}"
@@ -79,6 +120,16 @@ pkgs.mkShell {
echo "Usage: restore-db <backup.sql[.gz]>"
return 1
fi
# Refuse to run against a database this shell does not manage. The dumps
# are --all-databases: restoring one would overwrite every schema on the
# server and reset root's password. That is fine for a local dev copy and
# catastrophic on the production box.
if [ "$SS_DB_MODE" = "existing" ]; then
echo "Refusing: this shell is talking to a pre-existing database." >&2
echo "restore-db only runs against the project-local dev database." >&2
return 1
fi
echo "Restoring database from $file ..."
if [[ "$file" == *.gz ]]; then
gunzip -c "$file" | mysql -u root --socket="$MYSQL_SOCKET"
@@ -99,6 +150,7 @@ pkgs.mkShell {
export -f restore-db
# Auto-restore if --argstr backupFile was passed to nix-shell
# (restore-db itself refuses when the database is not ours)
if [ -n "${backupFile}" ]; then
restore-db "${backupFile}"
fi