Skip to content

Defer TST database cleanup until after production deploy - #305

Open
fmercadop wants to merge 6 commits into
mainfrom
infra/tst-cleanup-on-deploy-v2
Open

fmercadop wants to merge 6 commits into
mainfrom
infra/tst-cleanup-on-deploy-v2

Conversation

@fmercadop

@fmercadop fmercadop commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Feature PR databases (BALBOA_PR_<id>) now survive merge into a release branch and are dropped by the deploy pipeline (targeted cleanup + sweep) once the release actually ships, so QA can keep querying them during the release window. The PR-close pipeline (15) now only drops a PR's database when it was abandoned, not when it was merged.

This is a port of the same logic already proposed in the Azure DevOps PR for our production repo (infra/cleanup-tst-on-deploy) -- same rules, adapted to this repo's branch convention (feature/, release/, hotfix/, infra/), database naming (<base>_PR_<id>), and the GitHub REST API.

Status: fully tested end-to-end in this repo's real pipeline. Ready for review -- not merged yet, pending approval.

Equivalence with the Azure DevOps PR

This section exists so approving this PR and the Azure PR can be treated as approving the same change twice, not two different changes. Verified by reading both branches' current content side by side, not from memory.

Identical decision logic, confirmed line-by-line:

  • Pipeline 15 (PR close): drop only on abandonment, never on merge. Azure checks the webhook's PR status; this repo reads GitHub's native pull_request.merged field -- different mechanism, same outcome, and this repo's version is simpler because GitHub's event already carries merged: true/false directly (Azure's generic "PR updated" webhook doesn't, which is why that side needs an extra status-check step).
  • Pipeline 30 targeted cleanup: look up the PR for the deploy's merge commit, check its source is a release branch, list the merged feature PRs into it, drop their databases. Same algorithm on both sides, paginated the same way (100 per page).
  • Sweep: the exact same 5 release rules, evaluated in the exact same order, with the exact same default thresholds (15 / 15 / 45 days). Only the per-platform PR-status vocabulary differs (Azure: active/completed/ abandoned; GitHub: open/merged/abandoned) -- same meaning, each platform's own terms.
  • Same safety design on both sides: every cleanup step is non-blocking (continueOnError / continue-on-error), placed after the version-bump push, and never fails the deploy; anything left behind is retried on the next deploy.

Differences, and why each one is expected, not a divergence:

Azure DevOps balboa (GitHub)
Branch prefixes fb/, rb/, hf/, infra/ feature/, release/, hotfix/, infra/
Database naming TST_EDP__PR<id> (hardcoded literal) <base>_PR_<id> (derived from target.database, generic)
API Azure DevOps REST API, PAT auth GitHub REST API, token auth, different endpoints for the same lookups
Output variables ##vso[task.setvariable...] (stdout) $GITHUB_OUTPUT (file) -- each platform's own mechanism

Two of those are deliberate improvements made during this port, not just translation, and are worth flagging explicitly:

  1. The database-name pattern (show_pr_databases macro) is generic (target.database ~ '_PR_%') instead of the Azure side's hardcoded TST_EDP__PR% literal, and the excluded CI user defaults to target.user instead of a hardcoded SVC_AZURE_DEVOPS. Neither changes any drop/keep decision -- they only remove two environment-specific literals. These could optionally be backported to the Azure PR too if full byte-level symmetry is wanted, but they're not required for the behavior to match.
  2. A real bug was found and fixed here: this repo's deploy container's shell doesn't support the bash here-strings/arrays both the Azure and original GitHub scripts used (IFS=',' read -ra ... <<< "$IDS"), which silently broke the drop loops (caught by continue-on-error, so the deploy was never affected, but the actual database drop didn't happen). Fixed here with a POSIX-safe loop. I have not verified whether Azure Pipelines' hosted agents have the same issue -- Claude only read that code, it did not execute it there. Worth a specific check when reviewing the Azure PR, since it has the identical shell pattern.

