Seed fresh local DB from .dev production dump automatically

This commit is contained in:
kurogeek committed 2026-10-16 03:37:12 +00:00
1 parent abf482eeb0
commit 23e07f12e5
3 files changed
+29 -7

No files matched your search

+22 -3
View File
@@ -10,6 +10,8 @@ MYSQL_DATA="$STATE/mysql"
MYSQL_SOCK="$STATE/mysql.sock"
MYSQL_PID="$STATE/mysql.pid"
MYSQL_LOG="$STATE/mysql.log"
# Production export (phpMyAdmin .sql, or the .zip it came in). Seeded into every fresh DB when present.
DB_DUMP="${DEV_DB_DUMP:-$STATE/greaterc_db2024.sql}"
# Must match DB_* in both apps' .env (only the port differs, to avoid clashing with a system MySQL).
DB_PORT="${DEV_DB_PORT:-3307}"
@@ -32,7 +34,7 @@ Usage: dev <command>
db start|stop|status manage local MariaDB (data in .dev/mysql, port $DB_PORT); start runs backend migrations
db shell mysql client on the local DB
db import <dump.sql> drop + recreate the DB from a production dump (schema + data)
db reset drop + recreate the DB, then migrate (empty tables)
db reset drop + recreate the DB, seed from .dev/greaterc_db2024.sql[.zip] if present, migrate
serve frontend php artisan serve -> http://127.0.0.1:$FRONTEND_PORT
serve backend php artisan serve -> http://127.0.0.1:$BACKEND_PORT
up db + both apps; Ctrl-C stops everything
@@ -77,12 +79,15 @@ db_start() {
done
mariadb-admin --socket="$MYSQL_SOCK" -uroot ping >/dev/null 2>&1 || { echo "db: failed to start, see $MYSQL_LOG" >&2; exit 1; }
mariadb --socket="$MYSQL_SOCK" -uroot <<SQL
CREATE DATABASE IF NOT EXISTS \`$DB_NAME\` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER IF NOT EXISTS '$DB_USER'@'127.0.0.1' IDENTIFIED BY '$DB_PASS';
CREATE USER IF NOT EXISTS '$DB_USER'@'localhost' IDENTIFIED BY '$DB_PASS';
GRANT ALL ON \`$DB_NAME\`.* TO '$DB_USER'@'127.0.0.1', '$DB_USER'@'localhost';
FLUSH PRIVILEGES;
SQL
if ! mariadb --socket="$MYSQL_SOCK" -uroot -e "USE \`$DB_NAME\`" 2>/dev/null; then
db_recreate
db_seed
fi
db_migrate
echo "db: listening on 127.0.0.1:$DB_PORT (pid $(cat "$MYSQL_PID"))"
}
@@ -97,6 +102,19 @@ db_recreate() {
mariadb --socket="$MYSQL_SOCK" -uroot -e "DROP DATABASE IF EXISTS \`$DB_NAME\`; CREATE DATABASE \`$DB_NAME\` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;"
}
# Load the production export into the (empty) DB. Unzips once if only the archive is present.
db_seed() {
if [ ! -f "$DB_DUMP" ] && [ -f "$DB_DUMP.zip" ]; then
unzip -o -q "$DB_DUMP.zip" "$(basename "$DB_DUMP")" -d "$(dirname "$DB_DUMP")"
fi
if [ ! -f "$DB_DUMP" ]; then
echo "db: no dump at $DB_DUMP, starting with empty tables"
return
fi
mariadb --socket="$MYSQL_SOCK" -uroot "$DB_NAME" <"$DB_DUMP"
echo "db: seeded from $DB_DUMP"
}
db_stop() {
db_running || { echo "db: not running"; return; }
mariadb-admin --socket="$MYSQL_SOCK" -uroot shutdown
@@ -126,8 +144,9 @@ db_import() {
db_reset() {
db_running || db_start
db_recreate
db_seed
db_migrate
echo "db: reset to empty schema"
echo "db: reset"
}
composer_install() {