Skip to content

Make Device.id the primary key - #94

Merged
adamshiervani merged 1 commit into
devfrom
device-primary-key
Nov 6, 2026
Merged

adamshiervani merged 1 commit into
devfrom
device-primary-key

Conversation

@adamshiervani

@adamshiervani adamshiervani commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor
  • Device had only unique indexes. Logical replication, as used by a live database move, rejects every UPDATE and DELETE on a table without a replica identity.
  • The migration turns the existing Device_id_key index into Device_pkey (PRIMARY KEY USING INDEX): no rebuild, no scan.
  • No code change. The old Prisma client works with the migrated table, so the migration can run before the deploy.

Checked on PostgreSQL 14: existing rows kept, no drift against schema.prisma, replicated UPDATE/DELETE work, duplicate ids still rejected. Unit (110) and e2e (87) pass with the old and the new Prisma client.


Note

Medium Risk
Schema migration on a core table: production deploy order and DB state matter, though the migration is designed to be low-impact (index reuse, no scan).

Overview
Promotes Device.id to the table primary key so PostgreSQL has a replica identity for logical replication (UPDATE/DELETE no longer rejected).

A new migration reuses the existing unique index (Device_id_key) with PRIMARY KEY USING INDEX, avoiding a rebuild or full table scan. schema.prisma replaces @unique on id with @id so the client matches the database.

No application code changes in this PR; deploy can run the migration before shipping the updated Prisma schema.

Reviewed by Cursor Bugbot for commit 4347f19. Bugbot is set up for automated code reviews on this repo. Configure here.

Device had only unique indexes, so logical replication (a database
move with live migration) rejects every UPDATE and DELETE on it. The
migration reuses the existing unique index on id as the primary key:
no table rewrite and no scan, since the column is already NOT NULL.
Code that runs on the old Prisma client works with the migrated table.
@adamshiervani
adamshiervani merged commit 41a069b into dev Nov 6, 2026
3 checks passed
adamshiervani added a commit that referenced this pull request Nov 6, 2026
- #94: Device.id becomes the primary key, so a live database move (logical replication) can replicate updates and deletes on Device. The migration was applied to the production database before this merge.
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.

1 participant