Added index to ensure combinations of uprn and portfolio id remain unique

This commit is contained in:
Khalim Conn-Kowlessar 2025-12-21 12:36:49 +00:00
parent ee022b83c4
commit 4a899c27dc
4 changed files with 4906 additions and 29 deletions

View file

@ -0,0 +1 @@
CREATE UNIQUE INDEX "uq_property_portfolio_uprn" ON "property" USING btree ("portfolio_id","uprn") WHERE "property"."uprn" IS NOT NULL;

File diff suppressed because it is too large Load diff

View file

@ -967,6 +967,13 @@
"when": 1765400667595,
"tag": "0137_shallow_speedball",
"breakpoints": true
},
{
"idx": 138,
"version": "7",
"when": 1766319125106,
"tag": "0138_neat_havok",
"breakpoints": true
}
]
}

View file

@ -9,10 +9,12 @@ import {
boolean,
smallint,
bigint,
uniqueIndex,
} from "drizzle-orm/pg-core";
import { portfolio, PortfolioStatus } from "./portfolio";
import { InferModel } from "drizzle-orm";
import { materialTypeEnum } from "./materials";
import { sql } from "drizzle-orm";
// This is a placeholder for the property schema
export interface PropertyMeta {
@ -91,35 +93,52 @@ export const propertyCreationStatusEnum = pgEnum(
export const epcEnum = pgEnum("epc", Epc);
export const propertyStatusEnum = pgEnum("status", PortfolioStatus);
export const property = pgTable("property", {
id: bigserial("id", { mode: "bigint" }).primaryKey(),
portfolioId: bigint("portfolio_id", { mode: "bigint" })
.notNull()
.references(() => portfolio.id),
creationStatus: propertyCreationStatusEnum("creation_status").notNull(),
uprn: bigint("uprn", { mode: "bigint" }),
landlordPropertyId: text("landlord_property_id"), // Optional ID used by landlords
buildingReferenceNumber: bigint("building_reference_number", {
mode: "bigint",
}),
status: propertyStatusEnum("status"),
address: text("address"),
postcode: text("postcode"),
hasPreConditionReport: boolean("has_pre_condition_report"),
hasRecommendations: boolean("has_recommendations"),
createdAt: timestamp("created_at").notNull().defaultNow(),
updatedAt: timestamp("updated_at").notNull().defaultNow(),
propertyType: text("property_type"),
builtForm: text("built_form"),
localAuthority: text("local_authority"),
constituency: text("constituency"),
numberOfRooms: integer("number_of_rooms"),
yearBuilt: text("year_built"),
tenure: text("tenure"),
currentEpcRating: epcEnum("current_epc_rating"),
currentSapPoints: real("current_sap_points"),
currentValuation: real("current_valuation"),
});
export const property = pgTable(
"property",
{
id: bigserial("id", { mode: "bigint" }).primaryKey(),
portfolioId: bigint("portfolio_id", { mode: "bigint" })
.notNull()
.references(() => portfolio.id),
creationStatus: propertyCreationStatusEnum("creation_status").notNull(),
uprn: bigint("uprn", { mode: "bigint" }),
landlordPropertyId: text("landlord_property_id"),
buildingReferenceNumber: bigint("building_reference_number", {
mode: "bigint",
}),
status: propertyStatusEnum("status"),
address: text("address"),
postcode: text("postcode"),
hasPreConditionReport: boolean("has_pre_condition_report"),
hasRecommendations: boolean("has_recommendations"),
createdAt: timestamp("created_at").notNull().defaultNow(),
updatedAt: timestamp("updated_at").notNull().defaultNow(),
propertyType: text("property_type"),
builtForm: text("built_form"),
localAuthority: text("local_authority"),
constituency: text("constituency"),
numberOfRooms: integer("number_of_rooms"),
yearBuilt: text("year_built"),
tenure: text("tenure"),
currentEpcRating: epcEnum("current_epc_rating"),
currentSapPoints: real("current_sap_points"),
currentValuation: real("current_valuation"),
},
(table) => [
uniqueIndex("uq_property_portfolio_uprn")
.on(table.portfolioId, table.uprn)
.where(sql`${table.uprn} IS NOT NULL`),
]
);
export const FeatureRating: [string, ...string[]] = [
"Very good",