duplicate ip fix

This commit is contained in:
Vaibhav Pathak 2026-01-21 16:22:50 +05:30
parent 2bd01fb3fc
commit 5c06c43457

View File

@ -769,37 +769,25 @@ spec:
return 0 return 0
fi fi
# Get or create app_id # Get or create app_id (using INSERT ... ON DUPLICATE KEY UPDATE for idempotency)
echo " Ensuring app entry exists for: $app_name"
run_mysql "INSERT INTO apps (name, token, creation_date, last_update, is_deleted) VALUES ('$app_name', UUID(), NOW(), NOW(), 0) ON DUPLICATE KEY UPDATE last_update=NOW()"
APP_ID=$(run_mysql "SELECT id FROM apps WHERE name='$app_name'") APP_ID=$(run_mysql "SELECT id FROM apps WHERE name='$app_name'")
if [ -z "$APP_ID" ]; then if [ -z "$APP_ID" ]; then
echo " Creating app entry for: $app_name" echo " Failed to get app_id for: $app_name"
if run_mysql "INSERT INTO apps (name, token, creation_date, last_update, is_deleted) VALUES ('$app_name', UUID(), NOW(), NOW(), 0)"; then
APP_ID=$(run_mysql "SELECT id FROM apps WHERE name='$app_name'")
echo " Created app with ID: $APP_ID"
else
echo " Failed to create app entry for: $app_name"
return 1 return 1
fi fi
fi echo " Using app_id: $APP_ID"
if [ -n "$APP_ID" ]; then # Register IP (using INSERT IGNORE to handle duplicates gracefully)
# Check if IP already exists
EXISTING=$(run_mysql "SELECT id FROM app_ips_accesses WHERE app_id=$APP_ID AND ipaddress='$ip'")
if [ -z "$EXISTING" ]; then
echo " Registering IP $ip for app $app_name (app_id: $APP_ID)" echo " Registering IP $ip for app $app_name (app_id: $APP_ID)"
if run_mysql "INSERT INTO app_ips_accesses (app_id, ipaddress, creation_date, last_update, is_deleted) VALUES ($APP_ID, '$ip', NOW(), NOW(), 0)"; then if run_mysql "INSERT IGNORE INTO app_ips_accesses (app_id, ipaddress, creation_date, last_update, is_deleted) VALUES ($APP_ID, '$ip', NOW(), NOW(), 0)"; then
echo " Successfully registered IP $ip" echo " Successfully registered IP $ip"
else else
echo " Failed to register IP $ip" echo " Failed to register IP $ip"
fi fi
else
echo " IP $ip already registered for $app_name"
fi
else
echo " Could not get APP_ID for $app_name"
fi
return 0 return 0
} }