Skip to content

fix(satp-hermes): migrate all DBs on startup#4378

Open
mskrzypkows wants to merge 9 commits into
hyperledger-cacti:mainfrom
mskrzypkows:db_init_at_startup_pr
Open

fix(satp-hermes): migrate all DBs on startup#4378
mskrzypkows wants to merge 9 commits into
hyperledger-cacti:mainfrom
mskrzypkows:db_init_at_startup_pr

Conversation

@mskrzypkows

Copy link
Copy Markdown

The CLI only forwarded localRepository and remoteRepository to SATPGatewayConfig, leaving auditRepository and oracleLogRepository to fall back to a random UUID-named SQLite file. Those databases were never migrated, causing the gateway to crash on the first write to the audit or oracle log tables.

  • Validate and pass auditRepository and oracleLogRepository from the config JSON in the CLI, consistent with how local/remote repos are already handled
  • Extend createDBRepository() to run migrate.latest() across all four configured repositories instead of localRepository only
  • Add integration test asserting that startup() creates the expected tables (logs, remote-logs, audit_entries, oracle_logs) in all four databases

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes SATP Hermes gateway startup failures caused by un-migrated databases by ensuring all configured repositories (local, remote, audit, oracle) are validated, passed through the CLI, and migrated during gateway startup.

Changes:

  • CLI now validates and forwards auditRepository and oracleLogRepository from the JSON config into SATPGatewayConfig.
  • Gateway startup migration logic is extended from “local only” to include all configured repositories.
  • Integration coverage is added to assert that DB tables are created after startup() across the configured repositories.

Reviewed changes

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

File Description
packages/cactus-plugin-satp-hermes/src/test/typescript/integration/gateway/gateway-init-startup.test.ts Adds an integration test to verify that DB migrations create the expected tables on startup.
packages/cactus-plugin-satp-hermes/src/main/typescript/plugin-satp-hermes-gateway.ts Updates createDBRepository() to migrate more than just the local repository.
packages/cactus-plugin-satp-hermes/src/main/typescript/plugin-satp-hermes-gateway-cli.ts Validates and passes audit/oracle repository configs so they don’t fall back to unintended defaults.

Comment on lines 1255 to 1259
@@ -1257,15 +1257,25 @@ export class SATPGateway implements IPluginWebService, ICactusPlugin {
this.logger.info(
`${fnTag}: Created migration source: ${JSON.stringify(migrationSource)}`,
);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@mskrzypkows could you please address this comment as well?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Yes, I have a fix for it. Testing it and will push.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Based on the comment create better solution, calling repository.migrate for each repository from SATPGateway. This way we don't need additional connection to the DB.

@RafaelAPB RafaelAPB Jun 11, 2026

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.

Thank you @mskrzypkows. Looking better! Can you please make sure that:

  1. errors initializing each DB or its migration are propagated up and handled
  2. can you please add the DB cleanup to the onShutdown hook on the SATP gateway?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

done

mskrzypkows and others added 6 commits June 12, 2026 13:39
… on startup

The CLI only forwarded localRepository and remoteRepository to SATPGatewayConfig,
leaving auditRepository and oracleLogRepository to fall back to a random UUID-named
SQLite file. Those databases were never migrated, causing the gateway to crash on
the first write to the audit or oracle log tables.

- Validate and pass auditRepository and oracleLogRepository from the config JSON
  in the CLI, consistent with how local/remote repos are already handled
- Extend createDBRepository() to run migrate.latest() across all four configured
  repositories instead of localRepository only
- Add integration test asserting that startup() creates the expected tables
  (logs, remote-logs, audit_entries, oracle_logs) in all four databases

Assisted-by: anthropic:claude-sonnet-4.6
Signed-off-by: Maciej Skrzypkowski <mskr@gmx.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Signed-off-by: Maciej Skrzypkowski <mskr@gmx.com>
createDBRepository() created temporary Knex instances from config to
run migrate.latest(), then destroyed them. This caused three bugs:

- For SQLite :memory: databases each Knex() call opens a separate
  in-memory DB, so migrations ran on a throwaway connection while
  the repository's own connection stayed unmigrated.
- database.destroy() was not in a finally block, leaking connections
  when migrate.latest() threw.
- auditRepository had no default fallback, unlike localRepository.

Add migrate() to IRepository and implement it in all four concrete
repositories so createDBRepository() migrates the actual runtime
connections directly.

Assisted-by: anthropic:claude-sonnet-4.6
Signed-off-by: Maciej Skrzypkowski <mskr@gmx.com>
for the IPFSRemoteLogRepository

Signed-off-by: Maciej Skrzypkowski <mskr@gmx.com>
Signed-off-by: Maciej Skrzypkowski <mskr@gmx.com>
Signed-off-by: Maciej Skrzypkowski <mskr@gmx.com>
@mskrzypkows mskrzypkows force-pushed the db_init_at_startup_pr branch from f9869c8 to 8e0461d Compare June 12, 2026 11:41
db_init_at_startup_pr

Signed-off-by: Maciej Skrzypkowski <mskr@gmx.com>
@mskrzypkows mskrzypkows force-pushed the db_init_at_startup_pr branch from 7dbfd6b to 684170f Compare June 26, 2026 16:29
this.remoteRepository,
this.auditRepository,
this.oracleLogRepository,
].filter((r): r is NonNullable<typeof r> => r != null);

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.

Can you make the condition more explicit (on the filtering do the check if the database is non null and valid) instead of doing a non nullable type enforcement?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I moved explicit check into the loop.

Signed-off-by: Maciej Skrzypkowski <mskr@gmx.com>

@RafaelAPB RafaelAPB 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.

@mskrzypkows I mean what is on the suggestions rest LGTM

Signed-off-by: Maciej Skrzypkowski <mskr@gmx.com>
@mskrzypkows

Copy link
Copy Markdown
Author

Combined suggestions into one commit and added warning log.

@RafaelAPB

Copy link
Copy Markdown
Contributor

@mskrzypkows thank you once again. I think we are good to go. Please 1) rebase with main; 2) squash your commits into one.

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.

4 participants