What was tested, and how

  • 23 unit tests (automate/git/tests/), covering the full sweep decision table, pagination, a GitHub API query quirk, 404 handling, and invalid-config handling.
  • A real, live rehearsal against this repo's actual GitHub PRs and Snowflake databases:
    • A merged feature PR correctly keeps its database; an abandoned one still gets dropped immediately (pipeline 15).
    • The sweep correctly drops a database whose release was abandoned, and correctly keeps one whose release still has an open PR to main.
    • The real QA clone -> build -> swap -> drop sequence was let run to full completion and verified.
  • Two full real production deploys exercising the new pipeline-30 steps:
    • One where the deploying branch was not a release (targeted cleanup correctly no-ops; the sweep handles any stray databases).
    • One where a real feature PR had been merged into the release being deployed: the targeted-cleanup step found that PR via the GitHub API and dropped exactly its database, with the sweep correctly finding nothing left to do afterward.
  • The POSIX-shell bug above was found and fixed during this process, then verified under both sh and bash before merging the fix.

Everything above was done on main for real and has since been reverted (this repo was only meant to validate the logic, not keep it applied) -- see the revert PR (#304) for the full trail if useful. This PR reapplies the final, validated code cleanly on top of current main.

Next step

Once this is approved, the Azure DevOps PR with the equivalent logic for our production repo can be reviewed and merged.

Fernando Mercado added 3 commits November 2, 2026 14:31
Feature PR databases now survive merge into a release branch and are
dropped by the deploy pipeline (targeted cleanup + sweep) once the
release actually ships, so QA can keep querying them during the
release window. The PR-close pipeline now only drops on abandonment.

Ported from the Azure DevOps side (infra/cleanup-tst-on-deploy), adapted
to balboa's feature/release branch convention, <base>_PR_<id> database
naming, and the GitHub REST API. Covered by a new pytest suite under
automate/git/tests (23 cases, mocked GitHub API).
The deploy container's run: shell doesn't support bash here-strings
(<<<) or read -ra arrays -- confirmed live: the real pipeline 30 run
for PR #299 hit "sh: Syntax error: redirection unexpected" and exit
code 2 on the sweep's drop loop. continue-on-error kept the deploy
green, but BALBOA_PR_295 was never actually dropped.

Replaced with a POSIX-safe comma-split via IFS word-splitting, which
works identically under sh and bash. Reproduced the exact failure and
verified the fix under both shells before committing.
"TST" was carried over literally from the Azure DevOps source, where it
is part of the real database name (TST_EDP__PR<id>). In balboa the real
database is <base>_PR_<id> -- there is no "TST" anywhere in the actual
name. balboa's own pre-existing code already calls this concept "PR
database" (grant_access_to_pr_database.sql, "Create PR database", etc.);
this aligns the new code with that convention.

Renamed files, macro name, env vars (TST_* -> PR_*), step names/ids, and
comments/docstrings. No behavior change -- confirmed by the full test
suite (23/23 passing) and macro syntax check.
@github-actions

github-actions Bot commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

🧪 dbt Workflow Status

  • Branch Validation: success
  • Deploy Marker Management: skipped
  • dbt Validations: success

✅ All dbt validations passed

Fernando Mercado and others added 2 commits November 2, 2026 14:33
workflow_dispatch runs have no github.event.number, so the database name
resolved to <DB>_PR_ and the drop targeted nothing useful. Manual runs now
require a pr_number input, used as a fallback for the PR number.

A validation step rejects empty or non-numeric PR numbers before the drop.
It uses a POSIX `case` rather than bash `[[ =~ ]]` because this container's
run: shell is sh (see 3dc4174). The drop step now reads the database name
from the shell environment instead of interpolating ${{ }} into the script,
and checkout falls back to github.sha when there is no pull_request.
@github-actions

github-actions Bot commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

🧪 dbt Workflow Status

  • Branch Validation: success
  • Deploy Marker Management: skipped
  • dbt Validations: success

✅ All dbt validations passed

@fmercadop fmercadop self-assigned this Nov 12, 2026
@fmercadop
fmercadop requested a review from noel November 12, 2026 19:05

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant