Repository: juspay/hyperswitch
File: .github/scripts/validate_migrations.sh (Shell)

Code before:
      yq ".rule_patterns.$rule.pattern" "$RULES_FILE"
    fi
  done | paste -sd'|' -
)

# Extract warning rule patterns
WARNING_PATTERNS=$(
  for rule in $(yq ".folder_rules.${MIGRATION_FOLDER}.warnings[]" "$RULES_FILE" 2>/dev/null || echo ""); do
    if [[ -n "$rule" ]]; then
      yq ".rule_patterns.$rule.pattern" "$RULES_FILE"
    fi
  done | paste -sd'|' -
)

BREAKING=0
WARNINGS=0

while IFS= read -r file; do
  [[ -z "$file" ]] && continue

  # Check for breaking changes
  if grep -iEq "$BREAKING_PATTERNS" "$file" 2>/dev/null; then
    echo "BREAKING: $file"
    MATCH_COUNT=$(grep -icE "$BREAKING_PATTERNS" "$file" 2>/dev/null || echo "0")
    grep -inE "$BREAKING_PATTERNS" "$file" 2>/dev/null | sed 's/^/  /' || true
    BREAKING=$((BREAKING + MATCH_COUNT))
  fi

  # Check for warnings
  if grep -iEq "$WARNING_PATTERNS" "$file" 2>/dev/null; then
    echo "WARNING: $file"
    MATCH_COUNT=$(grep -icE "$WARNING_PATTERNS" "$file" 2>/dev/null || echo "0")
    grep -inE "$WARNING_PATTERNS" "$file" 2>/dev/null | sed 's/^/  /' || true
    WARNINGS=$((WARNINGS + MATCH_COUNT))
  fi
done <<< "$NEW_MIGRATIONS"

echo "breaking_changes=$BREAKING" >> "$GITHUB_OUTPUT"
echo "warnings=$WARNINGS" >> "$GITHUB_OUTPUT"

if [[ $BREAKING -gt 0 ]]; then
  echo "::error::Breaking changes detected in $MIGRATION_FOLDER database migrations."
  echo "::error::Found $BREAKING breaking change(s)."
  exit 1
fi

Review comment (suggestion):
Nit: Prefer to use the longer variant of the flags (for example, passed to the `grep` command) over the short ones.

Code after:
      yq ".rule_patterns.$rule.pattern" "$RULES_FILE"
    fi
  done | paste -sd'|' -
)

# Extract warning rule patterns
WARNING_PATTERNS=$(
  for rule in $(yq ".folder_rules.${MIGRATION_FOLDER}.warnings[]" "$RULES_FILE" 2>/dev/null || echo ""); do
    if [[ -n "$rule" ]]; then
      yq ".rule_patterns.$rule.pattern" "$RULES_FILE"
    fi
  done | paste -sd'|' -
)

BREAKING=0
WARNINGS=0

while IFS= read -r file; do
  [[ -z "$file" ]] && continue

  # Check for breaking changes
  if grep --ignore-case --extended-regexp --quiet "$BREAKING_PATTERNS" "$file" 2>/dev/null; then
    echo "BREAKING: $file"
    MATCH_COUNT=$(grep --ignore-case --count --extended-regexp "$BREAKING_PATTERNS" "$file" 2>/dev/null || echo "0")
    grep --ignore-case --line-number --extended-regexp "$BREAKING_PATTERNS" "$file" 2>/dev/null | sed 's/^/  /' || true
    BREAKING=$((BREAKING + MATCH_COUNT))
  fi

  # Check for warnings
  if grep --ignore-case --extended-regexp --quiet "$WARNING_PATTERNS" "$file" 2>/dev/null; then
    echo "WARNING: $file"
    MATCH_COUNT=$(grep --ignore-case --count --extended-regexp "$WARNING_PATTERNS" "$file" 2>/dev/null || echo "0")
    grep --ignore-case --line-number --extended-regexp "$WARNING_PATTERNS" "$file" 2>/dev/null | sed 's/^/  /' || true
    WARNINGS=$((WARNINGS + MATCH_COUNT))
  fi
done <<< "$NEW_MIGRATIONS"

echo "breaking_changes=$BREAKING" >> "$GITHUB_OUTPUT"
echo "warnings=$WARNINGS" >> "$GITHUB_OUTPUT"

if [[ $BREAKING -gt 0 ]]; then
  echo "::error::Breaking changes detected in $MIGRATION_FOLDER database migrations."
  echo "::error::Found $BREAKING breaking change(s)."
  exit 1
fi