Skip to content

chores: updated - #528

Open
KodeSage wants to merge 1 commit into
codebestia:mainfrom
KodeSage:feat/devices_prekeys
Open

chores: updated#528
KodeSage wants to merge 1 commit into
codebestia:mainfrom
KodeSage:feat/devices_prekeys

Conversation

@KodeSage

Copy link
Copy Markdown
Contributor

Description

Locks in the schema-level guarantees of the device_prekeys table (signed + one-time prekeys in one table, discriminated by key_type) with regression coverage.

The table itself already landed with the unified prekey work: apps/backend/src/db/schema.ts defines every column, index, and constraint the issue calls for, and apps/backend/drizzle/0000_stale_mandarin.sql migrates it. Verified against the issue's acceptance criteria:

Requirement Enforced by
id uuid PK, keyId integer, publicKey text, signature nullable, consumed boolean default false, createdAt column definitions
keyType pgEnum (signed / one_time) prekey_type enum
deviceId uuid FK, cascade delete device_prekeys_device_id_devices_id_fk
Unique (deviceId, keyType, keyId) device_prekeys_device_type_keyid_idx
Partial index on (deviceId) WHERE key_type='one_time' AND consumed=false device_prekeys_one_time_available_idx
Exactly one active signed prekey per device partial unique device_prekeys_signed_device_idx WHERE key_type='signed'
Signed prekey rows require a non-null signature DB check device_prekeys_signed_requires_signature
Consuming a one-time prekey flips consumed=true atomically SELECT … FOR UPDATE SKIP LOCKED inside a transaction, routes/users.ts

The gap this PR closes: nothing pinned those invariants. Every one of them lives in DDL rather than route code, so an edit to schema.ts could silently drop the check constraint or a partial index and no test would fail.

Changes:

  1. New devicePrekeys.schema.test.ts — 15 tests asserting the table shape and invariants against the live Drizzle definition via getTableConfig. Partial-index and check-constraint predicates are serialized through PgDialect and compared as SQL text, so weakening a predicate fails the build. A final block scans the generated migration DDL, which catches the case where a constraint exists in TypeScript but was never generated into a migration and so never reaches Postgres.
  2. Tightened the atomic-consume assertion in users.bundle.test.ts — it previously only asserted that some update ran, which would still pass if the row lock or SKIP LOCKED were removed. It now asserts the transaction, for('update', { skipLocked: true }), and set({ consumed: true }) specifically, pinning both the atomicity and the flip-don't-delete behaviour that keeps bundle-fetch history auditable.

Note on the FK target: the issue specifies deviceId → user_devices.id. It points at devices.id instead, which is correct rather than a deviation — user_devices was merged into devices, now the single canonical device registry that messages.senderDeviceId, messageEnvelopes.recipientDeviceId, and pushSubscriptions.deviceId all reference. Left as is.

Type of change

  • Bug fix
  • New feature
  • Documentation update
  • Other — test coverage (no production code changed; the diff is test files only)

Issue numbers

Fixes #154

Testing

  • Full backend suite green: 413 tests across 42 files.
  • The new tests were checked against a negative control — temporarily deleting the signed-prekey partial unique index and the check constraint from schema.ts failed exactly the three relevant tests (allows exactly one signed prekey per device, allows many one-time prekeys per device, requires a signature on signed prekey rows), confirming the assertions are not vacuous. schema.ts was then restored and is unmodified in this PR.
  • ESLint and Prettier clean on both touched files.
  • tsc --noEmit reports 9 errors, all pre-existing on main (in roomManager, uploads, stellarListener) and none in the files touched here.

Checklist

  • I have read the contributing guidelines
  • I have tested my changes locally
  • My code follows the project's coding standards

@drips-wave

drips-wave Bot commented Jul 30, 2026

Copy link
Copy Markdown

@KodeSage 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

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.

Add device_prekeys schema (signed prekey + one-time prekeys)

1 participant