Commit c0e2fcae658b2ee8e39076545f01d79f6f126f3d

Authored by zichun
1 parent 7caeb6c5

fix(scripts): load .env.local in seed-dev-admin so it targets correct DB

Previously the script used hardcoded defaults (127.0.0.1:3306 / xly_erp)
and ignored the user's remote test DB. Now sources .env.local like
setup-test-db.sh does. Documents the prereq that Spring Boot must have
started at least once (Flyway applies V1) before seeding.
Showing 1 changed file with 20 additions and 8 deletions
scripts/seed-dev-admin.sh
1 1 #!/usr/bin/env bash
2 2 # Seed a dev admin user (sUserName=admin, password=666666).
3   -# Run once after first `mvn spring-boot:run` so Flyway has applied V1.
4   -# Idempotent: uses INSERT IGNORE on the unique key.
  3 +#
  4 +# Prerequisites:
  5 +# 1. .env.local exists with DB_HOST/DB_PORT/DB_SCHEMA/DB_USER/DB_PASSWORD
  6 +# 2. Schema has been created — i.e. Spring Boot has started at least once
  7 +# so Flyway has applied V1__initial_schema.sql (creates tUser etc.)
  8 +#
  9 +# Idempotent: uses INSERT IGNORE on the unique key (sUserName).
5 10  
6 11 set -euo pipefail
7 12  
8   -DB_HOST="${DB_HOST:-127.0.0.1}"
9   -DB_PORT="${DB_PORT:-3306}"
10   -DB_SCHEMA="${DB_SCHEMA:-xly_erp}"
11   -DB_USER="${DB_USER:-root}"
  13 +# macOS Homebrew mysql-client is keg-only; prepend common install paths.
  14 +for p in /opt/homebrew/opt/mysql-client/bin /usr/local/opt/mysql-client/bin; do
  15 + [ -d "$p" ] && case ":$PATH:" in *":$p:"*) ;; *) PATH="$p:$PATH" ;; esac
  16 +done
  17 +
  18 +ENV_FILE="$(dirname "$0")/../.env.local"
  19 +[ -f "$ENV_FILE" ] || { echo "[seed-dev-admin] ⚠️ .env.local 不存在($ENV_FILE)" >&2; exit 1; }
  20 +
  21 +set -a; . "$ENV_FILE"; set +a
12 22  
13 23 # BCrypt(666666) cost 10. Spring's BCryptPasswordEncoder accepts both $2a$ and $2b$ prefixes.
14 24 HASH='$2b$10$ZzbGP0yWo3QJkaiZqEgtAOQVrzrti911VqbY7FoethhGQFPV0/Oj6'
15 25  
16   -mysql -h "$DB_HOST" -P "$DB_PORT" -u "$DB_USER" ${DB_PASSWORD:+-p"$DB_PASSWORD"} "$DB_SCHEMA" <<SQL
  26 +echo "[seed-dev-admin] target: ${DB_USER}@${DB_HOST}:${DB_PORT}/${DB_SCHEMA}"
  27 +
  28 +mysql -h"${DB_HOST}" -P"${DB_PORT}" -u"${DB_USER}" -p"${DB_PASSWORD}" "${DB_SCHEMA}" <<SQL
17 29 INSERT IGNORE INTO tUser
18 30 (sBrandsId, sSubsidiaryId, tCreateDate,
19 31 sUserNo, sUserName, sUserType, sLanguage, bCanModifyDocs,
... ... @@ -24,4 +36,4 @@ VALUES
24 36 '$HASH', 'system', 0);
25 37 SQL
26 38  
27   -echo "Seeded dev admin: sUserName=admin / password=666666"
  39 +echo "[seed-dev-admin] OK — sUserName=admin / password=666666"
... ...