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

+6 -3
View File
@@ -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 . -- <cmd>` forwards to the `dev` script; `nix develop` drops you into a shell with
+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() {
+1 -1
View File
@@ -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: {