Add DB schema from production dump; apply on db start, fix env passthrough to artisan serve
This commit is contained in:
1 parent
4d77464988
commit
d38f9a1a83
3 files changed
+171
-10
No files matched your search
@@ -29,9 +29,10 @@ usage() {
|
||||
Usage: dev <command>
|
||||
|
||||
setup composer install both apps + init local MariaDB
|
||||
db start|stop|status manage local MariaDB (data in .dev/mysql, port $DB_PORT)
|
||||
db start|stop|status manage local MariaDB (data in .dev/mysql, port $DB_PORT); start applies dev/schema.sql
|
||||
db shell mysql client on the local DB
|
||||
db import <dump.sql> load a production dump (schema lives only in the prod DB)
|
||||
db import <dump.sql> drop + recreate the DB from a production dump (schema + data)
|
||||
db reset drop + recreate the DB with dev/schema.sql only (empty tables)
|
||||
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
|
||||
@@ -81,9 +82,19 @@ 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
|
||||
db_apply_schema
|
||||
echo "db: listening on 127.0.0.1:$DB_PORT (pid $(cat "$MYSQL_PID"))"
|
||||
}
|
||||
|
||||
# Idempotent (CREATE TABLE IF NOT EXISTS), so safe on every start; a no-op once tables exist.
|
||||
db_apply_schema() {
|
||||
mariadb --socket="$MYSQL_SOCK" -uroot "$DB_NAME" <"$ROOT/dev/schema.sql"
|
||||
}
|
||||
|
||||
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;"
|
||||
}
|
||||
|
||||
db_stop() {
|
||||
db_running || { echo "db: not running"; return; }
|
||||
mariadb-admin --socket="$MYSQL_SOCK" -uroot shutdown
|
||||
@@ -99,14 +110,24 @@ db_status() {
|
||||
fi
|
||||
}
|
||||
|
||||
# phpMyAdmin dumps carry bare CREATE TABLEs, so start from an empty database.
|
||||
db_import() {
|
||||
local dump="${1:?usage: dev db import <dump.sql>}"
|
||||
[ -f "$dump" ] || { echo "db: no such file: $dump" >&2; exit 1; }
|
||||
db_running || db_start
|
||||
db_recreate
|
||||
mariadb --socket="$MYSQL_SOCK" -uroot "$DB_NAME" <"$dump"
|
||||
echo "db: imported $dump"
|
||||
mariadb --socket="$MYSQL_SOCK" -uroot "$DB_NAME" -e "SHOW TABLES"
|
||||
}
|
||||
|
||||
db_reset() {
|
||||
db_running || db_start
|
||||
db_recreate
|
||||
db_apply_schema
|
||||
echo "db: reset to dev/schema.sql"
|
||||
}
|
||||
|
||||
composer_install() {
|
||||
for app in "$FRONTEND" "$BACKEND"; do
|
||||
[ -f "$app/.env" ] || { echo "missing $app/.env" >&2; exit 1; }
|
||||
@@ -118,7 +139,7 @@ serve() {
|
||||
local dir port
|
||||
dir="$(app_dir "$1")"
|
||||
case "$1" in frontend) port="$FRONTEND_PORT" ;; backend) port="$BACKEND_PORT" ;; esac
|
||||
cd "$dir" && exec php artisan serve --host=127.0.0.1 --port="$port"
|
||||
cd "$dir" && exec php artisan serve --no-reload --host=127.0.0.1 --port="$port"
|
||||
}
|
||||
|
||||
up() {
|
||||
@@ -135,15 +156,16 @@ up() {
|
||||
db_stop
|
||||
}
|
||||
trap cleanup EXIT INT TERM
|
||||
(cd "$FRONTEND" && php artisan serve --host=127.0.0.1 --port="$FRONTEND_PORT") & pids+=($!)
|
||||
(cd "$BACKEND" && php artisan serve --host=127.0.0.1 --port="$BACKEND_PORT") & pids+=($!)
|
||||
# --no-reload: without it `artisan serve` drops our DB_*/APP_* overrides from the php -S child env.
|
||||
(cd "$FRONTEND" && php artisan serve --no-reload --host=127.0.0.1 --port="$FRONTEND_PORT") & pids+=($!)
|
||||
(cd "$BACKEND" && php artisan serve --no-reload --host=127.0.0.1 --port="$BACKEND_PORT") & pids+=($!)
|
||||
echo "frontend: http://127.0.0.1:$FRONTEND_PORT"
|
||||
echo "backend: http://127.0.0.1:$BACKEND_PORT"
|
||||
wait
|
||||
}
|
||||
|
||||
down() {
|
||||
pkill -f "artisan serve --host=127.0.0.1 --port=($FRONTEND_PORT|$BACKEND_PORT)" 2>/dev/null || true
|
||||
pkill -f "artisan serve --no-reload --host=127.0.0.1 --port=($FRONTEND_PORT|$BACKEND_PORT)" 2>/dev/null || true
|
||||
db_stop
|
||||
}
|
||||
|
||||
@@ -157,6 +179,7 @@ case "$cmd" in
|
||||
stop) db_stop ;;
|
||||
status) db_status ;;
|
||||
shell) db_running || db_start; exec mariadb --socket="$MYSQL_SOCK" -uroot "$DB_NAME" ;;
|
||||
reset) db_reset ;;
|
||||
import) db_import "$@" ;;
|
||||
*) usage; exit 2 ;;
|
||||
esac ;;
|
||||
|
||||
Reference in new issue
Block a user