Skip to content

Commit 8a06e44

Browse files
authored
Merge pull request #23 from touchmegit1/dev
feat: enhance database reset script with schema bootstrapping and imp…
2 parents feb5d4a + 8941f4a commit 8a06e44

File tree

1 file changed

+39
-2
lines changed

1 file changed

+39
-2
lines changed

deploy/scripts/reset-db-and-seed.sh

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,43 @@ require_file() {
2424

2525
count_table() {
2626
local table="$1"
27-
docker compose -f "$COMPOSE_FILE" exec -T mysql \
28-
mysql -N -s -uroot -p"$MYSQL_ROOT_PASSWORD" "$MYSQL_DATABASE" -e "SELECT COUNT(*) FROM $table;" | tr -d '\r'
27+
local out
28+
if out="$(docker compose -f "$COMPOSE_FILE" exec -T mysql \
29+
mysql -N -s -uroot -p"$MYSQL_ROOT_PASSWORD" "$MYSQL_DATABASE" -e "SELECT COUNT(*) FROM $table;" 2>/dev/null | tr -d '\r')"; then
30+
printf '%s\n' "${out:-0}"
31+
else
32+
# Table may not exist yet - treat as 0 so fallback logic can continue.
33+
printf '0\n'
34+
fi
35+
}
36+
37+
bootstrap_schema() {
38+
log "6/10" "Bootstrapping schema via backend (SPRING_JPA_DDL_AUTO=update)"
39+
40+
# Start backend once with schema auto-update to create missing tables in fresh DB.
41+
SPRING_JPA_DDL_AUTO=update SPRING_SQL_INIT_MODE=never \
42+
docker compose -f "$COMPOSE_FILE" up -d backend
43+
44+
local attempts=40
45+
local i=1
46+
while [ "$i" -le "$attempts" ]; do
47+
users_tbl="$(docker compose -f "$COMPOSE_FILE" exec -T mysql \
48+
mysql -N -s -uroot -p"$MYSQL_ROOT_PASSWORD" "$MYSQL_DATABASE" \
49+
-e "SELECT COUNT(*) FROM information_schema.tables WHERE table_schema='$MYSQL_DATABASE' AND table_name='users';" 2>/dev/null | tr -d '\r' || echo 0)"
50+
51+
if [ "${users_tbl:-0}" = "1" ]; then
52+
echo "Schema bootstrap ready (users table exists)."
53+
return 0
54+
fi
55+
56+
echo "Waiting schema bootstrap... ($i/$attempts)"
57+
sleep 3
58+
i=$((i + 1))
59+
done
60+
61+
echo "Schema bootstrap timed out. Backend logs:"
62+
docker compose -f "$COMPOSE_FILE" logs --tail=120 backend || true
63+
return 1
2964
}
3065

3166
seed_with_data_sql() {
@@ -134,6 +169,8 @@ log "5/10" "Dropping and recreating database ($MYSQL_DATABASE)"
134169
printf "%s\n" "$SQL_RESET" | docker compose -f "$COMPOSE_FILE" exec -T mysql \
135170
mysql -uroot -p"$MYSQL_ROOT_PASSWORD"
136171

172+
bootstrap_schema
173+
137174
seed_with_data_sql
138175

139176
log "7/10" "Verifying critical table counts after data.sql"

0 commit comments

Comments
 (0)