Initial commit: SQL Server 2025 local dev stack
This commit is contained in:
47
scripts/seed.sh
Executable file
47
scripts/seed.sh
Executable file
@@ -0,0 +1,47 @@
|
||||
#!/usr/bin/env bash
|
||||
set -Eeuo pipefail
|
||||
|
||||
ts() { date +"%Y-%m-%d %H:%M:%S"; }
|
||||
log() { echo "[$(ts)] [INFO] $*"; }
|
||||
warn(){ echo "[$(ts)] [WARN] $*" >&2; }
|
||||
err() { echo "[$(ts)] [ERROR] $*" >&2; }
|
||||
die() { err "$*"; exit 1; }
|
||||
|
||||
trap 'err "Seed failed. Showing last 200 log lines:"; docker logs --tail=200 mssql2025 2>/dev/null || true' ERR
|
||||
|
||||
cd "$(dirname "$0")/.."
|
||||
|
||||
[[ -f .env ]] || die "Missing .env. Create it first: cp .env.example .env"
|
||||
|
||||
set -a
|
||||
# shellcheck disable=SC1091
|
||||
source .env
|
||||
set +a
|
||||
|
||||
[[ -n "${MSSQL_SA_PASSWORD:-}" ]] || die "MSSQL_SA_PASSWORD is empty in .env"
|
||||
|
||||
if ! docker ps --format '{{.Names}}' | grep -qx 'mssql2025'; then
|
||||
die "Container mssql2025 is not running. Start it first: ./scripts/start.sh"
|
||||
fi
|
||||
|
||||
health="$(docker inspect -f '{{.State.Health.Status}}' mssql2025 2>/dev/null || true)"
|
||||
[[ "$health" == "healthy" ]] || die "Container is not healthy (health=$health). Run ./scripts/start.sh"
|
||||
|
||||
shopt -s nullglob
|
||||
files=(db/seed/*.sql)
|
||||
|
||||
if (( ${#files[@]} == 0 )); then
|
||||
warn "No db/seed/*.sql files found. Nothing to do."
|
||||
exit 0
|
||||
fi
|
||||
|
||||
log "Executing ${#files[@]} seed file(s) (idempotent)."
|
||||
for f in "${files[@]}"; do
|
||||
bn="$(basename "$f")"
|
||||
log "-> seed: $bn"
|
||||
docker exec -i mssql2025 /opt/mssql-tools18/bin/sqlcmd \
|
||||
-S localhost -U sa -P "$MSSQL_SA_PASSWORD" \
|
||||
-b -i "/seed/$bn" < /dev/null
|
||||
done
|
||||
|
||||
log "Seed completed ✅"
|
||||
Reference in New Issue
Block a user