Skip to content

fix(tests): drop throwaway MySQL schemas after integration suites - #21106

Open
vbudhram wants to merge 1 commit into
mainfrom
fxa-14412
Open

fix(tests): drop throwaway MySQL schemas after integration suites#21106
vbudhram wants to merge 1 commit into
mainfrom
fxa-14412

Conversation

@vbudhram

@vbudhram vbudhram commented Aug 25, 2026

Copy link
Copy Markdown
Contributor

Because

  • testAccountDatabaseSetup creates a testAccount-<uuid> schema for each spec file and never drops it. Closing the pool does not drop a schema.
  • A local MySQL gains one schema per spec file per run. One developer machine held 184 of them before anyone noticed. CI is not affected.

This pull request

  • Adds testAccountDatabaseTeardown(db?) to tests.ts and exports it. The helper reads its own schema with SELECT DATABASE(). It drops that schema only when the name starts with testAccount-, then closes the pool in a finally.
  • Calls the teardown from afterAll in the 12 .in.spec.ts files that call the setup. Jest runs afterAll for failing runs too, so those runs also clean up.
  • Sweeps testAccount-* schemas older than 3 hours at the start of the setup, from information_schema.TABLES.CREATE_TIME. This covers a crash or a Ctrl-C, where no hook runs. The sweep is age based, so a concurrent run keeps its schema.
  • Moves the hook in backup-code.manager.in.spec.ts to afterEach. That file calls the setup in beforeEach, so afterAll still left four schemas per run.
  • Adds tests.spec.ts, a unit spec for the testAccount- guard. It mocks the kysely sql tag, so the spec needs no MySQL.

Issue that this pull request solves

Closes: https://mozilla-hub.atlassian.net/browse/FXA-14412

Checklist

Put an x in the boxes that apply

  • My commit is GPG signed.
  • If applicable, I have modified or added tests which pass locally.
  • I have added necessary documentation (if appropriate).
  • I have verified that my changes render correctly in RTL (if appropriate).
  • I have manually reviewed all AI generated code.

How to review (Optional)

  • Key files/areas to focus on: the two DROP DATABASE paths in tests.ts. Both check the testAccount- prefix.
  • Suggested review order: tests.ts, then tests.spec.ts, then the spec file hooks.
  • Risky or complex parts: the orphan sweep drops schemas that it did not create.

Screenshots (Optional)

Other information (Optional)

Round 2 reorders the afterAll in recovery-phone.manager.in.spec.ts. The Redis cleanup ran first. A Redis failure therefore skipped the teardown and leaked the schema and the pool. The database teardown now runs first. The other 11 files already call it first.

nx test-unit shared-db-mysql-account: 8 passed, 0 failed.
nx test-integration accounts-recovery-phone: 16 passed, 0 failed. No testAccount-% schema was left behind.

@vbudhram
vbudhram requested a review from a team as a code owner August 25, 2026 18:13
Copilot AI balanced review requested due to automatic review settings August 25, 2026 18:13
@vbudhram vbudhram added the auto label Aug 25, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds lifecycle cleanup for temporary MySQL schemas used by integration tests.

Changes:

  • Adds schema teardown and orphan sweeping.
  • Exports the teardown helper.
  • Updates 12 integration suites to drop temporary schemas.

Reviewed changes

Copilot reviewed 14 out of 14 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
libs/shared/db/mysql/account/src/lib/tests.ts Adds schema cleanup logic.
libs/shared/db/mysql/account/src/index.ts Exports teardown helper.
libs/shared/account/account/src/lib/account.manager.in.spec.ts Uses teardown helper.
libs/payments/stripe/src/lib/accountCustomer/accountCustomer.repository.in.spec.ts Uses teardown helper.
libs/payments/stripe/src/lib/accountCustomer/accountCustomer.manager.in.spec.ts Uses teardown helper.
libs/payments/paypal/src/lib/paypalCustomer/paypalCustomer.repository.in.spec.ts Uses teardown helper.
libs/payments/paypal/src/lib/paypalCustomer/paypalCustomer.manager.in.spec.ts Uses teardown helper.
libs/payments/cart/src/lib/checkout.service.in.spec.ts Uses teardown helper.
libs/payments/cart/src/lib/cart.manager.in.spec.ts Uses teardown helper.
libs/accounts/two-factor/src/lib/backup-code.manager.in.spec.ts Cleans each per-test schema.
libs/accounts/recovery-phone/src/lib/recovery-phone.manager.in.spec.ts Uses teardown helper.
libs/accounts/passkey/src/lib/passkey.security.in.spec.ts Uses teardown helper.
libs/accounts/passkey/src/lib/passkey.repository.in.spec.ts Uses teardown helper.
libs/accounts/passkey/src/lib/passkey.manager.in.spec.ts Uses teardown helper.

💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +113 to +115
HAVING MAX(CREATE_TIME) < NOW() - INTERVAL ${sql.lit(
ORPHAN_SCHEMA_MAX_AGE_HOURS
)} HOUR
afterAll(async () => {
await clearRedisSmsKeys();
await db.destroy();
await testAccountDatabaseTeardown(db);
Comment on lines +79 to +83
try {
const name = await currentSchema(db);
if (name?.startsWith(TEST_SCHEMA_PREFIX)) {
await sql`DROP DATABASE IF EXISTS ${sql.table(name)}`.execute(db);
}
## Because

- `testAccountDatabaseSetup` creates a `testAccount-<uuid>` schema for each spec file and never drops it. Closing the pool does not drop a schema.
- A local MySQL gains one schema per spec file per run. One developer machine held 184 of them before anyone noticed. CI is not affected.

## This pull request

- Adds `testAccountDatabaseTeardown(db?)` to `tests.ts` and exports it. The helper reads its own schema with `SELECT DATABASE()`. It drops that schema only when the name starts with `testAccount-`, then closes the pool in a `finally`.
- Calls the teardown from `afterAll` in the 12 `.in.spec.ts` files that call the setup. Jest runs `afterAll` for failing runs too, so those runs also clean up.
- Sweeps `testAccount-*` schemas older than 3 hours at the start of the setup, from `information_schema.TABLES.CREATE_TIME`. This covers a crash or a Ctrl-C, where no hook runs. The sweep is age based, so a concurrent run keeps its schema.
- Moves the hook in `backup-code.manager.in.spec.ts` to `afterEach`. That file calls the setup in `beforeEach`, so `afterAll` still left four schemas per run.
- Adds `tests.spec.ts`, a unit spec for the `testAccount-` guard. It mocks the `kysely` `sql` tag, so the spec needs no MySQL.

## Issue that this pull request solves

Closes: https://mozilla-hub.atlassian.net/browse/FXA-14412
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants