Skip to content

Commit 7b84a73

Browse files
authored
Merge pull request #17 from touchmegit1/dev
feat: enhance database reset and seeding script with error handling and verification
2 parents 63cb926 + 491d13a commit 7b84a73

File tree

1 file changed

+94
-13
lines changed

1 file changed

+94
-13
lines changed

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

Lines changed: 94 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
#!/usr/bin/env bash
22
# Reset (drop + create) database and seed again on Azure VM.
3+
# Robust mode: continues through SQL errors, then verifies critical tables.
34
set -euo pipefail
45

56
DEPLOY_PATH="${DEPLOY_PATH:-/opt/smalltrend}"
67
COMPOSE_FILE="${COMPOSE_FILE:-$DEPLOY_PATH/docker-compose.prod.yml}"
78
ENV_FILE="${ENV_FILE:-$DEPLOY_PATH/deploy/env/backend.env}"
89
SEED_FILE="${SEED_FILE:-$DEPLOY_PATH/backend/src/main/resources/data.sql}"
10+
BACKUP_DIR="${BACKUP_DIR:-$DEPLOY_PATH/backup_data_value}"
11+
SEED_LOG_DIR="${SEED_LOG_DIR:-$DEPLOY_PATH/deploy/seed-logs}"
912

1013
log() {
1114
printf "[%s] %s\n" "$1" "$2"
@@ -19,14 +22,71 @@ require_file() {
1922
}
2023
}
2124

22-
log "1/7" "Checking required files"
25+
count_table() {
26+
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'
29+
}
30+
31+
seed_with_data_sql() {
32+
mkdir -p "$SEED_LOG_DIR"
33+
local err_log="$SEED_LOG_DIR/data_sql_errors_$(date +%Y%m%d_%H%M%S).log"
34+
35+
log "6/10" "Seeding from data.sql with --force (won't stop at first SQL error)"
36+
set +e
37+
docker compose -f "$COMPOSE_FILE" exec -T mysql \
38+
mysql --force --default-character-set=utf8mb4 -uroot -p"$MYSQL_ROOT_PASSWORD" "$MYSQL_DATABASE" < "$SEED_FILE" 2>"$err_log"
39+
local rc=$?
40+
set -e
41+
42+
if [ "$rc" -ne 0 ]; then
43+
log "WARN" "data.sql finished with errors (continued). See: $err_log"
44+
else
45+
log "INFO" "data.sql import completed successfully"
46+
fi
47+
}
48+
49+
seed_with_backup_files() {
50+
[ -d "$BACKUP_DIR" ] || {
51+
log "WARN" "Backup directory not found: $BACKUP_DIR"
52+
return 1
53+
}
54+
55+
log "8/10" "Fallback seed from backup_data_value/*.sql"
56+
57+
# Disable FK checks to avoid order dependency across split files.
58+
docker compose -f "$COMPOSE_FILE" exec -T mysql \
59+
mysql -uroot -p"$MYSQL_ROOT_PASSWORD" "$MYSQL_DATABASE" -e "SET FOREIGN_KEY_CHECKS=0;"
60+
61+
local imported=0
62+
for f in "$BACKUP_DIR"/smalltrend_*.sql; do
63+
[ -f "$f" ] || continue
64+
echo "Importing $(basename "$f")"
65+
set +e
66+
docker compose -f "$COMPOSE_FILE" exec -T mysql \
67+
mysql --force --default-character-set=utf8mb4 -uroot -p"$MYSQL_ROOT_PASSWORD" "$MYSQL_DATABASE" < "$f"
68+
local rc=$?
69+
set -e
70+
if [ "$rc" -ne 0 ]; then
71+
log "WARN" "Import had errors: $(basename "$f") (continued)"
72+
fi
73+
imported=$((imported + 1))
74+
done
75+
76+
docker compose -f "$COMPOSE_FILE" exec -T mysql \
77+
mysql -uroot -p"$MYSQL_ROOT_PASSWORD" "$MYSQL_DATABASE" -e "SET FOREIGN_KEY_CHECKS=1;"
78+
79+
log "INFO" "Fallback import attempted $imported file(s)"
80+
}
81+
82+
log "1/10" "Checking required files"
2383
require_file "$COMPOSE_FILE"
2484
require_file "$ENV_FILE"
2585
require_file "$SEED_FILE"
2686

2787
cd "$DEPLOY_PATH"
2888

