diff --git a/environment-provisioner/workflow-template.yaml b/environment-provisioner/workflow-template.yaml index 8b33dac..eb1bc5d 100644 --- a/environment-provisioner/workflow-template.yaml +++ b/environment-provisioner/workflow-template.yaml @@ -769,36 +769,24 @@ spec: return 0 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'") if [ -z "$APP_ID" ]; then - echo " Creating app entry 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 - fi + echo " Failed to get app_id for: $app_name" + return 1 fi + echo " Using app_id: $APP_ID" - if [ -n "$APP_ID" ]; then - # 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)" - 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 - echo " Successfully registered IP $ip" - else - echo " Failed to register IP $ip" - fi - else - echo " IP $ip already registered for $app_name" - fi + # Register IP (using INSERT IGNORE to handle duplicates gracefully) + echo " Registering IP $ip for app $app_name (app_id: $APP_ID)" + 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" else - echo " Could not get APP_ID for $app_name" + echo " Failed to register IP $ip" fi return 0 }