Skip to content

feat: unify db helper test functions#318

Merged
robert-zaremba merged 11 commits intomasterfrom
robert/db-cleanup
Jan 23, 2026
Merged

feat: unify db helper test functions#318
robert-zaremba merged 11 commits intomasterfrom
robert/db-cleanup

Conversation

@robert-zaremba
Copy link
Contributor

@robert-zaremba robert-zaremba commented Jan 23, 2026

Summary by Sourcery

Unify database test helpers across indexer packages and clean up obsolete schema and test utilities.

New Features:

  • Introduce shared initDb and dropTables helpers in the lib test-helpers module for managing D1 database schema in tests.

Bug Fixes:

  • Remove the unused nbtc_withdrawal table from the initial BTC indexer schema and update documentation to reference nbtc_redeem_requests instead.

Enhancements:

  • Update BTC and Sui indexer tests to use the shared database helper functions for setup and teardown, and expose a cleanupDB helper from the BTC indexer test suite.

Documentation:

  • Correct the BTC indexer README to describe the current tables used for tracking nBTC state.

Signed-off-by: Robert Zaremba <robert@zaremba.ch>
@robert-zaremba robert-zaremba self-assigned this Jan 23, 2026
@robert-zaremba robert-zaremba requested a review from a team as a code owner January 23, 2026 11:37
Copilot AI review requested due to automatic review settings January 23, 2026 11:37
@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented Jan 23, 2026

Reviewer's Guide

Unifies database test helper logic across btcindexer and sui-indexer by centralizing migration application and table cleanup in a shared lib helper, updating tests to use it, and aligning schema/docs with current tables (removing nbtc_withdrawal).

Entity relationship diagram for updated nBTC test database schema

