From 23e07f12e5156e124a43b500bc380afc1e3614d6 Mon Sep 17 00:00:00 2001 From: kurogeek Date: Fri, 16 Oct 2026 03:37:12 +0000 Subject: [PATCH] Seed fresh local DB from .dev production dump automatically --- README.md | 9 ++++++--- dev/dev | 25 ++++++++++++++++++++++--- flake.nix | 2 +- 3 files changed, 29 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 46f09fe..6133fbd 100644 --- a/README.md +++ b/README.md @@ -28,11 +28,14 @@ tables byte-for-byte (verified by diffing `mariadb-dump --no-data` against the p its creates are guarded with `Schema::hasTable`, so running `migrate` on production only records it. Add new migrations in the backend for any schema change; the site app has none. -To work with real content, load a production dump (drops and recreates the local DB): +Real content: drop the production export at `.dev/greaterc_db2024.sql` (or the `.sql.zip` +phpMyAdmin produced — it's unzipped on demand; both are git-ignored). A fresh DB seeds +itself from it on first `nix run`; existing data is never overwritten on restart. ```sh -nix run . -- db import .dev/greaterc_db2024.sql # phpMyAdmin export, kept out of git -nix run . -- db reset # back to empty schema +nix run . -- db reset # wipe + reseed from the dump (or empty schema if none) +nix run . -- db import other.sql # load a different dump +DEV_DB_DUMP=/path/to/x.sql nix run # use a dump elsewhere ``` `nix run . -- ` forwards to the `dev` script; `nix develop` drops you into a shell with diff --git a/dev/dev b/dev/dev index 40d4193..38711bd 100755 --- a/dev/dev +++ b/dev/dev @@ -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 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 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 </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() { diff --git a/flake.nix b/flake.nix index 49cede7..1d311ff 100644 --- a/flake.nix +++ b/flake.nix @@ -9,7 +9,7 @@ forAll = f: nixpkgs.lib.genAttrs systems (system: f nixpkgs.legacyPackages.${system}); toolchain = pkgs: let php = pkgs.php83; in - [ php php.packages.composer pkgs.mariadb pkgs.nodejs_22 ]; + [ php php.packages.composer pkgs.mariadb pkgs.nodejs_22 pkgs.unzip ]; in { devShells = forAll (pkgs: {