Skip to content

feat(exports): persist daily export records and claims in Postgres - #1664

Merged
greatest0fallt1me merged 2 commits into
CalloraOrg:mainfrom
salienne:feat/postgres-export-store-1557
Nov 24, 2026
Merged

greatest0fallt1me merged 2 commits into
CalloraOrg:mainfrom
salienne:feat/postgres-export-store-1557

Conversation

@salienne

Copy link
Copy Markdown
Contributor

Summary

ReportExporterService used InMemoryExportStore in every deployment. Export history was lost on restart, and two instances could export the same developer and day. This PR adds PostgresExportStore on the existing developer_exports table, plus a new developer_export_claims table for atomic daily claims.

  • PostgresExportStore implements DeveloperExportStore. save upserts on (developer_id, s3_key). listByDeveloper keeps the expiry filter, the format filter, the (exported_at DESC, id DESC) keyset cursor, and the rule that a cursor takes precedence over offset.
  • claimDay is one INSERT ... ON CONFLICT DO UPDATE ... WHERE statement. A claimed day is always refused. A completed day is refused unless forced. completeDay and releaseDay match the in-memory semantics.
  • Migration 0033_developer_export_claims adds the claims table and a unique index on developer_exports (developer_id, s3_key), with a down migration.
  • createAppExports uses PostgresExportStore by default, and InMemoryExportStore in demo mode and under NODE_ENV=test.

What breaks if this is wrong

  • A wrong claimDay could let two instances export the same day, or block a day that should export. The SQL decides the transition in one statement, so the guard is the single point to check.
  • A wrong timestamp comparison would hide or repeat export records on the list endpoint. Timestamps are fixed-width ISO text compared with COLLATE "C".
  • Standard deployments must apply migration 0033 before exports are enabled. Without the table, the first export fails.

Deployment and rollback

  • Apply 0033_developer_export_claims.sql before enabling exports.
  • Roll back with 0033_developer_export_claims.down.sql. It drops the claims table and the unique index, so claim state is lost. The file carries destructive-approved: #1557.

Verification

Not run. This environment has no clone, no Node install, and no database.

  • Mocked SQL tests in postgresExportStore.test.ts check the statements, bound parameters, and result mapping.
  • exportStoreContract.test.ts runs the same behaviour table against InMemoryExportStore. It runs against PostgresExportStore only when DATABASE_URL is set. That is the only check of the concurrency guarantee, and it has not run.
  • Not run: npm test, npm run typecheck, npm run db:check-migrations, lint.

Acceptance criteria

  • After a restart, GET /api/exports lists previously saved records. Not verified against a database.
  • Two concurrent claimDay calls: exactly one returns true. Covered by the contract test, which needs DATABASE_URL. Not run.
  • save of the same developer and s3Key replaces the row. Covered by the in-memory contract and the SQL upsert.
  • The same behavioural table passes against both stores. The table is written. The Postgres run has not happened.

Not in this PR

  • The issue's README note. The export behaviour is documented in docs/scheduled-exports.md instead.

Closes #1557

- Add PostgresExportStore, a DeveloperExportStore on developer_exports and a new developer_export_claims table.
  Reason: ReportExporterService used InMemoryExportStore in every deployment, so a restart lost export history and two instances could export the same developer day.
  Consequence: outside demo mode and tests, export records and daily claims are stored in Postgres.
- claimDay is one INSERT ... ON CONFLICT statement. A claimed day is always refused, and a completed day is refused unless forced.
  Consequence: two concurrent claims cannot both succeed. This depends on Postgres row locking and is not yet verified against a live database.
- Add migration 0033 with a primary key on (developer_id, day) for claims and a unique index on developer_exports (developer_id, s3_key), with a down migration.
  Consequence: re-saving the same artifact replaces its row instead of duplicating it. The down migration drops the claims table and the index, and carries the destructive-approved marker for CalloraOrg#1557.
- createAppExports defaults to PostgresExportStore, and to InMemoryExportStore in demo mode and under NODE_ENV=test.
  Consequence: existing callers are unchanged. Standard deployments must apply migration 0033 before enabling exports.
- Add mocked SQL tests for PostgresExportStore, and a behaviour contract that runs against the in-memory store and against Postgres when DATABASE_URL is set.
  Consequence: the Postgres contract is skipped unless DATABASE_URL is provided. Typecheck, lint and jest have not been run.
- Document the store in docs/scheduled-exports.md and add a CHANGELOG entry.

Closes CalloraOrg#1557
@drips-wave

drips-wave Bot commented Nov 24, 2026

Copy link
Copy Markdown

@salienne Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

@greatest0fallt1me
greatest0fallt1me merged commit e0f5041 into CalloraOrg:main Nov 24, 2026
1 check passed
@greatest0fallt1me

Copy link
Copy Markdown
Contributor

Thanks @salienne! We resolved the merge conflicts on our side and merged this. 🎉

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.

Back ReportExporterService with a Postgres DeveloperExportStore on the developer_exports table

2 participants