feat: add partial unique index for single primary address per client

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-04-14 12:51:45 -04:00
parent 59dd418542
commit f90dba036f
4 changed files with 7106 additions and 1 deletions

View File

@@ -6,6 +6,7 @@ import {
numeric,
jsonb,
index,
uniqueIndex,
primaryKey,
} from 'drizzle-orm/pg-core';
import { sql } from 'drizzle-orm';
@@ -173,7 +174,13 @@ export const clientAddresses = pgTable(
createdAt: timestamp('created_at', { withTimezone: true }).notNull().defaultNow(),
updatedAt: timestamp('updated_at', { withTimezone: true }).notNull().defaultNow(),
},
(table) => [index('idx_ca_client').on(table.clientId), index('idx_ca_port').on(table.portId)],
(table) => [
index('idx_ca_client').on(table.clientId),
index('idx_ca_port').on(table.portId),
uniqueIndex('idx_ca_primary')
.on(table.clientId)
.where(sql`${table.isPrimary} = true`),
],
);
export type Client = typeof clients.$inferSelect;