Seed fresh local DB from .dev production dump automatically
This commit is contained in:
1 parent
abf482eeb0
commit
23e07f12e5
3 files changed
+29
-7
No files matched your search
@@ -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
|
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.
|
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
|
```sh
|
||||||
nix run . -- db import .dev/greaterc_db2024.sql # phpMyAdmin export, kept out of git
|
nix run . -- db reset # wipe + reseed from the dump (or empty schema if none)
|
||||||
nix run . -- db reset # back to empty schema
|
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
|
`nix run . -- <cmd>` forwards to the `dev` script; `nix develop` drops you into a shell with
|
||||||
|
|||||||
@@ -10,6 +10,8 @@ MYSQL_DATA="$STATE/mysql"
|
|||||||
MYSQL_SOCK="$STATE/mysql.sock"
|
MYSQL_SOCK="$STATE/mysql.sock"
|
||||||
MYSQL_PID="$STATE/mysql.pid"
|
MYSQL_PID="$STATE/mysql.pid"
|
||||||
MYSQL_LOG="$STATE/mysql.log"
|
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).
|
# Must match DB_* in both apps' .env (only the port differs, to avoid clashing with a system MySQL).
|
||||||
DB_PORT="${DEV_DB_PORT:-3307}"
|
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 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 shell mysql client on the local DB
|
||||||
db import <dump.sql> drop + recreate the DB from a production dump (schema + data)
|
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 frontend php artisan serve -> http://127.0.0.1:$FRONTEND_PORT
|
||||||
serve backend php artisan serve -> http://127.0.0.1:$BACKEND_PORT
|
serve backend php artisan serve -> http://127.0.0.1:$BACKEND_PORT
|
||||||
up db + both apps; Ctrl-C stops everything
|
up db + both apps; Ctrl-C stops everything
|
||||||
@@ -77,12 +79,15 @@ db_start() {
|
|||||||
done
|
done
|
||||||
mariadb-admin --socket="$MYSQL_SOCK" -uroot ping >/dev/null 2>&1 || { echo "db: failed to start, see $MYSQL_LOG" >&2; exit 1; }
|
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
|
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'@'127.0.0.1' IDENTIFIED BY '$DB_PASS';
|
||||||
CREATE USER IF NOT EXISTS '$DB_USER'@'localhost' 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';
|
GRANT ALL ON \`$DB_NAME\`.* TO '$DB_USER'@'127.0.0.1', '$DB_USER'@'localhost';
|
||||||
FLUSH PRIVILEGES;
|
FLUSH PRIVILEGES;
|
||||||
SQL
|
SQL
|
||||||
|
if ! mariadb --socket="$MYSQL_SOCK" -uroot -e "USE \`$DB_NAME\`" 2>/dev/null; then
|
||||||
|
db_recreate
|
||||||
|
db_seed
|
||||||
|
fi
|
||||||
db_migrate
|
db_migrate
|
||||||
echo "db: listening on 127.0.0.1:$DB_PORT (pid $(cat "$MYSQL_PID"))"
|
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;"
|
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_stop() {
|
||||||
db_running || { echo "db: not running"; return; }
|
db_running || { echo "db: not running"; return; }
|
||||||
mariadb-admin --socket="$MYSQL_SOCK" -uroot shutdown
|
mariadb-admin --socket="$MYSQL_SOCK" -uroot shutdown
|
||||||
@@ -126,8 +144,9 @@ db_import() {
|
|||||||
db_reset() {
|
db_reset() {
|
||||||
db_running || db_start
|
db_running || db_start
|
||||||
db_recreate
|
db_recreate
|
||||||
|
db_seed
|
||||||
db_migrate
|
db_migrate
|
||||||
echo "db: reset to empty schema"
|
echo "db: reset"
|
||||||
}
|
}
|
||||||
|
|
||||||
composer_install() {
|
composer_install() {
|
||||||
|
|||||||
@@ -9,7 +9,7 @@
|
|||||||
forAll = f: nixpkgs.lib.genAttrs systems (system: f nixpkgs.legacyPackages.${system});
|
forAll = f: nixpkgs.lib.genAttrs systems (system: f nixpkgs.legacyPackages.${system});
|
||||||
toolchain = pkgs:
|
toolchain = pkgs:
|
||||||
let php = pkgs.php83; in
|
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
|
in
|
||||||
{
|
{
|
||||||
devShells = forAll (pkgs: {
|
devShells = forAll (pkgs: {
|
||||||
|
|||||||
Reference in new issue
Block a user