29-
log "2/7" "Reading required DB variables from backend.env"
89+
log "2/10" "Reading required DB variables from backend.env"
3090
get_env_value() {
3191
local key="$1"
3292
sed -n "s/^${key}=//p" "$ENV_FILE" | tail -n 1 | tr -d '\r'
@@ -38,10 +98,10 @@ MYSQL_ROOT_PASSWORD="$(get_env_value MYSQL_ROOT_PASSWORD)"
3898
: "${MYSQL_DATABASE:=smalltrend}"
3999
: "${MYSQL_ROOT_PASSWORD:=root1234}"
40100

41-
log "3/7" "Starting mysql service"
101+
log "3/10" "Starting mysql service"
42102
docker compose -f "$COMPOSE_FILE" up -d mysql
43103

44-
log "4/7" "Waiting for mysql health"
104+
log "4/10" "Waiting for mysql health"
45105
MYSQL_CONTAINER_ID="$(docker compose -f "$COMPOSE_FILE" ps -q mysql)"
46106
[ -n "$MYSQL_CONTAINER_ID" ] || {
47107
echo "Could not resolve mysql container ID"
@@ -70,16 +130,37 @@ fi
70130
DB_ESCAPED="$(printf '%s' "$MYSQL_DATABASE" | sed 's/`/``/g')"
71131
SQL_RESET="DROP DATABASE IF EXISTS \`$DB_ESCAPED\`; CREATE DATABASE \`$DB_ESCAPED\` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;"
72132

73-
log "5/7" "Dropping and recreating database ($MYSQL_DATABASE)"
74-
printf "%s\n" "$SQL_RESET" | docker compose -f "$COMPOSE_FILE" exec -T mysql sh -lc \
75-
"mysql -uroot -p\"$MYSQL_ROOT_PASSWORD\""
133+
log "5/10" "Dropping and recreating database ($MYSQL_DATABASE)"
134+
printf "%s\n" "$SQL_RESET" | docker compose -f "$COMPOSE_FILE" exec -T mysql \
135+
mysql -uroot -p"$MYSQL_ROOT_PASSWORD"
76136

77-
log "6/7" "Seeding database from $SEED_FILE"
78-
docker compose -f "$COMPOSE_FILE" exec -T mysql sh -lc \
79-
"mysql --default-character-set=utf8mb4 -uroot -p\"$MYSQL_ROOT_PASSWORD\" \"$MYSQL_DATABASE\"" < "$SEED_FILE"
137+
seed_with_data_sql
80138

81-
log "7/7" "Done"
82-
echo "Database reset + seed completed for: $MYSQL_DATABASE"
139+
log "7/10" "Verifying critical table counts after data.sql"
140+
USERS_COUNT="$(count_table users)"
141+
PRODUCTS_COUNT="$(count_table products)"
142+
VARIANTS_COUNT="$(count_table product_variants)"
143+
STOCK_COUNT="$(count_table inventory_stock)"
144+
echo "users=$USERS_COUNT, products=$PRODUCTS_COUNT, variants=$VARIANTS_COUNT, inventory_stock=$STOCK_COUNT"
145+
146+
if [ "$PRODUCTS_COUNT" = "0" ] || [ "$VARIANTS_COUNT" = "0" ]; then
147+
log "WARN" "Critical product tables are empty after data.sql. Running fallback import..."
148+
seed_with_backup_files
149+
fi
150+
151+
log "9/10" "Final verification"
152+
USERS_COUNT="$(count_table users)"
153+
PRODUCTS_COUNT="$(count_table products)"
154+
VARIANTS_COUNT="$(count_table product_variants)"
155+
STOCK_COUNT="$(count_table inventory_stock)"
156+
echo "users=$USERS_COUNT, products=$PRODUCTS_COUNT, variants=$VARIANTS_COUNT, inventory_stock=$STOCK_COUNT"
83157

84-
echo "Next command (optional):"
158+
if [ "$PRODUCTS_COUNT" = "0" ] || [ "$VARIANTS_COUNT" = "0" ]; then
159+
log "ERROR" "Seed finished but critical tables are still empty. Check logs in $SEED_LOG_DIR and backup SQL files."
160+
exit 1
161+
fi
162+
163+
log "10/10" "Done"
164+
echo "Database reset + seed completed for: $MYSQL_DATABASE"
165+
echo "Next command:"
85166
echo " docker compose -f $COMPOSE_FILE up -d --remove-orphans"

0 commit comments

Comments
 (0)