From 34d845cbe8d4a9c94b81d2f62221c1be97b0c31f Mon Sep 17 00:00:00 2001 From: Kaushik Iska Date: Sun, 15 Oct 2023 12:28:47 -0400 Subject: [PATCH] fix fkey constraint --- nexus/catalog/migrations/V9__mirror_stats_rels.sql | 8 ++++++-- ui/prisma/schema.prisma | 6 ++++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/nexus/catalog/migrations/V9__mirror_stats_rels.sql b/nexus/catalog/migrations/V9__mirror_stats_rels.sql index aa41d348b1..e4b518c640 100644 --- a/nexus/catalog/migrations/V9__mirror_stats_rels.sql +++ b/nexus/catalog/migrations/V9__mirror_stats_rels.sql @@ -1,6 +1,10 @@ -- For the cdc_batches, set batch_id as the primary key ALTER TABLE peerdb_stats.cdc_batches -ADD CONSTRAINT pk_cdc_batches PRIMARY KEY (batch_id); +ADD COLUMN id SERIAL PRIMARY KEY; + +-- add unique constraint on flow_name and batch_id +ALTER TABLE peerdb_stats.cdc_batches +ADD CONSTRAINT uq_cdc_batches_flow_batch UNIQUE (flow_name, batch_id); -- add incrementing id column to cdc_batch_table, make this the primary key ALTER TABLE peerdb_stats.cdc_batch_table @@ -23,7 +27,7 @@ FOREIGN KEY (flow_name) REFERENCES peerdb_stats.cdc_flows(flow_name) ON DELETE C -- Composite foreign key for flow_name and batch_id in cdc_batch_table ALTER TABLE peerdb_stats.cdc_batch_table ADD CONSTRAINT fk_cdc_batch_table_flow_batch -FOREIGN KEY (batch_id) REFERENCES peerdb_stats.cdc_batches(batch_id) ON DELETE CASCADE; +FOREIGN KEY (flow_name, batch_id) REFERENCES peerdb_stats.cdc_batches(flow_name, batch_id) ON DELETE CASCADE; -- Foreign key for run_uuid in qrep_partitions ALTER TABLE peerdb_stats.qrep_partitions diff --git a/ui/prisma/schema.prisma b/ui/prisma/schema.prisma index cdeaf76e7e..a39ca5fe3f 100644 --- a/ui/prisma/schema.prisma +++ b/ui/prisma/schema.prisma @@ -71,23 +71,25 @@ model cdc_batch_table { num_rows BigInt metadata Json? id Int @id @default(autoincrement()) - cdc_batches cdc_batches @relation(fields: [batch_id], references: [batch_id], onDelete: Cascade, onUpdate: NoAction, map: "fk_cdc_batch_table_flow_batch") + cdc_batches cdc_batches @relation(fields: [flow_name, batch_id], references: [flow_name, batch_id], onDelete: Cascade, onUpdate: NoAction, map: "fk_cdc_batch_table_flow_batch") @@schema("peerdb_stats") } model cdc_batches { flow_name String - batch_id BigInt @id(map: "pk_cdc_batches") + batch_id BigInt rows_in_batch Int batch_start_lsn Decimal @db.Decimal batch_end_lsn Decimal @db.Decimal start_time DateTime @db.Timestamp(6) end_time DateTime? @db.Timestamp(6) metadata Json? + id Int @id @default(autoincrement()) cdc_batch_table cdc_batch_table[] cdc_flows cdc_flows @relation(fields: [flow_name], references: [flow_name], onDelete: Cascade, onUpdate: NoAction, map: "fk_cdc_batches_flow_name") + @@unique([flow_name, batch_id], map: "uq_cdc_batches_flow_batch") @@schema("peerdb_stats") }