erDiagram
  nbtc_minting {
    TEXT id
    TEXT address_id
    INTEGER status
    INTEGER created_at
  }

  nbtc_redeem_requests {
    TEXT id
    TEXT sender
    INTEGER amount
    TEXT recipient
    INTEGER created_at
    INTEGER status
  }

  setups {
    INTEGER id
    TEXT config
  }

  btc_blocks {
    INTEGER height
    TEXT hash
    INTEGER created_at
  }

  nbtc_deposit_addresses {
    INTEGER id
    INTEGER setup_id
    TEXT address
  }

  nbtc_withdrawal {
    TEXT sui_tx_id
    TEXT sender
    INTEGER amount
    TEXT recipient
    TEXT note
    INTEGER sent_at
    TEXT btc_tx_id
    INTEGER status
  }

  nbtc_deposit_addresses ||--o{ nbtc_minting : records_deposits_for
  setups ||--o{ nbtc_deposit_addresses : provides_config_for

  nbtc_withdrawal ||--|| nbtc_redeem_requests : replaced_by_in_schema
Loading

File-Level Changes

Change Details Files
Introduce shared DB test helper for migrations and table cleanup and remove per-package db.test helpers.
  • Add MIGRATIONS_PATH constant pointing to btcindexer/db/migrations in lib test-helpers init_db module.
  • Define shared list of tables and a dropTables(db) helper that drops them in FK-safe order.
  • Expose initDb(db) wrapper that applies migrations using the shared migrations path.
  • Delete package-local db.test.ts helpers in btcindexer and sui-indexer.
packages/lib/src/test-helpers/init_db.ts
packages/btcindexer/src/db.test.ts
packages/sui-indexer/src/db.test.ts
Update btcindexer and sui-indexer tests to use the shared DB init/drop helpers and to add explicit DB cleanup in helper-based suites.
  • Replace local initDb imports in tests with imports from @gonative-cc/lib/test-helpers/init_db.
  • Replace inline afterEach drop-table logic with calls to shared dropTables helper using Miniflare D1 instance.
  • Extend TestIndexerHelper interface and setupTestIndexerSuite to expose a cleanupDB method that delegates to dropTables(db).
  • Simplify btcindexer.test.ts afterEach to rely on suite cleanup rather than custom DROP TABLE logic.
packages/btcindexer/src/btcindexer.helpers.test.ts
packages/btcindexer/src/storage.test.ts
packages/sui-indexer/src/storage.test.ts
packages/btcindexer/src/btcindexer.test.ts
Align schema and documentation with current table set by removing nbtc_withdrawal uses and references.
  • Remove nbtc_withdrawal table definition and related index and status comments from the initial schema migration.
  • Update btcindexer README to refer to nbtc_redeem_requests instead of nbtc_withdrawal as the second main state table.
  • Remove nbtc_withdrawal from any per-test table-drop lists that were inlined before centralization.
packages/btcindexer/db/migrations/0001_initial_schema.sql
packages/btcindexer/README.md
packages/btcindexer/src/storage.test.ts
packages/sui-indexer/src/storage.test.ts
packages/btcindexer/src/btcindexer.test.ts

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey - I've found 2 issues, and left some high level feedback:

  • In init_db.ts, initDb is declared async but doesn't await or return the applyMigrations promise, so callers may proceed before migrations complete; consider return applyMigrations(db, MIGRATIONS_PATH);.
  • In btcindexer.test.ts the per-test DB cleanup logic was removed but suite.cleanupDB() from setupTestIndexerSuite isn’t called in afterEach, which may allow state to leak between tests; consider invoking the shared cleanup there.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- In `init_db.ts`, `initDb` is declared `async` but doesn't `await` or return the `applyMigrations` promise, so callers may proceed before migrations complete; consider `return applyMigrations(db, MIGRATIONS_PATH);`.
- In `btcindexer.test.ts` the per-test DB cleanup logic was removed but `suite.cleanupDB()` from `setupTestIndexerSuite` isn’t called in `afterEach`, which may allow state to leak between tests; consider invoking the shared cleanup there.

## Individual Comments

### Comment 1
<location> `packages/lib/src/test-helpers/init_db.ts:40-41` </location>
<code_context>
-
-const MIGRATIONS_PATH = path.resolve(__dirname, "../db/migrations");
-
-export async function initDb(db: D1Database) {
-	await applyMigrations(db, MIGRATIONS_PATH);
-}
</code_context>

<issue_to_address>
**issue (bug_risk):** initDb does not await applyMigrations, which can make DB-dependent tests flaky

Because initDb is async but doesn’t return/await applyMigrations, callers that do `await initDb(db)` can continue before migrations finish, leading to flaky DB tests. Please return the promise (`return applyMigrations(db, MIGRATIONS_PATH);`) or explicitly `await` it so the schema is guaranteed to be ready before use.
</issue_to_address>

### Comment 2
<location> `packages/btcindexer/README.md:84` </location>
<code_context>
-The state is stored in the `nbtc_minting` and `nbtc_withdrawal` tables in the SQL (D1) database.
+The state is stored in the `nbtc_minting` and `nbtc_redeem_requests` tables in the SQL (D1) database.

 Bitcoin transaction can have one of the following status:

</code_context>

<issue_to_address>
**suggestion (typo):** Consider pluralizing "transaction" and "status" for correct grammar.

For example: "Bitcoin transactions can have one of the following statuses:".
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This pull request consolidates duplicate database helper test functions from individual packages into a shared library location. The PR removes the db.test.ts files from btcindexer and sui-indexer packages and centralizes initDb and dropTables functions in @gonative-cc/lib/test-helpers/init_db. Additionally, the PR removes the obsolete nbtc_withdrawal table from the database schema and updates related documentation.

Changes:

  • Centralized database test helper functions (initDb, dropTables) into @gonative-cc/lib/test-helpers/init_db
  • Removed duplicate db.test.ts files from btcindexer and sui-indexer packages
  • Removed obsolete nbtc_withdrawal table from database migration and updated README documentation

Reviewed changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
packages/lib/src/test-helpers/init_db.ts Added centralized initDb and dropTables functions with hardcoded migration path and table list
packages/sui-indexer/src/db.test.ts Removed - functionality moved to centralized location
packages/btcindexer/src/db.test.ts Removed - functionality moved to centralized location
packages/sui-indexer/src/storage.test.ts Updated imports and simplified afterEach to use centralized dropTables
packages/btcindexer/src/storage.test.ts Updated imports and simplified afterEach to use centralized dropTables
packages/btcindexer/src/btcindexer.test.ts Removed table dropping code from afterEach but missing call to cleanup function
packages/btcindexer/src/btcindexer.helpers.test.ts Added cleanupDB helper function to test interface that wraps dropTables
packages/btcindexer/db/migrations/0001_initial_schema.sql Removed obsolete nbtc_withdrawal table definition and related comments
packages/btcindexer/README.md Updated documentation to reference nbtc_redeem_requests instead of nbtc_withdrawal

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Signed-off-by: Robert Zaremba <robert@zaremba.ch>
Signed-off-by: Robert Zaremba <robert@zaremba.ch>
Signed-off-by: Robert Zaremba <robert@zaremba.ch>
Copy link
Contributor

@sczembor sczembor left a comment

Choose a reason for hiding this comment

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

short review

Signed-off-by: Robert Zaremba <robert@zaremba.ch>
Signed-off-by: Robert Zaremba <robert@zaremba.ch>
await initDb(db);

storage = new D1Storage(db);
indexerStorage = new D1Storage(db);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

bug - one instance of storage should be under test

Signed-off-by: Robert Zaremba <robert@zaremba.ch>
Signed-off-by: Robert Zaremba <robert@zaremba.ch>
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

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


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +27 to +29
const numCreate = cleanedMigration.match(/\bcreate\b/gi)?.length || 0;
assert(result.count > 0, "migrations execution failed");
assert(result.count === numCreate, "migrations execution failed");
Copy link

Copilot AI Jan 23, 2026

Choose a reason for hiding this comment

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

The assertion that result.count equals the number of CREATE statements may be fragile and depends on D1's internal behavior. Consider documenting this assumption more clearly or making the validation more robust. For example, you could verify the actual tables exist rather than relying on result.count, or add a comment explaining what result.count represents in the context of D1 DDL operations.

Suggested change
const numCreate = cleanedMigration.match(/\bcreate\b/gi)?.length || 0;
assert(result.count > 0, "migrations execution failed");
assert(result.count === numCreate, "migrations execution failed");
// D1's `result.count` reflects the number of statements executed. We only
// assert that some statements ran, rather than assuming a strict mapping
// to a derived count of CREATE statements, which would be fragile.
assert(result.count > 0, "migrations execution failed");

Copilot uses AI. Check for mistakes.
Copy link
Contributor

@sczembor sczembor left a comment

Choose a reason for hiding this comment

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

utACK

Signed-off-by: Robert Zaremba <robert@zaremba.ch>
Signed-off-by: Robert Zaremba <robert@zaremba.ch>
Signed-off-by: Robert Zaremba <robert@zaremba.ch>
@robert-zaremba robert-zaremba merged commit 73336bc into master Jan 23, 2026
11 of 12 checks passed
@robert-zaremba robert-zaremba deleted the robert/db-cleanup branch January 23, 2026 14:50
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.

3 participants