diff --git a/environment-provisioner/workflow-template.yaml b/environment-provisioner/workflow-template.yaml index 2740da1..01d78f4 100644 --- a/environment-provisioner/workflow-template.yaml +++ b/environment-provisioner/workflow-template.yaml @@ -796,21 +796,33 @@ spec: return 0 fi - # Get or create app_id (using INSERT ... ON DUPLICATE KEY UPDATE for idempotency) - echo " Ensuring app entry exists for: $app_name" - INSERT_RESULT=$(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()" 2>&1) || true - echo " INSERT result: '$INSERT_RESULT'" + # Try to find existing app with various name formats + # Database may have: HADES, hades, hades-service, hadesService, etc. + echo " Looking for existing app matching: $app_name" - # Debug: Show all apps in table - echo " DEBUG: Listing apps in database..." - run_mysql "SELECT id, name FROM apps LIMIT 10" 2>&1 || true + # Extract base name (remove -service suffix if present) + BASE_NAME=$(echo "$app_name" | sed 's/-service$//' | sed 's/Service$//') + BASE_NAME_UPPER=$(echo "$BASE_NAME" | tr '[:lower:]' '[:upper:]') + BASE_NAME_LOWER=$(echo "$BASE_NAME" | tr '[:upper:]' '[:lower:]') - APP_ID=$(run_mysql "SELECT id FROM apps WHERE name='$app_name'" 2>&1) || true - echo " DEBUG: Raw APP_ID result: '$APP_ID'" + # Try multiple name variations (case-insensitive search) + APP_ID="" + for try_name in "$app_name" "$BASE_NAME" "$BASE_NAME_UPPER" "$BASE_NAME_LOWER"; do + APP_ID=$(run_mysql "SELECT id FROM apps WHERE LOWER(name)=LOWER('$try_name')" 2>&1) || true + APP_ID=$(echo "$APP_ID" | grep -o '[0-9]*' | head -1) + if [ -n "$APP_ID" ]; then + echo " Found existing app with name variation: $try_name (id: $APP_ID)" + break + fi + done - # Clean APP_ID - remove any non-numeric characters - APP_ID=$(echo "$APP_ID" | grep -o '[0-9]*' | head -1) - echo " DEBUG: Cleaned APP_ID: '$APP_ID'" + # If not found, create new app + if [ -z "$APP_ID" ]; then + echo " No existing app found, creating: $app_name" + run_mysql "INSERT INTO apps (name, token, creation_date, last_update, is_deleted) VALUES ('$app_name', UUID(), NOW(), NOW(), 0)" 2>&1 || true + APP_ID=$(run_mysql "SELECT id FROM apps WHERE name='$app_name'" 2>&1) || true + APP_ID=$(echo "$APP_ID" | grep -o '[0-9]*' | head -1) + fi if [ -z "$APP_ID" ]; then echo " Failed to get app_id for: $app_name"