Skip to content

Commit 49b0a46

Browse files
authored
Merge pull request #38 from touchmegit1:shift
feat: Enhance schema bootstrap process with dynamic table readiness checks
2 parents c8eb86a + c51f5d9 commit 49b0a46

File tree

1 file changed

+58
-4
lines changed

1 file changed

+58
-4
lines changed

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

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,32 +46,86 @@ scalar_query() {
4646
fi
4747
}
4848

49+
seed_tables_from_file() {
50+
local source_file="$1"
51+
awk '
52+
{
53+
if (match($0, /^[[:space:]]*(TRUNCATE TABLE|INSERT INTO|ALTER TABLE)[[:space:]]+`?([A-Za-z0-9_]+)`?/, m)) {
54+
table = m[2]
55+
if (!(table in seen)) {
56+
seen[table] = 1
57+
print table
58+
}
59+
}
60+
}
61+
' "$source_file"
62+
}
63+
4964
bootstrap_schema() {
5065
log "6/10" "Bootstrapping schema via backend (SPRING_JPA_DDL_AUTO=update)"
5166

5267
# Start backend once with schema auto-update to create missing tables in fresh DB.
5368
SPRING_JPA_DDL_AUTO=update SPRING_SQL_INIT_MODE=never \
5469
docker compose -f "$COMPOSE_FILE" up -d backend
5570

56-
local attempts=40
71+
local schema_seed_file="$SEED_FILE"
72+
case "$SEED_STRATEGY" in
73+
data-only|data-first)
74+
schema_seed_file="$LEGACY_SEED_FILE"
75+
;;
76+
esac
77+
78+
mapfile -t required_tables < <(seed_tables_from_file "$schema_seed_file")
79+
if [ "${#required_tables[@]}" -eq 0 ]; then
80+
echo "Warning: cannot derive required tables from $schema_seed_file, fallback to users table readiness check."
81+
required_tables=("users")
82+
fi
83+
84+
local required_count="${#required_tables[@]}"
85+
local in_list
86+
in_list="$(printf "'%s'," "${required_tables[@]}")"
87+
in_list="${in_list%,}"
88+
89+
local attempts=80
5790
local i=1
91+
local users_tbl
92+
local ready_count
5893
while [ "$i" -le "$attempts" ]; do
5994
users_tbl="$(docker compose -f "$COMPOSE_FILE" exec -T mysql \
6095
mysql -N -s -uroot -p"$MYSQL_ROOT_PASSWORD" "$MYSQL_DATABASE" \
6196
-e "SELECT COUNT(*) FROM information_schema.tables WHERE table_schema='$MYSQL_DATABASE' AND table_name='users';" 2>/dev/null | tr -d '\r' || echo 0)"
6297

63-
if [ "${users_tbl:-0}" = "1" ]; then
64-
echo "Schema bootstrap ready (users table exists)."
98+
ready_count="$(docker compose -f "$COMPOSE_FILE" exec -T mysql \
99+
mysql -N -s -uroot -p"$MYSQL_ROOT_PASSWORD" "$MYSQL_DATABASE" \
100+
-e "SELECT COUNT(DISTINCT table_name) FROM information_schema.tables WHERE table_schema='$MYSQL_DATABASE' AND table_name IN ($in_list);" 2>/dev/null | tr -d '\r' || echo 0)"
101+
102+
if [ "${ready_count:-0}" = "$required_count" ]; then
103+
echo "Schema bootstrap ready ($ready_count/$required_count seed tables exist)."
65104
return 0
66105
fi
67106

68-
echo "Waiting schema bootstrap... ($i/$attempts)"
107+
echo "Waiting schema bootstrap... ($i/$attempts) users=${users_tbl:-0}, seed_tables=${ready_count:-0}/$required_count"
69108
sleep 3
70109
i=$((i + 1))
71110
done
72111

112+
local missing=()
113+
local tbl
114+
for tbl in "${required_tables[@]}"; do
115+
local exists
116+
exists="$(docker compose -f "$COMPOSE_FILE" exec -T mysql \
117+
mysql -N -s -uroot -p"$MYSQL_ROOT_PASSWORD" "$MYSQL_DATABASE" \
118+
-e "SELECT COUNT(*) FROM information_schema.tables WHERE table_schema='$MYSQL_DATABASE' AND table_name='$tbl';" 2>/dev/null | tr -d '\r' || echo 0)"
119+
if [ "${exists:-0}" != "1" ]; then
120+
missing+=("$tbl")
121+
fi
122+
done
123+
73124
echo "Schema bootstrap timed out. Backend logs:"
74125
docker compose -f "$COMPOSE_FILE" logs --tail=120 backend || true
126+
if [ "${#missing[@]}" -gt 0 ]; then
127+
echo "Missing tables after bootstrap: ${missing[*]}"
128+
fi
75129
return 1
76130
}
77131

0 commit comments

Comments
 (0)