diff --git a/.github/ISSUE_TEMPLATE/bug_report.yml b/.github/ISSUE_TEMPLATE/bug_report.yml index 71df4cf..bf29113 100644 --- a/.github/ISSUE_TEMPLATE/bug_report.yml +++ b/.github/ISSUE_TEMPLATE/bug_report.yml @@ -16,7 +16,7 @@ body: attributes: label: ALPR Community version description: Find this under Settings → Release. Include the source revision there when reporting an installation or update problem. - placeholder: v0.1.59 + placeholder: v0.1.60 validations: required: true diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 3b568a3..60c5913 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -116,6 +116,13 @@ jobs: ALPR_AXIS_TEST_SERVER_URL: postgresql://postgres:fixture-test-password@127.0.0.1:5432/postgres run: yarn test:camera-inputs:postgres + - name: Verify tracked upgrades, Blue Iris bindings and read-only display access + env: + ALPR_COMMUNITY_TEST_SERVER_URL: postgresql://postgres:fixture-test-password@127.0.0.1:5432/postgres + run: | + git fetch --no-tags origin tag v0.1.42 tag v0.1.55 tag v0.1.58 tag v0.1.59 + yarn test:community-bugfixes:postgres + - name: Exercise load, no-op, guarded clear, and refusal paths shell: bash run: | @@ -187,6 +194,7 @@ jobs: VALUES ('vehicle-image-asset-cleanup:v1', 'ci-disposable-pg17'), ('vehicle-image-asset-campaign:v1', 'ci-disposable-pg17'), + ('overview-export:v1', 'ci-disposable-pg17'), ('vehicle-event-shadow:v1', 'ci-disposable-pg17'); INSERT INTO public.codex_integration_test_guard (scope, guard_token) VALUES ('vehicle-image-crop:v1', 'ci-disposable-pg17'); @@ -198,7 +206,10 @@ jobs: DATABASE_URL='postgresql://postgres:fixture-test-password@127.0.0.1:5432/fixture_test' \ yarn test:vehicle-image-asset-cleanup:postgres || pg_test_failed=1 - DATABASE_URL='postgresql://postgres:fixture-test-password@127.0.0.1:5432/fixture_test' \ + OVERVIEW_EXPORT_POSTGRES_TEST_OPT_IN='true' \ + OVERVIEW_EXPORT_POSTGRES_TEST_DATABASE='fixture_test' \ + OVERVIEW_EXPORT_POSTGRES_TEST_GUARD_TOKEN='ci-disposable-pg17' \ + DATABASE_URL='postgresql://postgres:fixture-test-password@127.0.0.1:5432/fixture_test' \ yarn test:overview-export:postgres || pg_test_failed=1 VEHICLE_IMAGE_ASSET_POSTGRES_TEST_OPT_IN='true' \ diff --git a/CHANGELOG.md b/CHANGELOG.md index 100e17b..cb7cd92 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,13 @@ # Changelog +## 0.1.60 — 2026-11-02 + +- Track ordered SQL migrations with verified checksums and legacy completion markers. Preserve the single-file updater entry point, atomic application, count validation and rollback. Completed cleanups no longer replay; Blue Iris v2 events remain visible and preserved. +- Resolve Blue Iris display names and short IDs through persisted camera inventory and explicit LPR recording bindings. Refresh camera discovery in settings, expose separate alert/read tolerances and require an explicit owner when several ALPR reads match. +- Replace the inactive Home Assistant IP-bypass setting with revocable, expiring read-only displays, optional camera scope and exact parent-origin restrictions. Administrative access continues to require its existing login. +- Update User Guide 3.28, current installation/update examples and Home Assistant access guidance. Existing installations use the normal updater; no manual database edits or intermediate release are required. +- Add disposable PostgreSQL upgrade fixtures for v0.1.42, v0.1.55, v0.1.58, v0.1.59 and fresh installs, plus production browser checks for Home Assistant and Blue Iris setup. + ## 0.1.59 — 2026-11-01 - Add configurable AXIS License Plate Verifier inputs through HTTP push, MQTT, or both under Settings > Integrations > Camera Inputs. Existing Blue Iris cameras continue automatically. diff --git a/app/actions.js b/app/actions.js index d1fe2fc..7504d14 100644 --- a/app/actions.js +++ b/app/actions.js @@ -115,6 +115,8 @@ import { revalidatePath, revalidateTag, unstable_noStore } from "next/cache"; import { cookies, headers } from "next/headers"; import { redirect } from "next/navigation"; import crypto from "crypto"; +import { HomeAssistantDisplayService } from "@/lib/home-assistant-display.mjs"; +import { readMatchTolerance } from "@/lib/blue-iris-camera-binding.mjs"; import { getConfig, saveConfig } from "@/lib/settings"; import { environmentManagedSettings, @@ -1756,6 +1758,7 @@ export async function testBlueIrisConnection() { try { const config = await getConfig(); const result = await new BlueIrisClient(config.blueiris).testConnection(); + await new BlueIrisVehicleFrameRepository(await getPool()).syncBlueIrisCameraInventory(result.cameras); return { success: true, ...result }; } catch (error) { return { @@ -1774,7 +1777,13 @@ export async function previewBlueIrisAlertMatch(input = {}) { timestamp: input.timestamp, toleranceSeconds: input.toleranceSeconds, }); - return { success: true, ...result }; + const readToleranceSeconds = readMatchTolerance(input.readToleranceSeconds); + const reads = await new BlueIrisVehicleFrameRepository(await getPool()).findReadCandidates({ + camera: input.camera, timestamp: input.timestamp, toleranceSeconds: readToleranceSeconds, + }); + return { success: true, ...result, readToleranceSeconds, reads: reads.map(read => ({ + id: read.id, cameraName: read.camera_name, plateNumber: read.plate_number, timestamp: new Date(read.timestamp).toISOString(), + })) }; } catch (error) { return { success: false, @@ -1785,6 +1794,9 @@ export async function previewBlueIrisAlertMatch(input = {}) { export async function selectBlueIrisVehicleFrame(input = {}) { await requirePermission("system.manage_settings"); + if (input.readId != null && (!Number.isSafeInteger(Number(input.readId)) || Number(input.readId) < 1)) { + return { success: false, message: "Choose a valid ALPR read to own the image." }; + } try { const config = await getConfig(); const pool = await getPool(); @@ -1796,10 +1808,8 @@ export async function selectBlueIrisVehicleFrame(input = {}) { camera: input.camera, cameraName: input.cameraName, timestamp: input.timestamp, - toleranceSeconds: Math.min( - 30, - Math.max(1, Number.parseInt(String(input.readToleranceSeconds ?? 3), 10) || 3) - ), + toleranceSeconds: readMatchTolerance(input.readToleranceSeconds), + readId: input.readId == null ? null : Number(input.readId), }); revalidatePath("/live_feed"); return { success: result.status === "ready", ...result }; @@ -1977,10 +1987,39 @@ export async function getBlueIrisVehicleFrameQueueStatus(input = {}) { } } +export async function getHomeAssistantDisplays() { + await requirePermission("system.manage_settings"); + try { + const pool = await getPool(); + const [grants, cameras] = await Promise.all([ + new HomeAssistantDisplayService(pool).list(), + pool.query("SELECT DISTINCT camera_name FROM public.plate_reads WHERE NULLIF(BTRIM(camera_name),'') IS NOT NULL ORDER BY camera_name"), + ]); + return { success: true, grants, cameras: cameras.rows.map(row => row.camera_name) }; + } catch { return { success: false, error: "Unable to load display access." }; } +} + +export async function createHomeAssistantDisplay(input = {}) { + const principal = await requirePermission("system.manage_settings"); + try { + const data = await new HomeAssistantDisplayService(await getPool()).create(input, principal); + return { success: true, data }; + } catch { return { success: false, error: "Unable to create display access. Check the name, exact Home Assistant origin, camera and expiration (1–90 days)." }; } +} + +export async function revokeHomeAssistantDisplay(id) { + await requirePermission("system.manage_settings"); + try { + await new HomeAssistantDisplayService(await getPool()).revoke(id); + return { success: true }; + } catch { return { success: false, error: "Unable to revoke display access." }; } +} + export async function getVehicleImageSourceSetup() { await requirePermission("system.manage_settings"); try { const runtime = await getBlueIrisVehicleFrameRuntime(); + await runtime.queue.refreshCameraInventory({ force: true }); const [profiles, inventory] = await Promise.all([ runtime.repository.listOverviewPairProfiles(), (await getPool()).query(`SELECT short_name, display_name FROM public.blue_iris_camera_inventory @@ -2007,9 +2046,10 @@ export async function saveVehicleImageSource(input = {}) { const inventory = await (await getPool()).query(`SELECT display_name FROM public.blue_iris_camera_inventory WHERE present = TRUE AND enabled = TRUE AND LOWER(BTRIM(short_name)) = LOWER(BTRIM($1))`, [normalized.sourceCameraShortName]); - if (inventory.rows.length !== 1 || inventory.rows[0].display_name.trim().toLowerCase() !== normalized.sourceCameraName.toLowerCase()) { + if (inventory.rows.length !== 1) { return { success: false, error: "The selected camera does not match the Blue Iris camera list. Test the Blue Iris connection and try again." }; } + normalized.sourceCameraName = inventory.rows[0].display_name.trim(); const runtime = await getBlueIrisVehicleFrameRuntime(); const saved = await runtime.repository.saveOverviewPairProfile({ ...normalized, replacePrimary: true }, principal); runtime.worker.wake(); @@ -2898,13 +2938,7 @@ export async function updateSettings(formData) { } if (updateIfExists("haEnabled") || updateIfExists("haWhitelist")) { - newConfig.homeassistant = { - ...currentConfig.homeassistant, - enabled: formData.get("haEnabled") === "true", - whitelist: formData.get("haWhitelist") - ? JSON.parse(formData.get("haWhitelist")) - : currentConfig.homeassistant?.whitelist || [], - }; + throw new Error("The IP whitelist is retired. Create read-only display access in Home Assistant settings."); } if ( updateIfExists("bihost") || diff --git a/app/api/home-assistant/display/[grantId]/image/[readId]/route.js b/app/api/home-assistant/display/[grantId]/image/[readId]/route.js new file mode 100644 index 0000000..8068250 --- /dev/null +++ b/app/api/home-assistant/display/[grantId]/image/[readId]/route.js @@ -0,0 +1,8 @@ +import { getPool } from "@/lib/db"; +import fileStorage from "@/lib/fileStorage"; +import { HomeAssistantDisplayService, createDisplayReadHandler } from "@/lib/home-assistant-display.mjs"; + +export const dynamic = "force-dynamic"; +export async function GET(request, context) { + return createDisplayReadHandler({ service: new HomeAssistantDisplayService(await getPool()), readImage: path => fileStorage.getImage(path) })(request, context); +} diff --git a/app/api/home-assistant/display/[grantId]/route.js b/app/api/home-assistant/display/[grantId]/route.js new file mode 100644 index 0000000..7898b6d --- /dev/null +++ b/app/api/home-assistant/display/[grantId]/route.js @@ -0,0 +1,7 @@ +import { getPool } from "@/lib/db"; +import { HomeAssistantDisplayService, createDisplayReadHandler } from "@/lib/home-assistant-display.mjs"; + +export const dynamic = "force-dynamic"; +export async function GET(request, context) { + return createDisplayReadHandler({ service: new HomeAssistantDisplayService(await getPool()) })(request, context); +} diff --git a/app/api/verify-whitelist/route.js b/app/api/verify-whitelist/route.js index 8c89509..b55afd3 100644 --- a/app/api/verify-whitelist/route.js +++ b/app/api/verify-whitelist/route.js @@ -1,94 +1,5 @@ -import { getConfig } from "@/lib/settings"; import { NextResponse } from "next/server"; -// Validates IPv4 address format -const isValidIPv4 = (ip) => { - const ipv4Regex = /^(\d{1,3}\.){3}\d{1,3}$/; - if (!ipv4Regex.test(ip)) return false; - - const parts = ip.split("."); - return parts.every((part) => { - const num = parseInt(part, 10); - return num >= 0 && num <= 255; - }); -}; - -// Validates IPv6 address format -const isValidIPv6 = (ip) => { - // Remove any leading/trailing brackets - ip = ip.replace(/^\[|\]$/g, ""); - - // Handle compressed IPv6 format - const ipv6Regex = - /^([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$|^(([0-9a-fA-F]{1,4}:){0,6}[0-9a-fA-F]{1,4})?::([0-9a-fA-F]{1,4}:){0,6}[0-9a-fA-F]{1,4}$/; - return ipv6Regex.test(ip); -}; - -// Normalizes an IP address -const normalizeIP = (ip) => { - ip = ip.trim(); - - // Handle IPv4-mapped IPv6 addresses - if (ip.startsWith("::ffff:")) { - ip = ip.substring(7); - } - - // Remove any square brackets from IPv6 - ip = ip.replace(/^\[|\]$/g, ""); - - return ip; -}; - -// Gets client IP from X-Forwarded-For header -const getClientIP = (forwardedFor) => { - if (!forwardedFor) return null; - - // Split the header into individual IPs - const ips = forwardedFor.split(",").map((ip) => normalizeIP(ip)); - - // Get the leftmost valid IP (original client) - for (const ip of ips) { - if (isValidIPv4(ip) || isValidIPv6(ip)) { - return ip; - } - } - - return null; -}; - -export async function POST(request) { - try { - const { headers } = await request.json(); - const forwardedFor = headers["x-forwarded-for"]; - if (!forwardedFor) { - console.warn("No X-Forwarded-For header present"); - return NextResponse.json({ allowed: false }); - } - - // Get the client IP from X-Forwarded-For header - const clientIP = getClientIP(forwardedFor); - if (!clientIP) { - console.warn("No valid IP found in X-Forwarded-For header"); - return NextResponse.json({ allowed: false }); - } - - // Load the configuration data - const config = await getConfig(); - const whitelist = config.homeassistant?.whitelist || []; - - if (whitelist.length === 0) { - console.warn("No whitelisted IPs configured"); - return NextResponse.json({ allowed: false }); - } - - // Normalize all whitelist IPs and compare - const normalizedWhitelist = whitelist.map(normalizeIP); - const isAllowedIp = normalizedWhitelist.includes(normalizeIP(clientIP)); - - return NextResponse.json({ allowed: isAllowedIp }); - } catch { - console.error("IP whitelist check failed"); - return NextResponse.json({ allowed: false }, { status: 500 }); - } +export async function POST() { + return NextResponse.json({ error: "IP login bypass is retired. Create read-only display access in Home Assistant settings." }, { status: 410 }); } - diff --git a/app/embed/home-assistant/[grantId]/route.js b/app/embed/home-assistant/[grantId]/route.js new file mode 100644 index 0000000..71a47f5 --- /dev/null +++ b/app/embed/home-assistant/[grantId]/route.js @@ -0,0 +1,18 @@ +import { randomBytes } from "node:crypto"; +import { getPool } from "@/lib/db"; +import { HomeAssistantDisplayService, displayHeaders } from "@/lib/home-assistant-display.mjs"; +import { displayDocument } from "@/lib/home-assistant-display-document.mjs"; + +export const dynamic = "force-dynamic"; +export async function GET(_request, { params }) { + try { + const { grantId } = await params; + const grant = await new HomeAssistantDisplayService(await getPool()).publicGrant(grantId); + if (!grant) return new Response("Display expired, revoked or unavailable.", { status: 404, headers: displayHeaders }); + const nonce = randomBytes(18).toString("base64"); + return new Response(displayDocument(nonce), { headers: { + ...displayHeaders, "Content-Type": "text/html; charset=utf-8", + "Content-Security-Policy": `default-src 'none'; script-src 'nonce-${nonce}'; style-src 'nonce-${nonce}'; connect-src 'self'; img-src 'self' blob:; frame-ancestors ${grant.allowed_origin}; base-uri 'none'; form-action 'none'`, + } }); + } catch { return new Response("Display temporarily unavailable.", { status: 503, headers: displayHeaders }); } +} diff --git a/app/settings/BlueIrisConnectionTest.jsx b/app/settings/BlueIrisConnectionTest.jsx index 775f7d8..e1e4fb7 100644 --- a/app/settings/BlueIrisConnectionTest.jsx +++ b/app/settings/BlueIrisConnectionTest.jsx @@ -17,12 +17,15 @@ export default function BlueIrisConnectionTest() { const [camera, setCamera] = useState(""); const [timestamp, setTimestamp] = useState(""); const [tolerance, setTolerance] = useState("90"); + const [readTolerance, setReadTolerance] = useState("3"); + const [readId, setReadId] = useState(""); const [match, setMatch] = useState(null); const [vehicleFrame, setVehicleFrame] = useState(null); const testConnection = () => { setConnection(null); setMatch(null); + setReadId(""); setVehicleFrame(null); startTransition(async () => { const result = await testBlueIrisConnection(); @@ -33,6 +36,7 @@ export default function BlueIrisConnectionTest() { const searchAlerts = () => { setMatch(null); + setReadId(""); setVehicleFrame(null); const parsed = new Date(timestamp); if (!timestamp || Number.isNaN(parsed.getTime())) { @@ -40,18 +44,21 @@ export default function BlueIrisConnectionTest() { return; } startTransition(async () => { - setMatch(await previewBlueIrisAlertMatch({ + const result = await previewBlueIrisAlertMatch({ camera, timestamp: parsed.toISOString(), toleranceSeconds: Number(tolerance), - })); + readToleranceSeconds: Number(readTolerance), + }); + setMatch({ ...result, readTimestamp: parsed.toISOString() }); + if (result.reads?.length === 1) setReadId(String(result.reads[0].id)); }); }; const selectVehicleFrame = () => { setVehicleFrame(null); const selectedCamera = connection?.cameras?.find((item) => item.id === camera); - const parsed = new Date(match?.alert?.timestamp || timestamp); + const parsed = new Date(match?.readTimestamp || timestamp); if (Number.isNaN(parsed.getTime())) { setVehicleFrame({ success: false, message: "Find a matching Blue Iris alert first." }); return; @@ -61,6 +68,8 @@ export default function BlueIrisConnectionTest() { camera, cameraName: selectedCamera?.name || camera, timestamp: parsed.toISOString(), + readId: Number(readId), + readToleranceSeconds: match.readToleranceSeconds, })); }); }; @@ -87,18 +96,19 @@ export default function BlueIrisConnectionTest() {
- { setCamera(event.target.value); setMatch(null); setVehicleFrame(null); }} className="h-10 w-full rounded-md border border-input bg-background px-3 text-sm"> {connection.cameras.map((item) => )}
- setTimestamp(event.target.value)} /> + { setTimestamp(event.target.value); setMatch(null); setVehicleFrame(null); }} />
- - setTolerance(event.target.value)} /> + + { setTolerance(event.target.value); setMatch(null); setVehicleFrame(null); }} />
+
{ setReadTolerance(event.target.value); setMatch(null); setVehicleFrame(null); }} />
diff --git a/app/settings/HomeAssistantAccessSettings.jsx b/app/settings/HomeAssistantAccessSettings.jsx new file mode 100644 index 0000000..7e003ee --- /dev/null +++ b/app/settings/HomeAssistantAccessSettings.jsx @@ -0,0 +1,53 @@ +"use client"; + +import { useEffect, useState } from "react"; +import { createHomeAssistantDisplay, getHomeAssistantDisplays, revokeHomeAssistantDisplay } from "@/app/actions"; +import { Button } from "@/components/ui/button"; +import { Input } from "@/components/ui/input"; +import { Label } from "@/components/ui/label"; + +export default function HomeAssistantAccessSettings() { + const [data, setData] = useState(null), [error, setError] = useState(""); + const [label, setLabel] = useState("Home Assistant display"), [origin, setOrigin] = useState(""); + const [camera, setCamera] = useState(""), [days, setDays] = useState("30"); + const [busy, setBusy] = useState(false), [link, setLink] = useState(""); + async function reload() { + const result = await getHomeAssistantDisplays(); + if (!result.success) throw new Error(result.error); + setData(result); + } + useEffect(() => { reload().catch(() => setError("Unable to load display access.")); }, []); + async function create() { + setBusy(true); setError(""); setLink(""); + try { + const result = await createHomeAssistantDisplay({ label, allowedOrigin: origin, cameraName: camera, days: Number(days) }); + if (!result.success) throw new Error(result.error); + setLink(`${window.location.origin}/embed/home-assistant/${result.data.id}#token=${result.data.token}`); + await reload(); + } catch (failure) { setError(failure.message); } finally { setBusy(false); } + } + async function revoke(id) { + setBusy(true); setError(""); + try { + const result = await revokeHomeAssistantDisplay(id); + if (!result.success) throw new Error(result.error); + if (link.includes(id)) setLink(""); + await reload(); + } catch (failure) { setError(failure.message); } finally { setBusy(false); } + } + return
+

Home Assistant read-only displays

+

Create a revocable link for a Home Assistant webpage card. Displays show up to 25 reads and their images from the last 24 hours. They cannot edit records, open settings or control ALPR.

+

The old IP login-bypass whitelist is retired. Each display uses its own credential. Keep the link private. Use HTTPS for ALPR when Home Assistant uses HTTPS.

+
+
setLabel(e.target.value)} />
+
setOrigin(e.target.value)} />

Include the scheme and port. Leave out dashboard paths.

+
+
setDays(e.target.value)} />
+
+ + {error &&

{error}

} + {link &&
e.target.select()} />

The credential is shown once. Revoke this display and create another if the link is lost or shared.

} + {!data ?

Loading displays…

:
{data.grants.length === 0 &&

No display access has been created.

}{data.grants.map(grant =>

{grant.label}

{grant.allowed_origin} · {grant.camera_name || "All cameras"}

{grant.revoked_at ? "Revoked" : `Expires ${new Date(grant.expires_at).toLocaleString()}`}

)}
} +
; +} diff --git a/app/settings/SettingsForm.jsx b/app/settings/SettingsForm.jsx index ae17b01..d709de9 100644 --- a/app/settings/SettingsForm.jsx +++ b/app/settings/SettingsForm.jsx @@ -51,6 +51,7 @@ import ReleaseInformationCard from "./ReleaseInformationCard"; import StorageHealthCard from "./StorageHealthCard"; import StorageMaintenancePanel from "./StorageMaintenancePanel"; import BlueIrisConnectionTest from "./BlueIrisConnectionTest"; +import HomeAssistantAccessSettings from "./HomeAssistantAccessSettings"; import WindowsConnectionSettings from "./WindowsConnectionSettings"; const DATA_PRIVACY_ROUTES = Object.freeze({ @@ -178,15 +179,6 @@ export default function SettingsForm({ newFormData.append("webhookAllowHttp", formData.get("webhookAllowHttp") === "on"); newFormData.append("webhookAllowPrivateNetworks", formData.get("webhookAllowPrivateNetworks") === "on"); break; - case "homeassistant": - // IP-list saves are partial updates and must preserve the enable switch. - if (!formData.has("haWhitelist") || formData.has("haEnabled")) { - newFormData.append("haEnabled", formData.get("haEnabled") === "on"); - } - if (formData.get("haWhitelist")) { - newFormData.append("haWhitelist", formData.get("haWhitelist")); - } - break; case "blueiris": [ "bihost", @@ -671,49 +663,7 @@ export default function SettingsForm({ ); - const renderHomeAssistantSection = () => ( -
-
-

- HomeAssistant iframe Login Bypass -

-

- Configure access for HomeAssistant iframe integration. -

-
-
-
-
-
- -

- Allow specific devices to bypass authentication when accessing - the app via HomeAssistant iframe. -

-
- -
-
- - {initialSettings.homeassistant?.enabled && ( - { - const formData = new FormData(); - formData.append("haWhitelist", JSON.stringify(newIPs)); - handleSettingsSubmit(formData); - }} - /> - )} -
-
- ); + const renderHomeAssistantSection = () => ; const renderSecuritySection = () => (
@@ -1029,85 +979,3 @@ export default function SettingsForm({ ); } - -const IPWhitelistManager = ({ initialIPs = [], onUpdate }) => { - const [newIP, setNewIP] = useState(""); - const [error, setError] = useState(""); - - const isValidIP = (ip) => { - // Basic IP validation (IPv4 and IPv6) - const ipv4Regex = /^(\d{1,3}\.){3}\d{1,3}$/; - const ipv6Regex = - /^([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$|^(([0-9a-fA-F]{1,4}:){0,6}[0-9a-fA-F]{1,4})?::([0-9a-fA-F]{1,4}:){0,6}[0-9a-fA-F]{1,4}$/; - - return ipv4Regex.test(ip) || ipv6Regex.test(ip); - }; - - const handleAddIP = () => { - setError(""); - if (!newIP) { - setError("Please enter an IP address"); - return; - } - - if (!isValidIP(newIP)) { - setError("Please enter a valid IP address"); - return; - } - - if (initialIPs.includes(newIP)) { - setError("This IP is already in the whitelist"); - return; - } - - onUpdate([...initialIPs, newIP]); - setNewIP(""); - }; - - const handleRemoveIP = (ipToRemove) => { - onUpdate(initialIPs.filter((ip) => ip !== ipToRemove)); - }; - - return ( -
-
- -
- setNewIP(e.target.value)} - placeholder="Enter IP address" - className="flex-1" - /> - -
- {error &&

{error}

} -
- -
- {initialIPs.map((ip) => ( - - {ip} - - - ))} -
-
- ); -}; diff --git a/components/settings/VehicleImageSourceSettings.jsx b/components/settings/VehicleImageSourceSettings.jsx index 5d22316..f83faff 100644 --- a/components/settings/VehicleImageSourceSettings.jsx +++ b/components/settings/VehicleImageSourceSettings.jsx @@ -8,16 +8,22 @@ import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"; import { Switch } from "@/components/ui/switch"; +import { resolveBlueIrisCamera } from "@/lib/blue-iris-camera-binding.mjs"; function DirectionImageSource({ cameraName, direction, profiles, cameras, onSaved }) { const saved = profiles.filter((item) => item.plateCameraName === cameraName && item.directionLabel === direction) .sort((a, b) => Number(b.enabled) - Number(a.enabled) || b.id - a.id)[0]; - const lprCamera = cameras.find((camera) => camera.name.toLowerCase() === cameraName.toLowerCase()); - const [draft, setDraft] = useState(() => saved || { sourceMode: "lpr_camera", sourceCameraName: cameraName, + const lprCamera = resolveBlueIrisCamera(cameras, cameraName); + const [draft, setDraft] = useState(() => saved || { sourceMode: "lpr_camera", sourceCameraName: lprCamera?.name || cameraName, sourceCameraShortName: lprCamera?.id || "", expectedDeltaMs: 0, toleranceMs: 1500, enabled: true }); const [busy, setBusy] = useState(false); const [message, setMessage] = useState(""); const prefix = `image-source-${direction.replace(/[^a-z0-9]/gi, "-")}`; + const lprId = lprCamera?.id, lprName = lprCamera?.name; + useEffect(() => { + if (lprId) setDraft(current => current.sourceMode === "lpr_camera" && !current.sourceCameraShortName + ? { ...current, sourceCameraName: lprName, sourceCameraShortName: lprId } : current); + }, [lprId, lprName]); async function save() { setBusy(true); setMessage(""); try { @@ -41,24 +47,23 @@ function DirectionImageSource({ cameraName, direction, profiles, cameras, onSave
- {draft.sourceMode === "overview" ?
- +
+ -
:

- Uses recordings from {cameraName}. The view must show the whole vehicle, rather than just the plate. -

} +
+ {draft.sourceMode === "lpr_camera" &&

Bind the ALPR camera {cameraName} to its Blue Iris recording camera. Its view must show the whole vehicle.

} {draft.sourceMode === "overview" ?
{ let active = true; getVehicleImageSourceSetup().then((result) => { @@ -100,17 +106,23 @@ export default function VehicleImageSourceSettings({ profile }) { if (!result.success) throw new Error(result.error); setData(result.data); } + async function refresh() { + setRefreshing(true); setError(""); + try { await reload(); } catch (failure) { setError(failure.message); } + finally { setRefreshing(false); } + } const directions = [...new Set([profile.frontDirectionLabel, profile.rearDirectionLabel].filter(Boolean))]; return Vehicle images for ReID Choose where ALPR gets whole-vehicle images for each saved direction. The same setup works on Windows and Linux. +

Blue Iris must record the selected camera. ALPR checks the recorded frames near each new plate read and rejects incomplete, ambiguous, or monochrome night views. A plate thumbnail alone cannot produce a reliable ReID match.

{error ?

{error}

: !data ?

Loading image setup…

: !directions.length ?

Save your camera direction labels above first.

: <> - {!data.cameras.length ?

Open Blue Iris settings and click Test connection to load your camera list, then return here.

: null} + {!data.cameras.length ?

No enabled Blue Iris recording cameras are available. Check the connection and refresh the camera list.

: null} {directions.map((direction) => )} } diff --git a/docs/COMMUNITY_PRODUCT_ROADMAP.md b/docs/COMMUNITY_PRODUCT_ROADMAP.md index 58bf9b6..9848ab3 100644 --- a/docs/COMMUNITY_PRODUCT_ROADMAP.md +++ b/docs/COMMUNITY_PRODUCT_ROADMAP.md @@ -1,6 +1,17 @@ # Community product roadmap -Last reviewed: November 1, 2026 +Last reviewed: November 2, 2026 + +## Reliable upgrades and integration setup + +Community v0.1.60 tracks ordered database migrations with completion markers and +checksums, preserving existing updater entry points and rollback safeguards. +Completed cleanup does not replay, and Blue Iris v2 direction events are preserved. +Blue Iris setup binds ALPR aliases to explicit recording short IDs, refreshes the +camera list and requires an explicit read owner when several records match. +Home Assistant uses revocable, expiring read-only displays with camera scopes and +exact parent origins; the inactive IP bypass controls are retired. See +[Home Assistant displays](HOME_ASSISTANT.md) and [Database migrations](../migrations/README.md). ## Axis camera recognition @@ -142,8 +153,7 @@ Visual similarity and direction are review aids, not identity proof. - Pushover credentials, defaults, usage, and direct tests; - SMTP email transport, sender identity, and direct tests; - HMAC-SHA256-signed webhooks with explicit HTTP and private-network policy; -- an optional Home Assistant iframe authentication-bypass whitelist for exact - trusted client IP addresses. +- revocable, camera-scoped read-only Home Assistant displays with exact embedding origins. Integrations are inactive until an administrator supplies endpoints and credentials and enables them. They do not require extra ALPR host packages. diff --git a/docs/HOME_ASSISTANT.md b/docs/HOME_ASSISTANT.md new file mode 100644 index 0000000..a03a681 --- /dev/null +++ b/docs/HOME_ASSISTANT.md @@ -0,0 +1,47 @@ +# Home Assistant displays + +Open **Settings > Home Assistant** as an ALPR administrator. Enter a display name, +the exact Home Assistant origin (scheme, hostname and optional port), the camera +to expose or **All cameras**, and an expiration of 1–90 days. Create the display +and copy the link immediately into a Home Assistant **Webpage** card. + +The link credential is shown once. It is stored as a SHA-256 hash in ALPR. The +secret is in the link's URL fragment, which browsers do not send in HTTP request +URLs or referrers. The display captures it in tab-scoped session storage and +removes it from its address. Do not share the link or publish it in diagnostics. +Create a new grant if it is lost. Administrators can revoke each grant separately. + +The display contains up to 25 recent plate reads from the last 24 hours, with +plate/camera/time/direction and an available vehicle or plate image. It refreshes +every 15 seconds while visible. No record editing, settings, application API +access or administrative session is granted. All data and image requests check +the credential, camera scope, expiration and revocation, and use `no-store`. +Revocation prevents subsequent requests; the panel clears on its next refresh. +Previously viewed or copied information cannot be recalled. + +Only the specified Home Assistant origin can frame the display. The rest of ALPR +continues to block framing and require ordinary session authentication. The +display's credential never substitutes for a session or the ingestion API key. +It works without third-party cookies. It does not trust client IPs, forwarded +headers, the referrer, or possession of a Home Assistant session as authentication. + +If Home Assistant uses HTTPS, serve ALPR through HTTPS too; browsers block an +HTTP iframe inside an HTTPS page. Local HTTP can be used when both applications +are served over HTTP. The configured origin must exactly match the browser's +Home Assistant address, including a non-default port. Use separate grants for +different origins or devices so each can be revoked independently. + +The legacy **Enable Whitelist** and IP login-bypass controls are retired. Old +configuration values are retained for compatibility but grant no access. The +obsolete verifier endpoint returns HTTP 410 to authenticated callers. + +## Verification + +`test/home-assistant-display.test.mjs` checks credential isolation and policy +validation. The PostgreSQL upgrade matrix covers camera scope, expiration and +revocation. `scripts/test-community-bugfixes-local.mjs PG_BIN --browser` additionally +checks the production app in a real browser, including an approved and a blocked +iframe origin, images, revocation, and administrator create/revoke actions. Set +`ALPR_TEST_PLAYWRIGHT_MODULE` to a local `playwright-core/index.mjs` and +`ALPR_TEST_BROWSER` to the browser executable. All records and credentials used by +these fixtures are synthetic, and the local runner creates a separate cluster. diff --git a/docs/INSTALL.md b/docs/INSTALL.md index 36e361d..7e87bd7 100644 --- a/docs/INSTALL.md +++ b/docs/INSTALL.md @@ -64,7 +64,7 @@ Clone the canonical repository and detach at the exact stable release tag: ```bash git clone https://github.com/prsmith777/ALPR-Database-Community.git cd ALPR-Database-Community -git checkout --detach v0.1.59 +git checkout --detach v0.1.60 ./alpr-community install ``` diff --git a/docs/UPDATES.md b/docs/UPDATES.md index 452d60e..9104e76 100644 --- a/docs/UPDATES.md +++ b/docs/UPDATES.md @@ -47,7 +47,7 @@ shown on the GitHub Releases page, rather than deploying moving `main`: ```bash git clone https://github.com/prsmith777/ALPR-Database-Community.git cd ALPR-Database-Community -git checkout --detach v0.1.59 +git checkout --detach v0.1.60 ./alpr-community install ``` @@ -88,7 +88,7 @@ alpr_native_update() ( set -Eeuo pipefail download_dir="$(mktemp -d /tmp/alpr-maintenance-download.XXXXXX)" trap 'rm -rf -- "$download_dir"' EXIT - asset_base="https://github.com/prsmith777/ALPR-Database-Community/releases/download/v0.1.59" + asset_base="https://github.com/prsmith777/ALPR-Database-Community/releases/download/v0.1.60" curl -fLsS --retry 3 "$asset_base/community-maintenance.sh" -o "$download_dir/community-maintenance.sh" curl -fLsS --retry 3 "$asset_base/community-maintenance.sh.sha256" -o "$download_dir/community-maintenance.sh.sha256" (cd "$download_dir" && sha256sum --check --strict community-maintenance.sh.sha256) @@ -99,7 +99,7 @@ unset -f alpr_native_update ``` The launcher verifies the canonical repository and exact released tag, loads the -fixed tools outside your checkout, and asks for **INSTALL v0.1.59**. It uses the +fixed tools outside your checkout, and asks for **INSTALL v0.1.60**. It uses the same guarded backup/install workflow and does not reset or reinstall your system. It temporarily stops only this installation's matching per-user systemd update agent, then restarts it with the installed code. Keep the terminal open and do @@ -371,3 +371,20 @@ ALPR_UPDATE_CLEANUP=ALPR_UPDATE_CLEANUP These acknowledgements prevent an unattended typo from installing, accepting, rolling back, or deleting rollback assets. They are not passwords. + +## v0.1.60: tracked migration transition + +Existing installations use the normal Windows or Linux update workflow. A direct +update from v0.1.55, v0.1.58 or v0.1.59 requires no manual SQL and no intermediate +release. The updater adopts existing migration completion markers and applies +only pending work. Blue Iris v2 zone-crossing observations are preserved and +remain visible to current-direction queries. Backup, count-loss rejection and +rollback remain enabled. + +If v0.1.58 previously rolled back with a direction-observation count decrease, +retain that restored installation and recovery backup, then update normally to +v0.1.60. Do not delete observations or disable count validation to force an update. +If an applied-migration checksum differs, keep the backup and investigate the +source/package mismatch. Do not edit the stored checksum or replay old cleanup. + +For the new Home Assistant access setup, see [Home Assistant displays](HOME_ASSISTANT.md). diff --git a/docs/security-baseline.md b/docs/security-baseline.md index 513e547..edfcdca 100644 --- a/docs/security-baseline.md +++ b/docs/security-baseline.md @@ -40,6 +40,15 @@ fail closed and clear the session cookie. Middleware does not authenticate by client IP, does not trust `X-Forwarded-For`, and does not call the legacy whitelist verifier. +The dedicated `/embed/home-assistant/UUID` display shell and its GET-only +`/api/home-assistant/display/UUID` data/image routes use a separate revocable, +expiring read-only credential. Credentials are stored hashed, supplied in a +Bearer header for every data/image request, and cannot authorize normal pages, +server actions, generic images or any mutation. Camera scopes are enforced in +the data queries. Only the display shell permits framing, using a nonce-based +CSP and its exact configured parent origin. All other pages retain frame denial. +See [Home Assistant displays](HOME_ASSISTANT.md). The old IP whitelist UI is retired. + API-key comparison uses `crypto.timingSafeEqual` after checking byte lengths. Unequal-length credentials are rejected without calling `timingSafeEqual`. diff --git a/lib/blue-iris-camera-binding.mjs b/lib/blue-iris-camera-binding.mjs new file mode 100644 index 0000000..a129be9 --- /dev/null +++ b/lib/blue-iris-camera-binding.mjs @@ -0,0 +1,13 @@ +const key = value => String(value || "").trim().toLowerCase(); + +// A display name may be shared; only an unambiguous alias is auto-selected. +export function resolveBlueIrisCamera(cameras, alias) { + const matches = cameras.filter(camera => key(camera.id) === key(alias) || key(camera.name) === key(alias)); + return matches.length === 1 ? matches[0] : null; +} + +export function readMatchTolerance(value = 3) { + const seconds = Number(value); + if (!Number.isFinite(seconds) || seconds < 1 || seconds > 30) throw new Error("ALPR read tolerance must be between 1 and 30 seconds."); + return seconds; +} diff --git a/lib/blue-iris-vehicle-frame-repository.mjs b/lib/blue-iris-vehicle-frame-repository.mjs index f49947a..4f43991 100644 --- a/lib/blue-iris-vehicle-frame-repository.mjs +++ b/lib/blue-iris-vehicle-frame-repository.mjs @@ -3356,18 +3356,40 @@ export class BlueIrisVehicleFrameRepository { return result.rowCount || 0; } - async findNearestRead({ cameraName, timestamp, toleranceSeconds = 3 }) { + async findReadCandidates({ cameraName, camera, timestamp, toleranceSeconds = 3, readId = null }) { const result = await this.pool.query( - `SELECT id, plate_number, camera_name, "timestamp", crop_coordinates, vehicle_image_path - FROM public.plate_reads - WHERE LOWER(BTRIM(camera_name)) = LOWER(BTRIM($1)) - AND "timestamp" BETWEEN $2::timestamptz - make_interval(secs => $3) - AND $2::timestamptz + make_interval(secs => $3) - ORDER BY ABS(EXTRACT(EPOCH FROM ("timestamp" - $2::timestamptz))), id - LIMIT 1`, - [String(cameraName || "").trim(), timestamp, Number(toleranceSeconds)] + `WITH selected_camera AS ( + SELECT short_name, display_name FROM public.blue_iris_camera_inventory + WHERE present AND enabled AND LOWER(BTRIM(short_name)) = LOWER(BTRIM($4)) + ), candidate_aliases AS ( + SELECT LOWER(BTRIM(short_name)) AS name FROM selected_camera + UNION SELECT LOWER(BTRIM(display_name)) FROM selected_camera + ), aliases AS ( + SELECT name FROM candidate_aliases alias + WHERE (SELECT COUNT(*) FROM public.blue_iris_camera_inventory inventory + WHERE inventory.present AND (LOWER(BTRIM(inventory.display_name)) = alias.name + OR LOWER(BTRIM(inventory.short_name)) = alias.name)) = 1 + UNION SELECT LOWER(BTRIM(plate_camera_name)) FROM public.vehicle_overview_pair_profiles + WHERE source_mode = 'lpr_camera' AND LOWER(BTRIM(source_camera_short_name)) = LOWER(BTRIM($4)) + UNION SELECT LOWER(BTRIM($1)) WHERE $4::text IS NULL + ) + SELECT reads.id, reads.plate_number, reads.camera_name, reads.timestamp, reads.crop_coordinates, reads.vehicle_image_path + FROM public.plate_reads reads + WHERE LOWER(BTRIM(reads.camera_name)) IN (SELECT name FROM aliases) + AND reads.timestamp BETWEEN $2::timestamptz - make_interval(secs => $3) + AND $2::timestamptz + make_interval(secs => $3) + AND ($5::integer IS NULL OR reads.id = $5) + ORDER BY ABS(EXTRACT(EPOCH FROM (reads.timestamp - $2::timestamptz))), reads.id + LIMIT 21`, + [String(cameraName || "").trim(), timestamp, Number(toleranceSeconds), camera || null, readId] ); - return result.rows?.[0] || null; + return result.rows || []; + } + + async findNearestRead(input) { + const reads = await this.findReadCandidates(input); + // Require an explicit choice when multiple reads could own the image. + return reads.length === 1 ? reads[0] : null; } async claimNext({ includeHistorical = false } = {}) { @@ -3460,7 +3482,7 @@ export class BlueIrisVehicleFrameRepository { AND source_role = 'primary' AND tolerance_ms <= 3000 AND ((source_mode = 'overview' AND LOWER(BTRIM(source_camera_name)) <> LOWER(BTRIM(plate_camera_name))) - OR (source_mode = 'lpr_camera' AND LOWER(BTRIM(source_camera_name)) = LOWER(BTRIM(plate_camera_name)))) + OR (source_mode = 'lpr_camera' AND NULLIF(BTRIM(source_camera_short_name), '') IS NOT NULL AND expected_delta_ms = 0)) AND LOWER(BTRIM(plate_camera_name)) = LOWER(BTRIM(reads.camera_name)) AND LOWER(BTRIM(direction_label)) = LOWER(BTRIM(reads.bi_trigger_direction_label)) ORDER BY priority, id diff --git a/lib/blue-iris-vehicle-frame.mjs b/lib/blue-iris-vehicle-frame.mjs index a56bfe6..104bf8f 100644 --- a/lib/blue-iris-vehicle-frame.mjs +++ b/lib/blue-iris-vehicle-frame.mjs @@ -1076,11 +1076,13 @@ export class BlueIrisVehicleFrameService { ); } - async processNearestRead({ camera, cameraName, timestamp, toleranceSeconds = 3 }) { + async processNearestRead({ camera, cameraName, timestamp, toleranceSeconds = 3, readId = null }) { const read = await this.repository.findNearestRead({ cameraName, + camera, timestamp, toleranceSeconds, + readId, }); if (!read) { return { @@ -1203,10 +1205,11 @@ export class BlueIrisVehicleFrameService { const overviewContext = normalizedOverviewContext(profile.overview_context); const lprCameraSource = profile.source_mode === "lpr_camera"; - if (lprCameraSource && (String(profile.source_camera_name || "").trim().toLowerCase() - !== String(read.camera_name || "").trim().toLowerCase() + if (lprCameraSource && (!String(profile.source_camera_short_name || "").trim() + || String(profile.source_camera_short_name).trim().toLowerCase() !== String(camera || "").trim().toLowerCase() + || (profile.plate_camera_name && String(profile.plate_camera_name).trim().toLowerCase() !== String(read.camera_name).trim().toLowerCase()) || Number(profile.expected_delta_ms) !== 0)) { - throw new Error("The LPR image source must use its own camera and event time."); + throw new Error("The LPR image source must use its explicit recording camera binding and event time."); } const sourceKind = primaryOverviewSourceKind(profile); const historyMode = String(profile.profile_kind || "").trim() === "entry_history"; diff --git a/lib/help-manual.mjs b/lib/help-manual.mjs index 1d904a0..319b3f2 100644 --- a/lib/help-manual.mjs +++ b/lib/help-manual.mjs @@ -12,8 +12,8 @@ export const HELP_MANUAL = Object.freeze({ title: "ALPR Database Community User Guide", shortTitle: "User Guide", description: "A practical guide to using, configuring, and safely administering the portable Community application.", - manualVersion: "3.27", - updatedAt: "November 1, 2026", + manualVersion: "3.28", + updatedAt: "November 2, 2026", coverageBaseline: "Published clean-history Community release with portable configuration and no private deployment controls.", filename: "ALPR-Database-Community-User-Guide.pdf", sections: [ @@ -385,7 +385,7 @@ export const HELP_MANUAL = Object.freeze({ title: "Choose a whole-vehicle image source", items: [ "Save the camera direction profile first. Connect Blue Iris, refresh its camera inventory, and enable recording for the source camera.", - "In Vehicle images for ReID, choose This LPR camera for each saved travel direction if its recordings show the entire vehicle. No overview camera is required in that case.", + "In Vehicle images for ReID, choose This LPR camera for each saved travel direction if its recordings show the entire vehicle. Select its Blue Iris recording camera explicitly; the ALPR name may differ from the Blue Iris display name or short ID. No overview camera is required in that case.", "Otherwise choose Overview camera, select its Blue Iris camera, and set the timing offset from the plate read in seconds.", "Set Timing tolerance (seconds) between 0.25 and 3. This is the recording window before and after the offset time; each direction saves its own tolerance.", "Each direction reports Not saved yet, Unsaved changes, or Saved and enabled. A displayed default is not an active mapping until it is saved.", @@ -523,7 +523,7 @@ export const HELP_MANUAL = Object.freeze({ "Open Settings > Blue Iris. Enter the Blue Iris hostname or IP and include :port when it is not port 80.", "Enter a Blue Iris user that can list cameras, search alerts, and retrieve playback frames. Set the request timeout from 2 through 30 seconds.", "Keep the timeline export profile between 0 and 3. Set the minimum overview export width and height; exports below that resolution fail closed instead of saving a low-resolution Vehicle View.", - "Save, run the read-only connection test, select a camera and local plate-read time, search within the tolerance window, and optionally select the best vehicle frame. The test does not change Blue Iris recordings.", + "Save, run the read-only connection test, and select a camera and local plate-read time. Blue Iris alert tolerance and ALPR read tolerance are separate settings. Choose the exact persisted ALPR read that will own the image; multiple matching reads require an explicit choice. Frame sampling uses that read and its timestamp. The test does not change Blue Iris recordings.", ], }, { type: "steps", @@ -549,18 +549,20 @@ export const HELP_MANUAL = Object.freeze({ { id: "home-assistant", title: "Home Assistant integration", - summary: "Use the iframe login-bypass whitelist only for explicitly trusted device addresses.", + summary: "Create revocable read-only iframe displays with limited camera access.", roles: ["administrator"], - keywords: ["home assistant", "iframe", "whitelist", "bypass authentication", "ip address"], + keywords: ["home assistant", "iframe", "read-only", "display", "revoke", "whitelist"], settingsViews: ["/settings/home-assistant"], blocks: [{ type: "steps", - title: "Configure iframe access", - items: [ - "Open Settings > Home Assistant and leave Enable Whitelist off unless an iframe device must bypass the ALPR login page.", - "When needed, enable the whitelist and add only the exact IPv4 or IPv6 address of each trusted Home Assistant device.", - "Remove stale addresses when devices change. Do not whitelist a broad proxy, shared gateway, or untrusted client because any request seen from that address can bypass authentication.", - "Test from the intended Home Assistant device and separately confirm that a non-whitelisted browser still receives the ALPR login page.", + title: "Configure a read-only display", + items: [ + "Open Settings > Home Assistant. The legacy IP whitelist is retired and cannot bypass authentication.", + "Enter a display name and the exact Home Assistant origin, including http:// or https:// and the port. Dashboard paths and wildcards are not accepted.", + "Choose one camera or all cameras, then choose an expiration of 1 to 90 days. Create the link and copy it immediately into a Home Assistant webpage card. The credential is displayed once; keep it private.", + "The display shows up to 25 recent reads and their images from the last 24 hours. It cannot open settings, modify records, use normal application APIs or create an administrator session.", + "Use HTTPS for ALPR when Home Assistant uses HTTPS. Display credentials do not depend on third-party cookies. Access from an unapproved iframe origin is blocked.", + "Revoke a display to stop subsequent data and image requests. The display clears on its next refresh; information already viewed cannot be recalled. A normal browser still requires ALPR login.", ], }], }, diff --git a/lib/home-assistant-display-document.mjs b/lib/home-assistant-display-document.mjs new file mode 100644 index 0000000..1e42773 --- /dev/null +++ b/lib/home-assistant-display-document.mjs @@ -0,0 +1,37 @@ +export function displayDocument(nonce) { + return ` +ALPR read-only display

ALPR

Read-only · recent reads from the last 24 hours

Connecting…

+`; +} diff --git a/lib/home-assistant-display.mjs b/lib/home-assistant-display.mjs new file mode 100644 index 0000000..c750eea --- /dev/null +++ b/lib/home-assistant-display.mjs @@ -0,0 +1,107 @@ +import { createHash, randomBytes, randomUUID, timingSafeEqual } from "node:crypto"; +import { isIP } from "node:net"; + +export const DISPLAY_ID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i; +const digest = value => createHash("sha256").update(value).digest("hex"); +const validToken = value => typeof value === "string" && /^[A-Za-z0-9_-]{43}$/.test(value); +export const displayHeaders = Object.freeze({ + "Cache-Control": "no-store, private", "Referrer-Policy": "no-referrer", + "X-Content-Type-Options": "nosniff", "Content-Security-Policy": "default-src 'none'; frame-ancestors 'none'", +}); + +export function normalizeDisplayGrant(input = {}) { + const label = String(input.label || "").trim(); + let origin; + try { origin = new URL(String(input.allowedOrigin || "").trim()); } catch { throw new Error("Enter the full Home Assistant origin, including http:// or https:// and its port."); } + if (!["http:", "https:"].includes(origin.protocol) || origin.username || origin.password + || origin.pathname !== "/" || origin.search || origin.hash || origin.origin === "null") + throw new Error("Use an exact Home Assistant origin with no path, credentials, query or fragment."); + const host = origin.hostname.replace(/^\[|\]$/g, ""); + if (!isIP(host) && !/^(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)*[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?$/i.test(host)) + throw new Error("Use one exact Home Assistant host; wildcards and policy expressions are not allowed."); + if (!label || label.length > 80) throw new Error("Enter a display name of up to 80 characters."); + const days = Number(input.days ?? 30); + if (!Number.isInteger(days) || days < 1 || days > 90) throw new Error("Choose an expiration of 1 to 90 days."); + const cameraName = String(input.cameraName || "").trim(); + if (cameraName.length > 100) throw new Error("Invalid camera selection."); + return { label, allowedOrigin: origin.origin, days, cameraName: cameraName || null }; +} + +export class HomeAssistantDisplayService { + constructor(pool, { now = () => Date.now() } = {}) { this.pool = pool; this.now = now; } + async list() { + const result = await this.pool.query(`SELECT id, label, allowed_origin, camera_name, created_at, expires_at, revoked_at + FROM public.home_assistant_display_grants ORDER BY created_at DESC LIMIT 100`); + return result.rows; + } + async create(input, actor) { + const value = normalizeDisplayGrant(input); + if (value.cameraName) { + const camera = await this.pool.query("SELECT 1 FROM public.plate_reads WHERE camera_name = $1 LIMIT 1", [value.cameraName]); + if (!camera.rows.length) throw new Error("Choose a camera with ALPR read history."); + } + const id = randomUUID(), token = randomBytes(32).toString("base64url"); + const expires = new Date(this.now() + value.days * 86_400_000); + await this.pool.query(`INSERT INTO public.home_assistant_display_grants + (id,label,token_hash,allowed_origin,camera_name,created_by,expires_at) VALUES ($1,$2,$3,$4,$5,$6,$7)`, + [id, value.label, digest(token), value.allowedOrigin, value.cameraName, String(actor?.username || "administrator").slice(0,100), expires]); + return { id, token, expiresAt: expires.toISOString() }; + } + async revoke(id) { + if (!DISPLAY_ID.test(id || "")) throw new Error("Invalid display."); + await this.pool.query("UPDATE public.home_assistant_display_grants SET revoked_at = COALESCE(revoked_at,CURRENT_TIMESTAMP) WHERE id = $1", [id]); + } + async publicGrant(id) { + if (!DISPLAY_ID.test(id || "")) return null; + const result = await this.pool.query(`SELECT id, allowed_origin FROM public.home_assistant_display_grants + WHERE id = $1 AND revoked_at IS NULL AND expires_at > CURRENT_TIMESTAMP`, [id]); + return result.rows[0] || null; + } + async authorize(id, authorization) { + if (!DISPLAY_ID.test(id || "")) return null; + const token = /^Bearer ([A-Za-z0-9_-]{43})$/.exec(authorization || "")?.[1]; + if (!validToken(token)) return null; + const result = await this.pool.query(`SELECT id, label, camera_name, token_hash FROM public.home_assistant_display_grants + WHERE id = $1 AND revoked_at IS NULL AND expires_at > CURRENT_TIMESTAMP`, [id]); + const grant = result.rows[0]; + if (!grant || !/^[0-9a-f]{64}$/.test(grant.token_hash || "")) return null; + if (!timingSafeEqual(Buffer.from(digest(token), "hex"), Buffer.from(grant.token_hash, "hex"))) return null; + return { id: grant.id, label: grant.label, cameraName: grant.camera_name }; + } + async reads(grant) { + const result = await this.pool.query(`SELECT id, plate_number, camera_name, timestamp, + bi_trigger_direction_label AS direction, + (vehicle_image_path IS NOT NULL OR image_path IS NOT NULL) AS has_image + FROM public.plate_reads WHERE ($1::text IS NULL OR camera_name = $1) + AND timestamp > CURRENT_TIMESTAMP - INTERVAL '24 hours' + ORDER BY timestamp DESC, id DESC LIMIT 25`, [grant.cameraName]); + return { label: grant.label, reads: result.rows }; + } + async imagePath(grant, readId) { + if (!/^[1-9]\d{0,9}$/.test(String(readId || ""))) return null; + const result = await this.pool.query(`SELECT COALESCE(vehicle_image_path,image_path) AS path + FROM public.plate_reads WHERE id = $1 AND ($2::text IS NULL OR camera_name = $2) + AND timestamp > CURRENT_TIMESTAMP - INTERVAL '24 hours'`, [readId, grant.cameraName]); + return result.rows[0]?.path || null; + } +} + +// An explicit credential is required for every request. Display credentials are +// never accepted by browser session verification, general images, or mutations. +export function createDisplayReadHandler({ service, readImage }) { + return async (request, { params }) => { + try { + const { grantId, readId } = await params; + const grant = await service.authorize(grantId, request.headers.get("authorization")); + if (!grant) return Response.json({ error: "Display access expired, revoked or invalid." }, { status: 401, headers: displayHeaders }); + if (readId !== undefined) { + const path = await service.imagePath(grant, readId); + const image = path ? await readImage(path) : null; + return new Response(image, { status: image ? 200 : 404, headers: { ...displayHeaders, "Content-Type": "image/jpeg" } }); + } + return Response.json(await service.reads(grant), { headers: displayHeaders }); + } catch { + return Response.json({ error: "Display temporarily unavailable." }, { status: 503, headers: displayHeaders }); + } + }; +} diff --git a/lib/middleware-auth.mjs b/lib/middleware-auth.mjs index 4a86928..c71a8f1 100644 --- a/lib/middleware-auth.mjs +++ b/lib/middleware-auth.mjs @@ -163,6 +163,14 @@ export function createMiddlewareHandler({ return async function handleMiddleware(request) { const pathname = request.nextUrl.pathname; + // These dedicated GET-only routes verify a revocable read-only display + // credential themselves. They never authorize normal pages or APIs. + const displayId = "[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}"; + if ((!request.method || request.method === "GET") && ( + new RegExp(`^/embed/home-assistant/${displayId}$`, "i").test(pathname) + || new RegExp(`^/api/home-assistant/display/${displayId}(?:/image/[1-9]\\d{0,9})?$`, "i").test(pathname) + )) return next(); + // This exact receiver authenticates its own scoped source credential before // reading the body. All camera administration routes retain session auth. if (/^\/api\/camera-inputs\/axis\/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(pathname)) return next(); diff --git a/lib/release-info.mjs b/lib/release-info.mjs index 59684d2..b6e62cc 100644 --- a/lib/release-info.mjs +++ b/lib/release-info.mjs @@ -4,57 +4,15 @@ import builtReleaseMetadata from "./built-release-metadata.mjs"; import { HELP_MANUAL } from "./help-manual.mjs"; export const CURRENT_RELEASE_NOTES = Object.freeze({ - title: "Axis HTTP and MQTT camera inputs", - publishedAt: "2026-11-01", + title: "Reliable upgrades, Blue Iris setup and Home Assistant displays", + publishedAt: "2026-11-02", items: Object.freeze([ - "Choose Blue Iris or AXIS License Plate Verifier for each camera in Settings > Integrations > Camera Inputs; existing Blue Iris cameras continue automatically.", - "Receive Axis recognition through authenticated HTTP push, MQTT, or both, including camera-supplied plate and scene images.", - "Configure camera identity, broker and topic, image storage, movement direction labels, and immediate or finalized passage processing.", - "Persist incoming events before acknowledgement, deduplicate transports, retry processing, preserve human corrections, and apply later camera observations to the same passage.", - "Update User Guide 3.27 with setup, diagnostics, source compatibility and the remaining need for camera-specific hardware verification.", - "Update vulnerable application and tooling dependencies and remove unused legacy image and UI-generator packages.", - "Resize Recognition Feed and Plate Database columns with draggable header dividers or the keyboard; remember widths separately for each table in this browser.", - "Reset one column by double-clicking its divider, or restore the full layout with Reset widths.", - "Show narrow text with ellipses and full-value hover text, protect minimum control widths, and scroll wider tables horizontally.", - "Keep Actions compact in a menu with full names; widening the column restores individual icons while preserving permissions and confirmations.", - "Update User Guide 3.26 with adjustable-column and compact-action instructions.", - "Start administrator corrections with batch correction and recurring misread aliases enabled, retaining preview and unchanged-plate safeguards.", - "Scroll long tag menus to the final tag in the records table and image detail modal.", - "Enable the optional State/Country column in Settings > General; it is off by default in Recognition Feed and Plate Database.", - "Enter State/Country manually or choose a previous entry, shared across reads of the same effective plate with audited changes and indexed suggestions.", - "Update User Guide 3.25 and Settings help for the correction defaults and manual State/Country workflow.", - "Export the original Docker database before native Windows installation, with explicit source selection and plate-read count review.", - "Move supported Docker ALPR installations from Linux or Windows to native Windows using a verified database/image/settings/authentication bundle.", - "Restore the selected source application state after export and refuse changed sources, ambiguous database settings or nonempty import targets.", - "Update User Guide 3.24 and the migration roadmap with both source-platform procedures and preservation of existing new records.", - "Keep Windows updates running through brief status-file sharing locks; progress reporting failures no longer interrupt maintenance.", - "Change the private PostgreSQL port under Settings > General, with conflict checks, verified service restart and automatic configuration recovery.", - "Preserve the selected database port through software rollback, browser updates and retained-data reinstall; keep browser and Blue Iris addresses unchanged.", - "Update User Guide 3.23 and Windows guidance for both connection port controls.", - "Choose application and database ports in Windows Setup for side-by-side Docker migration, with conflict checks and shortcuts using the verified address.", - "Change the native Windows application port under Settings > General, preserving network access and API keys with verified restart and recovery.", - "Correct Windows-managed setting labels and update User Guide 3.22 with port changes and independent Docker/native database guidance.", - "Set up Linux Software Updates automatically after a healthy fresh installation or activated migration, including startup after reboot and a verified service heartbeat.", - "Preserve matching existing helpers and keep ALPR running if helper setup needs attention; retain ./alpr-community agent install for older hosts and recovery.", - "Update User Guide 3.21, Linux installation, migration, bootstrap and update guidance, with real systemd unattended-startup CI coverage.", - "Expand the Windows user guide with step-by-step native Windows installation, setup choices, first login, LAN access, backups and migration.", - "Document Windows updates in Settings > Software Updates, recovery and acceptance, and synchronize the public roadmap and compatibility guides with the stable release.", - "Add a Community documentation wiki linking installation, backups, migration, troubleshooting and the versioned guides.", - "Preserve generated direction predictions by skipping byte-identical verified schema and migration SQL during Windows updates.", - "Check every database table before restarting workers; allow completed temporary work only after that checkpoint, and retain affected table counts through rollback.", - "Wait briefly for Windows release-folder locks to clear before publishing a verified update; preserve all copies if access remains blocked.", - "Retry interrupted Windows updates without deleting existing release files or changing the active application during staging.", - "Stop Setup on update errors and verify the expected running release before reporting a successful installation.", - "Install stable Windows Community updates from Settings > Software Updates, with download progress, automatic recovery backup, restart, validation, acceptance and rollback.", - "Run graphical Setup once to update an existing Windows installation in place and enable browser updates, preserving passwords, API key, records, images and settings.", - "Recover the previous release automatically after an unsuccessful Windows update and retain accepted backups when another release arrives.", - "Correct a PowerShell 5.1 process-result bug that falsely reported successful Windows migration backups as failed.", - "Install natively on Windows 10 22H2 x64 or Windows 11 x64 with automatic application and database services.", - "Restore retained Windows data after reinstall with a verified recovery backup, preserving records, images, passwords, API keys, settings, and network preferences.", - "Choose a recorded LPR camera or separate overview camera as the whole-vehicle image source for each saved travel direction.", - "Reject plate-only, clipped, ambiguous, and monochrome LPR frames before they become ReID evidence.", - "Move a verified logical database, images, settings, and authentication backup from Linux or Windows into an empty native Windows installation.", - "Show component download percentages and animated progress during Windows setup and removal; retain the source and verified backup for recovery.", + "Apply only pending database migrations, verify their checksums, and adopt existing completion markers without replaying historical cleanup.", + "Preserve Blue Iris v2 direction observations and expose them to current-direction queries while retaining backup, row-count validation and rollback.", + "Bind ALPR camera aliases to explicit Blue Iris recording short IDs, refresh camera inventory in setup, and use the saved binding in automatic vehicle processing.", + "Show separate Blue Iris alert and ALPR read tolerances and require an explicit image owner when several ALPR reads match.", + "Create revocable, expiring, camera-scoped read-only Home Assistant display links with exact parent-origin restrictions; retire the inactive IP login-bypass controls.", + "Update User Guide 3.28, release and installation guidance. Existing installations use their normal update workflow without manual SQL or an intermediate release.", ]), }); diff --git a/lib/settings-help-coverage.mjs b/lib/settings-help-coverage.mjs index abacf7d..03a9b2c 100644 --- a/lib/settings-help-coverage.mjs +++ b/lib/settings-help-coverage.mjs @@ -90,7 +90,7 @@ export const SETTINGS_HELP_COVERAGE = Object.freeze([ { sectionId: "home-assistant", routes: ["/settings/home-assistant"], - requiredTerms: ["iframe", "enable whitelist", "bypass authentication", "non-whitelisted browser"], + requiredTerms: ["iframe", "read-only", "revoke", "normal browser"], }, ]); diff --git a/lib/vehicle-image-source.mjs b/lib/vehicle-image-source.mjs index 21285e7..3af58ca 100644 --- a/lib/vehicle-image-source.mjs +++ b/lib/vehicle-image-source.mjs @@ -4,7 +4,7 @@ export function normalizeVehicleImageSource(input = {}) { const sourceMode = text(input.sourceMode || "overview"); const plateCameraName = text(input.plateCameraName); const directionLabel = text(input.directionLabel); - const sourceCameraName = sourceMode === "lpr_camera" ? plateCameraName : text(input.sourceCameraName); + const sourceCameraName = text(input.sourceCameraName) || (sourceMode === "lpr_camera" ? plateCameraName : ""); const sourceCameraShortName = text(input.sourceCameraShortName); if (!["overview", "lpr_camera"].includes(sourceMode)) throw new Error("Choose a vehicle image source."); if (!plateCameraName || !directionLabel || !sourceCameraName) throw new Error("Choose a camera and a saved direction."); diff --git a/migrations.sql b/migrations.sql index 74f162e..5d99df5 100644 --- a/migrations.sql +++ b/migrations.sql @@ -1,3 +1,43 @@ +-- GENERATED by node scripts/build-migrations.mjs. Edit only new migration sources. +-- The single-file, pure-SQL entry point also supports older Windows updaters, +-- Docker's psql runner, and logical imports. Callers must use one transaction. +SELECT pg_advisory_xact_lock(6839271042261); +CREATE TABLE IF NOT EXISTS public.schema_migrations ( + version VARCHAR(100) PRIMARY KEY, + description TEXT NOT NULL, + applied_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP +); +ALTER TABLE public.schema_migrations ADD COLUMN IF NOT EXISTS sql_sha256 CHAR(64); +-- Legacy markers were committed by the previous transactional SQL runner. +-- Validate its identity authority before adopting markers as the baseline. +DO $baseline$ +BEGIN + IF EXISTS (SELECT 1 FROM public.schema_migrations WHERE version = '2026092701_native_reid') THEN + IF to_regclass('public.vehicle_reid_control') IS NULL + OR to_regclass('public.vehicle_direction_observations') IS NULL THEN + RAISE EXCEPTION 'Legacy migration baseline is incomplete'; + END IF; + IF NOT EXISTS (SELECT 1 FROM public.vehicle_reid_control WHERE singleton AND mode = 'v2_primary') THEN + RAISE EXCEPTION 'Legacy identity authority does not match its migration marker'; + END IF; + END IF; +END; +$baseline$; + +-- 0001_2026071901_identity_audit_foundation.sql +DO $migration$ +DECLARE recorded_hash TEXT; +BEGIN + SELECT sql_sha256 INTO recorded_hash FROM public.schema_migrations WHERE version = '2026071901_identity_audit_foundation'; + IF FOUND THEN + IF recorded_hash IS NOT NULL AND recorded_hash <> 'f01d375f3d3c55f7d80ae6596d0e09a3a787f637557ff3fb82046333b0ed7474' THEN + RAISE EXCEPTION 'Applied migration checksum differs: 2026071901_identity_audit_foundation'; + END IF; + IF recorded_hash IS NULL THEN + UPDATE public.schema_migrations SET sql_sha256 = 'f01d375f3d3c55f7d80ae6596d0e09a3a787f637557ff3fb82046333b0ed7474' WHERE version = '2026071901_identity_audit_foundation'; + END IF; + ELSE + EXECUTE $source_f01d375f3d3c55f7$-- migration: 2026071901_identity_audit_foundation -- Retire an obsolete reconciliation phase before installing current checks. DO $reconciliation_upgrade$ BEGIN @@ -26,11 +66,11 @@ CREATE EXTENSION IF NOT EXISTS pg_trgm WITH SCHEMA public; CREATE EXTENSION IF NOT EXISTS fuzzystrmatch WITH SCHEMA public; -- Modify plate_notifications -ALTER TABLE IF EXISTS public.plate_notifications +ALTER TABLE IF EXISTS public.plate_notifications ADD COLUMN IF NOT EXISTS priority integer DEFAULT 1; -- Modify plate_reads -ALTER TABLE IF EXISTS public.plate_reads +ALTER TABLE IF EXISTS public.plate_reads ADD COLUMN IF NOT EXISTS camera_name character varying(25), ADD COLUMN IF NOT EXISTS image_path varchar(255), ADD COLUMN IF NOT EXISTS thumbnail_path varchar(255), @@ -58,11 +98,11 @@ CREATE UNIQUE INDEX IF NOT EXISTS uq_plate_reads_event_identity -- Please for the love of god work... -- Fix in reference to #57 and ipct reports about db config on new installs -DO $$ +DO $$ BEGIN IF NOT EXISTS ( - SELECT 1 FROM pg_constraint - WHERE conname = 'plates_pkey' + SELECT 1 FROM pg_constraint + WHERE conname = 'plates_pkey' AND conrelid = 'public.plates'::regclass ) THEN ALTER TABLE public.plates ADD CONSTRAINT plates_pkey PRIMARY KEY (plate_number); @@ -70,15 +110,15 @@ BEGIN END $$; -- Modify known_plates -ALTER TABLE IF EXISTS public.known_plates +ALTER TABLE IF EXISTS public.known_plates ADD COLUMN IF NOT EXISTS ignore BOOLEAN DEFAULT FALSE; -- Modify plates -ALTER TABLE IF EXISTS public.plates +ALTER TABLE IF EXISTS public.plates ADD COLUMN IF NOT EXISTS occurrence_count INTEGER NOT NULL DEFAULT 0; -- Create index if not exists -DO $$ +DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM pg_indexes WHERE indexname = 'idx_plates_occurrence_count') THEN CREATE INDEX idx_plates_occurrence_count ON plates(occurrence_count); @@ -95,7 +135,7 @@ BEGIN VALUES (NEW.plate_number, 1) ON CONFLICT (plate_number) DO UPDATE SET occurrence_count = plates.occurrence_count + 1; - + -- Handle UPDATE operation (plate number correction) ELSIF TG_OP = 'UPDATE' AND OLD.plate_number != NEW.plate_number THEN -- Increment the new plate number count (or create if not exists) @@ -103,40 +143,40 @@ BEGIN VALUES (NEW.plate_number, 1) ON CONFLICT (plate_number) DO UPDATE SET occurrence_count = plates.occurrence_count + 1; - + -- Only decrement the old plate if it still exists - UPDATE plates + UPDATE plates SET occurrence_count = occurrence_count - 1 WHERE plate_number = OLD.plate_number; - + -- Clean up if occurrence count reaches zero DELETE FROM plates WHERE plate_number = OLD.plate_number AND occurrence_count <= 0; - + -- Handle DELETE operation ELSIF TG_OP = 'DELETE' THEN -- Only attempt to decrement if the plate still exists - UPDATE plates + UPDATE plates SET occurrence_count = occurrence_count - 1 WHERE plate_number = OLD.plate_number; - + -- Clean up if occurrence count reaches zero DELETE FROM plates WHERE plate_number = OLD.plate_number AND occurrence_count <= 0; END IF; - + RETURN NULL; END; $$ LANGUAGE plpgsql; -- Update trigger to also handle UPDATE operations -DO $$ +DO $$ BEGIN -- Drop the existing trigger if it exists DROP TRIGGER IF EXISTS plate_reads_count_trigger ON plate_reads; - + -- Create the updated trigger CREATE TRIGGER plate_reads_count_trigger AFTER INSERT OR UPDATE OR DELETE ON plate_reads @@ -687,15 +727,58 @@ VALUES ( 'Create users, roles, permissions, database sessions, scoped credentials, and append-only audit events.' ) ON CONFLICT (version) DO NOTHING; +$source_f01d375f3d3c55f7$; + IF NOT EXISTS (SELECT 1 FROM public.schema_migrations WHERE version = '2026071901_identity_audit_foundation') THEN + RAISE EXCEPTION 'Migration did not record completion: 2026071901_identity_audit_foundation'; + END IF; + UPDATE public.schema_migrations SET sql_sha256 = 'f01d375f3d3c55f7d80ae6596d0e09a3a787f637557ff3fb82046333b0ed7474' WHERE version = '2026071901_identity_audit_foundation'; + END IF; +END; +$migration$; +-- 0002_2026071902_assistant_authorization.sql +DO $migration$ +DECLARE recorded_hash TEXT; +BEGIN + SELECT sql_sha256 INTO recorded_hash FROM public.schema_migrations WHERE version = '2026071902_assistant_authorization'; + IF FOUND THEN + IF recorded_hash IS NOT NULL AND recorded_hash <> 'd3df38fb02808c4eadb393d713721ee13b7e6e9a083d3a2bb4a53bb2f1419a4d' THEN + RAISE EXCEPTION 'Applied migration checksum differs: 2026071902_assistant_authorization'; + END IF; + IF recorded_hash IS NULL THEN + UPDATE public.schema_migrations SET sql_sha256 = 'd3df38fb02808c4eadb393d713721ee13b7e6e9a083d3a2bb4a53bb2f1419a4d' WHERE version = '2026071902_assistant_authorization'; + END IF; + ELSE + EXECUTE $source_d3df38fb02808c4e$-- migration: 2026071902_assistant_authorization INSERT INTO public.schema_migrations (version, description) VALUES ( '2026071902_assistant_authorization', 'Restrict AI Assistant access to explicitly authorized administrators.' ) ON CONFLICT (version) DO NOTHING; +$source_d3df38fb02808c4e$; + IF NOT EXISTS (SELECT 1 FROM public.schema_migrations WHERE version = '2026071902_assistant_authorization') THEN + RAISE EXCEPTION 'Migration did not record completion: 2026071902_assistant_authorization'; + END IF; + UPDATE public.schema_migrations SET sql_sha256 = 'd3df38fb02808c4eadb393d713721ee13b7e6e9a083d3a2bb4a53bb2f1419a4d' WHERE version = '2026071902_assistant_authorization'; + END IF; +END; +$migration$; - +-- 0003_2026071903_immutable_plate_reviews.sql +DO $migration$ +DECLARE recorded_hash TEXT; +BEGIN + SELECT sql_sha256 INTO recorded_hash FROM public.schema_migrations WHERE version = '2026071903_immutable_plate_reviews'; + IF FOUND THEN + IF recorded_hash IS NOT NULL AND recorded_hash <> '14949d1af86cf73b35f66cd46213645f49777bf20bca708f373d79d7b2f2c6e6' THEN + RAISE EXCEPTION 'Applied migration checksum differs: 2026071903_immutable_plate_reviews'; + END IF; + IF recorded_hash IS NULL THEN + UPDATE public.schema_migrations SET sql_sha256 = '14949d1af86cf73b35f66cd46213645f49777bf20bca708f373d79d7b2f2c6e6' WHERE version = '2026071903_immutable_plate_reviews'; + END IF; + ELSE + EXECUTE $source_14949d1af86cf73b$-- migration: 2026071903_immutable_plate_reviews -- Immutable plate observations, append-only review history, and reviewed aliases ALTER TABLE public.plate_reads ADD COLUMN IF NOT EXISTS observed_plate VARCHAR(10), @@ -888,7 +971,29 @@ VALUES ( 'Preserve observed plates, append plate review history, and add reviewed recurring aliases.' ) ON CONFLICT (version) DO NOTHING; +$source_14949d1af86cf73b$; + IF NOT EXISTS (SELECT 1 FROM public.schema_migrations WHERE version = '2026071903_immutable_plate_reviews') THEN + RAISE EXCEPTION 'Migration did not record completion: 2026071903_immutable_plate_reviews'; + END IF; + UPDATE public.schema_migrations SET sql_sha256 = '14949d1af86cf73b35f66cd46213645f49777bf20bca708f373d79d7b2f2c6e6' WHERE version = '2026071903_immutable_plate_reviews'; + END IF; +END; +$migration$; +-- 0004_2026072101_repair_plate_occurrence_counts.sql +DO $migration$ +DECLARE recorded_hash TEXT; +BEGIN + SELECT sql_sha256 INTO recorded_hash FROM public.schema_migrations WHERE version = '2026072101_repair_plate_occurrence_counts'; + IF FOUND THEN + IF recorded_hash IS NOT NULL AND recorded_hash <> 'b8be738359f8a585fbdca85704ae6b84d0884b0e309e06bf4e991d87a8a6ba5e' THEN + RAISE EXCEPTION 'Applied migration checksum differs: 2026072101_repair_plate_occurrence_counts'; + END IF; + IF recorded_hash IS NULL THEN + UPDATE public.schema_migrations SET sql_sha256 = 'b8be738359f8a585fbdca85704ae6b84d0884b0e309e06bf4e991d87a8a6ba5e' WHERE version = '2026072101_repair_plate_occurrence_counts'; + END IF; + ELSE + EXECUTE $source_b8be738359f8a585$-- migration: 2026072101_repair_plate_occurrence_counts -- Reconcile stored plate occurrence counts after bulk imports. DO $$ DECLARE @@ -950,8 +1055,29 @@ BEGIN ); END IF; END $$; +$source_b8be738359f8a585$; + IF NOT EXISTS (SELECT 1 FROM public.schema_migrations WHERE version = '2026072101_repair_plate_occurrence_counts') THEN + RAISE EXCEPTION 'Migration did not record completion: 2026072101_repair_plate_occurrence_counts'; + END IF; + UPDATE public.schema_migrations SET sql_sha256 = 'b8be738359f8a585fbdca85704ae6b84d0884b0e309e06bf4e991d87a8a6ba5e' WHERE version = '2026072101_repair_plate_occurrence_counts'; + END IF; +END; +$migration$; - +-- 0005_2026072201_unified_notification_foundation.sql +DO $migration$ +DECLARE recorded_hash TEXT; +BEGIN + SELECT sql_sha256 INTO recorded_hash FROM public.schema_migrations WHERE version = '2026072201_unified_notification_foundation'; + IF FOUND THEN + IF recorded_hash IS NOT NULL AND recorded_hash <> 'a0b21763fddc498b342ae6b50518c48f6da08efa9197fd4c8543ad63965d216c' THEN + RAISE EXCEPTION 'Applied migration checksum differs: 2026072201_unified_notification_foundation'; + END IF; + IF recorded_hash IS NULL THEN + UPDATE public.schema_migrations SET sql_sha256 = 'a0b21763fddc498b342ae6b50518c48f6da08efa9197fd4c8543ad63965d216c' WHERE version = '2026072201_unified_notification_foundation'; + END IF; + ELSE + EXECUTE $source_a0b21763fddc498b$-- migration: 2026072201_unified_notification_foundation -- Channel-neutral notification rule foundation ----------------------------- -- This schema is intentionally inert until existing Pushover and MQTT paths -- are migrated in a later release. New rules default disabled, and this @@ -1172,7 +1298,29 @@ VALUES ( 'Add inert channel-neutral rules, nested conditions, actions, executions, deliveries, and attempts.' ) ON CONFLICT (version) DO NOTHING; +$source_a0b21763fddc498b$; + IF NOT EXISTS (SELECT 1 FROM public.schema_migrations WHERE version = '2026072201_unified_notification_foundation') THEN + RAISE EXCEPTION 'Migration did not record completion: 2026072201_unified_notification_foundation'; + END IF; + UPDATE public.schema_migrations SET sql_sha256 = 'a0b21763fddc498b342ae6b50518c48f6da08efa9197fd4c8543ad63965d216c' WHERE version = '2026072201_unified_notification_foundation'; + END IF; +END; +$migration$; +-- 0006_2026072202_notification_migration_preview.sql +DO $migration$ +DECLARE recorded_hash TEXT; +BEGIN + SELECT sql_sha256 INTO recorded_hash FROM public.schema_migrations WHERE version = '2026072202_notification_migration_preview'; + IF FOUND THEN + IF recorded_hash IS NOT NULL AND recorded_hash <> '36dcaafb49cc41d4bb68bf108d6410a77be03dc8b4665fd8011ac31382ed61a6' THEN + RAISE EXCEPTION 'Applied migration checksum differs: 2026072202_notification_migration_preview'; + END IF; + IF recorded_hash IS NULL THEN + UPDATE public.schema_migrations SET sql_sha256 = '36dcaafb49cc41d4bb68bf108d6410a77be03dc8b4665fd8011ac31382ed61a6' WHERE version = '2026072202_notification_migration_preview'; + END IF; + ELSE + EXECUTE $source_36dcaafb49cc41d4$-- migration: 2026072202_notification_migration_preview -- Extend the inert shared evaluator vocabulary for legacy known-name rules. -- This still copies no rules and changes no active delivery path. ALTER TABLE IF EXISTS public.notification_conditions @@ -1199,7 +1347,29 @@ VALUES ( 'Add known-name rule vocabulary for the read-only legacy notification migration preview.' ) ON CONFLICT (version) DO NOTHING; +$source_36dcaafb49cc41d4$; + IF NOT EXISTS (SELECT 1 FROM public.schema_migrations WHERE version = '2026072202_notification_migration_preview') THEN + RAISE EXCEPTION 'Migration did not record completion: 2026072202_notification_migration_preview'; + END IF; + UPDATE public.schema_migrations SET sql_sha256 = '36dcaafb49cc41d4bb68bf108d6410a77be03dc8b4665fd8011ac31382ed61a6' WHERE version = '2026072202_notification_migration_preview'; + END IF; +END; +$migration$; +-- 0007_2026072203_disabled_notification_rule_migration.sql +DO $migration$ +DECLARE recorded_hash TEXT; +BEGIN + SELECT sql_sha256 INTO recorded_hash FROM public.schema_migrations WHERE version = '2026072203_disabled_notification_rule_migration'; + IF FOUND THEN + IF recorded_hash IS NOT NULL AND recorded_hash <> 'fd4e2c6faaee48f2b6bce8150d7994e744a48f2d2e13c93b074bcc60227697db' THEN + RAISE EXCEPTION 'Applied migration checksum differs: 2026072203_disabled_notification_rule_migration'; + END IF; + IF recorded_hash IS NULL THEN + UPDATE public.schema_migrations SET sql_sha256 = 'fd4e2c6faaee48f2b6bce8150d7994e744a48f2d2e13c93b074bcc60227697db' WHERE version = '2026072203_disabled_notification_rule_migration'; + END IF; + ELSE + EXECUTE $source_fd4e2c6faaee48f2$-- migration: 2026072203_disabled_notification_rule_migration -- Record each legacy rule copied into the inert unified model. The unique source -- identity makes the application migration safe to retry, while the restricted -- target reference preserves provenance for later review and cutover. @@ -1224,7 +1394,29 @@ VALUES ( 'Track idempotent disabled-only copies of legacy Pushover and MQTT rules.' ) ON CONFLICT (version) DO NOTHING; +$source_fd4e2c6faaee48f2$; + IF NOT EXISTS (SELECT 1 FROM public.schema_migrations WHERE version = '2026072203_disabled_notification_rule_migration') THEN + RAISE EXCEPTION 'Migration did not record completion: 2026072203_disabled_notification_rule_migration'; + END IF; + UPDATE public.schema_migrations SET sql_sha256 = 'fd4e2c6faaee48f2b6bce8150d7994e744a48f2d2e13c93b074bcc60227697db' WHERE version = '2026072203_disabled_notification_rule_migration'; + END IF; +END; +$migration$; +-- 0008_2026072204_notification_shadow_review.sql +DO $migration$ +DECLARE recorded_hash TEXT; +BEGIN + SELECT sql_sha256 INTO recorded_hash FROM public.schema_migrations WHERE version = '2026072204_notification_shadow_review'; + IF FOUND THEN + IF recorded_hash IS NOT NULL AND recorded_hash <> 'a63c8c4819806838ed90fbb1050fb4157c8720a77852e42c4bd15507eff503bf' THEN + RAISE EXCEPTION 'Applied migration checksum differs: 2026072204_notification_shadow_review'; + END IF; + IF recorded_hash IS NULL THEN + UPDATE public.schema_migrations SET sql_sha256 = 'a63c8c4819806838ed90fbb1050fb4157c8720a77852e42c4bd15507eff503bf' WHERE version = '2026072204_notification_shadow_review'; + END IF; + ELSE + EXECUTE $source_a63c8c4819806838$-- migration: 2026072204_notification_shadow_review -- Append-only administrator evidence for a specific disabled rule version and -- exact shadow-test sample. Recording a review cannot enable or change a rule. CREATE TABLE IF NOT EXISTS public.notification_rule_shadow_reviews ( @@ -1264,7 +1456,29 @@ VALUES ( 'Add append-only approval evidence for safe disabled unified-rule shadow comparisons.' ) ON CONFLICT (version) DO NOTHING; +$source_a63c8c4819806838$; + IF NOT EXISTS (SELECT 1 FROM public.schema_migrations WHERE version = '2026072204_notification_shadow_review') THEN + RAISE EXCEPTION 'Migration did not record completion: 2026072204_notification_shadow_review'; + END IF; + UPDATE public.schema_migrations SET sql_sha256 = 'a63c8c4819806838ed90fbb1050fb4157c8720a77852e42c4bd15507eff503bf' WHERE version = '2026072204_notification_shadow_review'; + END IF; +END; +$migration$; +-- 0009_2026072205_guarded_notification_cutover.sql +DO $migration$ +DECLARE recorded_hash TEXT; +BEGIN + SELECT sql_sha256 INTO recorded_hash FROM public.schema_migrations WHERE version = '2026072205_guarded_notification_cutover'; + IF FOUND THEN + IF recorded_hash IS NOT NULL AND recorded_hash <> '2b1d3ab7450601319d10e8a7f0acdb726c2e08ecd7338b1caa07b3edc29e7a51' THEN + RAISE EXCEPTION 'Applied migration checksum differs: 2026072205_guarded_notification_cutover'; + END IF; + IF recorded_hash IS NULL THEN + UPDATE public.schema_migrations SET sql_sha256 = '2b1d3ab7450601319d10e8a7f0acdb726c2e08ecd7338b1caa07b3edc29e7a51' WHERE version = '2026072205_guarded_notification_cutover'; + END IF; + ELSE + EXECUTE $source_2b1d3ab745060131$-- migration: 2026072205_guarded_notification_cutover -- Append-only history for explicit per-rule cutovers and rollbacks. The live -- source/target enabled flags remain the source of truth; these rows preserve -- who changed them, which reviewed version was involved, and why. @@ -1304,7 +1518,29 @@ VALUES ( 'Add append-only evidence for guarded per-rule unified notification cutover and rollback.' ) ON CONFLICT (version) DO NOTHING; +$source_2b1d3ab745060131$; + IF NOT EXISTS (SELECT 1 FROM public.schema_migrations WHERE version = '2026072205_guarded_notification_cutover') THEN + RAISE EXCEPTION 'Migration did not record completion: 2026072205_guarded_notification_cutover'; + END IF; + UPDATE public.schema_migrations SET sql_sha256 = '2b1d3ab7450601319d10e8a7f0acdb726c2e08ecd7338b1caa07b3edc29e7a51' WHERE version = '2026072205_guarded_notification_cutover'; + END IF; +END; +$migration$; +-- 0010_2026072401_monitored_plates_and_orphan_retirement.sql +DO $migration$ +DECLARE recorded_hash TEXT; +BEGIN + SELECT sql_sha256 INTO recorded_hash FROM public.schema_migrations WHERE version = '2026072401_monitored_plates_and_orphan_retirement'; + IF FOUND THEN + IF recorded_hash IS NOT NULL AND recorded_hash <> 'be020451a14b272ecafc6259104a3cdd6cc4e493576a26fcf3674fa08d75359e' THEN + RAISE EXCEPTION 'Applied migration checksum differs: 2026072401_monitored_plates_and_orphan_retirement'; + END IF; + IF recorded_hash IS NULL THEN + UPDATE public.schema_migrations SET sql_sha256 = 'be020451a14b272ecafc6259104a3cdd6cc4e493576a26fcf3674fa08d75359e' WHERE version = '2026072401_monitored_plates_and_orphan_retirement'; + END IF; + ELSE + EXECUTE $source_be020451a14b272e$-- migration: 2026072401_monitored_plates_and_orphan_retirement -- Preserve the legacy flagged boolean for evaluator compatibility while adding -- user-facing monitoring context. Existing monitored plates receive a stable -- timestamp and the normal priority; original reads and plate identities are @@ -1343,7 +1579,29 @@ VALUES ( 'Add monitored-plate context and audited retirement for safely disabled orphaned notification migrations.' ) ON CONFLICT (version) DO NOTHING; +$source_be020451a14b272e$; + IF NOT EXISTS (SELECT 1 FROM public.schema_migrations WHERE version = '2026072401_monitored_plates_and_orphan_retirement') THEN + RAISE EXCEPTION 'Migration did not record completion: 2026072401_monitored_plates_and_orphan_retirement'; + END IF; + UPDATE public.schema_migrations SET sql_sha256 = 'be020451a14b272ecafc6259104a3cdd6cc4e493576a26fcf3674fa08d75359e' WHERE version = '2026072401_monitored_plates_and_orphan_retirement'; + END IF; +END; +$migration$; +-- 0011_2026072403_finalize_legacy_mqtt_rules.sql +DO $migration$ +DECLARE recorded_hash TEXT; +BEGIN + SELECT sql_sha256 INTO recorded_hash FROM public.schema_migrations WHERE version = '2026072403_finalize_legacy_mqtt_rules'; + IF FOUND THEN + IF recorded_hash IS NOT NULL AND recorded_hash <> 'e3208e285ff5ac876bcc58e2e1763b6e6fb65a2473228da0a22d233511f0d515' THEN + RAISE EXCEPTION 'Applied migration checksum differs: 2026072403_finalize_legacy_mqtt_rules'; + END IF; + IF recorded_hash IS NULL THEN + UPDATE public.schema_migrations SET sql_sha256 = 'e3208e285ff5ac876bcc58e2e1763b6e6fb65a2473228da0a22d233511f0d515' WHERE version = '2026072403_finalize_legacy_mqtt_rules'; + END IF; + ELSE + EXECUTE $source_e3208e285ff5ac87$-- migration: 2026072403_finalize_legacy_mqtt_rules -- Finalization is the deliberate end of a legacy notification rule runtime. Preserve -- an immutable, credential-free copy of each source rule before deleting it, -- then remove the mapping from active migration/cutover workflows. Unified @@ -1382,7 +1640,29 @@ VALUES ( 'Archive verified cutover MQTT source rules and remove their legacy runtime rows.' ) ON CONFLICT (version) DO NOTHING; +$source_e3208e285ff5ac87$; + IF NOT EXISTS (SELECT 1 FROM public.schema_migrations WHERE version = '2026072403_finalize_legacy_mqtt_rules') THEN + RAISE EXCEPTION 'Migration did not record completion: 2026072403_finalize_legacy_mqtt_rules'; + END IF; + UPDATE public.schema_migrations SET sql_sha256 = 'e3208e285ff5ac876bcc58e2e1763b6e6fb65a2473228da0a22d233511f0d515' WHERE version = '2026072403_finalize_legacy_mqtt_rules'; + END IF; +END; +$migration$; +-- 0012_2026072404_finalize_legacy_pushover_rules.sql +DO $migration$ +DECLARE recorded_hash TEXT; +BEGIN + SELECT sql_sha256 INTO recorded_hash FROM public.schema_migrations WHERE version = '2026072404_finalize_legacy_pushover_rules'; + IF FOUND THEN + IF recorded_hash IS NOT NULL AND recorded_hash <> '220121de1db7202627f8a4d63a983244a0b0dcac8b6398b7560d07d9edbc50c5' THEN + RAISE EXCEPTION 'Applied migration checksum differs: 2026072404_finalize_legacy_pushover_rules'; + END IF; + IF recorded_hash IS NULL THEN + UPDATE public.schema_migrations SET sql_sha256 = '220121de1db7202627f8a4d63a983244a0b0dcac8b6398b7560d07d9edbc50c5' WHERE version = '2026072404_finalize_legacy_pushover_rules'; + END IF; + ELSE + EXECUTE $source_220121de1db72026$-- migration: 2026072404_finalize_legacy_pushover_rules -- The same verified-delivery boundary now retires legacy exact-plate Pushover -- rows. No credentials are copied into the immutable source snapshot. INSERT INTO public.schema_migrations (version, description) @@ -1391,7 +1671,29 @@ VALUES ( 'Extend verified legacy finalization and immutable snapshots to migrated Pushover rules.' ) ON CONFLICT (version) DO NOTHING; +$source_220121de1db72026$; + IF NOT EXISTS (SELECT 1 FROM public.schema_migrations WHERE version = '2026072404_finalize_legacy_pushover_rules') THEN + RAISE EXCEPTION 'Migration did not record completion: 2026072404_finalize_legacy_pushover_rules'; + END IF; + UPDATE public.schema_migrations SET sql_sha256 = '220121de1db7202627f8a4d63a983244a0b0dcac8b6398b7560d07d9edbc50c5' WHERE version = '2026072404_finalize_legacy_pushover_rules'; + END IF; +END; +$migration$; +-- 0013_2026072206_disabled_rule_editor.sql +DO $migration$ +DECLARE recorded_hash TEXT; +BEGIN + SELECT sql_sha256 INTO recorded_hash FROM public.schema_migrations WHERE version = '2026072206_disabled_rule_editor'; + IF FOUND THEN + IF recorded_hash IS NOT NULL AND recorded_hash <> 'f03cee25c2a5855e768b8ec65252849cb8743e58130fae4aa64d6a148cf51ae2' THEN + RAISE EXCEPTION 'Applied migration checksum differs: 2026072206_disabled_rule_editor'; + END IF; + IF recorded_hash IS NULL THEN + UPDATE public.schema_migrations SET sql_sha256 = 'f03cee25c2a5855e768b8ec65252849cb8743e58130fae4aa64d6a148cf51ae2' WHERE version = '2026072206_disabled_rule_editor'; + END IF; + ELSE + EXECUTE $source_f03cee25c2a5855e$-- migration: 2026072206_disabled_rule_editor -- Disabled unified rules may intentionally expand beyond legacy behavior. -- Evidence remains append-only and version/fingerprint bound; a regression -- (legacy match lost by unified logic) can never use this approval mode. @@ -1449,7 +1751,29 @@ VALUES ( 'Allow version-bound approval of intentional disabled-rule expansions without enabling delivery.' ) ON CONFLICT (version) DO NOTHING; +$source_f03cee25c2a5855e$; + IF NOT EXISTS (SELECT 1 FROM public.schema_migrations WHERE version = '2026072206_disabled_rule_editor') THEN + RAISE EXCEPTION 'Migration did not record completion: 2026072206_disabled_rule_editor'; + END IF; + UPDATE public.schema_migrations SET sql_sha256 = 'f03cee25c2a5855e768b8ec65252849cb8743e58130fae4aa64d6a148cf51ae2' WHERE version = '2026072206_disabled_rule_editor'; + END IF; +END; +$migration$; +-- 0014_2026072402_notification_operations.sql +DO $migration$ +DECLARE recorded_hash TEXT; +BEGIN + SELECT sql_sha256 INTO recorded_hash FROM public.schema_migrations WHERE version = '2026072402_notification_operations'; + IF FOUND THEN + IF recorded_hash IS NOT NULL AND recorded_hash <> 'ecf177871c9298c23f498c54445731addc6d28fb91cf69cbc8a81c16cfecf4b2' THEN + RAISE EXCEPTION 'Applied migration checksum differs: 2026072402_notification_operations'; + END IF; + IF recorded_hash IS NULL THEN + UPDATE public.schema_migrations SET sql_sha256 = 'ecf177871c9298c23f498c54445731addc6d28fb91cf69cbc8a81c16cfecf4b2' WHERE version = '2026072402_notification_operations'; + END IF; + ELSE + EXECUTE $source_ecf177871c9298c2$-- migration: 2026072402_notification_operations -- Notification operations adds an explicit rule clock, optional quiet hours, -- and lease-safe scheduled evaluation for camera activity rules. Existing -- accepted-read rules retain their behavior and remain unscheduled. @@ -1490,7 +1814,29 @@ VALUES ( 'Add explicit rule time zones, quiet hours, and lease-safe scheduled camera activity evaluation.' ) ON CONFLICT (version) DO NOTHING; +$source_ecf177871c9298c2$; + IF NOT EXISTS (SELECT 1 FROM public.schema_migrations WHERE version = '2026072402_notification_operations') THEN + RAISE EXCEPTION 'Migration did not record completion: 2026072402_notification_operations'; + END IF; + UPDATE public.schema_migrations SET sql_sha256 = 'ecf177871c9298c23f498c54445731addc6d28fb91cf69cbc8a81c16cfecf4b2' WHERE version = '2026072402_notification_operations'; + END IF; +END; +$migration$; +-- 0015_2026072501_notification_rule_soft_delete.sql +DO $migration$ +DECLARE recorded_hash TEXT; +BEGIN + SELECT sql_sha256 INTO recorded_hash FROM public.schema_migrations WHERE version = '2026072501_notification_rule_soft_delete'; + IF FOUND THEN + IF recorded_hash IS NOT NULL AND recorded_hash <> '595308ec24272af10c554739f3db738da61cd8e3eb5ef76fc5014908d61e24bd' THEN + RAISE EXCEPTION 'Applied migration checksum differs: 2026072501_notification_rule_soft_delete'; + END IF; + IF recorded_hash IS NULL THEN + UPDATE public.schema_migrations SET sql_sha256 = '595308ec24272af10c554739f3db738da61cd8e3eb5ef76fc5014908d61e24bd' WHERE version = '2026072501_notification_rule_soft_delete'; + END IF; + ELSE + EXECUTE $source_595308ec24272af1$-- migration: 2026072501_notification_rule_soft_delete -- Administrators can remove disabled rules from the active workspace without -- breaking immutable execution, delivery, migration, or audit references. ALTER TABLE public.notification_rules @@ -1553,7 +1899,29 @@ VALUES ( 'Allow guarded deletion of disabled notification rules and repair inherited UTC defaults on legacy-migrated rules.' ) ON CONFLICT (version) DO NOTHING; +$source_595308ec24272af1$; + IF NOT EXISTS (SELECT 1 FROM public.schema_migrations WHERE version = '2026072501_notification_rule_soft_delete') THEN + RAISE EXCEPTION 'Migration did not record completion: 2026072501_notification_rule_soft_delete'; + END IF; + UPDATE public.schema_migrations SET sql_sha256 = '595308ec24272af10c554739f3db738da61cd8e3eb5ef76fc5014908d61e24bd' WHERE version = '2026072501_notification_rule_soft_delete'; + END IF; +END; +$migration$; +-- 0016_2026072502_retention_maintenance_preview.sql +DO $migration$ +DECLARE recorded_hash TEXT; +BEGIN + SELECT sql_sha256 INTO recorded_hash FROM public.schema_migrations WHERE version = '2026072502_retention_maintenance_preview'; + IF FOUND THEN + IF recorded_hash IS NOT NULL AND recorded_hash <> '25475c187788e013227d43b8db6dbf7dc9c7975cfb97894e0563a4a96e0c8565' THEN + RAISE EXCEPTION 'Applied migration checksum differs: 2026072502_retention_maintenance_preview'; + END IF; + IF recorded_hash IS NULL THEN + UPDATE public.schema_migrations SET sql_sha256 = '25475c187788e013227d43b8db6dbf7dc9c7975cfb97894e0563a4a96e0c8565' WHERE version = '2026072502_retention_maintenance_preview'; + END IF; + ELSE + EXECUTE $source_25475c187788e013$-- migration: 2026072502_retention_maintenance_preview -- Retention planning now runs outside ingestion. The initial maintenance job -- is deliberately dry-run only: it records bounded database candidate counts -- and never deletes rows or files. @@ -1587,7 +1955,29 @@ VALUES ( 'Move retention planning out of ingestion into a scheduled, single-flight, dry-run-only maintenance worker.' ) ON CONFLICT (version) DO NOTHING; +$source_25475c187788e013$; + IF NOT EXISTS (SELECT 1 FROM public.schema_migrations WHERE version = '2026072502_retention_maintenance_preview') THEN + RAISE EXCEPTION 'Migration did not record completion: 2026072502_retention_maintenance_preview'; + END IF; + UPDATE public.schema_migrations SET sql_sha256 = '25475c187788e013227d43b8db6dbf7dc9c7975cfb97894e0563a4a96e0c8565' WHERE version = '2026072502_retention_maintenance_preview'; + END IF; +END; +$migration$; +-- 0017_2026072503_storage_reconciliation.sql +DO $migration$ +DECLARE recorded_hash TEXT; +BEGIN + SELECT sql_sha256 INTO recorded_hash FROM public.schema_migrations WHERE version = '2026072503_storage_reconciliation'; + IF FOUND THEN + IF recorded_hash IS NOT NULL AND recorded_hash <> 'd1023a50501251478072af0961033d7201bc8bd8b360463ccd4ed4cda0670fb0' THEN + RAISE EXCEPTION 'Applied migration checksum differs: 2026072503_storage_reconciliation'; + END IF; + IF recorded_hash IS NULL THEN + UPDATE public.schema_migrations SET sql_sha256 = 'd1023a50501251478072af0961033d7201bc8bd8b360463ccd4ed4cda0670fb0' WHERE version = '2026072503_storage_reconciliation'; + END IF; + ELSE + EXECUTE $source_d1023a5050125147$-- migration: 2026072503_storage_reconciliation -- Read-only storage reconciliation persists resumable traversal state and an -- exact finding inventory. These tables do not provide any deletion action. CREATE TABLE IF NOT EXISTS public.storage_reconciliation_runs ( @@ -1667,7 +2057,29 @@ VALUES ( 'Add bounded resumable read-only storage reconciliation runs and durable orphan/missing-reference inventory.' ) ON CONFLICT (version) DO NOTHING; +$source_d1023a5050125147$; + IF NOT EXISTS (SELECT 1 FROM public.schema_migrations WHERE version = '2026072503_storage_reconciliation') THEN + RAISE EXCEPTION 'Migration did not record completion: 2026072503_storage_reconciliation'; + END IF; + UPDATE public.schema_migrations SET sql_sha256 = 'd1023a50501251478072af0961033d7201bc8bd8b360463ccd4ed4cda0670fb0' WHERE version = '2026072503_storage_reconciliation'; + END IF; +END; +$migration$; +-- 0018_2026072504_vehicle_direction_profiles.sql +DO $migration$ +DECLARE recorded_hash TEXT; +BEGIN + SELECT sql_sha256 INTO recorded_hash FROM public.schema_migrations WHERE version = '2026072504_vehicle_direction_profiles'; + IF FOUND THEN + IF recorded_hash IS NOT NULL AND recorded_hash <> 'd7fd8819edefee8b40b43c5b5c46b0032d76210ed7d7c30ff77ed36b06abca66' THEN + RAISE EXCEPTION 'Applied migration checksum differs: 2026072504_vehicle_direction_profiles'; + END IF; + IF recorded_hash IS NULL THEN + UPDATE public.schema_migrations SET sql_sha256 = 'd7fd8819edefee8b40b43c5b5c46b0032d76210ed7d7c30ff77ed36b06abca66' WHERE version = '2026072504_vehicle_direction_profiles'; + END IF; + ELSE + EXECUTE $source_d7fd8819edefee8b$-- migration: 2026072504_vehicle_direction_profiles -- Recover a reconciliation run that failed before automatic retry scheduling -- was available. Future failures set this bounded retry directly at runtime. UPDATE public.maintenance_job_state @@ -1744,7 +2156,29 @@ VALUES ( 'Add configurable per-camera direction meanings and audited ReID-assisted front/rear calibration.' ) ON CONFLICT (version) DO NOTHING; +$source_d7fd8819edefee8b$; + IF NOT EXISTS (SELECT 1 FROM public.schema_migrations WHERE version = '2026072504_vehicle_direction_profiles') THEN + RAISE EXCEPTION 'Migration did not record completion: 2026072504_vehicle_direction_profiles'; + END IF; + UPDATE public.schema_migrations SET sql_sha256 = 'd7fd8819edefee8b40b43c5b5c46b0032d76210ed7d7c30ff77ed36b06abca66' WHERE version = '2026072504_vehicle_direction_profiles'; + END IF; +END; +$migration$; +-- 0019_2026072601_vehicle_direction_notifications.sql +DO $migration$ +DECLARE recorded_hash TEXT; +BEGIN + SELECT sql_sha256 INTO recorded_hash FROM public.schema_migrations WHERE version = '2026072601_vehicle_direction_notifications'; + IF FOUND THEN + IF recorded_hash IS NOT NULL AND recorded_hash <> '332c329d9bc25d868a09f2077dd6928b87ca4481a651ddd3a9c4a5cd77f4ab2b' THEN + RAISE EXCEPTION 'Applied migration checksum differs: 2026072601_vehicle_direction_notifications'; + END IF; + IF recorded_hash IS NULL THEN + UPDATE public.schema_migrations SET sql_sha256 = '332c329d9bc25d868a09f2077dd6928b87ca4481a651ddd3a9c4a5cd77f4ab2b' WHERE version = '2026072601_vehicle_direction_notifications'; + END IF; + ELSE + EXECUTE $source_332c329d9bc25d86$-- migration: 2026072601_vehicle_direction_notifications -- Direction classification is emitted after Vehicle ReID completes, so it has -- a distinct event type and can be filtered using camera-configured labels. ALTER TABLE IF EXISTS public.notification_rules @@ -1782,8 +2216,29 @@ VALUES ( 'Add direction-classified notification events and camera-configured direction conditions.' ) ON CONFLICT (version) DO NOTHING; +$source_332c329d9bc25d86$; + IF NOT EXISTS (SELECT 1 FROM public.schema_migrations WHERE version = '2026072601_vehicle_direction_notifications') THEN + RAISE EXCEPTION 'Migration did not record completion: 2026072601_vehicle_direction_notifications'; + END IF; + UPDATE public.schema_migrations SET sql_sha256 = '332c329d9bc25d868a09f2077dd6928b87ca4481a651ddd3a9c4a5cd77f4ab2b' WHERE version = '2026072601_vehicle_direction_notifications'; + END IF; +END; +$migration$; - +-- 0020_2026072603_vehicle_direction_backfill.sql +DO $migration$ +DECLARE recorded_hash TEXT; +BEGIN + SELECT sql_sha256 INTO recorded_hash FROM public.schema_migrations WHERE version = '2026072603_vehicle_direction_backfill'; + IF FOUND THEN + IF recorded_hash IS NOT NULL AND recorded_hash <> '2781fff2dcc8064ea3ef57cceac76d6946d3815dad632f1a94499e87ec399482' THEN + RAISE EXCEPTION 'Applied migration checksum differs: 2026072603_vehicle_direction_backfill'; + END IF; + IF recorded_hash IS NULL THEN + UPDATE public.schema_migrations SET sql_sha256 = '2781fff2dcc8064ea3ef57cceac76d6946d3815dad632f1a94499e87ec399482' WHERE version = '2026072603_vehicle_direction_backfill'; + END IF; + ELSE + EXECUTE $source_2781fff2dcc8064e$-- migration: 2026072603_vehicle_direction_backfill -- Historical direction work is derived from current canonical crop evidence and is -- naturally resumable: current observations are skipped, while repeat -- failures are retained for review instead of blocking the remaining corpus. @@ -1810,7 +2265,29 @@ VALUES ( 'Add paced resumable historical direction backfill with bounded failure tracking.' ) ON CONFLICT (version) DO NOTHING; +$source_2781fff2dcc8064e$; + IF NOT EXISTS (SELECT 1 FROM public.schema_migrations WHERE version = '2026072603_vehicle_direction_backfill') THEN + RAISE EXCEPTION 'Migration did not record completion: 2026072603_vehicle_direction_backfill'; + END IF; + UPDATE public.schema_migrations SET sql_sha256 = '2781fff2dcc8064ea3ef57cceac76d6946d3815dad632f1a94499e87ec399482' WHERE version = '2026072603_vehicle_direction_backfill'; + END IF; +END; +$migration$; +-- 0021_2026072701_vehicle_direction_reevaluation_queue.sql +DO $migration$ +DECLARE recorded_hash TEXT; +BEGIN + SELECT sql_sha256 INTO recorded_hash FROM public.schema_migrations WHERE version = '2026072701_vehicle_direction_reevaluation_queue'; + IF FOUND THEN + IF recorded_hash IS NOT NULL AND recorded_hash <> 'f2ad1c134cb693aaefcdc51b481173b4cf4a3201d37c0033ff2b8db984e3cde8' THEN + RAISE EXCEPTION 'Applied migration checksum differs: 2026072701_vehicle_direction_reevaluation_queue'; + END IF; + IF recorded_hash IS NULL THEN + UPDATE public.schema_migrations SET sql_sha256 = 'f2ad1c134cb693aaefcdc51b481173b4cf4a3201d37c0033ff2b8db984e3cde8' WHERE version = '2026072701_vehicle_direction_reevaluation_queue'; + END IF; + ELSE + EXECUTE $source_f2ad1c134cb693aa$-- migration: 2026072701_vehicle_direction_reevaluation_queue -- Historical re-evaluation is queued separately from ordinary live direction -- work. Existing observations remain visible until their replacement is ready, -- and an administrator can pause only the historical queue without delaying @@ -1842,7 +2319,29 @@ VALUES ( 'Preserve current directions during re-evaluation and add durable pause/resume controls.' ) ON CONFLICT (version) DO NOTHING; +$source_f2ad1c134cb693aa$; + IF NOT EXISTS (SELECT 1 FROM public.schema_migrations WHERE version = '2026072701_vehicle_direction_reevaluation_queue') THEN + RAISE EXCEPTION 'Migration did not record completion: 2026072701_vehicle_direction_reevaluation_queue'; + END IF; + UPDATE public.schema_migrations SET sql_sha256 = 'f2ad1c134cb693aaefcdc51b481173b4cf4a3201d37c0033ff2b8db984e3cde8' WHERE version = '2026072701_vehicle_direction_reevaluation_queue'; + END IF; +END; +$migration$; +-- 0022_2026072703_notification_action_history.sql +DO $migration$ +DECLARE recorded_hash TEXT; +BEGIN + SELECT sql_sha256 INTO recorded_hash FROM public.schema_migrations WHERE version = '2026072703_notification_action_history'; + IF FOUND THEN + IF recorded_hash IS NOT NULL AND recorded_hash <> '388dd00019c45870a8019554a2765e85261c66128fe98e6aa3fa00100f40c522' THEN + RAISE EXCEPTION 'Applied migration checksum differs: 2026072703_notification_action_history'; + END IF; + IF recorded_hash IS NULL THEN + UPDATE public.schema_migrations SET sql_sha256 = '388dd00019c45870a8019554a2765e85261c66128fe98e6aa3fa00100f40c522' WHERE version = '2026072703_notification_action_history'; + END IF; + ELSE + EXECUTE $source_388dd00019c45870$-- migration: 2026072703_notification_action_history -- Editing a disabled notification rule must not erase the action identity -- retained by delivery history. Keep prior versions as inert historical rows -- and reserve each position only within the current, non-retired action set. @@ -1870,7 +2369,29 @@ VALUES ( 'Preserve delivered notification action identities when disabled rules are edited.' ) ON CONFLICT (version) DO NOTHING; +$source_388dd00019c45870$; + IF NOT EXISTS (SELECT 1 FROM public.schema_migrations WHERE version = '2026072703_notification_action_history') THEN + RAISE EXCEPTION 'Migration did not record completion: 2026072703_notification_action_history'; + END IF; + UPDATE public.schema_migrations SET sql_sha256 = '388dd00019c45870a8019554a2765e85261c66128fe98e6aa3fa00100f40c522' WHERE version = '2026072703_notification_action_history'; + END IF; +END; +$migration$; +-- 0023_2026072704_blue_iris_alert_correlation.sql +DO $migration$ +DECLARE recorded_hash TEXT; +BEGIN + SELECT sql_sha256 INTO recorded_hash FROM public.schema_migrations WHERE version = '2026072704_blue_iris_alert_correlation'; + IF FOUND THEN + IF recorded_hash IS NOT NULL AND recorded_hash <> '0e17945cdd4693445e2badc808ed505bd3c96d57f8b77e65d94508ae4c44259d' THEN + RAISE EXCEPTION 'Applied migration checksum differs: 2026072704_blue_iris_alert_correlation'; + END IF; + IF recorded_hash IS NULL THEN + UPDATE public.schema_migrations SET sql_sha256 = '0e17945cdd4693445e2badc808ed505bd3c96d57f8b77e65d94508ae4c44259d' WHERE version = '2026072704_blue_iris_alert_correlation'; + END IF; + ELSE + EXECUTE $source_0e17945cdd469344$-- migration: 2026072704_blue_iris_alert_correlation -- Preserve the Blue Iris alert pointer received with new plate reads. This is -- metadata only: the continuous BVR recording remains managed by Blue Iris. -- Historical reads can be correlated through the read-only alertlist API. @@ -1889,7 +2410,29 @@ VALUES ( 'Preserve Blue Iris alert clip and offset metadata for read-only continuous-recording correlation.' ) ON CONFLICT (version) DO NOTHING; +$source_0e17945cdd469344$; + IF NOT EXISTS (SELECT 1 FROM public.schema_migrations WHERE version = '2026072704_blue_iris_alert_correlation') THEN + RAISE EXCEPTION 'Migration did not record completion: 2026072704_blue_iris_alert_correlation'; + END IF; + UPDATE public.schema_migrations SET sql_sha256 = '0e17945cdd4693445e2badc808ed505bd3c96d57f8b77e65d94508ae4c44259d' WHERE version = '2026072704_blue_iris_alert_correlation'; + END IF; +END; +$migration$; +-- 0024_2026072801_blue_iris_vehicle_frames.sql +DO $migration$ +DECLARE recorded_hash TEXT; +BEGIN + SELECT sql_sha256 INTO recorded_hash FROM public.schema_migrations WHERE version = '2026072801_blue_iris_vehicle_frames'; + IF FOUND THEN + IF recorded_hash IS NOT NULL AND recorded_hash <> '2b726d3e20e2269bc89af2bbce634f36a6711943690305e52b2820b0889b5e1b' THEN + RAISE EXCEPTION 'Applied migration checksum differs: 2026072801_blue_iris_vehicle_frames'; + END IF; + IF recorded_hash IS NULL THEN + UPDATE public.schema_migrations SET sql_sha256 = '2b726d3e20e2269bc89af2bbce634f36a6711943690305e52b2820b0889b5e1b' WHERE version = '2026072801_blue_iris_vehicle_frames'; + END IF; + ELSE + EXECUTE $source_2b726d3e20e2269b$-- migration: 2026072801_blue_iris_vehicle_frames -- Keep at most one derived Blue Iris vehicle-overview frame per plate read. -- The source BVR remains in Blue Iris; only the best bounded sample is retained. ALTER TABLE IF EXISTS public.plate_reads @@ -1922,7 +2465,29 @@ VALUES ( 'Retain one best derived Blue Iris vehicle-overview frame per ALPR read with terminal retention-aware states.' ) ON CONFLICT (version) DO NOTHING; +$source_2b726d3e20e2269b$; + IF NOT EXISTS (SELECT 1 FROM public.schema_migrations WHERE version = '2026072801_blue_iris_vehicle_frames') THEN + RAISE EXCEPTION 'Migration did not record completion: 2026072801_blue_iris_vehicle_frames'; + END IF; + UPDATE public.schema_migrations SET sql_sha256 = '2b726d3e20e2269bc89af2bbce634f36a6711943690305e52b2820b0889b5e1b' WHERE version = '2026072801_blue_iris_vehicle_frames'; + END IF; +END; +$migration$; +-- 0025_2026072802_blue_iris_vehicle_frame_queue.sql +DO $migration$ +DECLARE recorded_hash TEXT; +BEGIN + SELECT sql_sha256 INTO recorded_hash FROM public.schema_migrations WHERE version = '2026072802_blue_iris_vehicle_frame_queue'; + IF FOUND THEN + IF recorded_hash IS NOT NULL AND recorded_hash <> 'ebb8ab4fba149ccbcf792ee6b68767fc545d58c8eb14bb49e4e44aecac200e3a' THEN + RAISE EXCEPTION 'Applied migration checksum differs: 2026072802_blue_iris_vehicle_frame_queue'; + END IF; + IF recorded_hash IS NULL THEN + UPDATE public.schema_migrations SET sql_sha256 = 'ebb8ab4fba149ccbcf792ee6b68767fc545d58c8eb14bb49e4e44aecac200e3a' WHERE version = '2026072802_blue_iris_vehicle_frame_queue'; + END IF; + ELSE + EXECUTE $source_ebb8ab4fba149ccb$-- migration: 2026072802_blue_iris_vehicle_frame_queue -- New accepted reads are processed automatically while historical work is -- explicitly queued and can be paused independently. A short processing lease -- lets work recover safely after an application restart. @@ -1966,7 +2531,29 @@ VALUES ( 'Automatically process live Blue Iris vehicle frames with durable retries and controlled historical backfill.' ) ON CONFLICT (version) DO NOTHING; +$source_ebb8ab4fba149ccb$; + IF NOT EXISTS (SELECT 1 FROM public.schema_migrations WHERE version = '2026072802_blue_iris_vehicle_frame_queue') THEN + RAISE EXCEPTION 'Migration did not record completion: 2026072802_blue_iris_vehicle_frame_queue'; + END IF; + UPDATE public.schema_migrations SET sql_sha256 = 'ebb8ab4fba149ccbcf792ee6b68767fc545d58c8eb14bb49e4e44aecac200e3a' WHERE version = '2026072802_blue_iris_vehicle_frame_queue'; + END IF; +END; +$migration$; +-- 0026_2026072803_blue_iris_vehicle_frame_quality.sql +DO $migration$ +DECLARE recorded_hash TEXT; +BEGIN + SELECT sql_sha256 INTO recorded_hash FROM public.schema_migrations WHERE version = '2026072803_blue_iris_vehicle_frame_quality'; + IF FOUND THEN + IF recorded_hash IS NOT NULL AND recorded_hash <> 'ccc0fc3d2a30645b5ba851ad13dc3d5ab5e9ac818963fff285e20b4c65434339' THEN + RAISE EXCEPTION 'Applied migration checksum differs: 2026072803_blue_iris_vehicle_frame_quality'; + END IF; + IF recorded_hash IS NULL THEN + UPDATE public.schema_migrations SET sql_sha256 = 'ccc0fc3d2a30645b5ba851ad13dc3d5ab5e9ac818963fff285e20b4c65434339' WHERE version = '2026072803_blue_iris_vehicle_frame_quality'; + END IF; + ELSE + EXECUTE $source_ccc0fc3d2a30645b$-- migration: 2026072803_blue_iris_vehicle_frame_quality -- Explain why the quality-aware multiframe selector chose a particular -- vehicle view without retaining the other sampled images or Blue Iris video. ALTER TABLE IF EXISTS public.plate_reads @@ -1978,7 +2565,29 @@ VALUES ( 'Record bounded quality, tracking, and timing diagnostics for the selected Blue Iris vehicle frame.' ) ON CONFLICT (version) DO NOTHING; +$source_ccc0fc3d2a30645b$; + IF NOT EXISTS (SELECT 1 FROM public.schema_migrations WHERE version = '2026072803_blue_iris_vehicle_frame_quality') THEN + RAISE EXCEPTION 'Migration did not record completion: 2026072803_blue_iris_vehicle_frame_quality'; + END IF; + UPDATE public.schema_migrations SET sql_sha256 = 'ccc0fc3d2a30645b5ba851ad13dc3d5ab5e9ac818963fff285e20b4c65434339' WHERE version = '2026072803_blue_iris_vehicle_frame_quality'; + END IF; +END; +$migration$; +-- 0027_2026072901_storage_monitoring_maintenance.sql +DO $migration$ +DECLARE recorded_hash TEXT; +BEGIN + SELECT sql_sha256 INTO recorded_hash FROM public.schema_migrations WHERE version = '2026072901_storage_monitoring_maintenance'; + IF FOUND THEN + IF recorded_hash IS NOT NULL AND recorded_hash <> '4e283bddfbaaf8c33bc74d9235d8212f746c46c84035d93136889d8b822bdb45' THEN + RAISE EXCEPTION 'Applied migration checksum differs: 2026072901_storage_monitoring_maintenance'; + END IF; + IF recorded_hash IS NULL THEN + UPDATE public.schema_migrations SET sql_sha256 = '4e283bddfbaaf8c33bc74d9235d8212f746c46c84035d93136889d8b822bdb45' WHERE version = '2026072901_storage_monitoring_maintenance'; + END IF; + ELSE + EXECUTE $source_4e283bddfbaaf8c3$-- migration: 2026072901_storage_monitoring_maintenance -- Phase 1 storage monitoring and guarded maintenance. Automatic cleanup is -- deliberately disabled and has no approved categories. The only destructive -- operation represented by this schema is a one-time, confirmed manual run @@ -2220,7 +2829,29 @@ VALUES ( 'Add persisted storage thresholds, runtime liveness, exact category measurements, rate-limited maintenance alerts, and token-guarded manual derived-orphan cleanup.' ) ON CONFLICT (version) DO NOTHING; +$source_4e283bddfbaaf8c3$; + IF NOT EXISTS (SELECT 1 FROM public.schema_migrations WHERE version = '2026072901_storage_monitoring_maintenance') THEN + RAISE EXCEPTION 'Migration did not record completion: 2026072901_storage_monitoring_maintenance'; + END IF; + UPDATE public.schema_migrations SET sql_sha256 = '4e283bddfbaaf8c33bc74d9235d8212f746c46c84035d93136889d8b822bdb45' WHERE version = '2026072901_storage_monitoring_maintenance'; + END IF; +END; +$migration$; +-- 0028_2026072902_automatic_derived_orphan_cleanup.sql +DO $migration$ +DECLARE recorded_hash TEXT; +BEGIN + SELECT sql_sha256 INTO recorded_hash FROM public.schema_migrations WHERE version = '2026072902_automatic_derived_orphan_cleanup'; + IF FOUND THEN + IF recorded_hash IS NOT NULL AND recorded_hash <> 'ce36cd42e16dbba572ac09f2583f88b0241d137da0c41d76eea757475c0795da' THEN + RAISE EXCEPTION 'Applied migration checksum differs: 2026072902_automatic_derived_orphan_cleanup'; + END IF; + IF recorded_hash IS NULL THEN + UPDATE public.schema_migrations SET sql_sha256 = 'ce36cd42e16dbba572ac09f2583f88b0241d137da0c41d76eea757475c0795da' WHERE version = '2026072902_automatic_derived_orphan_cleanup'; + END IF; + ELSE + EXECUTE $source_ce36cd42e16dbba5$-- migration: 2026072902_automatic_derived_orphan_cleanup -- Phase 2A: separately approved, bounded automatic cleanup for derived -- orphans only. Approval history is append-only and defaults to no rows/off. INSERT INTO public.permissions (permission_key, description) @@ -2385,7 +3016,29 @@ VALUES ( 'Add Administrator-only, revisioned and default-off automatic derived-orphan cleanup approval and circuit-breaker state.' ) ON CONFLICT (version) DO NOTHING; +$source_ce36cd42e16dbba5$; + IF NOT EXISTS (SELECT 1 FROM public.schema_migrations WHERE version = '2026072902_automatic_derived_orphan_cleanup') THEN + RAISE EXCEPTION 'Migration did not record completion: 2026072902_automatic_derived_orphan_cleanup'; + END IF; + UPDATE public.schema_migrations SET sql_sha256 = 'ce36cd42e16dbba572ac09f2583f88b0241d137da0c41d76eea757475c0795da' WHERE version = '2026072902_automatic_derived_orphan_cleanup'; + END IF; +END; +$migration$; +-- 0029_2026072903_host_retention_intents.sql +DO $migration$ +DECLARE recorded_hash TEXT; +BEGIN + SELECT sql_sha256 INTO recorded_hash FROM public.schema_migrations WHERE version = '2026072903_host_retention_intents'; + IF FOUND THEN + IF recorded_hash IS NOT NULL AND recorded_hash <> 'ad752ca876d229c01a590b6fbb5ee0ab524afbf8717c786abd18224df904a8bc' THEN + RAISE EXCEPTION 'Applied migration checksum differs: 2026072903_host_retention_intents'; + END IF; + IF recorded_hash IS NULL THEN + UPDATE public.schema_migrations SET sql_sha256 = 'ad752ca876d229c01a590b6fbb5ee0ab524afbf8717c786abd18224df904a8bc' WHERE version = '2026072903_host_retention_intents'; + END IF; + ELSE + EXECUTE $source_ad752ca876d229c0$-- migration: 2026072903_host_retention_intents -- Phase 3 is a fail-closed database control plane. Only the separately installed -- fixed host worker can inspect or remove host artifacts. CREATE TABLE IF NOT EXISTS public.host_maintenance_config ( @@ -2538,7 +3191,29 @@ END $$; INSERT INTO public.schema_migrations(version,description) VALUES ('2026072903_host_retention_intents','Add three-boundary, default-off host-maintenance intent/immutable preview/receipt control plane.') ON CONFLICT(version) DO NOTHING; +$source_ad752ca876d229c0$; + IF NOT EXISTS (SELECT 1 FROM public.schema_migrations WHERE version = '2026072903_host_retention_intents') THEN + RAISE EXCEPTION 'Migration did not record completion: 2026072903_host_retention_intents'; + END IF; + UPDATE public.schema_migrations SET sql_sha256 = 'ad752ca876d229c01a590b6fbb5ee0ab524afbf8717c786abd18224df904a8bc' WHERE version = '2026072903_host_retention_intents'; + END IF; +END; +$migration$; +-- 0030_2026073001_manual_database_backup.sql +DO $migration$ +DECLARE recorded_hash TEXT; +BEGIN + SELECT sql_sha256 INTO recorded_hash FROM public.schema_migrations WHERE version = '2026073001_manual_database_backup'; + IF FOUND THEN + IF recorded_hash IS NOT NULL AND recorded_hash <> '58986107ced11757cd48df501ddadee35b622f1e257cffea9aa5667e294357ca' THEN + RAISE EXCEPTION 'Applied migration checksum differs: 2026073001_manual_database_backup'; + END IF; + IF recorded_hash IS NULL THEN + UPDATE public.schema_migrations SET sql_sha256 = '58986107ced11757cd48df501ddadee35b622f1e257cffea9aa5667e294357ca' WHERE version = '2026073001_manual_database_backup'; + END IF; + ELSE + EXECUTE $source_58986107ced11757$-- migration: 2026073001_manual_database_backup -- Manual database backup is deliberately separate from retention intents: it -- accepts no path, command, schedule, or restore input from the application. ALTER TABLE public.host_maintenance_worker_state @@ -2591,7 +3266,29 @@ CREATE UNIQUE INDEX IF NOT EXISTS idx_host_database_backup_requests_one_active INSERT INTO public.schema_migrations(version,description) VALUES ('2026073001_manual_database_backup','Add a fixed no-input manual database-backup request and status control plane.') ON CONFLICT(version) DO NOTHING; +$source_58986107ced11757$; + IF NOT EXISTS (SELECT 1 FROM public.schema_migrations WHERE version = '2026073001_manual_database_backup') THEN + RAISE EXCEPTION 'Migration did not record completion: 2026073001_manual_database_backup'; + END IF; + UPDATE public.schema_migrations SET sql_sha256 = '58986107ced11757cd48df501ddadee35b622f1e257cffea9aa5667e294357ca' WHERE version = '2026073001_manual_database_backup'; + END IF; +END; +$migration$; +-- 0031_2026080301_configurable_image_retirement_grace.sql +DO $migration$ +DECLARE recorded_hash TEXT; +BEGIN + SELECT sql_sha256 INTO recorded_hash FROM public.schema_migrations WHERE version = '2026080301_configurable_image_retirement_grace'; + IF FOUND THEN + IF recorded_hash IS NOT NULL AND recorded_hash <> '1c22ea13e6c67ca4f84e2755e0b3f18d2e558ac30500307bdb351860e83cadb9' THEN + RAISE EXCEPTION 'Applied migration checksum differs: 2026080301_configurable_image_retirement_grace'; + END IF; + IF recorded_hash IS NULL THEN + UPDATE public.schema_migrations SET sql_sha256 = '1c22ea13e6c67ca4f84e2755e0b3f18d2e558ac30500307bdb351860e83cadb9' WHERE version = '2026080301_configurable_image_retirement_grace'; + END IF; + ELSE + EXECUTE $source_1c22ea13e6c67ca4$-- migration: 2026080301_configurable_image_retirement_grace -- Image retirement remains manual-only, but its post-retirement recovery -- window is administrator-configurable from one to 365 days. Seven days stays -- the default. The fixed worker revalidates the same persisted value. @@ -2604,7 +3301,29 @@ ALTER TABLE public.host_maintenance_config INSERT INTO public.schema_migrations(version,description) VALUES ('2026080301_configurable_image_retirement_grace','Allow an audited 1-365 day manual image-retirement grace while retaining a seven-day default.') ON CONFLICT(version) DO NOTHING; +$source_1c22ea13e6c67ca4$; + IF NOT EXISTS (SELECT 1 FROM public.schema_migrations WHERE version = '2026080301_configurable_image_retirement_grace') THEN + RAISE EXCEPTION 'Migration did not record completion: 2026080301_configurable_image_retirement_grace'; + END IF; + UPDATE public.schema_migrations SET sql_sha256 = '1c22ea13e6c67ca4f84e2755e0b3f18d2e558ac30500307bdb351860e83cadb9' WHERE version = '2026080301_configurable_image_retirement_grace'; + END IF; +END; +$migration$; +-- 0032_2026080501_read_only_backup_catalog.sql +DO $migration$ +DECLARE recorded_hash TEXT; +BEGIN + SELECT sql_sha256 INTO recorded_hash FROM public.schema_migrations WHERE version = '2026080501_read_only_backup_catalog'; + IF FOUND THEN + IF recorded_hash IS NOT NULL AND recorded_hash <> '519d8e0397b693537cdf6747de3de711aabb9244a85a7457db4714af26d783c1' THEN + RAISE EXCEPTION 'Applied migration checksum differs: 2026080501_read_only_backup_catalog'; + END IF; + IF recorded_hash IS NULL THEN + UPDATE public.schema_migrations SET sql_sha256 = '519d8e0397b693537cdf6747de3de711aabb9244a85a7457db4714af26d783c1' WHERE version = '2026080501_read_only_backup_catalog'; + END IF; + ELSE + EXECUTE $source_519d8e0397b69353$-- migration: 2026080501_read_only_backup_catalog -- Persist the worker's validated, path-free rollback-backup inventory as -- append-only read-only evidence. Rollback-backup execution and scheduling are -- explicitly disabled until a later migration adds catalog-bound approval. @@ -2653,7 +3372,29 @@ END $$; INSERT INTO public.schema_migrations(version,description) VALUES ('2026080501_read_only_backup_catalog','Persist immutable worker-validated rollback-backup catalog snapshots while keeping destructive retention disabled.') ON CONFLICT(version) DO NOTHING; +$source_519d8e0397b69353$; + IF NOT EXISTS (SELECT 1 FROM public.schema_migrations WHERE version = '2026080501_read_only_backup_catalog') THEN + RAISE EXCEPTION 'Migration did not record completion: 2026080501_read_only_backup_catalog'; + END IF; + UPDATE public.schema_migrations SET sql_sha256 = '519d8e0397b693537cdf6747de3de711aabb9244a85a7457db4714af26d783c1' WHERE version = '2026080501_read_only_backup_catalog'; + END IF; +END; +$migration$; +-- 0033_2026080702_blue_iris_trigger_direction_shadow.sql +DO $migration$ +DECLARE recorded_hash TEXT; +BEGIN + SELECT sql_sha256 INTO recorded_hash FROM public.schema_migrations WHERE version = '2026080702_blue_iris_trigger_direction_shadow'; + IF FOUND THEN + IF recorded_hash IS NOT NULL AND recorded_hash <> '7412381d223c5bfa717934dedad8a7c362fa01db25ebb91c9995f68a9bee1d4f' THEN + RAISE EXCEPTION 'Applied migration checksum differs: 2026080702_blue_iris_trigger_direction_shadow'; + END IF; + IF recorded_hash IS NULL THEN + UPDATE public.schema_migrations SET sql_sha256 = '7412381d223c5bfa717934dedad8a7c362fa01db25ebb91c9995f68a9bee1d4f' WHERE version = '2026080702_blue_iris_trigger_direction_shadow'; + END IF; + ELSE + EXECUTE $source_7412381d223c5bfa$-- migration: 2026080702_blue_iris_trigger_direction_shadow -- Blue Iris can include the ordered zone crossing which caused an alert in -- its &TYPE macro. Keep this as independent shadow evidence until daylight -- validation proves the camera-specific mapping in both directions. @@ -2720,11 +3461,55 @@ CREATE INDEX IF NOT EXISTS idx_plate_reads_bi_trigger_direction INSERT INTO public.schema_migrations(version,description) VALUES ('2026080702_blue_iris_trigger_direction_shadow','Store and map ordered Blue Iris &TYPE zone crossings as camera-specific shadow direction evidence.') ON CONFLICT(version) DO NOTHING; +$source_7412381d223c5bfa$; + IF NOT EXISTS (SELECT 1 FROM public.schema_migrations WHERE version = '2026080702_blue_iris_trigger_direction_shadow') THEN + RAISE EXCEPTION 'Migration did not record completion: 2026080702_blue_iris_trigger_direction_shadow'; + END IF; + UPDATE public.schema_migrations SET sql_sha256 = '7412381d223c5bfa717934dedad8a7c362fa01db25ebb91c9995f68a9bee1d4f' WHERE version = '2026080702_blue_iris_trigger_direction_shadow'; + END IF; +END; +$migration$; +-- 0034_2026080703_blue_iris_trigger_direction_hardening.sql +DO $migration$ +DECLARE recorded_hash TEXT; +BEGIN + SELECT sql_sha256 INTO recorded_hash FROM public.schema_migrations WHERE version = '2026080703_blue_iris_trigger_direction_hardening'; + IF FOUND THEN + IF recorded_hash IS NOT NULL AND recorded_hash <> '77f34650b80a2cd26a013e9a2eeb8709991a6299b64d4d28850229075bd87dce' THEN + RAISE EXCEPTION 'Applied migration checksum differs: 2026080703_blue_iris_trigger_direction_hardening'; + END IF; + IF recorded_hash IS NULL THEN + UPDATE public.schema_migrations SET sql_sha256 = '77f34650b80a2cd26a013e9a2eeb8709991a6299b64d4d28850229075bd87dce' WHERE version = '2026080703_blue_iris_trigger_direction_hardening'; + END IF; + ELSE + EXECUTE $source_77f34650b80a2cd2$-- migration: 2026080703_blue_iris_trigger_direction_hardening INSERT INTO public.schema_migrations(version,description) VALUES ('2026080703_blue_iris_trigger_direction_hardening','Separate Blue Iris mapping revisions from ReID, enforce exact reverse crossings, and scope diagnostics by camera.') ON CONFLICT(version) DO NOTHING; +$source_77f34650b80a2cd2$; + IF NOT EXISTS (SELECT 1 FROM public.schema_migrations WHERE version = '2026080703_blue_iris_trigger_direction_hardening') THEN + RAISE EXCEPTION 'Migration did not record completion: 2026080703_blue_iris_trigger_direction_hardening'; + END IF; + UPDATE public.schema_migrations SET sql_sha256 = '77f34650b80a2cd26a013e9a2eeb8709991a6299b64d4d28850229075bd87dce' WHERE version = '2026080703_blue_iris_trigger_direction_hardening'; + END IF; +END; +$migration$; +-- 0035_2026080801_daytime_overview_vehicle_views.sql +DO $migration$ +DECLARE recorded_hash TEXT; +BEGIN + SELECT sql_sha256 INTO recorded_hash FROM public.schema_migrations WHERE version = '2026080801_daytime_overview_vehicle_views'; + IF FOUND THEN + IF recorded_hash IS NOT NULL AND recorded_hash <> 'fade00cc2cf4d3a15d2002a944ede78ca6c1e0392d0645f813a33557dc07753a' THEN + RAISE EXCEPTION 'Applied migration checksum differs: 2026080801_daytime_overview_vehicle_views'; + END IF; + IF recorded_hash IS NULL THEN + UPDATE public.schema_migrations SET sql_sha256 = 'fade00cc2cf4d3a15d2002a944ede78ca6c1e0392d0645f813a33557dc07753a' WHERE version = '2026080801_daytime_overview_vehicle_views'; + END IF; + ELSE + EXECUTE $source_fade00cc2cf4d3a1$-- migration: 2026080801_daytime_overview_vehicle_views -- Phase 2 overview Vehicle Views. Motion alerts from an overview-capable Blue -- Iris camera are screened and sampled independently from plate ingestion. -- Direction-aware, camera-pair timing profiles associate one retained daytime @@ -2861,7 +3646,29 @@ CREATE INDEX IF NOT EXISTS idx_plate_reads_overview_waiting INSERT INTO public.schema_migrations(version,description) VALUES ('2026080801_daytime_overview_vehicle_views','Ingest daytime Blue Iris overview candidates and conservatively associate primary or driveway-fallback Vehicle Views by camera timing and direction.') ON CONFLICT(version) DO NOTHING; +$source_fade00cc2cf4d3a1$; + IF NOT EXISTS (SELECT 1 FROM public.schema_migrations WHERE version = '2026080801_daytime_overview_vehicle_views') THEN + RAISE EXCEPTION 'Migration did not record completion: 2026080801_daytime_overview_vehicle_views'; + END IF; + UPDATE public.schema_migrations SET sql_sha256 = 'fade00cc2cf4d3a15d2002a944ede78ca6c1e0392d0645f813a33557dc07753a' WHERE version = '2026080801_daytime_overview_vehicle_views'; + END IF; +END; +$migration$; +-- 0036_2026080901_overview_primary_claim_safety.sql +DO $migration$ +DECLARE recorded_hash TEXT; +BEGIN + SELECT sql_sha256 INTO recorded_hash FROM public.schema_migrations WHERE version = '2026080901_overview_primary_claim_safety'; + IF FOUND THEN + IF recorded_hash IS NOT NULL AND recorded_hash <> 'bdaf47e8614d92edc64a1cffa1e387d0eab7b57f7d2762231358898354a64bb9' THEN + RAISE EXCEPTION 'Applied migration checksum differs: 2026080901_overview_primary_claim_safety'; + END IF; + IF recorded_hash IS NULL THEN + UPDATE public.schema_migrations SET sql_sha256 = 'bdaf47e8614d92edc64a1cffa1e387d0eab7b57f7d2762231358898354a64bb9' WHERE version = '2026080901_overview_primary_claim_safety'; + END IF; + ELSE + EXECUTE $source_bdaf47e8614d92ed$-- migration: 2026080901_overview_primary_claim_safety -- Primary Street Overview retrieval is read-owned and may outlive the worker -- process which originally claimed it. A per-claim token prevents a reclaimed -- worker from overwriting a newer result, while next_attempt_at supports one @@ -2893,7 +3700,29 @@ CREATE INDEX IF NOT EXISTS idx_plate_reads_overview_retry_ready INSERT INTO public.schema_migrations(version,description) VALUES ('2026080901_overview_primary_claim_safety','Add token-guarded profile-aware Street Overview claims, bounded recording-finalization retries, and a primary timing tolerance compatible with the six-second sample window.') ON CONFLICT(version) DO NOTHING; +$source_bdaf47e8614d92ed$; + IF NOT EXISTS (SELECT 1 FROM public.schema_migrations WHERE version = '2026080901_overview_primary_claim_safety') THEN + RAISE EXCEPTION 'Migration did not record completion: 2026080901_overview_primary_claim_safety'; + END IF; + UPDATE public.schema_migrations SET sql_sha256 = 'bdaf47e8614d92edc64a1cffa1e387d0eab7b57f7d2762231358898354a64bb9' WHERE version = '2026080901_overview_primary_claim_safety'; + END IF; +END; +$migration$; +-- 0037_2026080902_blue_iris_timeline_exports.sql +DO $migration$ +DECLARE recorded_hash TEXT; +BEGIN + SELECT sql_sha256 INTO recorded_hash FROM public.schema_migrations WHERE version = '2026080902_blue_iris_timeline_exports'; + IF FOUND THEN + IF recorded_hash IS NOT NULL AND recorded_hash <> '1614237019b86ec9cbf1b016501eb9045cd24262add9e7b2d5cf8adc57b4fbae' THEN + RAISE EXCEPTION 'Applied migration checksum differs: 2026080902_blue_iris_timeline_exports'; + END IF; + IF recorded_hash IS NULL THEN + UPDATE public.schema_migrations SET sql_sha256 = '1614237019b86ec9cbf1b016501eb9045cd24262add9e7b2d5cf8adc57b4fbae' WHERE version = '2026080902_blue_iris_timeline_exports'; + END IF; + ELSE + EXECUTE $source_1614237019b86ec9$-- migration: 2026080902_blue_iris_timeline_exports -- Timeline exports replace sixty-one sequential Blue Iris /time requests for -- primary Street Overview work. The export ledger owns only API-generated -- temporary files. Blue Iris owns Clipboard retention; ALPR never requests @@ -2953,7 +3782,29 @@ CREATE INDEX IF NOT EXISTS idx_plate_reads_overview_claim_heartbeat INSERT INTO public.schema_migrations(version,description) VALUES ('2026080902_blue_iris_timeline_exports','Track read-owned temporary Blue Iris timeline exports and bound overview processing with heartbeats and deadlines.') ON CONFLICT(version) DO NOTHING; +$source_1614237019b86ec9$; + IF NOT EXISTS (SELECT 1 FROM public.schema_migrations WHERE version = '2026080902_blue_iris_timeline_exports') THEN + RAISE EXCEPTION 'Migration did not record completion: 2026080902_blue_iris_timeline_exports'; + END IF; + UPDATE public.schema_migrations SET sql_sha256 = '1614237019b86ec9cbf1b016501eb9045cd24262add9e7b2d5cf8adc57b4fbae' WHERE version = '2026080902_blue_iris_timeline_exports'; + END IF; +END; +$migration$; +-- 0038_2026080903_blue_iris_clipboard_retention.sql +DO $migration$ +DECLARE recorded_hash TEXT; +BEGIN + SELECT sql_sha256 INTO recorded_hash FROM public.schema_migrations WHERE version = '2026080903_blue_iris_clipboard_retention'; + IF FOUND THEN + IF recorded_hash IS NOT NULL AND recorded_hash <> '264266c57ba807c2922ed6f35423ffe1e4476967f40c829ef3bca3ef56dd908c' THEN + RAISE EXCEPTION 'Applied migration checksum differs: 2026080903_blue_iris_clipboard_retention'; + END IF; + IF recorded_hash IS NULL THEN + UPDATE public.schema_migrations SET sql_sha256 = '264266c57ba807c2922ed6f35423ffe1e4476967f40c829ef3bca3ef56dd908c' WHERE version = '2026080903_blue_iris_clipboard_retention'; + END IF; + ELSE + EXECUTE $source_264266c57ba807c2$-- migration: 2026080903_blue_iris_clipboard_retention -- Retire the earlier remote-delete workflow without removing its additive -- compatibility columns. Completed downloads become terminal ledger entries; -- Blue Iris removes Clipboard exports according to its configured retention. @@ -2974,7 +3825,29 @@ WHERE status IN ('delete_pending', 'deleting'); INSERT INTO public.schema_migrations(version,description) VALUES ('2026080903_blue_iris_clipboard_retention','Stop ALPR remote-delete attempts, leave Blue Iris Clipboard retention authoritative, and normalize earlier delete-pending export rows.') ON CONFLICT(version) DO NOTHING; +$source_264266c57ba807c2$; + IF NOT EXISTS (SELECT 1 FROM public.schema_migrations WHERE version = '2026080903_blue_iris_clipboard_retention') THEN + RAISE EXCEPTION 'Migration did not record completion: 2026080903_blue_iris_clipboard_retention'; + END IF; + UPDATE public.schema_migrations SET sql_sha256 = '264266c57ba807c2922ed6f35423ffe1e4476967f40c829ef3bca3ef56dd908c' WHERE version = '2026080903_blue_iris_clipboard_retention'; + END IF; +END; +$migration$; +-- 0039_2026081001_overview_export_idempotency.sql +DO $migration$ +DECLARE recorded_hash TEXT; +BEGIN + SELECT sql_sha256 INTO recorded_hash FROM public.schema_migrations WHERE version = '2026081001_overview_export_idempotency'; + IF FOUND THEN + IF recorded_hash IS NOT NULL AND recorded_hash <> '0fead20c50460ac8f9ccf0be77ff9bc1075affa5bf1d2c866adc392ffabf88c1' THEN + RAISE EXCEPTION 'Applied migration checksum differs: 2026081001_overview_export_idempotency'; + END IF; + IF recorded_hash IS NULL THEN + UPDATE public.schema_migrations SET sql_sha256 = '0fead20c50460ac8f9ccf0be77ff9bc1075affa5bf1d2c866adc392ffabf88c1' WHERE version = '2026081001_overview_export_idempotency'; + END IF; + ELSE + EXECUTE $source_0fead20c50460ac8$-- migration: 2026081001_overview_export_idempotency -- Overview processing must not use PostgreSQL timestamps as compare-and-swap -- revisions because the JavaScript Date representation loses microseconds. -- Timeline exports also need a stable identity which survives claim renewal, @@ -3048,7 +3921,29 @@ ALTER TABLE public.plate_reads INSERT INTO public.schema_migrations(version,description) VALUES ('2026081001_overview_export_idempotency','Use integer overview-profile revisions and stable at-most-once Blue Iris timeline-export identities across retries and worker restarts.') ON CONFLICT(version) DO NOTHING; +$source_0fead20c50460ac8$; + IF NOT EXISTS (SELECT 1 FROM public.schema_migrations WHERE version = '2026081001_overview_export_idempotency') THEN + RAISE EXCEPTION 'Migration did not record completion: 2026081001_overview_export_idempotency'; + END IF; + UPDATE public.schema_migrations SET sql_sha256 = '0fead20c50460ac8f9ccf0be77ff9bc1075affa5bf1d2c866adc392ffabf88c1' WHERE version = '2026081001_overview_export_idempotency'; + END IF; +END; +$migration$; +-- 0040_2026081002_street_overview_pair_sharing.sql +DO $migration$ +DECLARE recorded_hash TEXT; +BEGIN + SELECT sql_sha256 INTO recorded_hash FROM public.schema_migrations WHERE version = '2026081002_street_overview_pair_sharing'; + IF FOUND THEN + IF recorded_hash IS NOT NULL AND recorded_hash <> '2c44b766f72f80ee1a51bd4bae8842ebad5da11b189f9e551eddb6fdf13eab02' THEN + RAISE EXCEPTION 'Applied migration checksum differs: 2026081002_street_overview_pair_sharing'; + END IF; + IF recorded_hash IS NULL THEN + UPDATE public.schema_migrations SET sql_sha256 = '2c44b766f72f80ee1a51bd4bae8842ebad5da11b189f9e551eddb6fdf13eab02' WHERE version = '2026081002_street_overview_pair_sharing'; + END IF; + ELSE + EXECUTE $source_2c44b766f72f80ee$-- migration: 2026081002_street_overview_pair_sharing -- A validated primary Street Overview frame may be copied to one uniquely -- matching companion Street LPR read. New installations begin in shadow mode: -- matches are recorded for review, but no read or file is changed until an @@ -3143,7 +4038,29 @@ CREATE UNIQUE INDEX IF NOT EXISTS idx_vehicle_overview_read_shares_unique_live_s INSERT INTO public.schema_migrations(version,description) VALUES ('2026081002_street_overview_pair_sharing','Add shadow-first, one-to-one Street LPR companion sharing with independent image files and durable provenance.') ON CONFLICT(version) DO NOTHING; +$source_2c44b766f72f80ee$; + IF NOT EXISTS (SELECT 1 FROM public.schema_migrations WHERE version = '2026081002_street_overview_pair_sharing') THEN + RAISE EXCEPTION 'Migration did not record completion: 2026081002_street_overview_pair_sharing'; + END IF; + UPDATE public.schema_migrations SET sql_sha256 = '2c44b766f72f80ee1a51bd4bae8842ebad5da11b189f9e551eddb6fdf13eab02' WHERE version = '2026081002_street_overview_pair_sharing'; + END IF; +END; +$migration$; +-- 0041_2026081003_live_feed_page_first.sql +DO $migration$ +DECLARE recorded_hash TEXT; +BEGIN + SELECT sql_sha256 INTO recorded_hash FROM public.schema_migrations WHERE version = '2026081003_live_feed_page_first'; + IF FOUND THEN + IF recorded_hash IS NOT NULL AND recorded_hash <> 'f6d4442d50e4e667332a5ca9ec5c1925a6861ea223de81c97d3c2ac706a66f60' THEN + RAISE EXCEPTION 'Applied migration checksum differs: 2026081003_live_feed_page_first'; + END IF; + IF recorded_hash IS NULL THEN + UPDATE public.schema_migrations SET sql_sha256 = 'f6d4442d50e4e667332a5ca9ec5c1925a6861ea223de81c97d3c2ac706a66f60' WHERE version = '2026081003_live_feed_page_first'; + END IF; + ELSE + EXECUTE $source_f6d4442d50e4e667$-- migration: 2026081003_live_feed_page_first -- Live Feed must page the inexpensive read identity set before joining tags, -- vehicle evidence, and other presentation data. This composite order index -- supports its default newest-first page without sorting the full read history. @@ -3153,7 +4070,29 @@ CREATE INDEX IF NOT EXISTS idx_plate_reads_live_feed_timestamp INSERT INTO public.schema_migrations(version,description) VALUES ('2026081003_live_feed_page_first','Page Live Feed read identities before presentation joins and support newest-first lookup with a composite index.') ON CONFLICT(version) DO NOTHING; +$source_f6d4442d50e4e667$; + IF NOT EXISTS (SELECT 1 FROM public.schema_migrations WHERE version = '2026081003_live_feed_page_first') THEN + RAISE EXCEPTION 'Migration did not record completion: 2026081003_live_feed_page_first'; + END IF; + UPDATE public.schema_migrations SET sql_sha256 = 'f6d4442d50e4e667332a5ca9ec5c1925a6861ea223de81c97d3c2ac706a66f60' WHERE version = '2026081003_live_feed_page_first'; + END IF; +END; +$migration$; +-- 0042_2026081004_entry_lpr_route_fallback.sql +DO $migration$ +DECLARE recorded_hash TEXT; +BEGIN + SELECT sql_sha256 INTO recorded_hash FROM public.schema_migrations WHERE version = '2026081004_entry_lpr_route_fallback'; + IF FOUND THEN + IF recorded_hash IS NOT NULL AND recorded_hash <> '80ac5ef682132e1ed3b9efe3f55c40f88ee4469401ea3636bf48e9ed69a7d10f' THEN + RAISE EXCEPTION 'Applied migration checksum differs: 2026081004_entry_lpr_route_fallback'; + END IF; + IF recorded_hash IS NULL THEN + UPDATE public.schema_migrations SET sql_sha256 = '80ac5ef682132e1ed3b9efe3f55c40f88ee4469401ea3636bf48e9ed69a7d10f' WHERE version = '2026081004_entry_lpr_route_fallback'; + END IF; + ELSE + EXECUTE $source_80ac5ef682132e1e$-- migration: 2026081004_entry_lpr_route_fallback -- Gate LPR fallback is a read-to-read association. It never consumes a Blue -- Iris alert and never creates a synthetic Street read. New installations start -- in shadow mode so route matches can be reviewed before copied images are @@ -3275,7 +4214,29 @@ CREATE UNIQUE INDEX IF NOT EXISTS idx_vehicle_entry_fallback_unique_live_event INSERT INTO public.schema_migrations(version,description) VALUES ('2026081004_entry_lpr_route_fallback','Add shadow-first, dual-camera Gate LPR read-to-read fallback for explicitly configured driveway routes.') ON CONFLICT(version) DO NOTHING; +$source_80ac5ef682132e1e$; + IF NOT EXISTS (SELECT 1 FROM public.schema_migrations WHERE version = '2026081004_entry_lpr_route_fallback') THEN + RAISE EXCEPTION 'Migration did not record completion: 2026081004_entry_lpr_route_fallback'; + END IF; + UPDATE public.schema_migrations SET sql_sha256 = '80ac5ef682132e1ed3b9efe3f55c40f88ee4469401ea3636bf48e9ed69a7d10f' WHERE version = '2026081004_entry_lpr_route_fallback'; + END IF; +END; +$migration$; +-- 0043_2026081005_entry_overview_primary.sql +DO $migration$ +DECLARE recorded_hash TEXT; +BEGIN + SELECT sql_sha256 INTO recorded_hash FROM public.schema_migrations WHERE version = '2026081005_entry_overview_primary'; + IF FOUND THEN + IF recorded_hash IS NOT NULL AND recorded_hash <> '81076d72147a5e9cfa0d3c3a97deb15e451ad1ec515c9adc6c08a2b8da4d7d81' THEN + RAISE EXCEPTION 'Applied migration checksum differs: 2026081005_entry_overview_primary'; + END IF; + IF recorded_hash IS NULL THEN + UPDATE public.schema_migrations SET sql_sha256 = '81076d72147a5e9cfa0d3c3a97deb15e451ad1ec515c9adc6c08a2b8da4d7d81' WHERE version = '2026081005_entry_overview_primary'; + END IF; + ELSE + EXECUTE $source_81076d72147a5e9c$-- migration: 2026081005_entry_overview_primary -- Gate LPR reads may use the same read-owned continuous-recording pipeline as -- Street LPR reads while retaining distinct provenance. Existing primary -- profiles remain Street profiles until an administrator explicitly changes @@ -3346,7 +4307,29 @@ ALTER TABLE public.plate_reads INSERT INTO public.schema_migrations(version,description) VALUES ('2026081005_entry_overview_primary','Add explicit Gate Overview primary profile context and distinct read-owned Vehicle View provenance.') ON CONFLICT(version) DO NOTHING; +$source_81076d72147a5e9c$; + IF NOT EXISTS (SELECT 1 FROM public.schema_migrations WHERE version = '2026081005_entry_overview_primary') THEN + RAISE EXCEPTION 'Migration did not record completion: 2026081005_entry_overview_primary'; + END IF; + UPDATE public.schema_migrations SET sql_sha256 = '81076d72147a5e9cfa0d3c3a97deb15e451ad1ec515c9adc6c08a2b8da4d7d81' WHERE version = '2026081005_entry_overview_primary'; + END IF; +END; +$migration$; +-- 0044_2026081006_entry_overview_history_backfill.sql +DO $migration$ +DECLARE recorded_hash TEXT; +BEGIN + SELECT sql_sha256 INTO recorded_hash FROM public.schema_migrations WHERE version = '2026081006_entry_overview_history_backfill'; + IF FOUND THEN + IF recorded_hash IS NOT NULL AND recorded_hash <> 'bbf8dab03f1dc42a5d3cd51ee368d85b39b3d52f145c131c1325b33bd19e0ced' THEN + RAISE EXCEPTION 'Applied migration checksum differs: 2026081006_entry_overview_history_backfill'; + END IF; + IF recorded_hash IS NULL THEN + UPDATE public.schema_migrations SET sql_sha256 = 'bbf8dab03f1dc42a5d3cd51ee368d85b39b3d52f145c131c1325b33bd19e0ced' WHERE version = '2026081006_entry_overview_history_backfill'; + END IF; + ELSE + EXECUTE $source_bbf8dab03f1dc42a$-- migration: 2026081006_entry_overview_history_backfill -- Direction-independent Gate Overview history is deliberately separate from -- the live direction profiles. A run first materializes an immutable preview; -- confirmation may queue only that exact preview. No migration statement @@ -3626,7 +4609,29 @@ ALTER TABLE public.plate_reads INSERT INTO public.schema_migrations(version,description) VALUES ('2026081006_entry_overview_history_backfill','Add immutable direction-independent Gate Overview history profiles and preview-first, bounded, resumable backfill runs without auto-queueing reads.') ON CONFLICT(version) DO NOTHING; +$source_bbf8dab03f1dc42a$; + IF NOT EXISTS (SELECT 1 FROM public.schema_migrations WHERE version = '2026081006_entry_overview_history_backfill') THEN + RAISE EXCEPTION 'Migration did not record completion: 2026081006_entry_overview_history_backfill'; + END IF; + UPDATE public.schema_migrations SET sql_sha256 = 'bbf8dab03f1dc42a5d3cd51ee368d85b39b3d52f145c131c1325b33bd19e0ced' WHERE version = '2026081006_entry_overview_history_backfill'; + END IF; +END; +$migration$; +-- 0045_2026081201_entry_overview_route_payload.sql +DO $migration$ +DECLARE recorded_hash TEXT; +BEGIN + SELECT sql_sha256 INTO recorded_hash FROM public.schema_migrations WHERE version = '2026081201_entry_overview_route_payload'; + IF FOUND THEN + IF recorded_hash IS NOT NULL AND recorded_hash <> 'bd535f26693e67e5b138420d7b5ef63d93b22483dc5c3bda6b4e8cda94aff1cc' THEN + RAISE EXCEPTION 'Applied migration checksum differs: 2026081201_entry_overview_route_payload'; + END IF; + IF recorded_hash IS NULL THEN + UPDATE public.schema_migrations SET sql_sha256 = 'bd535f26693e67e5b138420d7b5ef63d93b22483dc5c3bda6b4e8cda94aff1cc' WHERE version = '2026081201_entry_overview_route_payload'; + END IF; + ELSE + EXECUTE $source_bd535f26693e67e5$-- migration: 2026081201_entry_overview_route_payload -- Route fallback v2 keeps the two Gate LPR reads as immutable identity and -- timing evidence, but may copy only an already validated Gate Overview -- (Overview01) result owned by one of those exact reads. The independent payload @@ -3720,11 +4725,55 @@ ALTER TABLE public.plate_reads INSERT INTO public.schema_migrations(version,description) VALUES ('2026081201_entry_overview_route_payload','Add shadow-gated Overview01 payload snapshots to dual-Entry-read route fallback without enabling or queuing work.') ON CONFLICT(version) DO NOTHING; +$source_bd535f26693e67e5$; + IF NOT EXISTS (SELECT 1 FROM public.schema_migrations WHERE version = '2026081201_entry_overview_route_payload') THEN + RAISE EXCEPTION 'Migration did not record completion: 2026081201_entry_overview_route_payload'; + END IF; + UPDATE public.schema_migrations SET sql_sha256 = 'bd535f26693e67e5b138420d7b5ef63d93b22483dc5c3bda6b4e8cda94aff1cc' WHERE version = '2026081201_entry_overview_route_payload'; + END IF; +END; +$migration$; +-- 0046_2026081101_entry_overview_history_retry.sql +DO $migration$ +DECLARE recorded_hash TEXT; +BEGIN + SELECT sql_sha256 INTO recorded_hash FROM public.schema_migrations WHERE version = '2026081101_entry_overview_history_retry'; + IF FOUND THEN + IF recorded_hash IS NOT NULL AND recorded_hash <> 'f8082fcb0bc3740f0d039cb2fb439c5de73cb0fb9b951d7c0dbefde6216f5372' THEN + RAISE EXCEPTION 'Applied migration checksum differs: 2026081101_entry_overview_history_retry'; + END IF; + IF recorded_hash IS NULL THEN + UPDATE public.schema_migrations SET sql_sha256 = 'f8082fcb0bc3740f0d039cb2fb439c5de73cb0fb9b951d7c0dbefde6216f5372' WHERE version = '2026081101_entry_overview_history_retry'; + END IF; + ELSE + EXECUTE $source_f8082fcb0bc3740f$-- migration: 2026081101_entry_overview_history_retry INSERT INTO public.schema_migrations(version,description) VALUES ('2026081101_entry_overview_history_retry','Allow one audited, operator-authorized retry cycle for terminal transient Gate Overview history import failures while preserving existing views and export identity.') ON CONFLICT(version) DO NOTHING; +$source_f8082fcb0bc3740f$; + IF NOT EXISTS (SELECT 1 FROM public.schema_migrations WHERE version = '2026081101_entry_overview_history_retry') THEN + RAISE EXCEPTION 'Migration did not record completion: 2026081101_entry_overview_history_retry'; + END IF; + UPDATE public.schema_migrations SET sql_sha256 = 'f8082fcb0bc3740f0d039cb2fb439c5de73cb0fb9b951d7c0dbefde6216f5372' WHERE version = '2026081101_entry_overview_history_retry'; + END IF; +END; +$migration$; +-- 0047_2026081301_integration_ingress_receipts.sql +DO $migration$ +DECLARE recorded_hash TEXT; +BEGIN + SELECT sql_sha256 INTO recorded_hash FROM public.schema_migrations WHERE version = '2026081301_integration_ingress_receipts'; + IF FOUND THEN + IF recorded_hash IS NOT NULL AND recorded_hash <> '9d5e750a272bfa13e7b642fa053469fbad65790e61d0fc9ee8a8ab316596816e' THEN + RAISE EXCEPTION 'Applied migration checksum differs: 2026081301_integration_ingress_receipts'; + END IF; + IF recorded_hash IS NULL THEN + UPDATE public.schema_migrations SET sql_sha256 = '9d5e750a272bfa13e7b642fa053469fbad65790e61d0fc9ee8a8ab316596816e' WHERE version = '2026081301_integration_ingress_receipts'; + END IF; + ELSE + EXECUTE $source_9d5e750a272bfa13$-- migration: 2026081301_integration_ingress_receipts -- Bounded operational receipts for authenticated integration requests. These -- rows intentionally contain request shape and outcome metadata only. Plate -- values, request bodies, images, AI dumps, and Blue Iris paths are excluded. @@ -3787,7 +4836,29 @@ CREATE INDEX IF NOT EXISTS idx_integration_ingress_receipts_trigger_diagnostics INSERT INTO public.schema_migrations(version,description) VALUES ('2026081301_integration_ingress_receipts','Add bounded metadata-only integration request receipts and trigger-type diagnostics.') ON CONFLICT(version) DO NOTHING; +$source_9d5e750a272bfa13$; + IF NOT EXISTS (SELECT 1 FROM public.schema_migrations WHERE version = '2026081301_integration_ingress_receipts') THEN + RAISE EXCEPTION 'Migration did not record completion: 2026081301_integration_ingress_receipts'; + END IF; + UPDATE public.schema_migrations SET sql_sha256 = '9d5e750a272bfa13e7b642fa053469fbad65790e61d0fc9ee8a8ab316596816e' WHERE version = '2026081301_integration_ingress_receipts'; + END IF; +END; +$migration$; +-- 0048_2026081302_ingress_receipt_diagnostics_v2.sql +DO $migration$ +DECLARE recorded_hash TEXT; +BEGIN + SELECT sql_sha256 INTO recorded_hash FROM public.schema_migrations WHERE version = '2026081302_ingress_receipt_diagnostics_v2'; + IF FOUND THEN + IF recorded_hash IS NOT NULL AND recorded_hash <> '11fe1596a173e44d2f7377eb43d0f20dc2821b18d89616d135f744967c324803' THEN + RAISE EXCEPTION 'Applied migration checksum differs: 2026081302_ingress_receipt_diagnostics_v2'; + END IF; + IF recorded_hash IS NULL THEN + UPDATE public.schema_migrations SET sql_sha256 = '11fe1596a173e44d2f7377eb43d0f20dc2821b18d89616d135f744967c324803' WHERE version = '2026081302_ingress_receipt_diagnostics_v2'; + END IF; + ELSE + EXECUTE $source_11fe1596a173e44d$-- migration: 2026081302_ingress_receipt_diagnostics_v2 -- Versioned, metadata-only evidence for ambiguous trigger aliases and the -- existing reads targeted by duplicate submissions. Existing receipts remain -- schema v1; new application writes explicitly identify schema v2. @@ -3805,7 +4876,29 @@ ALTER TABLE public.integration_ingress_receipts INSERT INTO public.schema_migrations(version,description) VALUES ('2026081302_ingress_receipt_diagnostics_v2','Version ingress receipts and add sanitized trigger-alias conflict plus duplicate-target read evidence.') ON CONFLICT(version) DO NOTHING; +$source_11fe1596a173e44d$; + IF NOT EXISTS (SELECT 1 FROM public.schema_migrations WHERE version = '2026081302_ingress_receipt_diagnostics_v2') THEN + RAISE EXCEPTION 'Migration did not record completion: 2026081302_ingress_receipt_diagnostics_v2'; + END IF; + UPDATE public.schema_migrations SET sql_sha256 = '11fe1596a173e44d2f7377eb43d0f20dc2821b18d89616d135f744967c324803' WHERE version = '2026081302_ingress_receipt_diagnostics_v2'; + END IF; +END; +$migration$; +-- 0049_2026081303_read_pipeline_timeline.sql +DO $migration$ +DECLARE recorded_hash TEXT; +BEGIN + SELECT sql_sha256 INTO recorded_hash FROM public.schema_migrations WHERE version = '2026081303_read_pipeline_timeline'; + IF FOUND THEN + IF recorded_hash IS NOT NULL AND recorded_hash <> 'aedf80b0819e8c2f9434ddf3b5368de66858c30532aa1d786de234641cf6893a' THEN + RAISE EXCEPTION 'Applied migration checksum differs: 2026081303_read_pipeline_timeline'; + END IF; + IF recorded_hash IS NULL THEN + UPDATE public.schema_migrations SET sql_sha256 = 'aedf80b0819e8c2f9434ddf3b5368de66858c30532aa1d786de234641cf6893a' WHERE version = '2026081303_read_pipeline_timeline'; + END IF; + ELSE + EXECUTE $source_aedf80b0819e8c2f$-- migration: 2026081303_read_pipeline_timeline -- Sanitized, append-only application evidence for each accepted read. Events -- follow the parent read lifecycle so existing read cleanup remains unchanged. -- The details object is deliberately small and never stores plate text, @@ -3841,7 +4934,29 @@ CREATE INDEX IF NOT EXISTS idx_plate_read_pipeline_events_request INSERT INTO public.schema_migrations(version,description) VALUES ('2026081303_read_pipeline_timeline','Add sanitized append-only per-read pipeline events without changing read cleanup behavior.') ON CONFLICT(version) DO NOTHING; +$source_aedf80b0819e8c2f$; + IF NOT EXISTS (SELECT 1 FROM public.schema_migrations WHERE version = '2026081303_read_pipeline_timeline') THEN + RAISE EXCEPTION 'Migration did not record completion: 2026081303_read_pipeline_timeline'; + END IF; + UPDATE public.schema_migrations SET sql_sha256 = 'aedf80b0819e8c2f9434ddf3b5368de66858c30532aa1d786de234641cf6893a' WHERE version = '2026081303_read_pipeline_timeline'; + END IF; +END; +$migration$; +-- 0050_2026081401_logging_retention_incidents.sql +DO $migration$ +DECLARE recorded_hash TEXT; +BEGIN + SELECT sql_sha256 INTO recorded_hash FROM public.schema_migrations WHERE version = '2026081401_logging_retention_incidents'; + IF FOUND THEN + IF recorded_hash IS NOT NULL AND recorded_hash <> 'af48e063910627a1f013945ee830ccd2eff068ee2006ea041fd8b04f486c6501' THEN + RAISE EXCEPTION 'Applied migration checksum differs: 2026081401_logging_retention_incidents'; + END IF; + IF recorded_hash IS NULL THEN + UPDATE public.schema_migrations SET sql_sha256 = 'af48e063910627a1f013945ee830ccd2eff068ee2006ea041fd8b04f486c6501' WHERE version = '2026081401_logging_retention_incidents'; + END IF; + ELSE + EXECUTE $source_af48e063910627a1$-- migration: 2026081401_logging_retention_incidents -- Immutable incident snapshots preserve a bounded, sanitized evidence package -- before retention can remove source rows. A scope protects matching live rows -- until protected_until, while the snapshot and its digest remain append-only. @@ -4007,7 +5122,29 @@ CREATE INDEX IF NOT EXISTS idx_logging_retention_previews_actor INSERT INTO public.schema_migrations(version,description) VALUES ('2026081401_logging_retention_incidents','Add immutable incident snapshots, preview-bound log retention, and a partitioned append-only audit archive.') ON CONFLICT(version) DO NOTHING; +$source_af48e063910627a1$; + IF NOT EXISTS (SELECT 1 FROM public.schema_migrations WHERE version = '2026081401_logging_retention_incidents') THEN + RAISE EXCEPTION 'Migration did not record completion: 2026081401_logging_retention_incidents'; + END IF; + UPDATE public.schema_migrations SET sql_sha256 = 'af48e063910627a1f013945ee830ccd2eff068ee2006ea041fd8b04f486c6501' WHERE version = '2026081401_logging_retention_incidents'; + END IF; +END; +$migration$; +-- 0051_2026081402_vehicle_image_asset_foundation.sql +DO $migration$ +DECLARE recorded_hash TEXT; +BEGIN + SELECT sql_sha256 INTO recorded_hash FROM public.schema_migrations WHERE version = '2026081402_vehicle_image_asset_foundation'; + IF FOUND THEN + IF recorded_hash IS NOT NULL AND recorded_hash <> '429a92b8cbf3130ba5c8682af784d0133765c86e73e62b5a4a48b73a37b8c0b7' THEN + RAISE EXCEPTION 'Applied migration checksum differs: 2026081402_vehicle_image_asset_foundation'; + END IF; + IF recorded_hash IS NULL THEN + UPDATE public.schema_migrations SET sql_sha256 = '429a92b8cbf3130ba5c8682af784d0133765c86e73e62b5a4a48b73a37b8c0b7' WHERE version = '2026081402_vehicle_image_asset_foundation'; + END IF; + ELSE + EXECUTE $source_429a92b8cbf3130b$-- migration: 2026081402_vehicle_image_asset_foundation -- Canonical, byte-addressed ownership for ready Overview JPEGs. This is an -- inert catalog foundation: it does not queue work, backfill reads, alter the -- current read-owned Vehicle View, or change the existing ReID pipeline. @@ -4162,7 +5299,29 @@ ALTER TABLE public.storage_reconciliation_runs INSERT INTO public.schema_migrations(version,description) VALUES ('2026081402_vehicle_image_asset_foundation','Add an inert canonical Overview JPEG catalog, read provenance links, and storage-reference protection.') ON CONFLICT(version) DO NOTHING; +$source_429a92b8cbf3130b$; + IF NOT EXISTS (SELECT 1 FROM public.schema_migrations WHERE version = '2026081402_vehicle_image_asset_foundation') THEN + RAISE EXCEPTION 'Migration did not record completion: 2026081402_vehicle_image_asset_foundation'; + END IF; + UPDATE public.schema_migrations SET sql_sha256 = '429a92b8cbf3130ba5c8682af784d0133765c86e73e62b5a4a48b73a37b8c0b7' WHERE version = '2026081402_vehicle_image_asset_foundation'; + END IF; +END; +$migration$; +-- 0052_2026081403_vehicle_image_asset_catalog_campaign.sql +DO $migration$ +DECLARE recorded_hash TEXT; +BEGIN + SELECT sql_sha256 INTO recorded_hash FROM public.schema_migrations WHERE version = '2026081403_vehicle_image_asset_catalog_campaign'; + IF FOUND THEN + IF recorded_hash IS NOT NULL AND recorded_hash <> '060c032a543ea290885397c87acd5043d93b7955f803a1610e397379fedea544' THEN + RAISE EXCEPTION 'Applied migration checksum differs: 2026081403_vehicle_image_asset_catalog_campaign'; + END IF; + IF recorded_hash IS NULL THEN + UPDATE public.schema_migrations SET sql_sha256 = '060c032a543ea290885397c87acd5043d93b7955f803a1610e397379fedea544' WHERE version = '2026081403_vehicle_image_asset_catalog_campaign'; + END IF; + ELSE + EXECUTE $source_060c032a543ea290$-- migration: 2026081403_vehicle_image_asset_catalog_campaign -- Operator-controlled, preview-first population of the canonical Overview -- catalog. Creating a run only freezes eligible read snapshots. A dedicated -- worker hashes and validates those source JPEGs without publishing files; @@ -4329,7 +5488,29 @@ CREATE INDEX IF NOT EXISTS idx_vehicle_image_asset_catalog_items_hash INSERT INTO public.schema_migrations(version,description) VALUES ('2026081403_vehicle_image_asset_catalog_campaign','Add a durable preview-first, operator-confirmed campaign for canonical Overview assets.') ON CONFLICT(version) DO NOTHING; +$source_060c032a543ea290$; + IF NOT EXISTS (SELECT 1 FROM public.schema_migrations WHERE version = '2026081403_vehicle_image_asset_catalog_campaign') THEN + RAISE EXCEPTION 'Migration did not record completion: 2026081403_vehicle_image_asset_catalog_campaign'; + END IF; + UPDATE public.schema_migrations SET sql_sha256 = '060c032a543ea290885397c87acd5043d93b7955f803a1610e397379fedea544' WHERE version = '2026081403_vehicle_image_asset_catalog_campaign'; + END IF; +END; +$migration$; +-- 0053_2026081404_vehicle_image_asset_live_catalog.sql +DO $migration$ +DECLARE recorded_hash TEXT; +BEGIN + SELECT sql_sha256 INTO recorded_hash FROM public.schema_migrations WHERE version = '2026081404_vehicle_image_asset_live_catalog'; + IF FOUND THEN + IF recorded_hash IS NOT NULL AND recorded_hash <> 'f3d636a0fe38158e5be274ea9a77e6b7f3de753616a135005cf8df3550326eb4' THEN + RAISE EXCEPTION 'Applied migration checksum differs: 2026081404_vehicle_image_asset_live_catalog'; + END IF; + IF recorded_hash IS NULL THEN + UPDATE public.schema_migrations SET sql_sha256 = 'f3d636a0fe38158e5be274ea9a77e6b7f3de753616a135005cf8df3550326eb4' WHERE version = '2026081404_vehicle_image_asset_live_catalog'; + END IF; + ELSE + EXECUTE $source_f3d636a0fe38158e$-- migration: 2026081404_vehicle_image_asset_live_catalog -- Durable, default-off cataloging for Overview images that become ready after -- the initial operator campaign. The live worker is additionally gated on a -- completed campaign and yields whenever an operator campaign is active. No @@ -4424,7 +5605,29 @@ CREATE INDEX IF NOT EXISTS idx_vehicle_image_asset_live_catalog_status INSERT INTO public.schema_migrations(version,description) VALUES ('2026081404_vehicle_image_asset_live_catalog','Add default-off, post-campaign automatic cataloging for newly ready Overview assets.') ON CONFLICT(version) DO NOTHING; +$source_f3d636a0fe38158e$; + IF NOT EXISTS (SELECT 1 FROM public.schema_migrations WHERE version = '2026081404_vehicle_image_asset_live_catalog') THEN + RAISE EXCEPTION 'Migration did not record completion: 2026081404_vehicle_image_asset_live_catalog'; + END IF; + UPDATE public.schema_migrations SET sql_sha256 = 'f3d636a0fe38158e5be274ea9a77e6b7f3de753616a135005cf8df3550326eb4' WHERE version = '2026081404_vehicle_image_asset_live_catalog'; + END IF; +END; +$migration$; +-- 0054_2026081405_vehicle_event_shadow_correlation.sql +DO $migration$ +DECLARE recorded_hash TEXT; +BEGIN + SELECT sql_sha256 INTO recorded_hash FROM public.schema_migrations WHERE version = '2026081405_vehicle_event_shadow_correlation'; + IF FOUND THEN + IF recorded_hash IS NOT NULL AND recorded_hash <> '0aae75ff13d3e61e7caaea6382fa0629b3135979a578e3a22a32765edb7dd418' THEN + RAISE EXCEPTION 'Applied migration checksum differs: 2026081405_vehicle_event_shadow_correlation'; + END IF; + IF recorded_hash IS NULL THEN + UPDATE public.schema_migrations SET sql_sha256 = '0aae75ff13d3e61e7caaea6382fa0629b3135979a578e3a22a32765edb7dd418' WHERE version = '2026081405_vehicle_event_shadow_correlation'; + END IF; + ELSE + EXECUTE $source_0aae75ff13d3e61e$-- migration: 2026081405_vehicle_event_shadow_correlation -- Provider-neutral shadow passage events built only from current, -- identity-eligible canonical Overview links. The correlator is default-off, -- never gates ingestion or Vehicle View readiness, and does not alter the @@ -4611,7 +5814,29 @@ CREATE INDEX IF NOT EXISTS idx_vehicle_event_shadow_decisions_recent INSERT INTO public.schema_migrations(version,description) VALUES ('2026081405_vehicle_event_shadow_correlation','Add default-off provider-neutral shadow correlation for conservative paired canonical Overview observations.') ON CONFLICT(version) DO NOTHING; +$source_0aae75ff13d3e61e$; + IF NOT EXISTS (SELECT 1 FROM public.schema_migrations WHERE version = '2026081405_vehicle_event_shadow_correlation') THEN + RAISE EXCEPTION 'Migration did not record completion: 2026081405_vehicle_event_shadow_correlation'; + END IF; + UPDATE public.schema_migrations SET sql_sha256 = '0aae75ff13d3e61e7caaea6382fa0629b3135979a578e3a22a32765edb7dd418' WHERE version = '2026081405_vehicle_event_shadow_correlation'; + END IF; +END; +$migration$; +-- 0055_2026081406_vehicle_image_crop_campaign.sql +DO $migration$ +DECLARE recorded_hash TEXT; +BEGIN + SELECT sql_sha256 INTO recorded_hash FROM public.schema_migrations WHERE version = '2026081406_vehicle_image_crop_campaign'; + IF FOUND THEN + IF recorded_hash IS NOT NULL AND recorded_hash <> '70f03aca9952e417f45d4ca69074d6c419bc987c8b738b5df125e0c24cbf0ec5' THEN + RAISE EXCEPTION 'Applied migration checksum differs: 2026081406_vehicle_image_crop_campaign'; + END IF; + IF recorded_hash IS NULL THEN + UPDATE public.schema_migrations SET sql_sha256 = '70f03aca9952e417f45d4ca69074d6c419bc987c8b738b5df125e0c24cbf0ec5' WHERE version = '2026081406_vehicle_image_crop_campaign'; + END IF; + ELSE + EXECUTE $source_70f03aca9952e417$-- migration: 2026081406_vehicle_image_crop_campaign -- Asset-owned vehicle crops derived from immutable canonical Overview JPEGs. -- The migration is deliberately inert: it creates no run, job, derivative, -- file, embedding, attribute, ReID result, or external-provider request. @@ -4853,7 +6078,29 @@ ALTER TABLE public.storage_reconciliation_runs INSERT INTO public.schema_migrations(version,description) VALUES ('2026081406_vehicle_image_crop_campaign','Add inert asset-owned canonical Overview vehicle crops with preview-first bounded operator batches and storage-reference protection.') ON CONFLICT(version) DO NOTHING; +$source_70f03aca9952e417$; + IF NOT EXISTS (SELECT 1 FROM public.schema_migrations WHERE version = '2026081406_vehicle_image_crop_campaign') THEN + RAISE EXCEPTION 'Migration did not record completion: 2026081406_vehicle_image_crop_campaign'; + END IF; + UPDATE public.schema_migrations SET sql_sha256 = '70f03aca9952e417f45d4ca69074d6c419bc987c8b738b5df125e0c24cbf0ec5' WHERE version = '2026081406_vehicle_image_crop_campaign'; + END IF; +END; +$migration$; +-- 0056_2026081501_vehicle_image_crop_live_worker.sql +DO $migration$ +DECLARE recorded_hash TEXT; +BEGIN + SELECT sql_sha256 INTO recorded_hash FROM public.schema_migrations WHERE version = '2026081501_vehicle_image_crop_live_worker'; + IF FOUND THEN + IF recorded_hash IS NOT NULL AND recorded_hash <> 'dd44c7ed464e4a0ac52ac73752df3d4fde5ca8cccd492d05b0b633d6ff877be7' THEN + RAISE EXCEPTION 'Applied migration checksum differs: 2026081501_vehicle_image_crop_live_worker'; + END IF; + IF recorded_hash IS NULL THEN + UPDATE public.schema_migrations SET sql_sha256 = 'dd44c7ed464e4a0ac52ac73752df3d4fde5ca8cccd492d05b0b633d6ff877be7' WHERE version = '2026081501_vehicle_image_crop_live_worker'; + END IF; + ELSE + EXECUTE $source_dd44c7ed464e4a0a$-- migration: 2026081501_vehicle_image_crop_live_worker -- Durable, default-off crop generation for canonical Overview assets that -- become identity-eligible after the operator-controlled crop campaign. The -- migration queues no work. The low-priority worker reuses the exact campaign @@ -4961,7 +6208,29 @@ CREATE INDEX IF NOT EXISTS idx_vehicle_image_crop_live_status INSERT INTO public.schema_migrations(version,description) VALUES ('2026081501_vehicle_image_crop_live_worker','Add default-off automatic canonical Overview crop generation after a completed operator crop campaign.') ON CONFLICT(version) DO NOTHING; +$source_dd44c7ed464e4a0a$; + IF NOT EXISTS (SELECT 1 FROM public.schema_migrations WHERE version = '2026081501_vehicle_image_crop_live_worker') THEN + RAISE EXCEPTION 'Migration did not record completion: 2026081501_vehicle_image_crop_live_worker'; + END IF; + UPDATE public.schema_migrations SET sql_sha256 = 'dd44c7ed464e4a0ac52ac73752df3d4fde5ca8cccd492d05b0b633d6ff877be7' WHERE version = '2026081501_vehicle_image_crop_live_worker'; + END IF; +END; +$migration$; +-- 0057_2026081502_vehicle_overview_framing_repair.sql +DO $migration$ +DECLARE recorded_hash TEXT; +BEGIN + SELECT sql_sha256 INTO recorded_hash FROM public.schema_migrations WHERE version = '2026081502_vehicle_overview_framing_repair'; + IF FOUND THEN + IF recorded_hash IS NOT NULL AND recorded_hash <> 'd2f17a71a765a524dd8b44c6f7c6e21e3582d1f119bc84c8d81c5789426b75ec' THEN + RAISE EXCEPTION 'Applied migration checksum differs: 2026081502_vehicle_overview_framing_repair'; + END IF; + IF recorded_hash IS NULL THEN + UPDATE public.schema_migrations SET sql_sha256 = 'd2f17a71a765a524dd8b44c6f7c6e21e3582d1f119bc84c8d81c5789426b75ec' WHERE version = '2026081502_vehicle_overview_framing_repair'; + END IF; + ELSE + EXECUTE $source_d2f17a71a765a524$-- migration: 2026081502_vehicle_overview_framing_repair -- Operator-selected repair of genuinely clipped or overly tight direct -- Overview images. Preview rows freeze the original ready image and its -- acquisition profile. No migration work is queued, and the original image @@ -5118,7 +6387,29 @@ CREATE INDEX IF NOT EXISTS idx_overview_framing_repair_jobs_run INSERT INTO public.schema_migrations(version,description) VALUES ('2026081502_vehicle_overview_framing_repair','Add inert preview-first bounded repair jobs for operator-selected edge-clipped or overly tight direct Overview images while preserving the current image unless a replacement proves more complete.') ON CONFLICT(version) DO NOTHING; +$source_d2f17a71a765a524$; + IF NOT EXISTS (SELECT 1 FROM public.schema_migrations WHERE version = '2026081502_vehicle_overview_framing_repair') THEN + RAISE EXCEPTION 'Migration did not record completion: 2026081502_vehicle_overview_framing_repair'; + END IF; + UPDATE public.schema_migrations SET sql_sha256 = 'd2f17a71a765a524dd8b44c6f7c6e21e3582d1f119bc84c8d81c5789426b75ec' WHERE version = '2026081502_vehicle_overview_framing_repair'; + END IF; +END; +$migration$; +-- 0058_2026081503_overview_framing_repair_export_identity.sql +DO $migration$ +DECLARE recorded_hash TEXT; +BEGIN + SELECT sql_sha256 INTO recorded_hash FROM public.schema_migrations WHERE version = '2026081503_overview_framing_repair_export_identity'; + IF FOUND THEN + IF recorded_hash IS NOT NULL AND recorded_hash <> 'f7bfbea57c60ca6650d861656f4de104182095b2b3c27a17371db5a064ebaa5a' THEN + RAISE EXCEPTION 'Applied migration checksum differs: 2026081503_overview_framing_repair_export_identity'; + END IF; + IF recorded_hash IS NULL THEN + UPDATE public.schema_migrations SET sql_sha256 = 'f7bfbea57c60ca6650d861656f4de104182095b2b3c27a17371db5a064ebaa5a' WHERE version = '2026081503_overview_framing_repair_export_identity'; + END IF; + ELSE + EXECUTE $source_f7bfbea57c60ca66$-- migration: 2026081503_overview_framing_repair_export_identity -- A framing repair is a distinct semantic export for the same read and source -- window. Its per-job identity prevents collisions with the original live -- export while keeping retries and worker restarts idempotent. @@ -5132,7 +6423,29 @@ ALTER TABLE public.blue_iris_timeline_exports INSERT INTO public.schema_migrations(version,description) VALUES ('2026081503_overview_framing_repair_export_identity','Give each guarded Overview framing repair a stable distinct timeline-export identity while permitting its claim-owned ready Vehicle View to use the export ledger.') ON CONFLICT(version) DO NOTHING; +$source_f7bfbea57c60ca66$; + IF NOT EXISTS (SELECT 1 FROM public.schema_migrations WHERE version = '2026081503_overview_framing_repair_export_identity') THEN + RAISE EXCEPTION 'Migration did not record completion: 2026081503_overview_framing_repair_export_identity'; + END IF; + UPDATE public.schema_migrations SET sql_sha256 = 'f7bfbea57c60ca6650d861656f4de104182095b2b3c27a17371db5a064ebaa5a' WHERE version = '2026081503_overview_framing_repair_export_identity'; + END IF; +END; +$migration$; +-- 0059_2026081504_withdraw_overview_framing_repair.sql +DO $migration$ +DECLARE recorded_hash TEXT; +BEGIN + SELECT sql_sha256 INTO recorded_hash FROM public.schema_migrations WHERE version = '2026081504_withdraw_overview_framing_repair'; + IF FOUND THEN + IF recorded_hash IS NOT NULL AND recorded_hash <> '9ddfea79d5b5b08dc55b3d77352bfe855cc55f917ac11d11e914d00e1f9aab2b' THEN + RAISE EXCEPTION 'Applied migration checksum differs: 2026081504_withdraw_overview_framing_repair'; + END IF; + IF recorded_hash IS NULL THEN + UPDATE public.schema_migrations SET sql_sha256 = '9ddfea79d5b5b08dc55b3d77352bfe855cc55f917ac11d11e914d00e1f9aab2b' WHERE version = '2026081504_withdraw_overview_framing_repair'; + END IF; + ELSE + EXECUTE $source_9ddfea79d5b5b08d$-- migration: 2026081504_withdraw_overview_framing_repair -- The saved-pixel framing audit and operator repair campaign were withdrawn -- after production canaries. Preserve the additive tables as immutable audit -- history, restore any read still owned by an interrupted repair claim to its @@ -5214,7 +6527,29 @@ END $$; INSERT INTO public.schema_migrations(version,description) VALUES ('2026081504_withdraw_overview_framing_repair','Withdraw the saved-pixel framing audit and operator repair workflow while restoring any interrupted read and retaining prior repair evidence as inert audit history.') ON CONFLICT(version) DO NOTHING; +$source_9ddfea79d5b5b08d$; + IF NOT EXISTS (SELECT 1 FROM public.schema_migrations WHERE version = '2026081504_withdraw_overview_framing_repair') THEN + RAISE EXCEPTION 'Migration did not record completion: 2026081504_withdraw_overview_framing_repair'; + END IF; + UPDATE public.schema_migrations SET sql_sha256 = '9ddfea79d5b5b08dc55b3d77352bfe855cc55f917ac11d11e914d00e1f9aab2b' WHERE version = '2026081504_withdraw_overview_framing_repair'; + END IF; +END; +$migration$; +-- 0060_2026081505_vehicle_asset_embedding_campaign.sql +DO $migration$ +DECLARE recorded_hash TEXT; +BEGIN + SELECT sql_sha256 INTO recorded_hash FROM public.schema_migrations WHERE version = '2026081505_vehicle_asset_embedding_campaign'; + IF FOUND THEN + IF recorded_hash IS NOT NULL AND recorded_hash <> '605848c4fb1a97499e124634cc95144b58d79838e41425b0ea952818ed57ac29' THEN + RAISE EXCEPTION 'Applied migration checksum differs: 2026081505_vehicle_asset_embedding_campaign'; + END IF; + IF recorded_hash IS NULL THEN + UPDATE public.schema_migrations SET sql_sha256 = '605848c4fb1a97499e124634cc95144b58d79838e41425b0ea952818ed57ac29' WHERE version = '2026081505_vehicle_asset_embedding_campaign'; + END IF; + ELSE + EXECUTE $source_605848c4fb1a9749$-- migration: 2026081505_vehicle_asset_embedding_campaign -- Provider-neutral embeddings for immutable canonical Overview crop bytes. -- This migration is deliberately inert: it creates no run, job, embedding, -- profile, cluster, attribute, ReID assignment, or external-provider request. @@ -5395,7 +6730,29 @@ CREATE INDEX IF NOT EXISTS idx_vehicle_asset_embedding_jobs_claim INSERT INTO public.schema_migrations(version,description) VALUES ('2026081505_vehicle_asset_embedding_campaign','Add inert provider-neutral canonical crop embeddings and a preview-first operator campaign without enabling any ReID v2 consumer.') ON CONFLICT(version) DO NOTHING; +$source_605848c4fb1a9749$; + IF NOT EXISTS (SELECT 1 FROM public.schema_migrations WHERE version = '2026081505_vehicle_asset_embedding_campaign') THEN + RAISE EXCEPTION 'Migration did not record completion: 2026081505_vehicle_asset_embedding_campaign'; + END IF; + UPDATE public.schema_migrations SET sql_sha256 = '605848c4fb1a97499e124634cc95144b58d79838e41425b0ea952818ed57ac29' WHERE version = '2026081505_vehicle_asset_embedding_campaign'; + END IF; +END; +$migration$; +-- 0061_2026081506_vehicle_asset_attribute_campaign.sql +DO $migration$ +DECLARE recorded_hash TEXT; +BEGIN + SELECT sql_sha256 INTO recorded_hash FROM public.schema_migrations WHERE version = '2026081506_vehicle_asset_attribute_campaign'; + IF FOUND THEN + IF recorded_hash IS NOT NULL AND recorded_hash <> '0f901cd4464f544da2250edae833bd05bafa70b85be2e6053875b97e8dc57f97' THEN + RAISE EXCEPTION 'Applied migration checksum differs: 2026081506_vehicle_asset_attribute_campaign'; + END IF; + IF recorded_hash IS NULL THEN + UPDATE public.schema_migrations SET sql_sha256 = '0f901cd4464f544da2250edae833bd05bafa70b85be2e6053875b97e8dc57f97' WHERE version = '2026081506_vehicle_asset_attribute_campaign'; + END IF; + ELSE + EXECUTE $source_0f901cd4464f544d$-- migration: 2026081506_vehicle_asset_attribute_campaign -- Provider-neutral local attribute observations for immutable canonical crop bytes. -- This migration is deliberately inert: it creates no run or job and does not -- alter current read-owned attributes, ReID, assignments, events, or providers. @@ -5593,7 +6950,29 @@ CREATE INDEX IF NOT EXISTS idx_vehicle_asset_attribute_jobs_claim INSERT INTO public.schema_migrations(version,description) VALUES ('2026081506_vehicle_asset_attribute_campaign','Add inert immutable crop-owned local color and body-type observations plus a preview-first operator campaign without changing current ReID or external providers.') ON CONFLICT(version) DO NOTHING; +$source_0f901cd4464f544d$; + IF NOT EXISTS (SELECT 1 FROM public.schema_migrations WHERE version = '2026081506_vehicle_asset_attribute_campaign') THEN + RAISE EXCEPTION 'Migration did not record completion: 2026081506_vehicle_asset_attribute_campaign'; + END IF; + UPDATE public.schema_migrations SET sql_sha256 = '0f901cd4464f544da2250edae833bd05bafa70b85be2e6053875b97e8dc57f97' WHERE version = '2026081506_vehicle_asset_attribute_campaign'; + END IF; +END; +$migration$; +-- 0062_2026081507_vehicle_reid_v2_pair_reviews.sql +DO $migration$ +DECLARE recorded_hash TEXT; +BEGIN + SELECT sql_sha256 INTO recorded_hash FROM public.schema_migrations WHERE version = '2026081507_vehicle_reid_v2_pair_reviews'; + IF FOUND THEN + IF recorded_hash IS NOT NULL AND recorded_hash <> 'f42b62c3ca9bc21a876b730b920da7b23b87c774047676b76bfe007a7533e487' THEN + RAISE EXCEPTION 'Applied migration checksum differs: 2026081507_vehicle_reid_v2_pair_reviews'; + END IF; + IF recorded_hash IS NULL THEN + UPDATE public.schema_migrations SET sql_sha256 = 'f42b62c3ca9bc21a876b730b920da7b23b87c774047676b76bfe007a7533e487' WHERE version = '2026081507_vehicle_reid_v2_pair_reviews'; + END IF; + ELSE + EXECUTE $source_f42b62c3ca9bc21a$-- migration: 2026081507_vehicle_reid_v2_pair_reviews -- Human ReID v2 labels belong to an immutable canonical crop pair and exact -- embedding contract. They are calibration evidence only: this migration -- creates no profile, cluster, assignment, threshold, queue, or provider call. @@ -5666,7 +7045,29 @@ CREATE INDEX IF NOT EXISTS idx_vehicle_reid_v2_pair_reviews_high INSERT INTO public.schema_migrations(version,description) VALUES ('2026081507_vehicle_reid_v2_pair_reviews','Add audited crop-owned ReID v2 Same, Different, and Unsure pair labels for human calibration without changing profiles or assignments.') ON CONFLICT(version) DO NOTHING; +$source_f42b62c3ca9bc21a$; + IF NOT EXISTS (SELECT 1 FROM public.schema_migrations WHERE version = '2026081507_vehicle_reid_v2_pair_reviews') THEN + RAISE EXCEPTION 'Migration did not record completion: 2026081507_vehicle_reid_v2_pair_reviews'; + END IF; + UPDATE public.schema_migrations SET sql_sha256 = 'f42b62c3ca9bc21a876b730b920da7b23b87c774047676b76bfe007a7533e487' WHERE version = '2026081507_vehicle_reid_v2_pair_reviews'; + END IF; +END; +$migration$; +-- 0063_2026081601_vehicle_reid_v2_diverse_review_campaign.sql +DO $migration$ +DECLARE recorded_hash TEXT; +BEGIN + SELECT sql_sha256 INTO recorded_hash FROM public.schema_migrations WHERE version = '2026081601_vehicle_reid_v2_diverse_review_campaign'; + IF FOUND THEN + IF recorded_hash IS NOT NULL AND recorded_hash <> 'b241ac3e6dd1f3d10ddb89fad0a07ba00b7a816984ee4cb78db9a6027ea17acb' THEN + RAISE EXCEPTION 'Applied migration checksum differs: 2026081601_vehicle_reid_v2_diverse_review_campaign'; + END IF; + IF recorded_hash IS NULL THEN + UPDATE public.schema_migrations SET sql_sha256 = 'b241ac3e6dd1f3d10ddb89fad0a07ba00b7a816984ee4cb78db9a6027ea17acb' WHERE version = '2026081601_vehicle_reid_v2_diverse_review_campaign'; + END IF; + ELSE + EXECUTE $source_b241ac3e6dd1f3d1$-- migration: 2026081601_vehicle_reid_v2_diverse_review_campaign -- One operator-started campaign can collect a bounded, diversity-aware human -- sample without recycling the vehicles used by earlier pair reviews. The -- campaign is only a review-workload boundary; it never applies a ReID @@ -5734,7 +7135,29 @@ CREATE INDEX IF NOT EXISTS idx_vehicle_reid_v2_pair_reviews_campaign INSERT INTO public.schema_migrations(version,description) VALUES ('2026081601_vehicle_reid_v2_diverse_review_campaign','Add one explicit 500-review ReID v2 diversity campaign with frozen inventory and campaign-bound human labels; automatic plate evidence remains visibly separate and no identity threshold or assignment is applied.') ON CONFLICT(version) DO NOTHING; +$source_b241ac3e6dd1f3d1$; + IF NOT EXISTS (SELECT 1 FROM public.schema_migrations WHERE version = '2026081601_vehicle_reid_v2_diverse_review_campaign') THEN + RAISE EXCEPTION 'Migration did not record completion: 2026081601_vehicle_reid_v2_diverse_review_campaign'; + END IF; + UPDATE public.schema_migrations SET sql_sha256 = 'b241ac3e6dd1f3d10ddb89fad0a07ba00b7a816984ee4cb78db9a6027ea17acb' WHERE version = '2026081601_vehicle_reid_v2_diverse_review_campaign'; + END IF; +END; +$migration$; +-- 0064_2026081602_vehicle_reid_v2_profile_candidates.sql +DO $migration$ +DECLARE recorded_hash TEXT; +BEGIN + SELECT sql_sha256 INTO recorded_hash FROM public.schema_migrations WHERE version = '2026081602_vehicle_reid_v2_profile_candidates'; + IF FOUND THEN + IF recorded_hash IS NOT NULL AND recorded_hash <> '1ae636c695df115b0248d3f9dd8c13a2fb8698805eca6e696f68c3a18f5a571c' THEN + RAISE EXCEPTION 'Applied migration checksum differs: 2026081602_vehicle_reid_v2_profile_candidates'; + END IF; + IF recorded_hash IS NULL THEN + UPDATE public.schema_migrations SET sql_sha256 = '1ae636c695df115b0248d3f9dd8c13a2fb8698805eca6e696f68c3a18f5a571c' WHERE version = '2026081602_vehicle_reid_v2_profile_candidates'; + END IF; + ELSE + EXECUTE $source_1ae636c695df115b$-- migration: 2026081602_vehicle_reid_v2_profile_candidates -- ReID v2 profile candidates are immutable shadow snapshots built only from -- exact effective/corrected plate agreement and audited Same-vehicle labels. -- They preserve explicit evidence and do not infer a score cutoff, @@ -5890,7 +7313,29 @@ FOR EACH ROW EXECUTE FUNCTION public.prevent_vehicle_reid_v2_profile_candidate_m INSERT INTO public.schema_migrations(version,description) VALUES ('2026081602_vehicle_reid_v2_profile_candidates','Add immutable evidence-backed ReID v2 shadow profile candidate snapshots using exact effective plates and audited Same labels, with conflicts retained separately and no threshold, cluster, or assignment write.') ON CONFLICT(version) DO NOTHING; +$source_1ae636c695df115b$; + IF NOT EXISTS (SELECT 1 FROM public.schema_migrations WHERE version = '2026081602_vehicle_reid_v2_profile_candidates') THEN + RAISE EXCEPTION 'Migration did not record completion: 2026081602_vehicle_reid_v2_profile_candidates'; + END IF; + UPDATE public.schema_migrations SET sql_sha256 = '1ae636c695df115b0248d3f9dd8c13a2fb8698805eca6e696f68c3a18f5a571c' WHERE version = '2026081602_vehicle_reid_v2_profile_candidates'; + END IF; +END; +$migration$; +-- 0065_2026092701_native_reid.sql +DO $migration$ +DECLARE recorded_hash TEXT; +BEGIN + SELECT sql_sha256 INTO recorded_hash FROM public.schema_migrations WHERE version = '2026092701_native_reid'; + IF FOUND THEN + IF recorded_hash IS NOT NULL AND recorded_hash <> '97dca9b1f8253d31379799d1b1a775c196f5141565ee8ee19a40a05c58fd7cf9' THEN + RAISE EXCEPTION 'Applied migration checksum differs: 2026092701_native_reid'; + END IF; + IF recorded_hash IS NULL THEN + UPDATE public.schema_migrations SET sql_sha256 = '97dca9b1f8253d31379799d1b1a775c196f5141565ee8ee19a40a05c58fd7cf9' WHERE version = '2026092701_native_reid'; + END IF; + ELSE + EXECUTE $source_97dca9b1f8253d31$-- migration: 2026092701_native_reid -- One-time retirement of obsolete derived-index objects on upgrade. -- Run only with the app stopped and a verified database backup (the updater -- enforces both). Original reads, images, users, tags, and reviews are retained. @@ -7145,7 +8590,7 @@ WHERE labels.embedding_model = 'canonical-crop-direction-v2'; CREATE OR REPLACE VIEW public.current_vehicle_direction_observations AS SELECT observations.* FROM public.vehicle_direction_observations observations -WHERE observations.classifier_version IN ('blue-iris-zone-crossing-v1','axis-lpv-direction-v1') +WHERE observations.classifier_version IN ('blue-iris-zone-crossing-v1','blue-iris-zone-crossing-v2-primary','axis-lpv-direction-v1') OR (observations.classifier_version = 'canonical-crop-orientation-knn-v2' AND EXISTS (SELECT 1 FROM public.vehicle_direction_sources source WHERE source.read_id = observations.read_id @@ -7153,13 +8598,35 @@ WHERE observations.classifier_version IN ('blue-iris-zone-crossing-v1','axis-lpv -- Retire only derived predictions/calibration that cannot identify their crop. DELETE FROM public.vehicle_direction_observations - WHERE classifier_version NOT IN ('blue-iris-zone-crossing-v1','axis-lpv-direction-v1') AND source_embedding_id IS NULL; + WHERE classifier_version IN ('vehicle-reid-orientation-knn-v1','canonical-crop-orientation-knn-v2') AND source_embedding_id IS NULL; DELETE FROM public.vehicle_orientation_labels WHERE source_embedding_id IS NULL; INSERT INTO public.schema_migrations(version, description) VALUES ('2026092701_native_reid', 'Native canonical vehicle identity and automatic image processing') ON CONFLICT (version) DO NOTHING; +$source_97dca9b1f8253d31$; + IF NOT EXISTS (SELECT 1 FROM public.schema_migrations WHERE version = '2026092701_native_reid') THEN + RAISE EXCEPTION 'Migration did not record completion: 2026092701_native_reid'; + END IF; + UPDATE public.schema_migrations SET sql_sha256 = '97dca9b1f8253d31379799d1b1a775c196f5141565ee8ee19a40a05c58fd7cf9' WHERE version = '2026092701_native_reid'; + END IF; +END; +$migration$; +-- 0066_2026082201_optional_radar_events.sql +DO $migration$ +DECLARE recorded_hash TEXT; +BEGIN + SELECT sql_sha256 INTO recorded_hash FROM public.schema_migrations WHERE version = '2026082201_optional_radar_events'; + IF FOUND THEN + IF recorded_hash IS NOT NULL AND recorded_hash <> '2380bba8b3e592493d8a0baa32e5b72c6dbf442fe2dda1086a5fbaba002a7a07' THEN + RAISE EXCEPTION 'Applied migration checksum differs: 2026082201_optional_radar_events'; + END IF; + IF recorded_hash IS NULL THEN + UPDATE public.schema_migrations SET sql_sha256 = '2380bba8b3e592493d8a0baa32e5b72c6dbf442fe2dda1086a5fbaba002a7a07' WHERE version = '2026082201_optional_radar_events'; + END IF; + ELSE + EXECUTE $source_2380bba8b3e59249$-- migration: 2026082201_optional_radar_events -- Direct local speed-radar ingestion. The source reuses an existing MQTT broker -- credential record, stores only bounded JSON vehicle detections, and links one -- event to at most one ALPR read through timestamp and configured direction. @@ -7212,7 +8679,29 @@ CREATE INDEX IF NOT EXISTS idx_radar_events_speed INSERT INTO public.schema_migrations(version,description) VALUES ('2026082201_optional_radar_events','Add default-disabled generic radar event storage for schema compatibility.') ON CONFLICT(version) DO NOTHING; +$source_2380bba8b3e59249$; + IF NOT EXISTS (SELECT 1 FROM public.schema_migrations WHERE version = '2026082201_optional_radar_events') THEN + RAISE EXCEPTION 'Migration did not record completion: 2026082201_optional_radar_events'; + END IF; + UPDATE public.schema_migrations SET sql_sha256 = '2380bba8b3e592493d8a0baa32e5b72c6dbf442fe2dda1086a5fbaba002a7a07' WHERE version = '2026082201_optional_radar_events'; + END IF; +END; +$migration$; +-- 0067_2026082202_blue_iris_camera_inventory.sql +DO $migration$ +DECLARE recorded_hash TEXT; +BEGIN + SELECT sql_sha256 INTO recorded_hash FROM public.schema_migrations WHERE version = '2026082202_blue_iris_camera_inventory'; + IF FOUND THEN + IF recorded_hash IS NOT NULL AND recorded_hash <> '1675696b7675af6e2b0249a0d530719092aa48bea1d0ee47435db9482e0419a7' THEN + RAISE EXCEPTION 'Applied migration checksum differs: 2026082202_blue_iris_camera_inventory'; + END IF; + IF recorded_hash IS NULL THEN + UPDATE public.schema_migrations SET sql_sha256 = '1675696b7675af6e2b0249a0d530719092aa48bea1d0ee47435db9482e0419a7' WHERE version = '2026082202_blue_iris_camera_inventory'; + END IF; + ELSE + EXECUTE $source_1675696b7675af6e$-- migration: 2026082202_blue_iris_camera_inventory -- Cache the sanitized Blue Iris camera inventory so playback links can use the -- stable short camera identifier even when reads and overview profiles retain -- the operator-facing display name. The vehicle-frame worker refreshes this @@ -7235,7 +8724,29 @@ CREATE INDEX IF NOT EXISTS idx_blue_iris_camera_inventory_display_name INSERT INTO public.schema_migrations(version,description) VALUES ('2026082202_blue_iris_camera_inventory','Cache sanitized Blue Iris display-to-short camera mappings for reliable plate-capture and Vehicle View playback links.') ON CONFLICT(version) DO NOTHING; +$source_1675696b7675af6e$; + IF NOT EXISTS (SELECT 1 FROM public.schema_migrations WHERE version = '2026082202_blue_iris_camera_inventory') THEN + RAISE EXCEPTION 'Migration did not record completion: 2026082202_blue_iris_camera_inventory'; + END IF; + UPDATE public.schema_migrations SET sql_sha256 = '1675696b7675af6e2b0249a0d530719092aa48bea1d0ee47435db9482e0419a7' WHERE version = '2026082202_blue_iris_camera_inventory'; + END IF; +END; +$migration$; +-- 0068_2026082203_recognition_filter_indexes.sql +DO $migration$ +DECLARE recorded_hash TEXT; +BEGIN + SELECT sql_sha256 INTO recorded_hash FROM public.schema_migrations WHERE version = '2026082203_recognition_filter_indexes'; + IF FOUND THEN + IF recorded_hash IS NOT NULL AND recorded_hash <> 'b6faa8ece7108b67257e11d599b9f215821b73b1e9759b4b1d092b42d400c1e7' THEN + RAISE EXCEPTION 'Applied migration checksum differs: 2026082203_recognition_filter_indexes'; + END IF; + IF recorded_hash IS NULL THEN + UPDATE public.schema_migrations SET sql_sha256 = 'b6faa8ece7108b67257e11d599b9f215821b73b1e9759b4b1d092b42d400c1e7' WHERE version = '2026082203_recognition_filter_indexes'; + END IF; + ELSE + EXECUTE $source_b6faa8ece7108b67$-- migration: 2026082203_recognition_filter_indexes -- Recognition Feed filters should use bounded index lookups instead of -- rescanning the read history whenever an operator changes a control. CREATE INDEX IF NOT EXISTS idx_plate_reads_filter_camera @@ -7255,7 +8766,29 @@ CREATE INDEX IF NOT EXISTS idx_vehicle_direction_filter_status INSERT INTO public.schema_migrations(version,description) VALUES ('2026082203_recognition_filter_indexes','Add query-path indexes for Recognition Feed camera, tag, review-status, and direction filters.') ON CONFLICT(version) DO NOTHING; +$source_b6faa8ece7108b67$; + IF NOT EXISTS (SELECT 1 FROM public.schema_migrations WHERE version = '2026082203_recognition_filter_indexes') THEN + RAISE EXCEPTION 'Migration did not record completion: 2026082203_recognition_filter_indexes'; + END IF; + UPDATE public.schema_migrations SET sql_sha256 = 'b6faa8ece7108b67257e11d599b9f215821b73b1e9759b4b1d092b42d400c1e7' WHERE version = '2026082203_recognition_filter_indexes'; + END IF; +END; +$migration$; +-- 0069_2026082301_vehicle_passage_foundation.sql +DO $migration$ +DECLARE recorded_hash TEXT; +BEGIN + SELECT sql_sha256 INTO recorded_hash FROM public.schema_migrations WHERE version = '2026082301_vehicle_passage_foundation'; + IF FOUND THEN + IF recorded_hash IS NOT NULL AND recorded_hash <> 'cf4fa5e6908abfa44aa0c68967fb866e0065b4e3cb490352e754193094e9a87c' THEN + RAISE EXCEPTION 'Applied migration checksum differs: 2026082301_vehicle_passage_foundation'; + END IF; + IF recorded_hash IS NULL THEN + UPDATE public.schema_migrations SET sql_sha256 = 'cf4fa5e6908abfa44aa0c68967fb866e0065b4e3cb490352e754193094e9a87c' WHERE version = '2026082301_vehicle_passage_foundation'; + END IF; + ELSE + EXECUTE $source_cf4fa5e6908abfa4$-- migration: 2026082301_vehicle_passage_foundation -- Passage-first traffic foundation. This is additive and default-shadow: the -- legacy radar-to-read fields remain readable while new observations acquire -- explicit clock provenance, audit evidence, and canonical passage ownership. @@ -7706,7 +9239,29 @@ CREATE INDEX IF NOT EXISTS idx_radar_ingest_receipts_duplicate_event INSERT INTO public.schema_migrations(version,description) VALUES ('2026082301_vehicle_passage_foundation','Retain the default-shadow vehicle-passage schema for compatibility without enabling a Community traffic interface.') ON CONFLICT(version) DO NOTHING; +$source_cf4fa5e6908abfa4$; + IF NOT EXISTS (SELECT 1 FROM public.schema_migrations WHERE version = '2026082301_vehicle_passage_foundation') THEN + RAISE EXCEPTION 'Migration did not record completion: 2026082301_vehicle_passage_foundation'; + END IF; + UPDATE public.schema_migrations SET sql_sha256 = 'cf4fa5e6908abfa44aa0c68967fb866e0065b4e3cb490352e754193094e9a87c' WHERE version = '2026082301_vehicle_passage_foundation'; + END IF; +END; +$migration$; +-- 0070_2026100501_portable_vehicle_image_sources.sql +DO $migration$ +DECLARE recorded_hash TEXT; +BEGIN + SELECT sql_sha256 INTO recorded_hash FROM public.schema_migrations WHERE version = '2026100501_portable_vehicle_image_sources'; + IF FOUND THEN + IF recorded_hash IS NOT NULL AND recorded_hash <> 'e9ed04349e8aaaa2d7e8101b36e4dddc6e3a6353776bb7d482064d6cab331916' THEN + RAISE EXCEPTION 'Applied migration checksum differs: 2026100501_portable_vehicle_image_sources'; + END IF; + IF recorded_hash IS NULL THEN + UPDATE public.schema_migrations SET sql_sha256 = 'e9ed04349e8aaaa2d7e8101b36e4dddc6e3a6353776bb7d482064d6cab331916' WHERE version = '2026100501_portable_vehicle_image_sources'; + END IF; + ELSE + EXECUTE $source_e9ed04349e8aaaa2$-- migration: 2026100501_portable_vehicle_image_sources -- Explicitly reviewed full-vehicle recording sources. Existing mappings retain -- overview behavior; a same-camera source requires an explicit opt-in. ALTER TABLE public.vehicle_overview_pair_profiles @@ -7723,7 +9278,29 @@ ALTER TABLE public.vehicle_overview_pair_profiles INSERT INTO public.schema_migrations(version,description) VALUES ('2026100501_portable_vehicle_image_sources','Record explicit whole-vehicle LPR recording sources alongside existing overview mappings.') ON CONFLICT(version) DO NOTHING; +$source_e9ed04349e8aaaa2$; + IF NOT EXISTS (SELECT 1 FROM public.schema_migrations WHERE version = '2026100501_portable_vehicle_image_sources') THEN + RAISE EXCEPTION 'Migration did not record completion: 2026100501_portable_vehicle_image_sources'; + END IF; + UPDATE public.schema_migrations SET sql_sha256 = 'e9ed04349e8aaaa2d7e8101b36e4dddc6e3a6353776bb7d482064d6cab331916' WHERE version = '2026100501_portable_vehicle_image_sources'; + END IF; +END; +$migration$; +-- 0071_2026102201_manual_plate_state_country.sql +DO $migration$ +DECLARE recorded_hash TEXT; +BEGIN + SELECT sql_sha256 INTO recorded_hash FROM public.schema_migrations WHERE version = '2026102201_manual_plate_state_country'; + IF FOUND THEN + IF recorded_hash IS NOT NULL AND recorded_hash <> '89d9d9dfd25c6a3d8fba30c4cff08a5e73630373fd5c66ed8d2ac068caf5c01e' THEN + RAISE EXCEPTION 'Applied migration checksum differs: 2026102201_manual_plate_state_country'; + END IF; + IF recorded_hash IS NULL THEN + UPDATE public.schema_migrations SET sql_sha256 = '89d9d9dfd25c6a3d8fba30c4cff08a5e73630373fd5c66ed8d2ac068caf5c01e' WHERE version = '2026102201_manual_plate_state_country'; + END IF; + ELSE + EXECUTE $source_89d9d9dfd25c6a3d$-- migration: 2026102201_manual_plate_state_country -- Manually entered metadata shared by reads of the same effective plate. ALTER TABLE public.plates ADD COLUMN IF NOT EXISTS state_country VARCHAR(100); CREATE INDEX IF NOT EXISTS idx_plates_state_country_options @@ -7732,8 +9309,29 @@ CREATE INDEX IF NOT EXISTS idx_plates_state_country_options INSERT INTO public.schema_migrations(version, description) VALUES ('2026102201_manual_plate_state_country', 'Add optional manually entered State/Country plate metadata.') ON CONFLICT(version) DO NOTHING; +$source_89d9d9dfd25c6a3d$; + IF NOT EXISTS (SELECT 1 FROM public.schema_migrations WHERE version = '2026102201_manual_plate_state_country') THEN + RAISE EXCEPTION 'Migration did not record completion: 2026102201_manual_plate_state_country'; + END IF; + UPDATE public.schema_migrations SET sql_sha256 = '89d9d9dfd25c6a3d8fba30c4cff08a5e73630373fd5c66ed8d2ac068caf5c01e' WHERE version = '2026102201_manual_plate_state_country'; + END IF; +END; +$migration$; - +-- 0072_2026110101_axis_camera_inputs.sql +DO $migration$ +DECLARE recorded_hash TEXT; +BEGIN + SELECT sql_sha256 INTO recorded_hash FROM public.schema_migrations WHERE version = '2026110101_axis_camera_inputs'; + IF FOUND THEN + IF recorded_hash IS NOT NULL AND recorded_hash <> '5fc6b4a9d1c2cb7952374d83e9c9b6efc95ad14c4894a5faeb39c22f2b38ee8d' THEN + RAISE EXCEPTION 'Applied migration checksum differs: 2026110101_axis_camera_inputs'; + END IF; + IF recorded_hash IS NULL THEN + UPDATE public.schema_migrations SET sql_sha256 = '5fc6b4a9d1c2cb7952374d83e9c9b6efc95ad14c4894a5faeb39c22f2b38ee8d' WHERE version = '2026110101_axis_camera_inputs'; + END IF; + ELSE + EXECUTE $source_5fc6b4a9d1c2cb79$-- migration: 2026110101_axis_camera_inputs -- Configurable incoming camera sources. Existing Blue Iris inputs remain implicit. CREATE TABLE IF NOT EXISTS public.camera_inputs ( id UUID PRIMARY KEY, @@ -7796,7 +9394,29 @@ CREATE INDEX IF NOT EXISTS idx_camera_input_event_passage ON public.camera_input INSERT INTO public.schema_migrations(version,description) VALUES ('2026110101_axis_camera_inputs','Configurable Blue Iris and Axis HTTP/MQTT inputs with durable Axis receipts and passage correlation.') ON CONFLICT(version) DO NOTHING; +$source_5fc6b4a9d1c2cb79$; + IF NOT EXISTS (SELECT 1 FROM public.schema_migrations WHERE version = '2026110101_axis_camera_inputs') THEN + RAISE EXCEPTION 'Migration did not record completion: 2026110101_axis_camera_inputs'; + END IF; + UPDATE public.schema_migrations SET sql_sha256 = '5fc6b4a9d1c2cb7952374d83e9c9b6efc95ad14c4894a5faeb39c22f2b38ee8d' WHERE version = '2026110101_axis_camera_inputs'; + END IF; +END; +$migration$; +-- 0073_2026110102_axis_ready_state.sql +DO $migration$ +DECLARE recorded_hash TEXT; +BEGIN + SELECT sql_sha256 INTO recorded_hash FROM public.schema_migrations WHERE version = '2026110102_axis_ready_state'; + IF FOUND THEN + IF recorded_hash IS NOT NULL AND recorded_hash <> '60b0f2dca6193fc2d6c58eca29e79bf309df06d06ba2953dc18ecd49caac4425' THEN + RAISE EXCEPTION 'Applied migration checksum differs: 2026110102_axis_ready_state'; + END IF; + IF recorded_hash IS NULL THEN + UPDATE public.schema_migrations SET sql_sha256 = '60b0f2dca6193fc2d6c58eca29e79bf309df06d06ba2953dc18ecd49caac4425' WHERE version = '2026110102_axis_ready_state'; + END IF; + ELSE + EXECUTE $source_60b0f2dca6193fc2$-- migration: 2026110102_axis_ready_state ALTER TABLE public.vehicle_direction_observations DROP CONSTRAINT IF EXISTS vehicle_direction_ready_state; ALTER TABLE public.vehicle_direction_observations ADD CONSTRAINT vehicle_direction_ready_state CHECK ( (status='ready' AND direction_label IS NOT NULL AND @@ -7804,3 +9424,121 @@ ALTER TABLE public.vehicle_direction_observations ADD CONSTRAINT vehicle_directi (orientation IN ('front','rear') AND orientation_confidence IS NOT NULL))) OR (status<>'ready' AND orientation='unknown' AND direction_label IS NULL) ) NOT VALID; +INSERT INTO public.schema_migrations(version,description) VALUES ('2026110102_axis_ready_state','Preserve Axis observation state validation in the migration ledger.') ON CONFLICT(version) DO NOTHING; +$source_60b0f2dca6193fc2$; + IF NOT EXISTS (SELECT 1 FROM public.schema_migrations WHERE version = '2026110102_axis_ready_state') THEN + RAISE EXCEPTION 'Migration did not record completion: 2026110102_axis_ready_state'; + END IF; + UPDATE public.schema_migrations SET sql_sha256 = '60b0f2dca6193fc2d6c58eca29e79bf309df06d06ba2953dc18ecd49caac4425' WHERE version = '2026110102_axis_ready_state'; + END IF; +END; +$migration$; + +-- 0074_2026110103_preserve_event_directions.sql +DO $migration$ +DECLARE recorded_hash TEXT; +BEGIN + SELECT sql_sha256 INTO recorded_hash FROM public.schema_migrations WHERE version = '2026110103_preserve_event_directions'; + IF FOUND THEN + IF recorded_hash IS NOT NULL AND recorded_hash <> '416946b095af24910ab99871a5a18cbbce4928e7ed054f114893b9203a087881' THEN + RAISE EXCEPTION 'Applied migration checksum differs: 2026110103_preserve_event_directions'; + END IF; + IF recorded_hash IS NULL THEN + UPDATE public.schema_migrations SET sql_sha256 = '416946b095af24910ab99871a5a18cbbce4928e7ed054f114893b9203a087881' WHERE version = '2026110103_preserve_event_directions'; + END IF; + ELSE + EXECUTE $source_416946b095af2491$-- migration: 2026110103_preserve_event_directions +-- Zone events have no image embedding. Their visibility is independent of crops. +CREATE OR REPLACE VIEW public.current_vehicle_direction_observations AS +SELECT observations.* FROM public.vehicle_direction_observations observations +WHERE observations.classifier_version IN ( + 'blue-iris-zone-crossing-v1', 'blue-iris-zone-crossing-v2-primary', 'axis-lpv-direction-v1' +) OR (observations.classifier_version = 'canonical-crop-orientation-knn-v2' + AND EXISTS (SELECT 1 FROM public.vehicle_direction_sources source + WHERE source.read_id = observations.read_id + AND source.embedding_id = observations.source_embedding_id)); +INSERT INTO public.schema_migrations(version, description) VALUES + ('2026110103_preserve_event_directions', 'Preserve Blue Iris and Axis events and expose current primary direction observations.') +ON CONFLICT(version) DO NOTHING; +$source_416946b095af2491$; + IF NOT EXISTS (SELECT 1 FROM public.schema_migrations WHERE version = '2026110103_preserve_event_directions') THEN + RAISE EXCEPTION 'Migration did not record completion: 2026110103_preserve_event_directions'; + END IF; + UPDATE public.schema_migrations SET sql_sha256 = '416946b095af24910ab99871a5a18cbbce4928e7ed054f114893b9203a087881' WHERE version = '2026110103_preserve_event_directions'; + END IF; +END; +$migration$; + +-- 0075_2026110104_home_assistant_displays.sql +DO $migration$ +DECLARE recorded_hash TEXT; +BEGIN + SELECT sql_sha256 INTO recorded_hash FROM public.schema_migrations WHERE version = '2026110104_home_assistant_displays'; + IF FOUND THEN + IF recorded_hash IS NOT NULL AND recorded_hash <> 'b6094420fd73a4513ffc8475df33ef751b9b4102b35c87753096d9fb6d261b0c' THEN + RAISE EXCEPTION 'Applied migration checksum differs: 2026110104_home_assistant_displays'; + END IF; + IF recorded_hash IS NULL THEN + UPDATE public.schema_migrations SET sql_sha256 = 'b6094420fd73a4513ffc8475df33ef751b9b4102b35c87753096d9fb6d261b0c' WHERE version = '2026110104_home_assistant_displays'; + END IF; + ELSE + EXECUTE $source_b6094420fd73a451$-- migration: 2026110104_home_assistant_displays +CREATE TABLE public.home_assistant_display_grants ( + id UUID PRIMARY KEY, + label VARCHAR(80) NOT NULL CHECK (BTRIM(label) <> ''), + token_hash CHAR(64) NOT NULL UNIQUE CHECK (token_hash ~ '^[0-9a-f]{64}$'), + allowed_origin TEXT NOT NULL, + camera_name VARCHAR(100), + created_by VARCHAR(100) NOT NULL, + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + expires_at TIMESTAMPTZ NOT NULL, + revoked_at TIMESTAMPTZ, + CHECK (expires_at > created_at) +); +INSERT INTO public.schema_migrations(version, description) VALUES + ('2026110104_home_assistant_displays', 'Revocable, camera-scoped read-only display credentials for Home Assistant.') +ON CONFLICT(version) DO NOTHING; +$source_b6094420fd73a451$; + IF NOT EXISTS (SELECT 1 FROM public.schema_migrations WHERE version = '2026110104_home_assistant_displays') THEN + RAISE EXCEPTION 'Migration did not record completion: 2026110104_home_assistant_displays'; + END IF; + UPDATE public.schema_migrations SET sql_sha256 = 'b6094420fd73a4513ffc8475df33ef751b9b4102b35c87753096d9fb6d261b0c' WHERE version = '2026110104_home_assistant_displays'; + END IF; +END; +$migration$; + +-- 0076_2026110105_explicit_lpr_recording_binding.sql +DO $migration$ +DECLARE recorded_hash TEXT; +BEGIN + SELECT sql_sha256 INTO recorded_hash FROM public.schema_migrations WHERE version = '2026110105_explicit_lpr_recording_binding'; + IF FOUND THEN + IF recorded_hash IS NOT NULL AND recorded_hash <> 'ae7c8a94fcbe92b33e41dd60bf1530a28c3e714854e9bba28f8588175ce0791c' THEN + RAISE EXCEPTION 'Applied migration checksum differs: 2026110105_explicit_lpr_recording_binding'; + END IF; + IF recorded_hash IS NULL THEN + UPDATE public.schema_migrations SET sql_sha256 = 'ae7c8a94fcbe92b33e41dd60bf1530a28c3e714854e9bba28f8588175ce0791c' WHERE version = '2026110105_explicit_lpr_recording_binding'; + END IF; + ELSE + EXECUTE $source_ae7c8a94fcbe92b3$-- migration: 2026110105_explicit_lpr_recording_binding +-- An ALPR camera alias and its recording camera display name may differ. The +-- explicitly selected Blue Iris short ID identifies the recording source. +ALTER TABLE public.vehicle_overview_pair_profiles + DROP CONSTRAINT vehicle_overview_distinct_camera_check; +ALTER TABLE public.vehicle_overview_pair_profiles + ADD CONSTRAINT vehicle_overview_distinct_camera_check CHECK ( + (source_mode = 'overview' AND LOWER(BTRIM(source_camera_name)) <> LOWER(BTRIM(plate_camera_name))) + OR (source_mode = 'lpr_camera' AND source_role = 'primary' + AND expected_delta_ms = 0 AND NULLIF(BTRIM(source_camera_short_name), '') IS NOT NULL) + ) NOT VALID; +INSERT INTO public.schema_migrations(version,description) VALUES + ('2026110105_explicit_lpr_recording_binding', 'Bind ALPR camera aliases to an explicitly selected Blue Iris recording short ID.') +ON CONFLICT(version) DO NOTHING; +$source_ae7c8a94fcbe92b3$; + IF NOT EXISTS (SELECT 1 FROM public.schema_migrations WHERE version = '2026110105_explicit_lpr_recording_binding') THEN + RAISE EXCEPTION 'Migration did not record completion: 2026110105_explicit_lpr_recording_binding'; + END IF; + UPDATE public.schema_migrations SET sql_sha256 = 'ae7c8a94fcbe92b33e41dd60bf1530a28c3e714854e9bba28f8588175ce0791c' WHERE version = '2026110105_explicit_lpr_recording_binding'; + END IF; +END; +$migration$; diff --git a/migrations/0001_2026071901_identity_audit_foundation.sql b/migrations/0001_2026071901_identity_audit_foundation.sql new file mode 100644 index 0000000..7f3a0dc --- /dev/null +++ b/migrations/0001_2026071901_identity_audit_foundation.sql @@ -0,0 +1,690 @@ +-- migration: 2026071901_identity_audit_foundation +-- Retire an obsolete reconciliation phase before installing current checks. +DO $reconciliation_upgrade$ +BEGIN + IF to_regclass('public.storage_reconciliation_runs') IS NOT NULL THEN + UPDATE public.storage_reconciliation_runs SET phase = 'plate-reads' + WHERE phase = 'capture-assets'; + ALTER TABLE public.storage_reconciliation_runs + DROP COLUMN IF EXISTS max_capture_asset_id CASCADE, + DROP COLUMN IF EXISTS capture_asset_cursor CASCADE; + END IF; +END +$reconciliation_upgrade$; + +-- Community updates stop the application before applying this transaction. +-- Retain deterministic lock ordering for maintenance tooling that may also hold +-- radar_settings. Clean installs do not have the table yet and skip this lock. +DO $migration_lock$ +BEGIN + IF to_regclass('public.radar_settings') IS NOT NULL THEN + EXECUTE 'LOCK TABLE public.radar_settings IN ACCESS EXCLUSIVE MODE'; + END IF; +END +$migration_lock$; + +CREATE EXTENSION IF NOT EXISTS pg_trgm WITH SCHEMA public; +CREATE EXTENSION IF NOT EXISTS fuzzystrmatch WITH SCHEMA public; + +-- Modify plate_notifications +ALTER TABLE IF EXISTS public.plate_notifications + ADD COLUMN IF NOT EXISTS priority integer DEFAULT 1; + +-- Modify plate_reads +ALTER TABLE IF EXISTS public.plate_reads + ADD COLUMN IF NOT EXISTS camera_name character varying(25), + ADD COLUMN IF NOT EXISTS image_path varchar(255), + ADD COLUMN IF NOT EXISTS thumbnail_path varchar(255), + ADD COLUMN IF NOT EXISTS bi_path varchar(100), + ADD COLUMN IF NOT EXISTS plate_annotation varchar(255), + ADD COLUMN IF NOT EXISTS crop_coordinates int[], + ADD COLUMN IF NOT EXISTS ocr_annotation jsonb, + ADD COLUMN IF NOT EXISTS confidence decimal, + ADD COLUMN IF NOT EXISTS bi_zone varchar(30), + ADD COLUMN IF NOT EXISTS validated boolean DEFAULT false, + ADD COLUMN IF NOT EXISTS event_identity varchar(80); + +-- Exact Blue Iris resubmissions are identified by plate, event time, and +-- camera. The camera column keeps simultaneous observations independent. +CREATE INDEX IF NOT EXISTS idx_plate_reads_event_identity + ON public.plate_reads (plate_number, timestamp, camera_name); + +-- New reads carry a stable event identity. The partial unique index lets +-- historical rows remain nullable while atomically suppressing concurrent +-- resubmissions of the same Blue Iris event. +CREATE UNIQUE INDEX IF NOT EXISTS uq_plate_reads_event_identity + ON public.plate_reads (event_identity) + WHERE event_identity IS NOT NULL; + + +-- Please for the love of god work... +-- Fix in reference to #57 and ipct reports about db config on new installs +DO $$ +BEGIN + IF NOT EXISTS ( + SELECT 1 FROM pg_constraint + WHERE conname = 'plates_pkey' + AND conrelid = 'public.plates'::regclass + ) THEN + ALTER TABLE public.plates ADD CONSTRAINT plates_pkey PRIMARY KEY (plate_number); + END IF; +END $$; + +-- Modify known_plates +ALTER TABLE IF EXISTS public.known_plates + ADD COLUMN IF NOT EXISTS ignore BOOLEAN DEFAULT FALSE; + +-- Modify plates +ALTER TABLE IF EXISTS public.plates + ADD COLUMN IF NOT EXISTS occurrence_count INTEGER NOT NULL DEFAULT 0; + +-- Create index if not exists +DO $$ +BEGIN + IF NOT EXISTS (SELECT 1 FROM pg_indexes WHERE indexname = 'idx_plates_occurrence_count') THEN + CREATE INDEX idx_plates_occurrence_count ON plates(occurrence_count); + END IF; +END $$; + +-- Count incrementing function +CREATE OR REPLACE FUNCTION update_plate_occurrence_count() +RETURNS TRIGGER AS $$ +BEGIN + -- Handle INSERT operation + IF TG_OP = 'INSERT' THEN + INSERT INTO plates (plate_number, occurrence_count) + VALUES (NEW.plate_number, 1) + ON CONFLICT (plate_number) + DO UPDATE SET occurrence_count = plates.occurrence_count + 1; + + -- Handle UPDATE operation (plate number correction) + ELSIF TG_OP = 'UPDATE' AND OLD.plate_number != NEW.plate_number THEN + -- Increment the new plate number count (or create if not exists) + INSERT INTO plates (plate_number, occurrence_count) + VALUES (NEW.plate_number, 1) + ON CONFLICT (plate_number) + DO UPDATE SET occurrence_count = plates.occurrence_count + 1; + + -- Only decrement the old plate if it still exists + UPDATE plates + SET occurrence_count = occurrence_count - 1 + WHERE plate_number = OLD.plate_number; + + -- Clean up if occurrence count reaches zero + DELETE FROM plates + WHERE plate_number = OLD.plate_number + AND occurrence_count <= 0; + + -- Handle DELETE operation + ELSIF TG_OP = 'DELETE' THEN + -- Only attempt to decrement if the plate still exists + UPDATE plates + SET occurrence_count = occurrence_count - 1 + WHERE plate_number = OLD.plate_number; + + -- Clean up if occurrence count reaches zero + DELETE FROM plates + WHERE plate_number = OLD.plate_number + AND occurrence_count <= 0; + END IF; + + RETURN NULL; +END; +$$ LANGUAGE plpgsql; + +-- Update trigger to also handle UPDATE operations +DO $$ +BEGIN + -- Drop the existing trigger if it exists + DROP TRIGGER IF EXISTS plate_reads_count_trigger ON plate_reads; + + -- Create the updated trigger + CREATE TRIGGER plate_reads_count_trigger + AFTER INSERT OR UPDATE OR DELETE ON plate_reads + FOR EACH ROW + EXECUTE FUNCTION update_plate_occurrence_count(); +END $$; + +-- Clerical stuff +CREATE TABLE IF NOT EXISTS devmgmt ( + id SERIAL PRIMARY KEY, + update1 BOOLEAN DEFAULT FALSE +); +INSERT INTO devmgmt (id, update1) +SELECT 1, false +WHERE NOT EXISTS (SELECT 1 FROM devmgmt); + +ALTER TABLE IF EXISTS public.devmgmt + ADD COLUMN IF NOT EXISTS training_last_record INTEGER DEFAULT 0; + + +CREATE TABLE IF NOT EXISTS mqttbrokers ( + id SERIAL PRIMARY KEY, + name VARCHAR(255), + broker VARCHAR(255), + port INTEGER DEFAULT 1883, + topic VARCHAR(255), + username VARCHAR(255), + password VARCHAR(255), + use_tls BOOLEAN DEFAULT FALSE +); + +CREATE TABLE IF NOT EXISTS mqttnotifications ( + id SERIAL PRIMARY KEY, + plate_number VARCHAR(50), + name VARCHAR(255), + enabled BOOLEAN DEFAULT TRUE, + brokerid INTEGER REFERENCES mqttbrokers(id) ON DELETE CASCADE, + message TEXT, + includeKnownPlateInfo BOOLEAN DEFAULT TRUE +); + +-- MQTT integration v2 ------------------------------------------------------- +-- Keep the legacy topic column and mqttnotifications table intact so this +-- migration is non-destructive. The new application no longer uses them. +ALTER TABLE IF EXISTS public.mqttbrokers + ADD COLUMN IF NOT EXISTS enabled BOOLEAN NOT NULL DEFAULT TRUE, + ADD COLUMN IF NOT EXISTS client_id VARCHAR(255), + ADD COLUMN IF NOT EXISTS created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + ADD COLUMN IF NOT EXISTS updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP; + +CREATE TABLE IF NOT EXISTS public.mqtt_settings ( + id SMALLINT PRIMARY KEY DEFAULT 1 CHECK (id = 1), + enabled BOOLEAN NOT NULL DEFAULT FALSE, + base_topic VARCHAR(512) NOT NULL DEFAULT 'Blue Iris/ALPR', + camera_topic_template VARCHAR(512) NOT NULL DEFAULT '{base_topic}/{camera_key}', + default_qos SMALLINT NOT NULL DEFAULT 1 CHECK (default_qos BETWEEN 0 AND 2), + retain_messages BOOLEAN NOT NULL DEFAULT FALSE, + payload_profile VARCHAR(50) NOT NULL DEFAULT 'generic_json' + CHECK (payload_profile IN ('generic_json', 'homeseer', 'home_assistant')), + local_timezone VARCHAR(100) NOT NULL DEFAULT 'UTC', + hour_format SMALLINT NOT NULL DEFAULT 12 CHECK (hour_format IN (12, 24)), + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP +); + +INSERT INTO public.mqtt_settings (id) +VALUES (1) +ON CONFLICT (id) DO NOTHING; + +CREATE TABLE IF NOT EXISTS public.mqtt_cameras ( + id SERIAL PRIMARY KEY, + camera_name VARCHAR(255) NOT NULL, + camera_key VARCHAR(100) NOT NULL UNIQUE, + enabled BOOLEAN NOT NULL DEFAULT TRUE, + topic_override VARCHAR(65535), + first_seen_at TIMESTAMPTZ, + last_seen_at TIMESTAMPTZ, + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + CONSTRAINT mqtt_cameras_camera_key_format + CHECK (camera_key ~ '^[a-z0-9]+(-[a-z0-9]+)*$') +); + +CREATE UNIQUE INDEX IF NOT EXISTS mqtt_cameras_camera_name_lower_key + ON public.mqtt_cameras (LOWER(camera_name)); + +CREATE TABLE IF NOT EXISTS public.mqtt_rules ( + id SERIAL PRIMARY KEY, + name VARCHAR(255) NOT NULL, + enabled BOOLEAN NOT NULL DEFAULT TRUE, + match_type VARCHAR(50) NOT NULL + CHECK (match_type IN ( + 'any_plate', + 'exact_plate', + 'any_known_plate', + 'known_name', + 'tag' + )), + match_value TEXT, + plate_match_mode VARCHAR(20) NOT NULL DEFAULT 'off' + CHECK (plate_match_mode IN ('off', 'strict', 'balanced', 'broad')), + fuzzy_enabled BOOLEAN NOT NULL DEFAULT FALSE, + fuzzy_max_distance SMALLINT NOT NULL DEFAULT 1 + CHECK (fuzzy_max_distance BETWEEN 0 AND 2), + fuzzy_min_length SMALLINT NOT NULL DEFAULT 5 + CHECK (fuzzy_min_length BETWEEN 1 AND 20), + fuzzy_require_unique BOOLEAN NOT NULL DEFAULT TRUE, + fuzzy_ocr_aware BOOLEAN NOT NULL DEFAULT TRUE, + broker_id INTEGER NOT NULL REFERENCES public.mqttbrokers(id) ON DELETE RESTRICT, + destination_mode VARCHAR(50) NOT NULL DEFAULT 'per_camera' + CHECK (destination_mode IN ('per_camera', 'fixed_topic')), + fixed_topic VARCHAR(65535), + message TEXT, + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + CONSTRAINT mqtt_rules_match_value_required CHECK ( + match_type IN ('any_plate', 'any_known_plate') + OR NULLIF(BTRIM(match_value), '') IS NOT NULL + ), + CONSTRAINT mqtt_rules_fixed_topic_required CHECK ( + destination_mode = 'per_camera' + OR NULLIF(BTRIM(fixed_topic), '') IS NOT NULL + ) +); + +ALTER TABLE public.mqtt_rules + ADD COLUMN IF NOT EXISTS plate_match_mode VARCHAR(20); + +UPDATE public.mqtt_rules +SET plate_match_mode = CASE + WHEN fuzzy_enabled THEN 'balanced' + ELSE 'off' +END +WHERE plate_match_mode IS NULL; + +ALTER TABLE public.mqtt_rules + ALTER COLUMN plate_match_mode SET DEFAULT 'off', + ALTER COLUMN plate_match_mode SET NOT NULL; + +ALTER TABLE public.mqtt_rules + DROP CONSTRAINT IF EXISTS mqtt_rules_plate_match_mode_check; + +ALTER TABLE public.mqtt_rules + ADD CONSTRAINT mqtt_rules_plate_match_mode_check + CHECK (plate_match_mode IN ('off', 'strict', 'balanced', 'broad')); + +CREATE TABLE IF NOT EXISTS public.mqtt_rule_cameras ( + rule_id INTEGER NOT NULL REFERENCES public.mqtt_rules(id) ON DELETE CASCADE, + camera_id INTEGER NOT NULL REFERENCES public.mqtt_cameras(id) ON DELETE CASCADE, + PRIMARY KEY (rule_id, camera_id) +); + +CREATE INDEX IF NOT EXISTS idx_mqtt_rules_enabled + ON public.mqtt_rules (enabled) WHERE enabled = TRUE; +CREATE INDEX IF NOT EXISTS idx_mqtt_rules_broker_id + ON public.mqtt_rules (broker_id); +CREATE INDEX IF NOT EXISTS idx_mqtt_rule_cameras_camera_id + ON public.mqtt_rule_cameras (camera_id); +CREATE INDEX IF NOT EXISTS idx_mqtt_cameras_enabled + ON public.mqtt_cameras (enabled) WHERE enabled = TRUE; + +CREATE OR REPLACE FUNCTION public.mqtt_set_updated_at() +RETURNS TRIGGER AS $$ +BEGIN + NEW.updated_at = CURRENT_TIMESTAMP; + RETURN NEW; +END; +$$ LANGUAGE plpgsql; + +DROP TRIGGER IF EXISTS mqttbrokers_set_updated_at ON public.mqttbrokers; +CREATE TRIGGER mqttbrokers_set_updated_at +BEFORE UPDATE ON public.mqttbrokers +FOR EACH ROW EXECUTE FUNCTION public.mqtt_set_updated_at(); + +DROP TRIGGER IF EXISTS mqtt_settings_set_updated_at ON public.mqtt_settings; +CREATE TRIGGER mqtt_settings_set_updated_at +BEFORE UPDATE ON public.mqtt_settings +FOR EACH ROW EXECUTE FUNCTION public.mqtt_set_updated_at(); + +DROP TRIGGER IF EXISTS mqtt_cameras_set_updated_at ON public.mqtt_cameras; +CREATE TRIGGER mqtt_cameras_set_updated_at +BEFORE UPDATE ON public.mqtt_cameras +FOR EACH ROW EXECUTE FUNCTION public.mqtt_set_updated_at(); + +DROP TRIGGER IF EXISTS mqtt_rules_set_updated_at ON public.mqtt_rules; +CREATE TRIGGER mqtt_rules_set_updated_at +BEFORE UPDATE ON public.mqtt_rules +FOR EACH ROW EXECUTE FUNCTION public.mqtt_set_updated_at(); + +-- Durable MQTT delivery outbox and activity history ------------------------- +-- A queue row represents one camera observation going to one broker/topic. +-- The unique dedupe key suppresses only an exact resubmission of that same +-- camera event and destination; different cameras remain independent. +CREATE TABLE IF NOT EXISTS public.mqtt_deliveries ( + id BIGSERIAL PRIMARY KEY, + dedupe_key VARCHAR(80) NOT NULL UNIQUE, + event_id VARCHAR(255) NOT NULL, + read_id INTEGER REFERENCES public.plate_reads(id) ON DELETE SET NULL, + camera_id INTEGER REFERENCES public.mqtt_cameras(id) ON DELETE SET NULL, + camera_key VARCHAR(100) NOT NULL, + camera_name VARCHAR(255) NOT NULL, + broker_id INTEGER NOT NULL REFERENCES public.mqttbrokers(id) ON DELETE RESTRICT, + topic VARCHAR(65535) NOT NULL, + payload JSONB NOT NULL, + qos SMALLINT NOT NULL DEFAULT 1 CHECK (qos BETWEEN 0 AND 2), + retain BOOLEAN NOT NULL DEFAULT FALSE, + status VARCHAR(20) NOT NULL DEFAULT 'pending' + CHECK (status IN ('pending', 'processing', 'retry', 'succeeded', 'dead')), + attempt_count INTEGER NOT NULL DEFAULT 0 CHECK (attempt_count >= 0), + max_attempts SMALLINT NOT NULL DEFAULT 5 CHECK (max_attempts BETWEEN 1 AND 20), + next_attempt_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + locked_at TIMESTAMPTZ, + locked_by VARCHAR(255), + last_error TEXT, + published_at TIMESTAMPTZ, + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + CONSTRAINT mqtt_deliveries_camera_key_format + CHECK (camera_key ~ '^[a-z0-9]+(-[a-z0-9]+)*$'), + CONSTRAINT mqtt_deliveries_payload_object + CHECK (jsonb_typeof(payload) = 'object'), + CONSTRAINT mqtt_deliveries_lock_state CHECK ( + ( + status = 'processing' + AND locked_at IS NOT NULL + AND NULLIF(BTRIM(locked_by), '') IS NOT NULL + ) + OR + ( + status <> 'processing' + AND locked_at IS NULL + AND locked_by IS NULL + ) + ), + CONSTRAINT mqtt_deliveries_published_state CHECK ( + (status = 'succeeded' AND published_at IS NOT NULL) + OR + (status <> 'succeeded' AND published_at IS NULL) + ) +); + +CREATE TABLE IF NOT EXISTS public.mqtt_delivery_attempts ( + id BIGSERIAL PRIMARY KEY, + delivery_id BIGINT NOT NULL + REFERENCES public.mqtt_deliveries(id) ON DELETE CASCADE, + attempt_number INTEGER NOT NULL CHECK (attempt_number > 0), + outcome VARCHAR(20) NOT NULL + CHECK (outcome IN ('succeeded', 'retry', 'dead')), + worker_id VARCHAR(255), + error_code VARCHAR(100), + error_message TEXT, + started_at TIMESTAMPTZ NOT NULL, + completed_at TIMESTAMPTZ NOT NULL, + UNIQUE (delivery_id, attempt_number) +); + +CREATE INDEX IF NOT EXISTS idx_mqtt_deliveries_due + ON public.mqtt_deliveries (next_attempt_at, id) + WHERE status IN ('pending', 'retry'); +CREATE INDEX IF NOT EXISTS idx_mqtt_deliveries_created_at + ON public.mqtt_deliveries (created_at DESC, id DESC); +CREATE INDEX IF NOT EXISTS idx_mqtt_deliveries_read_id + ON public.mqtt_deliveries (read_id); +CREATE INDEX IF NOT EXISTS idx_mqtt_deliveries_broker_id + ON public.mqtt_deliveries (broker_id); +CREATE INDEX IF NOT EXISTS idx_mqtt_delivery_attempts_delivery_id + ON public.mqtt_delivery_attempts (delivery_id, attempt_number DESC); + +DROP TRIGGER IF EXISTS mqtt_deliveries_set_updated_at ON public.mqtt_deliveries; +CREATE TRIGGER mqtt_deliveries_set_updated_at +BEFORE UPDATE ON public.mqtt_deliveries +FOR EACH ROW EXECUTE FUNCTION public.mqtt_set_updated_at(); + +-- Identity, roles, and audit foundation -------------------------------------- +-- This first slice is deliberately non-disruptive: it creates the durable +-- identity model without changing the existing password-only login. A later +-- migration will bootstrap the first named owner and cut sessions over only +-- after the compatibility path has been tested. +CREATE TABLE IF NOT EXISTS public.schema_migrations ( + version VARCHAR(100) PRIMARY KEY, + description TEXT NOT NULL, + applied_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP +); + +CREATE TABLE IF NOT EXISTS public.users ( + id BIGSERIAL PRIMARY KEY, + username VARCHAR(64) NOT NULL, + display_name VARCHAR(120) NOT NULL, + password_hash TEXT NOT NULL, + status VARCHAR(20) NOT NULL DEFAULT 'active' + CHECK (status IN ('active', 'disabled')), + password_changed_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + last_login_at TIMESTAMPTZ, + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + CONSTRAINT users_username_normalized + CHECK ( + username = LOWER(username) + AND username ~ '^[a-z0-9][a-z0-9._-]{1,62}[a-z0-9]$' + ), + CONSTRAINT users_display_name_present + CHECK (NULLIF(BTRIM(display_name), '') IS NOT NULL), + CONSTRAINT users_password_hash_present + CHECK (NULLIF(BTRIM(password_hash), '') IS NOT NULL) +); + +CREATE UNIQUE INDEX IF NOT EXISTS users_username_lower_key + ON public.users (LOWER(username)); + +ALTER TABLE public.users + ADD COLUMN IF NOT EXISTS deleted_at TIMESTAMPTZ; + +ALTER TABLE public.users + ADD COLUMN IF NOT EXISTS must_change_password BOOLEAN NOT NULL DEFAULT FALSE; + +CREATE TABLE IF NOT EXISTS public.roles ( + id SMALLSERIAL PRIMARY KEY, + name VARCHAR(50) NOT NULL UNIQUE, + display_name VARCHAR(80) NOT NULL, + description TEXT NOT NULL, + system_role BOOLEAN NOT NULL DEFAULT TRUE, + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + CONSTRAINT roles_name_format + CHECK (name ~ '^[a-z][a-z0-9_]{2,49}$') +); + +CREATE TABLE IF NOT EXISTS public.permissions ( + id SMALLSERIAL PRIMARY KEY, + permission_key VARCHAR(100) NOT NULL UNIQUE, + description TEXT NOT NULL, + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + CONSTRAINT permissions_key_format + CHECK (permission_key ~ '^[a-z][a-z0-9_]*(\.[a-z][a-z0-9_]*)+$') +); + +CREATE TABLE IF NOT EXISTS public.role_permissions ( + role_id SMALLINT NOT NULL REFERENCES public.roles(id) ON DELETE CASCADE, + permission_id SMALLINT NOT NULL + REFERENCES public.permissions(id) ON DELETE CASCADE, + PRIMARY KEY (role_id, permission_id) +); + +CREATE TABLE IF NOT EXISTS public.user_roles ( + user_id BIGINT NOT NULL REFERENCES public.users(id) ON DELETE CASCADE, + role_id SMALLINT NOT NULL REFERENCES public.roles(id) ON DELETE RESTRICT, + granted_by_user_id BIGINT REFERENCES public.users(id) ON DELETE RESTRICT, + granted_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + PRIMARY KEY (user_id, role_id) +); + +CREATE TABLE IF NOT EXISTS public.user_sessions ( + id BIGSERIAL PRIMARY KEY, + user_id BIGINT NOT NULL REFERENCES public.users(id) ON DELETE CASCADE, + token_hash CHAR(64) NOT NULL UNIQUE, + user_agent VARCHAR(255) NOT NULL DEFAULT 'Unknown Device', + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + last_used_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + expires_at TIMESTAMPTZ NOT NULL, + revoked_at TIMESTAMPTZ, + revoke_reason VARCHAR(100), + CONSTRAINT user_sessions_token_hash_format + CHECK (token_hash ~ '^[0-9a-f]{64}$'), + CONSTRAINT user_sessions_expiration_order + CHECK (expires_at > created_at), + CONSTRAINT user_sessions_revocation_pair + CHECK ( + (revoked_at IS NULL AND revoke_reason IS NULL) + OR + (revoked_at IS NOT NULL AND NULLIF(BTRIM(revoke_reason), '') IS NOT NULL) + ) +); + +CREATE INDEX IF NOT EXISTS idx_user_sessions_active + ON public.user_sessions (user_id, expires_at) + WHERE revoked_at IS NULL; + +CREATE TABLE IF NOT EXISTS public.login_attempt_limits ( + subject_hash CHAR(64) PRIMARY KEY, + failed_attempts INTEGER NOT NULL DEFAULT 0 + CHECK (failed_attempts >= 0), + window_started_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + blocked_until TIMESTAMPTZ, + updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + CONSTRAINT login_attempt_limits_subject_hash_format + CHECK (subject_hash ~ '^[0-9a-f]{64}$') +); + +CREATE INDEX IF NOT EXISTS idx_login_attempt_limits_updated_at + ON public.login_attempt_limits (updated_at); + +CREATE TABLE IF NOT EXISTS public.api_credentials ( + id BIGSERIAL PRIMARY KEY, + owner_user_id BIGINT REFERENCES public.users(id) ON DELETE RESTRICT, + name VARCHAR(120) NOT NULL, + key_prefix VARCHAR(16) NOT NULL UNIQUE, + secret_hash CHAR(64) NOT NULL UNIQUE, + scopes TEXT[] NOT NULL DEFAULT ARRAY[]::TEXT[], + status VARCHAR(20) NOT NULL DEFAULT 'active' + CHECK (status IN ('active', 'revoked')), + last_used_at TIMESTAMPTZ, + expires_at TIMESTAMPTZ, + revoked_at TIMESTAMPTZ, + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + CONSTRAINT api_credentials_name_present + CHECK (NULLIF(BTRIM(name), '') IS NOT NULL), + CONSTRAINT api_credentials_prefix_format + CHECK (key_prefix ~ '^[0-9a-f]{8,16}$'), + CONSTRAINT api_credentials_secret_hash_format + CHECK (secret_hash ~ '^[0-9a-f]{64}$'), + CONSTRAINT api_credentials_scope_values_present + CHECK (array_position(scopes, NULL) IS NULL) +); + +CREATE INDEX IF NOT EXISTS idx_api_credentials_owner + ON public.api_credentials (owner_user_id, status); + +CREATE TABLE IF NOT EXISTS public.audit_events ( + id BIGSERIAL PRIMARY KEY, + actor_user_id BIGINT REFERENCES public.users(id) ON DELETE RESTRICT, + actor_api_credential_id BIGINT + REFERENCES public.api_credentials(id) ON DELETE RESTRICT, + source VARCHAR(20) NOT NULL + CHECK (source IN ('browser', 'api', 'system')), + event_type VARCHAR(100) NOT NULL, + resource_type VARCHAR(100), + resource_id VARCHAR(255), + outcome VARCHAR(20) NOT NULL + CHECK (outcome IN ('succeeded', 'denied', 'failed')), + reason TEXT, + request_id VARCHAR(100), + metadata JSONB NOT NULL DEFAULT '{}'::JSONB + CHECK (jsonb_typeof(metadata) = 'object'), + occurred_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + CONSTRAINT audit_events_single_actor + CHECK (num_nonnulls(actor_user_id, actor_api_credential_id) <= 1), + CONSTRAINT audit_events_type_present + CHECK (NULLIF(BTRIM(event_type), '') IS NOT NULL) +); + +CREATE INDEX IF NOT EXISTS idx_audit_events_occurred_at + ON public.audit_events (occurred_at DESC, id DESC); +CREATE INDEX IF NOT EXISTS idx_audit_events_actor_user + ON public.audit_events (actor_user_id, occurred_at DESC) + WHERE actor_user_id IS NOT NULL; +CREATE INDEX IF NOT EXISTS idx_audit_events_resource + ON public.audit_events (resource_type, resource_id, occurred_at DESC) + WHERE resource_type IS NOT NULL; + +CREATE OR REPLACE FUNCTION public.identity_set_updated_at() +RETURNS TRIGGER AS $$ +BEGIN + NEW.updated_at = CURRENT_TIMESTAMP; + RETURN NEW; +END; +$$ LANGUAGE plpgsql; + +DROP TRIGGER IF EXISTS users_set_updated_at ON public.users; +CREATE TRIGGER users_set_updated_at +BEFORE UPDATE ON public.users +FOR EACH ROW EXECUTE FUNCTION public.identity_set_updated_at(); + +DROP TRIGGER IF EXISTS roles_set_updated_at ON public.roles; +CREATE TRIGGER roles_set_updated_at +BEFORE UPDATE ON public.roles +FOR EACH ROW EXECUTE FUNCTION public.identity_set_updated_at(); + +CREATE OR REPLACE FUNCTION public.prevent_audit_event_mutation() +RETURNS TRIGGER AS $$ +BEGIN + RAISE EXCEPTION 'audit_events is append-only'; +END; +$$ LANGUAGE plpgsql; + +DROP TRIGGER IF EXISTS audit_events_append_only ON public.audit_events; +CREATE TRIGGER audit_events_append_only +BEFORE UPDATE OR DELETE ON public.audit_events +FOR EACH ROW EXECUTE FUNCTION public.prevent_audit_event_mutation(); + +INSERT INTO public.roles (name, display_name, description) +VALUES + ('administrator', 'Administrator', 'Full application and security administration.'), + ('operator', 'Operator', 'Day-to-day plate review, automation, and data management.'), + ('viewer', 'Viewer', 'Read-only access to plate data and approved exports.'), + ('auditor', 'Auditor', 'Read-only investigations with audit-history access.') +ON CONFLICT (name) DO UPDATE +SET display_name = EXCLUDED.display_name, + description = EXCLUDED.description; + +INSERT INTO public.permissions (permission_key, description) +VALUES + ('system.manage_users', 'Create, disable, delete, and assign roles to users.'), + ('system.manage_settings', 'Change application and integration settings.'), + ('system.view_audit', 'View append-only audit history.'), + ('plate.read', 'View plate reads, images, and known-plate details.'), + ('plate.review', 'Confirm, correct, or reject plate reads.'), + ('plate.delete', 'Delete plate reads and plate records.'), + ('known_plate.manage', 'Manage known plates and their notes.'), + ('tag.manage', 'Create, edit, assign, and remove tags.'), + ('notification.manage', 'Manage notification rules and delivery state.'), + ('mqtt.manage', 'Manage MQTT brokers, cameras, rules, and activity.'), + ('export.create', 'Create and download approved exports.'), + ('maintenance.manage', 'Run approved storage and database maintenance.') +ON CONFLICT (permission_key) DO UPDATE +SET description = EXCLUDED.description; + +-- Keep the durable database grants synchronized with the application role +-- matrix when an existing installation receives a least-privilege correction. +DELETE FROM public.role_permissions AS role_permission +USING public.roles AS role +WHERE role_permission.role_id = role.id + AND role.name IN ('administrator', 'operator', 'viewer', 'auditor'); + +INSERT INTO public.role_permissions (role_id, permission_id) +SELECT role.id, permission.id +FROM public.roles AS role +CROSS JOIN public.permissions AS permission +WHERE + role.name = 'administrator' + OR ( + role.name = 'operator' + AND permission.permission_key IN ( + 'plate.read', + 'plate.review', + 'known_plate.manage', + 'tag.manage' + ) + ) + OR ( + role.name = 'viewer' + AND permission.permission_key IN ( + 'plate.read' + ) + ) + OR ( + role.name = 'auditor' + AND permission.permission_key IN ( + 'plate.read', + 'system.view_audit', + 'export.create' + ) + ) +ON CONFLICT (role_id, permission_id) DO NOTHING; + +INSERT INTO public.schema_migrations (version, description) +VALUES ( + '2026071901_identity_audit_foundation', + 'Create users, roles, permissions, database sessions, scoped credentials, and append-only audit events.' +) +ON CONFLICT (version) DO NOTHING; diff --git a/migrations/0002_2026071902_assistant_authorization.sql b/migrations/0002_2026071902_assistant_authorization.sql new file mode 100644 index 0000000..2e271cd --- /dev/null +++ b/migrations/0002_2026071902_assistant_authorization.sql @@ -0,0 +1,7 @@ +-- migration: 2026071902_assistant_authorization +INSERT INTO public.schema_migrations (version, description) +VALUES ( + '2026071902_assistant_authorization', + 'Restrict AI Assistant access to explicitly authorized administrators.' +) +ON CONFLICT (version) DO NOTHING; diff --git a/migrations/0003_2026071903_immutable_plate_reviews.sql b/migrations/0003_2026071903_immutable_plate_reviews.sql new file mode 100644 index 0000000..cd6f3f0 --- /dev/null +++ b/migrations/0003_2026071903_immutable_plate_reviews.sql @@ -0,0 +1,193 @@ +-- migration: 2026071903_immutable_plate_reviews +-- Immutable plate observations, append-only review history, and reviewed aliases +ALTER TABLE public.plate_reads + ADD COLUMN IF NOT EXISTS observed_plate VARCHAR(10), + ADD COLUMN IF NOT EXISTS review_status VARCHAR(24) NOT NULL DEFAULT 'unreviewed', + ADD COLUMN IF NOT EXISTS review_revision INTEGER NOT NULL DEFAULT 0, + ADD COLUMN IF NOT EXISTS last_reviewed_at TIMESTAMPTZ, + ADD COLUMN IF NOT EXISTS last_reviewed_by BIGINT REFERENCES public.users(id) ON DELETE SET NULL; + +UPDATE public.plate_reads +SET observed_plate = plate_number +WHERE observed_plate IS NULL; + +UPDATE public.plate_reads +SET review_status = CASE WHEN validated THEN 'confirmed' ELSE 'unreviewed' END +WHERE review_revision = 0 + AND review_status = 'unreviewed'; + +ALTER TABLE public.plate_reads + ALTER COLUMN observed_plate SET NOT NULL; + +DO $$ +BEGIN + IF NOT EXISTS ( + SELECT 1 FROM pg_constraint + WHERE conname = 'plate_reads_review_status_check' + ) THEN + ALTER TABLE public.plate_reads + ADD CONSTRAINT plate_reads_review_status_check + CHECK (review_status IN ( + 'unreviewed', 'confirmed', 'corrected', 'rejected', 'alias_resolved' + )); + END IF; + IF NOT EXISTS ( + SELECT 1 FROM pg_constraint + WHERE conname = 'plate_reads_review_revision_check' + ) THEN + ALTER TABLE public.plate_reads + ADD CONSTRAINT plate_reads_review_revision_check + CHECK (review_revision >= 0); + END IF; +END; +$$; + +CREATE TABLE IF NOT EXISTS public.plate_aliases ( + id BIGSERIAL PRIMARY KEY, + source_plate VARCHAR(10) NOT NULL, + target_plate VARCHAR(10) NOT NULL, + camera_name VARCHAR(30), + enabled BOOLEAN NOT NULL DEFAULT TRUE, + reason VARCHAR(120) NOT NULL, + created_by_user_id BIGINT REFERENCES public.users(id) ON DELETE SET NULL, + created_by_username VARCHAR(64) NOT NULL, + created_by_display_name VARCHAR(120) NOT NULL, + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + disabled_by_user_id BIGINT REFERENCES public.users(id) ON DELETE SET NULL, + disabled_at TIMESTAMPTZ, + use_count BIGINT NOT NULL DEFAULT 0, + last_used_at TIMESTAMPTZ, + CONSTRAINT plate_aliases_different_values CHECK (source_plate <> target_plate), + CONSTRAINT plate_aliases_use_count_check CHECK (use_count >= 0) +); + +CREATE UNIQUE INDEX IF NOT EXISTS uq_plate_aliases_enabled_scope + ON public.plate_aliases ( + source_plate, + COALESCE(LOWER(camera_name), '') + ) + WHERE enabled = TRUE; + +CREATE INDEX IF NOT EXISTS idx_plate_aliases_target + ON public.plate_aliases (target_plate, enabled); + +ALTER TABLE public.plate_reads + ADD COLUMN IF NOT EXISTS applied_alias_id BIGINT + REFERENCES public.plate_aliases(id) ON DELETE SET NULL; + +CREATE TABLE IF NOT EXISTS public.plate_review_batches ( + id BIGSERIAL PRIMARY KEY, + source_plate VARCHAR(10) NOT NULL, + target_plate VARCHAR(10) NOT NULL, + criteria JSONB NOT NULL DEFAULT '{}'::JSONB + CHECK (jsonb_typeof(criteria) = 'object'), + matched_count INTEGER NOT NULL CHECK (matched_count > 0), + actor_user_id BIGINT REFERENCES public.users(id) ON DELETE SET NULL, + actor_username VARCHAR(64) NOT NULL, + actor_display_name VARCHAR(120) NOT NULL, + reason VARCHAR(120) NOT NULL, + notes TEXT, + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP +); + +CREATE TABLE IF NOT EXISTS public.plate_read_reviews ( + id BIGSERIAL PRIMARY KEY, + read_id INTEGER REFERENCES public.plate_reads(id) ON DELETE RESTRICT, + read_event_identity VARCHAR(80), + action VARCHAR(24) NOT NULL + CHECK (action IN ( + 'confirm', 'correct', 'reject', 'reopen', 'reverse', 'alias_applied' + )), + previous_plate VARCHAR(10) NOT NULL, + new_plate VARCHAR(10) NOT NULL, + previous_status VARCHAR(24) NOT NULL + CHECK (previous_status IN ( + 'unreviewed', 'confirmed', 'corrected', 'rejected', 'alias_resolved' + )), + new_status VARCHAR(24) NOT NULL + CHECK (new_status IN ( + 'unreviewed', 'confirmed', 'corrected', 'rejected', 'alias_resolved' + )), + reason VARCHAR(120), + notes TEXT, + actor_user_id BIGINT REFERENCES public.users(id) ON DELETE SET NULL, + actor_username VARCHAR(64) NOT NULL, + actor_display_name VARCHAR(120) NOT NULL, + alias_id BIGINT REFERENCES public.plate_aliases(id) ON DELETE SET NULL, + batch_id BIGINT REFERENCES public.plate_review_batches(id) ON DELETE SET NULL, + reverses_review_id BIGINT REFERENCES public.plate_read_reviews(id) ON DELETE SET NULL, + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP +); + +CREATE INDEX IF NOT EXISTS idx_plate_read_reviews_read + ON public.plate_read_reviews (read_id, created_at DESC, id DESC); +CREATE INDEX IF NOT EXISTS idx_plate_read_reviews_actor + ON public.plate_read_reviews (actor_user_id, created_at DESC) + WHERE actor_user_id IS NOT NULL; +CREATE INDEX IF NOT EXISTS idx_plate_read_reviews_batch + ON public.plate_read_reviews (batch_id) + WHERE batch_id IS NOT NULL; + +CREATE OR REPLACE FUNCTION public.preserve_observed_plate() +RETURNS TRIGGER AS $$ +BEGIN + IF TG_OP = 'INSERT' AND NEW.observed_plate IS NULL THEN + NEW.observed_plate = NEW.plate_number; + ELSIF TG_OP = 'UPDATE' + AND OLD.observed_plate IS DISTINCT FROM NEW.observed_plate THEN + RAISE EXCEPTION 'plate_reads.observed_plate is immutable'; + END IF; + RETURN NEW; +END; +$$ LANGUAGE plpgsql; + +DROP TRIGGER IF EXISTS plate_reads_preserve_observed ON public.plate_reads; +CREATE TRIGGER plate_reads_preserve_observed +BEFORE INSERT OR UPDATE ON public.plate_reads +FOR EACH ROW EXECUTE FUNCTION public.preserve_observed_plate(); + +CREATE OR REPLACE FUNCTION public.prevent_plate_review_mutation() +RETURNS TRIGGER AS $$ +BEGIN + RAISE EXCEPTION 'plate_read_reviews is append-only'; +END; +$$ LANGUAGE plpgsql; + +DROP TRIGGER IF EXISTS plate_read_reviews_append_only ON public.plate_read_reviews; +CREATE TRIGGER plate_read_reviews_append_only +BEFORE UPDATE OR DELETE ON public.plate_read_reviews +FOR EACH ROW EXECUTE FUNCTION public.prevent_plate_review_mutation(); + +CREATE OR REPLACE FUNCTION public.prevent_plate_alias_delete() +RETURNS TRIGGER AS $$ +BEGIN + RAISE EXCEPTION 'plate aliases must be disabled, not deleted'; +END; +$$ LANGUAGE plpgsql; + +DROP TRIGGER IF EXISTS plate_aliases_no_delete ON public.plate_aliases; +CREATE TRIGGER plate_aliases_no_delete +BEFORE DELETE ON public.plate_aliases +FOR EACH ROW EXECUTE FUNCTION public.prevent_plate_alias_delete(); + +INSERT INTO public.permissions (permission_key, description) +VALUES + ('plate.review.batch', 'Preview and apply reviewed bulk plate corrections.'), + ('plate.alias.manage', 'Create and disable recurring plate misread aliases.') +ON CONFLICT (permission_key) DO UPDATE +SET description = EXCLUDED.description; + +INSERT INTO public.role_permissions (role_id, permission_id) +SELECT role.id, permission.id +FROM public.roles AS role +CROSS JOIN public.permissions AS permission +WHERE role.name = 'administrator' + AND permission.permission_key IN ('plate.review.batch', 'plate.alias.manage') +ON CONFLICT (role_id, permission_id) DO NOTHING; + +INSERT INTO public.schema_migrations (version, description) +VALUES ( + '2026071903_immutable_plate_reviews', + 'Preserve observed plates, append plate review history, and add reviewed recurring aliases.' +) +ON CONFLICT (version) DO NOTHING; diff --git a/migrations/0004_2026072101_repair_plate_occurrence_counts.sql b/migrations/0004_2026072101_repair_plate_occurrence_counts.sql new file mode 100644 index 0000000..951a881 --- /dev/null +++ b/migrations/0004_2026072101_repair_plate_occurrence_counts.sql @@ -0,0 +1,62 @@ +-- migration: 2026072101_repair_plate_occurrence_counts +-- Reconcile stored plate occurrence counts after bulk imports. +DO $$ +DECLARE + mismatch_count BIGINT; +BEGIN + IF NOT EXISTS ( + SELECT 1 + FROM public.schema_migrations + WHERE version = '2026072101_repair_plate_occurrence_counts' + ) THEN + WITH actual_counts AS ( + SELECT plate_number, COUNT(*)::INTEGER AS read_count + FROM public.plate_reads + GROUP BY plate_number + ) + UPDATE public.plates AS plate + SET occurrence_count = actual.read_count + FROM actual_counts AS actual + WHERE actual.plate_number = plate.plate_number + AND plate.occurrence_count IS DISTINCT FROM actual.read_count; + + UPDATE public.plates AS plate + SET occurrence_count = 0 + WHERE plate.occurrence_count <> 0 + AND NOT EXISTS ( + SELECT 1 + FROM public.plate_reads AS read + WHERE read.plate_number = plate.plate_number + ); + + SELECT COUNT(*) + INTO mismatch_count + FROM ( + SELECT + COALESCE(plate.plate_number, actual.plate_number) AS plate_number, + plate.occurrence_count, + COALESCE(actual.read_count, 0) AS read_count + FROM public.plates AS plate + FULL OUTER JOIN ( + SELECT plate_number, COUNT(*)::INTEGER AS read_count + FROM public.plate_reads + GROUP BY plate_number + ) AS actual + ON actual.plate_number = plate.plate_number + WHERE plate.plate_number IS NULL + OR plate.occurrence_count IS DISTINCT FROM COALESCE(actual.read_count, 0) + ) AS mismatches; + + IF mismatch_count <> 0 THEN + RAISE EXCEPTION + 'Plate occurrence reconciliation left % mismatched plate rows', + mismatch_count; + END IF; + + INSERT INTO public.schema_migrations (version, description) + VALUES ( + '2026072101_repair_plate_occurrence_counts', + 'Reconcile stored plate occurrence counts with imported plate reads.' + ); + END IF; +END $$; diff --git a/migrations/0005_2026072201_unified_notification_foundation.sql b/migrations/0005_2026072201_unified_notification_foundation.sql new file mode 100644 index 0000000..5cb788a --- /dev/null +++ b/migrations/0005_2026072201_unified_notification_foundation.sql @@ -0,0 +1,221 @@ +-- migration: 2026072201_unified_notification_foundation +-- Channel-neutral notification rule foundation ----------------------------- +-- This schema is intentionally inert until existing Pushover and MQTT paths +-- are migrated in a later release. New rules default disabled, and this +-- migration neither copies nor changes any existing notification behavior. +CREATE TABLE IF NOT EXISTS public.notification_rules ( + id BIGSERIAL PRIMARY KEY, + name VARCHAR(255) NOT NULL, + description TEXT, + enabled BOOLEAN NOT NULL DEFAULT FALSE, + event_type VARCHAR(50) NOT NULL DEFAULT 'plate_read.accepted' + CHECK (event_type IN ('plate_read.accepted', 'camera.activity_check')), + cooldown_seconds INTEGER NOT NULL DEFAULT 0 + CHECK (cooldown_seconds BETWEEN 0 AND 2678400), + version INTEGER NOT NULL DEFAULT 1 CHECK (version > 0), + created_by_user_id BIGINT REFERENCES public.users(id) ON DELETE SET NULL, + updated_by_user_id BIGINT REFERENCES public.users(id) ON DELETE SET NULL, + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + CONSTRAINT notification_rules_name_present + CHECK (NULLIF(BTRIM(name), '') IS NOT NULL) +); + +CREATE TABLE IF NOT EXISTS public.notification_condition_groups ( + id BIGSERIAL PRIMARY KEY, + rule_id BIGINT NOT NULL REFERENCES public.notification_rules(id) ON DELETE CASCADE, + parent_group_id BIGINT, + combinator VARCHAR(10) NOT NULL DEFAULT 'all' + CHECK (combinator IN ('all', 'any', 'not')), + negated BOOLEAN NOT NULL DEFAULT FALSE, + position INTEGER NOT NULL DEFAULT 0 CHECK (position >= 0), + UNIQUE (id, rule_id), + CONSTRAINT notification_condition_groups_parent_same_rule + FOREIGN KEY (parent_group_id, rule_id) + REFERENCES public.notification_condition_groups(id, rule_id) + ON DELETE CASCADE +); + +CREATE UNIQUE INDEX IF NOT EXISTS uq_notification_condition_groups_root + ON public.notification_condition_groups (rule_id) + WHERE parent_group_id IS NULL; +CREATE INDEX IF NOT EXISTS idx_notification_condition_groups_parent + ON public.notification_condition_groups (parent_group_id, position); + +CREATE TABLE IF NOT EXISTS public.notification_conditions ( + id BIGSERIAL PRIMARY KEY, + group_id BIGINT NOT NULL + REFERENCES public.notification_condition_groups(id) ON DELETE CASCADE, + condition_type VARCHAR(50) NOT NULL + CHECK (condition_type IN ( + 'always', + 'event_type', + 'plate_match', + 'camera', + 'known_plate', + 'known_name', + 'tag', + 'watchlist', + 'confidence', + 'read_count', + 'local_time_window' + )), + operator VARCHAR(30) NOT NULL, + operand JSONB NOT NULL DEFAULT '{}'::JSONB, + position INTEGER NOT NULL DEFAULT 0 CHECK (position >= 0), + CONSTRAINT notification_conditions_operand_object + CHECK (jsonb_typeof(operand) = 'object') +); + +CREATE INDEX IF NOT EXISTS idx_notification_conditions_group + ON public.notification_conditions (group_id, position, id); + +CREATE TABLE IF NOT EXISTS public.notification_channels ( + id BIGSERIAL PRIMARY KEY, + name VARCHAR(255) NOT NULL, + channel_type VARCHAR(30) NOT NULL + CHECK (channel_type IN ('pushover', 'mqtt', 'email', 'webhook')), + enabled BOOLEAN NOT NULL DEFAULT FALSE, + credential_reference VARCHAR(255), + configuration JSONB NOT NULL DEFAULT '{}'::JSONB, + created_by_user_id BIGINT REFERENCES public.users(id) ON DELETE SET NULL, + updated_by_user_id BIGINT REFERENCES public.users(id) ON DELETE SET NULL, + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + CONSTRAINT notification_channels_name_present + CHECK (NULLIF(BTRIM(name), '') IS NOT NULL), + CONSTRAINT notification_channels_configuration_object + CHECK (jsonb_typeof(configuration) = 'object') +); + +COMMENT ON COLUMN public.notification_channels.credential_reference IS + 'Reference to separately protected credentials; never store a secret in this field.'; + +CREATE TABLE IF NOT EXISTS public.notification_actions ( + id BIGSERIAL PRIMARY KEY, + rule_id BIGINT NOT NULL REFERENCES public.notification_rules(id) ON DELETE CASCADE, + channel_id BIGINT NOT NULL REFERENCES public.notification_channels(id) ON DELETE RESTRICT, + enabled BOOLEAN NOT NULL DEFAULT TRUE, + position INTEGER NOT NULL DEFAULT 0 CHECK (position >= 0), + configuration JSONB NOT NULL DEFAULT '{}'::JSONB, + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + UNIQUE (rule_id, position), + CONSTRAINT notification_actions_configuration_object + CHECK (jsonb_typeof(configuration) = 'object') +); + +CREATE INDEX IF NOT EXISTS idx_notification_actions_channel + ON public.notification_actions (channel_id); + +CREATE TABLE IF NOT EXISTS public.notification_executions ( + id BIGSERIAL PRIMARY KEY, + execution_key VARCHAR(100) NOT NULL UNIQUE, + event_id VARCHAR(255) NOT NULL, + event_type VARCHAR(50) NOT NULL, + read_id INTEGER REFERENCES public.plate_reads(id) ON DELETE SET NULL, + rule_id BIGINT NOT NULL REFERENCES public.notification_rules(id) ON DELETE RESTRICT, + rule_version INTEGER NOT NULL CHECK (rule_version > 0), + outcome VARCHAR(30) NOT NULL + CHECK (outcome IN ( + 'matched', 'not_matched', 'suppressed', 'disabled', + 'event_filtered', 'invalid', 'error' + )), + reason VARCHAR(100) NOT NULL, + decision JSONB NOT NULL DEFAULT '{}'::JSONB, + evaluated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + CONSTRAINT notification_executions_decision_object + CHECK (jsonb_typeof(decision) = 'object') +); + +CREATE INDEX IF NOT EXISTS idx_notification_executions_rule_activity + ON public.notification_executions (rule_id, evaluated_at DESC, id DESC); +CREATE INDEX IF NOT EXISTS idx_notification_executions_event + ON public.notification_executions (event_id, evaluated_at DESC); + +CREATE TABLE IF NOT EXISTS public.notification_deliveries ( + id BIGSERIAL PRIMARY KEY, + dedupe_key VARCHAR(100) NOT NULL UNIQUE, + execution_id BIGINT NOT NULL + REFERENCES public.notification_executions(id) ON DELETE CASCADE, + action_id BIGINT NOT NULL REFERENCES public.notification_actions(id) ON DELETE RESTRICT, + channel_id BIGINT NOT NULL REFERENCES public.notification_channels(id) ON DELETE RESTRICT, + payload JSONB NOT NULL DEFAULT '{}'::JSONB, + status VARCHAR(20) NOT NULL DEFAULT 'pending' + CHECK (status IN ('pending', 'processing', 'retry', 'succeeded', 'dead', 'cancelled')), + attempt_count INTEGER NOT NULL DEFAULT 0 CHECK (attempt_count >= 0), + max_attempts SMALLINT NOT NULL DEFAULT 5 CHECK (max_attempts BETWEEN 1 AND 20), + next_attempt_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + locked_at TIMESTAMPTZ, + locked_by VARCHAR(255), + last_error TEXT, + delivered_at TIMESTAMPTZ, + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + CONSTRAINT notification_deliveries_payload_object + CHECK (jsonb_typeof(payload) = 'object'), + CONSTRAINT notification_deliveries_lock_state CHECK ( + (status = 'processing' AND locked_at IS NOT NULL AND NULLIF(BTRIM(locked_by), '') IS NOT NULL) + OR (status <> 'processing' AND locked_at IS NULL AND locked_by IS NULL) + ), + CONSTRAINT notification_deliveries_delivered_state CHECK ( + (status = 'succeeded' AND delivered_at IS NOT NULL) + OR (status <> 'succeeded' AND delivered_at IS NULL) + ) +); + +CREATE INDEX IF NOT EXISTS idx_notification_deliveries_due + ON public.notification_deliveries (next_attempt_at, id) + WHERE status IN ('pending', 'retry'); +CREATE INDEX IF NOT EXISTS idx_notification_deliveries_activity + ON public.notification_deliveries (created_at DESC, id DESC); + +CREATE TABLE IF NOT EXISTS public.notification_delivery_attempts ( + id BIGSERIAL PRIMARY KEY, + delivery_id BIGINT NOT NULL + REFERENCES public.notification_deliveries(id) ON DELETE CASCADE, + attempt_number INTEGER NOT NULL CHECK (attempt_number > 0), + outcome VARCHAR(20) NOT NULL CHECK (outcome IN ('succeeded', 'failed')), + response JSONB NOT NULL DEFAULT '{}'::JSONB, + error TEXT, + started_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + completed_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + UNIQUE (delivery_id, attempt_number), + CONSTRAINT notification_delivery_attempts_response_object + CHECK (jsonb_typeof(response) = 'object') +); + +CREATE OR REPLACE FUNCTION public.notification_set_updated_at() +RETURNS TRIGGER AS $$ +BEGIN + NEW.updated_at = CURRENT_TIMESTAMP; + RETURN NEW; +END; +$$ LANGUAGE plpgsql; + +DROP TRIGGER IF EXISTS notification_rules_set_updated_at ON public.notification_rules; +CREATE TRIGGER notification_rules_set_updated_at +BEFORE UPDATE ON public.notification_rules +FOR EACH ROW EXECUTE FUNCTION public.notification_set_updated_at(); + +DROP TRIGGER IF EXISTS notification_channels_set_updated_at ON public.notification_channels; +CREATE TRIGGER notification_channels_set_updated_at +BEFORE UPDATE ON public.notification_channels +FOR EACH ROW EXECUTE FUNCTION public.notification_set_updated_at(); + +DROP TRIGGER IF EXISTS notification_actions_set_updated_at ON public.notification_actions; +CREATE TRIGGER notification_actions_set_updated_at +BEFORE UPDATE ON public.notification_actions +FOR EACH ROW EXECUTE FUNCTION public.notification_set_updated_at(); + +DROP TRIGGER IF EXISTS notification_deliveries_set_updated_at ON public.notification_deliveries; +CREATE TRIGGER notification_deliveries_set_updated_at +BEFORE UPDATE ON public.notification_deliveries +FOR EACH ROW EXECUTE FUNCTION public.notification_set_updated_at(); + +INSERT INTO public.schema_migrations (version, description) +VALUES ( + '2026072201_unified_notification_foundation', + 'Add inert channel-neutral rules, nested conditions, actions, executions, deliveries, and attempts.' +) +ON CONFLICT (version) DO NOTHING; diff --git a/migrations/0006_2026072202_notification_migration_preview.sql b/migrations/0006_2026072202_notification_migration_preview.sql new file mode 100644 index 0000000..75ac48b --- /dev/null +++ b/migrations/0006_2026072202_notification_migration_preview.sql @@ -0,0 +1,27 @@ +-- migration: 2026072202_notification_migration_preview +-- Extend the inert shared evaluator vocabulary for legacy known-name rules. +-- This still copies no rules and changes no active delivery path. +ALTER TABLE IF EXISTS public.notification_conditions + DROP CONSTRAINT IF EXISTS notification_conditions_condition_type_check; +ALTER TABLE IF EXISTS public.notification_conditions + ADD CONSTRAINT notification_conditions_condition_type_check + CHECK (condition_type IN ( + 'always', + 'event_type', + 'plate_match', + 'camera', + 'known_plate', + 'known_name', + 'tag', + 'watchlist', + 'confidence', + 'read_count', + 'local_time_window' + )); + +INSERT INTO public.schema_migrations (version, description) +VALUES ( + '2026072202_notification_migration_preview', + 'Add known-name rule vocabulary for the read-only legacy notification migration preview.' +) +ON CONFLICT (version) DO NOTHING; diff --git a/migrations/0007_2026072203_disabled_notification_rule_migration.sql b/migrations/0007_2026072203_disabled_notification_rule_migration.sql new file mode 100644 index 0000000..6f2492b --- /dev/null +++ b/migrations/0007_2026072203_disabled_notification_rule_migration.sql @@ -0,0 +1,25 @@ +-- migration: 2026072203_disabled_notification_rule_migration +-- Record each legacy rule copied into the inert unified model. The unique source +-- identity makes the application migration safe to retry, while the restricted +-- target reference preserves provenance for later review and cutover. +CREATE TABLE IF NOT EXISTS public.notification_rule_migrations ( + id BIGSERIAL PRIMARY KEY, + source_type VARCHAR(20) NOT NULL + CHECK (source_type IN ('pushover', 'mqtt')), + source_id BIGINT NOT NULL CHECK (source_id > 0), + target_rule_id BIGINT NOT NULL UNIQUE + REFERENCES public.notification_rules(id) ON DELETE RESTRICT, + applied_by_user_id BIGINT REFERENCES public.users(id) ON DELETE SET NULL, + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + UNIQUE (source_type, source_id) +); + +CREATE INDEX IF NOT EXISTS idx_notification_rule_migrations_created_at + ON public.notification_rule_migrations (created_at DESC, id DESC); + +INSERT INTO public.schema_migrations (version, description) +VALUES ( + '2026072203_disabled_notification_rule_migration', + 'Track idempotent disabled-only copies of legacy Pushover and MQTT rules.' +) +ON CONFLICT (version) DO NOTHING; diff --git a/migrations/0008_2026072204_notification_shadow_review.sql b/migrations/0008_2026072204_notification_shadow_review.sql new file mode 100644 index 0000000..bddafa8 --- /dev/null +++ b/migrations/0008_2026072204_notification_shadow_review.sql @@ -0,0 +1,40 @@ +-- migration: 2026072204_notification_shadow_review +-- Append-only administrator evidence for a specific disabled rule version and +-- exact shadow-test sample. Recording a review cannot enable or change a rule. +CREATE TABLE IF NOT EXISTS public.notification_rule_shadow_reviews ( + id BIGSERIAL PRIMARY KEY, + rule_id BIGINT NOT NULL + REFERENCES public.notification_rules(id) ON DELETE RESTRICT, + rule_version INTEGER NOT NULL CHECK (rule_version > 0), + reviewer_user_id BIGINT REFERENCES public.users(id) ON DELETE SET NULL, + sample_count INTEGER NOT NULL CHECK (sample_count > 0), + agreement_count INTEGER NOT NULL CHECK (agreement_count = sample_count), + mismatch_count INTEGER NOT NULL DEFAULT 0 CHECK (mismatch_count = 0), + report_fingerprint CHAR(64) NOT NULL + CHECK (report_fingerprint ~ '^[0-9a-f]{64}$'), + reviewed_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + UNIQUE (rule_id, rule_version, report_fingerprint) +); + +CREATE INDEX IF NOT EXISTS idx_notification_rule_shadow_reviews_activity + ON public.notification_rule_shadow_reviews (rule_id, reviewed_at DESC, id DESC); + +CREATE OR REPLACE FUNCTION public.prevent_notification_shadow_review_mutation() +RETURNS TRIGGER AS $$ +BEGIN + RAISE EXCEPTION 'notification_rule_shadow_reviews is append-only'; +END; +$$ LANGUAGE plpgsql; + +DROP TRIGGER IF EXISTS notification_rule_shadow_reviews_append_only + ON public.notification_rule_shadow_reviews; +CREATE TRIGGER notification_rule_shadow_reviews_append_only +BEFORE UPDATE OR DELETE ON public.notification_rule_shadow_reviews +FOR EACH ROW EXECUTE FUNCTION public.prevent_notification_shadow_review_mutation(); + +INSERT INTO public.schema_migrations (version, description) +VALUES ( + '2026072204_notification_shadow_review', + 'Add append-only approval evidence for safe disabled unified-rule shadow comparisons.' +) +ON CONFLICT (version) DO NOTHING; diff --git a/migrations/0009_2026072205_guarded_notification_cutover.sql b/migrations/0009_2026072205_guarded_notification_cutover.sql new file mode 100644 index 0000000..eae8ee1 --- /dev/null +++ b/migrations/0009_2026072205_guarded_notification_cutover.sql @@ -0,0 +1,40 @@ +-- migration: 2026072205_guarded_notification_cutover +-- Append-only history for explicit per-rule cutovers and rollbacks. The live +-- source/target enabled flags remain the source of truth; these rows preserve +-- who changed them, which reviewed version was involved, and why. +CREATE TABLE IF NOT EXISTS public.notification_rule_cutover_events ( + id BIGSERIAL PRIMARY KEY, + migration_id BIGINT NOT NULL + REFERENCES public.notification_rule_migrations(id) ON DELETE RESTRICT, + direction VARCHAR(20) NOT NULL + CHECK (direction IN ('cutover', 'rollback')), + rule_version INTEGER NOT NULL CHECK (rule_version > 0), + actor_user_id BIGINT REFERENCES public.users(id) ON DELETE SET NULL, + metadata JSONB NOT NULL DEFAULT '{}'::JSONB, + occurred_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + CONSTRAINT notification_rule_cutover_events_metadata_object + CHECK (jsonb_typeof(metadata) = 'object') +); + +CREATE INDEX IF NOT EXISTS idx_notification_rule_cutover_events_activity + ON public.notification_rule_cutover_events (migration_id, occurred_at DESC, id DESC); + +CREATE OR REPLACE FUNCTION public.prevent_notification_cutover_event_mutation() +RETURNS TRIGGER AS $$ +BEGIN + RAISE EXCEPTION 'notification_rule_cutover_events is append-only'; +END; +$$ LANGUAGE plpgsql; + +DROP TRIGGER IF EXISTS notification_rule_cutover_events_append_only + ON public.notification_rule_cutover_events; +CREATE TRIGGER notification_rule_cutover_events_append_only +BEFORE UPDATE OR DELETE ON public.notification_rule_cutover_events +FOR EACH ROW EXECUTE FUNCTION public.prevent_notification_cutover_event_mutation(); + +INSERT INTO public.schema_migrations (version, description) +VALUES ( + '2026072205_guarded_notification_cutover', + 'Add append-only evidence for guarded per-rule unified notification cutover and rollback.' +) +ON CONFLICT (version) DO NOTHING; diff --git a/migrations/0010_2026072401_monitored_plates_and_orphan_retirement.sql b/migrations/0010_2026072401_monitored_plates_and_orphan_retirement.sql new file mode 100644 index 0000000..b0abbf1 --- /dev/null +++ b/migrations/0010_2026072401_monitored_plates_and_orphan_retirement.sql @@ -0,0 +1,39 @@ +-- migration: 2026072401_monitored_plates_and_orphan_retirement +-- Preserve the legacy flagged boolean for evaluator compatibility while adding +-- user-facing monitoring context. Existing monitored plates receive a stable +-- timestamp and the normal priority; original reads and plate identities are +-- unchanged. +ALTER TABLE public.plates + ADD COLUMN IF NOT EXISTS monitor_reason TEXT, + ADD COLUMN IF NOT EXISTS monitor_priority VARCHAR(20) NOT NULL DEFAULT 'normal', + ADD COLUMN IF NOT EXISTS monitored_at TIMESTAMPTZ; + +ALTER TABLE public.plates + DROP CONSTRAINT IF EXISTS plates_monitor_priority_check; +ALTER TABLE public.plates + ADD CONSTRAINT plates_monitor_priority_check + CHECK (monitor_priority IN ('low', 'normal', 'high', 'critical')); + +UPDATE public.plates +SET monitored_at = COALESCE(monitored_at, CURRENT_TIMESTAMP) +WHERE flagged = TRUE AND monitored_at IS NULL; + +-- A removed legacy source can leave a safely disabled unified copy behind. +-- Retiring the mapping keeps the target disabled and preserves all rows and +-- evidence while removing it from active migration workflows. +ALTER TABLE public.notification_rule_migrations + ADD COLUMN IF NOT EXISTS retired_at TIMESTAMPTZ, + ADD COLUMN IF NOT EXISTS retired_by_user_id BIGINT + REFERENCES public.users(id) ON DELETE SET NULL, + ADD COLUMN IF NOT EXISTS retirement_reason TEXT; + +CREATE INDEX IF NOT EXISTS idx_notification_rule_migrations_active + ON public.notification_rule_migrations (source_type, source_id) + WHERE retired_at IS NULL; + +INSERT INTO public.schema_migrations (version, description) +VALUES ( + '2026072401_monitored_plates_and_orphan_retirement', + 'Add monitored-plate context and audited retirement for safely disabled orphaned notification migrations.' +) +ON CONFLICT (version) DO NOTHING; diff --git a/migrations/0011_2026072403_finalize_legacy_mqtt_rules.sql b/migrations/0011_2026072403_finalize_legacy_mqtt_rules.sql new file mode 100644 index 0000000..4d00c6e --- /dev/null +++ b/migrations/0011_2026072403_finalize_legacy_mqtt_rules.sql @@ -0,0 +1,39 @@ +-- migration: 2026072403_finalize_legacy_mqtt_rules +-- Finalization is the deliberate end of a legacy notification rule runtime. Preserve +-- an immutable, credential-free copy of each source rule before deleting it, +-- then remove the mapping from active migration/cutover workflows. Unified +-- rule delivery and its append-only cutover evidence remain authoritative. +ALTER TABLE public.notification_rule_migrations + ADD COLUMN IF NOT EXISTS legacy_snapshot JSONB, + ADD COLUMN IF NOT EXISTS finalized_at TIMESTAMPTZ, + ADD COLUMN IF NOT EXISTS finalized_by_user_id BIGINT + REFERENCES public.users(id) ON DELETE SET NULL, + ADD COLUMN IF NOT EXISTS finalization_reason TEXT; + +ALTER TABLE public.notification_rule_migrations + DROP CONSTRAINT IF EXISTS notification_rule_migrations_legacy_snapshot_object; +ALTER TABLE public.notification_rule_migrations + ADD CONSTRAINT notification_rule_migrations_legacy_snapshot_object + CHECK (legacy_snapshot IS NULL OR jsonb_typeof(legacy_snapshot) = 'object'); + +ALTER TABLE public.notification_rule_migrations + DROP CONSTRAINT IF EXISTS notification_rule_migrations_finalization_complete; +ALTER TABLE public.notification_rule_migrations + ADD CONSTRAINT notification_rule_migrations_finalization_complete + CHECK ( + (finalized_at IS NULL AND legacy_snapshot IS NULL AND finalization_reason IS NULL) + OR + (finalized_at IS NOT NULL AND legacy_snapshot IS NOT NULL + AND NULLIF(BTRIM(finalization_reason), '') IS NOT NULL) + ); + +CREATE INDEX IF NOT EXISTS idx_notification_rule_migrations_unfinalized + ON public.notification_rule_migrations (source_type, source_id) + WHERE retired_at IS NULL AND finalized_at IS NULL; + +INSERT INTO public.schema_migrations (version, description) +VALUES ( + '2026072403_finalize_legacy_mqtt_rules', + 'Archive verified cutover MQTT source rules and remove their legacy runtime rows.' +) +ON CONFLICT (version) DO NOTHING; diff --git a/migrations/0012_2026072404_finalize_legacy_pushover_rules.sql b/migrations/0012_2026072404_finalize_legacy_pushover_rules.sql new file mode 100644 index 0000000..9cf2fd3 --- /dev/null +++ b/migrations/0012_2026072404_finalize_legacy_pushover_rules.sql @@ -0,0 +1,9 @@ +-- migration: 2026072404_finalize_legacy_pushover_rules +-- The same verified-delivery boundary now retires legacy exact-plate Pushover +-- rows. No credentials are copied into the immutable source snapshot. +INSERT INTO public.schema_migrations (version, description) +VALUES ( + '2026072404_finalize_legacy_pushover_rules', + 'Extend verified legacy finalization and immutable snapshots to migrated Pushover rules.' +) +ON CONFLICT (version) DO NOTHING; diff --git a/migrations/0013_2026072206_disabled_rule_editor.sql b/migrations/0013_2026072206_disabled_rule_editor.sql new file mode 100644 index 0000000..24016b7 --- /dev/null +++ b/migrations/0013_2026072206_disabled_rule_editor.sql @@ -0,0 +1,58 @@ +-- migration: 2026072206_disabled_rule_editor +-- Disabled unified rules may intentionally expand beyond legacy behavior. +-- Evidence remains append-only and version/fingerprint bound; a regression +-- (legacy match lost by unified logic) can never use this approval mode. +ALTER TABLE public.notification_rule_shadow_reviews + ADD COLUMN IF NOT EXISTS approval_mode VARCHAR(30) NOT NULL DEFAULT 'parity'; +ALTER TABLE public.notification_rule_shadow_reviews + ADD COLUMN IF NOT EXISTS expansion_count INTEGER NOT NULL DEFAULT 0; +ALTER TABLE public.notification_rule_shadow_reviews + ADD COLUMN IF NOT EXISTS regression_count INTEGER NOT NULL DEFAULT 0; + +ALTER TABLE public.notification_rule_shadow_reviews + DROP CONSTRAINT IF EXISTS notification_rule_shadow_reviews_agreement_count_check; +ALTER TABLE public.notification_rule_shadow_reviews + DROP CONSTRAINT IF EXISTS notification_rule_shadow_reviews_mismatch_count_check; + +DO $$ +BEGIN + IF NOT EXISTS ( + SELECT 1 FROM pg_constraint + WHERE conname = 'notification_shadow_reviews_counts_valid' + AND conrelid = 'public.notification_rule_shadow_reviews'::regclass + ) THEN + ALTER TABLE public.notification_rule_shadow_reviews + ADD CONSTRAINT notification_shadow_reviews_counts_valid CHECK ( + agreement_count >= 0 + AND mismatch_count >= 0 + AND agreement_count + mismatch_count = sample_count + AND expansion_count >= 0 + AND regression_count >= 0 + AND expansion_count + regression_count = mismatch_count + ); + END IF; + IF NOT EXISTS ( + SELECT 1 FROM pg_constraint + WHERE conname = 'notification_shadow_reviews_mode_valid' + AND conrelid = 'public.notification_rule_shadow_reviews'::regclass + ) THEN + ALTER TABLE public.notification_rule_shadow_reviews + ADD CONSTRAINT notification_shadow_reviews_mode_valid CHECK ( + approval_mode IN ('parity', 'intentional_expansion') + AND ( + (approval_mode = 'parity' AND mismatch_count = 0) + OR + (approval_mode = 'intentional_expansion' + AND expansion_count > 0 + AND regression_count = 0) + ) + ); + END IF; +END $$; + +INSERT INTO public.schema_migrations (version, description) +VALUES ( + '2026072206_disabled_rule_editor', + 'Allow version-bound approval of intentional disabled-rule expansions without enabling delivery.' +) +ON CONFLICT (version) DO NOTHING; diff --git a/migrations/0014_2026072402_notification_operations.sql b/migrations/0014_2026072402_notification_operations.sql new file mode 100644 index 0000000..97887f4 --- /dev/null +++ b/migrations/0014_2026072402_notification_operations.sql @@ -0,0 +1,41 @@ +-- migration: 2026072402_notification_operations +-- Notification operations adds an explicit rule clock, optional quiet hours, +-- and lease-safe scheduled evaluation for camera activity rules. Existing +-- accepted-read rules retain their behavior and remain unscheduled. +ALTER TABLE public.notification_rules + ADD COLUMN IF NOT EXISTS time_zone VARCHAR(100) NOT NULL DEFAULT 'UTC', + ADD COLUMN IF NOT EXISTS quiet_hours JSONB NOT NULL DEFAULT '{"enabled":false}'::JSONB, + ADD COLUMN IF NOT EXISTS evaluation_interval_seconds INTEGER, + ADD COLUMN IF NOT EXISTS next_evaluation_at TIMESTAMPTZ, + ADD COLUMN IF NOT EXISTS evaluation_locked_at TIMESTAMPTZ, + ADD COLUMN IF NOT EXISTS evaluation_locked_by VARCHAR(255); + +ALTER TABLE public.notification_rules + DROP CONSTRAINT IF EXISTS notification_rules_quiet_hours_object; +ALTER TABLE public.notification_rules + ADD CONSTRAINT notification_rules_quiet_hours_object + CHECK (jsonb_typeof(quiet_hours) = 'object'); +ALTER TABLE public.notification_rules + DROP CONSTRAINT IF EXISTS notification_rules_evaluation_interval; +ALTER TABLE public.notification_rules + ADD CONSTRAINT notification_rules_evaluation_interval + CHECK (evaluation_interval_seconds IS NULL OR + evaluation_interval_seconds BETWEEN 60 AND 86400); +ALTER TABLE public.notification_rules + DROP CONSTRAINT IF EXISTS notification_rules_evaluation_lock_state; +ALTER TABLE public.notification_rules + ADD CONSTRAINT notification_rules_evaluation_lock_state CHECK ( + (evaluation_locked_at IS NULL AND evaluation_locked_by IS NULL) OR + (evaluation_locked_at IS NOT NULL AND NULLIF(BTRIM(evaluation_locked_by), '') IS NOT NULL) + ); + +CREATE INDEX IF NOT EXISTS idx_notification_rules_activity_due + ON public.notification_rules (next_evaluation_at, id) + WHERE enabled = TRUE AND event_type = 'camera.activity_check'; + +INSERT INTO public.schema_migrations (version, description) +VALUES ( + '2026072402_notification_operations', + 'Add explicit rule time zones, quiet hours, and lease-safe scheduled camera activity evaluation.' +) +ON CONFLICT (version) DO NOTHING; diff --git a/migrations/0015_2026072501_notification_rule_soft_delete.sql b/migrations/0015_2026072501_notification_rule_soft_delete.sql new file mode 100644 index 0000000..725f1eb --- /dev/null +++ b/migrations/0015_2026072501_notification_rule_soft_delete.sql @@ -0,0 +1,63 @@ +-- migration: 2026072501_notification_rule_soft_delete +-- Administrators can remove disabled rules from the active workspace without +-- breaking immutable execution, delivery, migration, or audit references. +ALTER TABLE public.notification_rules + ADD COLUMN IF NOT EXISTS deleted_at TIMESTAMPTZ, + ADD COLUMN IF NOT EXISTS deleted_by_user_id BIGINT + REFERENCES public.users(id) ON DELETE SET NULL, + ADD COLUMN IF NOT EXISTS deletion_reason VARCHAR(100); + +ALTER TABLE public.notification_rules + DROP CONSTRAINT IF EXISTS notification_rules_deleted_state; +ALTER TABLE public.notification_rules + ADD CONSTRAINT notification_rules_deleted_state CHECK ( + (deleted_at IS NULL AND deletion_reason IS NULL) OR + (deleted_at IS NOT NULL AND enabled = FALSE AND + NULLIF(BTRIM(deletion_reason), '') IS NOT NULL) + ); + +CREATE INDEX IF NOT EXISTS idx_notification_rules_visible + ON public.notification_rules (enabled DESC, updated_at DESC, id DESC) + WHERE deleted_at IS NULL; + +-- Legacy copies predate explicit rule clocks and inherited the old UTC schema +-- default. Only those tracked migration targets receive the configured local +-- timezone; independently created rules that intentionally use UTC are kept. +UPDATE public.notification_rules rule +SET time_zone = COALESCE(NULLIF(BTRIM(settings.local_timezone), ''), 'America/Denver'), + quiet_hours = CASE + WHEN COALESCE(rule.quiet_hours->>'timeZone', rule.quiet_hours->>'time_zone') = 'UTC' + THEN (rule.quiet_hours - 'time_zone') || jsonb_build_object( + 'timeZone', COALESCE(NULLIF(BTRIM(settings.local_timezone), ''), 'America/Denver') + ) + ELSE rule.quiet_hours + END, + version = rule.version + 1, + updated_at = CURRENT_TIMESTAMP +FROM public.mqtt_settings settings +WHERE settings.id = 1 + AND rule.time_zone = 'UTC' + AND EXISTS ( + SELECT 1 FROM public.notification_rule_migrations migration + WHERE migration.target_rule_id = rule.id + ); + +UPDATE public.notification_conditions condition +SET operand = (condition.operand - 'time_zone') || jsonb_build_object( + 'timeZone', COALESCE(NULLIF(BTRIM(settings.local_timezone), ''), 'America/Denver') +) +FROM public.notification_condition_groups condition_group, + public.notification_rule_migrations migration, + public.mqtt_settings settings +WHERE settings.id = 1 + AND condition.group_id = condition_group.id + AND migration.target_rule_id = condition_group.rule_id + AND condition.condition_type = 'local_time_window' + AND COALESCE(condition.operand->>'timeZone', condition.operand->>'time_zone') = 'UTC'; + +INSERT INTO public.schema_migrations (version, description) +VALUES ( + '2026072501_notification_rule_soft_delete', + 'Allow guarded deletion of disabled notification rules and repair inherited UTC defaults on legacy-migrated rules.' +) +ON CONFLICT (version) DO NOTHING; diff --git a/migrations/0016_2026072502_retention_maintenance_preview.sql b/migrations/0016_2026072502_retention_maintenance_preview.sql new file mode 100644 index 0000000..4995d45 --- /dev/null +++ b/migrations/0016_2026072502_retention_maintenance_preview.sql @@ -0,0 +1,34 @@ +-- migration: 2026072502_retention_maintenance_preview +-- Retention planning now runs outside ingestion. The initial maintenance job +-- is deliberately dry-run only: it records bounded database candidate counts +-- and never deletes rows or files. +CREATE TABLE IF NOT EXISTS public.maintenance_job_state ( + job_name VARCHAR(100) PRIMARY KEY, + enabled BOOLEAN NOT NULL DEFAULT TRUE, + mode VARCHAR(20) NOT NULL DEFAULT 'dry-run', + status VARCHAR(20) NOT NULL DEFAULT 'idle', + interval_seconds INTEGER NOT NULL DEFAULT 86400, + next_run_at TIMESTAMPTZ, + last_started_at TIMESTAMPTZ, + last_completed_at TIMESTAMPTZ, + last_result JSONB, + last_error TEXT, + updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + CONSTRAINT maintenance_job_mode CHECK (mode = 'dry-run'), + CONSTRAINT maintenance_job_status CHECK (status IN ('idle', 'running', 'failed')), + CONSTRAINT maintenance_job_interval CHECK (interval_seconds BETWEEN 3600 AND 604800), + CONSTRAINT maintenance_job_result_object CHECK ( + last_result IS NULL OR jsonb_typeof(last_result) = 'object' + ) +); + +CREATE INDEX IF NOT EXISTS idx_maintenance_job_due + ON public.maintenance_job_state (next_run_at, job_name) + WHERE enabled = TRUE; + +INSERT INTO public.schema_migrations (version, description) +VALUES ( + '2026072502_retention_maintenance_preview', + 'Move retention planning out of ingestion into a scheduled, single-flight, dry-run-only maintenance worker.' +) +ON CONFLICT (version) DO NOTHING; diff --git a/migrations/0017_2026072503_storage_reconciliation.sql b/migrations/0017_2026072503_storage_reconciliation.sql new file mode 100644 index 0000000..cf79204 --- /dev/null +++ b/migrations/0017_2026072503_storage_reconciliation.sql @@ -0,0 +1,80 @@ +-- migration: 2026072503_storage_reconciliation +-- Read-only storage reconciliation persists resumable traversal state and an +-- exact finding inventory. These tables do not provide any deletion action. +CREATE TABLE IF NOT EXISTS public.storage_reconciliation_runs ( + id BIGSERIAL PRIMARY KEY, + status VARCHAR(20) NOT NULL DEFAULT 'running', + phase VARCHAR(30) NOT NULL DEFAULT 'filesystem', + scan_started_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + completed_at TIMESTAMPTZ, + max_plate_read_id BIGINT NOT NULL DEFAULT 0, + plate_read_cursor BIGINT NOT NULL DEFAULT 0, + files_scanned BIGINT NOT NULL DEFAULT 0, + bytes_scanned BIGINT NOT NULL DEFAULT 0, + references_checked BIGINT NOT NULL DEFAULT 0, + recent_files_skipped BIGINT NOT NULL DEFAULT 0, + skipped_entries BIGINT NOT NULL DEFAULT 0, + error_count BIGINT NOT NULL DEFAULT 0, + orphan_files BIGINT NOT NULL DEFAULT 0, + orphan_bytes BIGINT NOT NULL DEFAULT 0, + missing_reference_paths BIGINT NOT NULL DEFAULT 0, + last_error TEXT, + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + CONSTRAINT storage_reconciliation_status CHECK (status IN ('running', 'completed', 'failed')), + CONSTRAINT storage_reconciliation_phase CHECK ( + phase IN ('filesystem', 'plate-reads', 'completed') + ), + CONSTRAINT storage_reconciliation_counts CHECK ( + max_plate_read_id >= 0 AND + plate_read_cursor >= 0 AND + files_scanned >= 0 AND bytes_scanned >= 0 AND references_checked >= 0 AND + recent_files_skipped >= 0 AND skipped_entries >= 0 AND error_count >= 0 AND + orphan_files >= 0 AND orphan_bytes >= 0 AND missing_reference_paths >= 0 + ) +); + +CREATE UNIQUE INDEX IF NOT EXISTS uq_storage_reconciliation_running + ON public.storage_reconciliation_runs (status) + WHERE status = 'running'; + +CREATE TABLE IF NOT EXISTS public.storage_reconciliation_directories ( + id BIGSERIAL PRIMARY KEY, + run_id BIGINT NOT NULL REFERENCES public.storage_reconciliation_runs(id) ON DELETE CASCADE, + relative_path VARCHAR(2048) NOT NULL, + cursor_name VARCHAR(255), + completed BOOLEAN NOT NULL DEFAULT FALSE, + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + UNIQUE (run_id, relative_path) +); + +CREATE INDEX IF NOT EXISTS idx_storage_reconciliation_directories_due + ON public.storage_reconciliation_directories (run_id, completed, relative_path); + +CREATE TABLE IF NOT EXISTS public.storage_reconciliation_items ( + id BIGSERIAL PRIMARY KEY, + run_id BIGINT NOT NULL REFERENCES public.storage_reconciliation_runs(id) ON DELETE CASCADE, + finding_type VARCHAR(30) NOT NULL, + relative_path VARCHAR(2048) NOT NULL, + size_bytes BIGINT, + modified_at TIMESTAMPTZ, + reference_type VARCHAR(80), + owner_id BIGINT, + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + CONSTRAINT storage_reconciliation_finding_type CHECK ( + finding_type IN ('orphan-file', 'missing-reference') + ), + CONSTRAINT storage_reconciliation_item_size CHECK (size_bytes IS NULL OR size_bytes >= 0), + UNIQUE (run_id, finding_type, relative_path) +); + +CREATE INDEX IF NOT EXISTS idx_storage_reconciliation_items_review + ON public.storage_reconciliation_items (run_id, finding_type, relative_path); + +INSERT INTO public.schema_migrations (version, description) +VALUES ( + '2026072503_storage_reconciliation', + 'Add bounded resumable read-only storage reconciliation runs and durable orphan/missing-reference inventory.' +) +ON CONFLICT (version) DO NOTHING; diff --git a/migrations/0018_2026072504_vehicle_direction_profiles.sql b/migrations/0018_2026072504_vehicle_direction_profiles.sql new file mode 100644 index 0000000..44d830f --- /dev/null +++ b/migrations/0018_2026072504_vehicle_direction_profiles.sql @@ -0,0 +1,77 @@ +-- migration: 2026072504_vehicle_direction_profiles +-- Recover a reconciliation run that failed before automatic retry scheduling +-- was available. Future failures set this bounded retry directly at runtime. +UPDATE public.maintenance_job_state +SET next_run_at = LEAST( + COALESCE(next_run_at, CURRENT_TIMESTAMP + INTERVAL '1 minute'), + CURRENT_TIMESTAMP + INTERVAL '1 minute' + ), + updated_at = CURRENT_TIMESTAMP +WHERE job_name = 'storage-reconciliation' + AND status = 'failed'; + +-- Camera direction is administrator-defined rather than inferred from a +-- camera name. Human front/rear labels calibrate the existing local Vehicle +-- ReID descriptor for each camera; low-confidence results remain unknown. +CREATE TABLE IF NOT EXISTS public.camera_direction_profiles ( + camera_key VARCHAR(100) PRIMARY KEY, + camera_name VARCHAR(100) NOT NULL, + enabled BOOLEAN NOT NULL DEFAULT TRUE, + front_direction_label VARCHAR(80) NOT NULL, + rear_direction_label VARCHAR(80) NOT NULL, + minimum_confidence REAL NOT NULL DEFAULT 0.68 + CHECK (minimum_confidence BETWEEN 0.5 AND 0.95), + profile_version INTEGER NOT NULL DEFAULT 1 CHECK (profile_version > 0), + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + CONSTRAINT camera_direction_labels_differ CHECK ( + LOWER(BTRIM(front_direction_label)) <> LOWER(BTRIM(rear_direction_label)) + ) +); + +CREATE TABLE IF NOT EXISTS public.vehicle_orientation_labels ( + id BIGSERIAL PRIMARY KEY, + read_id INTEGER NOT NULL REFERENCES public.plate_reads(id) ON DELETE CASCADE, + camera_key VARCHAR(100) NOT NULL, + embedding_model VARCHAR(80) NOT NULL, + orientation VARCHAR(10) NOT NULL CHECK (orientation IN ('front', 'rear')), + actor_user_id BIGINT REFERENCES public.users(id) ON DELETE SET NULL, + actor_username VARCHAR(64) NOT NULL, + actor_display_name VARCHAR(120) NOT NULL, + revision INTEGER NOT NULL DEFAULT 1 CHECK (revision > 0), + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + UNIQUE (read_id, embedding_model) +); + +CREATE INDEX IF NOT EXISTS idx_vehicle_orientation_labels_camera + ON public.vehicle_orientation_labels (camera_key, embedding_model, orientation, updated_at DESC); + +CREATE TABLE IF NOT EXISTS public.vehicle_direction_observations ( + read_id INTEGER PRIMARY KEY REFERENCES public.plate_reads(id) ON DELETE CASCADE, + camera_key VARCHAR(100) NOT NULL, + embedding_model VARCHAR(80) NOT NULL, + classifier_version VARCHAR(80) NOT NULL, + profile_version INTEGER NOT NULL CHECK (profile_version > 0), + status VARCHAR(20) NOT NULL CHECK (status IN ('collecting', 'ready', 'unknown')), + orientation VARCHAR(10) NOT NULL CHECK (orientation IN ('front', 'rear', 'unknown')), + orientation_confidence REAL CHECK (orientation_confidence BETWEEN 0 AND 1), + direction_label VARCHAR(80), + sample_counts JSONB NOT NULL DEFAULT '{"front":0,"rear":0}'::JSONB, + evaluated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + CONSTRAINT vehicle_direction_ready_state CHECK ( + (status = 'ready' AND orientation IN ('front', 'rear') AND + orientation_confidence IS NOT NULL AND direction_label IS NOT NULL) OR + (status <> 'ready' AND orientation = 'unknown' AND direction_label IS NULL) + ) +); + +CREATE INDEX IF NOT EXISTS idx_vehicle_direction_observations_camera + ON public.vehicle_direction_observations (camera_key, status, direction_label, evaluated_at DESC); + +INSERT INTO public.schema_migrations (version, description) +VALUES ( + '2026072504_vehicle_direction_profiles', + 'Add configurable per-camera direction meanings and audited ReID-assisted front/rear calibration.' +) +ON CONFLICT (version) DO NOTHING; diff --git a/migrations/0019_2026072601_vehicle_direction_notifications.sql b/migrations/0019_2026072601_vehicle_direction_notifications.sql new file mode 100644 index 0000000..979fecc --- /dev/null +++ b/migrations/0019_2026072601_vehicle_direction_notifications.sql @@ -0,0 +1,38 @@ +-- migration: 2026072601_vehicle_direction_notifications +-- Direction classification is emitted after Vehicle ReID completes, so it has +-- a distinct event type and can be filtered using camera-configured labels. +ALTER TABLE IF EXISTS public.notification_rules + DROP CONSTRAINT IF EXISTS notification_rules_event_type_check; +ALTER TABLE IF EXISTS public.notification_rules + ADD CONSTRAINT notification_rules_event_type_check + CHECK (event_type IN ( + 'plate_read.accepted', + 'vehicle.direction_classified', + 'camera.activity_check' + )); + +ALTER TABLE IF EXISTS public.notification_conditions + DROP CONSTRAINT IF EXISTS notification_conditions_condition_type_check; +ALTER TABLE IF EXISTS public.notification_conditions + ADD CONSTRAINT notification_conditions_condition_type_check + CHECK (condition_type IN ( + 'always', + 'event_type', + 'plate_match', + 'camera', + 'direction', + 'known_plate', + 'known_name', + 'tag', + 'watchlist', + 'confidence', + 'read_count', + 'local_time_window' + )); + +INSERT INTO public.schema_migrations (version, description) +VALUES ( + '2026072601_vehicle_direction_notifications', + 'Add direction-classified notification events and camera-configured direction conditions.' +) +ON CONFLICT (version) DO NOTHING; diff --git a/migrations/0020_2026072603_vehicle_direction_backfill.sql b/migrations/0020_2026072603_vehicle_direction_backfill.sql new file mode 100644 index 0000000..ffc5c3f --- /dev/null +++ b/migrations/0020_2026072603_vehicle_direction_backfill.sql @@ -0,0 +1,27 @@ +-- migration: 2026072603_vehicle_direction_backfill +-- Historical direction work is derived from current canonical crop evidence and is +-- naturally resumable: current observations are skipped, while repeat +-- failures are retained for review instead of blocking the remaining corpus. +CREATE TABLE IF NOT EXISTS public.vehicle_direction_backfill_failures ( + read_id INTEGER PRIMARY KEY REFERENCES public.plate_reads(id) ON DELETE CASCADE, + embedding_model VARCHAR(80) NOT NULL, + classifier_version VARCHAR(80) NOT NULL, + profile_version INTEGER NOT NULL CHECK (profile_version > 0), + attempt_count INTEGER NOT NULL DEFAULT 1 CHECK (attempt_count > 0), + error_code VARCHAR(80) NOT NULL, + error_message VARCHAR(500) NOT NULL, + first_failed_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + last_failed_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP +); + +CREATE INDEX IF NOT EXISTS idx_vehicle_direction_backfill_failures_retry + ON public.vehicle_direction_backfill_failures ( + embedding_model, classifier_version, profile_version, attempt_count, last_failed_at + ); + +INSERT INTO public.schema_migrations (version, description) +VALUES ( + '2026072603_vehicle_direction_backfill', + 'Add paced resumable historical direction backfill with bounded failure tracking.' +) +ON CONFLICT (version) DO NOTHING; diff --git a/migrations/0021_2026072701_vehicle_direction_reevaluation_queue.sql b/migrations/0021_2026072701_vehicle_direction_reevaluation_queue.sql new file mode 100644 index 0000000..bbb1b9a --- /dev/null +++ b/migrations/0021_2026072701_vehicle_direction_reevaluation_queue.sql @@ -0,0 +1,32 @@ +-- migration: 2026072701_vehicle_direction_reevaluation_queue +-- Historical re-evaluation is queued separately from ordinary live direction +-- work. Existing observations remain visible until their replacement is ready, +-- and an administrator can pause only the historical queue without delaying +-- newly ingested reads. +CREATE TABLE IF NOT EXISTS public.vehicle_direction_reevaluation_queue ( + read_id INTEGER PRIMARY KEY REFERENCES public.plate_reads(id) ON DELETE CASCADE, + camera_key VARCHAR(120) NOT NULL, + requested_by_user_id BIGINT REFERENCES public.users(id) ON DELETE SET NULL, + requested_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP +); + +CREATE INDEX IF NOT EXISTS idx_vehicle_direction_reevaluation_queue_order + ON public.vehicle_direction_reevaluation_queue (requested_at, read_id); + +CREATE TABLE IF NOT EXISTS public.vehicle_direction_reevaluation_control ( + singleton BOOLEAN PRIMARY KEY DEFAULT TRUE CHECK (singleton = TRUE), + paused BOOLEAN NOT NULL DEFAULT FALSE, + updated_by_user_id BIGINT REFERENCES public.users(id) ON DELETE SET NULL, + updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP +); + +INSERT INTO public.vehicle_direction_reevaluation_control (singleton, paused) +VALUES (TRUE, FALSE) +ON CONFLICT (singleton) DO NOTHING; + +INSERT INTO public.schema_migrations (version, description) +VALUES ( + '2026072701_vehicle_direction_reevaluation_queue', + 'Preserve current directions during re-evaluation and add durable pause/resume controls.' +) +ON CONFLICT (version) DO NOTHING; diff --git a/migrations/0022_2026072703_notification_action_history.sql b/migrations/0022_2026072703_notification_action_history.sql new file mode 100644 index 0000000..302c297 --- /dev/null +++ b/migrations/0022_2026072703_notification_action_history.sql @@ -0,0 +1,28 @@ +-- migration: 2026072703_notification_action_history +-- Editing a disabled notification rule must not erase the action identity +-- retained by delivery history. Keep prior versions as inert historical rows +-- and reserve each position only within the current, non-retired action set. +ALTER TABLE IF EXISTS public.notification_actions + ADD COLUMN IF NOT EXISTS retired_at TIMESTAMPTZ, + ADD COLUMN IF NOT EXISTS retired_by_user_id BIGINT + REFERENCES public.users(id) ON DELETE SET NULL; + +ALTER TABLE IF EXISTS public.notification_actions + DROP CONSTRAINT IF EXISTS notification_actions_rule_id_position_key; + +CREATE UNIQUE INDEX IF NOT EXISTS uq_notification_actions_active_position + ON public.notification_actions (rule_id, position) + WHERE retired_at IS NULL; + +CREATE INDEX IF NOT EXISTS idx_notification_actions_retired + ON public.notification_actions (rule_id, retired_at, id); + +COMMENT ON COLUMN public.notification_actions.retired_at IS + 'Set when an edited rule replaces this action; delivery history continues to reference the retired row.'; + +INSERT INTO public.schema_migrations (version, description) +VALUES ( + '2026072703_notification_action_history', + 'Preserve delivered notification action identities when disabled rules are edited.' +) +ON CONFLICT (version) DO NOTHING; diff --git a/migrations/0023_2026072704_blue_iris_alert_correlation.sql b/migrations/0023_2026072704_blue_iris_alert_correlation.sql new file mode 100644 index 0000000..abc6917 --- /dev/null +++ b/migrations/0023_2026072704_blue_iris_alert_correlation.sql @@ -0,0 +1,19 @@ +-- migration: 2026072704_blue_iris_alert_correlation +-- Preserve the Blue Iris alert pointer received with new plate reads. This is +-- metadata only: the continuous BVR recording remains managed by Blue Iris. +-- Historical reads can be correlated through the read-only alertlist API. +ALTER TABLE IF EXISTS public.plate_reads + ADD COLUMN IF NOT EXISTS bi_alert_clip TEXT, + ADD COLUMN IF NOT EXISTS bi_alert_path TEXT, + ADD COLUMN IF NOT EXISTS bi_alert_offset_ms BIGINT; + +CREATE INDEX IF NOT EXISTS idx_plate_reads_bi_alert_clip + ON public.plate_reads (bi_alert_clip) + WHERE bi_alert_clip IS NOT NULL; + +INSERT INTO public.schema_migrations (version, description) +VALUES ( + '2026072704_blue_iris_alert_correlation', + 'Preserve Blue Iris alert clip and offset metadata for read-only continuous-recording correlation.' +) +ON CONFLICT (version) DO NOTHING; diff --git a/migrations/0024_2026072801_blue_iris_vehicle_frames.sql b/migrations/0024_2026072801_blue_iris_vehicle_frames.sql new file mode 100644 index 0000000..6f54648 --- /dev/null +++ b/migrations/0024_2026072801_blue_iris_vehicle_frames.sql @@ -0,0 +1,33 @@ +-- migration: 2026072801_blue_iris_vehicle_frames +-- Keep at most one derived Blue Iris vehicle-overview frame per plate read. +-- The source BVR remains in Blue Iris; only the best bounded sample is retained. +ALTER TABLE IF EXISTS public.plate_reads + ADD COLUMN IF NOT EXISTS vehicle_image_status VARCHAR(20), + ADD COLUMN IF NOT EXISTS vehicle_image_path TEXT, + ADD COLUMN IF NOT EXISTS vehicle_image_timestamp TIMESTAMPTZ, + ADD COLUMN IF NOT EXISTS vehicle_image_score REAL, + ADD COLUMN IF NOT EXISTS vehicle_image_detection_confidence REAL, + ADD COLUMN IF NOT EXISTS vehicle_image_detection_box JSONB, + ADD COLUMN IF NOT EXISTS vehicle_image_width INTEGER, + ADD COLUMN IF NOT EXISTS vehicle_image_height INTEGER, + ADD COLUMN IF NOT EXISTS vehicle_image_sampled_count SMALLINT, + ADD COLUMN IF NOT EXISTS vehicle_image_error_code VARCHAR(80), + ADD COLUMN IF NOT EXISTS vehicle_image_retryable BOOLEAN NOT NULL DEFAULT TRUE, + ADD COLUMN IF NOT EXISTS vehicle_image_updated_at TIMESTAMPTZ; + +ALTER TABLE IF EXISTS public.plate_reads + DROP CONSTRAINT IF EXISTS plate_reads_vehicle_image_status_check; +ALTER TABLE IF EXISTS public.plate_reads + ADD CONSTRAINT plate_reads_vehicle_image_status_check + CHECK (vehicle_image_status IS NULL OR vehicle_image_status IN ('pending', 'processing', 'ready', 'unavailable', 'failed')); + +CREATE INDEX IF NOT EXISTS idx_plate_reads_vehicle_image_work + ON public.plate_reads (vehicle_image_status, vehicle_image_retryable, vehicle_image_updated_at, id) + WHERE vehicle_image_status IS NOT NULL AND vehicle_image_status <> 'ready'; + +INSERT INTO public.schema_migrations (version, description) +VALUES ( + '2026072801_blue_iris_vehicle_frames', + 'Retain one best derived Blue Iris vehicle-overview frame per ALPR read with terminal retention-aware states.' +) +ON CONFLICT (version) DO NOTHING; diff --git a/migrations/0025_2026072802_blue_iris_vehicle_frame_queue.sql b/migrations/0025_2026072802_blue_iris_vehicle_frame_queue.sql new file mode 100644 index 0000000..8111cdb --- /dev/null +++ b/migrations/0025_2026072802_blue_iris_vehicle_frame_queue.sql @@ -0,0 +1,44 @@ +-- migration: 2026072802_blue_iris_vehicle_frame_queue +-- New accepted reads are processed automatically while historical work is +-- explicitly queued and can be paused independently. A short processing lease +-- lets work recover safely after an application restart. +ALTER TABLE IF EXISTS public.plate_reads + ADD COLUMN IF NOT EXISTS vehicle_image_queue_kind VARCHAR(20), + ADD COLUMN IF NOT EXISTS vehicle_image_attempt_count SMALLINT NOT NULL DEFAULT 0; + +ALTER TABLE IF EXISTS public.plate_reads + DROP CONSTRAINT IF EXISTS plate_reads_vehicle_image_status_check; +ALTER TABLE IF EXISTS public.plate_reads + ADD CONSTRAINT plate_reads_vehicle_image_status_check + CHECK (vehicle_image_status IS NULL OR vehicle_image_status IN ('pending', 'processing', 'ready', 'unavailable', 'failed')); + +ALTER TABLE IF EXISTS public.plate_reads + DROP CONSTRAINT IF EXISTS plate_reads_vehicle_image_queue_kind_check; +ALTER TABLE IF EXISTS public.plate_reads + ADD CONSTRAINT plate_reads_vehicle_image_queue_kind_check + CHECK (vehicle_image_queue_kind IS NULL OR vehicle_image_queue_kind IN ('live', 'historical', 'manual', 'overview', 'overview_backfill', 'overview_repair')); + +CREATE TABLE IF NOT EXISTS public.vehicle_frame_processing_control ( + singleton BOOLEAN PRIMARY KEY DEFAULT TRUE CHECK (singleton), + historical_paused BOOLEAN NOT NULL DEFAULT TRUE, + updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP +); + +INSERT INTO public.vehicle_frame_processing_control (singleton, historical_paused) +VALUES (TRUE, TRUE) +ON CONFLICT (singleton) DO NOTHING; + +DROP INDEX IF EXISTS public.idx_plate_reads_vehicle_image_work; +CREATE INDEX IF NOT EXISTS idx_plate_reads_vehicle_image_work + ON public.plate_reads ( + vehicle_image_status, vehicle_image_queue_kind, + vehicle_image_retryable, vehicle_image_updated_at, id + ) + WHERE vehicle_image_path IS NULL; + +INSERT INTO public.schema_migrations (version, description) +VALUES ( + '2026072802_blue_iris_vehicle_frame_queue', + 'Automatically process live Blue Iris vehicle frames with durable retries and controlled historical backfill.' +) +ON CONFLICT (version) DO NOTHING; diff --git a/migrations/0026_2026072803_blue_iris_vehicle_frame_quality.sql b/migrations/0026_2026072803_blue_iris_vehicle_frame_quality.sql new file mode 100644 index 0000000..c20b316 --- /dev/null +++ b/migrations/0026_2026072803_blue_iris_vehicle_frame_quality.sql @@ -0,0 +1,12 @@ +-- migration: 2026072803_blue_iris_vehicle_frame_quality +-- Explain why the quality-aware multiframe selector chose a particular +-- vehicle view without retaining the other sampled images or Blue Iris video. +ALTER TABLE IF EXISTS public.plate_reads + ADD COLUMN IF NOT EXISTS vehicle_image_selection_metadata JSONB; + +INSERT INTO public.schema_migrations (version, description) +VALUES ( + '2026072803_blue_iris_vehicle_frame_quality', + 'Record bounded quality, tracking, and timing diagnostics for the selected Blue Iris vehicle frame.' +) +ON CONFLICT (version) DO NOTHING; diff --git a/migrations/0027_2026072901_storage_monitoring_maintenance.sql b/migrations/0027_2026072901_storage_monitoring_maintenance.sql new file mode 100644 index 0000000..d9c03f2 --- /dev/null +++ b/migrations/0027_2026072901_storage_monitoring_maintenance.sql @@ -0,0 +1,242 @@ +-- migration: 2026072901_storage_monitoring_maintenance +-- Phase 1 storage monitoring and guarded maintenance. Automatic cleanup is +-- deliberately disabled and has no approved categories. The only destructive +-- operation represented by this schema is a one-time, confirmed manual run +-- over a frozen preview of reconciliation-confirmed derived-file orphans. +CREATE TABLE IF NOT EXISTS public.storage_maintenance_config ( + singleton BOOLEAN PRIMARY KEY DEFAULT TRUE CHECK (singleton), + warning_percent NUMERIC(5,2) NOT NULL DEFAULT 80, + critical_percent NUMERIC(5,2) NOT NULL DEFAULT 90, + check_interval_seconds INTEGER NOT NULL DEFAULT 3600, + stale_after_seconds INTEGER NOT NULL DEFAULT 10800, + alert_cooldown_seconds INTEGER NOT NULL DEFAULT 21600, + email_enabled BOOLEAN NOT NULL DEFAULT FALSE, + email_recipients JSONB NOT NULL DEFAULT '[]'::JSONB, + webhook_enabled BOOLEAN NOT NULL DEFAULT FALSE, + webhook_url TEXT, + cleanup_enabled BOOLEAN NOT NULL DEFAULT FALSE, + cleanup_interval_seconds INTEGER NOT NULL DEFAULT 86400, + automatic_categories JSONB NOT NULL DEFAULT '[]'::JSONB, + orphan_grace_seconds INTEGER NOT NULL DEFAULT 604800, + updated_by_user_id BIGINT REFERENCES public.users(id) ON DELETE SET NULL, + updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + CONSTRAINT storage_maintenance_thresholds CHECK ( + warning_percent >= 1 AND critical_percent <= 99.9 AND warning_percent < critical_percent + ), + CONSTRAINT storage_maintenance_intervals CHECK ( + check_interval_seconds BETWEEN 60 AND 86400 AND + stale_after_seconds BETWEEN 120 AND 604800 AND + alert_cooldown_seconds BETWEEN 300 AND 2592000 AND + cleanup_interval_seconds BETWEEN 3600 AND 604800 AND + orphan_grace_seconds BETWEEN 86400 AND 31536000 + ), + CONSTRAINT storage_maintenance_email_recipients_array CHECK ( + jsonb_typeof(email_recipients) = 'array' + ), + CONSTRAINT storage_maintenance_automatic_categories_empty CHECK ( + automatic_categories = '[]'::JSONB + ) +); + +INSERT INTO public.storage_maintenance_config (singleton) +VALUES (TRUE) +ON CONFLICT (singleton) DO NOTHING; + +CREATE TABLE IF NOT EXISTS public.maintenance_runtime_state ( + runtime_name VARCHAR(100) PRIMARY KEY, + worker_id VARCHAR(255), + started_at TIMESTAMPTZ, + heartbeat_at TIMESTAMPTZ, + last_error TEXT, + updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP +); + +CREATE TABLE IF NOT EXISTS public.maintenance_runs ( + id BIGSERIAL PRIMARY KEY, + job_name VARCHAR(100) NOT NULL, + trigger_type VARCHAR(20) NOT NULL CHECK (trigger_type IN ('scheduled', 'manual')), + mode VARCHAR(20) NOT NULL CHECK (mode IN ('preview', 'execute', 'measure')), + status VARCHAR(20) NOT NULL CHECK ( + status IN ('previewed', 'queued', 'running', 'completed', 'failed', 'cancelled') + ), + actor_user_id BIGINT REFERENCES public.users(id) ON DELETE SET NULL, + preview_run_id BIGINT REFERENCES public.maintenance_runs(id) ON DELETE RESTRICT, + started_at TIMESTAMPTZ, + completed_at TIMESTAMPTZ, + duration_ms BIGINT CHECK (duration_ms IS NULL OR duration_ms >= 0), + candidate_count BIGINT NOT NULL DEFAULT 0 CHECK (candidate_count >= 0), + candidate_bytes BIGINT NOT NULL DEFAULT 0 CHECK (candidate_bytes >= 0), + reclaimed_bytes BIGINT NOT NULL DEFAULT 0 CHECK (reclaimed_bytes >= 0), + failure_count BIGINT NOT NULL DEFAULT 0 CHECK (failure_count >= 0), + last_error TEXT, + configuration JSONB NOT NULL DEFAULT '{}'::JSONB CHECK (jsonb_typeof(configuration) = 'object'), + result JSONB NOT NULL DEFAULT '{}'::JSONB CHECK (jsonb_typeof(result) = 'object'), + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP +); + +CREATE INDEX IF NOT EXISTS idx_maintenance_runs_job_activity + ON public.maintenance_runs (job_name, created_at DESC, id DESC); + +CREATE TABLE IF NOT EXISTS public.maintenance_cleanup_items ( + id BIGSERIAL PRIMARY KEY, + run_id BIGINT NOT NULL REFERENCES public.maintenance_runs(id) ON DELETE CASCADE, + relative_path VARCHAR(2048) NOT NULL, + category VARCHAR(30) NOT NULL CHECK (category = 'derived'), + observed_size_bytes BIGINT NOT NULL CHECK (observed_size_bytes >= 0), + observed_modified_at TIMESTAMPTZ NOT NULL, + status VARCHAR(40) NOT NULL DEFAULT 'candidate' CHECK ( + status IN ( + 'candidate', 'deleted', 'skipped-missing', 'skipped-changed', + 'skipped-referenced', 'skipped-unsafe', 'failed' + ) + ), + reclaimed_bytes BIGINT NOT NULL DEFAULT 0 CHECK (reclaimed_bytes >= 0), + error TEXT, + completed_at TIMESTAMPTZ, + UNIQUE (run_id, relative_path) +); + +CREATE INDEX IF NOT EXISTS idx_maintenance_cleanup_items_run + ON public.maintenance_cleanup_items (run_id, status, id); + +CREATE TABLE IF NOT EXISTS public.maintenance_cleanup_tokens ( + token_hash CHAR(64) PRIMARY KEY CHECK (token_hash ~ '^[0-9a-f]{64}$'), + preview_run_id BIGINT NOT NULL UNIQUE REFERENCES public.maintenance_runs(id) ON DELETE CASCADE, + confirmation_phrase VARCHAR(255) NOT NULL, + expires_at TIMESTAMPTZ NOT NULL, + consumed_at TIMESTAMPTZ, + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + CONSTRAINT maintenance_cleanup_token_lifecycle CHECK ( + consumed_at IS NULL OR consumed_at >= created_at + ) +); + +CREATE TABLE IF NOT EXISTS public.storage_measurements ( + id BIGSERIAL PRIMARY KEY, + measured_at TIMESTAMPTZ NOT NULL, + filesystem_total_bytes BIGINT, + filesystem_used_bytes BIGINT, + filesystem_available_bytes BIGINT, + filesystem_used_percent NUMERIC(5,2), + source_image_bytes BIGINT, + source_image_count BIGINT, + thumbnail_bytes BIGINT, + thumbnail_count BIGINT, + derived_vehicle_image_bytes BIGINT, + derived_vehicle_image_count BIGINT, + database_bytes BIGINT, + docker_bytes BIGINT, + backup_bytes BIGINT, + host_snapshot_measured_at TIMESTAMPTZ, + errors JSONB NOT NULL DEFAULT '[]'::JSONB CHECK (jsonb_typeof(errors) = 'array'), + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + CONSTRAINT storage_measurements_nonnegative CHECK ( + COALESCE(filesystem_total_bytes, 0) >= 0 AND + COALESCE(filesystem_used_bytes, 0) >= 0 AND + COALESCE(filesystem_available_bytes, 0) >= 0 AND + COALESCE(source_image_bytes, 0) >= 0 AND COALESCE(source_image_count, 0) >= 0 AND + COALESCE(thumbnail_bytes, 0) >= 0 AND COALESCE(thumbnail_count, 0) >= 0 AND + COALESCE(derived_vehicle_image_bytes, 0) >= 0 AND COALESCE(derived_vehicle_image_count, 0) >= 0 AND + COALESCE(database_bytes, 0) >= 0 AND COALESCE(docker_bytes, 0) >= 0 AND + COALESCE(backup_bytes, 0) >= 0 + ) +); + +CREATE INDEX IF NOT EXISTS idx_storage_measurements_recent + ON public.storage_measurements (measured_at DESC, id DESC); + +CREATE TABLE IF NOT EXISTS public.maintenance_alert_state ( + event_key VARCHAR(255) PRIMARY KEY, + severity VARCHAR(20) NOT NULL CHECK (severity IN ('ok', 'warning', 'critical')), + fingerprint CHAR(64), + first_observed_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + last_observed_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + last_notified_at TIMESTAMPTZ, + next_eligible_at TIMESTAMPTZ, + resolved_at TIMESTAMPTZ, + occurrence_count BIGINT NOT NULL DEFAULT 1 CHECK (occurrence_count > 0), + suppressed_count BIGINT NOT NULL DEFAULT 0 CHECK (suppressed_count >= 0), + details JSONB NOT NULL DEFAULT '{}'::JSONB CHECK (jsonb_typeof(details) = 'object'), + updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP +); + +CREATE TABLE IF NOT EXISTS public.maintenance_alert_deliveries ( + id BIGSERIAL PRIMARY KEY, + dedupe_key CHAR(64) NOT NULL UNIQUE CHECK (dedupe_key ~ '^[0-9a-f]{64}$'), + event_key VARCHAR(255) NOT NULL REFERENCES public.maintenance_alert_state(event_key) ON DELETE CASCADE, + channel_type VARCHAR(20) NOT NULL CHECK (channel_type IN ('email', 'webhook')), + payload JSONB NOT NULL DEFAULT '{}'::JSONB CHECK (jsonb_typeof(payload) = 'object'), + status VARCHAR(20) NOT NULL DEFAULT 'pending' CHECK ( + status IN ('pending', 'processing', 'retry', 'succeeded', 'dead') + ), + attempt_count INTEGER NOT NULL DEFAULT 0 CHECK (attempt_count >= 0), + max_attempts SMALLINT NOT NULL DEFAULT 5 CHECK (max_attempts BETWEEN 1 AND 20), + next_attempt_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + locked_at TIMESTAMPTZ, + locked_by VARCHAR(255), + last_error TEXT, + delivered_at TIMESTAMPTZ, + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + CONSTRAINT maintenance_alert_delivery_lock CHECK ( + (status = 'processing' AND locked_at IS NOT NULL AND NULLIF(BTRIM(locked_by), '') IS NOT NULL) + OR (status <> 'processing' AND locked_at IS NULL AND locked_by IS NULL) + ) +); + +CREATE INDEX IF NOT EXISTS idx_maintenance_alert_deliveries_due + ON public.maintenance_alert_deliveries (next_attempt_at, id) + WHERE status IN ('pending', 'retry'); + +-- Maintenance webhook destination URLs can contain bearer credentials. Keep +-- them only in storage_maintenance_config; workers resolve the current value +-- immediately before delivery. This idempotently removes legacy queue copies +-- and destination-bearing error details without changing delivery status. +UPDATE public.maintenance_alert_deliveries +SET payload = payload - 'url', + last_error = CASE WHEN last_error IS NULL THEN NULL + ELSE 'Maintenance webhook delivery error details were redacted' END, + updated_at = CURRENT_TIMESTAMP +WHERE channel_type = 'webhook' + AND (payload ? 'url' OR last_error IS NOT NULL); + +DO $$ +BEGIN + IF NOT EXISTS ( + SELECT 1 + FROM pg_constraint constraint_record + JOIN pg_attribute column_record + ON column_record.attrelid = constraint_record.conrelid + AND column_record.attnum = ANY (constraint_record.conkey) + WHERE constraint_record.contype = 'f' + AND constraint_record.conrelid = 'public.storage_maintenance_config'::regclass + AND column_record.attname = 'updated_by_user_id' + ) THEN + ALTER TABLE public.storage_maintenance_config + ADD CONSTRAINT storage_maintenance_config_updated_by_fkey + FOREIGN KEY (updated_by_user_id) REFERENCES public.users(id) ON DELETE SET NULL; + END IF; + IF NOT EXISTS ( + SELECT 1 + FROM pg_constraint constraint_record + JOIN pg_attribute column_record + ON column_record.attrelid = constraint_record.conrelid + AND column_record.attnum = ANY (constraint_record.conkey) + WHERE constraint_record.contype = 'f' + AND constraint_record.conrelid = 'public.maintenance_runs'::regclass + AND column_record.attname = 'actor_user_id' + ) THEN + ALTER TABLE public.maintenance_runs + ADD CONSTRAINT maintenance_runs_actor_user_fkey + FOREIGN KEY (actor_user_id) REFERENCES public.users(id) ON DELETE SET NULL; + END IF; +END; +$$; + +INSERT INTO public.schema_migrations (version, description) +VALUES ( + '2026072901_storage_monitoring_maintenance', + 'Add persisted storage thresholds, runtime liveness, exact category measurements, rate-limited maintenance alerts, and token-guarded manual derived-orphan cleanup.' +) +ON CONFLICT (version) DO NOTHING; diff --git a/migrations/0028_2026072902_automatic_derived_orphan_cleanup.sql b/migrations/0028_2026072902_automatic_derived_orphan_cleanup.sql new file mode 100644 index 0000000..6643e8f --- /dev/null +++ b/migrations/0028_2026072902_automatic_derived_orphan_cleanup.sql @@ -0,0 +1,165 @@ +-- migration: 2026072902_automatic_derived_orphan_cleanup +-- Phase 2A: separately approved, bounded automatic cleanup for derived +-- orphans only. Approval history is append-only and defaults to no rows/off. +INSERT INTO public.permissions (permission_key, description) +VALUES ( + 'maintenance.automatic_cleanup.approve', + 'Approve, suspend, or acknowledge automatic derived-orphan cleanup.' +) +ON CONFLICT (permission_key) DO UPDATE SET description = EXCLUDED.description; + +INSERT INTO public.role_permissions (role_id, permission_id) +SELECT role.id, permission.id +FROM public.roles AS role +CROSS JOIN public.permissions AS permission +WHERE role.name = 'administrator' + AND permission.permission_key = 'maintenance.automatic_cleanup.approve' +ON CONFLICT (role_id, permission_id) DO NOTHING; + +CREATE TABLE IF NOT EXISTS public.storage_cleanup_automatic_approvals ( + id BIGSERIAL PRIMARY KEY, + category VARCHAR(40) NOT NULL CHECK (category = 'derived-orphans'), + revision BIGINT NOT NULL CHECK (revision > 0), + enabled BOOLEAN NOT NULL DEFAULT FALSE, + interval_seconds INTEGER NOT NULL DEFAULT 86400 CHECK (interval_seconds BETWEEN 86400 AND 604800), + grace_seconds INTEGER NOT NULL DEFAULT 604800 CHECK (grace_seconds BETWEEN 604800 AND 31536000), + actor_user_id BIGINT NOT NULL, + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + UNIQUE (category, revision), + CONSTRAINT storage_cleanup_approval_actor_fkey + FOREIGN KEY (actor_user_id) REFERENCES public.users(id) ON DELETE RESTRICT +); + +CREATE TABLE IF NOT EXISTS public.storage_cleanup_automatic_state ( + category VARCHAR(40) PRIMARY KEY CHECK (category = 'derived-orphans'), + next_run_at TIMESTAMPTZ, + last_run_id BIGINT REFERENCES public.maintenance_runs(id) ON DELETE SET NULL, + source_reconciliation_run_id BIGINT, + circuit_breaker_open BOOLEAN NOT NULL DEFAULT FALSE, + circuit_breaker_opened_at TIMESTAMPTZ, + circuit_breaker_reason TEXT, + circuit_breaker_run_id BIGINT REFERENCES public.maintenance_runs(id) ON DELETE SET NULL, + acknowledged_at TIMESTAMPTZ, + acknowledged_by_user_id BIGINT, + acknowledged_run_id BIGINT, + acknowledgement_reconciliation_run_id BIGINT, + updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + CONSTRAINT storage_cleanup_state_ack_actor_fkey + FOREIGN KEY (acknowledged_by_user_id) REFERENCES public.users(id) ON DELETE SET NULL, + CONSTRAINT storage_cleanup_breaker_state CHECK ( + (circuit_breaker_open AND circuit_breaker_opened_at IS NOT NULL AND circuit_breaker_reason IS NOT NULL) + OR (NOT circuit_breaker_open) + ), + CONSTRAINT storage_cleanup_ack_evidence CHECK ( + (acknowledged_at IS NULL AND acknowledged_run_id IS NULL AND acknowledgement_reconciliation_run_id IS NULL) + OR (acknowledged_at IS NOT NULL AND acknowledged_run_id IS NOT NULL AND acknowledgement_reconciliation_run_id IS NOT NULL) + ) +); + +DO $$ +BEGIN + IF NOT EXISTS ( + SELECT 1 FROM pg_constraint + WHERE conname = 'storage_cleanup_approval_actor_fkey' + AND conrelid = 'public.storage_cleanup_automatic_approvals'::regclass + ) THEN + ALTER TABLE public.storage_cleanup_automatic_approvals + ADD CONSTRAINT storage_cleanup_approval_actor_fkey + FOREIGN KEY (actor_user_id) REFERENCES public.users(id) ON DELETE RESTRICT; + END IF; + IF NOT EXISTS ( + SELECT 1 FROM pg_constraint + WHERE conname = 'storage_cleanup_state_ack_actor_fkey' + AND conrelid = 'public.storage_cleanup_automatic_state'::regclass + ) THEN + ALTER TABLE public.storage_cleanup_automatic_state + ADD CONSTRAINT storage_cleanup_state_ack_actor_fkey + FOREIGN KEY (acknowledged_by_user_id) REFERENCES public.users(id) ON DELETE SET NULL; + END IF; +END; +$$; + +ALTER TABLE public.maintenance_runs + ADD COLUMN IF NOT EXISTS source_reconciliation_run_id BIGINT; +ALTER TABLE public.storage_cleanup_automatic_state + ADD COLUMN IF NOT EXISTS acknowledged_run_id BIGINT; +ALTER TABLE public.storage_cleanup_automatic_state + ADD COLUMN IF NOT EXISTS acknowledgement_reconciliation_run_id BIGINT; + +DO $$ +BEGIN + IF NOT EXISTS ( + SELECT 1 FROM pg_constraint + WHERE conname = 'storage_cleanup_ack_evidence' + AND conrelid = 'public.storage_cleanup_automatic_state'::regclass + ) THEN + ALTER TABLE public.storage_cleanup_automatic_state + ADD CONSTRAINT storage_cleanup_ack_evidence CHECK ( + (acknowledged_at IS NULL AND acknowledged_run_id IS NULL AND acknowledgement_reconciliation_run_id IS NULL) + OR (acknowledged_at IS NOT NULL AND acknowledged_run_id IS NOT NULL AND acknowledgement_reconciliation_run_id IS NOT NULL) + ); + END IF; +END; +$$; + +DO $$ +BEGIN + IF NOT EXISTS ( + SELECT 1 FROM pg_constraint + WHERE conname = 'maintenance_runs_source_reconciliation_fkey' + AND conrelid = 'public.maintenance_runs'::regclass + ) THEN + ALTER TABLE public.maintenance_runs + ADD CONSTRAINT maintenance_runs_source_reconciliation_fkey + FOREIGN KEY (source_reconciliation_run_id) + REFERENCES public.storage_reconciliation_runs(id) ON DELETE RESTRICT; + END IF; + IF NOT EXISTS ( + SELECT 1 FROM pg_constraint + WHERE conname = 'storage_cleanup_state_source_reconciliation_fkey' + AND conrelid = 'public.storage_cleanup_automatic_state'::regclass + ) THEN + ALTER TABLE public.storage_cleanup_automatic_state + ADD CONSTRAINT storage_cleanup_state_source_reconciliation_fkey + FOREIGN KEY (source_reconciliation_run_id) + REFERENCES public.storage_reconciliation_runs(id) ON DELETE SET NULL; + END IF; + IF NOT EXISTS ( + SELECT 1 FROM pg_constraint + WHERE conname = 'storage_cleanup_state_acknowledged_run_fkey' + AND conrelid = 'public.storage_cleanup_automatic_state'::regclass + ) THEN + ALTER TABLE public.storage_cleanup_automatic_state + ADD CONSTRAINT storage_cleanup_state_acknowledged_run_fkey + FOREIGN KEY (acknowledged_run_id) + REFERENCES public.maintenance_runs(id) ON DELETE RESTRICT; + END IF; + IF NOT EXISTS ( + SELECT 1 FROM pg_constraint + WHERE conname = 'storage_cleanup_state_ack_reconciliation_fkey' + AND conrelid = 'public.storage_cleanup_automatic_state'::regclass + ) THEN + ALTER TABLE public.storage_cleanup_automatic_state + ADD CONSTRAINT storage_cleanup_state_ack_reconciliation_fkey + FOREIGN KEY (acknowledgement_reconciliation_run_id) + REFERENCES public.storage_reconciliation_runs(id) ON DELETE RESTRICT; + END IF; +END; +$$; + +ALTER TABLE public.maintenance_cleanup_items + DROP CONSTRAINT IF EXISTS maintenance_cleanup_items_status_check; +ALTER TABLE public.maintenance_cleanup_items + ADD CONSTRAINT maintenance_cleanup_items_status_check CHECK ( + status IN ( + 'candidate', 'deleted', 'skipped-missing', 'skipped-changed', + 'skipped-referenced', 'skipped-unsafe', 'skipped-limit', 'failed' + ) + ); + +INSERT INTO public.schema_migrations (version, description) +VALUES ( + '2026072902_automatic_derived_orphan_cleanup', + 'Add Administrator-only, revisioned and default-off automatic derived-orphan cleanup approval and circuit-breaker state.' +) +ON CONFLICT (version) DO NOTHING; diff --git a/migrations/0029_2026072903_host_retention_intents.sql b/migrations/0029_2026072903_host_retention_intents.sql new file mode 100644 index 0000000..d9f8361 --- /dev/null +++ b/migrations/0029_2026072903_host_retention_intents.sql @@ -0,0 +1,153 @@ +-- migration: 2026072903_host_retention_intents +-- Phase 3 is a fail-closed database control plane. Only the separately installed +-- fixed host worker can inspect or remove host artifacts. +CREATE TABLE IF NOT EXISTS public.host_maintenance_config ( + category VARCHAR(40) PRIMARY KEY CHECK (category IN ('docker-build-cache', 'unused-alpr-images', 'rollout-backups')), + automation_supported BOOLEAN NOT NULL DEFAULT TRUE, + scheduled_enabled BOOLEAN NOT NULL DEFAULT FALSE, + interval_seconds INTEGER NOT NULL DEFAULT 604800 CHECK (interval_seconds BETWEEN 86400 AND 2592000), + retained_verified_count INTEGER NOT NULL DEFAULT 5 CHECK (retained_verified_count BETWEEN 5 AND 50), + minimum_age_days INTEGER NOT NULL DEFAULT 30 CHECK (minimum_age_days BETWEEN 1 AND 365), + next_run_at TIMESTAMPTZ, + activation_revision BIGINT NOT NULL DEFAULT 0 CHECK (activation_revision >= 0), + activated_at TIMESTAMPTZ, activated_by_user_id BIGINT, + circuit_breaker_open BOOLEAN NOT NULL DEFAULT FALSE, + circuit_breaker_opened_at TIMESTAMPTZ, circuit_breaker_reason TEXT, + circuit_breaker_generation BIGINT NOT NULL DEFAULT 0 CHECK (circuit_breaker_generation >= 0), + updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP +); +INSERT INTO public.host_maintenance_config (category, automation_supported, minimum_age_days) VALUES + ('docker-build-cache', TRUE, 7), ('unused-alpr-images', FALSE, 7), ('rollout-backups', TRUE, 30) +ON CONFLICT (category) DO NOTHING; + +CREATE TABLE IF NOT EXISTS public.host_maintenance_environment_identity ( + singleton BOOLEAN PRIMARY KEY DEFAULT TRUE CHECK(singleton), environment_id VARCHAR(200) NOT NULL, + database_identity VARCHAR(200) NOT NULL, created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + UNIQUE(environment_id,database_identity) +); + +CREATE TABLE IF NOT EXISTS public.host_maintenance_intents ( + id BIGSERIAL PRIMARY KEY, intent_type VARCHAR(20) NOT NULL CHECK (intent_type IN ('preview','execute','scheduled')), + category VARCHAR(40) NOT NULL CHECK (category IN ('docker-build-cache','unused-alpr-images','rollout-backups')), + environment_id VARCHAR(200) NOT NULL, + database_identity VARCHAR(200) NOT NULL, + status VARCHAR(20) NOT NULL DEFAULT 'pending' CHECK (status IN ('pending','processing','completed','failed','cancelled')), + actor_user_id BIGINT, preview_intent_id BIGINT REFERENCES public.host_maintenance_intents(id) ON DELETE RESTRICT, + run_id BIGINT REFERENCES public.maintenance_runs(id) ON DELETE SET NULL, locked_at TIMESTAMPTZ, locked_by VARCHAR(255), + requested_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, started_at TIMESTAMPTZ, completed_at TIMESTAMPTZ, + inventory_revision VARCHAR(200), inventory_measured_at TIMESTAMPTZ, + candidate_count BIGINT NOT NULL DEFAULT 0 CHECK(candidate_count>=0), candidate_bytes BIGINT NOT NULL DEFAULT 0 CHECK(candidate_bytes>=0), + reclaimed_bytes BIGINT NOT NULL DEFAULT 0 CHECK(reclaimed_bytes>=0), last_error TEXT, + receipt JSONB NOT NULL DEFAULT '{}'::jsonb CHECK(jsonb_typeof(receipt)='object'), updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + UNIQUE(id,category), UNIQUE(id,category,actor_user_id), UNIQUE(id,category,environment_id,database_identity) +); +CREATE INDEX IF NOT EXISTS idx_host_maintenance_intents_due ON public.host_maintenance_intents(status,requested_at,id); + +CREATE TABLE IF NOT EXISTS public.host_maintenance_approvals ( + id BIGSERIAL PRIMARY KEY, category VARCHAR(40) NOT NULL REFERENCES public.host_maintenance_config(category) ON DELETE RESTRICT, + revision BIGINT NOT NULL CHECK(revision>0), enabled BOOLEAN NOT NULL, interval_seconds INTEGER NOT NULL, + retained_verified_count INTEGER NOT NULL, minimum_age_days INTEGER NOT NULL, actor_user_id BIGINT NOT NULL, + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, UNIQUE(category,revision) +); +CREATE TABLE IF NOT EXISTS public.host_maintenance_previews ( + id BIGSERIAL PRIMARY KEY, intent_id BIGINT NOT NULL UNIQUE REFERENCES public.host_maintenance_intents(id) ON DELETE RESTRICT, + category VARCHAR(40) NOT NULL REFERENCES public.host_maintenance_config(category) ON DELETE RESTRICT, actor_user_id BIGINT NOT NULL, + token_hash CHAR(64) NOT NULL UNIQUE CHECK(token_hash~'^[0-9a-f]{64}$'), environment_id VARCHAR(200) NOT NULL, + database_identity VARCHAR(200) NOT NULL, + policy_revision BIGINT NOT NULL, worker_generation VARCHAR(200) NOT NULL, inventory_revision VARCHAR(200) NOT NULL, + candidate_set_hash CHAR(64) NOT NULL CHECK(candidate_set_hash~'^[0-9a-f]{64}$'), inventory_measured_at TIMESTAMPTZ NOT NULL, + expires_at TIMESTAMPTZ NOT NULL, candidate_count BIGINT NOT NULL CHECK(candidate_count>=0), candidate_bytes BIGINT NOT NULL CHECK(candidate_bytes>=0), + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + CONSTRAINT host_maintenance_preview_intent_binding_fkey FOREIGN KEY(intent_id,category,actor_user_id) + REFERENCES public.host_maintenance_intents(id,category,actor_user_id) ON DELETE RESTRICT, + CONSTRAINT host_maintenance_preview_environment_binding_fkey FOREIGN KEY(intent_id,category,environment_id,database_identity) + REFERENCES public.host_maintenance_intents(id,category,environment_id,database_identity) ON DELETE RESTRICT +); +-- Plaintext tokens are ephemeral here and atomically DELETE ... RETURNING once; previews remain fully immutable. +CREATE TABLE IF NOT EXISTS public.host_maintenance_preview_deliveries ( + preview_id BIGINT PRIMARY KEY REFERENCES public.host_maintenance_previews(id) ON DELETE CASCADE, + opaque_token VARCHAR(128) NOT NULL UNIQUE, created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP +); +CREATE TABLE IF NOT EXISTS public.host_maintenance_preview_items ( + id BIGSERIAL PRIMARY KEY, preview_id BIGINT NOT NULL REFERENCES public.host_maintenance_previews(id) ON DELETE RESTRICT, + artifact_kind VARCHAR(40) NOT NULL CHECK(artifact_kind IN ('docker-build-cache','rollout-backup','unused-alpr-image')), + opaque_id VARCHAR(200) NOT NULL, identity VARCHAR(200) NOT NULL, bytes BIGINT NOT NULL CHECK(bytes>=0), + evidence JSONB NOT NULL DEFAULT '{}'::jsonb CHECK(jsonb_typeof(evidence)='object'), UNIQUE(preview_id,artifact_kind,opaque_id) +); +CREATE TABLE IF NOT EXISTS public.host_maintenance_preview_consumptions ( + preview_id BIGINT PRIMARY KEY REFERENCES public.host_maintenance_previews(id) ON DELETE RESTRICT, + execution_intent_id BIGINT NOT NULL UNIQUE REFERENCES public.host_maintenance_intents(id) ON DELETE RESTRICT, + actor_user_id BIGINT NOT NULL, consumed_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP +); +CREATE TABLE IF NOT EXISTS public.host_maintenance_receipts ( + id BIGSERIAL PRIMARY KEY, intent_id BIGINT NOT NULL UNIQUE REFERENCES public.host_maintenance_intents(id) ON DELETE RESTRICT, + category VARCHAR(40) NOT NULL REFERENCES public.host_maintenance_config(category) ON DELETE RESTRICT, + environment_id VARCHAR(200) NOT NULL, database_identity VARCHAR(200) NOT NULL, worker_generation VARCHAR(200) NOT NULL, inventory_revision VARCHAR(200) NOT NULL, + candidate_set_hash CHAR(64) NOT NULL CHECK(candidate_set_hash~'^[0-9a-f]{64}$'), success BOOLEAN NOT NULL, + reclaimed_bytes BIGINT NOT NULL DEFAULT 0 CHECK(reclaimed_bytes>=0), result JSONB NOT NULL DEFAULT '{}'::jsonb CHECK(jsonb_typeof(result)='object'), + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + CONSTRAINT host_maintenance_receipt_intent_binding_fkey FOREIGN KEY(intent_id,category,environment_id,database_identity) + REFERENCES public.host_maintenance_intents(id,category,environment_id,database_identity) ON DELETE RESTRICT +); +CREATE TABLE IF NOT EXISTS public.host_maintenance_receipt_items ( + id BIGSERIAL PRIMARY KEY, receipt_id BIGINT NOT NULL REFERENCES public.host_maintenance_receipts(id) ON DELETE RESTRICT, + artifact_kind VARCHAR(40) NOT NULL CHECK(artifact_kind IN ('docker-build-cache','rollout-backup','unused-alpr-image')), + opaque_id VARCHAR(200) NOT NULL, identity VARCHAR(200) NOT NULL, status VARCHAR(20) NOT NULL CHECK(status IN ('deleted','quarantined','skipped','failed')), + reclaimed_bytes BIGINT NOT NULL DEFAULT 0 CHECK(reclaimed_bytes>=0), error TEXT, UNIQUE(receipt_id,artifact_kind,opaque_id) +); +CREATE TABLE IF NOT EXISTS public.host_maintenance_acknowledgements ( + id BIGSERIAL PRIMARY KEY, category VARCHAR(40) NOT NULL REFERENCES public.host_maintenance_config(category) ON DELETE RESTRICT, + breaker_generation BIGINT NOT NULL CHECK(breaker_generation>0), failed_intent_id BIGINT NOT NULL REFERENCES public.host_maintenance_intents(id) ON DELETE RESTRICT, + actor_user_id BIGINT NOT NULL, evidence JSONB NOT NULL CHECK(jsonb_typeof(evidence)='object'), created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + UNIQUE(category,breaker_generation) +); +CREATE TABLE IF NOT EXISTS public.host_maintenance_worker_state ( + environment_id VARCHAR(200) PRIMARY KEY, database_identity VARCHAR(200) NOT NULL, worker_generation VARCHAR(200) NOT NULL, worker_id VARCHAR(255) NOT NULL, + heartbeat_at TIMESTAMPTZ NOT NULL, inventory_revision VARCHAR(200) NOT NULL, inventory_measured_at TIMESTAMPTZ NOT NULL, + last_error TEXT, updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP +); + +CREATE OR REPLACE FUNCTION public.reject_host_maintenance_evidence_mutation() RETURNS TRIGGER LANGUAGE plpgsql AS $$ +BEGIN RAISE EXCEPTION 'host maintenance evidence is append-only'; END; $$; +CREATE OR REPLACE FUNCTION public.validate_host_maintenance_evidence_binding() RETURNS TRIGGER LANGUAGE plpgsql AS $$ +DECLARE bound_category TEXT; BEGIN + IF TG_TABLE_NAME='host_maintenance_preview_items' THEN SELECT category INTO bound_category FROM public.host_maintenance_previews WHERE id=NEW.preview_id; + ELSIF TG_TABLE_NAME='host_maintenance_receipt_items' THEN SELECT category INTO bound_category FROM public.host_maintenance_receipts WHERE id=NEW.receipt_id; + ELSIF TG_TABLE_NAME='host_maintenance_acknowledgements' THEN SELECT category INTO bound_category FROM public.host_maintenance_intents WHERE id=NEW.failed_intent_id; + IF bound_category IS DISTINCT FROM NEW.category THEN RAISE EXCEPTION 'host acknowledgement category mismatch'; END IF; RETURN NEW; + ELSE PERFORM 1 FROM public.host_maintenance_previews p JOIN public.host_maintenance_intents i ON i.id=NEW.execution_intent_id + WHERE p.id=NEW.preview_id AND p.actor_user_id=NEW.actor_user_id AND i.actor_user_id=NEW.actor_user_id AND i.category=p.category + AND i.environment_id=p.environment_id AND i.database_identity=p.database_identity; + IF NOT FOUND THEN RAISE EXCEPTION 'host preview consumption binding mismatch'; END IF; RETURN NEW; END IF; + IF (bound_category='docker-build-cache' AND NEW.artifact_kind<>'docker-build-cache') OR + (bound_category='unused-alpr-images' AND NEW.artifact_kind<>'unused-alpr-image') OR + (bound_category='rollout-backups' AND NEW.artifact_kind<>'rollout-backup') THEN RAISE EXCEPTION 'host artifact category mismatch'; END IF; + RETURN NEW; END; $$; +DROP TRIGGER IF EXISTS host_maintenance_preview_item_binding ON public.host_maintenance_preview_items; +CREATE TRIGGER host_maintenance_preview_item_binding BEFORE INSERT ON public.host_maintenance_preview_items FOR EACH ROW EXECUTE FUNCTION public.validate_host_maintenance_evidence_binding(); +DROP TRIGGER IF EXISTS host_maintenance_consumption_binding ON public.host_maintenance_preview_consumptions; +CREATE TRIGGER host_maintenance_consumption_binding BEFORE INSERT ON public.host_maintenance_preview_consumptions FOR EACH ROW EXECUTE FUNCTION public.validate_host_maintenance_evidence_binding(); +DROP TRIGGER IF EXISTS host_maintenance_receipt_item_binding ON public.host_maintenance_receipt_items; +CREATE TRIGGER host_maintenance_receipt_item_binding BEFORE INSERT ON public.host_maintenance_receipt_items FOR EACH ROW EXECUTE FUNCTION public.validate_host_maintenance_evidence_binding(); +DROP TRIGGER IF EXISTS host_maintenance_ack_binding ON public.host_maintenance_acknowledgements; +CREATE TRIGGER host_maintenance_ack_binding BEFORE INSERT ON public.host_maintenance_acknowledgements FOR EACH ROW EXECUTE FUNCTION public.validate_host_maintenance_evidence_binding(); +DO $$ DECLARE table_name TEXT; BEGIN + FOREACH table_name IN ARRAY ARRAY['host_maintenance_environment_identity','host_maintenance_approvals','host_maintenance_previews','host_maintenance_preview_items', + 'host_maintenance_preview_consumptions','host_maintenance_receipts','host_maintenance_receipt_items','host_maintenance_acknowledgements'] LOOP + EXECUTE format('DROP TRIGGER IF EXISTS host_maintenance_append_only ON public.%I',table_name); + EXECUTE format('CREATE TRIGGER host_maintenance_append_only BEFORE UPDATE OR DELETE ON public.%I FOR EACH ROW EXECUTE FUNCTION public.reject_host_maintenance_evidence_mutation()',table_name); + END LOOP; +END $$; + +DO $$ BEGIN + IF NOT EXISTS(SELECT 1 FROM pg_constraint WHERE conname='host_maintenance_config_actor_fkey' AND conrelid='public.host_maintenance_config'::regclass) THEN ALTER TABLE public.host_maintenance_config ADD CONSTRAINT host_maintenance_config_actor_fkey FOREIGN KEY(activated_by_user_id) REFERENCES public.users(id) ON DELETE SET NULL; END IF; + IF NOT EXISTS(SELECT 1 FROM pg_constraint WHERE conname='host_maintenance_intent_actor_fkey' AND conrelid='public.host_maintenance_intents'::regclass) THEN ALTER TABLE public.host_maintenance_intents ADD CONSTRAINT host_maintenance_intent_actor_fkey FOREIGN KEY(actor_user_id) REFERENCES public.users(id) ON DELETE SET NULL; END IF; + IF NOT EXISTS(SELECT 1 FROM pg_constraint WHERE conname='host_maintenance_approval_actor_fkey' AND conrelid='public.host_maintenance_approvals'::regclass) THEN ALTER TABLE public.host_maintenance_approvals ADD CONSTRAINT host_maintenance_approval_actor_fkey FOREIGN KEY(actor_user_id) REFERENCES public.users(id) ON DELETE RESTRICT; END IF; + IF NOT EXISTS(SELECT 1 FROM pg_constraint WHERE conname='host_maintenance_preview_actor_fkey' AND conrelid='public.host_maintenance_previews'::regclass) THEN ALTER TABLE public.host_maintenance_previews ADD CONSTRAINT host_maintenance_preview_actor_fkey FOREIGN KEY(actor_user_id) REFERENCES public.users(id) ON DELETE RESTRICT; END IF; + IF NOT EXISTS(SELECT 1 FROM pg_constraint WHERE conname='host_maintenance_consumption_actor_fkey' AND conrelid='public.host_maintenance_preview_consumptions'::regclass) THEN ALTER TABLE public.host_maintenance_preview_consumptions ADD CONSTRAINT host_maintenance_consumption_actor_fkey FOREIGN KEY(actor_user_id) REFERENCES public.users(id) ON DELETE RESTRICT; END IF; + IF NOT EXISTS(SELECT 1 FROM pg_constraint WHERE conname='host_maintenance_ack_actor_fkey' AND conrelid='public.host_maintenance_acknowledgements'::regclass) THEN ALTER TABLE public.host_maintenance_acknowledgements ADD CONSTRAINT host_maintenance_ack_actor_fkey FOREIGN KEY(actor_user_id) REFERENCES public.users(id) ON DELETE RESTRICT; END IF; +END $$; + +INSERT INTO public.schema_migrations(version,description) VALUES + ('2026072903_host_retention_intents','Add three-boundary, default-off host-maintenance intent/immutable preview/receipt control plane.') +ON CONFLICT(version) DO NOTHING; diff --git a/migrations/0030_2026073001_manual_database_backup.sql b/migrations/0030_2026073001_manual_database_backup.sql new file mode 100644 index 0000000..e8195ce --- /dev/null +++ b/migrations/0030_2026073001_manual_database_backup.sql @@ -0,0 +1,53 @@ +-- migration: 2026073001_manual_database_backup +-- Manual database backup is deliberately separate from retention intents: it +-- accepts no path, command, schedule, or restore input from the application. +ALTER TABLE public.host_maintenance_worker_state + ADD COLUMN IF NOT EXISTS database_backup_capability VARCHAR(80) + CHECK(database_backup_capability IS NULL OR database_backup_capability='database-backup-create-v1'); +ALTER TABLE public.host_maintenance_worker_state + ADD COLUMN IF NOT EXISTS database_backup_capability_at TIMESTAMPTZ; + +CREATE TABLE IF NOT EXISTS public.host_database_backup_requests ( + id BIGSERIAL PRIMARY KEY, + environment_id VARCHAR(200) NOT NULL, + database_identity VARCHAR(200) NOT NULL, + status VARCHAR(20) NOT NULL DEFAULT 'pending' CHECK(status IN ('pending','processing','completed','failed')), + actor_user_id BIGINT, + requested_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + started_at TIMESTAMPTZ, + completed_at TIMESTAMPTZ, + locked_at TIMESTAMPTZ, + locked_by VARCHAR(255), + worker_generation VARCHAR(200), + replay_count INTEGER NOT NULL DEFAULT 0 CHECK(replay_count BETWEEN 0 AND 2), + filename VARCHAR(255) CHECK(filename IS NULL OR filename~'^alpr-postgres-[0-9]{8}T[0-9]{6}Z-[0-9]+[.]dump$'), + size_bytes BIGINT CHECK(size_bytes IS NULL OR size_bytes>0), + checksum_sha256 CHAR(64) CHECK(checksum_sha256 IS NULL OR checksum_sha256~'^[0-9a-f]{64}$'), + verified BOOLEAN NOT NULL DEFAULT FALSE, + last_error TEXT, + updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP +); +DO $$ +BEGIN + IF NOT EXISTS ( + SELECT 1 + FROM pg_constraint + WHERE conname = 'host_database_backup_requests_actor_user_id_fkey' + AND conrelid = 'public.host_database_backup_requests'::regclass + ) THEN + ALTER TABLE public.host_database_backup_requests + ADD CONSTRAINT host_database_backup_requests_actor_user_id_fkey + FOREIGN KEY (actor_user_id) REFERENCES public.users(id) ON DELETE SET NULL; + END IF; +END $$; +ALTER TABLE public.host_database_backup_requests + ADD COLUMN IF NOT EXISTS replay_count INTEGER NOT NULL DEFAULT 0 CHECK(replay_count BETWEEN 0 AND 2); +CREATE INDEX IF NOT EXISTS idx_host_database_backup_requests_due + ON public.host_database_backup_requests(status,requested_at,id); +CREATE UNIQUE INDEX IF NOT EXISTS idx_host_database_backup_requests_one_active + ON public.host_database_backup_requests(environment_id,database_identity) + WHERE status IN ('pending','processing'); + +INSERT INTO public.schema_migrations(version,description) VALUES + ('2026073001_manual_database_backup','Add a fixed no-input manual database-backup request and status control plane.') +ON CONFLICT(version) DO NOTHING; diff --git a/migrations/0031_2026080301_configurable_image_retirement_grace.sql b/migrations/0031_2026080301_configurable_image_retirement_grace.sql new file mode 100644 index 0000000..b41e126 --- /dev/null +++ b/migrations/0031_2026080301_configurable_image_retirement_grace.sql @@ -0,0 +1,13 @@ +-- migration: 2026080301_configurable_image_retirement_grace +-- Image retirement remains manual-only, but its post-retirement recovery +-- window is administrator-configurable from one to 365 days. Seven days stays +-- the default. The fixed worker revalidates the same persisted value. +ALTER TABLE public.host_maintenance_config + DROP CONSTRAINT IF EXISTS host_maintenance_config_minimum_age_days_check; +ALTER TABLE public.host_maintenance_config + ADD CONSTRAINT host_maintenance_config_minimum_age_days_check + CHECK (minimum_age_days BETWEEN 1 AND 365); + +INSERT INTO public.schema_migrations(version,description) VALUES + ('2026080301_configurable_image_retirement_grace','Allow an audited 1-365 day manual image-retirement grace while retaining a seven-day default.') +ON CONFLICT(version) DO NOTHING; diff --git a/migrations/0032_2026080501_read_only_backup_catalog.sql b/migrations/0032_2026080501_read_only_backup_catalog.sql new file mode 100644 index 0000000..c7a4425 --- /dev/null +++ b/migrations/0032_2026080501_read_only_backup_catalog.sql @@ -0,0 +1,49 @@ +-- migration: 2026080501_read_only_backup_catalog +-- Persist the worker's validated, path-free rollback-backup inventory as +-- append-only read-only evidence. Rollback-backup execution and scheduling are +-- explicitly disabled until a later migration adds catalog-bound approval. +UPDATE public.host_maintenance_config + SET automation_supported=FALSE,scheduled_enabled=FALSE,next_run_at=NULL,updated_at=CURRENT_TIMESTAMP + WHERE category='rollout-backups'; + +CREATE TABLE IF NOT EXISTS public.host_backup_catalog_snapshots ( + id BIGSERIAL PRIMARY KEY, + catalog_version VARCHAR(80) NOT NULL CHECK(catalog_version='host-backup-catalog-v1'), + environment_id VARCHAR(200) NOT NULL,database_identity VARCHAR(200) NOT NULL,worker_generation VARCHAR(200) NOT NULL, + inventory_revision VARCHAR(200) NOT NULL,catalog_revision CHAR(64) NOT NULL CHECK(catalog_revision~'^[0-9a-f]{64}$'), + inventory_measured_at TIMESTAMPTZ NOT NULL,catalog_complete BOOLEAN NOT NULL,release_ledger_complete BOOLEAN NOT NULL, + authoritative_current_release_count INTEGER NOT NULL CHECK(authoritative_current_release_count>=0), + backup_restore_lease BOOLEAN NOT NULL,build_lease BOOLEAN NOT NULL,deploy_lease BOOLEAN NOT NULL,rollback_lease BOOLEAN NOT NULL, + backup_count BIGINT NOT NULL CHECK(backup_count>=0),backup_bytes BIGINT NOT NULL CHECK(backup_bytes>=0), + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + UNIQUE(environment_id,database_identity,inventory_revision,catalog_revision), + CONSTRAINT host_backup_catalog_environment_fkey FOREIGN KEY(environment_id,database_identity) + REFERENCES public.host_maintenance_environment_identity(environment_id,database_identity) ON DELETE RESTRICT +); + +CREATE TABLE IF NOT EXISTS public.host_backup_catalog_entries ( + id BIGSERIAL PRIMARY KEY,snapshot_id BIGINT NOT NULL REFERENCES public.host_backup_catalog_snapshots(id) ON DELETE RESTRICT, + opaque_id VARCHAR(200) NOT NULL,identity VARCHAR(200) NOT NULL,bytes BIGINT NOT NULL CHECK(bytes>=0),backup_created_at TIMESTAMPTZ NOT NULL, + checksum_verified BOOLEAN NOT NULL,checksum_sha256 CHAR(64) CHECK(checksum_sha256 IS NULL OR checksum_sha256~'^[0-9a-f]{64}$'), + explicitly_protected BOOLEAN NOT NULL,current_release BOOLEAN NOT NULL,rollback_chain BOOLEAN NOT NULL, + image_ids JSONB NOT NULL DEFAULT '[]'::jsonb CHECK(jsonb_typeof(image_ids)='array'), + backup_environment_id VARCHAR(200) NOT NULL,backup_database_identity VARCHAR(200) NOT NULL,release_id VARCHAR(200) NOT NULL, + schema_version VARCHAR(200) NOT NULL,postgres_format VARCHAR(200) NOT NULL,device VARCHAR(200) NOT NULL,inode VARCHAR(200) NOT NULL, + modified_at TIMESTAMPTZ NOT NULL,partial BOOLEAN NOT NULL,symlink BOOLEAN NOT NULL,hardlink_count INTEGER NOT NULL CHECK(hardlink_count>0), + UNIQUE(snapshot_id,opaque_id) +); +CREATE INDEX IF NOT EXISTS idx_host_backup_catalog_snapshots_current + ON public.host_backup_catalog_snapshots(environment_id,database_identity,id DESC); + +DO $$ DECLARE table_name TEXT; BEGIN + FOREACH table_name IN ARRAY ARRAY['host_backup_catalog_snapshots','host_backup_catalog_entries'] LOOP + EXECUTE format('DROP TRIGGER IF EXISTS host_backup_catalog_append_only ON public.%I',table_name); + EXECUTE format('CREATE TRIGGER host_backup_catalog_append_only BEFORE UPDATE OR DELETE ON public.%I FOR EACH ROW EXECUTE FUNCTION public.reject_host_maintenance_evidence_mutation()',table_name); + EXECUTE format('DROP TRIGGER IF EXISTS host_backup_catalog_no_truncate ON public.%I',table_name); + EXECUTE format('CREATE TRIGGER host_backup_catalog_no_truncate BEFORE TRUNCATE ON public.%I FOR EACH STATEMENT EXECUTE FUNCTION public.reject_host_maintenance_evidence_mutation()',table_name); + END LOOP; +END $$; + +INSERT INTO public.schema_migrations(version,description) VALUES + ('2026080501_read_only_backup_catalog','Persist immutable worker-validated rollback-backup catalog snapshots while keeping destructive retention disabled.') +ON CONFLICT(version) DO NOTHING; diff --git a/migrations/0033_2026080702_blue_iris_trigger_direction_shadow.sql b/migrations/0033_2026080702_blue_iris_trigger_direction_shadow.sql new file mode 100644 index 0000000..2b3211f --- /dev/null +++ b/migrations/0033_2026080702_blue_iris_trigger_direction_shadow.sql @@ -0,0 +1,67 @@ +-- migration: 2026080702_blue_iris_trigger_direction_shadow +-- Blue Iris can include the ordered zone crossing which caused an alert in +-- its &TYPE macro. Keep this as independent shadow evidence until daylight +-- validation proves the camera-specific mapping in both directions. +ALTER TABLE public.camera_direction_profiles + ADD COLUMN IF NOT EXISTS blue_iris_motion_enabled BOOLEAN NOT NULL DEFAULT FALSE, + ADD COLUMN IF NOT EXISTS blue_iris_front_trigger_type VARCHAR(80), + ADD COLUMN IF NOT EXISTS blue_iris_rear_trigger_type VARCHAR(80), + ADD COLUMN IF NOT EXISTS blue_iris_motion_profile_version INTEGER NOT NULL DEFAULT 1 + CHECK (blue_iris_motion_profile_version > 0); + +ALTER TABLE public.camera_direction_profiles + DROP CONSTRAINT IF EXISTS camera_direction_blue_iris_trigger_shape; +ALTER TABLE public.camera_direction_profiles + ADD CONSTRAINT camera_direction_blue_iris_trigger_shape CHECK ( + blue_iris_motion_enabled = FALSE OR ( + blue_iris_front_trigger_type ~ '^MOTION_[A-H]>[A-H]$' AND + blue_iris_rear_trigger_type ~ '^MOTION_[A-H]>[A-H]$' AND + SUBSTRING(blue_iris_front_trigger_type FROM 8 FOR 1) <> + SUBSTRING(blue_iris_front_trigger_type FROM 10 FOR 1) AND + blue_iris_rear_trigger_type = + 'MOTION_' || SUBSTRING(blue_iris_front_trigger_type FROM 10 FOR 1) || + '>' || SUBSTRING(blue_iris_front_trigger_type FROM 8 FOR 1) + ) + ); + +ALTER TABLE public.plate_reads + ADD COLUMN IF NOT EXISTS bi_trigger_type VARCHAR(80), + ADD COLUMN IF NOT EXISTS bi_trigger_direction_status VARCHAR(20), + ADD COLUMN IF NOT EXISTS bi_trigger_direction_label VARCHAR(80), + ADD COLUMN IF NOT EXISTS bi_trigger_direction_profile_version INTEGER, + ADD COLUMN IF NOT EXISTS bi_trigger_direction_algorithm VARCHAR(80), + ADD COLUMN IF NOT EXISTS bi_trigger_direction_error_code VARCHAR(80); + +ALTER TABLE public.plate_reads + DROP CONSTRAINT IF EXISTS plate_reads_bi_trigger_type_shape; +ALTER TABLE public.plate_reads + ADD CONSTRAINT plate_reads_bi_trigger_type_shape CHECK ( + bi_trigger_type IS NULL OR bi_trigger_type ~ '^[A-Z0-9_!>,+\-]{1,80}$' + ); +ALTER TABLE public.plate_reads + DROP CONSTRAINT IF EXISTS plate_reads_bi_trigger_direction_state; +ALTER TABLE public.plate_reads + ADD CONSTRAINT plate_reads_bi_trigger_direction_state CHECK ( + (bi_trigger_direction_status IS NULL AND + bi_trigger_direction_label IS NULL AND + bi_trigger_direction_profile_version IS NULL AND + bi_trigger_direction_algorithm IS NULL AND + bi_trigger_direction_error_code IS NULL) OR + (bi_trigger_direction_status = 'ready' AND + bi_trigger_direction_label IS NOT NULL AND + bi_trigger_direction_profile_version IS NOT NULL AND + bi_trigger_direction_algorithm IS NOT NULL AND + bi_trigger_direction_error_code IS NULL) OR + (bi_trigger_direction_status = 'unknown' AND + bi_trigger_direction_label IS NULL AND + bi_trigger_direction_algorithm IS NOT NULL AND + bi_trigger_direction_error_code IS NOT NULL) + ); + +CREATE INDEX IF NOT EXISTS idx_plate_reads_bi_trigger_direction + ON public.plate_reads (camera_name, bi_trigger_direction_status, timestamp DESC) + WHERE bi_trigger_direction_status IS NOT NULL; + +INSERT INTO public.schema_migrations(version,description) VALUES + ('2026080702_blue_iris_trigger_direction_shadow','Store and map ordered Blue Iris &TYPE zone crossings as camera-specific shadow direction evidence.') +ON CONFLICT(version) DO NOTHING; diff --git a/migrations/0034_2026080703_blue_iris_trigger_direction_hardening.sql b/migrations/0034_2026080703_blue_iris_trigger_direction_hardening.sql new file mode 100644 index 0000000..3206129 --- /dev/null +++ b/migrations/0034_2026080703_blue_iris_trigger_direction_hardening.sql @@ -0,0 +1,4 @@ +-- migration: 2026080703_blue_iris_trigger_direction_hardening +INSERT INTO public.schema_migrations(version,description) VALUES + ('2026080703_blue_iris_trigger_direction_hardening','Separate Blue Iris mapping revisions from ReID, enforce exact reverse crossings, and scope diagnostics by camera.') +ON CONFLICT(version) DO NOTHING; diff --git a/migrations/0035_2026080801_daytime_overview_vehicle_views.sql b/migrations/0035_2026080801_daytime_overview_vehicle_views.sql new file mode 100644 index 0000000..bb3ce1f --- /dev/null +++ b/migrations/0035_2026080801_daytime_overview_vehicle_views.sql @@ -0,0 +1,137 @@ +-- migration: 2026080801_daytime_overview_vehicle_views +-- Phase 2 overview Vehicle Views. Motion alerts from an overview-capable Blue +-- Iris camera are screened and sampled independently from plate ingestion. +-- Direction-aware, camera-pair timing profiles associate one retained daytime +-- frame only after both LPR reads have had time to arrive. Nighttime candidates +-- and nighttime plate reads are terminal and never enter the processing queue. +CREATE TABLE IF NOT EXISTS public.vehicle_overview_pair_profiles ( + id BIGSERIAL PRIMARY KEY, + source_camera_name VARCHAR(120) NOT NULL CHECK (BTRIM(source_camera_name) <> ''), + plate_camera_name VARCHAR(120) NOT NULL CHECK (BTRIM(plate_camera_name) <> ''), + direction_label VARCHAR(80) NOT NULL CHECK (BTRIM(direction_label) <> ''), + source_role VARCHAR(16) NOT NULL DEFAULT 'primary' + CHECK (source_role IN ('primary','fallback')), + expected_delta_ms INTEGER NOT NULL DEFAULT 0 + CHECK (expected_delta_ms BETWEEN -30000 AND 30000), + tolerance_ms INTEGER NOT NULL DEFAULT 1500 + CHECK (tolerance_ms BETWEEN 250 AND 10000), + priority SMALLINT NOT NULL DEFAULT 0 CHECK (priority BETWEEN 0 AND 100), + enabled BOOLEAN NOT NULL DEFAULT TRUE, + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + UNIQUE (source_camera_name, plate_camera_name, direction_label) +); + +CREATE TABLE IF NOT EXISTS public.vehicle_overview_candidates ( + id BIGSERIAL PRIMARY KEY, + event_identity CHAR(64) NOT NULL UNIQUE CHECK (event_identity ~ '^[0-9a-f]{64}$'), + source_camera_name VARCHAR(120) NOT NULL CHECK (BTRIM(source_camera_name) <> ''), + event_timestamp TIMESTAMPTZ NOT NULL, + bi_alert_clip TEXT, + bi_alert_path TEXT, + bi_alert_offset_ms BIGINT, + bi_trigger_type VARCHAR(80), + daylight_status VARCHAR(16) NOT NULL + CHECK (daylight_status IN ('daytime','nighttime')), + monochrome_ratio REAL CHECK (monochrome_ratio IS NULL OR monochrome_ratio BETWEEN 0 AND 1), + status VARCHAR(20) NOT NULL + CHECK (status IN ('pending','processing','ready','matching','associated','ambiguous','unavailable','failed')), + frame_path TEXT, + frame_timestamp TIMESTAMPTZ, + frame_score REAL, + detection_confidence REAL, + detection_box JSONB, + image_width INTEGER, + image_height INTEGER, + sampled_count SMALLINT, + attempt_count SMALLINT NOT NULL DEFAULT 0 CHECK (attempt_count BETWEEN 0 AND 20), + match_attempt_count SMALLINT NOT NULL DEFAULT 0 CHECK (match_attempt_count BETWEEN 0 AND 20), + retryable BOOLEAN NOT NULL DEFAULT TRUE, + error_code VARCHAR(80), + selection_metadata JSONB, + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP +); + +CREATE INDEX IF NOT EXISTS idx_vehicle_overview_candidates_work + ON public.vehicle_overview_candidates (status, updated_at, event_timestamp, id) + WHERE status IN ('pending','processing','ready','matching','failed'); + +CREATE INDEX IF NOT EXISTS idx_vehicle_overview_pair_profiles_lookup + ON public.vehicle_overview_pair_profiles ( + LOWER(BTRIM(source_camera_name)), LOWER(BTRIM(plate_camera_name)), + LOWER(BTRIM(direction_label)) + ) WHERE enabled = TRUE; + +CREATE TABLE IF NOT EXISTS public.vehicle_overview_associations ( + id BIGSERIAL PRIMARY KEY, + candidate_id BIGINT NOT NULL REFERENCES public.vehicle_overview_candidates(id) ON DELETE RESTRICT, + read_id INTEGER NOT NULL REFERENCES public.plate_reads(id) ON DELETE RESTRICT, + pair_profile_id BIGINT NOT NULL REFERENCES public.vehicle_overview_pair_profiles(id) ON DELETE RESTRICT, + algorithm VARCHAR(80) NOT NULL, + association_score REAL NOT NULL, + actual_delta_ms INTEGER NOT NULL, + timing_error_ms INTEGER NOT NULL CHECK (timing_error_ms >= 0), + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + UNIQUE (candidate_id, read_id), + UNIQUE (read_id) +); + +ALTER TABLE public.plate_reads + ADD COLUMN IF NOT EXISTS vehicle_image_source_kind VARCHAR(24), + ADD COLUMN IF NOT EXISTS vehicle_overview_candidate_id BIGINT; + +DO $$ +DECLARE + current_length INTEGER; +BEGIN + SELECT columns.character_maximum_length + INTO current_length + FROM information_schema.columns + WHERE columns.table_schema = 'public' + AND columns.table_name = 'plate_reads' + AND columns.column_name = 'vehicle_image_source_kind'; + + IF current_length IS DISTINCT FROM 40 THEN + EXECUTE 'ALTER TABLE public.plate_reads ALTER COLUMN vehicle_image_source_kind TYPE VARCHAR(40)'; + END IF; +END $$; + +ALTER TABLE public.plate_reads + DROP CONSTRAINT IF EXISTS plate_reads_vehicle_image_source_kind_check; +ALTER TABLE public.plate_reads + ADD CONSTRAINT plate_reads_vehicle_image_source_kind_check CHECK ( + vehicle_image_source_kind IS NULL OR + vehicle_image_source_kind IN ( + 'legacy_plate_camera','overview_primary','entry_overview_primary', + 'overview_fallback','overview_pair_share','entry_lpr_fallback', + 'entry_overview_route_fallback','entry_overview_history' + ) + ) NOT VALID; + +ALTER TABLE public.plate_reads + DROP CONSTRAINT IF EXISTS plate_reads_vehicle_image_queue_kind_check; +ALTER TABLE public.plate_reads + ADD CONSTRAINT plate_reads_vehicle_image_queue_kind_check + CHECK (vehicle_image_queue_kind IS NULL OR vehicle_image_queue_kind IN ('live', 'historical', 'manual', 'overview', 'overview_backfill', 'overview_repair')); + +DO $$ BEGIN + IF NOT EXISTS ( + SELECT 1 FROM pg_constraint + WHERE conname = 'plate_reads_vehicle_overview_candidate_fkey' + AND conrelid = 'public.plate_reads'::regclass + ) THEN + ALTER TABLE public.plate_reads + ADD CONSTRAINT plate_reads_vehicle_overview_candidate_fkey + FOREIGN KEY (vehicle_overview_candidate_id) + REFERENCES public.vehicle_overview_candidates(id) ON DELETE SET NULL; + END IF; +END $$; + +CREATE INDEX IF NOT EXISTS idx_plate_reads_overview_waiting + ON public.plate_reads (vehicle_image_queue_kind, timestamp DESC, id DESC) + WHERE vehicle_image_queue_kind = 'overview' AND vehicle_image_path IS NULL; + +INSERT INTO public.schema_migrations(version,description) VALUES + ('2026080801_daytime_overview_vehicle_views','Ingest daytime Blue Iris overview candidates and conservatively associate primary or driveway-fallback Vehicle Views by camera timing and direction.') +ON CONFLICT(version) DO NOTHING; diff --git a/migrations/0036_2026080901_overview_primary_claim_safety.sql b/migrations/0036_2026080901_overview_primary_claim_safety.sql new file mode 100644 index 0000000..a4d6415 --- /dev/null +++ b/migrations/0036_2026080901_overview_primary_claim_safety.sql @@ -0,0 +1,32 @@ +-- migration: 2026080901_overview_primary_claim_safety +-- Primary Street Overview retrieval is read-owned and may outlive the worker +-- process which originally claimed it. A per-claim token prevents a reclaimed +-- worker from overwriting a newer result, while next_attempt_at supports one +-- short, bounded retry when a fresh Blue Iris recording is still finalizing. +ALTER TABLE public.plate_reads + ADD COLUMN IF NOT EXISTS vehicle_image_claim_token UUID, + ADD COLUMN IF NOT EXISTS vehicle_image_next_attempt_at TIMESTAMPTZ; + +ALTER TABLE public.vehicle_overview_pair_profiles + DROP CONSTRAINT IF EXISTS vehicle_overview_primary_tolerance_ms_check; +ALTER TABLE public.vehicle_overview_pair_profiles + ADD CONSTRAINT vehicle_overview_primary_tolerance_ms_check CHECK ( + source_role <> 'primary' OR tolerance_ms <= 3000 + ) NOT VALID; + +ALTER TABLE public.vehicle_overview_pair_profiles + DROP CONSTRAINT IF EXISTS vehicle_overview_distinct_camera_check; +ALTER TABLE public.vehicle_overview_pair_profiles + ADD CONSTRAINT vehicle_overview_distinct_camera_check CHECK ( + LOWER(BTRIM(source_camera_name)) <> LOWER(BTRIM(plate_camera_name)) + ) NOT VALID; + +CREATE INDEX IF NOT EXISTS idx_plate_reads_overview_retry_ready + ON public.plate_reads (vehicle_image_next_attempt_at, timestamp DESC, id DESC) + WHERE vehicle_image_queue_kind = 'overview' + AND vehicle_image_status = 'failed' + AND vehicle_image_retryable = TRUE; + +INSERT INTO public.schema_migrations(version,description) VALUES + ('2026080901_overview_primary_claim_safety','Add token-guarded profile-aware Street Overview claims, bounded recording-finalization retries, and a primary timing tolerance compatible with the six-second sample window.') +ON CONFLICT(version) DO NOTHING; diff --git a/migrations/0037_2026080902_blue_iris_timeline_exports.sql b/migrations/0037_2026080902_blue_iris_timeline_exports.sql new file mode 100644 index 0000000..1db209f --- /dev/null +++ b/migrations/0037_2026080902_blue_iris_timeline_exports.sql @@ -0,0 +1,60 @@ +-- migration: 2026080902_blue_iris_timeline_exports +-- Timeline exports replace sixty-one sequential Blue Iris /time requests for +-- primary Street Overview work. The export ledger owns only API-generated +-- temporary files. Blue Iris owns Clipboard retention; ALPR never requests +-- deletion of remote exports and removes only its downloaded local workspace. +ALTER TABLE public.plate_reads + ADD COLUMN IF NOT EXISTS vehicle_image_heartbeat_at TIMESTAMPTZ, + ADD COLUMN IF NOT EXISTS vehicle_image_processing_deadline_at TIMESTAMPTZ, + ADD COLUMN IF NOT EXISTS vehicle_image_hard_deadline_at TIMESTAMPTZ; + +CREATE TABLE IF NOT EXISTS public.blue_iris_timeline_exports ( + id BIGSERIAL PRIMARY KEY, + export_token UUID NOT NULL UNIQUE, + read_id INTEGER NOT NULL REFERENCES public.plate_reads(id) ON DELETE CASCADE, + claim_token UUID NOT NULL, + source_camera_name VARCHAR(120) NOT NULL CHECK (BTRIM(source_camera_name) <> ''), + requested_start_at TIMESTAMPTZ NOT NULL, + requested_duration_ms INTEGER NOT NULL CHECK (requested_duration_ms BETWEEN 1000 AND 60000), + remote_path TEXT, + remote_uri TEXT, + remote_status TEXT, + remote_utc_ms BIGINT, + remote_duration_ms INTEGER CHECK (remote_duration_ms IS NULL OR remote_duration_ms > 0), + status VARCHAR(24) NOT NULL CHECK ( + status IN ('starting','exporting','ready','downloaded','delete_pending','deleting','deleted','failed') + ), + progress SMALLINT CHECK (progress IS NULL OR progress BETWEEN 0 AND 100), + file_size_bytes BIGINT CHECK (file_size_bytes IS NULL OR file_size_bytes >= 0), + video_width INTEGER CHECK (video_width IS NULL OR video_width > 0), + video_height INTEGER CHECK (video_height IS NULL OR video_height > 0), + media_duration_ms INTEGER CHECK (media_duration_ms IS NULL OR media_duration_ms > 0), + error_code VARCHAR(80), + error_details JSONB, + delete_attempt_count SMALLINT NOT NULL DEFAULT 0 CHECK (delete_attempt_count BETWEEN 0 AND 20), + next_delete_attempt_at TIMESTAMPTZ, + hard_deadline_at TIMESTAMPTZ NOT NULL DEFAULT (CURRENT_TIMESTAMP + INTERVAL '5 minutes'), + downloaded_at TIMESTAMPTZ, + deleted_at TIMESTAMPTZ, + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + UNIQUE (read_id, claim_token) +); + +CREATE INDEX IF NOT EXISTS idx_blue_iris_timeline_exports_cleanup + ON public.blue_iris_timeline_exports (next_delete_attempt_at, updated_at, id) + WHERE remote_path IS NOT NULL AND deleted_at IS NULL; + +CREATE INDEX IF NOT EXISTS idx_plate_reads_overview_claim_heartbeat + ON public.plate_reads ( + vehicle_image_heartbeat_at, + vehicle_image_processing_deadline_at, + vehicle_image_hard_deadline_at, + timestamp, + id + ) + WHERE vehicle_image_queue_kind = 'overview' AND vehicle_image_status = 'processing'; + +INSERT INTO public.schema_migrations(version,description) VALUES + ('2026080902_blue_iris_timeline_exports','Track read-owned temporary Blue Iris timeline exports and bound overview processing with heartbeats and deadlines.') +ON CONFLICT(version) DO NOTHING; diff --git a/migrations/0038_2026080903_blue_iris_clipboard_retention.sql b/migrations/0038_2026080903_blue_iris_clipboard_retention.sql new file mode 100644 index 0000000..afbaa86 --- /dev/null +++ b/migrations/0038_2026080903_blue_iris_clipboard_retention.sql @@ -0,0 +1,21 @@ +-- migration: 2026080903_blue_iris_clipboard_retention +-- Retire the earlier remote-delete workflow without removing its additive +-- compatibility columns. Completed downloads become terminal ledger entries; +-- Blue Iris removes Clipboard exports according to its configured retention. +UPDATE public.blue_iris_timeline_exports +SET status = CASE WHEN downloaded_at IS NOT NULL THEN 'downloaded' ELSE 'failed' END, + error_code = CASE + WHEN downloaded_at IS NOT NULL AND error_code = 'EXPORT_DELETE_FAILED' THEN NULL + ELSE error_code + END, + error_details = CASE + WHEN downloaded_at IS NOT NULL AND error_code = 'EXPORT_DELETE_FAILED' THEN NULL + ELSE error_details + END, + next_delete_attempt_at = NULL, + updated_at = CURRENT_TIMESTAMP +WHERE status IN ('delete_pending', 'deleting'); + +INSERT INTO public.schema_migrations(version,description) VALUES + ('2026080903_blue_iris_clipboard_retention','Stop ALPR remote-delete attempts, leave Blue Iris Clipboard retention authoritative, and normalize earlier delete-pending export rows.') +ON CONFLICT(version) DO NOTHING; diff --git a/migrations/0039_2026081001_overview_export_idempotency.sql b/migrations/0039_2026081001_overview_export_idempotency.sql new file mode 100644 index 0000000..99a3671 --- /dev/null +++ b/migrations/0039_2026081001_overview_export_idempotency.sql @@ -0,0 +1,74 @@ +-- migration: 2026081001_overview_export_idempotency +-- Overview processing must not use PostgreSQL timestamps as compare-and-swap +-- revisions because the JavaScript Date representation loses microseconds. +-- Timeline exports also need a stable identity which survives claim renewal, +-- retries, and worker restarts. Existing ledger rows represent an export which +-- was already requested, so they are conservatively marked as dispatched. +ALTER TABLE public.vehicle_overview_pair_profiles + ADD COLUMN IF NOT EXISTS revision BIGINT NOT NULL DEFAULT 1; + +ALTER TABLE public.vehicle_overview_pair_profiles + DROP CONSTRAINT IF EXISTS vehicle_overview_pair_profiles_revision_check; +ALTER TABLE public.vehicle_overview_pair_profiles + ADD CONSTRAINT vehicle_overview_pair_profiles_revision_check + CHECK (revision > 0) NOT VALID; + +ALTER TABLE public.blue_iris_timeline_exports + ADD COLUMN IF NOT EXISTS export_key CHAR(64), + ADD COLUMN IF NOT EXISTS pair_profile_id BIGINT, + ADD COLUMN IF NOT EXISTS profile_revision BIGINT, + ADD COLUMN IF NOT EXISTS algorithm_revision VARCHAR(80), + ADD COLUMN IF NOT EXISTS automatic_start_count SMALLINT NOT NULL DEFAULT 0, + ADD COLUMN IF NOT EXISTS start_requested_at TIMESTAMPTZ, + ADD COLUMN IF NOT EXISTS last_checked_at TIMESTAMPTZ, + ADD COLUMN IF NOT EXISTS preexisting_remote_paths JSONB NOT NULL DEFAULT '[]'::jsonb, + ADD COLUMN IF NOT EXISTS legacy_imported BOOLEAN NOT NULL DEFAULT FALSE; + +ALTER TABLE public.plate_reads + ADD COLUMN IF NOT EXISTS vehicle_image_recovery_count SMALLINT NOT NULL DEFAULT 0, + ADD COLUMN IF NOT EXISTS vehicle_image_last_recovered_at TIMESTAMPTZ; + +UPDATE public.blue_iris_timeline_exports +SET automatic_start_count = 1, + legacy_imported = TRUE, + start_requested_at = COALESCE(start_requested_at, created_at), + updated_at = CURRENT_TIMESTAMP +WHERE export_key IS NULL AND automatic_start_count = 0; + +ALTER TABLE public.blue_iris_timeline_exports + DROP CONSTRAINT IF EXISTS blue_iris_timeline_exports_automatic_start_count_check; +ALTER TABLE public.blue_iris_timeline_exports + ADD CONSTRAINT blue_iris_timeline_exports_automatic_start_count_check + CHECK (automatic_start_count BETWEEN 0 AND 1) NOT VALID; + +-- pair_profile_id is an immutable historical snapshot, not a live relation. +-- Deleting or editing a profile must not mutate an in-flight export identity. +ALTER TABLE public.blue_iris_timeline_exports + DROP CONSTRAINT IF EXISTS blue_iris_timeline_exports_pair_profile_fkey; +ALTER TABLE public.blue_iris_timeline_exports + DROP CONSTRAINT IF EXISTS blue_iris_timeline_exports_pair_profile_id_fkey; + +CREATE UNIQUE INDEX IF NOT EXISTS idx_blue_iris_timeline_exports_stable_key + ON public.blue_iris_timeline_exports (export_key) + WHERE export_key IS NOT NULL; + +CREATE INDEX IF NOT EXISTS idx_blue_iris_timeline_exports_read_resume + ON public.blue_iris_timeline_exports ( + read_id, source_camera_name, requested_start_at, requested_duration_ms, id + ); + +ALTER TABLE public.blue_iris_timeline_exports + DROP CONSTRAINT IF EXISTS blue_iris_timeline_exports_preexisting_paths_check; +ALTER TABLE public.blue_iris_timeline_exports + ADD CONSTRAINT blue_iris_timeline_exports_preexisting_paths_check + CHECK (jsonb_typeof(preexisting_remote_paths) = 'array') NOT VALID; + +ALTER TABLE public.plate_reads + DROP CONSTRAINT IF EXISTS plate_reads_vehicle_image_recovery_count_check; +ALTER TABLE public.plate_reads + ADD CONSTRAINT plate_reads_vehicle_image_recovery_count_check + CHECK (vehicle_image_recovery_count BETWEEN 0 AND 20) NOT VALID; + +INSERT INTO public.schema_migrations(version,description) VALUES + ('2026081001_overview_export_idempotency','Use integer overview-profile revisions and stable at-most-once Blue Iris timeline-export identities across retries and worker restarts.') +ON CONFLICT(version) DO NOTHING; diff --git a/migrations/0040_2026081002_street_overview_pair_sharing.sql b/migrations/0040_2026081002_street_overview_pair_sharing.sql new file mode 100644 index 0000000..e7c83e4 --- /dev/null +++ b/migrations/0040_2026081002_street_overview_pair_sharing.sql @@ -0,0 +1,95 @@ +-- migration: 2026081002_street_overview_pair_sharing +-- A validated primary Street Overview frame may be copied to one uniquely +-- matching companion Street LPR read. New installations begin in shadow mode: +-- matches are recorded for review, but no read or file is changed until an +-- administrator explicitly enables active sharing. +ALTER TABLE public.plate_reads + ADD COLUMN IF NOT EXISTS vehicle_image_source_read_id INTEGER; + +DO $$ +BEGIN + IF NOT EXISTS ( + SELECT 1 FROM pg_constraint + WHERE conname = 'plate_reads_vehicle_image_source_read_id_fkey' + ) THEN + ALTER TABLE public.plate_reads + ADD CONSTRAINT plate_reads_vehicle_image_source_read_id_fkey + FOREIGN KEY (vehicle_image_source_read_id) + REFERENCES public.plate_reads(id) ON DELETE SET NULL; + END IF; +END $$; + +ALTER TABLE public.plate_reads + DROP CONSTRAINT IF EXISTS plate_reads_vehicle_image_source_kind_check; +ALTER TABLE public.plate_reads + ADD CONSTRAINT plate_reads_vehicle_image_source_kind_check CHECK ( + vehicle_image_source_kind IS NULL OR + vehicle_image_source_kind IN ( + 'legacy_plate_camera','overview_primary','entry_overview_primary', + 'overview_fallback','overview_pair_share','entry_lpr_fallback', + 'entry_overview_route_fallback','entry_overview_history' + ) + ) NOT VALID; + +CREATE TABLE IF NOT EXISTS public.vehicle_overview_pair_sharing_settings ( + singleton BOOLEAN PRIMARY KEY DEFAULT TRUE CHECK (singleton), + mode VARCHAR(12) NOT NULL DEFAULT 'shadow' + CHECK (mode IN ('off','shadow','active')), + observation_started_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + updated_by_user_id BIGINT REFERENCES public.users(id) ON DELETE SET NULL, + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP +); + +INSERT INTO public.vehicle_overview_pair_sharing_settings (singleton, mode) +VALUES (TRUE, 'shadow') +ON CONFLICT (singleton) DO NOTHING; + +CREATE TABLE IF NOT EXISTS public.vehicle_overview_read_shares ( + id BIGSERIAL PRIMARY KEY, + decision_identity CHAR(64) NOT NULL UNIQUE, + source_read_id INTEGER REFERENCES public.plate_reads(id) ON DELETE SET NULL, + target_read_id INTEGER NOT NULL UNIQUE REFERENCES public.plate_reads(id) ON DELETE CASCADE, + status VARCHAR(16) NOT NULL CHECK ( + status IN ('proposed','processing','applied','rejected','failed') + ), + decision_reason VARCHAR(80) NOT NULL, + plate_number_snapshot VARCHAR(32), + direction_label_snapshot VARCHAR(100), + source_camera_name_snapshot VARCHAR(120), + target_camera_name_snapshot VARCHAR(120), + overview_camera_name_snapshot VARCHAR(120), + source_profile_id BIGINT, + source_profile_revision BIGINT, + target_profile_id BIGINT, + target_profile_revision BIGINT, + source_image_path_snapshot TEXT, + source_anchor_at TIMESTAMPTZ, + target_anchor_at TIMESTAMPTZ, + anchor_delta_ms INTEGER, + decision_metadata JSONB NOT NULL DEFAULT '{}'::jsonb, + claim_token UUID, + attempt_count SMALLINT NOT NULL DEFAULT 0 CHECK (attempt_count BETWEEN 0 AND 3), + target_image_path TEXT, + error_code VARCHAR(80), + error_details JSONB, + applied_at TIMESTAMPTZ, + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP +); + +CREATE INDEX IF NOT EXISTS idx_vehicle_overview_read_shares_claim + ON public.vehicle_overview_read_shares (status, created_at, id) + WHERE status IN ('proposed','processing'); + +CREATE INDEX IF NOT EXISTS idx_vehicle_overview_read_shares_source + ON public.vehicle_overview_read_shares (source_read_id, status, id) + WHERE source_read_id IS NOT NULL; + +CREATE UNIQUE INDEX IF NOT EXISTS idx_vehicle_overview_read_shares_unique_live_source + ON public.vehicle_overview_read_shares (source_read_id) + WHERE source_read_id IS NOT NULL AND status IN ('proposed','processing','applied'); + +INSERT INTO public.schema_migrations(version,description) VALUES + ('2026081002_street_overview_pair_sharing','Add shadow-first, one-to-one Street LPR companion sharing with independent image files and durable provenance.') +ON CONFLICT(version) DO NOTHING; diff --git a/migrations/0041_2026081003_live_feed_page_first.sql b/migrations/0041_2026081003_live_feed_page_first.sql new file mode 100644 index 0000000..bca1c69 --- /dev/null +++ b/migrations/0041_2026081003_live_feed_page_first.sql @@ -0,0 +1,10 @@ +-- migration: 2026081003_live_feed_page_first +-- Live Feed must page the inexpensive read identity set before joining tags, +-- vehicle evidence, and other presentation data. This composite order index +-- supports its default newest-first page without sorting the full read history. +CREATE INDEX IF NOT EXISTS idx_plate_reads_live_feed_timestamp + ON public.plate_reads ("timestamp" DESC, id DESC); + +INSERT INTO public.schema_migrations(version,description) VALUES + ('2026081003_live_feed_page_first','Page Live Feed read identities before presentation joins and support newest-first lookup with a composite index.') +ON CONFLICT(version) DO NOTHING; diff --git a/migrations/0042_2026081004_entry_lpr_route_fallback.sql b/migrations/0042_2026081004_entry_lpr_route_fallback.sql new file mode 100644 index 0000000..d5ef41d --- /dev/null +++ b/migrations/0042_2026081004_entry_lpr_route_fallback.sql @@ -0,0 +1,122 @@ +-- migration: 2026081004_entry_lpr_route_fallback +-- Gate LPR fallback is a read-to-read association. It never consumes a Blue +-- Iris alert and never creates a synthetic Street read. New installations start +-- in shadow mode so route matches can be reviewed before copied images are +-- allowed to fill a failed Street Overview result. +ALTER TABLE public.plate_reads + DROP CONSTRAINT IF EXISTS plate_reads_vehicle_image_source_kind_check; +ALTER TABLE public.plate_reads + ADD CONSTRAINT plate_reads_vehicle_image_source_kind_check CHECK ( + vehicle_image_source_kind IS NULL OR + vehicle_image_source_kind IN ( + 'legacy_plate_camera','overview_primary','entry_overview_primary','overview_fallback', + 'overview_pair_share','entry_lpr_fallback','entry_overview_route_fallback', + 'entry_overview_history' + ) + ) NOT VALID; + +CREATE TABLE IF NOT EXISTS public.vehicle_entry_fallback_settings ( + singleton BOOLEAN PRIMARY KEY DEFAULT TRUE CHECK (singleton), + mode VARCHAR(12) NOT NULL DEFAULT 'shadow' + CHECK (mode IN ('off','shadow','active')), + observation_started_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + updated_by_user_id BIGINT REFERENCES public.users(id) ON DELETE SET NULL, + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP +); + +INSERT INTO public.vehicle_entry_fallback_settings (singleton, mode) +VALUES (TRUE, 'shadow') +ON CONFLICT (singleton) DO NOTHING; + +CREATE TABLE IF NOT EXISTS public.vehicle_entry_route_profiles ( + id BIGSERIAL PRIMARY KEY, + route_name VARCHAR(120) NOT NULL, + target_camera_name VARCHAR(120) NOT NULL, + target_camera_key VARCHAR(120) GENERATED ALWAYS AS (LOWER(BTRIM(target_camera_name))) STORED, + target_direction_label VARCHAR(100) NOT NULL, + target_direction_key VARCHAR(100) GENERATED ALWAYS AS (LOWER(BTRIM(target_direction_label))) STORED, + source_direction_label VARCHAR(100) NOT NULL, + source_camera_names TEXT[] NOT NULL, + expected_delta_ms INTEGER NOT NULL CHECK (expected_delta_ms BETWEEN -30000 AND 30000), + tolerance_ms INTEGER NOT NULL DEFAULT 3000 CHECK (tolerance_ms BETWEEN 250 AND 15000), + event_window_ms INTEGER NOT NULL DEFAULT 3000 CHECK (event_window_ms BETWEEN 250 AND 5000), + minimum_source_count SMALLINT NOT NULL DEFAULT 2 CHECK (minimum_source_count BETWEEN 2 AND 4), + priority SMALLINT NOT NULL DEFAULT 0 CHECK (priority BETWEEN 0 AND 100), + enabled BOOLEAN NOT NULL DEFAULT TRUE, + revision BIGINT NOT NULL DEFAULT 1 CHECK (revision > 0), + updated_by_user_id BIGINT REFERENCES public.users(id) ON DELETE SET NULL, + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + CONSTRAINT vehicle_entry_route_profiles_name_check CHECK (BTRIM(route_name) <> ''), + CONSTRAINT vehicle_entry_route_profiles_target_camera_check CHECK (BTRIM(target_camera_name) <> ''), + CONSTRAINT vehicle_entry_route_profiles_target_direction_check CHECK (BTRIM(target_direction_label) <> ''), + CONSTRAINT vehicle_entry_route_profiles_source_direction_check CHECK (BTRIM(source_direction_label) <> ''), + CONSTRAINT vehicle_entry_route_profiles_source_cameras_check CHECK ( + cardinality(source_camera_names) >= minimum_source_count + ), + UNIQUE (target_camera_key, target_direction_key) +); + +CREATE TABLE IF NOT EXISTS public.vehicle_entry_fallback_decisions ( + id BIGSERIAL PRIMARY KEY, + decision_identity CHAR(64) NOT NULL UNIQUE, + source_event_key CHAR(64), + route_profile_id BIGINT NOT NULL, + route_profile_revision BIGINT NOT NULL CHECK (route_profile_revision > 0), + target_read_id INTEGER NOT NULL REFERENCES public.plate_reads(id) ON DELETE CASCADE, + source_read_id INTEGER REFERENCES public.plate_reads(id) ON DELETE SET NULL, + corroborating_read_ids INTEGER[] NOT NULL DEFAULT '{}', + status VARCHAR(16) NOT NULL CHECK ( + status IN ('proposed','processing','applied','rejected','failed') + ), + decision_reason VARCHAR(100) NOT NULL, + route_name_snapshot VARCHAR(120) NOT NULL, + target_plate_snapshot VARCHAR(32), + target_camera_name_snapshot VARCHAR(120), + target_direction_label_snapshot VARCHAR(100), + source_direction_label_snapshot VARCHAR(100), + source_camera_names_snapshot TEXT[] NOT NULL DEFAULT '{}', + source_image_path_snapshot TEXT, + source_timestamp_snapshot TIMESTAMPTZ, + source_detection_confidence REAL CHECK ( + source_detection_confidence IS NULL OR source_detection_confidence BETWEEN 0 AND 1 + ), + source_detection_box JSONB, + source_image_width INTEGER CHECK (source_image_width IS NULL OR source_image_width > 0), + source_image_height INTEGER CHECK (source_image_height IS NULL OR source_image_height > 0), + plate_evidence_class VARCHAR(80), + expected_delta_ms INTEGER, + actual_delta_ms INTEGER, + timing_error_ms INTEGER, + decision_score REAL, + decision_margin REAL, + decision_metadata JSONB NOT NULL DEFAULT '{}'::jsonb, + claim_token UUID, + attempt_count SMALLINT NOT NULL DEFAULT 0 CHECK (attempt_count BETWEEN 0 AND 3), + target_image_path TEXT, + error_code VARCHAR(80), + error_details JSONB, + applied_at TIMESTAMPTZ, + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + UNIQUE (target_read_id, route_profile_id, route_profile_revision), + CONSTRAINT vehicle_entry_fallback_decisions_metadata_check CHECK ( + jsonb_typeof(decision_metadata) = 'object' + ) +); + +CREATE INDEX IF NOT EXISTS idx_vehicle_entry_fallback_decisions_claim + ON public.vehicle_entry_fallback_decisions (status, created_at, id) + WHERE status IN ('proposed','processing'); + +CREATE INDEX IF NOT EXISTS idx_vehicle_entry_fallback_decisions_target + ON public.vehicle_entry_fallback_decisions (target_read_id, created_at DESC, id DESC); + +CREATE UNIQUE INDEX IF NOT EXISTS idx_vehicle_entry_fallback_unique_live_event + ON public.vehicle_entry_fallback_decisions (source_event_key) + WHERE source_event_key IS NOT NULL AND status IN ('proposed','processing','applied'); + +INSERT INTO public.schema_migrations(version,description) VALUES + ('2026081004_entry_lpr_route_fallback','Add shadow-first, dual-camera Gate LPR read-to-read fallback for explicitly configured driveway routes.') +ON CONFLICT(version) DO NOTHING; diff --git a/migrations/0043_2026081005_entry_overview_primary.sql b/migrations/0043_2026081005_entry_overview_primary.sql new file mode 100644 index 0000000..e8f8024 --- /dev/null +++ b/migrations/0043_2026081005_entry_overview_primary.sql @@ -0,0 +1,71 @@ +-- migration: 2026081005_entry_overview_primary +-- Gate LPR reads may use the same read-owned continuous-recording pipeline as +-- Street LPR reads while retaining distinct provenance. Existing primary +-- profiles remain Street profiles until an administrator explicitly changes +-- their context. Street companion sharing continues to accept only the legacy +-- overview_primary result kind, so Gate Overview results cannot cross into it. +ALTER TABLE public.vehicle_overview_pair_profiles + ADD COLUMN IF NOT EXISTS overview_context VARCHAR(16) NOT NULL DEFAULT 'street', + ADD COLUMN IF NOT EXISTS source_camera_short_name VARCHAR(80); + +ALTER TABLE public.vehicle_overview_pair_profiles + DROP CONSTRAINT IF EXISTS vehicle_overview_pair_profiles_context_check; +ALTER TABLE public.vehicle_overview_pair_profiles + ADD CONSTRAINT vehicle_overview_pair_profiles_context_check CHECK ( + overview_context IN ('street','entry') + ) NOT VALID; + +ALTER TABLE public.vehicle_overview_pair_profiles + DROP CONSTRAINT IF EXISTS vehicle_overview_pair_profiles_entry_camera_binding_check; +ALTER TABLE public.vehicle_overview_pair_profiles + ADD CONSTRAINT vehicle_overview_pair_profiles_entry_camera_binding_check CHECK ( + overview_context <> 'entry' + OR source_role <> 'primary' + OR NULLIF(BTRIM(source_camera_short_name), '') IS NOT NULL + ) NOT VALID; + +DO $$ +DECLARE + duplicate_identity RECORD; +BEGIN + SELECT LOWER(BTRIM(plate_camera_name)) AS plate_camera_key, + LOWER(BTRIM(direction_label)) AS direction_key, + COUNT(*) AS profile_count + INTO duplicate_identity + FROM public.vehicle_overview_pair_profiles + WHERE enabled = TRUE AND source_role = 'primary' + GROUP BY LOWER(BTRIM(plate_camera_name)), LOWER(BTRIM(direction_label)) + HAVING COUNT(*) > 1 + ORDER BY COUNT(*) DESC + LIMIT 1; + + IF FOUND THEN + RAISE EXCEPTION + 'Duplicate enabled primary overview profiles for camera % and direction % (% rows). Disable the duplicate before retrying this migration.', + duplicate_identity.plate_camera_key, + duplicate_identity.direction_key, + duplicate_identity.profile_count; + END IF; +END $$; + +CREATE UNIQUE INDEX IF NOT EXISTS idx_vehicle_overview_primary_profile_identity + ON public.vehicle_overview_pair_profiles ( + LOWER(BTRIM(plate_camera_name)), LOWER(BTRIM(direction_label)) + ) + WHERE enabled = TRUE AND source_role = 'primary'; + +ALTER TABLE public.plate_reads + DROP CONSTRAINT IF EXISTS plate_reads_vehicle_image_source_kind_check; +ALTER TABLE public.plate_reads + ADD CONSTRAINT plate_reads_vehicle_image_source_kind_check CHECK ( + vehicle_image_source_kind IS NULL OR + vehicle_image_source_kind IN ( + 'legacy_plate_camera','overview_primary','entry_overview_primary', + 'overview_fallback','overview_pair_share','entry_lpr_fallback', + 'entry_overview_route_fallback','entry_overview_history' + ) + ) NOT VALID; + +INSERT INTO public.schema_migrations(version,description) VALUES + ('2026081005_entry_overview_primary','Add explicit Gate Overview primary profile context and distinct read-owned Vehicle View provenance.') +ON CONFLICT(version) DO NOTHING; diff --git a/migrations/0044_2026081006_entry_overview_history_backfill.sql b/migrations/0044_2026081006_entry_overview_history_backfill.sql new file mode 100644 index 0000000..af92c89 --- /dev/null +++ b/migrations/0044_2026081006_entry_overview_history_backfill.sql @@ -0,0 +1,280 @@ +-- migration: 2026081006_entry_overview_history_backfill +-- Direction-independent Gate Overview history is deliberately separate from +-- the live direction profiles. A run first materializes an immutable preview; +-- confirmation may queue only that exact preview. No migration statement +-- enqueues a read or changes an existing Vehicle View. +CREATE TABLE IF NOT EXISTS public.vehicle_entry_overview_history_profiles ( + id BIGSERIAL PRIMARY KEY, + profile_key CHAR(64) NOT NULL, + revision BIGINT NOT NULL CHECK (revision > 0), + profile_kind VARCHAR(32) NOT NULL DEFAULT 'entry_history' + CHECK (profile_kind = 'entry_history'), + source_kind VARCHAR(32) NOT NULL DEFAULT 'entry_overview_history' + CHECK (source_kind = 'entry_overview_history'), + overview_context VARCHAR(16) NOT NULL DEFAULT 'entry' + CHECK (overview_context = 'entry'), + source_camera_name VARCHAR(120) NOT NULL + CHECK (BTRIM(source_camera_name) = 'Gate Overview'), + source_camera_short_name VARCHAR(80) NOT NULL + CHECK (BTRIM(source_camera_short_name) = 'Overview01'), + plate_camera_name VARCHAR(120) NOT NULL CHECK ( + LOWER(BTRIM(plate_camera_name)) IN ('gate lpr 1','gate lpr 2') + ), + expected_delta_ms INTEGER NOT NULL CHECK (expected_delta_ms BETWEEN -30000 AND 30000), + tolerance_ms INTEGER NOT NULL DEFAULT 3000 CHECK (tolerance_ms = 3000), + algorithm_revision VARCHAR(80) NOT NULL CHECK (BTRIM(algorithm_revision) <> ''), + enabled BOOLEAN NOT NULL DEFAULT TRUE, + supersedes_profile_id BIGINT REFERENCES public.vehicle_entry_overview_history_profiles(id) ON DELETE RESTRICT, + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + disabled_at TIMESTAMPTZ, + UNIQUE (profile_key, revision), + CHECK ((enabled = TRUE AND disabled_at IS NULL) + OR (enabled = FALSE AND disabled_at IS NOT NULL)) +); + +CREATE UNIQUE INDEX IF NOT EXISTS idx_entry_overview_history_profile_enabled + ON public.vehicle_entry_overview_history_profiles (LOWER(BTRIM(plate_camera_name))) + WHERE enabled = TRUE; + +CREATE INDEX IF NOT EXISTS idx_entry_overview_history_profile_versions + ON public.vehicle_entry_overview_history_profiles (profile_key, revision DESC, id DESC); + +CREATE OR REPLACE FUNCTION public.guard_entry_overview_history_profile_snapshot() +RETURNS trigger LANGUAGE plpgsql AS $$ +BEGIN + IF ROW( + NEW.profile_key, NEW.revision, NEW.profile_kind, NEW.source_kind, + NEW.overview_context, NEW.source_camera_name, NEW.source_camera_short_name, + NEW.plate_camera_name, NEW.expected_delta_ms, NEW.tolerance_ms, + NEW.algorithm_revision, NEW.supersedes_profile_id, NEW.created_at + ) IS DISTINCT FROM ROW( + OLD.profile_key, OLD.revision, OLD.profile_kind, OLD.source_kind, + OLD.overview_context, OLD.source_camera_name, OLD.source_camera_short_name, + OLD.plate_camera_name, OLD.expected_delta_ms, OLD.tolerance_ms, + OLD.algorithm_revision, OLD.supersedes_profile_id, OLD.created_at + ) THEN + RAISE EXCEPTION 'Gate Overview history profile snapshots are immutable; create a new revision.'; + END IF; + IF OLD.enabled = FALSE AND ( + NEW.enabled IS DISTINCT FROM OLD.enabled + OR NEW.disabled_at IS DISTINCT FROM OLD.disabled_at + ) THEN + RAISE EXCEPTION 'Disabled Gate Overview history profile snapshots cannot be revived or changed.'; + END IF; + IF OLD.enabled = TRUE AND NOT ( + (NEW.enabled = TRUE AND NEW.disabled_at IS NULL) + OR (NEW.enabled = FALSE AND NEW.disabled_at IS NOT NULL) + ) THEN + RAISE EXCEPTION 'Gate Overview history profile retirement must be one-way and timestamped.'; + END IF; + RETURN NEW; +END $$; + +DROP TRIGGER IF EXISTS trg_guard_entry_overview_history_profile_snapshot + ON public.vehicle_entry_overview_history_profiles; +CREATE TRIGGER trg_guard_entry_overview_history_profile_snapshot +BEFORE UPDATE ON public.vehicle_entry_overview_history_profiles +FOR EACH ROW EXECUTE FUNCTION public.guard_entry_overview_history_profile_snapshot(); + +ALTER TABLE public.vehicle_entry_overview_history_profiles + DROP CONSTRAINT IF EXISTS vehicle_entry_overview_history_profiles_check; +ALTER TABLE public.vehicle_entry_overview_history_profiles + ADD CONSTRAINT vehicle_entry_overview_history_profiles_check CHECK ( + (enabled = TRUE AND disabled_at IS NULL) + OR (enabled = FALSE AND disabled_at IS NOT NULL) + ) NOT VALID; + +CREATE TABLE IF NOT EXISTS public.vehicle_entry_overview_backfill_runs ( + id BIGSERIAL PRIMARY KEY, + scope_key CHAR(64) NOT NULL, + preview_fingerprint CHAR(64) NOT NULL, + status VARCHAR(16) NOT NULL DEFAULT 'previewed' CHECK ( + status IN ('previewed','running','paused','completed','cancelled') + ), + start_at TIMESTAMPTZ NOT NULL, + end_at TIMESTAMPTZ NOT NULL, + plate_camera_names JSONB NOT NULL, + profile_snapshot JSONB NOT NULL, + daylight_provider VARCHAR(80) NOT NULL, + daylight_model VARCHAR(80) NOT NULL, + algorithm_revision VARCHAR(80) NOT NULL, + batch_size INTEGER NOT NULL CHECK (batch_size BETWEEN 1 AND 500), + confirmed_at TIMESTAMPTZ, + paused_at TIMESTAMPTZ, + cancelled_at TIMESTAMPTZ, + completed_at TIMESTAMPTZ, + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + CHECK (end_at > start_at), + CHECK (jsonb_typeof(plate_camera_names) = 'array'), + CHECK (jsonb_typeof(profile_snapshot) = 'array') +); + +CREATE TABLE IF NOT EXISTS public.vehicle_entry_overview_backfill_jobs ( + id BIGSERIAL PRIMARY KEY, + run_id BIGINT NOT NULL REFERENCES public.vehicle_entry_overview_backfill_runs(id) ON DELETE RESTRICT, + read_id INTEGER NOT NULL REFERENCES public.plate_reads(id) ON DELETE RESTRICT, + semantic_key CHAR(64) NOT NULL, + profile_id BIGINT NOT NULL REFERENCES public.vehicle_entry_overview_history_profiles(id) ON DELETE RESTRICT, + profile_key CHAR(64) NOT NULL, + profile_revision BIGINT NOT NULL CHECK (profile_revision > 0), + profile_kind VARCHAR(32) NOT NULL DEFAULT 'entry_history' CHECK (profile_kind = 'entry_history'), + source_kind VARCHAR(32) NOT NULL DEFAULT 'entry_overview_history' + CHECK (source_kind = 'entry_overview_history'), + overview_context VARCHAR(16) NOT NULL DEFAULT 'entry' CHECK (overview_context = 'entry'), + source_camera_name VARCHAR(120) NOT NULL CHECK (BTRIM(source_camera_name) = 'Gate Overview'), + source_camera_short_name VARCHAR(80) NOT NULL CHECK (BTRIM(source_camera_short_name) = 'Overview01'), + plate_camera_name VARCHAR(120) NOT NULL, + read_timestamp TIMESTAMPTZ NOT NULL, + anchor_at TIMESTAMPTZ NOT NULL, + expected_delta_ms INTEGER NOT NULL, + tolerance_ms INTEGER NOT NULL DEFAULT 3000 CHECK (tolerance_ms = 3000), + algorithm_revision VARCHAR(80) NOT NULL, + daylight_provider VARCHAR(80) NOT NULL, + daylight_model VARCHAR(80) NOT NULL, + daylight_status VARCHAR(24) NOT NULL CHECK ( + daylight_status IN ('eligible','needs_preflight','nighttime','unverified','live_busy','preserved') + ), + daylight_evidence JSONB NOT NULL DEFAULT '{}'::jsonb, + status VARCHAR(20) NOT NULL DEFAULT 'previewed' CHECK ( + status IN ('previewed','queued','processing','ready','failed','unavailable','cancelled','superseded') + ), + attempt_count SMALLINT NOT NULL DEFAULT 0 CHECK (attempt_count BETWEEN 0 AND 2), + retryable BOOLEAN NOT NULL DEFAULT FALSE, + claim_token UUID, + heartbeat_at TIMESTAMPTZ, + processing_deadline_at TIMESTAMPTZ, + hard_deadline_at TIMESTAMPTZ, + next_attempt_at TIMESTAMPTZ, + error_code VARCHAR(80), + error_details JSONB, + operator_retry_count SMALLINT NOT NULL DEFAULT 0 CHECK (operator_retry_count BETWEEN 0 AND 1), + operator_retry_at TIMESTAMPTZ, + operator_retry_error_code VARCHAR(80), + prior_state_fingerprint CHAR(64) NOT NULL, + prior_image_path TEXT, + prior_image_status VARCHAR(20), + prior_queue_kind VARCHAR(24), + prior_attempt_count SMALLINT, + prior_retryable BOOLEAN, + prior_error_code VARCHAR(80), + prior_source_kind VARCHAR(32), + prior_overview_candidate_id BIGINT, + prior_source_read_id INTEGER, + prior_image_timestamp TIMESTAMPTZ, + prior_image_score REAL, + prior_detection_confidence REAL, + prior_detection_box JSONB, + prior_image_width INTEGER, + prior_image_height INTEGER, + prior_sampled_count SMALLINT, + prior_selection_metadata JSONB, + confirmed_at TIMESTAMPTZ, + ready_at TIMESTAMPTZ, + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + UNIQUE (run_id, read_id), + CHECK (jsonb_typeof(daylight_evidence) = 'object'), + CHECK (error_details IS NULL OR jsonb_typeof(error_details) = 'object'), + CHECK (prior_detection_box IS NULL OR jsonb_typeof(prior_detection_box) = 'object'), + CHECK (prior_selection_metadata IS NULL OR jsonb_typeof(prior_selection_metadata) = 'object') +); + +-- Keep migration replay safe if a development snapshot created this new table +-- before the complete provenance snapshot was added. +ALTER TABLE public.vehicle_entry_overview_backfill_jobs + ADD COLUMN IF NOT EXISTS prior_overview_candidate_id BIGINT, + ADD COLUMN IF NOT EXISTS prior_source_read_id INTEGER, + ADD COLUMN IF NOT EXISTS operator_retry_count SMALLINT NOT NULL DEFAULT 0, + ADD COLUMN IF NOT EXISTS operator_retry_at TIMESTAMPTZ, + ADD COLUMN IF NOT EXISTS operator_retry_error_code VARCHAR(80); + +ALTER TABLE public.vehicle_entry_overview_backfill_jobs + DROP CONSTRAINT IF EXISTS vehicle_entry_overview_backfill_jobs_operator_retry_count_check; +ALTER TABLE public.vehicle_entry_overview_backfill_jobs + ADD CONSTRAINT vehicle_entry_overview_backfill_jobs_operator_retry_count_check CHECK ( + operator_retry_count BETWEEN 0 AND 1 + ) NOT VALID; + +CREATE INDEX IF NOT EXISTS idx_entry_overview_backfill_jobs_claim + ON public.vehicle_entry_overview_backfill_jobs (read_timestamp, id) + WHERE status IN ('queued','processing','failed'); + +CREATE INDEX IF NOT EXISTS idx_entry_overview_backfill_jobs_run + ON public.vehicle_entry_overview_backfill_jobs (run_id, status, read_timestamp, id); + +ALTER TABLE public.vehicle_entry_overview_backfill_runs + DROP CONSTRAINT IF EXISTS vehicle_entry_overview_backfill_runs_scope_key_key; +ALTER TABLE public.vehicle_entry_overview_backfill_runs + DROP CONSTRAINT IF EXISTS vehicle_entry_overview_backfill_runs_preview_fingerprint_key; +ALTER TABLE public.vehicle_entry_overview_backfill_jobs + DROP CONSTRAINT IF EXISTS vehicle_entry_overview_backfill_jobs_semantic_key_key; + +CREATE UNIQUE INDEX IF NOT EXISTS idx_entry_overview_backfill_active_scope + ON public.vehicle_entry_overview_backfill_runs (scope_key) + WHERE status <> 'cancelled'; + +CREATE UNIQUE INDEX IF NOT EXISTS idx_entry_overview_backfill_active_preview + ON public.vehicle_entry_overview_backfill_runs (preview_fingerprint) + WHERE status <> 'cancelled'; + +CREATE INDEX IF NOT EXISTS idx_entry_overview_backfill_jobs_semantic + ON public.vehicle_entry_overview_backfill_jobs (semantic_key, id); + +ALTER TABLE public.vehicle_entry_overview_backfill_jobs + DROP CONSTRAINT IF EXISTS vehicle_entry_overview_backfill_jobs_daylight_status_check; +ALTER TABLE public.vehicle_entry_overview_backfill_jobs + ADD CONSTRAINT vehicle_entry_overview_backfill_jobs_daylight_status_check CHECK ( + daylight_status IN ('eligible','needs_preflight','nighttime','unverified','live_busy','preserved') + ) NOT VALID; + +ALTER TABLE public.plate_reads + ADD COLUMN IF NOT EXISTS vehicle_image_backfill_job_id BIGINT; + +DO $$ BEGIN + IF NOT EXISTS ( + SELECT 1 FROM pg_constraint + WHERE conname = 'plate_reads_vehicle_image_backfill_job_fkey' + AND conrelid = 'public.plate_reads'::regclass + ) THEN + ALTER TABLE public.plate_reads + ADD CONSTRAINT plate_reads_vehicle_image_backfill_job_fkey + FOREIGN KEY (vehicle_image_backfill_job_id) + REFERENCES public.vehicle_entry_overview_backfill_jobs(id) ON DELETE SET NULL; + END IF; +END $$; + +ALTER TABLE public.blue_iris_timeline_exports + ADD COLUMN IF NOT EXISTS profile_kind VARCHAR(32), + ADD COLUMN IF NOT EXISTS profile_identity CHAR(64); + +ALTER TABLE public.blue_iris_timeline_exports + DROP CONSTRAINT IF EXISTS blue_iris_timeline_exports_profile_kind_check; +ALTER TABLE public.blue_iris_timeline_exports + ADD CONSTRAINT blue_iris_timeline_exports_profile_kind_check CHECK ( + profile_kind IS NULL OR profile_kind IN ('pair','entry_history') + ) NOT VALID; + +ALTER TABLE public.plate_reads + DROP CONSTRAINT IF EXISTS plate_reads_vehicle_image_source_kind_check; +ALTER TABLE public.plate_reads + ADD CONSTRAINT plate_reads_vehicle_image_source_kind_check CHECK ( + vehicle_image_source_kind IS NULL OR + vehicle_image_source_kind IN ( + 'legacy_plate_camera','overview_primary','entry_overview_primary', + 'overview_fallback','overview_pair_share','entry_lpr_fallback', + 'entry_overview_route_fallback','entry_overview_history' + ) + ) NOT VALID; + +ALTER TABLE public.plate_reads + DROP CONSTRAINT IF EXISTS plate_reads_vehicle_image_queue_kind_check; +ALTER TABLE public.plate_reads + ADD CONSTRAINT plate_reads_vehicle_image_queue_kind_check CHECK ( + vehicle_image_queue_kind IS NULL OR + vehicle_image_queue_kind IN ('live','historical','manual','overview','overview_backfill','overview_repair') + ) NOT VALID; + +INSERT INTO public.schema_migrations(version,description) VALUES + ('2026081006_entry_overview_history_backfill','Add immutable direction-independent Gate Overview history profiles and preview-first, bounded, resumable backfill runs without auto-queueing reads.') +ON CONFLICT(version) DO NOTHING; diff --git a/migrations/0045_2026081201_entry_overview_route_payload.sql b/migrations/0045_2026081201_entry_overview_route_payload.sql new file mode 100644 index 0000000..014a5ca --- /dev/null +++ b/migrations/0045_2026081201_entry_overview_route_payload.sql @@ -0,0 +1,94 @@ +-- migration: 2026081201_entry_overview_route_payload +-- Route fallback v2 keeps the two Gate LPR reads as immutable identity and +-- timing evidence, but may copy only an already validated Gate Overview +-- (Overview01) result owned by one of those exact reads. The independent payload +-- gate starts in shadow even when the older route matcher was already active. +ALTER TABLE public.vehicle_entry_fallback_settings + ADD COLUMN IF NOT EXISTS overview_payload_mode VARCHAR(12) NOT NULL DEFAULT 'shadow'; + +ALTER TABLE public.vehicle_entry_fallback_settings + DROP CONSTRAINT IF EXISTS vehicle_entry_fallback_settings_overview_payload_mode_check; +ALTER TABLE public.vehicle_entry_fallback_settings + ADD CONSTRAINT vehicle_entry_fallback_settings_overview_payload_mode_check CHECK ( + overview_payload_mode IN ('off','shadow','active') + ) NOT VALID; + +ALTER TABLE public.vehicle_entry_fallback_decisions + ADD COLUMN IF NOT EXISTS algorithm_revision VARCHAR(80) NOT NULL DEFAULT 'entry-lpr-route-fallback-v1', + ADD COLUMN IF NOT EXISTS payload_read_id INTEGER REFERENCES public.plate_reads(id) ON DELETE SET NULL, + ADD COLUMN IF NOT EXISTS payload_image_path_snapshot TEXT, + ADD COLUMN IF NOT EXISTS payload_source_kind_snapshot VARCHAR(40), + ADD COLUMN IF NOT EXISTS payload_timestamp_snapshot TIMESTAMPTZ, + ADD COLUMN IF NOT EXISTS payload_detection_confidence REAL, + ADD COLUMN IF NOT EXISTS payload_detection_box JSONB, + ADD COLUMN IF NOT EXISTS payload_image_width INTEGER, + ADD COLUMN IF NOT EXISTS payload_image_height INTEGER, + ADD COLUMN IF NOT EXISTS payload_score REAL, + ADD COLUMN IF NOT EXISTS payload_sampled_count SMALLINT, + ADD COLUMN IF NOT EXISTS payload_selection_metadata JSONB; + +DO $entry_fallback_old_unique$ +DECLARE + constraint_name TEXT; +BEGIN + FOR constraint_name IN + SELECT con.conname + FROM pg_constraint con + WHERE con.conrelid = 'public.vehicle_entry_fallback_decisions'::regclass + AND con.contype = 'u' + AND ( + SELECT array_agg(attribute.attname ORDER BY key_column.ordinality) + FROM unnest(con.conkey) WITH ORDINALITY key_column(attnum, ordinality) + JOIN pg_attribute attribute + ON attribute.attrelid = con.conrelid + AND attribute.attnum = key_column.attnum + ) = ARRAY['target_read_id','route_profile_id','route_profile_revision']::name[] + LOOP + EXECUTE format( + 'ALTER TABLE public.vehicle_entry_fallback_decisions DROP CONSTRAINT %I', + constraint_name + ); + END LOOP; +END +$entry_fallback_old_unique$; +CREATE UNIQUE INDEX IF NOT EXISTS idx_vehicle_entry_fallback_algorithm_route_target + ON public.vehicle_entry_fallback_decisions ( + target_read_id, route_profile_id, route_profile_revision, algorithm_revision + ); + +ALTER TABLE public.vehicle_entry_fallback_decisions + DROP CONSTRAINT IF EXISTS vehicle_entry_fallback_payload_confidence_check; +ALTER TABLE public.vehicle_entry_fallback_decisions + ADD CONSTRAINT vehicle_entry_fallback_payload_confidence_check CHECK ( + payload_detection_confidence IS NULL OR payload_detection_confidence BETWEEN 0 AND 1 + ) NOT VALID; +ALTER TABLE public.vehicle_entry_fallback_decisions + DROP CONSTRAINT IF EXISTS vehicle_entry_fallback_payload_dimensions_check; +ALTER TABLE public.vehicle_entry_fallback_decisions + ADD CONSTRAINT vehicle_entry_fallback_payload_dimensions_check CHECK ( + (payload_image_width IS NULL OR payload_image_width > 0) + AND (payload_image_height IS NULL OR payload_image_height > 0) + AND (payload_sampled_count IS NULL OR payload_sampled_count > 0) + ) NOT VALID; +ALTER TABLE public.vehicle_entry_fallback_decisions + DROP CONSTRAINT IF EXISTS vehicle_entry_fallback_payload_metadata_check; +ALTER TABLE public.vehicle_entry_fallback_decisions + ADD CONSTRAINT vehicle_entry_fallback_payload_metadata_check CHECK ( + payload_selection_metadata IS NULL OR jsonb_typeof(payload_selection_metadata) = 'object' + ) NOT VALID; + +ALTER TABLE public.plate_reads + DROP CONSTRAINT IF EXISTS plate_reads_vehicle_image_source_kind_check; +ALTER TABLE public.plate_reads + ADD CONSTRAINT plate_reads_vehicle_image_source_kind_check CHECK ( + vehicle_image_source_kind IS NULL OR + vehicle_image_source_kind IN ( + 'legacy_plate_camera','overview_primary','entry_overview_primary', + 'overview_fallback','overview_pair_share','entry_lpr_fallback', + 'entry_overview_route_fallback','entry_overview_history' + ) + ) NOT VALID; + +INSERT INTO public.schema_migrations(version,description) VALUES + ('2026081201_entry_overview_route_payload','Add shadow-gated Overview01 payload snapshots to dual-Entry-read route fallback without enabling or queuing work.') +ON CONFLICT(version) DO NOTHING; diff --git a/migrations/0046_2026081101_entry_overview_history_retry.sql b/migrations/0046_2026081101_entry_overview_history_retry.sql new file mode 100644 index 0000000..d284fa6 --- /dev/null +++ b/migrations/0046_2026081101_entry_overview_history_retry.sql @@ -0,0 +1,4 @@ +-- migration: 2026081101_entry_overview_history_retry +INSERT INTO public.schema_migrations(version,description) VALUES + ('2026081101_entry_overview_history_retry','Allow one audited, operator-authorized retry cycle for terminal transient Gate Overview history import failures while preserving existing views and export identity.') +ON CONFLICT(version) DO NOTHING; diff --git a/migrations/0047_2026081301_integration_ingress_receipts.sql b/migrations/0047_2026081301_integration_ingress_receipts.sql new file mode 100644 index 0000000..55e64f9 --- /dev/null +++ b/migrations/0047_2026081301_integration_ingress_receipts.sql @@ -0,0 +1,63 @@ +-- migration: 2026081301_integration_ingress_receipts +-- Bounded operational receipts for authenticated integration requests. These +-- rows intentionally contain request shape and outcome metadata only. Plate +-- values, request bodies, images, AI dumps, and Blue Iris paths are excluded. +CREATE TABLE IF NOT EXISTS public.integration_ingress_receipts ( + id BIGSERIAL PRIMARY KEY, + request_id VARCHAR(128) NOT NULL, + integration VARCHAR(64) NOT NULL, + route_name VARCHAR(128) NOT NULL, + method VARCHAR(12), + content_type VARCHAR(128), + body_bytes BIGINT CHECK (body_bytes IS NULL OR body_bytes >= 0), + body_sha256 CHAR(64) CHECK ( + body_sha256 IS NULL OR body_sha256 ~ '^[0-9a-f]{64}$' + ), + payload_keys TEXT[] NOT NULL DEFAULT ARRAY[]::TEXT[], + unknown_payload_key_count INTEGER NOT NULL DEFAULT 0 + CHECK (unknown_payload_key_count >= 0), + camera_name VARCHAR(100), + event_timestamp_text VARCHAR(128), + trigger_field VARCHAR(32), + trigger_present BOOLEAN NOT NULL DEFAULT FALSE, + trigger_value_state VARCHAR(16) NOT NULL DEFAULT 'absent' + CHECK (trigger_value_state IN ('absent','blank','invalid','recorded')), + trigger_type VARCHAR(128), + heavy_fields JSONB NOT NULL DEFAULT '{}'::JSONB + CHECK (jsonb_typeof(heavy_fields) = 'object'), + state VARCHAR(16) NOT NULL DEFAULT 'received' + CHECK (state IN ('received','completed')), + received_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + completed_at TIMESTAMPTZ, + duration_ms BIGINT CHECK (duration_ms IS NULL OR duration_ms >= 0), + http_status SMALLINT CHECK ( + http_status IS NULL OR http_status BETWEEN 100 AND 599 + ), + outcome VARCHAR(64), + error_code VARCHAR(128), + processed_read_ids BIGINT[] NOT NULL DEFAULT ARRAY[]::BIGINT[], + processed_count INTEGER NOT NULL DEFAULT 0 CHECK (processed_count >= 0), + duplicate_count INTEGER NOT NULL DEFAULT 0 CHECK (duplicate_count >= 0), + ignored_count INTEGER NOT NULL DEFAULT 0 CHECK (ignored_count >= 0), + overview_work_queued BOOLEAN NOT NULL DEFAULT FALSE, + updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + CHECK (array_position(payload_keys, NULL) IS NULL), + CHECK (array_position(processed_read_ids, NULL) IS NULL), + CHECK ( + (state = 'received' AND completed_at IS NULL) + OR (state = 'completed' AND completed_at IS NOT NULL) + ) +); + +CREATE INDEX IF NOT EXISTS idx_integration_ingress_receipts_received + ON public.integration_ingress_receipts (received_at DESC, id DESC); +CREATE INDEX IF NOT EXISTS idx_integration_ingress_receipts_request + ON public.integration_ingress_receipts (request_id); +CREATE INDEX IF NOT EXISTS idx_integration_ingress_receipts_trigger_diagnostics + ON public.integration_ingress_receipts ( + integration, camera_name, trigger_value_state, received_at DESC + ); + +INSERT INTO public.schema_migrations(version,description) VALUES + ('2026081301_integration_ingress_receipts','Add bounded metadata-only integration request receipts and trigger-type diagnostics.') +ON CONFLICT(version) DO NOTHING; diff --git a/migrations/0048_2026081302_ingress_receipt_diagnostics_v2.sql b/migrations/0048_2026081302_ingress_receipt_diagnostics_v2.sql new file mode 100644 index 0000000..97ad48b --- /dev/null +++ b/migrations/0048_2026081302_ingress_receipt_diagnostics_v2.sql @@ -0,0 +1,18 @@ +-- migration: 2026081302_ingress_receipt_diagnostics_v2 +-- Versioned, metadata-only evidence for ambiguous trigger aliases and the +-- existing reads targeted by duplicate submissions. Existing receipts remain +-- schema v1; new application writes explicitly identify schema v2. +ALTER TABLE public.integration_ingress_receipts + ADD COLUMN IF NOT EXISTS receipt_schema_version SMALLINT NOT NULL DEFAULT 1 + CHECK (receipt_schema_version BETWEEN 1 AND 32767), + ADD COLUMN IF NOT EXISTS trigger_alias_fields TEXT[] NOT NULL + DEFAULT ARRAY[]::TEXT[], + ADD COLUMN IF NOT EXISTS trigger_alias_conflict BOOLEAN NOT NULL DEFAULT FALSE, + ADD COLUMN IF NOT EXISTS trigger_alias_distinct_value_count SMALLINT NOT NULL DEFAULT 0 + CHECK (trigger_alias_distinct_value_count BETWEEN 0 AND 3), + ADD COLUMN IF NOT EXISTS duplicate_target_read_ids BIGINT[] NOT NULL + DEFAULT ARRAY[]::BIGINT[]; + +INSERT INTO public.schema_migrations(version,description) VALUES + ('2026081302_ingress_receipt_diagnostics_v2','Version ingress receipts and add sanitized trigger-alias conflict plus duplicate-target read evidence.') +ON CONFLICT(version) DO NOTHING; diff --git a/migrations/0049_2026081303_read_pipeline_timeline.sql b/migrations/0049_2026081303_read_pipeline_timeline.sql new file mode 100644 index 0000000..64fcb24 --- /dev/null +++ b/migrations/0049_2026081303_read_pipeline_timeline.sql @@ -0,0 +1,36 @@ +-- migration: 2026081303_read_pipeline_timeline +-- Sanitized, append-only application evidence for each accepted read. Events +-- follow the parent read lifecycle so existing read cleanup remains unchanged. +-- The details object is deliberately small and never stores plate text, +-- request bodies, images, paths, alternate trigger values, or credentials. +CREATE TABLE IF NOT EXISTS public.plate_read_pipeline_events ( + id BIGSERIAL PRIMARY KEY, + read_id INTEGER NOT NULL REFERENCES public.plate_reads(id) ON DELETE CASCADE, + request_id VARCHAR(128), + ingress_receipt_id BIGINT REFERENCES public.integration_ingress_receipts(id) + ON DELETE SET NULL, + stage VARCHAR(32) NOT NULL CHECK ( + stage IN ('ingress','direction','notifications','vehicle-view') + ), + event_type VARCHAR(96) NOT NULL CHECK ( + event_type ~ '^[a-z0-9][a-z0-9_.-]*$' + ), + status VARCHAR(32) NOT NULL CHECK ( + status IN ('accepted','succeeded','queued','skipped','partial','failed','completed') + ), + component VARCHAR(64) NOT NULL, + details JSONB NOT NULL DEFAULT '{}'::JSONB CHECK ( + jsonb_typeof(details) = 'object' AND pg_column_size(details) <= 8192 + ), + occurred_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP +); + +CREATE INDEX IF NOT EXISTS idx_plate_read_pipeline_events_read + ON public.plate_read_pipeline_events (read_id, occurred_at, id); +CREATE INDEX IF NOT EXISTS idx_plate_read_pipeline_events_request + ON public.plate_read_pipeline_events (request_id) + WHERE request_id IS NOT NULL; + +INSERT INTO public.schema_migrations(version,description) VALUES + ('2026081303_read_pipeline_timeline','Add sanitized append-only per-read pipeline events without changing read cleanup behavior.') +ON CONFLICT(version) DO NOTHING; diff --git a/migrations/0050_2026081401_logging_retention_incidents.sql b/migrations/0050_2026081401_logging_retention_incidents.sql new file mode 100644 index 0000000..804f42b --- /dev/null +++ b/migrations/0050_2026081401_logging_retention_incidents.sql @@ -0,0 +1,166 @@ +-- migration: 2026081401_logging_retention_incidents +-- Immutable incident snapshots preserve a bounded, sanitized evidence package +-- before retention can remove source rows. A scope protects matching live rows +-- until protected_until, while the snapshot and its digest remain append-only. +CREATE TABLE IF NOT EXISTS public.logging_incidents ( + id BIGSERIAL PRIMARY KEY, + name VARCHAR(120) NOT NULL CHECK (NULLIF(BTRIM(name), '') IS NOT NULL), + description VARCHAR(1000), + scope_type VARCHAR(16) NOT NULL CHECK ( + scope_type IN ('request','read','window') + ), + request_id VARCHAR(128), + read_id INTEGER, + window_start TIMESTAMPTZ, + window_end TIMESTAMPTZ, + protected_until TIMESTAMPTZ NOT NULL, + snapshot_schema_version SMALLINT NOT NULL DEFAULT 1 CHECK ( + snapshot_schema_version = 1 + ), + snapshot JSONB NOT NULL CHECK ( + jsonb_typeof(snapshot) = 'object' AND pg_column_size(snapshot) <= 16777216 + ), + snapshot_sha256 CHAR(64) NOT NULL CHECK ( + snapshot_sha256 ~ '^[0-9a-f]{64}$' + ), + evidence_counts JSONB NOT NULL DEFAULT '{}'::JSONB CHECK ( + jsonb_typeof(evidence_counts) = 'object' + ), + created_by_user_id BIGINT NOT NULL REFERENCES public.users(id) ON DELETE RESTRICT, + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + CHECK (protected_until > created_at), + CHECK ( + (scope_type = 'request' AND NULLIF(BTRIM(request_id), '') IS NOT NULL + AND read_id IS NULL AND window_start IS NULL AND window_end IS NULL) + OR + (scope_type = 'read' AND read_id IS NOT NULL + AND request_id IS NULL AND window_start IS NULL AND window_end IS NULL) + OR + (scope_type = 'window' AND request_id IS NULL AND read_id IS NULL + AND window_start IS NOT NULL AND window_end IS NOT NULL + AND window_start < window_end) + ) +); + +CREATE INDEX IF NOT EXISTS idx_logging_incidents_protection + ON public.logging_incidents (protected_until DESC, id DESC); +CREATE INDEX IF NOT EXISTS idx_logging_incidents_request + ON public.logging_incidents (request_id) WHERE request_id IS NOT NULL; +CREATE INDEX IF NOT EXISTS idx_logging_incidents_read + ON public.logging_incidents (read_id) WHERE read_id IS NOT NULL; + +CREATE OR REPLACE FUNCTION public.prevent_logging_incident_mutation() +RETURNS TRIGGER AS $$ +BEGIN + RAISE EXCEPTION 'logging_incidents is append-only'; +END; +$$ LANGUAGE plpgsql; + +DROP TRIGGER IF EXISTS logging_incidents_append_only ON public.logging_incidents; +CREATE TRIGGER logging_incidents_append_only +BEFORE UPDATE OR DELETE ON public.logging_incidents +FOR EACH ROW EXECUTE FUNCTION public.prevent_logging_incident_mutation(); + +-- Old audit rows move into an immutable, time-partitioned archive before the +-- hot table can release them. The default partition is bounded by the manual +-- preview batch; future releases may add narrower monthly partitions without +-- changing the archive contract. +CREATE TABLE IF NOT EXISTS public.audit_event_archive ( + source_event_id BIGINT NOT NULL, + actor_user_id BIGINT, + actor_api_credential_id BIGINT, + source VARCHAR(20) NOT NULL, + event_type VARCHAR(100) NOT NULL, + resource_type VARCHAR(100), + resource_id VARCHAR(255), + outcome VARCHAR(20) NOT NULL, + reason TEXT, + request_id VARCHAR(100), + metadata JSONB NOT NULL, + occurred_at TIMESTAMPTZ NOT NULL, + archived_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + retention_preview_id BIGINT, + PRIMARY KEY (source_event_id, occurred_at) +) PARTITION BY RANGE (occurred_at); + +CREATE TABLE IF NOT EXISTS public.audit_event_archive_default + PARTITION OF public.audit_event_archive DEFAULT; + +CREATE INDEX IF NOT EXISTS idx_audit_event_archive_occurred + ON public.audit_event_archive (occurred_at DESC, source_event_id DESC); +CREATE INDEX IF NOT EXISTS idx_audit_event_archive_request + ON public.audit_event_archive (request_id, occurred_at DESC) + WHERE request_id IS NOT NULL; + +CREATE OR REPLACE FUNCTION public.prevent_audit_archive_mutation() +RETURNS TRIGGER AS $$ +BEGIN + RAISE EXCEPTION 'audit_event_archive is append-only'; +END; +$$ LANGUAGE plpgsql; + +DROP TRIGGER IF EXISTS audit_event_archive_append_only + ON public.audit_event_archive; +CREATE TRIGGER audit_event_archive_append_only +BEFORE UPDATE OR DELETE ON public.audit_event_archive +FOR EACH ROW EXECUTE FUNCTION public.prevent_audit_archive_mutation(); + +-- The hot audit table remains update-proof. A delete is permitted only after +-- an exact immutable archive copy exists, preserving the append-only evidence +-- contract while allowing the operational table to stay bounded. +CREATE OR REPLACE FUNCTION public.prevent_audit_event_mutation() +RETURNS TRIGGER AS $$ +BEGIN + IF TG_OP = 'DELETE' AND EXISTS ( + SELECT 1 + FROM public.audit_event_archive archived + WHERE archived.source_event_id = OLD.id + AND archived.occurred_at = OLD.occurred_at + AND archived.actor_user_id IS NOT DISTINCT FROM OLD.actor_user_id + AND archived.actor_api_credential_id IS NOT DISTINCT FROM OLD.actor_api_credential_id + AND archived.source = OLD.source + AND archived.event_type = OLD.event_type + AND archived.resource_type IS NOT DISTINCT FROM OLD.resource_type + AND archived.resource_id IS NOT DISTINCT FROM OLD.resource_id + AND archived.outcome = OLD.outcome + AND archived.reason IS NOT DISTINCT FROM OLD.reason + AND archived.request_id IS NOT DISTINCT FROM OLD.request_id + AND archived.metadata = OLD.metadata + ) THEN + RETURN OLD; + END IF; + RAISE EXCEPTION 'audit_events is append-only'; +END; +$$ LANGUAGE plpgsql; + +CREATE TABLE IF NOT EXISTS public.logging_retention_previews ( + id BIGSERIAL PRIMARY KEY, + token_hash CHAR(64) NOT NULL UNIQUE CHECK (token_hash ~ '^[0-9a-f]{64}$'), + actor_user_id BIGINT NOT NULL REFERENCES public.users(id) ON DELETE RESTRICT, + status VARCHAR(16) NOT NULL DEFAULT 'previewed' CHECK ( + status IN ('previewed','executed','invalidated') + ), + policy JSONB NOT NULL CHECK (jsonb_typeof(policy) = 'object'), + receipt_ids BIGINT[] NOT NULL DEFAULT ARRAY[]::BIGINT[], + audit_event_ids BIGINT[] NOT NULL DEFAULT ARRAY[]::BIGINT[], + candidate_count INTEGER NOT NULL DEFAULT 0 CHECK (candidate_count >= 0), + candidate_bytes BIGINT NOT NULL DEFAULT 0 CHECK (candidate_bytes >= 0), + expires_at TIMESTAMPTZ NOT NULL, + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + executed_at TIMESTAMPTZ, + result JSONB, + CHECK (array_position(receipt_ids, NULL) IS NULL), + CHECK (array_position(audit_event_ids, NULL) IS NULL), + CHECK (expires_at > created_at), + CHECK ( + (status = 'previewed' AND executed_at IS NULL) + OR (status IN ('executed','invalidated') AND executed_at IS NOT NULL) + ) +); + +CREATE INDEX IF NOT EXISTS idx_logging_retention_previews_actor + ON public.logging_retention_previews (actor_user_id, created_at DESC); + +INSERT INTO public.schema_migrations(version,description) VALUES + ('2026081401_logging_retention_incidents','Add immutable incident snapshots, preview-bound log retention, and a partitioned append-only audit archive.') +ON CONFLICT(version) DO NOTHING; diff --git a/migrations/0051_2026081402_vehicle_image_asset_foundation.sql b/migrations/0051_2026081402_vehicle_image_asset_foundation.sql new file mode 100644 index 0000000..200153a --- /dev/null +++ b/migrations/0051_2026081402_vehicle_image_asset_foundation.sql @@ -0,0 +1,155 @@ +-- migration: 2026081402_vehicle_image_asset_foundation +-- Canonical, byte-addressed ownership for ready Overview JPEGs. This is an +-- inert catalog foundation: it does not queue work, backfill reads, alter the +-- current read-owned Vehicle View, or change the existing ReID pipeline. +CREATE TABLE IF NOT EXISTS public.vehicle_image_assets ( + id BIGSERIAL PRIMARY KEY, + content_sha256 CHAR(64) NOT NULL UNIQUE CHECK ( + content_sha256 ~ '^[0-9a-f]{64}$' + ), + storage_path TEXT NOT NULL UNIQUE CHECK ( + storage_path = 'derived/vehicle-assets/' + || SUBSTRING(content_sha256 FROM 1 FOR 2) + || '/' || content_sha256 || '.jpg' + ), + media_type VARCHAR(40) NOT NULL DEFAULT 'image/jpeg' CHECK ( + media_type = 'image/jpeg' + ), + byte_size BIGINT NOT NULL CHECK (byte_size > 0), + image_width INTEGER NOT NULL CHECK (image_width > 0), + image_height INTEGER NOT NULL CHECK (image_height > 0), + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP +); + +CREATE OR REPLACE FUNCTION public.prevent_vehicle_image_asset_update() +RETURNS TRIGGER AS $$ +BEGIN + RAISE EXCEPTION 'vehicle_image_assets content is immutable'; +END; +$$ LANGUAGE plpgsql; + +DROP TRIGGER IF EXISTS vehicle_image_assets_immutable + ON public.vehicle_image_assets; +CREATE TRIGGER vehicle_image_assets_immutable +BEFORE UPDATE ON public.vehicle_image_assets +FOR EACH ROW EXECUTE FUNCTION public.prevent_vehicle_image_asset_update(); + +CREATE TABLE IF NOT EXISTS public.vehicle_image_asset_reads ( + asset_id BIGINT NOT NULL REFERENCES public.vehicle_image_assets(id) + ON DELETE RESTRICT, + read_id INTEGER NOT NULL REFERENCES public.plate_reads(id) + ON DELETE CASCADE, + source_kind VARCHAR(40) NOT NULL CHECK ( + source_kind IN ( + 'overview_primary', + 'entry_overview_primary', + 'overview_fallback', + 'overview_pair_share', + 'entry_overview_route_fallback', + 'entry_overview_history' + ) + ), + source_read_id INTEGER REFERENCES public.plate_reads(id) ON DELETE SET NULL, + relationship VARCHAR(24) NOT NULL CHECK ( + relationship IN ('primary','fallback','shared','display_fallback','history') + ), + identity_eligible BOOLEAN NOT NULL, + overview_context VARCHAR(12) NOT NULL CHECK ( + overview_context IN ('street','entry') + ), + captured_at TIMESTAMPTZ, + read_camera_name VARCHAR(120), + source_camera_name VARCHAR(120), + source_path_snapshot TEXT NOT NULL CHECK ( + NULLIF(BTRIM(source_path_snapshot), '') IS NOT NULL + ), + source_updated_at TIMESTAMPTZ, + detection_confidence REAL CHECK ( + detection_confidence IS NULL + OR detection_confidence BETWEEN 0 AND 1 + ), + detection_box JSONB CHECK ( + detection_box IS NULL OR jsonb_typeof(detection_box) = 'object' + ), + selection_metadata JSONB NOT NULL DEFAULT '{}'::JSONB CHECK ( + jsonb_typeof(selection_metadata) = 'object' + ), + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + PRIMARY KEY (asset_id, read_id), + UNIQUE (read_id), + CHECK ( + (source_kind = 'overview_primary' + AND source_read_id IS NULL + AND relationship = 'primary' + AND identity_eligible = TRUE + AND overview_context = 'street') + OR (source_kind = 'entry_overview_primary' + AND source_read_id IS NULL + AND relationship = 'primary' + AND identity_eligible = TRUE + AND overview_context = 'entry') + OR (source_kind = 'overview_fallback' + AND source_read_id IS NULL + AND relationship = 'fallback' + AND identity_eligible = TRUE + AND overview_context = 'street') + OR (source_kind = 'overview_pair_share' + AND (source_read_id IS NULL OR source_read_id <> read_id) + AND relationship = 'shared' + AND identity_eligible = TRUE + AND overview_context = 'street') + OR (source_kind = 'entry_overview_route_fallback' + AND (source_read_id IS NULL OR source_read_id <> read_id) + AND relationship = 'display_fallback' + AND identity_eligible = FALSE + AND overview_context = 'entry') + OR (source_kind = 'entry_overview_history' + AND source_read_id IS NULL + AND relationship = 'history' + AND identity_eligible = TRUE + AND overview_context = 'entry') + ) +); + +CREATE INDEX IF NOT EXISTS idx_vehicle_image_asset_reads_source + ON public.vehicle_image_asset_reads (source_read_id) + WHERE source_read_id IS NOT NULL; +CREATE INDEX IF NOT EXISTS idx_vehicle_image_asset_reads_identity + ON public.vehicle_image_asset_reads (overview_context, captured_at, read_id) + WHERE identity_eligible = TRUE; + +-- Storage reconciliation now treats canonical assets as first-class database +-- references. Defaults safely let an already-running pre-catalog scan finish. +ALTER TABLE public.storage_reconciliation_runs + ADD COLUMN IF NOT EXISTS max_vehicle_image_asset_id BIGINT NOT NULL DEFAULT 0, + ADD COLUMN IF NOT EXISTS vehicle_image_asset_cursor BIGINT NOT NULL DEFAULT 0; + +ALTER TABLE public.storage_reconciliation_runs + DROP CONSTRAINT IF EXISTS storage_reconciliation_phase; +ALTER TABLE public.storage_reconciliation_runs + ADD CONSTRAINT storage_reconciliation_phase CHECK ( + phase IN ( + 'filesystem', 'plate-reads', + 'vehicle-image-assets', 'vehicle-image-derivatives', 'completed' + ) + ); + +ALTER TABLE public.storage_reconciliation_runs + DROP CONSTRAINT IF EXISTS storage_reconciliation_counts; +ALTER TABLE public.storage_reconciliation_runs + ADD CONSTRAINT storage_reconciliation_counts CHECK ( + max_plate_read_id >= 0 + AND max_vehicle_image_asset_id >= 0 + AND plate_read_cursor >= 0 + AND vehicle_image_asset_cursor >= 0 + AND files_scanned >= 0 AND bytes_scanned >= 0 + AND references_checked >= 0 AND recent_files_skipped >= 0 + AND skipped_entries >= 0 AND error_count >= 0 + AND orphan_files >= 0 AND orphan_bytes >= 0 + AND missing_reference_paths >= 0 + ); + +INSERT INTO public.schema_migrations(version,description) VALUES + ('2026081402_vehicle_image_asset_foundation','Add an inert canonical Overview JPEG catalog, read provenance links, and storage-reference protection.') +ON CONFLICT(version) DO NOTHING; diff --git a/migrations/0052_2026081403_vehicle_image_asset_catalog_campaign.sql b/migrations/0052_2026081403_vehicle_image_asset_catalog_campaign.sql new file mode 100644 index 0000000..7c8d5de --- /dev/null +++ b/migrations/0052_2026081403_vehicle_image_asset_catalog_campaign.sql @@ -0,0 +1,167 @@ +-- migration: 2026081403_vehicle_image_asset_catalog_campaign +-- Operator-controlled, preview-first population of the canonical Overview +-- catalog. Creating a run only freezes eligible read snapshots. A dedicated +-- worker hashes and validates those source JPEGs without publishing files; +-- canonical storage is written only after an operator confirms a bounded +-- batch against the exact completed preview fingerprint. +CREATE TABLE IF NOT EXISTS public.vehicle_image_asset_catalog_runs ( + id BIGSERIAL PRIMARY KEY, + phase VARCHAR(16) NOT NULL DEFAULT 'preview' CHECK ( + phase IN ('preview','catalog','completed') + ), + status VARCHAR(16) NOT NULL DEFAULT 'previewing' CHECK ( + status IN ('previewing','ready','running','paused','completed','cancelled','failed') + ), + max_read_id INTEGER NOT NULL CHECK (max_read_id >= 0), + preview_cursor_read_id INTEGER NOT NULL DEFAULT 0 CHECK ( + preview_cursor_read_id >= 0 AND preview_cursor_read_id <= max_read_id + ), + candidate_reads INTEGER NOT NULL DEFAULT 0 CHECK (candidate_reads >= 0), + batch_size INTEGER NOT NULL DEFAULT 5 CHECK ( + batch_size IN (1,5,25,250) + ), + preview_fingerprint CHAR(64) CHECK ( + preview_fingerprint IS NULL OR preview_fingerprint ~ '^[0-9a-f]{64}$' + ), + actor_user_id BIGINT NOT NULL REFERENCES public.users(id) ON DELETE RESTRICT, + confirmed_actor_user_id BIGINT REFERENCES public.users(id) ON DELETE RESTRICT, + last_error_code VARCHAR(80), + last_error_details JSONB CHECK ( + last_error_details IS NULL OR jsonb_typeof(last_error_details) = 'object' + ), + confirmed_at TIMESTAMPTZ, + paused_at TIMESTAMPTZ, + cancelled_at TIMESTAMPTZ, + completed_at TIMESTAMPTZ, + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + CHECK ( + (status = 'previewing' AND phase = 'preview' AND preview_fingerprint IS NULL) + OR (status IN ('ready','running') AND phase = 'catalog' AND preview_fingerprint IS NOT NULL) + OR (status = 'paused') + OR (status = 'completed' AND phase = 'completed' AND preview_fingerprint IS NOT NULL) + OR (status IN ('cancelled','failed')) + ) +); + +CREATE UNIQUE INDEX IF NOT EXISTS idx_vehicle_image_asset_catalog_one_active + ON public.vehicle_image_asset_catalog_runs ((TRUE)) + WHERE status IN ('previewing','ready','running','paused'); + +CREATE TABLE IF NOT EXISTS public.vehicle_image_asset_catalog_items ( + id BIGSERIAL PRIMARY KEY, + run_id BIGINT NOT NULL REFERENCES public.vehicle_image_asset_catalog_runs(id) + ON DELETE RESTRICT, + -- This is an immutable snapshot identifier, not a live ownership edge. + -- Keeping it scalar preserves campaign evidence without blocking the + -- application's existing single-read deletion flow. + read_id INTEGER NOT NULL, + snapshot_fingerprint CHAR(64) NOT NULL CHECK ( + snapshot_fingerprint ~ '^[0-9a-f]{64}$' + ), + read_camera_name VARCHAR(120), + read_timestamp TIMESTAMPTZ NOT NULL, + source_status VARCHAR(20) NOT NULL CHECK (source_status = 'ready'), + source_path TEXT NOT NULL CHECK (NULLIF(BTRIM(source_path), '') IS NOT NULL), + source_kind VARCHAR(40) NOT NULL CHECK ( + source_kind IN ( + 'overview_primary', + 'entry_overview_primary', + 'overview_fallback', + 'overview_pair_share', + 'entry_overview_route_fallback', + 'entry_overview_history' + ) + ), + -- Preserve the exact source-read provenance that was fingerprinted even if + -- the live source read is later deleted. + source_read_id INTEGER, + source_updated_at TIMESTAMPTZ, + captured_at TIMESTAMPTZ, + source_score REAL, + -- These are lossless evidence snapshots. Legacy malformed metadata must be + -- materialized and terminalized per item, never abort the entire preview. + detection_confidence REAL, + detection_box JSONB, + source_width INTEGER, + source_height INTEGER, + sampled_count SMALLINT, + selection_metadata JSONB, + prior_link_state VARCHAR(12) NOT NULL CHECK ( + prior_link_state IN ('absent','stale') + ), + preview_sha256 CHAR(64) CHECK ( + preview_sha256 IS NULL OR preview_sha256 ~ '^[0-9a-f]{64}$' + ), + preview_byte_size BIGINT CHECK (preview_byte_size IS NULL OR preview_byte_size > 0), + preview_width INTEGER CHECK (preview_width IS NULL OR preview_width > 0), + preview_height INTEGER CHECK (preview_height IS NULL OR preview_height > 0), + canonical_path TEXT CHECK ( + canonical_path IS NULL OR canonical_path = 'derived/vehicle-assets/' + || SUBSTRING(preview_sha256 FROM 1 FOR 2) + || '/' || preview_sha256 || '.jpg' + ), + status VARCHAR(20) NOT NULL DEFAULT 'pending_preview' CHECK ( + status IN ( + 'pending_preview','previewing','previewed','queued','processing', + 'cataloged','already_current','superseded','unavailable','invalid', + 'failed','cancelled' + ) + ), + failure_stage VARCHAR(12) CHECK ( + failure_stage IS NULL OR failure_stage IN ('preview','catalog') + ), + attempt_count SMALLINT NOT NULL DEFAULT 0 CHECK (attempt_count BETWEEN 0 AND 3), + operator_retry_count SMALLINT NOT NULL DEFAULT 0 CHECK ( + operator_retry_count BETWEEN 0 AND 1 + ), + retryable BOOLEAN NOT NULL DEFAULT FALSE, + claim_token UUID, + heartbeat_at TIMESTAMPTZ, + processing_deadline_at TIMESTAMPTZ, + next_attempt_at TIMESTAMPTZ, + error_code VARCHAR(80), + error_details JSONB CHECK ( + error_details IS NULL OR jsonb_typeof(error_details) = 'object' + ), + asset_id BIGINT REFERENCES public.vehicle_image_assets(id) ON DELETE SET NULL, + previewed_at TIMESTAMPTZ, + cataloged_at TIMESTAMPTZ, + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + UNIQUE (run_id, read_id), + CHECK ( + (status IN ('previewed','queued','processing','cataloged','already_current') + AND preview_sha256 IS NOT NULL + AND preview_byte_size IS NOT NULL + AND preview_width IS NOT NULL + AND preview_height IS NOT NULL + AND canonical_path IS NOT NULL) + OR status NOT IN ('previewed','queued','processing','cataloged','already_current') + ), + CHECK ( + (status IN ('previewing','processing') + AND claim_token IS NOT NULL + AND processing_deadline_at IS NOT NULL) + OR (status NOT IN ('previewing','processing') + AND claim_token IS NULL + AND processing_deadline_at IS NULL) + ), + CHECK ( + (status = 'failed' AND failure_stage IS NOT NULL AND error_code IS NOT NULL) + OR status <> 'failed' + ) +); + +CREATE INDEX IF NOT EXISTS idx_vehicle_image_asset_catalog_items_run + ON public.vehicle_image_asset_catalog_items (run_id, status, read_id, id); +CREATE INDEX IF NOT EXISTS idx_vehicle_image_asset_catalog_items_claim + ON public.vehicle_image_asset_catalog_items (run_id, next_attempt_at, read_id, id) + WHERE status IN ('pending_preview','previewing','queued','processing','failed'); +CREATE INDEX IF NOT EXISTS idx_vehicle_image_asset_catalog_items_hash + ON public.vehicle_image_asset_catalog_items (run_id, preview_sha256) + WHERE preview_sha256 IS NOT NULL; + +INSERT INTO public.schema_migrations(version,description) VALUES + ('2026081403_vehicle_image_asset_catalog_campaign','Add a durable preview-first, operator-confirmed campaign for canonical Overview assets.') +ON CONFLICT(version) DO NOTHING; diff --git a/migrations/0053_2026081404_vehicle_image_asset_live_catalog.sql b/migrations/0053_2026081404_vehicle_image_asset_live_catalog.sql new file mode 100644 index 0000000..adff32c --- /dev/null +++ b/migrations/0053_2026081404_vehicle_image_asset_live_catalog.sql @@ -0,0 +1,95 @@ +-- migration: 2026081404_vehicle_image_asset_live_catalog +-- Durable, default-off cataloging for Overview images that become ready after +-- the initial operator campaign. The live worker is additionally gated on a +-- completed campaign and yields whenever an operator campaign is active. No +-- jobs are inserted by the migration, and current Vehicle View readiness does +-- not depend on this queue. +CREATE TABLE IF NOT EXISTS public.vehicle_image_asset_live_catalog_control ( + singleton BOOLEAN PRIMARY KEY DEFAULT TRUE CHECK (singleton = TRUE), + enabled BOOLEAN NOT NULL DEFAULT TRUE, + enabled_by_user_id BIGINT REFERENCES public.users(id) ON DELETE SET NULL, + enabled_at TIMESTAMPTZ, + disabled_at TIMESTAMPTZ, + updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + CHECK ( + (enabled = TRUE AND enabled_at IS NOT NULL) + OR enabled = FALSE + ) +); + +INSERT INTO public.vehicle_image_asset_live_catalog_control ( + singleton, enabled, enabled_by_user_id, enabled_at, disabled_at +) VALUES (TRUE, TRUE, NULL, CURRENT_TIMESTAMP, NULL) +ON CONFLICT (singleton) DO NOTHING; + +CREATE TABLE IF NOT EXISTS public.vehicle_image_asset_live_catalog_jobs ( + id BIGSERIAL PRIMARY KEY, + -- Deliberately not a foreign key: durable job evidence must not block the + -- existing single-read deletion lifecycle. + read_id INTEGER NOT NULL UNIQUE CHECK (read_id > 0), + source_path_snapshot TEXT NOT NULL CHECK ( + NULLIF(BTRIM(source_path_snapshot), '') IS NOT NULL + ), + source_kind_snapshot VARCHAR(40) NOT NULL CHECK ( + source_kind_snapshot IN ( + 'overview_primary', + 'entry_overview_primary', + 'overview_fallback', + 'overview_pair_share', + 'entry_overview_route_fallback', + 'entry_overview_history' + ) + ), + source_updated_at_snapshot TIMESTAMPTZ, + status VARCHAR(20) NOT NULL DEFAULT 'queued' CHECK ( + status IN ( + 'queued','processing','cataloged','superseded', + 'unavailable','invalid','failed' + ) + ), + attempt_count SMALLINT NOT NULL DEFAULT 0 CHECK ( + attempt_count BETWEEN 0 AND 5 + ), + operator_retry_count SMALLINT NOT NULL DEFAULT 0 CHECK ( + operator_retry_count BETWEEN 0 AND 1 + ), + retryable BOOLEAN NOT NULL DEFAULT TRUE, + claim_token UUID, + heartbeat_at TIMESTAMPTZ, + processing_deadline_at TIMESTAMPTZ, + next_attempt_at TIMESTAMPTZ, + error_code VARCHAR(80), + error_details JSONB CHECK ( + error_details IS NULL OR jsonb_typeof(error_details) = 'object' + ), + asset_id BIGINT REFERENCES public.vehicle_image_assets(id) ON DELETE SET NULL, + cataloged_at TIMESTAMPTZ, + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + CHECK ( + (status = 'processing' AND claim_token IS NOT NULL + AND processing_deadline_at IS NOT NULL) + OR (status <> 'processing' AND claim_token IS NULL + AND processing_deadline_at IS NULL) + ), + CHECK ( + (status = 'cataloged' AND asset_id IS NOT NULL AND cataloged_at IS NOT NULL) + OR status <> 'cataloged' + ), + CHECK ( + (status IN ('superseded','unavailable','invalid','failed') + AND error_code IS NOT NULL) + OR status NOT IN ('superseded','unavailable','invalid','failed') + ) +); + +CREATE INDEX IF NOT EXISTS idx_vehicle_image_asset_live_catalog_claim + ON public.vehicle_image_asset_live_catalog_jobs ( + next_attempt_at, read_id, id + ) WHERE status IN ('queued','processing','failed'); +CREATE INDEX IF NOT EXISTS idx_vehicle_image_asset_live_catalog_status + ON public.vehicle_image_asset_live_catalog_jobs (status, updated_at DESC, id DESC); + +INSERT INTO public.schema_migrations(version,description) VALUES + ('2026081404_vehicle_image_asset_live_catalog','Add default-off, post-campaign automatic cataloging for newly ready Overview assets.') +ON CONFLICT(version) DO NOTHING; diff --git a/migrations/0054_2026081405_vehicle_event_shadow_correlation.sql b/migrations/0054_2026081405_vehicle_event_shadow_correlation.sql new file mode 100644 index 0000000..48c1d26 --- /dev/null +++ b/migrations/0054_2026081405_vehicle_event_shadow_correlation.sql @@ -0,0 +1,187 @@ +-- migration: 2026081405_vehicle_event_shadow_correlation +-- Provider-neutral shadow passage events built only from current, +-- identity-eligible canonical Overview links. The correlator is default-off, +-- never gates ingestion or Vehicle View readiness, and does not alter the +-- current ReID, cluster, attribute, notification, or plate-review paths. +CREATE TABLE IF NOT EXISTS public.vehicle_event_shadow_control ( + singleton BOOLEAN PRIMARY KEY DEFAULT TRUE CHECK (singleton = TRUE), + enabled BOOLEAN NOT NULL DEFAULT FALSE, + settle_seconds INTEGER NOT NULL DEFAULT 20 CHECK ( + settle_seconds BETWEEN 5 AND 300 + ), + batch_size INTEGER NOT NULL DEFAULT 25 CHECK ( + batch_size IN (5,25,100) + ), + enabled_by_user_id BIGINT REFERENCES public.users(id) ON DELETE SET NULL, + enabled_at TIMESTAMPTZ, + disabled_at TIMESTAMPTZ, + updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + CHECK ((enabled = TRUE AND enabled_at IS NOT NULL) OR enabled = FALSE) +); + +INSERT INTO public.vehicle_event_shadow_control ( + singleton, enabled, settle_seconds, batch_size, disabled_at +) VALUES (TRUE, FALSE, 20, 25, CURRENT_TIMESTAMP) +ON CONFLICT (singleton) DO NOTHING; + +CREATE TABLE IF NOT EXISTS public.vehicle_events ( + id BIGSERIAL PRIMARY KEY, + event_identity CHAR(64) NOT NULL UNIQUE CHECK ( + event_identity ~ '^[0-9a-f]{64}$' + ), + status VARCHAR(16) NOT NULL DEFAULT 'shadow' CHECK ( + status IN ('shadow','retired') + ), + overview_context VARCHAR(12) NOT NULL CHECK ( + overview_context IN ('street','entry') + ), + correlation_class VARCHAR(20) NOT NULL CHECK ( + correlation_class IN ('shared_asset','timed_pair') + ), + event_timestamp TIMESTAMPTZ NOT NULL, + first_read_at TIMESTAMPTZ NOT NULL, + last_read_at TIMESTAMPTZ NOT NULL, + effective_plate_snapshot VARCHAR(32) NOT NULL CHECK ( + NULLIF(BTRIM(effective_plate_snapshot), '') IS NOT NULL + ), + direction_label_snapshot VARCHAR(100), + correlation_algorithm VARCHAR(80) NOT NULL, + correlation_revision INTEGER NOT NULL DEFAULT 1 CHECK ( + correlation_revision > 0 + ), + decision_metadata JSONB NOT NULL DEFAULT '{}'::JSONB CHECK ( + jsonb_typeof(decision_metadata) = 'object' + ), + retired_reason VARCHAR(80), + retired_at TIMESTAMPTZ, + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + CHECK (first_read_at <= last_read_at), + CHECK ( + (status = 'shadow' AND retired_reason IS NULL AND retired_at IS NULL) + OR (status = 'retired' AND retired_reason IS NOT NULL AND retired_at IS NOT NULL) + ) +); + +CREATE INDEX IF NOT EXISTS idx_vehicle_events_shadow_recent + ON public.vehicle_events (event_timestamp DESC, id DESC) + WHERE status = 'shadow'; + +CREATE TABLE IF NOT EXISTS public.vehicle_event_reads ( + event_id BIGINT NOT NULL REFERENCES public.vehicle_events(id) ON DELETE CASCADE, + -- Immutable evidence scalar: event history must not block single-read deletion. + read_id INTEGER NOT NULL CHECK (read_id > 0), + role VARCHAR(16) NOT NULL CHECK (role IN ('anchor','companion')), + asset_id BIGINT NOT NULL REFERENCES public.vehicle_image_assets(id) + ON DELETE RESTRICT, + read_camera_name VARCHAR(120) NOT NULL CHECK ( + NULLIF(BTRIM(read_camera_name), '') IS NOT NULL + ), + read_timestamp TIMESTAMPTZ NOT NULL, + effective_plate_snapshot VARCHAR(32) NOT NULL CHECK ( + NULLIF(BTRIM(effective_plate_snapshot), '') IS NOT NULL + ), + direction_status_snapshot VARCHAR(20), + direction_label_snapshot VARCHAR(100), + source_kind_snapshot VARCHAR(40) NOT NULL, + source_read_id_snapshot INTEGER, + source_path_snapshot TEXT NOT NULL CHECK ( + NULLIF(BTRIM(source_path_snapshot), '') IS NOT NULL + ), + source_updated_at_snapshot TIMESTAMPTZ, + captured_at_snapshot TIMESTAMPTZ, + active BOOLEAN NOT NULL DEFAULT TRUE, + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + PRIMARY KEY (event_id, read_id), + CHECK ( + direction_status_snapshot IS DISTINCT FROM 'ready' + OR NULLIF(BTRIM(direction_label_snapshot), '') IS NOT NULL + ), + CHECK (source_kind_snapshot IN ( + 'overview_primary','entry_overview_primary','overview_fallback', + 'overview_pair_share','entry_overview_history' + )) +); + +CREATE UNIQUE INDEX IF NOT EXISTS idx_vehicle_event_reads_one_active_event + ON public.vehicle_event_reads (read_id) + WHERE active = TRUE; +CREATE INDEX IF NOT EXISTS idx_vehicle_event_reads_asset + ON public.vehicle_event_reads (asset_id, event_id); + +CREATE TABLE IF NOT EXISTS public.vehicle_event_assets ( + event_id BIGINT NOT NULL REFERENCES public.vehicle_events(id) ON DELETE CASCADE, + asset_id BIGINT NOT NULL REFERENCES public.vehicle_image_assets(id) + ON DELETE RESTRICT, + role VARCHAR(16) NOT NULL CHECK (role IN ('primary','supporting')), + identity_eligible BOOLEAN NOT NULL DEFAULT TRUE CHECK ( + identity_eligible = TRUE + ), + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + PRIMARY KEY (event_id, asset_id) +); + +CREATE UNIQUE INDEX IF NOT EXISTS idx_vehicle_event_assets_one_primary + ON public.vehicle_event_assets (event_id) + WHERE role = 'primary'; +CREATE INDEX IF NOT EXISTS idx_vehicle_event_assets_asset + ON public.vehicle_event_assets (asset_id, event_id); + +CREATE TABLE IF NOT EXISTS public.vehicle_event_shadow_decisions ( + id BIGSERIAL PRIMARY KEY, + decision_identity CHAR(64) NOT NULL UNIQUE CHECK ( + decision_identity ~ '^[0-9a-f]{64}$' + ), + event_id BIGINT REFERENCES public.vehicle_events(id) ON DELETE RESTRICT, + outcome VARCHAR(16) NOT NULL CHECK ( + outcome IN ('proposed','rejected') + ), + reason VARCHAR(80) NOT NULL CHECK (NULLIF(BTRIM(reason), '') IS NOT NULL), + overview_context VARCHAR(12) NOT NULL CHECK ( + overview_context IN ('street','entry') + ), + anchor_read_id INTEGER NOT NULL CHECK (anchor_read_id > 0), + companion_read_id INTEGER CHECK ( + companion_read_id IS NULL OR companion_read_id > 0 + ), + anchor_asset_id BIGINT NOT NULL REFERENCES public.vehicle_image_assets(id) + ON DELETE RESTRICT, + companion_asset_id BIGINT REFERENCES public.vehicle_image_assets(id) + ON DELETE RESTRICT, + anchor_source_kind VARCHAR(40) NOT NULL, + anchor_source_read_id INTEGER, + anchor_source_path TEXT NOT NULL CHECK ( + NULLIF(BTRIM(anchor_source_path), '') IS NOT NULL + ), + anchor_source_updated_at TIMESTAMPTZ, + anchor_captured_at TIMESTAMPTZ, + anchor_plate_snapshot VARCHAR(32) NOT NULL, + anchor_direction_status VARCHAR(20), + anchor_direction_label VARCHAR(100), + candidate_count INTEGER NOT NULL DEFAULT 0 CHECK (candidate_count >= 0), + correlation_algorithm VARCHAR(80) NOT NULL, + decision_metadata JSONB NOT NULL DEFAULT '{}'::JSONB CHECK ( + jsonb_typeof(decision_metadata) = 'object' + ), + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + CHECK ( + (outcome = 'proposed' AND event_id IS NOT NULL + AND companion_read_id IS NOT NULL AND companion_asset_id IS NOT NULL) + OR (outcome = 'rejected' AND event_id IS NULL) + ), + CHECK (anchor_source_kind IN ( + 'overview_primary','entry_overview_primary','overview_fallback', + 'overview_pair_share','entry_overview_history' + )) +); + +CREATE INDEX IF NOT EXISTS idx_vehicle_event_shadow_decisions_anchor + ON public.vehicle_event_shadow_decisions ( + anchor_read_id, created_at DESC, id DESC + ); +CREATE INDEX IF NOT EXISTS idx_vehicle_event_shadow_decisions_recent + ON public.vehicle_event_shadow_decisions (created_at DESC, id DESC); + +INSERT INTO public.schema_migrations(version,description) VALUES + ('2026081405_vehicle_event_shadow_correlation','Add default-off provider-neutral shadow correlation for conservative paired canonical Overview observations.') +ON CONFLICT(version) DO NOTHING; diff --git a/migrations/0055_2026081406_vehicle_image_crop_campaign.sql b/migrations/0055_2026081406_vehicle_image_crop_campaign.sql new file mode 100644 index 0000000..74418e9 --- /dev/null +++ b/migrations/0055_2026081406_vehicle_image_crop_campaign.sql @@ -0,0 +1,242 @@ +-- migration: 2026081406_vehicle_image_crop_campaign +-- Asset-owned vehicle crops derived from immutable canonical Overview JPEGs. +-- The migration is deliberately inert: it creates no run, job, derivative, +-- file, embedding, attribute, ReID result, or external-provider request. +CREATE TABLE IF NOT EXISTS public.vehicle_image_derivatives ( + id BIGSERIAL PRIMARY KEY, + asset_id BIGINT NOT NULL REFERENCES public.vehicle_image_assets(id) + ON DELETE RESTRICT, + derivative_kind VARCHAR(32) NOT NULL CHECK ( + derivative_kind = 'vehicle_crop' + ), + algorithm_version VARCHAR(100) NOT NULL CHECK ( + NULLIF(BTRIM(algorithm_version), '') IS NOT NULL + ), + source_sha256 CHAR(64) NOT NULL CHECK ( + source_sha256 ~ '^[0-9a-f]{64}$' + ), + content_sha256 CHAR(64) NOT NULL CHECK ( + content_sha256 ~ '^[0-9a-f]{64}$' + ), + storage_path TEXT NOT NULL CHECK ( + storage_path = 'derived/vehicle-crops/' + || SUBSTRING(content_sha256 FROM 1 FOR 2) + || '/' || content_sha256 || '.jpg' + ), + media_type VARCHAR(40) NOT NULL DEFAULT 'image/jpeg' CHECK ( + media_type = 'image/jpeg' + ), + byte_size BIGINT NOT NULL CHECK (byte_size > 0), + image_width INTEGER NOT NULL CHECK (image_width > 0), + image_height INTEGER NOT NULL CHECK (image_height > 0), + crop_box JSONB NOT NULL CHECK ( + jsonb_typeof(crop_box) = 'object' + AND crop_box ?& ARRAY['left','top','width','height','paddingRatio'] + AND jsonb_typeof(crop_box->'left') = 'number' + AND jsonb_typeof(crop_box->'top') = 'number' + AND jsonb_typeof(crop_box->'width') = 'number' + AND jsonb_typeof(crop_box->'height') = 'number' + AND jsonb_typeof(crop_box->'paddingRatio') = 'number' + AND (crop_box->>'left')::integer >= 0 + AND (crop_box->>'top')::integer >= 0 + AND (crop_box->>'width')::integer > 0 + AND (crop_box->>'height')::integer > 0 + AND (crop_box->>'paddingRatio')::numeric BETWEEN 0 AND 0.5 + ), + detector_model VARCHAR(100) NOT NULL CHECK ( + NULLIF(BTRIM(detector_model), '') IS NOT NULL + ), + detection_confidence REAL CHECK ( + detection_confidence IS NULL OR detection_confidence BETWEEN 0 AND 1 + ), + evidence_read_id INTEGER NOT NULL CHECK (evidence_read_id > 0), + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + UNIQUE (asset_id, derivative_kind, algorithm_version) +); + +CREATE INDEX IF NOT EXISTS idx_vehicle_image_derivatives_content + ON public.vehicle_image_derivatives (content_sha256, id); +CREATE INDEX IF NOT EXISTS idx_vehicle_image_derivatives_storage + ON public.vehicle_image_derivatives (storage_path, id); + +CREATE OR REPLACE FUNCTION public.prevent_vehicle_image_derivative_update() +RETURNS TRIGGER AS $$ +BEGIN + RAISE EXCEPTION 'vehicle_image_derivatives content is immutable'; +END; +$$ LANGUAGE plpgsql; + +DROP TRIGGER IF EXISTS vehicle_image_derivatives_immutable + ON public.vehicle_image_derivatives; +CREATE TRIGGER vehicle_image_derivatives_immutable +BEFORE UPDATE ON public.vehicle_image_derivatives +FOR EACH ROW EXECUTE FUNCTION public.prevent_vehicle_image_derivative_update(); + +CREATE TABLE IF NOT EXISTS public.vehicle_image_crop_runs ( + id BIGSERIAL PRIMARY KEY, + status VARCHAR(16) NOT NULL DEFAULT 'previewing' CHECK ( + status IN ('previewing','ready','running','paused','completed','cancelled','failed') + ), + max_asset_id BIGINT NOT NULL CHECK (max_asset_id >= 0), + preview_fingerprint CHAR(64) CHECK ( + preview_fingerprint IS NULL OR preview_fingerprint ~ '^[0-9a-f]{64}$' + ), + batch_size INTEGER NOT NULL DEFAULT 5 CHECK ( + batch_size IN (1,5,25,250) + ), + actor_user_id BIGINT NOT NULL REFERENCES public.users(id) ON DELETE RESTRICT, + confirmed_actor_user_id BIGINT REFERENCES public.users(id) ON DELETE RESTRICT, + confirmed_at TIMESTAMPTZ, + paused_at TIMESTAMPTZ, + cancelled_at TIMESTAMPTZ, + completed_at TIMESTAMPTZ, + last_error_code VARCHAR(80), + last_error_details JSONB CHECK ( + last_error_details IS NULL OR jsonb_typeof(last_error_details) = 'object' + ), + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + CHECK ( + (status = 'previewing' AND preview_fingerprint IS NULL) + OR (status IN ('ready','running','paused','completed') + AND preview_fingerprint IS NOT NULL) + OR status IN ('cancelled','failed') + ) +); + +CREATE UNIQUE INDEX IF NOT EXISTS idx_vehicle_image_crop_one_active + ON public.vehicle_image_crop_runs ((TRUE)) + WHERE status IN ('previewing','ready','running','paused'); + +CREATE TABLE IF NOT EXISTS public.vehicle_image_crop_jobs ( + id BIGSERIAL PRIMARY KEY, + run_id BIGINT NOT NULL REFERENCES public.vehicle_image_crop_runs(id) + ON DELETE RESTRICT, + asset_id BIGINT NOT NULL REFERENCES public.vehicle_image_assets(id) + ON DELETE RESTRICT, + source_sha256 CHAR(64) NOT NULL CHECK ( + source_sha256 ~ '^[0-9a-f]{64}$' + ), + source_path TEXT NOT NULL CHECK (NULLIF(BTRIM(source_path), '') IS NOT NULL), + source_width INTEGER NOT NULL CHECK (source_width > 0), + source_height INTEGER NOT NULL CHECK (source_height > 0), + evidence_read_id INTEGER NOT NULL CHECK (evidence_read_id > 0), + evidence_source_kind VARCHAR(40) NOT NULL, + evidence_source_path TEXT NOT NULL CHECK ( + NULLIF(BTRIM(evidence_source_path), '') IS NOT NULL + ), + evidence_source_updated_at TIMESTAMPTZ, + detection_box JSONB, + detection_confidence REAL, + status VARCHAR(20) NOT NULL DEFAULT 'pending_preview' CHECK ( + status IN ( + 'pending_preview','previewing','previewed','queued','processing', + 'ready','already_current','source_changed','invalid','failed','cancelled' + ) + ), + failure_stage VARCHAR(12) CHECK ( + failure_stage IS NULL OR failure_stage IN ('preview','catalog') + ), + preview_sha256 CHAR(64) CHECK ( + preview_sha256 IS NULL OR preview_sha256 ~ '^[0-9a-f]{64}$' + ), + preview_path TEXT CHECK ( + preview_path IS NULL OR preview_path = 'derived/vehicle-crops/' + || SUBSTRING(preview_sha256 FROM 1 FOR 2) + || '/' || preview_sha256 || '.jpg' + ), + preview_byte_size BIGINT CHECK ( + preview_byte_size IS NULL OR preview_byte_size > 0 + ), + preview_width INTEGER CHECK (preview_width IS NULL OR preview_width > 0), + preview_height INTEGER CHECK (preview_height IS NULL OR preview_height > 0), + preview_crop_box JSONB CHECK ( + preview_crop_box IS NULL OR jsonb_typeof(preview_crop_box) = 'object' + ), + attempt_count SMALLINT NOT NULL DEFAULT 0 CHECK (attempt_count BETWEEN 0 AND 3), + operator_retry_count SMALLINT NOT NULL DEFAULT 0 CHECK ( + operator_retry_count BETWEEN 0 AND 1 + ), + retryable BOOLEAN NOT NULL DEFAULT FALSE, + claim_token UUID, + processing_deadline_at TIMESTAMPTZ, + next_attempt_at TIMESTAMPTZ, + error_code VARCHAR(80), + error_details JSONB CHECK ( + error_details IS NULL OR jsonb_typeof(error_details) = 'object' + ), + derivative_id BIGINT REFERENCES public.vehicle_image_derivatives(id) + ON DELETE SET NULL, + previewed_at TIMESTAMPTZ, + completed_at TIMESTAMPTZ, + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + UNIQUE (run_id, asset_id), + CHECK ( + (status IN ('previewed','queued','processing','ready','already_current') + AND preview_sha256 IS NOT NULL AND preview_path IS NOT NULL + AND preview_byte_size IS NOT NULL AND preview_width IS NOT NULL + AND preview_height IS NOT NULL AND preview_crop_box IS NOT NULL) + OR status NOT IN ('previewed','queued','processing','ready','already_current') + ), + CHECK ( + (status IN ('previewing','processing') + AND claim_token IS NOT NULL AND processing_deadline_at IS NOT NULL) + OR (status NOT IN ('previewing','processing') + AND claim_token IS NULL AND processing_deadline_at IS NULL) + ), + CHECK ( + (status IN ('source_changed','invalid','failed') AND error_code IS NOT NULL) + OR status NOT IN ('source_changed','invalid','failed') + ), + CHECK ( + (status IN ('ready','already_current') + AND derivative_id IS NOT NULL AND completed_at IS NOT NULL) + OR status NOT IN ('ready','already_current') + ) +); + +CREATE INDEX IF NOT EXISTS idx_vehicle_image_crop_jobs_claim + ON public.vehicle_image_crop_jobs ( + run_id, status, next_attempt_at, asset_id, id + ) WHERE status IN ( + 'pending_preview','previewing','queued','processing','failed' + ); +CREATE INDEX IF NOT EXISTS idx_vehicle_image_crop_jobs_hash + ON public.vehicle_image_crop_jobs (run_id, preview_sha256) + WHERE preview_sha256 IS NOT NULL; + +ALTER TABLE public.storage_reconciliation_runs + ADD COLUMN IF NOT EXISTS max_vehicle_image_derivative_id BIGINT NOT NULL DEFAULT 0, + ADD COLUMN IF NOT EXISTS vehicle_image_derivative_cursor BIGINT NOT NULL DEFAULT 0; + +ALTER TABLE public.storage_reconciliation_runs + DROP CONSTRAINT IF EXISTS storage_reconciliation_phase; +ALTER TABLE public.storage_reconciliation_runs + ADD CONSTRAINT storage_reconciliation_phase CHECK ( + phase IN ( + 'filesystem', 'plate-reads', + 'vehicle-image-assets', 'vehicle-image-derivatives', 'completed' + ) + ); + +ALTER TABLE public.storage_reconciliation_runs + DROP CONSTRAINT IF EXISTS storage_reconciliation_counts; +ALTER TABLE public.storage_reconciliation_runs + ADD CONSTRAINT storage_reconciliation_counts CHECK ( + max_plate_read_id >= 0 + AND max_vehicle_image_asset_id >= 0 + AND max_vehicle_image_derivative_id >= 0 + AND plate_read_cursor >= 0 + AND vehicle_image_asset_cursor >= 0 + AND vehicle_image_derivative_cursor >= 0 + AND files_scanned >= 0 AND bytes_scanned >= 0 + AND references_checked >= 0 AND recent_files_skipped >= 0 + AND skipped_entries >= 0 AND error_count >= 0 + AND orphan_files >= 0 AND orphan_bytes >= 0 + AND missing_reference_paths >= 0 + ); + +INSERT INTO public.schema_migrations(version,description) VALUES + ('2026081406_vehicle_image_crop_campaign','Add inert asset-owned canonical Overview vehicle crops with preview-first bounded operator batches and storage-reference protection.') +ON CONFLICT(version) DO NOTHING; diff --git a/migrations/0056_2026081501_vehicle_image_crop_live_worker.sql b/migrations/0056_2026081501_vehicle_image_crop_live_worker.sql new file mode 100644 index 0000000..757d3d4 --- /dev/null +++ b/migrations/0056_2026081501_vehicle_image_crop_live_worker.sql @@ -0,0 +1,108 @@ +-- migration: 2026081501_vehicle_image_crop_live_worker +-- Durable, default-off crop generation for canonical Overview assets that +-- become identity-eligible after the operator-controlled crop campaign. The +-- migration queues no work. The low-priority worker reuses the exact campaign +-- crop algorithm and yields whenever an operator crop campaign is active. +CREATE TABLE IF NOT EXISTS public.vehicle_image_crop_live_control ( + singleton BOOLEAN PRIMARY KEY DEFAULT TRUE CHECK (singleton = TRUE), + enabled BOOLEAN NOT NULL DEFAULT TRUE, + enabled_by_user_id BIGINT REFERENCES public.users(id) ON DELETE SET NULL, + enabled_at TIMESTAMPTZ, + disabled_at TIMESTAMPTZ, + updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + CHECK ( + (enabled = TRUE AND enabled_at IS NOT NULL) + OR enabled = FALSE + ) +); + +INSERT INTO public.vehicle_image_crop_live_control ( + singleton, enabled, enabled_by_user_id, enabled_at, disabled_at +) VALUES (TRUE, TRUE, NULL, CURRENT_TIMESTAMP, NULL) +ON CONFLICT (singleton) DO NOTHING; + +CREATE TABLE IF NOT EXISTS public.vehicle_image_crop_live_jobs ( + id BIGSERIAL PRIMARY KEY, + -- Snapshot scalars deliberately avoid foreign keys so durable worker + -- evidence cannot block existing read or future asset-retention workflows. + asset_id BIGINT NOT NULL UNIQUE CHECK (asset_id > 0), + source_sha256 CHAR(64) NOT NULL CHECK ( + source_sha256 ~ '^[0-9a-f]{64}$' + ), + source_path TEXT NOT NULL CHECK (NULLIF(BTRIM(source_path), '') IS NOT NULL), + source_width INTEGER NOT NULL CHECK (source_width > 0), + source_height INTEGER NOT NULL CHECK (source_height > 0), + evidence_read_id INTEGER NOT NULL CHECK (evidence_read_id > 0), + evidence_source_kind VARCHAR(40) NOT NULL CHECK ( + evidence_source_kind IN ( + 'overview_primary','entry_overview_primary','overview_fallback', + 'overview_pair_share','entry_overview_route_fallback', + 'entry_overview_history' + ) + ), + evidence_source_path TEXT NOT NULL CHECK ( + NULLIF(BTRIM(evidence_source_path), '') IS NOT NULL + ), + evidence_source_updated_at TIMESTAMPTZ, + detection_box JSONB NOT NULL CHECK ( + jsonb_typeof(detection_box) = 'object' + AND detection_box ?& ARRAY['left','top','right','bottom'] + ), + detection_confidence REAL CHECK ( + detection_confidence IS NULL + OR (detection_confidence >= 0 AND detection_confidence <= 1) + ), + status VARCHAR(24) NOT NULL DEFAULT 'queued' CHECK ( + status IN ( + 'queued','processing','ready','already_current','source_changed', + 'unavailable','invalid','failed' + ) + ), + attempt_count INTEGER NOT NULL DEFAULT 0 CHECK ( + attempt_count >= 0 AND attempt_count <= 5 + ), + operator_retry_count INTEGER NOT NULL DEFAULT 0 CHECK ( + operator_retry_count >= 0 AND operator_retry_count <= 1 + ), + retryable BOOLEAN NOT NULL DEFAULT TRUE, + claim_token UUID, + heartbeat_at TIMESTAMPTZ, + processing_deadline_at TIMESTAMPTZ, + next_attempt_at TIMESTAMPTZ, + error_code VARCHAR(80), + error_details JSONB CHECK ( + error_details IS NULL OR jsonb_typeof(error_details) = 'object' + ), + derivative_id BIGINT REFERENCES public.vehicle_image_derivatives(id) + ON DELETE SET NULL, + completed_at TIMESTAMPTZ, + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + CHECK ( + (status = 'processing' + AND claim_token IS NOT NULL AND processing_deadline_at IS NOT NULL) + OR (status <> 'processing' + AND claim_token IS NULL AND processing_deadline_at IS NULL) + ), + CHECK ( + (status IN ('source_changed','unavailable','invalid','failed') + AND error_code IS NOT NULL) + OR status NOT IN ('source_changed','unavailable','invalid','failed') + ), + CHECK ( + (status IN ('ready','already_current') + AND derivative_id IS NOT NULL AND completed_at IS NOT NULL) + OR status NOT IN ('ready','already_current') + ) +); + +CREATE INDEX IF NOT EXISTS idx_vehicle_image_crop_live_claim + ON public.vehicle_image_crop_live_jobs ( + next_attempt_at, asset_id, id + ) WHERE status IN ('queued','processing','failed'); +CREATE INDEX IF NOT EXISTS idx_vehicle_image_crop_live_status + ON public.vehicle_image_crop_live_jobs (status, updated_at DESC, id DESC); + +INSERT INTO public.schema_migrations(version,description) VALUES + ('2026081501_vehicle_image_crop_live_worker','Add default-off automatic canonical Overview crop generation after a completed operator crop campaign.') +ON CONFLICT(version) DO NOTHING; diff --git a/migrations/0057_2026081502_vehicle_overview_framing_repair.sql b/migrations/0057_2026081502_vehicle_overview_framing_repair.sql new file mode 100644 index 0000000..568bca1 --- /dev/null +++ b/migrations/0057_2026081502_vehicle_overview_framing_repair.sql @@ -0,0 +1,157 @@ +-- migration: 2026081502_vehicle_overview_framing_repair +-- Operator-selected repair of genuinely clipped or overly tight direct +-- Overview images. Preview rows freeze the original ready image and its +-- acquisition profile. No migration work is queued, and the original image +-- remains the current Vehicle View until a replacement proves materially more +-- complete. Blur, exposure, and multi-vehicle review findings are never repair +-- eligibility by themselves. +ALTER TABLE public.plate_reads + DROP CONSTRAINT IF EXISTS plate_reads_vehicle_image_queue_kind_check; +ALTER TABLE public.plate_reads + ADD CONSTRAINT plate_reads_vehicle_image_queue_kind_check CHECK ( + vehicle_image_queue_kind IS NULL OR + vehicle_image_queue_kind IN ( + 'live','historical','manual','overview','overview_backfill','overview_repair' + ) + ); + +CREATE TABLE IF NOT EXISTS public.vehicle_overview_framing_repair_runs ( + id BIGSERIAL PRIMARY KEY, + status VARCHAR(16) NOT NULL DEFAULT 'previewed' CHECK ( + status IN ('previewed','running','completed','cancelled') + ), + preview_fingerprint CHAR(64) NOT NULL CHECK ( + preview_fingerprint ~ '^[0-9a-f]{64}$' + ), + candidate_count INTEGER NOT NULL DEFAULT 0 CHECK (candidate_count >= 0), + created_by_user_id BIGINT REFERENCES public.users(id) ON DELETE SET NULL, + confirmed_by_user_id BIGINT REFERENCES public.users(id) ON DELETE SET NULL, + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + confirmed_at TIMESTAMPTZ, + completed_at TIMESTAMPTZ, + cancelled_at TIMESTAMPTZ, + updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP +); + +CREATE UNIQUE INDEX IF NOT EXISTS idx_overview_framing_repair_one_active + ON public.vehicle_overview_framing_repair_runs ((TRUE)) + WHERE status IN ('previewed','running'); + +CREATE TABLE IF NOT EXISTS public.vehicle_overview_framing_repair_jobs ( + id BIGSERIAL PRIMARY KEY, + run_id BIGINT NOT NULL REFERENCES public.vehicle_overview_framing_repair_runs(id) + ON DELETE CASCADE, + -- Snapshot scalars intentionally have no plate_reads foreign key. Durable + -- repair evidence must not block the existing per-read delete operation. + read_id INTEGER NOT NULL CHECK (read_id > 0), + plate_number VARCHAR(32), + camera_name VARCHAR(120) NOT NULL CHECK (NULLIF(BTRIM(camera_name), '') IS NOT NULL), + read_timestamp_text TEXT NOT NULL CHECK (NULLIF(BTRIM(read_timestamp_text), '') IS NOT NULL), + direction_label VARCHAR(80), + prior_image_path TEXT NOT NULL CHECK (NULLIF(BTRIM(prior_image_path), '') IS NOT NULL), + prior_image_status VARCHAR(24) NOT NULL CHECK (prior_image_status = 'ready'), + prior_queue_kind VARCHAR(24), + prior_attempt_count INTEGER NOT NULL CHECK (prior_attempt_count >= 0), + prior_retryable BOOLEAN NOT NULL, + prior_error_code VARCHAR(80), + prior_source_kind VARCHAR(40) NOT NULL CHECK ( + prior_source_kind IN ('overview_primary','entry_overview_primary') + ), + prior_source_read_id INTEGER, + prior_image_timestamp_text TEXT, + prior_image_score REAL, + prior_detection_confidence REAL CHECK ( + prior_detection_confidence IS NULL + OR (prior_detection_confidence >= 0 AND prior_detection_confidence <= 1) + ), + prior_detection_box JSONB CHECK ( + prior_detection_box IS NULL OR jsonb_typeof(prior_detection_box) = 'object' + ), + prior_image_width INTEGER CHECK (prior_image_width IS NULL OR prior_image_width > 0), + prior_image_height INTEGER CHECK (prior_image_height IS NULL OR prior_image_height > 0), + prior_sampled_count INTEGER CHECK (prior_sampled_count IS NULL OR prior_sampled_count >= 0), + prior_selection_metadata JSONB NOT NULL DEFAULT '{}'::jsonb CHECK ( + jsonb_typeof(prior_selection_metadata) = 'object' + ), + prior_image_updated_at_text TEXT NOT NULL CHECK ( + NULLIF(BTRIM(prior_image_updated_at_text), '') IS NOT NULL + ), + audited_detection_box JSONB NOT NULL CHECK ( + jsonb_typeof(audited_detection_box) = 'object' + AND audited_detection_box ?& ARRAY['left','top','right','bottom'] + ), + audited_completeness_tier SMALLINT NOT NULL CHECK ( + audited_completeness_tier IN (0,1) + ), + audited_edge_margin REAL NOT NULL CHECK (audited_edge_margin >= 0 AND audited_edge_margin < 0.015), + audited_edge_contacts SMALLINT NOT NULL CHECK (audited_edge_contacts BETWEEN 0 AND 4), + repair_reason VARCHAR(40) NOT NULL CHECK ( + repair_reason IN ('VEHICLE_TOUCHES_IMAGE_EDGE','VEHICLE_FRAMING_TOO_TIGHT') + ), + profile_id BIGINT NOT NULL CHECK (profile_id > 0), + profile_revision BIGINT NOT NULL CHECK (profile_revision > 0), + overview_context VARCHAR(12) NOT NULL CHECK (overview_context IN ('street','entry')), + source_camera_name VARCHAR(120) NOT NULL CHECK (NULLIF(BTRIM(source_camera_name), '') IS NOT NULL), + source_camera_short_name VARCHAR(80), + expected_delta_ms INTEGER NOT NULL CHECK (ABS(expected_delta_ms) <= 30000), + tolerance_ms INTEGER NOT NULL CHECK (tolerance_ms BETWEEN 250 AND 3000), + status VARCHAR(20) NOT NULL DEFAULT 'previewed' CHECK ( + status IN ( + 'previewed','queued','processing','repaired','preserved', + 'source_changed','unavailable','failed','cancelled' + ) + ), + attempt_count SMALLINT NOT NULL DEFAULT 0 CHECK (attempt_count BETWEEN 0 AND 2), + retryable BOOLEAN NOT NULL DEFAULT TRUE, + claim_token UUID, + heartbeat_at TIMESTAMPTZ, + processing_deadline_at TIMESTAMPTZ, + hard_deadline_at TIMESTAMPTZ, + next_attempt_at TIMESTAMPTZ, + error_code VARCHAR(80), + error_details JSONB CHECK ( + error_details IS NULL OR jsonb_typeof(error_details) = 'object' + ), + replacement_image_path TEXT, + replacement_detection_box JSONB CHECK ( + replacement_detection_box IS NULL OR jsonb_typeof(replacement_detection_box) = 'object' + ), + replacement_completeness_tier SMALLINT CHECK ( + replacement_completeness_tier IS NULL OR replacement_completeness_tier BETWEEN 0 AND 3 + ), + replacement_edge_margin REAL CHECK ( + replacement_edge_margin IS NULL OR replacement_edge_margin BETWEEN 0 AND 1 + ), + completed_at TIMESTAMPTZ, + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + UNIQUE (run_id, read_id), + CHECK ( + (status = 'processing' AND claim_token IS NOT NULL + AND processing_deadline_at IS NOT NULL AND hard_deadline_at IS NOT NULL) + OR (status <> 'processing' AND claim_token IS NULL + AND processing_deadline_at IS NULL AND hard_deadline_at IS NULL) + ), + CHECK ( + (status IN ('source_changed','unavailable','failed','preserved') AND error_code IS NOT NULL) + OR status NOT IN ('source_changed','unavailable','failed','preserved') + ), + CHECK ( + (status = 'repaired' AND replacement_image_path IS NOT NULL + AND replacement_detection_box IS NOT NULL + AND replacement_completeness_tier IS NOT NULL + AND replacement_edge_margin IS NOT NULL AND completed_at IS NOT NULL) + OR status <> 'repaired' + ) +); + +CREATE INDEX IF NOT EXISTS idx_overview_framing_repair_jobs_claim + ON public.vehicle_overview_framing_repair_jobs ( + status, next_attempt_at, id + ) WHERE status IN ('queued','processing','failed'); +CREATE INDEX IF NOT EXISTS idx_overview_framing_repair_jobs_run + ON public.vehicle_overview_framing_repair_jobs (run_id, status, id); + +INSERT INTO public.schema_migrations(version,description) VALUES + ('2026081502_vehicle_overview_framing_repair','Add inert preview-first bounded repair jobs for operator-selected edge-clipped or overly tight direct Overview images while preserving the current image unless a replacement proves more complete.') +ON CONFLICT(version) DO NOTHING; diff --git a/migrations/0058_2026081503_overview_framing_repair_export_identity.sql b/migrations/0058_2026081503_overview_framing_repair_export_identity.sql new file mode 100644 index 0000000..a076682 --- /dev/null +++ b/migrations/0058_2026081503_overview_framing_repair_export_identity.sql @@ -0,0 +1,14 @@ +-- migration: 2026081503_overview_framing_repair_export_identity +-- A framing repair is a distinct semantic export for the same read and source +-- window. Its per-job identity prevents collisions with the original live +-- export while keeping retries and worker restarts idempotent. +ALTER TABLE public.blue_iris_timeline_exports + DROP CONSTRAINT IF EXISTS blue_iris_timeline_exports_profile_kind_check; +ALTER TABLE public.blue_iris_timeline_exports + ADD CONSTRAINT blue_iris_timeline_exports_profile_kind_check CHECK ( + profile_kind IS NULL OR profile_kind IN ('pair','entry_history','framing_repair') + ) NOT VALID; + +INSERT INTO public.schema_migrations(version,description) VALUES + ('2026081503_overview_framing_repair_export_identity','Give each guarded Overview framing repair a stable distinct timeline-export identity while permitting its claim-owned ready Vehicle View to use the export ledger.') +ON CONFLICT(version) DO NOTHING; diff --git a/migrations/0059_2026081504_withdraw_overview_framing_repair.sql b/migrations/0059_2026081504_withdraw_overview_framing_repair.sql new file mode 100644 index 0000000..252e6d7 --- /dev/null +++ b/migrations/0059_2026081504_withdraw_overview_framing_repair.sql @@ -0,0 +1,82 @@ +-- migration: 2026081504_withdraw_overview_framing_repair +-- The saved-pixel framing audit and operator repair campaign were withdrawn +-- after production canaries. Preserve the additive tables as immutable audit +-- history, restore any read still owned by an interrupted repair claim to its +-- frozen prior ready state, and terminalize all work that could otherwise be +-- claimed by an older application image during a rolling rollback. +DO $$ +BEGIN + IF to_regclass('public.vehicle_overview_framing_repair_jobs') IS NOT NULL + AND to_regclass('public.vehicle_overview_framing_repair_runs') IS NOT NULL THEN + WITH prior AS MATERIALIZED ( + SELECT DISTINCT ON (reads.id) + reads.id AS read_id, + jobs.prior_queue_kind, + jobs.prior_attempt_count, + jobs.prior_retryable, + jobs.prior_error_code, + jobs.prior_image_updated_at_text + FROM public.plate_reads reads + JOIN public.vehicle_overview_framing_repair_jobs jobs + ON jobs.read_id = reads.id + WHERE reads.vehicle_image_queue_kind = 'overview_repair' + ORDER BY reads.id, + (jobs.claim_token IS NOT DISTINCT FROM reads.vehicle_image_claim_token) DESC, + jobs.updated_at DESC, + jobs.id DESC + ) + UPDATE public.plate_reads reads + SET vehicle_image_status = 'ready', + vehicle_image_queue_kind = prior.prior_queue_kind, + vehicle_image_attempt_count = prior.prior_attempt_count, + vehicle_image_retryable = prior.prior_retryable, + vehicle_image_error_code = prior.prior_error_code, + vehicle_image_claim_token = NULL, + vehicle_image_heartbeat_at = NULL, + vehicle_image_processing_deadline_at = NULL, + vehicle_image_hard_deadline_at = NULL, + vehicle_image_next_attempt_at = NULL, + vehicle_image_updated_at = prior.prior_image_updated_at_text::timestamptz + FROM prior + WHERE reads.id = prior.read_id; + + UPDATE public.blue_iris_timeline_exports + SET status = 'failed', + error_code = 'OVERVIEW_FRAMING_REPAIR_WITHDRAWN', + error_details = jsonb_build_object( + 'reason', 'FEATURE_WITHDRAWN', + 'migration', '2026081504_withdraw_overview_framing_repair' + ), + updated_at = CURRENT_TIMESTAMP + WHERE profile_kind = 'framing_repair' + AND status IN ('starting','exporting','ready'); + + UPDATE public.vehicle_overview_framing_repair_jobs + SET status = 'cancelled', + retryable = FALSE, + claim_token = NULL, + heartbeat_at = NULL, + processing_deadline_at = NULL, + hard_deadline_at = NULL, + next_attempt_at = NULL, + error_code = NULL, + error_details = jsonb_build_object( + 'reason', 'FEATURE_WITHDRAWN', + 'migration', '2026081504_withdraw_overview_framing_repair' + ), + completed_at = COALESCE(completed_at, CURRENT_TIMESTAMP), + updated_at = CURRENT_TIMESTAMP + WHERE status IN ('previewed','queued','processing') + OR (status = 'failed' AND retryable = TRUE); + + UPDATE public.vehicle_overview_framing_repair_runs + SET status = 'cancelled', + cancelled_at = COALESCE(cancelled_at, CURRENT_TIMESTAMP), + updated_at = CURRENT_TIMESTAMP + WHERE status IN ('previewed','running'); + END IF; +END $$; + +INSERT INTO public.schema_migrations(version,description) VALUES + ('2026081504_withdraw_overview_framing_repair','Withdraw the saved-pixel framing audit and operator repair workflow while restoring any interrupted read and retaining prior repair evidence as inert audit history.') +ON CONFLICT(version) DO NOTHING; diff --git a/migrations/0060_2026081505_vehicle_asset_embedding_campaign.sql b/migrations/0060_2026081505_vehicle_asset_embedding_campaign.sql new file mode 100644 index 0000000..32b662e --- /dev/null +++ b/migrations/0060_2026081505_vehicle_asset_embedding_campaign.sql @@ -0,0 +1,181 @@ +-- migration: 2026081505_vehicle_asset_embedding_campaign +-- Provider-neutral embeddings for immutable canonical Overview crop bytes. +-- This migration is deliberately inert: it creates no run, job, embedding, +-- profile, cluster, attribute, ReID assignment, or external-provider request. +CREATE TABLE IF NOT EXISTS public.vehicle_asset_embeddings ( + id BIGSERIAL PRIMARY KEY, + derivative_id BIGINT NOT NULL REFERENCES public.vehicle_image_derivatives(id) + ON DELETE RESTRICT, + model_name VARCHAR(100) NOT NULL CHECK ( + NULLIF(BTRIM(model_name), '') IS NOT NULL + ), + algorithm_version VARCHAR(100) NOT NULL CHECK ( + NULLIF(BTRIM(algorithm_version), '') IS NOT NULL + ), + source_sha256 CHAR(64) NOT NULL CHECK ( + source_sha256 ~ '^[0-9a-f]{64}$' + ), + embedding_sha256 CHAR(64) NOT NULL CHECK ( + embedding_sha256 ~ '^[0-9a-f]{64}$' + ), + embedding_dimensions SMALLINT NOT NULL CHECK ( + embedding_dimensions = 512 + ), + embedding BYTEA NOT NULL CHECK (OCTET_LENGTH(embedding) = 2048), + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + UNIQUE (derivative_id, model_name, algorithm_version) +); + +CREATE INDEX IF NOT EXISTS idx_vehicle_asset_embeddings_model + ON public.vehicle_asset_embeddings (model_name, algorithm_version, derivative_id); +CREATE INDEX IF NOT EXISTS idx_vehicle_asset_embeddings_content + ON public.vehicle_asset_embeddings (embedding_sha256, id); + +CREATE OR REPLACE FUNCTION public.prevent_vehicle_asset_embedding_update() +RETURNS TRIGGER AS $$ +BEGIN + RAISE EXCEPTION 'vehicle_asset_embeddings content is immutable'; +END; +$$ LANGUAGE plpgsql; + +DROP TRIGGER IF EXISTS vehicle_asset_embeddings_immutable + ON public.vehicle_asset_embeddings; +CREATE TRIGGER vehicle_asset_embeddings_immutable +BEFORE UPDATE ON public.vehicle_asset_embeddings +FOR EACH ROW EXECUTE FUNCTION public.prevent_vehicle_asset_embedding_update(); + +CREATE TABLE IF NOT EXISTS public.vehicle_asset_embedding_runs ( + id BIGSERIAL PRIMARY KEY, + status VARCHAR(16) NOT NULL DEFAULT 'previewing' CHECK ( + status IN ('previewing','ready','running','paused','completed','cancelled','failed') + ), + max_derivative_id BIGINT NOT NULL CHECK (max_derivative_id >= 0), + model_name VARCHAR(100) NOT NULL CHECK ( + NULLIF(BTRIM(model_name), '') IS NOT NULL + ), + algorithm_version VARCHAR(100) NOT NULL CHECK ( + NULLIF(BTRIM(algorithm_version), '') IS NOT NULL + ), + preview_fingerprint CHAR(64) CHECK ( + preview_fingerprint IS NULL OR preview_fingerprint ~ '^[0-9a-f]{64}$' + ), + batch_size INTEGER NOT NULL DEFAULT 5 CHECK ( + batch_size IN (1,5,25,250) + ), + actor_user_id BIGINT NOT NULL REFERENCES public.users(id) ON DELETE RESTRICT, + confirmed_actor_user_id BIGINT REFERENCES public.users(id) ON DELETE RESTRICT, + confirmed_at TIMESTAMPTZ, + paused_at TIMESTAMPTZ, + cancelled_at TIMESTAMPTZ, + completed_at TIMESTAMPTZ, + last_error_code VARCHAR(80), + last_error_details JSONB CHECK ( + last_error_details IS NULL OR jsonb_typeof(last_error_details) = 'object' + ), + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + CHECK ( + (status = 'previewing' AND preview_fingerprint IS NULL) + OR (status IN ('ready','running','paused','completed') + AND preview_fingerprint IS NOT NULL) + OR status IN ('cancelled','failed') + ) +); + +CREATE UNIQUE INDEX IF NOT EXISTS idx_vehicle_asset_embedding_one_active + ON public.vehicle_asset_embedding_runs ((TRUE)) + WHERE status IN ('previewing','ready','running','paused'); + +CREATE TABLE IF NOT EXISTS public.vehicle_asset_embedding_jobs ( + id BIGSERIAL PRIMARY KEY, + run_id BIGINT NOT NULL REFERENCES public.vehicle_asset_embedding_runs(id) + ON DELETE RESTRICT, + derivative_id BIGINT NOT NULL REFERENCES public.vehicle_image_derivatives(id) + ON DELETE RESTRICT, + asset_id BIGINT NOT NULL REFERENCES public.vehicle_image_assets(id) + ON DELETE RESTRICT, + source_sha256 CHAR(64) NOT NULL CHECK ( + source_sha256 ~ '^[0-9a-f]{64}$' + ), + source_path TEXT NOT NULL CHECK (NULLIF(BTRIM(source_path), '') IS NOT NULL), + source_width INTEGER NOT NULL CHECK (source_width > 0), + source_height INTEGER NOT NULL CHECK (source_height > 0), + source_algorithm_version VARCHAR(100) NOT NULL CHECK ( + NULLIF(BTRIM(source_algorithm_version), '') IS NOT NULL + ), + evidence_read_id INTEGER NOT NULL CHECK (evidence_read_id > 0), + evidence_source_kind VARCHAR(40) NOT NULL, + evidence_source_path TEXT NOT NULL CHECK ( + NULLIF(BTRIM(evidence_source_path), '') IS NOT NULL + ), + evidence_source_updated_at TIMESTAMPTZ, + status VARCHAR(20) NOT NULL DEFAULT 'pending_preview' CHECK ( + status IN ( + 'pending_preview','previewing','previewed','queued','processing', + 'ready','already_current','source_changed','invalid','failed','cancelled' + ) + ), + failure_stage VARCHAR(12) CHECK ( + failure_stage IS NULL OR failure_stage IN ('preview','embed') + ), + preview_embedding_sha256 CHAR(64) CHECK ( + preview_embedding_sha256 IS NULL OR preview_embedding_sha256 ~ '^[0-9a-f]{64}$' + ), + preview_embedding_dimensions SMALLINT CHECK ( + preview_embedding_dimensions IS NULL OR preview_embedding_dimensions = 512 + ), + preview_embedding_bytes INTEGER CHECK ( + preview_embedding_bytes IS NULL OR preview_embedding_bytes = 2048 + ), + attempt_count SMALLINT NOT NULL DEFAULT 0 CHECK (attempt_count BETWEEN 0 AND 3), + operator_retry_count SMALLINT NOT NULL DEFAULT 0 CHECK ( + operator_retry_count BETWEEN 0 AND 1 + ), + retryable BOOLEAN NOT NULL DEFAULT FALSE, + claim_token UUID, + processing_deadline_at TIMESTAMPTZ, + next_attempt_at TIMESTAMPTZ, + error_code VARCHAR(80), + error_details JSONB CHECK ( + error_details IS NULL OR jsonb_typeof(error_details) = 'object' + ), + embedding_id BIGINT REFERENCES public.vehicle_asset_embeddings(id) + ON DELETE SET NULL, + previewed_at TIMESTAMPTZ, + completed_at TIMESTAMPTZ, + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + UNIQUE (run_id, derivative_id), + CHECK ( + (status IN ('previewed','queued','processing','ready','already_current') + AND preview_embedding_sha256 IS NOT NULL + AND preview_embedding_dimensions = 512 + AND preview_embedding_bytes = 2048) + OR status NOT IN ('previewed','queued','processing','ready','already_current') + ), + CHECK ( + (status IN ('previewing','processing') + AND claim_token IS NOT NULL AND processing_deadline_at IS NOT NULL) + OR (status NOT IN ('previewing','processing') + AND claim_token IS NULL AND processing_deadline_at IS NULL) + ), + CHECK ( + (status IN ('source_changed','invalid','failed') AND error_code IS NOT NULL) + OR status NOT IN ('source_changed','invalid','failed') + ), + CHECK ( + (status IN ('ready','already_current') + AND embedding_id IS NOT NULL AND completed_at IS NOT NULL) + OR status NOT IN ('ready','already_current') + ) +); + +CREATE INDEX IF NOT EXISTS idx_vehicle_asset_embedding_jobs_run + ON public.vehicle_asset_embedding_jobs (run_id, status, derivative_id, id); +CREATE INDEX IF NOT EXISTS idx_vehicle_asset_embedding_jobs_claim + ON public.vehicle_asset_embedding_jobs (status, next_attempt_at, derivative_id, id) + WHERE status IN ('pending_preview','previewing','queued','processing','failed'); + +INSERT INTO public.schema_migrations(version,description) VALUES + ('2026081505_vehicle_asset_embedding_campaign','Add inert provider-neutral canonical crop embeddings and a preview-first operator campaign without enabling any ReID v2 consumer.') +ON CONFLICT(version) DO NOTHING; diff --git a/migrations/0061_2026081506_vehicle_asset_attribute_campaign.sql b/migrations/0061_2026081506_vehicle_asset_attribute_campaign.sql new file mode 100644 index 0000000..facd15d --- /dev/null +++ b/migrations/0061_2026081506_vehicle_asset_attribute_campaign.sql @@ -0,0 +1,198 @@ +-- migration: 2026081506_vehicle_asset_attribute_campaign +-- Provider-neutral local attribute observations for immutable canonical crop bytes. +-- This migration is deliberately inert: it creates no run or job and does not +-- alter current read-owned attributes, ReID, assignments, events, or providers. +CREATE TABLE IF NOT EXISTS public.vehicle_asset_attribute_observations ( + id BIGSERIAL PRIMARY KEY, + derivative_id BIGINT NOT NULL REFERENCES public.vehicle_image_derivatives(id) + ON DELETE RESTRICT, + attribute_key VARCHAR(40) NOT NULL CHECK ( + attribute_key IN ('color','body_type') + ), + provider VARCHAR(100) NOT NULL CHECK ( + NULLIF(BTRIM(provider), '') IS NOT NULL + ), + model_version VARCHAR(120) NOT NULL CHECK ( + NULLIF(BTRIM(model_version), '') IS NOT NULL + ), + algorithm_version VARCHAR(100) NOT NULL CHECK ( + NULLIF(BTRIM(algorithm_version), '') IS NOT NULL + ), + source_sha256 CHAR(64) NOT NULL CHECK ( + source_sha256 ~ '^[0-9a-f]{64}$' + ), + status VARCHAR(12) NOT NULL CHECK (status IN ('ready','unknown')), + attribute_value VARCHAR(120), + confidence REAL CHECK ( + confidence IS NULL OR (confidence >= 0 AND confidence <= 1) + ), + result_sha256 CHAR(64) NOT NULL CHECK ( + result_sha256 ~ '^[0-9a-f]{64}$' + ), + raw_result JSONB NOT NULL CHECK (jsonb_typeof(raw_result) = 'object'), + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + UNIQUE ( + derivative_id, attribute_key, provider, model_version, algorithm_version + ), + CHECK ( + (status = 'ready' AND NULLIF(BTRIM(attribute_value), '') IS NOT NULL + AND confidence IS NOT NULL) + OR (status = 'unknown' AND attribute_value IS NULL) + ) +); + +CREATE INDEX IF NOT EXISTS idx_vehicle_asset_attribute_observations_contract + ON public.vehicle_asset_attribute_observations ( + attribute_key, provider, model_version, algorithm_version, derivative_id + ); +CREATE INDEX IF NOT EXISTS idx_vehicle_asset_attribute_observations_value + ON public.vehicle_asset_attribute_observations ( + attribute_key, attribute_value, confidence DESC, derivative_id + ) WHERE status = 'ready'; + +CREATE OR REPLACE FUNCTION public.prevent_vehicle_asset_attribute_observation_update() +RETURNS TRIGGER AS $$ +BEGIN + RAISE EXCEPTION 'vehicle_asset_attribute_observations content is immutable'; +END; +$$ LANGUAGE plpgsql; + +DROP TRIGGER IF EXISTS vehicle_asset_attribute_observations_immutable + ON public.vehicle_asset_attribute_observations; +CREATE TRIGGER vehicle_asset_attribute_observations_immutable +BEFORE UPDATE ON public.vehicle_asset_attribute_observations +FOR EACH ROW EXECUTE FUNCTION public.prevent_vehicle_asset_attribute_observation_update(); + +CREATE TABLE IF NOT EXISTS public.vehicle_asset_attribute_runs ( + id BIGSERIAL PRIMARY KEY, + status VARCHAR(16) NOT NULL DEFAULT 'previewing' CHECK ( + status IN ('previewing','ready','running','paused','completed','cancelled','failed') + ), + max_derivative_id BIGINT NOT NULL CHECK (max_derivative_id >= 0), + algorithm_version VARCHAR(100) NOT NULL CHECK ( + NULLIF(BTRIM(algorithm_version), '') IS NOT NULL + ), + preview_fingerprint CHAR(64) CHECK ( + preview_fingerprint IS NULL OR preview_fingerprint ~ '^[0-9a-f]{64}$' + ), + batch_size INTEGER NOT NULL DEFAULT 5 CHECK ( + batch_size IN (1,5,25,250) + ), + actor_user_id BIGINT NOT NULL REFERENCES public.users(id) ON DELETE RESTRICT, + confirmed_actor_user_id BIGINT REFERENCES public.users(id) ON DELETE RESTRICT, + confirmed_at TIMESTAMPTZ, + paused_at TIMESTAMPTZ, + cancelled_at TIMESTAMPTZ, + completed_at TIMESTAMPTZ, + last_error_code VARCHAR(80), + last_error_details JSONB CHECK ( + last_error_details IS NULL OR jsonb_typeof(last_error_details) = 'object' + ), + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + CHECK ( + (status = 'previewing' AND preview_fingerprint IS NULL) + OR (status IN ('ready','running','paused','completed') + AND preview_fingerprint IS NOT NULL) + OR status IN ('cancelled','failed') + ) +); + +CREATE UNIQUE INDEX IF NOT EXISTS idx_vehicle_asset_attribute_one_active + ON public.vehicle_asset_attribute_runs ((TRUE)) + WHERE status IN ('previewing','ready','running','paused'); + +CREATE TABLE IF NOT EXISTS public.vehicle_asset_attribute_jobs ( + id BIGSERIAL PRIMARY KEY, + run_id BIGINT NOT NULL REFERENCES public.vehicle_asset_attribute_runs(id) + ON DELETE RESTRICT, + derivative_id BIGINT NOT NULL REFERENCES public.vehicle_image_derivatives(id) + ON DELETE RESTRICT, + asset_id BIGINT NOT NULL REFERENCES public.vehicle_image_assets(id) + ON DELETE RESTRICT, + source_sha256 CHAR(64) NOT NULL CHECK ( + source_sha256 ~ '^[0-9a-f]{64}$' + ), + source_path TEXT NOT NULL CHECK (NULLIF(BTRIM(source_path), '') IS NOT NULL), + source_width INTEGER NOT NULL CHECK (source_width > 0), + source_height INTEGER NOT NULL CHECK (source_height > 0), + source_algorithm_version VARCHAR(100) NOT NULL CHECK ( + NULLIF(BTRIM(source_algorithm_version), '') IS NOT NULL + ), + evidence_read_id INTEGER NOT NULL CHECK (evidence_read_id > 0), + evidence_source_kind VARCHAR(40) NOT NULL, + evidence_source_path TEXT NOT NULL CHECK ( + NULLIF(BTRIM(evidence_source_path), '') IS NOT NULL + ), + evidence_source_updated_at TIMESTAMPTZ, + status VARCHAR(20) NOT NULL DEFAULT 'pending_preview' CHECK ( + status IN ( + 'pending_preview','previewing','previewed','queued','processing', + 'ready','already_current','source_changed','invalid','failed','cancelled' + ) + ), + failure_stage VARCHAR(12) CHECK ( + failure_stage IS NULL OR failure_stage IN ('preview','observe') + ), + preview_result_sha256 CHAR(64) CHECK ( + preview_result_sha256 IS NULL OR preview_result_sha256 ~ '^[0-9a-f]{64}$' + ), + preview_result JSONB CHECK ( + preview_result IS NULL OR jsonb_typeof(preview_result) = 'object' + ), + preview_result_bytes INTEGER CHECK ( + preview_result_bytes IS NULL OR preview_result_bytes > 0 + ), + attempt_count SMALLINT NOT NULL DEFAULT 0 CHECK (attempt_count BETWEEN 0 AND 3), + operator_retry_count SMALLINT NOT NULL DEFAULT 0 CHECK ( + operator_retry_count BETWEEN 0 AND 1 + ), + retryable BOOLEAN NOT NULL DEFAULT FALSE, + claim_token UUID, + processing_deadline_at TIMESTAMPTZ, + next_attempt_at TIMESTAMPTZ, + error_code VARCHAR(80), + error_details JSONB CHECK ( + error_details IS NULL OR jsonb_typeof(error_details) = 'object' + ), + observations_created SMALLINT CHECK ( + observations_created IS NULL OR observations_created BETWEEN 0 AND 2 + ), + previewed_at TIMESTAMPTZ, + completed_at TIMESTAMPTZ, + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + UNIQUE (run_id, derivative_id), + CHECK ( + (status IN ('previewed','queued','processing','ready','already_current') + AND preview_result_sha256 IS NOT NULL + AND preview_result IS NOT NULL + AND preview_result_bytes > 0) + OR status NOT IN ('previewed','queued','processing','ready','already_current') + ), + CHECK ( + (status IN ('previewing','processing') + AND claim_token IS NOT NULL AND processing_deadline_at IS NOT NULL) + OR (status NOT IN ('previewing','processing') + AND claim_token IS NULL AND processing_deadline_at IS NULL) + ), + CHECK ( + (status IN ('source_changed','invalid','failed') AND error_code IS NOT NULL) + OR status NOT IN ('source_changed','invalid','failed') + ), + CHECK ( + (status IN ('ready','already_current') + AND observations_created IS NOT NULL AND completed_at IS NOT NULL) + OR status NOT IN ('ready','already_current') + ) +); + +CREATE INDEX IF NOT EXISTS idx_vehicle_asset_attribute_jobs_run + ON public.vehicle_asset_attribute_jobs (run_id, status, derivative_id, id); +CREATE INDEX IF NOT EXISTS idx_vehicle_asset_attribute_jobs_claim + ON public.vehicle_asset_attribute_jobs (status, next_attempt_at, derivative_id, id) + WHERE status IN ('pending_preview','previewing','queued','processing','failed'); + +INSERT INTO public.schema_migrations(version,description) VALUES + ('2026081506_vehicle_asset_attribute_campaign','Add inert immutable crop-owned local color and body-type observations plus a preview-first operator campaign without changing current ReID or external providers.') +ON CONFLICT(version) DO NOTHING; diff --git a/migrations/0062_2026081507_vehicle_reid_v2_pair_reviews.sql b/migrations/0062_2026081507_vehicle_reid_v2_pair_reviews.sql new file mode 100644 index 0000000..a514339 --- /dev/null +++ b/migrations/0062_2026081507_vehicle_reid_v2_pair_reviews.sql @@ -0,0 +1,73 @@ +-- migration: 2026081507_vehicle_reid_v2_pair_reviews +-- Human ReID v2 labels belong to an immutable canonical crop pair and exact +-- embedding contract. They are calibration evidence only: this migration +-- creates no profile, cluster, assignment, threshold, queue, or provider call. +CREATE TABLE IF NOT EXISTS public.vehicle_reid_v2_pair_reviews ( + id BIGSERIAL PRIMARY KEY, + derivative_id_low BIGINT NOT NULL + REFERENCES public.vehicle_image_derivatives(id) ON DELETE RESTRICT, + derivative_id_high BIGINT NOT NULL + REFERENCES public.vehicle_image_derivatives(id) ON DELETE RESTRICT, + source_sha256_low CHAR(64) NOT NULL CHECK ( + source_sha256_low ~ '^[0-9a-f]{64}$' + ), + source_sha256_high CHAR(64) NOT NULL CHECK ( + source_sha256_high ~ '^[0-9a-f]{64}$' + ), + embedding_id_low BIGINT NOT NULL + REFERENCES public.vehicle_asset_embeddings(id) ON DELETE RESTRICT, + embedding_id_high BIGINT NOT NULL + REFERENCES public.vehicle_asset_embeddings(id) ON DELETE RESTRICT, + embedding_model VARCHAR(100) NOT NULL CHECK ( + NULLIF(BTRIM(embedding_model), '') IS NOT NULL + ), + algorithm_version VARCHAR(100) NOT NULL CHECK ( + NULLIF(BTRIM(algorithm_version), '') IS NOT NULL + ), + similarity_score DOUBLE PRECISION NOT NULL CHECK ( + similarity_score >= -1 AND similarity_score <= 1 + ), + label VARCHAR(24) NOT NULL CHECK ( + label IN ('same_vehicle','different_vehicle','unsure') + ), + evidence_read_id_low INTEGER NOT NULL CHECK (evidence_read_id_low > 0), + evidence_read_id_high INTEGER NOT NULL CHECK (evidence_read_id_high > 0), + evidence_plate_low TEXT, + evidence_plate_high TEXT, + evidence_camera_low VARCHAR(120), + evidence_camera_high VARCHAR(120), + evidence_context_low VARCHAR(12) NOT NULL CHECK ( + evidence_context_low IN ('street','entry') + ), + evidence_context_high VARCHAR(12) NOT NULL CHECK ( + evidence_context_high IN ('street','entry') + ), + actor_user_id BIGINT REFERENCES public.users(id) ON DELETE SET NULL, + actor_username VARCHAR(64) NOT NULL CHECK ( + NULLIF(BTRIM(actor_username), '') IS NOT NULL + ), + actor_display_name VARCHAR(120) NOT NULL CHECK ( + NULLIF(BTRIM(actor_display_name), '') IS NOT NULL + ), + revision INTEGER NOT NULL DEFAULT 1 CHECK (revision > 0), + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + CHECK (derivative_id_low < derivative_id_high), + CHECK (embedding_id_low <> embedding_id_high), + CHECK (evidence_plate_low IS NULL OR NULLIF(BTRIM(evidence_plate_low), '') IS NOT NULL), + CHECK (evidence_plate_high IS NULL OR NULLIF(BTRIM(evidence_plate_high), '') IS NOT NULL), + UNIQUE (derivative_id_low, derivative_id_high, embedding_model, algorithm_version) +); + +CREATE INDEX IF NOT EXISTS idx_vehicle_reid_v2_pair_reviews_contract_label + ON public.vehicle_reid_v2_pair_reviews ( + embedding_model, algorithm_version, label, updated_at DESC, id DESC + ); +CREATE INDEX IF NOT EXISTS idx_vehicle_reid_v2_pair_reviews_high + ON public.vehicle_reid_v2_pair_reviews ( + derivative_id_high, embedding_model, algorithm_version + ); + +INSERT INTO public.schema_migrations(version,description) VALUES + ('2026081507_vehicle_reid_v2_pair_reviews','Add audited crop-owned ReID v2 Same, Different, and Unsure pair labels for human calibration without changing profiles or assignments.') +ON CONFLICT(version) DO NOTHING; diff --git a/migrations/0063_2026081601_vehicle_reid_v2_diverse_review_campaign.sql b/migrations/0063_2026081601_vehicle_reid_v2_diverse_review_campaign.sql new file mode 100644 index 0000000..0203cb6 --- /dev/null +++ b/migrations/0063_2026081601_vehicle_reid_v2_diverse_review_campaign.sql @@ -0,0 +1,68 @@ +-- migration: 2026081601_vehicle_reid_v2_diverse_review_campaign +-- One operator-started campaign can collect a bounded, diversity-aware human +-- sample without recycling the vehicles used by earlier pair reviews. The +-- campaign is only a review-workload boundary; it never applies a ReID +-- threshold or changes a vehicle assignment. +CREATE TABLE IF NOT EXISTS public.vehicle_reid_v2_review_campaigns ( + id BIGSERIAL PRIMARY KEY, + status VARCHAR(16) NOT NULL DEFAULT 'active' CHECK ( + status IN ('active','completed','cancelled') + ), + target_human_reviews INTEGER NOT NULL DEFAULT 500 CHECK ( + target_human_reviews BETWEEN 1 AND 500 + ), + frozen_max_derivative_id BIGINT NOT NULL CHECK ( + frozen_max_derivative_id > 0 + ), + embedding_model VARCHAR(100) NOT NULL CHECK ( + NULLIF(BTRIM(embedding_model), '') IS NOT NULL + ), + algorithm_version VARCHAR(100) NOT NULL CHECK ( + NULLIF(BTRIM(algorithm_version), '') IS NOT NULL + ), + actor_user_id BIGINT REFERENCES public.users(id) ON DELETE SET NULL, + actor_username VARCHAR(64) NOT NULL CHECK ( + NULLIF(BTRIM(actor_username), '') IS NOT NULL + ), + actor_display_name VARCHAR(120) NOT NULL CHECK ( + NULLIF(BTRIM(actor_display_name), '') IS NOT NULL + ), + completed_at TIMESTAMPTZ, + cancelled_at TIMESTAMPTZ, + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + CHECK ( + (status = 'completed' AND completed_at IS NOT NULL AND cancelled_at IS NULL) + OR (status = 'cancelled' AND cancelled_at IS NOT NULL AND completed_at IS NULL) + OR (status = 'active' AND completed_at IS NULL AND cancelled_at IS NULL) + ) +); + +CREATE UNIQUE INDEX IF NOT EXISTS idx_vehicle_reid_v2_review_campaign_one_active + ON public.vehicle_reid_v2_review_campaigns ((TRUE)) + WHERE status = 'active'; + +ALTER TABLE public.vehicle_reid_v2_pair_reviews + ADD COLUMN IF NOT EXISTS campaign_id BIGINT; + +DO $$ +BEGIN + IF NOT EXISTS ( + SELECT 1 FROM pg_constraint + WHERE conname = 'vehicle_reid_v2_pair_reviews_campaign_id_fkey' + ) THEN + ALTER TABLE public.vehicle_reid_v2_pair_reviews + ADD CONSTRAINT vehicle_reid_v2_pair_reviews_campaign_id_fkey + FOREIGN KEY (campaign_id) + REFERENCES public.vehicle_reid_v2_review_campaigns(id) + ON DELETE SET NULL; + END IF; +END $$; + +CREATE INDEX IF NOT EXISTS idx_vehicle_reid_v2_pair_reviews_campaign + ON public.vehicle_reid_v2_pair_reviews (campaign_id, updated_at DESC, id DESC) + WHERE campaign_id IS NOT NULL; + +INSERT INTO public.schema_migrations(version,description) VALUES + ('2026081601_vehicle_reid_v2_diverse_review_campaign','Add one explicit 500-review ReID v2 diversity campaign with frozen inventory and campaign-bound human labels; automatic plate evidence remains visibly separate and no identity threshold or assignment is applied.') +ON CONFLICT(version) DO NOTHING; diff --git a/migrations/0064_2026081602_vehicle_reid_v2_profile_candidates.sql b/migrations/0064_2026081602_vehicle_reid_v2_profile_candidates.sql new file mode 100644 index 0000000..ff286c7 --- /dev/null +++ b/migrations/0064_2026081602_vehicle_reid_v2_profile_candidates.sql @@ -0,0 +1,156 @@ +-- migration: 2026081602_vehicle_reid_v2_profile_candidates +-- ReID v2 profile candidates are immutable shadow snapshots built only from +-- exact effective/corrected plate agreement and audited Same-vehicle labels. +-- They preserve explicit evidence and do not infer a score cutoff, +-- or create a vehicle assignment. Conflicts fail closed into separate evidence. +CREATE TABLE IF NOT EXISTS public.vehicle_reid_v2_profile_candidate_runs ( + id BIGSERIAL PRIMARY KEY, + snapshot_fingerprint CHAR(64) NOT NULL UNIQUE CHECK ( + snapshot_fingerprint ~ '^[0-9a-f]{64}$' + ), + status VARCHAR(16) NOT NULL DEFAULT 'completed' CHECK (status = 'completed'), + algorithm_version VARCHAR(100) NOT NULL CHECK ( + NULLIF(BTRIM(algorithm_version), '') IS NOT NULL + ), + embedding_model VARCHAR(100) NOT NULL CHECK ( + NULLIF(BTRIM(embedding_model), '') IS NOT NULL + ), + embedding_algorithm_version VARCHAR(100) NOT NULL CHECK ( + NULLIF(BTRIM(embedding_algorithm_version), '') IS NOT NULL + ), + frozen_max_derivative_id BIGINT NOT NULL CHECK (frozen_max_derivative_id >= 0), + total_sources INTEGER NOT NULL CHECK (total_sources >= 0), + exact_plate_eligible_sources INTEGER NOT NULL CHECK ( + exact_plate_eligible_sources BETWEEN 0 AND total_sources + ), + human_same_reviews INTEGER NOT NULL CHECK (human_same_reviews >= 0), + human_different_reviews INTEGER NOT NULL CHECK (human_different_reviews >= 0), + candidate_profiles INTEGER NOT NULL CHECK (candidate_profiles >= 0), + candidate_members INTEGER NOT NULL CHECK (candidate_members >= 0), + conflicted_components INTEGER NOT NULL CHECK (conflicted_components >= 0), + conflicted_members INTEGER NOT NULL CHECK (conflicted_members >= 0), + ungrouped_sources INTEGER NOT NULL CHECK (ungrouped_sources >= 0), + -- Immutable actor snapshot scalar; not a live FK that could rewrite history. + actor_user_id BIGINT, + actor_username VARCHAR(64) NOT NULL CHECK ( + NULLIF(BTRIM(actor_username), '') IS NOT NULL + ), + actor_display_name VARCHAR(120) NOT NULL CHECK ( + NULLIF(BTRIM(actor_display_name), '') IS NOT NULL + ), + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + CHECK (candidate_members + conflicted_members + ungrouped_sources = total_sources) +); + +CREATE TABLE IF NOT EXISTS public.vehicle_reid_v2_profile_candidates ( + id BIGSERIAL PRIMARY KEY, + run_id BIGINT NOT NULL + REFERENCES public.vehicle_reid_v2_profile_candidate_runs(id) ON DELETE RESTRICT, + candidate_key CHAR(64) NOT NULL CHECK (candidate_key ~ '^[0-9a-f]{64}$'), + status VARCHAR(16) NOT NULL DEFAULT 'shadow' CHECK (status = 'shadow'), + evidence_basis VARCHAR(32) NOT NULL CHECK ( + evidence_basis IN ('exact_effective_plate','human_same','mixed') + ), + representative_derivative_id BIGINT NOT NULL CHECK (representative_derivative_id > 0), + member_count INTEGER NOT NULL CHECK (member_count >= 2), + anchor_plates JSONB NOT NULL DEFAULT '[]'::jsonb CHECK ( + JSONB_TYPEOF(anchor_plates) = 'array' + ), + camera_names JSONB NOT NULL DEFAULT '[]'::jsonb CHECK ( + JSONB_TYPEOF(camera_names) = 'array' + ), + overview_contexts JSONB NOT NULL DEFAULT '[]'::jsonb CHECK ( + JSONB_TYPEOF(overview_contexts) = 'array' + ), + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + UNIQUE (run_id, candidate_key) +); + +CREATE INDEX IF NOT EXISTS idx_vehicle_reid_v2_profile_candidates_run + ON public.vehicle_reid_v2_profile_candidates (run_id, member_count DESC, id); + +CREATE TABLE IF NOT EXISTS public.vehicle_reid_v2_profile_candidate_members ( + run_id BIGINT NOT NULL + REFERENCES public.vehicle_reid_v2_profile_candidate_runs(id) ON DELETE RESTRICT, + profile_candidate_id BIGINT NOT NULL + REFERENCES public.vehicle_reid_v2_profile_candidates(id) ON DELETE RESTRICT, + derivative_id BIGINT NOT NULL CHECK (derivative_id > 0), + embedding_id BIGINT NOT NULL CHECK (embedding_id > 0), + source_sha256 CHAR(64) NOT NULL CHECK (source_sha256 ~ '^[0-9a-f]{64}$'), + evidence_basis VARCHAR(32) NOT NULL CHECK ( + evidence_basis IN ('exact_effective_plate','human_same','mixed') + ), + effective_plates JSONB NOT NULL DEFAULT '[]'::jsonb CHECK ( + JSONB_TYPEOF(effective_plates) = 'array' + ), + read_id INTEGER NOT NULL CHECK (read_id > 0), + camera_name VARCHAR(120), + overview_context VARCHAR(12) CHECK (overview_context IN ('street','entry')), + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + PRIMARY KEY (profile_candidate_id, derivative_id), + UNIQUE (run_id, derivative_id) +); + +CREATE INDEX IF NOT EXISTS idx_vehicle_reid_v2_profile_candidate_members_derivative + ON public.vehicle_reid_v2_profile_candidate_members (derivative_id, run_id DESC); + +CREATE TABLE IF NOT EXISTS public.vehicle_reid_v2_profile_candidate_conflicts ( + run_id BIGINT NOT NULL + REFERENCES public.vehicle_reid_v2_profile_candidate_runs(id) ON DELETE RESTRICT, + conflict_key CHAR(64) NOT NULL CHECK (conflict_key ~ '^[0-9a-f]{64}$'), + reason VARCHAR(40) NOT NULL CHECK ( + reason IN ('human_different','dissimilar_effective_plates','mixed') + ), + derivative_ids JSONB NOT NULL CHECK (JSONB_TYPEOF(derivative_ids) = 'array'), + effective_plates JSONB NOT NULL DEFAULT '[]'::jsonb CHECK ( + JSONB_TYPEOF(effective_plates) = 'array' + ), + review_id BIGINT, + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + PRIMARY KEY (run_id, conflict_key) +); + +CREATE OR REPLACE FUNCTION public.prevent_vehicle_reid_v2_profile_candidate_mutation() +RETURNS TRIGGER LANGUAGE plpgsql AS $$ +BEGIN + IF TG_OP = 'INSERT' THEN + IF TG_TABLE_NAME <> 'vehicle_reid_v2_profile_candidate_runs' AND EXISTS ( + SELECT 1 + FROM public.vehicle_reid_v2_profile_candidate_runs runs + WHERE runs.id = NEW.run_id + AND runs.xmin::text = pg_current_xact_id()::text + ) THEN + RETURN NEW; + END IF; + END IF; + RAISE EXCEPTION 'ReID v2 profile candidate snapshots are immutable'; +END; +$$; + +DROP TRIGGER IF EXISTS vehicle_reid_v2_profile_candidate_runs_immutable + ON public.vehicle_reid_v2_profile_candidate_runs; +CREATE TRIGGER vehicle_reid_v2_profile_candidate_runs_immutable +BEFORE UPDATE OR DELETE ON public.vehicle_reid_v2_profile_candidate_runs +FOR EACH ROW EXECUTE FUNCTION public.prevent_vehicle_reid_v2_profile_candidate_mutation(); + +DROP TRIGGER IF EXISTS vehicle_reid_v2_profile_candidates_immutable + ON public.vehicle_reid_v2_profile_candidates; +CREATE TRIGGER vehicle_reid_v2_profile_candidates_immutable +BEFORE INSERT OR UPDATE OR DELETE ON public.vehicle_reid_v2_profile_candidates +FOR EACH ROW EXECUTE FUNCTION public.prevent_vehicle_reid_v2_profile_candidate_mutation(); + +DROP TRIGGER IF EXISTS vehicle_reid_v2_profile_candidate_members_immutable + ON public.vehicle_reid_v2_profile_candidate_members; +CREATE TRIGGER vehicle_reid_v2_profile_candidate_members_immutable +BEFORE INSERT OR UPDATE OR DELETE ON public.vehicle_reid_v2_profile_candidate_members +FOR EACH ROW EXECUTE FUNCTION public.prevent_vehicle_reid_v2_profile_candidate_mutation(); + +DROP TRIGGER IF EXISTS vehicle_reid_v2_profile_candidate_conflicts_immutable + ON public.vehicle_reid_v2_profile_candidate_conflicts; +CREATE TRIGGER vehicle_reid_v2_profile_candidate_conflicts_immutable +BEFORE INSERT OR UPDATE OR DELETE ON public.vehicle_reid_v2_profile_candidate_conflicts +FOR EACH ROW EXECUTE FUNCTION public.prevent_vehicle_reid_v2_profile_candidate_mutation(); + +INSERT INTO public.schema_migrations(version,description) VALUES + ('2026081602_vehicle_reid_v2_profile_candidates','Add immutable evidence-backed ReID v2 shadow profile candidate snapshots using exact effective plates and audited Same labels, with conflicts retained separately and no threshold, cluster, or assignment write.') +ON CONFLICT(version) DO NOTHING; diff --git a/migrations/0065_2026092701_native_reid.sql b/migrations/0065_2026092701_native_reid.sql new file mode 100644 index 0000000..fd7c6dc --- /dev/null +++ b/migrations/0065_2026092701_native_reid.sql @@ -0,0 +1,1269 @@ +-- migration: 2026092701_native_reid +-- One-time retirement of obsolete derived-index objects on upgrade. +-- Run only with the app stopped and a verified database backup (the updater +-- enforces both). Original reads, images, users, tags, and reviews are retained. +-- These names are upgrade targets, never runtime dependencies. +DROP FUNCTION IF EXISTS public.validate_vehicle_reid_control_transition() CASCADE; +DROP FUNCTION IF EXISTS public.validate_vehicle_reid_control_stage2_transition() CASCADE; +DROP FUNCTION IF EXISTS public.seed_vehicle_reid_v2_live_discovery_state() CASCADE; +DROP FUNCTION IF EXISTS public.guard_stopped_vehicle_reid_v1_writes() CASCADE; +DROP FUNCTION IF EXISTS public.guard_vehicle_reid_v2_origin_authority_mutation() CASCADE; +DROP FUNCTION IF EXISTS public.validate_vehicle_reid_v2_conversion_transition() CASCADE; +DROP FUNCTION IF EXISTS public.validate_vehicle_reid_v2_materialization_transition() CASCADE; +DROP FUNCTION IF EXISTS public.validate_vehicle_reid_v2_stage2_materialization_transition() CASCADE; +DROP FUNCTION IF EXISTS public.prevent_vehicle_reid_v2_conversion_snapshot_mutation() CASCADE; +DROP FUNCTION IF EXISTS public.capture_asset_set_updated_at() CASCADE; +DROP FUNCTION IF EXISTS public.camera_visual_profile_set_updated_at() CASCADE; +DROP FUNCTION IF EXISTS public.assert_vehicle_reid_v2_exact_materialization(bigint); +DROP FUNCTION IF EXISTS public.assert_vehicle_reid_v2_stage2_materialization(bigint); +DO $native_control$ +BEGIN + IF EXISTS (SELECT 1 FROM information_schema.columns + WHERE table_schema = 'public' AND table_name = 'vehicle_reid_control' + AND column_name = 'previous_mode') THEN + DROP TABLE public.vehicle_reid_control; + UPDATE public.vehicle_image_asset_live_catalog_control + SET enabled = TRUE, enabled_at = CURRENT_TIMESTAMP, disabled_at = NULL; + UPDATE public.vehicle_image_crop_live_control + SET enabled = TRUE, enabled_at = CURRENT_TIMESTAMP, disabled_at = NULL; + END IF; +END +$native_control$; +ALTER TABLE IF EXISTS public.vehicle_reid_v2_profiles + DROP COLUMN IF EXISTS origin_conversion_run_id CASCADE, + DROP COLUMN IF EXISTS origin_projection_key CASCADE; +ALTER TABLE IF EXISTS public.vehicle_reid_v2_profile_members + DROP COLUMN IF EXISTS origin_conversion_run_id CASCADE, + DROP COLUMN IF EXISTS origin_projected_member_fingerprint CASCADE; +ALTER TABLE IF EXISTS public.vehicle_reid_v2_read_assignments + DROP COLUMN IF EXISTS origin_conversion_run_id CASCADE, + DROP COLUMN IF EXISTS origin_disposition_fingerprint CASCADE; +ALTER TABLE IF EXISTS public.vehicle_reid_v2_profile_plate_anchors + DROP COLUMN IF EXISTS origin_conversion_run_id CASCADE, + DROP COLUMN IF EXISTS origin_projection_key CASCADE; +ALTER TABLE IF EXISTS public.vehicle_reid_v2_live_discovery_state + DROP COLUMN IF EXISTS transition_run_id CASCADE; +DROP TABLE IF EXISTS public.vehicle_reid_v2_conversion_v1_comparisons; +DROP TABLE IF EXISTS public.vehicle_reid_v2_conversion_conflicts; +DROP TABLE IF EXISTS public.vehicle_reid_v2_conversion_read_dispositions; +DROP TABLE IF EXISTS public.vehicle_reid_v2_conversion_projected_members; +DROP TABLE IF EXISTS public.vehicle_reid_v2_conversion_projected_profiles; +DROP TABLE IF EXISTS public.vehicle_reid_v2_conversion_jobs; +DROP TABLE IF EXISTS public.vehicle_reid_v2_conversion_review_evidence; +DROP TABLE IF EXISTS public.vehicle_reid_v2_conversion_read_evidence; +DROP TABLE IF EXISTS public.vehicle_reid_v2_conversion_crop_evidence; +DROP TABLE IF EXISTS public.vehicle_reid_v2_conversion_runs; +DROP TABLE IF EXISTS public.vehicle_plate_associations; +DROP TABLE IF EXISTS public.vehicle_cluster_assignments; +DROP TABLE IF EXISTS public.vehicle_clusters; +DROP TABLE IF EXISTS public.vehicle_match_feedback; +DROP TABLE IF EXISTS public.capture_assets; +DROP TABLE IF EXISTS public.camera_visual_profiles; + +-- ReID is native and exclusive: there is no alternate identity provider. +CREATE TABLE IF NOT EXISTS public.vehicle_reid_control ( + singleton BOOLEAN PRIMARY KEY DEFAULT TRUE CHECK (singleton = TRUE), + mode VARCHAR(16) NOT NULL DEFAULT 'v2_primary' CHECK (mode = 'v2_primary'), + processing_enabled BOOLEAN NOT NULL DEFAULT TRUE, + revision INTEGER NOT NULL DEFAULT 1 CHECK (revision > 0) +); +INSERT INTO public.vehicle_reid_control(singleton) VALUES (TRUE) +ON CONFLICT (singleton) DO NOTHING; + +CREATE TABLE IF NOT EXISTS public.vehicle_reid_v2_profiles ( + id BIGSERIAL PRIMARY KEY, + status VARCHAR(16) NOT NULL CHECK ( + status IN ('provisional','active','merged','retired') + ), + revision INTEGER NOT NULL DEFAULT 1 CHECK (revision > 0), + provenance_basis VARCHAR(32) NOT NULL CHECK ( + provenance_basis IN ( + 'exact_effective_plate','human_same','mixed','provisional_singleton' + ) + ), + representative_derivative_id BIGINT NOT NULL + REFERENCES public.vehicle_image_derivatives(id) ON DELETE RESTRICT, + representative_embedding_id BIGINT NOT NULL + REFERENCES public.vehicle_asset_embeddings(id) ON DELETE RESTRICT, + representative_source_sha256 CHAR(64) NOT NULL CHECK ( + representative_source_sha256 ~ '^[0-9a-f]{64}$' + ), + representative_evidence_fingerprint CHAR(64) NOT NULL CHECK ( + representative_evidence_fingerprint ~ '^[0-9a-f]{64}$' + ), + merged_into_profile_id BIGINT + REFERENCES public.vehicle_reid_v2_profiles(id) ON DELETE RESTRICT, + created_by_user_id BIGINT REFERENCES public.users(id) ON DELETE SET NULL, + created_by_username VARCHAR(64) NOT NULL CHECK ( + NULLIF(BTRIM(created_by_username), '') IS NOT NULL + ), + created_by_display_name VARCHAR(120) NOT NULL CHECK ( + NULLIF(BTRIM(created_by_display_name), '') IS NOT NULL + ), + merged_at TIMESTAMPTZ, + retired_at TIMESTAMPTZ, + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + CHECK ( + (status = 'merged' + AND merged_into_profile_id IS NOT NULL + AND merged_into_profile_id <> id + AND merged_at IS NOT NULL + AND retired_at IS NULL) + OR (status <> 'merged' + AND merged_into_profile_id IS NULL + AND merged_at IS NULL) + ), + CHECK ( + (status = 'retired' AND retired_at IS NOT NULL) + OR (status <> 'retired' AND retired_at IS NULL) + ), + CHECK ( + (status = 'provisional' AND provenance_basis = 'provisional_singleton') + OR status <> 'provisional' + ) +); + +CREATE INDEX IF NOT EXISTS idx_reid_v2_profiles_status + ON public.vehicle_reid_v2_profiles (status, updated_at DESC, id DESC); +CREATE INDEX IF NOT EXISTS idx_reid_v2_profiles_merge_target + ON public.vehicle_reid_v2_profiles (merged_into_profile_id) + WHERE merged_into_profile_id IS NOT NULL; + +CREATE TABLE IF NOT EXISTS public.vehicle_reid_v2_profile_members ( + id BIGSERIAL PRIMARY KEY, + profile_id BIGINT NOT NULL + REFERENCES public.vehicle_reid_v2_profiles(id) ON DELETE RESTRICT, + status VARCHAR(16) NOT NULL DEFAULT 'current' CHECK ( + status IN ('current','superseded','removed') + ), + revision INTEGER NOT NULL DEFAULT 1 CHECK (revision > 0), + derivative_id BIGINT NOT NULL + REFERENCES public.vehicle_image_derivatives(id) ON DELETE RESTRICT, + asset_id BIGINT NOT NULL + REFERENCES public.vehicle_image_assets(id) ON DELETE RESTRICT, + derivative_kind VARCHAR(32) NOT NULL CHECK (derivative_kind = 'vehicle_crop'), + crop_algorithm_version VARCHAR(100) NOT NULL CHECK ( + NULLIF(BTRIM(crop_algorithm_version), '') IS NOT NULL + ), + asset_source_sha256 CHAR(64) NOT NULL CHECK ( + asset_source_sha256 ~ '^[0-9a-f]{64}$' + ), + crop_content_sha256 CHAR(64) NOT NULL CHECK ( + crop_content_sha256 ~ '^[0-9a-f]{64}$' + ), + embedding_id BIGINT NOT NULL + REFERENCES public.vehicle_asset_embeddings(id) ON DELETE RESTRICT, + embedding_model VARCHAR(100) NOT NULL CHECK ( + NULLIF(BTRIM(embedding_model), '') IS NOT NULL + ), + embedding_algorithm_version VARCHAR(100) NOT NULL CHECK ( + NULLIF(BTRIM(embedding_algorithm_version), '') IS NOT NULL + ), + embedding_source_sha256 CHAR(64) NOT NULL CHECK ( + embedding_source_sha256 ~ '^[0-9a-f]{64}$' + ), + embedding_sha256 CHAR(64) NOT NULL CHECK ( + embedding_sha256 ~ '^[0-9a-f]{64}$' + ), + membership_basis VARCHAR(32) NOT NULL CHECK ( + membership_basis IN ( + 'exact_effective_plate','human_same','mixed','provisional_singleton' + ) + ), + representative_evidence_read_id INTEGER NOT NULL CHECK ( + representative_evidence_read_id > 0 + ), + source_revision_fingerprint CHAR(64) NOT NULL CHECK ( + source_revision_fingerprint ~ '^[0-9a-f]{64}$' + ), + evidence_fingerprint CHAR(64) NOT NULL CHECK ( + evidence_fingerprint ~ '^[0-9a-f]{64}$' + ), + ended_at TIMESTAMPTZ, + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + UNIQUE (profile_id, id), + CHECK ( + (status = 'current' AND ended_at IS NULL) + OR (status <> 'current' AND ended_at IS NOT NULL) + ) +); + +CREATE UNIQUE INDEX IF NOT EXISTS idx_reid_v2_member_one_current_crop + ON public.vehicle_reid_v2_profile_members (derivative_id) + WHERE status = 'current'; +CREATE INDEX IF NOT EXISTS idx_reid_v2_member_profile + ON public.vehicle_reid_v2_profile_members ( + profile_id, status, created_at DESC, id DESC + ); +CREATE INDEX IF NOT EXISTS idx_reid_v2_member_embedding + ON public.vehicle_reid_v2_profile_members (embedding_id, status, id); + +CREATE TABLE IF NOT EXISTS public.vehicle_reid_v2_read_assignments ( + id BIGSERIAL PRIMARY KEY, + read_id INTEGER NOT NULL REFERENCES public.plate_reads(id) ON DELETE CASCADE, + profile_id BIGINT NOT NULL + REFERENCES public.vehicle_reid_v2_profiles(id) ON DELETE RESTRICT, + status VARCHAR(16) NOT NULL DEFAULT 'active' CHECK ( + status IN ('active','superseded','withdrawn') + ), + revision INTEGER NOT NULL DEFAULT 1 CHECK (revision > 0), + assignment_basis VARCHAR(32) NOT NULL CHECK ( + assignment_basis IN ( + 'canonical_image','shared_asset','exact_effective_plate','human_same' + ) + ), + profile_membership_basis VARCHAR(32) NOT NULL CHECK ( + profile_membership_basis IN ( + 'exact_effective_plate','human_same','mixed','provisional_singleton' + ) + ), + profile_revision INTEGER NOT NULL CHECK (profile_revision > 0), + profile_member_id BIGINT, + asset_id BIGINT REFERENCES public.vehicle_image_assets(id) ON DELETE RESTRICT, + derivative_id BIGINT + REFERENCES public.vehicle_image_derivatives(id) ON DELETE RESTRICT, + embedding_id BIGINT + REFERENCES public.vehicle_asset_embeddings(id) ON DELETE RESTRICT, + normalized_effective_plate VARCHAR(32) CHECK ( + normalized_effective_plate IS NULL + OR normalized_effective_plate ~ '^[A-Z0-9]+$' + ), + plate_review_status VARCHAR(24) NOT NULL CHECK ( + plate_review_status IN ( + 'unreviewed','confirmed','corrected','rejected','alias_resolved' + ) + ), + plate_review_revision INTEGER NOT NULL CHECK (plate_review_revision >= 0), + plate_review_id BIGINT CHECK (plate_review_id IS NULL OR plate_review_id > 0), + applied_alias_id BIGINT CHECK (applied_alias_id IS NULL OR applied_alias_id > 0), + source_kind VARCHAR(40), + source_relationship VARCHAR(24), + source_path_snapshot TEXT, + source_updated_at TIMESTAMPTZ, + source_link_updated_at TIMESTAMPTZ, + evidence_fingerprint CHAR(64) NOT NULL CHECK ( + evidence_fingerprint ~ '^[0-9a-f]{64}$' + ), + ended_at TIMESTAMPTZ, + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + FOREIGN KEY (profile_id, profile_member_id) + REFERENCES public.vehicle_reid_v2_profile_members(profile_id, id) + ON DELETE RESTRICT, + CHECK (plate_review_status <> 'rejected'), + CHECK ( + (status = 'active' AND ended_at IS NULL) + OR (status <> 'active' AND ended_at IS NOT NULL) + ), + CHECK ( + assignment_basis <> 'exact_effective_plate' + OR ( + normalized_effective_plate IS NOT NULL + AND plate_review_status IN ('confirmed','corrected','alias_resolved') + ) + ), + CHECK ( + assignment_basis NOT IN ('canonical_image','shared_asset','human_same') + OR ( + profile_member_id IS NOT NULL AND asset_id IS NOT NULL + AND derivative_id IS NOT NULL AND embedding_id IS NOT NULL + AND NULLIF(BTRIM(source_path_snapshot), '') IS NOT NULL + AND source_link_updated_at IS NOT NULL + ) + ) +); + + +-- migration intentionally preserves sealed active history after its evidence +-- becomes stale and enforces one exact-current assignment through the +-- current-contract view plus a per-read transaction lock. Recreating the +-- obsolete index during a full migration replay would reject that valid +-- history before the later migration can replace the index again. +DROP INDEX IF EXISTS public.idx_reid_v2_assignment_one_active_read; +CREATE INDEX IF NOT EXISTS idx_reid_v2_assignment_profile + ON public.vehicle_reid_v2_read_assignments ( + profile_id, status, created_at DESC, id DESC + ); +CREATE INDEX IF NOT EXISTS idx_reid_v2_assignment_plate + ON public.vehicle_reid_v2_read_assignments ( + normalized_effective_plate, status, read_id + ) WHERE normalized_effective_plate IS NOT NULL; + +CREATE OR REPLACE FUNCTION public.validate_vehicle_reid_v2_profile_contract() +RETURNS TRIGGER AS $$ +BEGIN + IF NOT EXISTS ( + SELECT 1 + FROM public.vehicle_image_derivatives derivatives + JOIN public.vehicle_image_assets assets + ON assets.id = derivatives.asset_id + JOIN public.vehicle_asset_embeddings embeddings + ON embeddings.id = NEW.representative_embedding_id + AND embeddings.derivative_id = derivatives.id + WHERE derivatives.id = NEW.representative_derivative_id + AND derivatives.source_sha256 = assets.content_sha256 + AND embeddings.source_sha256 = derivatives.content_sha256 + AND derivatives.content_sha256 = NEW.representative_source_sha256 + ) THEN + RAISE EXCEPTION 'ReID v2 profile representative evidence is not an exact canonical contract' + USING ERRCODE = '23514', + CONSTRAINT = 'vehicle_reid_v2_profile_representative_contract'; + END IF; + + RETURN NEW; +END; +$$ LANGUAGE plpgsql; + +DROP TRIGGER IF EXISTS vehicle_reid_v2_profiles_validate_contract + ON public.vehicle_reid_v2_profiles; +CREATE TRIGGER vehicle_reid_v2_profiles_validate_contract +BEFORE INSERT OR UPDATE ON public.vehicle_reid_v2_profiles +FOR EACH ROW EXECUTE FUNCTION public.validate_vehicle_reid_v2_profile_contract(); + +CREATE OR REPLACE FUNCTION public.validate_vehicle_reid_v2_member_contract() +RETURNS TRIGGER AS $$ +BEGIN + IF NOT EXISTS ( + SELECT 1 + FROM public.vehicle_image_derivatives derivatives + JOIN public.vehicle_image_assets assets + ON assets.id = derivatives.asset_id + JOIN public.vehicle_asset_embeddings embeddings + ON embeddings.id = NEW.embedding_id + AND embeddings.derivative_id = derivatives.id + WHERE derivatives.id = NEW.derivative_id + AND assets.id = NEW.asset_id + AND derivatives.derivative_kind = NEW.derivative_kind + AND derivatives.algorithm_version = NEW.crop_algorithm_version + AND assets.content_sha256 = NEW.asset_source_sha256 + AND derivatives.source_sha256 = NEW.asset_source_sha256 + AND derivatives.content_sha256 = NEW.crop_content_sha256 + AND embeddings.model_name = NEW.embedding_model + AND embeddings.algorithm_version = NEW.embedding_algorithm_version + AND embeddings.source_sha256 = NEW.embedding_source_sha256 + AND embeddings.source_sha256 = NEW.crop_content_sha256 + AND embeddings.embedding_sha256 = NEW.embedding_sha256 + ) THEN + RAISE EXCEPTION 'ReID v2 member evidence is not one exact asset/crop/embedding contract' + USING ERRCODE = '23514', + CONSTRAINT = 'vehicle_reid_v2_member_canonical_contract'; + END IF; + + RETURN NEW; +END; +$$ LANGUAGE plpgsql; + +DROP TRIGGER IF EXISTS vehicle_reid_v2_members_validate_contract + ON public.vehicle_reid_v2_profile_members; +CREATE TRIGGER vehicle_reid_v2_members_validate_contract +BEFORE INSERT OR UPDATE ON public.vehicle_reid_v2_profile_members +FOR EACH ROW EXECUTE FUNCTION public.validate_vehicle_reid_v2_member_contract(); +CREATE TABLE IF NOT EXISTS public.vehicle_reid_v2_profile_plate_anchors ( + id BIGSERIAL PRIMARY KEY, + profile_id BIGINT NOT NULL + REFERENCES public.vehicle_reid_v2_profiles(id) ON DELETE RESTRICT, + status VARCHAR(16) NOT NULL DEFAULT 'current' CHECK ( + status IN ('current','superseded','withdrawn') + ), + normalized_plate VARCHAR(32) NOT NULL CHECK ( + normalized_plate ~ '^[A-Z0-9]+$' + ), + evidence_read_id INTEGER NOT NULL CHECK (evidence_read_id > 0), + plate_review_status VARCHAR(24) NOT NULL CHECK ( + plate_review_status IN ('confirmed','corrected','alias_resolved') + ), + plate_review_revision INTEGER NOT NULL CHECK (plate_review_revision >= 0), + plate_review_id BIGINT CHECK (plate_review_id IS NULL OR plate_review_id > 0), + applied_alias_id BIGINT CHECK (applied_alias_id IS NULL OR applied_alias_id > 0), + evidence_fingerprint CHAR(64) NOT NULL CHECK ( + evidence_fingerprint ~ '^[0-9a-f]{64}$' + ), + ended_at TIMESTAMPTZ, + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + CHECK ( + (status = 'current' AND ended_at IS NULL) + OR (status <> 'current' AND ended_at IS NOT NULL) + ) +); + +-- Exact-current uniqueness is enforced by the validation trigger below. A + +-- so the physical history may contain more than one status='current' row even +-- though the current-evidence view can expose at most one. +DROP INDEX IF EXISTS public.idx_reid_v2_plate_anchor_one_current_plate; +CREATE INDEX IF NOT EXISTS idx_reid_v2_plate_anchor_current_plate + ON public.vehicle_reid_v2_profile_plate_anchors (normalized_plate) + WHERE status = 'current'; +CREATE INDEX IF NOT EXISTS idx_reid_v2_plate_anchor_profile + ON public.vehicle_reid_v2_profile_plate_anchors ( + profile_id, status, normalized_plate, id + ); + +CREATE OR REPLACE FUNCTION public.validate_vehicle_reid_v2_plate_anchor_contract() +RETURNS TRIGGER AS $$ +BEGIN + IF NEW.status = 'current' THEN + PERFORM pg_advisory_xact_lock(hashtext( + 'vehicle_reid_v2_plate_anchor:' || NEW.normalized_plate + )); + IF NOT EXISTS ( + SELECT 1 + FROM public.plate_reads reads + WHERE reads.id = NEW.evidence_read_id + AND UPPER(REGEXP_REPLACE(reads.plate_number, '[^A-Za-z0-9]', '', 'g')) + = NEW.normalized_plate + AND reads.review_status = NEW.plate_review_status + AND reads.review_revision = NEW.plate_review_revision + AND reads.applied_alias_id IS NOT DISTINCT FROM NEW.applied_alias_id + AND NEW.plate_review_id IS NOT DISTINCT FROM ( + SELECT reviews.id + FROM public.plate_read_reviews reviews + WHERE reviews.read_id = NEW.evidence_read_id + ORDER BY reviews.created_at DESC, reviews.id DESC + LIMIT 1 + ) + ) THEN + RAISE EXCEPTION 'ReID v2 plate anchor is not exact current reviewed plate evidence' + USING ERRCODE = '23514', + CONSTRAINT = 'vehicle_reid_v2_plate_anchor_current_contract'; + END IF; + + IF EXISTS ( + SELECT 1 + FROM public.vehicle_reid_v2_profile_plate_anchors existing + JOIN public.plate_reads evidence ON evidence.id = existing.evidence_read_id + WHERE existing.id IS DISTINCT FROM NEW.id + AND existing.status = 'current' + AND existing.normalized_plate = NEW.normalized_plate + AND UPPER(REGEXP_REPLACE(evidence.plate_number, '[^A-Za-z0-9]', '', 'g')) + = existing.normalized_plate + AND evidence.review_status = existing.plate_review_status + AND evidence.review_revision = existing.plate_review_revision + AND evidence.applied_alias_id IS NOT DISTINCT FROM existing.applied_alias_id + AND existing.plate_review_id IS NOT DISTINCT FROM ( + SELECT reviews.id + FROM public.plate_read_reviews reviews + WHERE reviews.read_id = existing.evidence_read_id + ORDER BY reviews.created_at DESC, reviews.id DESC + LIMIT 1 + ) + ) THEN + RAISE EXCEPTION 'ReID v2 reviewed plate already anchors a current profile' + USING ERRCODE = '23514', + CONSTRAINT = 'vehicle_reid_v2_plate_anchor_one_exact_current_plate'; + END IF; + END IF; + + RETURN NEW; +END; +$$ LANGUAGE plpgsql; + +DROP TRIGGER IF EXISTS vehicle_reid_v2_plate_anchors_validate_contract + ON public.vehicle_reid_v2_profile_plate_anchors; +CREATE TRIGGER vehicle_reid_v2_plate_anchors_validate_contract +BEFORE INSERT OR UPDATE ON public.vehicle_reid_v2_profile_plate_anchors +FOR EACH ROW EXECUTE FUNCTION public.validate_vehicle_reid_v2_plate_anchor_contract(); +CREATE TABLE IF NOT EXISTS public.vehicle_reid_v2_live_jobs ( + read_id INTEGER PRIMARY KEY + REFERENCES public.plate_reads(id) ON DELETE CASCADE, + status VARCHAR(20) NOT NULL DEFAULT 'pending' CHECK ( + status IN ('pending','processing','ready','conflict','unavailable','failed') + ), + attempt_count SMALLINT NOT NULL DEFAULT 0 CHECK (attempt_count BETWEEN 0 AND 3), + operator_retry_count SMALLINT NOT NULL DEFAULT 0 CHECK ( + operator_retry_count BETWEEN 0 AND 1 + ), + retryable BOOLEAN NOT NULL DEFAULT TRUE, + claim_token UUID, + processing_deadline_at TIMESTAMPTZ, + next_attempt_at TIMESTAMPTZ, + profile_id BIGINT REFERENCES public.vehicle_reid_v2_profiles(id) ON DELETE RESTRICT, + assignment_id BIGINT + REFERENCES public.vehicle_reid_v2_read_assignments(id) ON DELETE RESTRICT, + result_basis VARCHAR(32), + error_code VARCHAR(80), + error_details JSONB CHECK ( + error_details IS NULL OR JSONB_TYPEOF(error_details) = 'object' + ), + completed_at TIMESTAMPTZ, + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + CHECK ( + (status = 'processing' AND claim_token IS NOT NULL + AND processing_deadline_at IS NOT NULL) + OR (status <> 'processing' AND claim_token IS NULL + AND processing_deadline_at IS NULL) + ), + CHECK ( + (status = 'ready' AND profile_id IS NOT NULL AND assignment_id IS NOT NULL + AND completed_at IS NOT NULL AND error_code IS NULL) + OR status <> 'ready' + ), + CHECK ( + (status IN ('conflict','unavailable','failed') AND error_code IS NOT NULL) + OR status NOT IN ('conflict','unavailable','failed') + ) +); + +CREATE INDEX IF NOT EXISTS idx_reid_v2_live_job_claim + ON public.vehicle_reid_v2_live_jobs ( + status, next_attempt_at, read_id + ) WHERE status IN ('pending','processing','failed'); +CREATE INDEX IF NOT EXISTS idx_reid_v2_live_job_profile + ON public.vehicle_reid_v2_live_jobs (profile_id, status, read_id) + WHERE profile_id IS NOT NULL; + +-- Audited Same decisions can collapse two stable public profile identifiers + +-- current view below, so a revised/stale review fails closed automatically. +CREATE TABLE IF NOT EXISTS public.vehicle_reid_v2_profile_merges ( + id BIGSERIAL PRIMARY KEY, + source_profile_id BIGINT NOT NULL + REFERENCES public.vehicle_reid_v2_profiles(id) ON DELETE RESTRICT, + target_profile_id BIGINT NOT NULL + REFERENCES public.vehicle_reid_v2_profiles(id) ON DELETE RESTRICT, + status VARCHAR(16) NOT NULL DEFAULT 'current' CHECK ( + status IN ('current','withdrawn') + ), + pair_review_id BIGINT NOT NULL + REFERENCES public.vehicle_reid_v2_pair_reviews(id) ON DELETE RESTRICT, + pair_review_revision INTEGER NOT NULL CHECK (pair_review_revision > 0), + evidence_fingerprint CHAR(64) NOT NULL UNIQUE CHECK ( + evidence_fingerprint ~ '^[0-9a-f]{64}$' + ), + actor_user_id BIGINT REFERENCES public.users(id) ON DELETE SET NULL, + actor_username VARCHAR(64) NOT NULL CHECK ( + NULLIF(BTRIM(actor_username), '') IS NOT NULL + ), + actor_display_name VARCHAR(120) NOT NULL CHECK ( + NULLIF(BTRIM(actor_display_name), '') IS NOT NULL + ), + ended_by_user_id BIGINT REFERENCES public.users(id) ON DELETE SET NULL, + ended_by_username VARCHAR(64), + ended_by_display_name VARCHAR(120), + end_reason VARCHAR(80), + ended_at TIMESTAMPTZ, + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + CHECK (source_profile_id <> target_profile_id), + CHECK ( + (status = 'current' AND ended_at IS NULL + AND ended_by_username IS NULL AND ended_by_display_name IS NULL + AND end_reason IS NULL) + OR (status = 'withdrawn' AND ended_at IS NOT NULL + AND NULLIF(BTRIM(ended_by_username), '') IS NOT NULL + AND NULLIF(BTRIM(ended_by_display_name), '') IS NOT NULL + AND NULLIF(BTRIM(end_reason), '') IS NOT NULL) + ) +); + +CREATE UNIQUE INDEX IF NOT EXISTS idx_reid_v2_profile_merge_one_current_source + ON public.vehicle_reid_v2_profile_merges (source_profile_id) + WHERE status = 'current'; +CREATE INDEX IF NOT EXISTS idx_reid_v2_profile_merge_target + ON public.vehicle_reid_v2_profile_merges ( + target_profile_id, status, source_profile_id + ); + +-- A profile member is usable by current consumers only while its exact crop, +-- embedding, and at least one identity-eligible canonical source link still +-- match. The historical authority row remains untouched when a source is +-- replaced; this view is the fail-closed current contract. +CREATE OR REPLACE VIEW public.vehicle_reid_v2_exact_profile_members AS +SELECT members.* +FROM public.vehicle_reid_v2_profile_members members +JOIN public.vehicle_reid_v2_profiles profiles ON profiles.id = members.profile_id +JOIN public.vehicle_image_derivatives derivatives + ON derivatives.id = members.derivative_id + AND derivatives.asset_id = members.asset_id + AND derivatives.derivative_kind = members.derivative_kind + AND derivatives.algorithm_version = members.crop_algorithm_version + AND derivatives.source_sha256 = members.asset_source_sha256 + AND derivatives.content_sha256 = members.crop_content_sha256 +JOIN public.vehicle_image_assets assets + ON assets.id = members.asset_id + AND assets.content_sha256 = members.asset_source_sha256 +JOIN public.vehicle_asset_embeddings embeddings + ON embeddings.id = members.embedding_id + AND embeddings.derivative_id = members.derivative_id + AND embeddings.model_name = members.embedding_model + AND embeddings.algorithm_version = members.embedding_algorithm_version + AND embeddings.source_sha256 = members.embedding_source_sha256 + AND embeddings.embedding_sha256 = members.embedding_sha256 +WHERE members.status = 'current' + AND profiles.status IN ('active','provisional') + AND EXISTS ( + SELECT 1 + FROM public.vehicle_image_asset_reads links + JOIN public.plate_reads reads ON reads.id = links.read_id + WHERE links.asset_id = members.asset_id + AND links.identity_eligible = TRUE + AND links.relationship <> 'display_fallback' + AND reads.vehicle_image_status = 'ready' + AND reads.vehicle_image_path = links.source_path_snapshot + AND reads.vehicle_image_source_kind = links.source_kind + AND reads.vehicle_image_updated_at IS NOT DISTINCT FROM links.source_updated_at + ) + AND ( + SELECT COUNT(DISTINCT UPPER( + REGEXP_REPLACE(reads.plate_number, '[^A-Za-z0-9]', '', 'g') + )) + FROM public.vehicle_image_asset_reads links + JOIN public.plate_reads reads ON reads.id = links.read_id + WHERE links.asset_id = members.asset_id + AND links.identity_eligible = TRUE + AND links.relationship <> 'display_fallback' + AND reads.vehicle_image_status = 'ready' + AND reads.vehicle_image_path = links.source_path_snapshot + AND reads.vehicle_image_source_kind = links.source_kind + AND reads.vehicle_image_updated_at IS NOT DISTINCT FROM links.source_updated_at + AND reads.review_status <> 'rejected' + AND UPPER(REGEXP_REPLACE(reads.plate_number, '[^A-Za-z0-9]', '', 'g')) <> '' + ) <= 1; + +CREATE OR REPLACE FUNCTION public.validate_vehicle_reid_v2_profile_merge() +RETURNS TRIGGER AS $$ +BEGIN + IF NEW.status <> 'current' THEN + RAISE EXCEPTION 'ReID v2 profile merge history must begin current' + USING ERRCODE = '23514', + CONSTRAINT = 'vehicle_reid_v2_profile_merge_initial_state'; + END IF; + IF EXISTS ( + SELECT 1 FROM public.vehicle_reid_v2_profile_merges merges + WHERE merges.status = 'current' + AND (merges.target_profile_id = NEW.source_profile_id + OR merges.source_profile_id = NEW.target_profile_id) + ) THEN + RAISE EXCEPTION 'ReID profile merge chains are not permitted' + USING ERRCODE = '23514', + CONSTRAINT = 'vehicle_reid_v2_profile_merge_no_chain'; + END IF; + + IF NOT EXISTS ( + SELECT 1 + FROM public.vehicle_reid_v2_pair_reviews reviews + JOIN public.vehicle_reid_v2_exact_profile_members low_member + ON low_member.derivative_id = reviews.derivative_id_low + JOIN public.vehicle_reid_v2_exact_profile_members high_member + ON high_member.derivative_id = reviews.derivative_id_high + LEFT JOIN public.vehicle_reid_v2_current_profile_merges low_existing_merge + ON low_existing_merge.source_profile_id = low_member.profile_id + LEFT JOIN public.vehicle_reid_v2_current_profile_merges high_existing_merge + ON high_existing_merge.source_profile_id = high_member.profile_id + WHERE reviews.id = NEW.pair_review_id + AND reviews.revision = NEW.pair_review_revision + AND reviews.label = 'same_vehicle' + AND ( + (COALESCE(low_existing_merge.target_profile_id, low_member.profile_id) + = NEW.source_profile_id + AND COALESCE(high_existing_merge.target_profile_id, high_member.profile_id) + = NEW.target_profile_id) + OR (COALESCE(low_existing_merge.target_profile_id, low_member.profile_id) + = NEW.target_profile_id + AND COALESCE(high_existing_merge.target_profile_id, high_member.profile_id) + = NEW.source_profile_id) + ) + AND reviews.source_sha256_low = low_member.crop_content_sha256 + AND reviews.source_sha256_high = high_member.crop_content_sha256 + AND reviews.embedding_id_low = low_member.embedding_id + AND reviews.embedding_id_high = high_member.embedding_id + AND reviews.embedding_model = low_member.embedding_model + AND reviews.embedding_model = high_member.embedding_model + AND reviews.algorithm_version = low_member.embedding_algorithm_version + AND reviews.algorithm_version = high_member.embedding_algorithm_version + AND EXISTS ( + SELECT 1 + FROM public.vehicle_reid_v2_current_profile_members current_members + WHERE current_members.canonical_profile_id = NEW.source_profile_id + ) + AND EXISTS ( + SELECT 1 + FROM public.vehicle_reid_v2_current_profile_members current_members + WHERE current_members.canonical_profile_id = NEW.target_profile_id + ) + ) THEN + RAISE EXCEPTION 'ReID profile merge requires an exact-current audited Same review' + USING ERRCODE = '23514', + CONSTRAINT = 'vehicle_reid_v2_profile_merge_same_contract'; + END IF; + + IF ( + SELECT COUNT(DISTINCT anchors.normalized_plate) + FROM public.vehicle_reid_v2_profile_plate_anchors anchors + JOIN public.plate_reads evidence ON evidence.id = anchors.evidence_read_id + WHERE anchors.status = 'current' + AND anchors.profile_id IN (NEW.source_profile_id, NEW.target_profile_id) + AND UPPER(REGEXP_REPLACE(evidence.plate_number, '[^A-Za-z0-9]', '', 'g')) + = anchors.normalized_plate + AND evidence.review_status = anchors.plate_review_status + AND evidence.review_revision = anchors.plate_review_revision + AND evidence.applied_alias_id IS NOT DISTINCT FROM anchors.applied_alias_id + AND anchors.plate_review_id IS NOT DISTINCT FROM ( + SELECT reviews.id + FROM public.plate_read_reviews reviews + WHERE reviews.read_id = anchors.evidence_read_id + ORDER BY reviews.created_at DESC, reviews.id DESC + LIMIT 1 + ) + ) > 1 THEN + RAISE EXCEPTION 'ReID profiles with clearly different reviewed plates cannot merge' + USING ERRCODE = '23514', + CONSTRAINT = 'vehicle_reid_v2_profile_merge_plate_conflict'; + END IF; + RETURN NEW; +END; +$$ LANGUAGE plpgsql; + +DROP TRIGGER IF EXISTS vehicle_reid_v2_profile_merges_validate + ON public.vehicle_reid_v2_profile_merges; +CREATE TRIGGER vehicle_reid_v2_profile_merges_validate +BEFORE INSERT ON public.vehicle_reid_v2_profile_merges +FOR EACH ROW EXECUTE FUNCTION public.validate_vehicle_reid_v2_profile_merge(); + +CREATE OR REPLACE FUNCTION public.guard_vehicle_reid_v2_profile_merge_history() +RETURNS TRIGGER AS $$ +BEGIN + IF TG_OP = 'DELETE' THEN + RAISE EXCEPTION 'ReID v2 profile merge history cannot be deleted' + USING ERRCODE = '23514', + CONSTRAINT = 'vehicle_reid_v2_profile_merge_history_immutable'; + END IF; + IF NOT ( + OLD.status = 'current' AND NEW.status = 'withdrawn' + AND (TO_JSONB(NEW) - ARRAY[ + 'status','ended_by_user_id','ended_by_username', + 'ended_by_display_name','end_reason','ended_at' + ]) = (TO_JSONB(OLD) - ARRAY[ + 'status','ended_by_user_id','ended_by_username', + 'ended_by_display_name','end_reason','ended_at' + ]) + ) THEN + RAISE EXCEPTION 'ReID v2 profile merge history permits only one audited withdrawal' + USING ERRCODE = '23514', + CONSTRAINT = 'vehicle_reid_v2_profile_merge_withdrawal_only'; + END IF; + RETURN NEW; +END; +$$ LANGUAGE plpgsql; + +DROP TRIGGER IF EXISTS vehicle_reid_v2_profile_merges_history_guard + ON public.vehicle_reid_v2_profile_merges; +CREATE TRIGGER vehicle_reid_v2_profile_merges_history_guard +BEFORE UPDATE OR DELETE ON public.vehicle_reid_v2_profile_merges +FOR EACH ROW EXECUTE FUNCTION public.guard_vehicle_reid_v2_profile_merge_history(); + +CREATE OR REPLACE VIEW public.vehicle_reid_v2_current_profile_merges AS +SELECT merges.* +FROM public.vehicle_reid_v2_profile_merges merges +JOIN public.vehicle_reid_v2_pair_reviews reviews + ON reviews.id = merges.pair_review_id + AND reviews.revision = merges.pair_review_revision + AND reviews.label = 'same_vehicle' +WHERE EXISTS ( + SELECT 1 + FROM public.vehicle_reid_v2_exact_profile_members low_member + JOIN public.vehicle_reid_v2_exact_profile_members high_member ON TRUE + LEFT JOIN public.vehicle_reid_v2_profile_merges low_existing_merge + ON low_existing_merge.source_profile_id = low_member.profile_id + AND low_existing_merge.status = 'current' + AND low_existing_merge.id <> merges.id + LEFT JOIN public.vehicle_reid_v2_profile_merges high_existing_merge + ON high_existing_merge.source_profile_id = high_member.profile_id + AND high_existing_merge.status = 'current' + AND high_existing_merge.id <> merges.id + WHERE low_member.derivative_id = reviews.derivative_id_low + AND high_member.derivative_id = reviews.derivative_id_high + AND reviews.source_sha256_low = low_member.crop_content_sha256 + AND reviews.source_sha256_high = high_member.crop_content_sha256 + AND reviews.embedding_id_low = low_member.embedding_id + AND reviews.embedding_id_high = high_member.embedding_id + AND reviews.embedding_model = low_member.embedding_model + AND reviews.embedding_model = high_member.embedding_model + AND reviews.algorithm_version = low_member.embedding_algorithm_version + AND reviews.algorithm_version = high_member.embedding_algorithm_version + AND ( + (COALESCE(low_existing_merge.target_profile_id, low_member.profile_id) + = merges.source_profile_id + AND COALESCE(high_existing_merge.target_profile_id, high_member.profile_id) + = merges.target_profile_id) + OR (COALESCE(low_existing_merge.target_profile_id, low_member.profile_id) + = merges.target_profile_id + AND COALESCE(high_existing_merge.target_profile_id, high_member.profile_id) + = merges.source_profile_id) + ) +) +AND merges.status = 'current' +AND ( + SELECT COUNT(DISTINCT anchors.normalized_plate) + FROM public.vehicle_reid_v2_profile_plate_anchors anchors + JOIN public.plate_reads evidence ON evidence.id = anchors.evidence_read_id + WHERE anchors.status = 'current' + AND anchors.profile_id IN (merges.source_profile_id, merges.target_profile_id) + AND UPPER(REGEXP_REPLACE(evidence.plate_number, '[^A-Za-z0-9]', '', 'g')) + = anchors.normalized_plate + AND evidence.review_status = anchors.plate_review_status + AND evidence.review_revision = anchors.plate_review_revision + AND evidence.applied_alias_id IS NOT DISTINCT FROM anchors.applied_alias_id + AND anchors.plate_review_id IS NOT DISTINCT FROM ( + SELECT latest_reviews.id + FROM public.plate_read_reviews latest_reviews + WHERE latest_reviews.read_id = anchors.evidence_read_id + ORDER BY latest_reviews.created_at DESC, latest_reviews.id DESC + LIMIT 1 + ) +) <= 1; + +-- Reviewed plate anchors also remain historical rows. Only the exact latest +-- plate-review contract is current, and merge aliases resolve to one stable + +CREATE OR REPLACE VIEW public.vehicle_reid_v2_current_plate_anchors AS +SELECT anchors.*, + COALESCE(merges.target_profile_id, anchors.profile_id) + AS canonical_profile_id +FROM public.vehicle_reid_v2_profile_plate_anchors anchors +JOIN public.plate_reads evidence ON evidence.id = anchors.evidence_read_id +LEFT JOIN public.vehicle_reid_v2_current_profile_merges merges + ON merges.source_profile_id = anchors.profile_id +JOIN public.vehicle_reid_v2_profiles profiles + ON profiles.id = COALESCE(merges.target_profile_id, anchors.profile_id) +WHERE anchors.status = 'current' + AND profiles.status IN ('active','provisional') + AND UPPER(REGEXP_REPLACE(evidence.plate_number, '[^A-Za-z0-9]', '', 'g')) + = anchors.normalized_plate + AND evidence.review_status = anchors.plate_review_status + AND evidence.review_revision = anchors.plate_review_revision + AND evidence.applied_alias_id IS NOT DISTINCT FROM anchors.applied_alias_id + AND anchors.plate_review_id IS NOT DISTINCT FROM ( + SELECT reviews.id + FROM public.plate_read_reviews reviews + WHERE reviews.read_id = anchors.evidence_read_id + ORDER BY reviews.created_at DESC, reviews.id DESC + LIMIT 1 + ); + +CREATE OR REPLACE VIEW public.vehicle_reid_v2_current_profile_members AS +WITH canonical_members AS ( + SELECT members.*, + COALESCE(merges.target_profile_id, members.profile_id) + AS canonical_profile_id + FROM public.vehicle_reid_v2_exact_profile_members members + LEFT JOIN public.vehicle_reid_v2_current_profile_merges merges + ON merges.source_profile_id = members.profile_id +) +SELECT members.* +FROM canonical_members members +WHERE NOT EXISTS ( + SELECT 1 + FROM public.vehicle_image_asset_reads links + JOIN public.plate_reads reads ON reads.id = links.read_id + JOIN public.vehicle_reid_v2_current_plate_anchors anchors + ON anchors.normalized_plate = UPPER( + REGEXP_REPLACE(reads.plate_number, '[^A-Za-z0-9]', '', 'g') + ) + WHERE links.asset_id = members.asset_id + AND links.identity_eligible = TRUE + AND links.relationship <> 'display_fallback' + AND reads.vehicle_image_status = 'ready' + AND reads.vehicle_image_path = links.source_path_snapshot + AND reads.vehicle_image_source_kind = links.source_kind + AND reads.vehicle_image_updated_at IS NOT DISTINCT FROM links.source_updated_at + AND reads.review_status IN ('confirmed','corrected','alias_resolved') + AND anchors.canonical_profile_id <> members.canonical_profile_id +) +AND NOT EXISTS ( + SELECT 1 + FROM public.vehicle_reid_v2_pair_reviews reviews + JOIN public.vehicle_reid_v2_exact_profile_members low_member + ON low_member.derivative_id = reviews.derivative_id_low + JOIN public.vehicle_reid_v2_exact_profile_members high_member + ON high_member.derivative_id = reviews.derivative_id_high + LEFT JOIN public.vehicle_reid_v2_current_profile_merges low_merges + ON low_merges.source_profile_id = low_member.profile_id + LEFT JOIN public.vehicle_reid_v2_current_profile_merges high_merges + ON high_merges.source_profile_id = high_member.profile_id + WHERE reviews.label IN ('different_vehicle','unsure') + AND reviews.embedding_model = low_member.embedding_model + AND reviews.embedding_model = high_member.embedding_model + AND reviews.algorithm_version = low_member.embedding_algorithm_version + AND reviews.algorithm_version = high_member.embedding_algorithm_version + AND reviews.source_sha256_low = low_member.crop_content_sha256 + AND reviews.source_sha256_high = high_member.crop_content_sha256 + AND reviews.embedding_id_low = low_member.embedding_id + AND reviews.embedding_id_high = high_member.embedding_id + AND COALESCE(low_merges.target_profile_id, low_member.profile_id) + = members.canonical_profile_id + AND COALESCE(high_merges.target_profile_id, high_member.profile_id) + = members.canonical_profile_id +); + +-- Exact-plate assignments now bind a durable current plate anchor. Frozen + +-- assignments may use the same current anchor without fabricating a frozen +-- disposition fingerprint. +-- + +-- from a sealed row whose source/review contract later changed. Preserve the +-- sealed history and enforce one exact-current assignment per read under a +-- per-read transaction lock instead. +DROP INDEX IF EXISTS public.idx_reid_v2_assignment_one_active_read; +CREATE INDEX IF NOT EXISTS idx_reid_v2_assignment_active_read_history + ON public.vehicle_reid_v2_read_assignments (read_id, id DESC) + WHERE status = 'active'; + +CREATE OR REPLACE FUNCTION public.validate_vehicle_reid_v2_assignment_contract() +RETURNS TRIGGER AS $$ +BEGIN + IF NEW.status = 'active' AND NOT EXISTS ( + SELECT 1 + FROM public.vehicle_reid_v2_profiles profiles + LEFT JOIN public.vehicle_reid_v2_profile_members members + ON members.id = NEW.profile_member_id + AND members.profile_id = NEW.profile_id + AND members.status = 'current' + WHERE profiles.id = NEW.profile_id + AND profiles.revision = NEW.profile_revision + AND profiles.status IN ('active','provisional') + AND ( + (NEW.assignment_basis = 'exact_effective_plate') + OR (NEW.assignment_basis IN ('canonical_image','shared_asset','human_same') + AND members.membership_basis = NEW.profile_membership_basis) + ) + ) THEN + RAISE EXCEPTION 'ReID v2 assignment does not bind the current profile/member revision and evidence basis' + USING ERRCODE = '23514', + CONSTRAINT = 'vehicle_reid_v2_assignment_profile_contract'; + END IF; + + IF NEW.status = 'active' + AND NEW.assignment_basis IN ('canonical_image','shared_asset','human_same') + AND NOT EXISTS ( + SELECT 1 + FROM public.vehicle_reid_v2_profile_members members + JOIN public.vehicle_image_asset_reads links + ON links.asset_id = members.asset_id + AND links.read_id = NEW.read_id + JOIN public.plate_reads reads ON reads.id = links.read_id + WHERE members.id = NEW.profile_member_id + AND members.profile_id = NEW.profile_id + AND members.status = 'current' + AND members.asset_id = NEW.asset_id + AND members.derivative_id = NEW.derivative_id + AND members.embedding_id = NEW.embedding_id + AND links.identity_eligible = TRUE + AND links.relationship <> 'display_fallback' + AND links.source_kind IS NOT DISTINCT FROM NEW.source_kind + AND links.relationship IS NOT DISTINCT FROM NEW.source_relationship + AND links.source_path_snapshot IS NOT DISTINCT FROM NEW.source_path_snapshot + AND links.source_updated_at IS NOT DISTINCT FROM NEW.source_updated_at + AND links.updated_at IS NOT DISTINCT FROM NEW.source_link_updated_at + AND reads.vehicle_image_status = 'ready' + AND reads.vehicle_image_path = links.source_path_snapshot + AND reads.vehicle_image_source_kind = links.source_kind + AND reads.vehicle_image_updated_at IS NOT DISTINCT FROM links.source_updated_at + ) THEN + RAISE EXCEPTION 'ReID v2 image assignment is not an exact current member/source-link contract' + USING ERRCODE = '23514', + CONSTRAINT = 'vehicle_reid_v2_assignment_member_contract'; + END IF; + + IF NEW.status = 'active' + AND NEW.assignment_basis = 'exact_effective_plate' + AND NOT EXISTS ( + SELECT 1 FROM public.plate_reads reads + WHERE reads.id = NEW.read_id + AND UPPER(REGEXP_REPLACE(reads.plate_number, '[^A-Za-z0-9]', '', 'g')) + = NEW.normalized_effective_plate + AND reads.review_status = NEW.plate_review_status + AND reads.review_revision = NEW.plate_review_revision + AND reads.applied_alias_id IS NOT DISTINCT FROM NEW.applied_alias_id + AND NEW.plate_review_id IS NOT DISTINCT FROM ( + SELECT reviews.id + FROM public.plate_read_reviews reviews + WHERE reviews.read_id = NEW.read_id + ORDER BY reviews.created_at DESC, reviews.id DESC + LIMIT 1 + ) + ) THEN + RAISE EXCEPTION 'ReID v2 exact-plate assignment is not current reviewed plate evidence' + USING ERRCODE = '23514', + CONSTRAINT = 'vehicle_reid_v2_assignment_plate_contract'; + END IF; + + IF NEW.status = 'active' + AND NEW.assignment_basis = 'exact_effective_plate' + AND NOT EXISTS ( + SELECT 1 + FROM public.vehicle_reid_v2_profile_plate_anchors anchors + WHERE anchors.profile_id = NEW.profile_id + AND anchors.status = 'current' + AND anchors.normalized_plate = NEW.normalized_effective_plate + ) THEN + RAISE EXCEPTION 'ReID v2 exact-plate assignment requires a current profile plate anchor' + USING ERRCODE = '23514', + CONSTRAINT = 'vehicle_reid_v2_assignment_plate_profile_contract'; + END IF; + + + IF NEW.status = 'active' THEN + PERFORM pg_advisory_xact_lock(hashtext( + 'vehicle_reid_v2_read_assignment:' || NEW.read_id::TEXT + )); + -- Most Stage 2 materializations start from an empty authority table. Do + -- the cheap indexed history probe first so those thousands of inserts do + -- not repeatedly expand the exact-current consumer view. If history is + -- present, retain the full fail-closed current-contract validation. + IF EXISTS ( + SELECT 1 + FROM public.vehicle_reid_v2_read_assignments prior + WHERE prior.read_id = NEW.read_id + AND prior.status = 'active' + AND prior.id IS DISTINCT FROM NEW.id + ) THEN + IF EXISTS ( + SELECT 1 + FROM public.vehicle_reid_v2_current_read_assignments existing + WHERE existing.read_id = NEW.read_id + AND existing.id IS DISTINCT FROM NEW.id + ) THEN + RAISE EXCEPTION 'ReID v2 read already has an exact-current assignment' + USING ERRCODE = '23514', + CONSTRAINT = 'vehicle_reid_v2_assignment_one_exact_current_read'; + END IF; + END IF; + END IF; + RETURN NEW; +END; +$$ LANGUAGE plpgsql; + +-- Every primary consumer reads this view rather than trusting status='active'. +-- It revalidates the exact current plate review or canonical source-link/member +-- contract and resolves a current merge alias without mutating history. +CREATE OR REPLACE VIEW public.vehicle_reid_v2_current_read_assignments AS +SELECT assignments.*, + COALESCE(merges.target_profile_id, assignments.profile_id) + AS canonical_profile_id +FROM public.vehicle_reid_v2_read_assignments assignments +JOIN public.vehicle_reid_v2_profiles source_profiles + ON source_profiles.id = assignments.profile_id +LEFT JOIN public.vehicle_reid_v2_current_profile_merges merges + ON merges.source_profile_id = assignments.profile_id +JOIN public.vehicle_reid_v2_profiles canonical_profiles + ON canonical_profiles.id = COALESCE(merges.target_profile_id, assignments.profile_id) +JOIN public.plate_reads reads ON reads.id = assignments.read_id +WHERE assignments.status = 'active' + AND source_profiles.status IN ('active','provisional') + AND canonical_profiles.status IN ('active','provisional') + AND source_profiles.revision = assignments.profile_revision + AND ( + ( + assignments.assignment_basis = 'exact_effective_plate' + AND UPPER(REGEXP_REPLACE(reads.plate_number, '[^A-Za-z0-9]', '', 'g')) + = assignments.normalized_effective_plate + AND reads.review_status = assignments.plate_review_status + AND reads.review_revision = assignments.plate_review_revision + AND reads.applied_alias_id IS NOT DISTINCT FROM assignments.applied_alias_id + AND assignments.plate_review_id IS NOT DISTINCT FROM ( + SELECT reviews.id + FROM public.plate_read_reviews reviews + WHERE reviews.read_id = assignments.read_id + ORDER BY reviews.created_at DESC, reviews.id DESC + LIMIT 1 + ) + AND EXISTS ( + SELECT 1 + FROM public.vehicle_reid_v2_current_plate_anchors anchors + WHERE anchors.canonical_profile_id = + COALESCE(merges.target_profile_id, assignments.profile_id) + AND anchors.normalized_plate = assignments.normalized_effective_plate + ) + ) + OR ( + assignments.assignment_basis IN ('canonical_image','shared_asset','human_same') + AND EXISTS ( + SELECT 1 + FROM public.vehicle_reid_v2_current_profile_members members + JOIN public.vehicle_image_asset_reads links + ON links.asset_id = members.asset_id + AND links.read_id = assignments.read_id + WHERE members.id = assignments.profile_member_id + AND members.profile_id = assignments.profile_id + AND members.asset_id = assignments.asset_id + AND members.derivative_id = assignments.derivative_id + AND members.embedding_id = assignments.embedding_id + AND members.membership_basis = assignments.profile_membership_basis + AND members.canonical_profile_id = + COALESCE(merges.target_profile_id, assignments.profile_id) + AND links.identity_eligible = TRUE + AND links.relationship <> 'display_fallback' + AND links.source_kind IS NOT DISTINCT FROM assignments.source_kind + AND links.relationship IS NOT DISTINCT FROM assignments.source_relationship + AND links.source_path_snapshot IS NOT DISTINCT FROM assignments.source_path_snapshot + AND links.source_updated_at IS NOT DISTINCT FROM assignments.source_updated_at + AND links.updated_at IS NOT DISTINCT FROM assignments.source_link_updated_at + AND reads.vehicle_image_status = 'ready' + AND reads.vehicle_image_path = links.source_path_snapshot + AND reads.vehicle_image_source_kind = links.source_kind + AND reads.vehicle_image_updated_at IS NOT DISTINCT FROM links.source_updated_at + ) + ) + ); + +CREATE TABLE IF NOT EXISTS public.vehicle_reid_v2_live_discovery_state ( + singleton BOOLEAN PRIMARY KEY DEFAULT TRUE CHECK (singleton = TRUE), + forward_cursor_read_id INTEGER NOT NULL DEFAULT 0 CHECK ( + forward_cursor_read_id >= 0 + ), + revisit_cursor_read_id INTEGER NOT NULL DEFAULT 0 CHECK ( + revisit_cursor_read_id >= 0 + ), + revisit_upper_read_id INTEGER NOT NULL DEFAULT 0 CHECK ( + revisit_upper_read_id >= 0 + AND revisit_cursor_read_id <= revisit_upper_read_id + ), + revisit_epoch BIGINT NOT NULL DEFAULT 0 CHECK (revisit_epoch >= 0), + forward_windows_since_revisit SMALLINT NOT NULL DEFAULT 0 CHECK ( + forward_windows_since_revisit BETWEEN 0 AND 8 + ), + last_scanned_at TIMESTAMPTZ, + revision BIGINT NOT NULL DEFAULT 1 CHECK (revision > 0), + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + CHECK (revisit_upper_read_id <= forward_cursor_read_id) +); +CREATE OR REPLACE FUNCTION public.validate_vehicle_reid_v2_live_discovery_state() +RETURNS TRIGGER AS $$ +BEGIN + IF TG_OP = 'DELETE' THEN + RAISE EXCEPTION 'The ReID v2 live discovery singleton cannot be deleted' + USING ERRCODE = '23514', + CONSTRAINT = 'vehicle_reid_v2_live_discovery_singleton_immutable'; + END IF; + IF NEW.forward_cursor_read_id < OLD.forward_cursor_read_id THEN + RAISE EXCEPTION 'The ReID v2 live forward cursor cannot rewind' + USING ERRCODE = '23514', + CONSTRAINT = 'vehicle_reid_v2_live_discovery_forward_monotonic'; + END IF; + IF NEW.revision <> OLD.revision + 1 THEN + RAISE EXCEPTION 'ReID v2 live discovery updates require the next revision' + USING ERRCODE = '23514', + CONSTRAINT = 'vehicle_reid_v2_live_discovery_revision'; + END IF; + RETURN NEW; +END; +$$ LANGUAGE plpgsql; + +DROP TRIGGER IF EXISTS vehicle_reid_v2_live_discovery_validate + ON public.vehicle_reid_v2_live_discovery_state; +CREATE TRIGGER vehicle_reid_v2_live_discovery_validate +BEFORE UPDATE OR DELETE ON public.vehicle_reid_v2_live_discovery_state +FOR EACH ROW EXECUTE FUNCTION public.validate_vehicle_reid_v2_live_discovery_state(); + +INSERT INTO public.vehicle_reid_v2_live_discovery_state(singleton) +VALUES (TRUE) ON CONFLICT (singleton) DO NOTHING; + +CREATE TABLE IF NOT EXISTS public.vehicle_asset_analysis_jobs ( + derivative_id BIGINT PRIMARY KEY REFERENCES public.vehicle_image_derivatives(id) ON DELETE CASCADE, + status VARCHAR(12) NOT NULL DEFAULT 'pending' CHECK (status IN ('pending','processing','ready','failed')), + attempt_count SMALLINT NOT NULL DEFAULT 0 CHECK (attempt_count BETWEEN 0 AND 3), + claim_token UUID, + lease_until TIMESTAMPTZ, + next_attempt_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + error_code VARCHAR(100), + updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP +); +CREATE INDEX IF NOT EXISTS idx_vehicle_asset_analysis_pending + ON public.vehicle_asset_analysis_jobs(next_attempt_at, derivative_id) + WHERE status <> 'ready' AND attempt_count < 3; + + +-- Read-facing attributes project exact-current canonical crops. +DO $attribute_upgrade$ +BEGIN + IF EXISTS (SELECT 1 FROM pg_class c JOIN pg_namespace n ON n.oid = c.relnamespace + WHERE n.nspname = 'public' AND c.relname = 'vehicle_attribute_observations' AND c.relkind = 'r') THEN + DROP TABLE public.vehicle_attribute_observations; + END IF; +END +$attribute_upgrade$; +CREATE OR REPLACE VIEW public.vehicle_attribute_observations AS +SELECT observations.id, links.read_id, observations.attribute_key, + observations.provider, observations.model_version, observations.status, + observations.attribute_value, observations.confidence, observations.raw_result, + observations.created_at AS evaluated_at +FROM public.vehicle_image_asset_reads links +JOIN public.plate_reads reads ON reads.id = links.read_id +JOIN public.vehicle_image_assets assets ON assets.id = links.asset_id +JOIN public.vehicle_image_derivatives derivatives ON derivatives.asset_id = assets.id + AND derivatives.source_sha256 = assets.content_sha256 + AND derivatives.derivative_kind = 'vehicle_crop' + AND derivatives.algorithm_version = 'canonical-overview-detection-box-v1' +JOIN public.vehicle_asset_attribute_observations observations + ON observations.derivative_id = derivatives.id + AND observations.source_sha256 = derivatives.content_sha256 + AND observations.algorithm_version = 'canonical-overview-crop-attributes-v1' +WHERE links.identity_eligible = TRUE AND links.relationship <> 'display_fallback' + AND reads.vehicle_image_status = 'ready' AND reads.vehicle_image_path = links.source_path_snapshot + AND reads.vehicle_image_source_kind = links.source_kind + AND reads.vehicle_image_updated_at IS NOT DISTINCT FROM links.source_updated_at; + +CREATE OR REPLACE VIEW public.vehicle_direction_sources AS +SELECT DISTINCT ON (links.read_id) + links.read_id, derivatives.id AS derivative_id, embeddings.id AS embedding_id, + derivatives.storage_path AS derived_path, derivatives.content_sha256 AS source_sha256, + embeddings.embedding AS vehicle_embedding, + 'canonical-crop-direction-v2'::text AS embedding_model, + 'vehicle_crop'::text AS asset_type, + embeddings.algorithm_version, + 'ready'::text AS status +FROM public.vehicle_image_asset_reads links +JOIN public.plate_reads reads ON reads.id = links.read_id +JOIN public.vehicle_image_assets assets ON assets.id = links.asset_id +JOIN public.vehicle_image_derivatives derivatives + ON derivatives.asset_id = assets.id + AND derivatives.source_sha256 = assets.content_sha256 + AND derivatives.derivative_kind = 'vehicle_crop' + AND derivatives.algorithm_version = 'canonical-overview-detection-box-v1' +JOIN public.vehicle_asset_embeddings embeddings + ON embeddings.derivative_id = derivatives.id + AND embeddings.source_sha256 = derivatives.content_sha256 + AND embeddings.model_name = 'vehicle-reid-0001-ir-fp16-v1' + AND embeddings.algorithm_version = 'canonical-overview-crop-embedding-v1' +WHERE links.identity_eligible = TRUE AND links.relationship <> 'display_fallback' + AND reads.vehicle_image_status = 'ready' + AND reads.vehicle_image_path = links.source_path_snapshot + AND reads.vehicle_image_source_kind = links.source_kind + AND reads.vehicle_image_updated_at IS NOT DISTINCT FROM links.source_updated_at +ORDER BY links.read_id, derivatives.id DESC, embeddings.id DESC; + + +ALTER TABLE public.vehicle_orientation_labels + ADD COLUMN IF NOT EXISTS source_embedding_id BIGINT REFERENCES public.vehicle_asset_embeddings(id) ON DELETE CASCADE; +ALTER TABLE public.vehicle_direction_observations + ADD COLUMN IF NOT EXISTS source_embedding_id BIGINT REFERENCES public.vehicle_asset_embeddings(id) ON DELETE CASCADE; + +-- Reviewed examples and predictions belong to the exact current canonical crop. +CREATE OR REPLACE VIEW public.current_vehicle_orientation_labels AS +SELECT labels.* FROM public.vehicle_orientation_labels labels +JOIN public.vehicle_direction_sources source + ON source.read_id = labels.read_id AND source.embedding_id = labels.source_embedding_id +WHERE labels.embedding_model = 'canonical-crop-direction-v2'; + +CREATE OR REPLACE VIEW public.current_vehicle_direction_observations AS +SELECT observations.* FROM public.vehicle_direction_observations observations +WHERE observations.classifier_version IN ('blue-iris-zone-crossing-v1','blue-iris-zone-crossing-v2-primary','axis-lpv-direction-v1') + OR (observations.classifier_version = 'canonical-crop-orientation-knn-v2' + AND EXISTS (SELECT 1 FROM public.vehicle_direction_sources source + WHERE source.read_id = observations.read_id + AND source.embedding_id = observations.source_embedding_id)); + +-- Retire only derived predictions/calibration that cannot identify their crop. +DELETE FROM public.vehicle_direction_observations + WHERE classifier_version IN ('vehicle-reid-orientation-knn-v1','canonical-crop-orientation-knn-v2') AND source_embedding_id IS NULL; +DELETE FROM public.vehicle_orientation_labels WHERE source_embedding_id IS NULL; + +INSERT INTO public.schema_migrations(version, description) VALUES + ('2026092701_native_reid', 'Native canonical vehicle identity and automatic image processing') +ON CONFLICT (version) DO NOTHING; diff --git a/migrations/0066_2026082201_optional_radar_events.sql b/migrations/0066_2026082201_optional_radar_events.sql new file mode 100644 index 0000000..1f22446 --- /dev/null +++ b/migrations/0066_2026082201_optional_radar_events.sql @@ -0,0 +1,53 @@ +-- migration: 2026082201_optional_radar_events +-- Direct local speed-radar ingestion. The source reuses an existing MQTT broker +-- credential record, stores only bounded JSON vehicle detections, and links one +-- event to at most one ALPR read through timestamp and configured direction. +CREATE TABLE IF NOT EXISTS public.radar_settings ( + id SMALLINT PRIMARY KEY DEFAULT 1 CHECK (id = 1), + enabled BOOLEAN NOT NULL DEFAULT FALSE, + broker_id INTEGER NOT NULL REFERENCES public.mqttbrokers(id) ON DELETE RESTRICT, + topic_filter VARCHAR(512) NOT NULL CHECK (BTRIM(topic_filter) <> ''), + source_key VARCHAR(255) NOT NULL CHECK (BTRIM(source_key) <> ''), + qos SMALLINT NOT NULL DEFAULT 1 CHECK (qos BETWEEN 0 AND 2), + correlation_window_ms INTEGER NOT NULL DEFAULT 8000 CHECK (correlation_window_ms BETWEEN 250 AND 60000), + inbound_alpr_direction VARCHAR(80) NOT NULL DEFAULT 'Entering' CHECK (BTRIM(inbound_alpr_direction) <> ''), + outbound_alpr_direction VARCHAR(80) NOT NULL DEFAULT 'Exiting' CHECK (BTRIM(outbound_alpr_direction) <> ''), + last_connected_at TIMESTAMPTZ, + last_message_at TIMESTAMPTZ, + last_error TEXT, + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP +); + +CREATE TABLE IF NOT EXISTS public.radar_events ( + id BIGSERIAL PRIMARY KEY, + source_key VARCHAR(255) NOT NULL CHECK (BTRIM(source_key) <> ''), + topic VARCHAR(512) NOT NULL CHECK (BTRIM(topic) <> ''), + event_timestamp TIMESTAMPTZ NOT NULL, + received_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + speed_mph NUMERIC(5,1) NOT NULL CHECK (speed_mph > 0 AND speed_mph <= 200), + signed_speed NUMERIC, + source_unit VARCHAR(16) NOT NULL CHECK (source_unit IN ('mph','kmh','mps')), + direction VARCHAR(16) NOT NULL CHECK (direction IN ('inbound','outbound')), + source VARCHAR(255), + label VARCHAR(255), + message_hash CHAR(64) NOT NULL UNIQUE CHECK (message_hash ~ '^[0-9a-f]{64}$'), + raw_payload JSONB NOT NULL CHECK (jsonb_typeof(raw_payload) = 'object'), + matched_read_id INTEGER UNIQUE REFERENCES public.plate_reads(id) ON DELETE SET NULL, + match_delta_ms INTEGER, + matched_at TIMESTAMPTZ, + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + CONSTRAINT radar_events_match_state CHECK ( + (matched_read_id IS NULL AND match_delta_ms IS NULL AND matched_at IS NULL) + OR (matched_read_id IS NOT NULL AND match_delta_ms IS NOT NULL AND matched_at IS NOT NULL) + ) +); + +CREATE INDEX IF NOT EXISTS idx_radar_events_unmatched + ON public.radar_events (event_timestamp, id) WHERE matched_read_id IS NULL; +CREATE INDEX IF NOT EXISTS idx_radar_events_speed + ON public.radar_events (speed_mph, event_timestamp DESC); + +INSERT INTO public.schema_migrations(version,description) VALUES + ('2026082201_optional_radar_events','Add default-disabled generic radar event storage for schema compatibility.') +ON CONFLICT(version) DO NOTHING; diff --git a/migrations/0067_2026082202_blue_iris_camera_inventory.sql b/migrations/0067_2026082202_blue_iris_camera_inventory.sql new file mode 100644 index 0000000..d806443 --- /dev/null +++ b/migrations/0067_2026082202_blue_iris_camera_inventory.sql @@ -0,0 +1,23 @@ +-- migration: 2026082202_blue_iris_camera_inventory +-- Cache the sanitized Blue Iris camera inventory so playback links can use the +-- stable short camera identifier even when reads and overview profiles retain +-- the operator-facing display name. The vehicle-frame worker refreshes this +-- metadata only after a successful read-only camlist request. +CREATE TABLE IF NOT EXISTS public.blue_iris_camera_inventory ( + short_name VARCHAR(80) PRIMARY KEY CHECK (BTRIM(short_name) <> ''), + display_name VARCHAR(255) NOT NULL CHECK (BTRIM(display_name) <> ''), + online BOOLEAN NOT NULL DEFAULT FALSE, + enabled BOOLEAN NOT NULL DEFAULT TRUE, + present BOOLEAN NOT NULL DEFAULT TRUE, + first_seen_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + last_seen_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP +); + +CREATE INDEX IF NOT EXISTS idx_blue_iris_camera_inventory_display_name + ON public.blue_iris_camera_inventory (LOWER(BTRIM(display_name))) + WHERE present = TRUE; + +INSERT INTO public.schema_migrations(version,description) VALUES + ('2026082202_blue_iris_camera_inventory','Cache sanitized Blue Iris display-to-short camera mappings for reliable plate-capture and Vehicle View playback links.') +ON CONFLICT(version) DO NOTHING; diff --git a/migrations/0068_2026082203_recognition_filter_indexes.sql b/migrations/0068_2026082203_recognition_filter_indexes.sql new file mode 100644 index 0000000..4f61dc2 --- /dev/null +++ b/migrations/0068_2026082203_recognition_filter_indexes.sql @@ -0,0 +1,20 @@ +-- migration: 2026082203_recognition_filter_indexes +-- Recognition Feed filters should use bounded index lookups instead of +-- rescanning the read history whenever an operator changes a control. +CREATE INDEX IF NOT EXISTS idx_plate_reads_filter_camera + ON public.plate_reads (LOWER(camera_name), "timestamp" DESC, id DESC) + WHERE camera_name IS NOT NULL; +CREATE INDEX IF NOT EXISTS idx_plate_reads_filter_review_status + ON public.plate_reads (review_status, "timestamp" DESC, id DESC) + WHERE review_status IS NOT NULL; +CREATE INDEX IF NOT EXISTS idx_plate_tags_filter_tag + ON public.plate_tags (tag_id, plate_number); +CREATE INDEX IF NOT EXISTS idx_vehicle_direction_filter_label + ON public.vehicle_direction_observations (LOWER(direction_label), read_id) + WHERE direction_label IS NOT NULL; +CREATE INDEX IF NOT EXISTS idx_vehicle_direction_filter_status + ON public.vehicle_direction_observations (status, read_id); + +INSERT INTO public.schema_migrations(version,description) VALUES + ('2026082203_recognition_filter_indexes','Add query-path indexes for Recognition Feed camera, tag, review-status, and direction filters.') +ON CONFLICT(version) DO NOTHING; diff --git a/migrations/0069_2026082301_vehicle_passage_foundation.sql b/migrations/0069_2026082301_vehicle_passage_foundation.sql new file mode 100644 index 0000000..4dbf2dc --- /dev/null +++ b/migrations/0069_2026082301_vehicle_passage_foundation.sql @@ -0,0 +1,451 @@ +-- migration: 2026082301_vehicle_passage_foundation +-- Passage-first traffic foundation. This is additive and default-shadow: the +-- legacy radar-to-read fields remain readable while new observations acquire +-- explicit clock provenance, audit evidence, and canonical passage ownership. +CREATE TABLE IF NOT EXISTS public.traffic_correlation_profiles ( + id BIGSERIAL PRIMARY KEY, + profile_key VARCHAR(100) NOT NULL CHECK (NULLIF(BTRIM(profile_key), '') IS NOT NULL), + version INTEGER NOT NULL CHECK (version > 0), + state VARCHAR(16) NOT NULL DEFAULT 'draft' CHECK ( + state IN ('draft','shadow','active','retired') + ), + camera_order JSONB NOT NULL DEFAULT '[]'::JSONB CHECK (jsonb_typeof(camera_order) = 'array'), + measured_distances JSONB NOT NULL DEFAULT '{}'::JSONB CHECK (jsonb_typeof(measured_distances) = 'object'), + direction_mappings JSONB NOT NULL DEFAULT '{}'::JSONB CHECK (jsonb_typeof(direction_mappings) = 'object'), + timing_offsets JSONB NOT NULL DEFAULT '{}'::JSONB CHECK (jsonb_typeof(timing_offsets) = 'object'), + correlation_window_ms INTEGER NOT NULL DEFAULT 15000 CHECK ( + correlation_window_ms BETWEEN 1000 AND 120000 + ), + confidence_thresholds JSONB NOT NULL DEFAULT '{}'::JSONB CHECK (jsonb_typeof(confidence_thresholds) = 'object'), + overview_settings JSONB NOT NULL DEFAULT '{}'::JSONB CHECK (jsonb_typeof(overview_settings) = 'object'), + clock_sanity_seconds INTEGER NOT NULL DEFAULT 300 CHECK ( + clock_sanity_seconds BETWEEN 5 AND 86400 + ), + supersedes_profile_id BIGINT REFERENCES public.traffic_correlation_profiles(id) ON DELETE RESTRICT, + created_by_user_id BIGINT REFERENCES public.users(id) ON DELETE SET NULL, + activated_by_user_id BIGINT REFERENCES public.users(id) ON DELETE SET NULL, + activated_at TIMESTAMPTZ, + retired_at TIMESTAMPTZ, + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + UNIQUE (profile_key, version), + CHECK ((state = 'active' AND activated_at IS NOT NULL) OR state <> 'active') +); + +CREATE UNIQUE INDEX IF NOT EXISTS idx_traffic_correlation_profiles_one_active + ON public.traffic_correlation_profiles (profile_key) + WHERE state = 'active'; + +INSERT INTO public.traffic_correlation_profiles ( + profile_key, version, state, camera_order, measured_distances, + direction_mappings, timing_offsets, confidence_thresholds, + overview_settings, clock_sanity_seconds +) VALUES ( + 'generic-radar-profile', 1, 'shadow', + '["street-lpr-1","street-lpr-2","street-overview"]'::JSONB, + '{}'::JSONB, '{}'::JSONB, '{}'::JSONB, '{}'::JSONB, '{}'::JSONB, 300 +) +ON CONFLICT (profile_key, version) DO NOTHING; + +CREATE TABLE IF NOT EXISTS public.vehicle_passages ( + id BIGSERIAL PRIMARY KEY, + canonical_event_time TIMESTAMPTZ NOT NULL, + direction VARCHAR(100), + selected_speed_mph NUMERIC(5,1) CHECK ( + selected_speed_mph IS NULL OR (selected_speed_mph > 0 AND selected_speed_mph <= 200) + ), + lifecycle_state VARCHAR(24) NOT NULL DEFAULT 'provisional' CHECK ( + lifecycle_state IN ('provisional','finalized','ambiguous','rejected') + ), + association_confidence NUMERIC(6,5) CHECK ( + association_confidence IS NULL OR (association_confidence >= 0 AND association_confidence <= 1) + ), + review_state VARCHAR(24) NOT NULL DEFAULT 'unreviewed' CHECK ( + review_state IN ('unreviewed','needs_review','confirmed','rejected') + ), + correlation_profile_id BIGINT REFERENCES public.traffic_correlation_profiles(id) ON DELETE RESTRICT, + correlation_profile_version INTEGER CHECK (correlation_profile_version IS NULL OR correlation_profile_version > 0), + correlation_algorithm VARCHAR(100), + legacy_radar_event_id BIGINT UNIQUE REFERENCES public.radar_events(id) ON DELETE RESTRICT, + manual_locked BOOLEAN NOT NULL DEFAULT FALSE, + reviewed_by_user_id BIGINT REFERENCES public.users(id) ON DELETE SET NULL, + reviewed_at TIMESTAMPTZ, + finalized_at TIMESTAMPTZ, + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + CHECK ((lifecycle_state = 'finalized' AND finalized_at IS NOT NULL) OR lifecycle_state <> 'finalized'), + CHECK ((reviewed_at IS NULL AND reviewed_by_user_id IS NULL) OR reviewed_at IS NOT NULL) +); + +CREATE INDEX IF NOT EXISTS idx_vehicle_passages_recent + ON public.vehicle_passages (canonical_event_time DESC, id DESC); +CREATE INDEX IF NOT EXISTS idx_vehicle_passages_review + ON public.vehicle_passages (review_state, canonical_event_time DESC, id DESC) + WHERE review_state IN ('unreviewed','needs_review'); + +CREATE TABLE IF NOT EXISTS public.camera_observations ( + id BIGSERIAL PRIMARY KEY, + source_key VARCHAR(255) NOT NULL CHECK (NULLIF(BTRIM(source_key), '') IS NOT NULL), + camera_key VARCHAR(120) NOT NULL CHECK (NULLIF(BTRIM(camera_key), '') IS NOT NULL), + observation_kind VARCHAR(20) NOT NULL CHECK ( + observation_kind IN ('lpr','overview','track','other') + ), + observation_timestamp TIMESTAMPTZ NOT NULL, + received_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + direction_label VARCHAR(100), + lane_label VARCHAR(100), + zone_label VARCHAR(100), + blue_iris_timeline_path TEXT, + blue_iris_clip_name VARCHAR(255), + detection_track_key VARCHAR(255), + plate_read_id INTEGER REFERENCES public.plate_reads(id) ON DELETE SET NULL, + overview_image_path TEXT, + raw_payload JSONB NOT NULL DEFAULT '{}'::JSONB CHECK (jsonb_typeof(raw_payload) = 'object'), + idempotency_key CHAR(64) NOT NULL UNIQUE CHECK (idempotency_key ~ '^[0-9a-f]{64}$'), + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP +); + +CREATE INDEX IF NOT EXISTS idx_camera_observations_recent + ON public.camera_observations (observation_timestamp DESC, id DESC); +CREATE INDEX IF NOT EXISTS idx_camera_observations_unidentified + ON public.camera_observations (camera_key, observation_timestamp DESC, id DESC) + WHERE plate_read_id IS NULL; + +ALTER TABLE public.vehicle_passages + ADD COLUMN IF NOT EXISTS selected_overview_observation_id BIGINT; + +DO $$ +BEGIN + IF NOT EXISTS ( + SELECT 1 FROM pg_constraint + WHERE conname = 'vehicle_passages_selected_overview_observation_fkey' + AND conrelid = 'public.vehicle_passages'::regclass + ) THEN + ALTER TABLE public.vehicle_passages + ADD CONSTRAINT vehicle_passages_selected_overview_observation_fkey + FOREIGN KEY (selected_overview_observation_id) + REFERENCES public.camera_observations(id) ON DELETE SET NULL; + END IF; +END $$; + +CREATE TABLE IF NOT EXISTS public.passage_observation_associations ( + id BIGSERIAL PRIMARY KEY, + passage_id BIGINT NOT NULL REFERENCES public.vehicle_passages(id) ON DELETE CASCADE, + observation_type VARCHAR(16) NOT NULL CHECK (observation_type IN ('radar','camera')), + radar_event_id BIGINT REFERENCES public.radar_events(id) ON DELETE RESTRICT, + camera_observation_id BIGINT REFERENCES public.camera_observations(id) ON DELETE RESTRICT, + time_delta_ms INTEGER, + predicted_travel_delta_ms INTEGER, + direction_score NUMERIC(8,5), + ordering_score NUMERIC(8,5), + headway_score NUMERIC(8,5), + plate_score NUMERIC(8,5), + reid_score NUMERIC(8,5), + lane_score NUMERIC(8,5), + clock_quality_score NUMERIC(8,5), + missing_observation_penalty NUMERIC(8,5), + reflection_penalty NUMERIC(8,5), + total_score NUMERIC(10,5), + confidence NUMERIC(6,5) CHECK (confidence IS NULL OR (confidence >= 0 AND confidence <= 1)), + confidence_class VARCHAR(16) CHECK ( + confidence_class IS NULL OR confidence_class IN ('high','medium','low','ambiguous') + ), + algorithm VARCHAR(100) NOT NULL CHECK (NULLIF(BTRIM(algorithm), '') IS NOT NULL), + algorithm_revision INTEGER NOT NULL CHECK (algorithm_revision > 0), + correlation_profile_id BIGINT REFERENCES public.traffic_correlation_profiles(id) ON DELETE RESTRICT, + correlation_profile_version INTEGER CHECK (correlation_profile_version IS NULL OR correlation_profile_version > 0), + association_source VARCHAR(16) NOT NULL CHECK ( + association_source IN ('automatic','manual','migration') + ), + manual_locked BOOLEAN NOT NULL DEFAULT FALSE, + evidence JSONB NOT NULL DEFAULT '{}'::JSONB CHECK (jsonb_typeof(evidence) = 'object'), + created_by_user_id BIGINT REFERENCES public.users(id) ON DELETE SET NULL, + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + detached_at TIMESTAMPTZ, + detached_by_user_id BIGINT REFERENCES public.users(id) ON DELETE SET NULL, + detach_reason TEXT, + CHECK ( + (observation_type = 'radar' AND radar_event_id IS NOT NULL AND camera_observation_id IS NULL) + OR (observation_type = 'camera' AND camera_observation_id IS NOT NULL AND radar_event_id IS NULL) + ), + CHECK ( + (detached_at IS NULL AND detached_by_user_id IS NULL AND detach_reason IS NULL) + OR (detached_at IS NOT NULL AND NULLIF(BTRIM(detach_reason), '') IS NOT NULL) + ) +); + +CREATE UNIQUE INDEX IF NOT EXISTS idx_passage_associations_active_radar + ON public.passage_observation_associations (radar_event_id) + WHERE radar_event_id IS NOT NULL AND detached_at IS NULL; +CREATE UNIQUE INDEX IF NOT EXISTS idx_passage_associations_active_camera + ON public.passage_observation_associations (camera_observation_id) + WHERE camera_observation_id IS NOT NULL AND detached_at IS NULL; +CREATE INDEX IF NOT EXISTS idx_passage_associations_passage + ON public.passage_observation_associations (passage_id, created_at, id); + +CREATE OR REPLACE FUNCTION public.protect_locked_passage_association() +RETURNS TRIGGER AS $$ +BEGIN + IF TG_OP = 'DELETE' AND OLD.manual_locked THEN + RAISE EXCEPTION 'locked passage association must be explicitly unlocked before detach'; + END IF; + IF TG_OP = 'UPDATE' AND OLD.manual_locked THEN + IF NEW.manual_locked THEN + IF (to_jsonb(NEW) - ARRAY['updated_at']) IS DISTINCT FROM + (to_jsonb(OLD) - ARRAY['updated_at']) THEN + RAISE EXCEPTION 'automatic overwrite of a locked passage association is forbidden'; + END IF; + ELSIF (to_jsonb(NEW) - ARRAY['manual_locked','updated_at']) IS DISTINCT FROM + (to_jsonb(OLD) - ARRAY['manual_locked','updated_at']) THEN + RAISE EXCEPTION 'unlock and mutation of a passage association must be separate operations'; + END IF; + END IF; + IF TG_OP = 'DELETE' THEN RETURN OLD; END IF; + RETURN NEW; +END; +$$ LANGUAGE plpgsql; + +DROP TRIGGER IF EXISTS passage_association_manual_lock ON public.passage_observation_associations; +CREATE TRIGGER passage_association_manual_lock +BEFORE UPDATE OR DELETE ON public.passage_observation_associations +FOR EACH ROW EXECUTE FUNCTION public.protect_locked_passage_association(); + +CREATE TABLE IF NOT EXISTS public.passage_review_history ( + id BIGSERIAL PRIMARY KEY, + passage_id BIGINT NOT NULL REFERENCES public.vehicle_passages(id) ON DELETE RESTRICT, + association_id BIGINT REFERENCES public.passage_observation_associations(id) ON DELETE SET NULL, + action VARCHAR(24) NOT NULL CHECK ( + action IN ('attach','detach','split','merge','reject','confirm','lock','unlock','note') + ), + actor_user_id BIGINT REFERENCES public.users(id) ON DELETE SET NULL, + reason TEXT, + before_state JSONB NOT NULL DEFAULT '{}'::JSONB CHECK (jsonb_typeof(before_state) = 'object'), + after_state JSONB NOT NULL DEFAULT '{}'::JSONB CHECK (jsonb_typeof(after_state) = 'object'), + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP +); + +CREATE INDEX IF NOT EXISTS idx_passage_review_history_passage + ON public.passage_review_history (passage_id, created_at DESC, id DESC); + +CREATE OR REPLACE FUNCTION public.reject_passage_review_history_mutation() +RETURNS TRIGGER AS $$ +BEGIN + RAISE EXCEPTION 'passage_review_history is append-only'; +END; +$$ LANGUAGE plpgsql; + +DROP TRIGGER IF EXISTS passage_review_history_append_only ON public.passage_review_history; +CREATE TRIGGER passage_review_history_append_only +BEFORE UPDATE OR DELETE ON public.passage_review_history +FOR EACH ROW EXECUTE FUNCTION public.reject_passage_review_history_mutation(); + +ALTER TABLE public.radar_settings + ADD COLUMN IF NOT EXISTS legacy_correlation_enabled BOOLEAN NOT NULL DEFAULT FALSE, + ADD COLUMN IF NOT EXISTS clock_sanity_seconds INTEGER NOT NULL DEFAULT 300; + +ALTER TABLE public.radar_settings + DROP CONSTRAINT IF EXISTS radar_settings_clock_sanity_seconds_check; +ALTER TABLE public.radar_settings + ADD CONSTRAINT radar_settings_clock_sanity_seconds_check CHECK ( + clock_sanity_seconds BETWEEN 5 AND 86400 + ); + +ALTER TABLE public.radar_events + ADD COLUMN IF NOT EXISTS device_timestamp_raw TEXT, + ADD COLUMN IF NOT EXISTS device_timestamp TIMESTAMPTZ, + ADD COLUMN IF NOT EXISTS canonical_timestamp TIMESTAMPTZ, + ADD COLUMN IF NOT EXISTS timestamp_quality VARCHAR(32), + ADD COLUMN IF NOT EXISTS timestamp_reason VARCHAR(100), + ADD COLUMN IF NOT EXISTS payload_hash CHAR(64), + ADD COLUMN IF NOT EXISTS idempotency_key CHAR(64), + ADD COLUMN IF NOT EXISTS duplicate_count INTEGER NOT NULL DEFAULT 0, + ADD COLUMN IF NOT EXISTS last_duplicate_at TIMESTAMPTZ, + ADD COLUMN IF NOT EXISTS passage_id BIGINT REFERENCES public.vehicle_passages(id) ON DELETE SET NULL; + +UPDATE public.radar_events +SET device_timestamp_raw = COALESCE( + raw_payload->>'timestamp', raw_payload->>'time', + raw_payload->>'event_timestamp', raw_payload->>'eventTimestamp', raw_payload->>'ts' + ), + device_timestamp = CASE + WHEN event_timestamp >= TIMESTAMPTZ '2000-01-01 00:00:00+00' + AND event_timestamp < TIMESTAMPTZ '2100-01-01 00:00:00+00' + THEN event_timestamp ELSE NULL END, + canonical_timestamp = CASE + WHEN event_timestamp >= TIMESTAMPTZ '2000-01-01 00:00:00+00' + AND event_timestamp < TIMESTAMPTZ '2100-01-01 00:00:00+00' + AND ABS(EXTRACT(EPOCH FROM (event_timestamp - received_at))) <= 300 + THEN event_timestamp ELSE received_at END, + timestamp_quality = CASE + WHEN event_timestamp < TIMESTAMPTZ '2000-01-01 00:00:00+00' + THEN 'historical_unsynced' + WHEN ABS(EXTRACT(EPOCH FROM (event_timestamp - received_at))) <= 300 + THEN 'device_synced' + ELSE 'receipt_fallback' END, + timestamp_reason = CASE + WHEN event_timestamp < TIMESTAMPTZ '2000-01-01 00:00:00+00' + THEN 'migration_pre_ntp_uptime' + WHEN ABS(EXTRACT(EPOCH FROM (event_timestamp - received_at))) <= 300 + THEN 'migration_device_within_receipt_window' + ELSE 'migration_device_outside_receipt_window' END, + payload_hash = message_hash, + idempotency_key = message_hash +WHERE canonical_timestamp IS NULL + OR timestamp_quality IS NULL + OR timestamp_reason IS NULL + OR payload_hash IS NULL + OR idempotency_key IS NULL; + +ALTER TABLE public.radar_events + ALTER COLUMN canonical_timestamp SET NOT NULL, + ALTER COLUMN timestamp_quality SET NOT NULL, + ALTER COLUMN timestamp_reason SET NOT NULL, + ALTER COLUMN payload_hash SET NOT NULL, + ALTER COLUMN idempotency_key SET NOT NULL; + +ALTER TABLE public.radar_events + DROP CONSTRAINT IF EXISTS radar_events_timestamp_quality_check; +ALTER TABLE public.radar_events + ADD CONSTRAINT radar_events_timestamp_quality_check CHECK ( + timestamp_quality IN ('device_synced','receipt_fallback','invalid','historical_unsynced') + ); +ALTER TABLE public.radar_events + DROP CONSTRAINT IF EXISTS radar_events_duplicate_count_check; +ALTER TABLE public.radar_events + ADD CONSTRAINT radar_events_duplicate_count_check CHECK (duplicate_count >= 0); + +CREATE UNIQUE INDEX IF NOT EXISTS idx_radar_events_idempotency_key + ON public.radar_events (idempotency_key); +CREATE INDEX IF NOT EXISTS idx_radar_events_canonical_recent + ON public.radar_events (canonical_timestamp DESC, id DESC); +CREATE INDEX IF NOT EXISTS idx_radar_events_passage + ON public.radar_events (passage_id, canonical_timestamp, id) + WHERE passage_id IS NOT NULL; +CREATE INDEX IF NOT EXISTS idx_radar_events_unassociated_passage + ON public.radar_events (canonical_timestamp, id) + WHERE passage_id IS NULL; + +-- Preserve an existing legacy match as explicitly reviewable migration +-- evidence. Unmatched and pre-NTP rows are not guessed into passages here; +-- the former belongs to bounded shadow processing and the latter remains at +-- the historical-backfill approval gate. +INSERT INTO public.vehicle_passages ( + canonical_event_time, direction, selected_speed_mph, lifecycle_state, + review_state, correlation_profile_id, correlation_profile_version, + correlation_algorithm, legacy_radar_event_id +) +SELECT radar.canonical_timestamp, radar.direction, radar.speed_mph, + 'ambiguous', 'needs_review', profiles.id, profiles.version, + 'legacy-radar-read-migration-v1', radar.id +FROM public.radar_events radar +JOIN public.traffic_correlation_profiles profiles + ON profiles.profile_key = 'generic-radar-profile' AND profiles.version = 1 +WHERE radar.matched_read_id IS NOT NULL + AND radar.timestamp_quality <> 'historical_unsynced' +ON CONFLICT (legacy_radar_event_id) DO NOTHING; + +INSERT INTO public.camera_observations ( + source_key, camera_key, observation_kind, observation_timestamp, + received_at, direction_label, blue_iris_timeline_path, plate_read_id, + raw_payload, idempotency_key +) +SELECT 'legacy-plate-read', + COALESCE(NULLIF(BTRIM(reads.camera_name), ''), 'unconfigured-camera'), + 'lpr', reads."timestamp", reads.created_at, + COALESCE(direction.direction_label, reads.bi_trigger_direction_label), + reads.bi_alert_path, reads.id, + jsonb_build_object('migration', 'legacy-radar-read-v1'), + repeat(md5('legacy-radar-read:' || reads.id::text), 2) +FROM public.vehicle_passages passages +JOIN public.radar_events radar ON radar.id = passages.legacy_radar_event_id +JOIN public.plate_reads reads ON reads.id = radar.matched_read_id +LEFT JOIN LATERAL ( + SELECT observations.direction_label + FROM public.vehicle_direction_observations observations + WHERE observations.read_id = reads.id + ORDER BY observations.evaluated_at DESC, observations.read_id DESC + LIMIT 1 +) direction ON TRUE +WHERE passages.correlation_algorithm = 'legacy-radar-read-migration-v1' +ON CONFLICT (idempotency_key) DO NOTHING; + +INSERT INTO public.passage_observation_associations ( + passage_id, observation_type, radar_event_id, time_delta_ms, + total_score, confidence_class, algorithm, algorithm_revision, + correlation_profile_id, correlation_profile_version, + association_source, evidence +) +SELECT passages.id, 'radar', radar.id, 0, NULL, 'ambiguous', + 'legacy-radar-read-migration-v1', 1, + passages.correlation_profile_id, passages.correlation_profile_version, + 'migration', jsonb_build_object( + 'legacyMatchedReadId', radar.matched_read_id, + 'legacyMatchDeltaMs', radar.match_delta_ms, + 'requiresReview', TRUE + ) +FROM public.vehicle_passages passages +JOIN public.radar_events radar ON radar.id = passages.legacy_radar_event_id +ON CONFLICT DO NOTHING; + +INSERT INTO public.passage_observation_associations ( + passage_id, observation_type, camera_observation_id, time_delta_ms, + total_score, confidence_class, algorithm, algorithm_revision, + correlation_profile_id, correlation_profile_version, + association_source, evidence +) +SELECT passages.id, 'camera', camera.id, + ROUND(EXTRACT(EPOCH FROM ( + camera.observation_timestamp - passages.canonical_event_time + )) * 1000)::INTEGER, + NULL, 'ambiguous', 'legacy-radar-read-migration-v1', 1, + passages.correlation_profile_id, passages.correlation_profile_version, + 'migration', jsonb_build_object('requiresReview', TRUE) +FROM public.vehicle_passages passages +JOIN public.radar_events radar ON radar.id = passages.legacy_radar_event_id +JOIN public.camera_observations camera ON camera.plate_read_id = radar.matched_read_id +WHERE camera.idempotency_key = repeat(md5('legacy-radar-read:' || radar.matched_read_id::text), 2) +ON CONFLICT DO NOTHING; + +UPDATE public.radar_events radar +SET passage_id = passages.id +FROM public.vehicle_passages passages +WHERE passages.legacy_radar_event_id = radar.id + AND radar.passage_id IS DISTINCT FROM passages.id; + +CREATE TABLE IF NOT EXISTS public.radar_ingest_receipts ( + id BIGSERIAL PRIMARY KEY, + source_key VARCHAR(255) NOT NULL CHECK (NULLIF(BTRIM(source_key), '') IS NOT NULL), + topic VARCHAR(512) NOT NULL CHECK (NULLIF(BTRIM(topic), '') IS NOT NULL), + received_at TIMESTAMPTZ NOT NULL, + qos SMALLINT CHECK (qos IS NULL OR qos BETWEEN 0 AND 2), + retained BOOLEAN, + mqtt_duplicate BOOLEAN, + status VARCHAR(16) NOT NULL CHECK (status IN ('accepted','duplicate','rejected')), + rejection_reason VARCHAR(100), + payload_hash CHAR(64) NOT NULL CHECK (payload_hash ~ '^[0-9a-f]{64}$'), + idempotency_key CHAR(64) CHECK (idempotency_key IS NULL OR idempotency_key ~ '^[0-9a-f]{64}$'), + raw_payload_text TEXT NOT NULL, + raw_payload JSONB CHECK (raw_payload IS NULL OR jsonb_typeof(raw_payload) = 'object'), + radar_event_id BIGINT REFERENCES public.radar_events(id) ON DELETE SET NULL, + duplicate_of_event_id BIGINT REFERENCES public.radar_events(id) ON DELETE SET NULL, + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + CHECK ((status = 'rejected' AND rejection_reason IS NOT NULL) OR status <> 'rejected'), + CHECK ((status = 'duplicate' AND duplicate_of_event_id IS NOT NULL) OR status <> 'duplicate') +); + +CREATE INDEX IF NOT EXISTS idx_radar_ingest_receipts_recent + ON public.radar_ingest_receipts (received_at DESC, id DESC); +CREATE INDEX IF NOT EXISTS idx_radar_ingest_receipts_status + ON public.radar_ingest_receipts (status, received_at DESC, id DESC); +CREATE INDEX IF NOT EXISTS idx_radar_ingest_receipts_event + ON public.radar_ingest_receipts (radar_event_id, id) + WHERE radar_event_id IS NOT NULL; +CREATE INDEX IF NOT EXISTS idx_radar_ingest_receipts_duplicate_event + ON public.radar_ingest_receipts (duplicate_of_event_id, id) + WHERE duplicate_of_event_id IS NOT NULL; + +INSERT INTO public.schema_migrations(version,description) VALUES + ('2026082301_vehicle_passage_foundation','Retain the default-shadow vehicle-passage schema for compatibility without enabling a Community traffic interface.') +ON CONFLICT(version) DO NOTHING; diff --git a/migrations/0070_2026100501_portable_vehicle_image_sources.sql b/migrations/0070_2026100501_portable_vehicle_image_sources.sql new file mode 100644 index 0000000..2103ef4 --- /dev/null +++ b/migrations/0070_2026100501_portable_vehicle_image_sources.sql @@ -0,0 +1,17 @@ +-- migration: 2026100501_portable_vehicle_image_sources +-- Explicitly reviewed full-vehicle recording sources. Existing mappings retain +-- overview behavior; a same-camera source requires an explicit opt-in. +ALTER TABLE public.vehicle_overview_pair_profiles + ADD COLUMN IF NOT EXISTS source_mode VARCHAR(16) NOT NULL DEFAULT 'overview'; +ALTER TABLE public.vehicle_overview_pair_profiles + DROP CONSTRAINT IF EXISTS vehicle_overview_distinct_camera_check; +ALTER TABLE public.vehicle_overview_pair_profiles + ADD CONSTRAINT vehicle_overview_distinct_camera_check CHECK ( + (source_mode = 'overview' AND LOWER(BTRIM(source_camera_name)) <> LOWER(BTRIM(plate_camera_name))) + OR (source_mode = 'lpr_camera' AND source_role = 'primary' + AND LOWER(BTRIM(source_camera_name)) = LOWER(BTRIM(plate_camera_name)) + AND expected_delta_ms = 0 AND NULLIF(BTRIM(source_camera_short_name), '') IS NOT NULL) + ) NOT VALID; +INSERT INTO public.schema_migrations(version,description) VALUES + ('2026100501_portable_vehicle_image_sources','Record explicit whole-vehicle LPR recording sources alongside existing overview mappings.') +ON CONFLICT(version) DO NOTHING; diff --git a/migrations/0071_2026102201_manual_plate_state_country.sql b/migrations/0071_2026102201_manual_plate_state_country.sql new file mode 100644 index 0000000..a251c60 --- /dev/null +++ b/migrations/0071_2026102201_manual_plate_state_country.sql @@ -0,0 +1,9 @@ +-- migration: 2026102201_manual_plate_state_country +-- Manually entered metadata shared by reads of the same effective plate. +ALTER TABLE public.plates ADD COLUMN IF NOT EXISTS state_country VARCHAR(100); +CREATE INDEX IF NOT EXISTS idx_plates_state_country_options + ON public.plates (LOWER(state_country), state_country) + WHERE NULLIF(BTRIM(state_country), '') IS NOT NULL; +INSERT INTO public.schema_migrations(version, description) VALUES + ('2026102201_manual_plate_state_country', 'Add optional manually entered State/Country plate metadata.') +ON CONFLICT(version) DO NOTHING; diff --git a/migrations/0072_2026110101_axis_camera_inputs.sql b/migrations/0072_2026110101_axis_camera_inputs.sql new file mode 100644 index 0000000..1479ddb --- /dev/null +++ b/migrations/0072_2026110101_axis_camera_inputs.sql @@ -0,0 +1,63 @@ +-- migration: 2026110101_axis_camera_inputs +-- Configurable incoming camera sources. Existing Blue Iris inputs remain implicit. +CREATE TABLE IF NOT EXISTS public.camera_inputs ( + id UUID PRIMARY KEY, + camera_name VARCHAR(30) NOT NULL, + source VARCHAR(16) NOT NULL CHECK(source IN ('blue_iris','axis')), + transport VARCHAR(8) NOT NULL CHECK(transport IN ('http','mqtt','both')), + enabled BOOLEAN NOT NULL DEFAULT TRUE, + device_id VARCHAR(64), + broker_id INTEGER REFERENCES public.mqttbrokers(id) ON DELETE RESTRICT, + topic_filter VARCHAR(512), + qos INTEGER NOT NULL DEFAULT 1 CHECK(qos IN (0,1)), + processing_mode VARCHAR(16) NOT NULL DEFAULT 'immediate' CHECK(processing_mode IN ('immediate','final')), + store_images BOOLEAN NOT NULL DEFAULT TRUE, + roi_filter VARCHAR(30), + direction_in VARCHAR(80) NOT NULL DEFAULT 'In', direction_out VARCHAR(80) NOT NULL DEFAULT 'Out', + credential_hash CHAR(64), + connection_status VARCHAR(32) NOT NULL DEFAULT 'waiting', + last_received_at TIMESTAMPTZ, last_imported_at TIMESTAMPTZ, last_error TEXT, + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + CHECK(source <> 'axis' OR device_id IS NOT NULL), + CHECK(transport NOT IN ('mqtt','both') OR (broker_id IS NOT NULL AND topic_filter IS NOT NULL)) +); +CREATE UNIQUE INDEX IF NOT EXISTS uq_camera_inputs_name ON public.camera_inputs(LOWER(BTRIM(camera_name))); +CREATE UNIQUE INDEX IF NOT EXISTS uq_camera_inputs_device ON public.camera_inputs(device_id) WHERE source='axis'; +CREATE TABLE IF NOT EXISTS public.camera_input_events ( + id BIGSERIAL PRIMARY KEY, + input_id UUID NOT NULL REFERENCES public.camera_inputs(id) ON DELETE RESTRICT, + fingerprint CHAR(64) NOT NULL, + payload JSONB NOT NULL, + transport VARCHAR(8) NOT NULL CHECK(transport IN ('http','mqtt')), + status VARCHAR(16) NOT NULL DEFAULT 'pending' CHECK(status IN ('pending','processed','ignored')), + read_id INTEGER REFERENCES public.plate_reads(id) ON DELETE SET NULL, + received_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + processed_at TIMESTAMPTZ, + leased_until TIMESTAMPTZ, lease_token UUID, + revision INTEGER NOT NULL DEFAULT 0, + attempts INTEGER NOT NULL DEFAULT 0, next_attempt_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + UNIQUE(input_id,fingerprint) +); +CREATE INDEX IF NOT EXISTS idx_camera_input_pending ON public.camera_input_events(next_attempt_at,id) WHERE status='pending'; +CREATE INDEX IF NOT EXISTS idx_camera_input_event_retention ON public.camera_input_events(processed_at,id) WHERE status<>'pending'; +CREATE TABLE IF NOT EXISTS public.camera_input_passages ( + id UUID PRIMARY KEY, + input_id UUID NOT NULL REFERENCES public.camera_inputs(id) ON DELETE RESTRICT, + car_id VARCHAR(64) NOT NULL, + source_camera VARCHAR(64) NOT NULL, + first_capture TIMESTAMPTZ NOT NULL, last_capture TIMESTAMPTZ NOT NULL, + last_event_id BIGINT REFERENCES public.camera_input_events(id) ON DELETE SET NULL, + last_state VARCHAR(12) NOT NULL, + last_seen TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + read_id INTEGER REFERENCES public.plate_reads(id) ON DELETE SET NULL, + finalized BOOLEAN NOT NULL DEFAULT FALSE, + direction_label VARCHAR(80), scene_image_path VARCHAR(255), scene_thumbnail_path VARCHAR(255) +); +CREATE INDEX IF NOT EXISTS idx_camera_input_passage_lookup ON public.camera_input_passages(input_id,car_id,last_capture DESC); +ALTER TABLE public.camera_input_events ADD COLUMN IF NOT EXISTS passage_id UUID + REFERENCES public.camera_input_passages(id) ON DELETE SET NULL; +CREATE INDEX IF NOT EXISTS idx_camera_input_event_passage ON public.camera_input_events(passage_id); +INSERT INTO public.schema_migrations(version,description) VALUES + ('2026110101_axis_camera_inputs','Configurable Blue Iris and Axis HTTP/MQTT inputs with durable Axis receipts and passage correlation.') +ON CONFLICT(version) DO NOTHING; diff --git a/migrations/0073_2026110102_axis_ready_state.sql b/migrations/0073_2026110102_axis_ready_state.sql new file mode 100644 index 0000000..1db150a --- /dev/null +++ b/migrations/0073_2026110102_axis_ready_state.sql @@ -0,0 +1,9 @@ +-- migration: 2026110102_axis_ready_state +ALTER TABLE public.vehicle_direction_observations DROP CONSTRAINT IF EXISTS vehicle_direction_ready_state; +ALTER TABLE public.vehicle_direction_observations ADD CONSTRAINT vehicle_direction_ready_state CHECK ( + (status='ready' AND direction_label IS NOT NULL AND + ((classifier_version='axis-lpv-direction-v1' AND orientation IN ('front','rear','unknown')) OR + (orientation IN ('front','rear') AND orientation_confidence IS NOT NULL))) OR + (status<>'ready' AND orientation='unknown' AND direction_label IS NULL) +) NOT VALID; +INSERT INTO public.schema_migrations(version,description) VALUES ('2026110102_axis_ready_state','Preserve Axis observation state validation in the migration ledger.') ON CONFLICT(version) DO NOTHING; diff --git a/migrations/0074_2026110103_preserve_event_directions.sql b/migrations/0074_2026110103_preserve_event_directions.sql new file mode 100644 index 0000000..fe29aa5 --- /dev/null +++ b/migrations/0074_2026110103_preserve_event_directions.sql @@ -0,0 +1,13 @@ +-- migration: 2026110103_preserve_event_directions +-- Zone events have no image embedding. Their visibility is independent of crops. +CREATE OR REPLACE VIEW public.current_vehicle_direction_observations AS +SELECT observations.* FROM public.vehicle_direction_observations observations +WHERE observations.classifier_version IN ( + 'blue-iris-zone-crossing-v1', 'blue-iris-zone-crossing-v2-primary', 'axis-lpv-direction-v1' +) OR (observations.classifier_version = 'canonical-crop-orientation-knn-v2' + AND EXISTS (SELECT 1 FROM public.vehicle_direction_sources source + WHERE source.read_id = observations.read_id + AND source.embedding_id = observations.source_embedding_id)); +INSERT INTO public.schema_migrations(version, description) VALUES + ('2026110103_preserve_event_directions', 'Preserve Blue Iris and Axis events and expose current primary direction observations.') +ON CONFLICT(version) DO NOTHING; diff --git a/migrations/0075_2026110104_home_assistant_displays.sql b/migrations/0075_2026110104_home_assistant_displays.sql new file mode 100644 index 0000000..d3662d6 --- /dev/null +++ b/migrations/0075_2026110104_home_assistant_displays.sql @@ -0,0 +1,16 @@ +-- migration: 2026110104_home_assistant_displays +CREATE TABLE public.home_assistant_display_grants ( + id UUID PRIMARY KEY, + label VARCHAR(80) NOT NULL CHECK (BTRIM(label) <> ''), + token_hash CHAR(64) NOT NULL UNIQUE CHECK (token_hash ~ '^[0-9a-f]{64}$'), + allowed_origin TEXT NOT NULL, + camera_name VARCHAR(100), + created_by VARCHAR(100) NOT NULL, + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + expires_at TIMESTAMPTZ NOT NULL, + revoked_at TIMESTAMPTZ, + CHECK (expires_at > created_at) +); +INSERT INTO public.schema_migrations(version, description) VALUES + ('2026110104_home_assistant_displays', 'Revocable, camera-scoped read-only display credentials for Home Assistant.') +ON CONFLICT(version) DO NOTHING; diff --git a/migrations/0076_2026110105_explicit_lpr_recording_binding.sql b/migrations/0076_2026110105_explicit_lpr_recording_binding.sql new file mode 100644 index 0000000..62948cf --- /dev/null +++ b/migrations/0076_2026110105_explicit_lpr_recording_binding.sql @@ -0,0 +1,14 @@ +-- migration: 2026110105_explicit_lpr_recording_binding +-- An ALPR camera alias and its recording camera display name may differ. The +-- explicitly selected Blue Iris short ID identifies the recording source. +ALTER TABLE public.vehicle_overview_pair_profiles + DROP CONSTRAINT vehicle_overview_distinct_camera_check; +ALTER TABLE public.vehicle_overview_pair_profiles + ADD CONSTRAINT vehicle_overview_distinct_camera_check CHECK ( + (source_mode = 'overview' AND LOWER(BTRIM(source_camera_name)) <> LOWER(BTRIM(plate_camera_name))) + OR (source_mode = 'lpr_camera' AND source_role = 'primary' + AND expected_delta_ms = 0 AND NULLIF(BTRIM(source_camera_short_name), '') IS NOT NULL) + ) NOT VALID; +INSERT INTO public.schema_migrations(version,description) VALUES + ('2026110105_explicit_lpr_recording_binding', 'Bind ALPR camera aliases to an explicitly selected Blue Iris recording short ID.') +ON CONFLICT(version) DO NOTHING; diff --git a/migrations/README.md b/migrations/README.md new file mode 100644 index 0000000..c6a7a0f --- /dev/null +++ b/migrations/README.md @@ -0,0 +1,25 @@ +# Database migrations + +Add new numbered SQL files; never edit a migration after releasing it. Each file +starts with `-- migration: VERSION` and records that version in `schema_migrations`. +Run `node scripts/build-migrations.mjs` and commit the generated `migrations.sql`. +`node scripts/build-migrations.mjs --check` rejects an outdated bundle. + +The generated bundle is the runtime entry point on Windows, Docker and logical +imports. It contains every source, so older updaters still work without a bridge +release or extra files. Execute it in one transaction. An advisory transaction +lock serializes migrations. Completed versions are skipped and their SHA-256 +checksums are verified. A failure rolls back both schema work and ledger updates. + +On the first upgrade, existing legacy completion markers are adopted with their +source checksums, without replaying those migrations. The legacy native identity +marker is checked against its authority control before adoption. Unknown legacy +markers are retained. Historical migrations that were never completed still run; +the canonical transition only retires explicitly named image-based classifiers. +Blue Iris and Axis event observations are preserved. New corrections belong in +a new migration, including corrections to views created by older versions. + +The files extracted from the legacy bundle retain their original statements and +markers, except for event-direction visibility and the corrected retirement +predicate needed when upgrading installations predating canonical identity. Backups, count validation +and rollback remain the responsibility of the existing deployment updater. diff --git a/next.config.js b/next.config.js index 2581b08..02ad6be 100644 --- a/next.config.js +++ b/next.config.js @@ -14,7 +14,6 @@ const nextConfig = { source: "/:path*", headers: [ { key: "X-Content-Type-Options", value: "nosniff" }, - { key: "X-Frame-Options", value: "DENY" }, { key: "Referrer-Policy", value: "no-referrer" }, { key: "Permissions-Policy", @@ -22,6 +21,11 @@ const nextConfig = { }, ], }, + { + // Only the dedicated display supplies its own exact-origin CSP policy. + source: "/((?!embed/home-assistant/[0-9a-fA-F-]{36}$).*)", + headers: [{ key: "X-Frame-Options", value: "DENY" }], + }, ]; }, outputFileTracingIncludes: { diff --git a/package.json b/package.json index 618c30d..68ddcd5 100644 --- a/package.json +++ b/package.json @@ -1,13 +1,13 @@ { "name": "blueiris-alpr-database", - "version": "0.1.59", + "version": "0.1.60", "private": true, "engines": { "node": ">=24.0.0 <25" }, "scripts": { "dev": "next dev", - "build": "next build", + "build": "node scripts/build-migrations.mjs --check && next build", "package:windows": "node scripts/build-windows-native.mjs", "package:windows:setup": "node scripts/build-windows-setup.mjs", "package:windows:update": "node scripts/build-windows-update.mjs", @@ -31,7 +31,10 @@ "test:vehicle-image-asset:postgres": "node scripts/test-vehicle-image-asset-postgres.mjs", "test:vehicle-event-shadow:postgres": "node scripts/test-vehicle-event-shadow-postgres.mjs", "test:vehicle-image-crop:postgres": "node scripts/test-vehicle-image-crop-postgres.mjs", - "typecheck": "tsc --noEmit" + "typecheck": "tsc --noEmit", + "migrations:build": "node scripts/build-migrations.mjs", + "migrations:check": "node scripts/build-migrations.mjs --check", + "test:community-bugfixes:postgres": "node scripts/test-community-bugfixes-postgres.mjs" }, "dependencies": { "@radix-ui/react-accordion": "^1.2.1", diff --git a/scripts/build-migrations.mjs b/scripts/build-migrations.mjs new file mode 100644 index 0000000..f659120 --- /dev/null +++ b/scripts/build-migrations.mjs @@ -0,0 +1,85 @@ +import { createHash } from "node:crypto"; +import { readFile, readdir, writeFile } from "node:fs/promises"; +import { fileURLToPath } from "node:url"; +import path from "node:path"; + +const root = fileURLToPath(new URL("../", import.meta.url)); +export const migrationHash = sql => createHash("sha256").update(sql.replace(/\r\n/g, "\n")).digest("hex"); + +export async function compileMigrations(directory = path.join(root, "migrations")) { + const files = (await readdir(directory)).filter(name => /^\d{4}_[a-z0-9_]+\.sql$/.test(name)).sort(); + if (!files.length) throw new Error("No migration sources found"); + const seen = new Set(); + const units = []; + for (const file of files) { + const sql = (await readFile(path.join(directory, file), "utf8")).replace(/\r\n/g, "\n"); + const version = sql.match(/^-- migration: ([a-z0-9_]+)$/m)?.[1]; + if (!version || seen.has(version)) throw new Error("Missing or duplicate migration version: " + file); + seen.add(version); + const hash = migrationHash(sql); + const quoted = "$source_" + hash.slice(0, 16) + "$"; + if (sql.includes(quoted)) throw new Error("Migration delimiter collision"); + units.push({ file, version, hash, sql, quoted }); + } + const prelude = `-- GENERATED by node scripts/build-migrations.mjs. Edit only new migration sources. +-- The single-file, pure-SQL entry point also supports older Windows updaters, +-- Docker's psql runner, and logical imports. Callers must use one transaction. +SELECT pg_advisory_xact_lock(6839271042261); +CREATE TABLE IF NOT EXISTS public.schema_migrations ( + version VARCHAR(100) PRIMARY KEY, + description TEXT NOT NULL, + applied_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP +); +ALTER TABLE public.schema_migrations ADD COLUMN IF NOT EXISTS sql_sha256 CHAR(64); +-- Legacy markers were committed by the previous transactional SQL runner. +-- Validate its identity authority before adopting markers as the baseline. +DO $baseline$ +BEGIN + IF EXISTS (SELECT 1 FROM public.schema_migrations WHERE version = '2026092701_native_reid') THEN + IF to_regclass('public.vehicle_reid_control') IS NULL + OR to_regclass('public.vehicle_direction_observations') IS NULL THEN + RAISE EXCEPTION 'Legacy migration baseline is incomplete'; + END IF; + IF NOT EXISTS (SELECT 1 FROM public.vehicle_reid_control WHERE singleton AND mode = 'v2_primary') THEN + RAISE EXCEPTION 'Legacy identity authority does not match its migration marker'; + END IF; + END IF; +END; +$baseline$; +`; + const body = units.map(unit => ` +-- ${unit.file} +DO $migration$ +DECLARE recorded_hash TEXT; +BEGIN + SELECT sql_sha256 INTO recorded_hash FROM public.schema_migrations WHERE version = '${unit.version}'; + IF FOUND THEN + IF recorded_hash IS NOT NULL AND recorded_hash <> '${unit.hash}' THEN + RAISE EXCEPTION 'Applied migration checksum differs: ${unit.version}'; + END IF; + IF recorded_hash IS NULL THEN + UPDATE public.schema_migrations SET sql_sha256 = '${unit.hash}' WHERE version = '${unit.version}'; + END IF; + ELSE + EXECUTE ${unit.quoted}${unit.sql}${unit.quoted}; + IF NOT EXISTS (SELECT 1 FROM public.schema_migrations WHERE version = '${unit.version}') THEN + RAISE EXCEPTION 'Migration did not record completion: ${unit.version}'; + END IF; + UPDATE public.schema_migrations SET sql_sha256 = '${unit.hash}' WHERE version = '${unit.version}'; + END IF; +END; +$migration$; +`).join(""); + return { sql: prelude + body, manifest: units.map(({ file, version, hash }) => ({ file, version, sha256: hash })) }; +} + +if (process.argv[1] && path.resolve(process.argv[1]) === fileURLToPath(import.meta.url)) { + const { sql, manifest } = await compileMigrations(); + if (process.argv.includes("--check")) { + if ((await readFile(path.join(root, "migrations.sql"), "utf8")).replace(/\r\n/g, "\n") !== sql) + throw new Error("migrations.sql is stale; run node scripts/build-migrations.mjs"); + } else { + await writeFile(path.join(root, "migrations.sql"), sql); + } + console.log(`Verified ${manifest.length} ordered, checksummed migrations`); +} diff --git a/scripts/native-reid-upgrade-policy.mjs b/scripts/native-reid-upgrade-policy.mjs index 8c6d1d3..e5c66b0 100644 --- a/scripts/native-reid-upgrade-policy.mjs +++ b/scripts/native-reid-upgrade-policy.mjs @@ -22,7 +22,7 @@ export const RETIRED_IDENTITY_TABLES = Object.freeze([ ]); export const RETIRED_DIRECTION_PREDICATES = Object.freeze({ vehicle_orientation_labels: "TRUE", - vehicle_direction_observations: "classifier_version <> 'blue-iris-zone-crossing-v1'", + vehicle_direction_observations: "classifier_version IN ('vehicle-reid-orientation-knn-v1','canonical-crop-orientation-knn-v2')", }); export function compareMinimumCounts(sourceCounts, targetCounts) { const losses = []; diff --git a/scripts/test-community-browser.mjs b/scripts/test-community-browser.mjs new file mode 100644 index 0000000..85fdbb0 --- /dev/null +++ b/scripts/test-community-browser.mjs @@ -0,0 +1,156 @@ +import assert from "node:assert/strict"; +import { mkdtemp, mkdir, writeFile, rm } from "node:fs/promises"; +import { existsSync } from "node:fs"; +import { pathToFileURL, fileURLToPath } from "node:url"; +import { spawn } from "node:child_process"; +import { randomBytes } from "node:crypto"; +import { once } from "node:events"; +import http from "node:http"; +import os from "node:os"; +import path from "node:path"; +import sharp from "sharp"; +import { HomeAssistantDisplayService } from "../lib/home-assistant-display.mjs"; + +const root = fileURLToPath(new URL("../", import.meta.url)); +const delay = ms=>new Promise(resolve=>setTimeout(resolve,ms)); +export async function exerciseBrowser(config) { + const modulePath = process.env.ALPR_TEST_PLAYWRIGHT_MODULE; + if (!modulePath || !path.isAbsolute(modulePath)) throw new Error("Set ALPR_TEST_PLAYWRIGHT_MODULE to an installed playwright-core module."); + const { chromium } = await import(pathToFileURL(modulePath).href); + const executablePath = process.env.ALPR_TEST_BROWSER || "C:\\Program Files (x86)\\Microsoft\\Edge\\Application\\msedge.exe"; + assert.ok(existsSync(executablePath)); + const home = await mkdtemp(path.join(os.tmpdir(),"alpr-community-browser-")); + assert.equal(path.dirname(home),os.tmpdir()); + const password = randomBytes(24).toString("hex"); + let source = "", browser, child; + const parent = http.createServer((_req,res)=>{res.setHeader("Content-Type","text/html");res.end(``);}); + const blockedParent = http.createServer((_req,res)=>{res.setHeader("Content-Type","text/html");res.end(``);}); + const blueIris = http.createServer(async (request,response)=>{ + if(request.method!=="POST"){response.writeHead(404);response.end();return;} + let text="";for await(const chunk of request)text+=chunk; + const command=JSON.parse(text || "{}"); + let result={result:"fail"}; + if(command.cmd==="login")result=command.response + ? {result:"success",session:"fixture-session",data:{systemName:"Synthetic Blue Iris",version:"6.1"}} + : {result:"fail",session:"fixture-challenge"}; + if(command.cmd==="camlist")result={result:"success",data:[{optionValue:"eastlpr",optionDisplay:"East LPR",isEnabled:true,isOnline:true}]}; + if(command.cmd==="alertlist")result={result:"success",data:[{camera:"eastlpr",date:Math.floor(config.read.timestamp.getTime()/1000),clip:"@fixture.bvr",offset:0}]}; + response.setHeader("Content-Type","application/json");response.end(JSON.stringify(result)); + }); + const placeholder = http.createServer();placeholder.listen(0,"127.0.0.1");await once(placeholder,"listening"); + const port=placeholder.address().port;await new Promise(resolve=>placeholder.close(resolve)); + parent.listen(0,"127.0.0.1");blockedParent.listen(0,"127.0.0.1");blueIris.listen(0,"127.0.0.1"); + await Promise.all([once(parent,"listening"),once(blockedParent,"listening"),once(blueIris,"listening")]); + const origin=`http://127.0.0.1:${port}`, parentOrigin=`http://127.0.0.1:${parent.address().port}`; + const logs=[]; + try { + await mkdir(path.join(home,"storage/images"),{recursive:true}); + await mkdir(path.join(home,"config"),{recursive:true}); + await writeFile(path.join(home,"config/settings.yaml"),JSON.stringify({blueiris:{host:`http://127.0.0.1:${blueIris.address().port}`,username:"fixture",password:"fixture-password"}})); + await config.pool.query(`INSERT INTO public.camera_direction_profiles(camera_key,camera_name,enabled,front_direction_label,rear_direction_label) + VALUES ('eastlpr','eastlpr',false,'Westbound','Eastbound') ON CONFLICT(camera_key) DO UPDATE SET front_direction_label='Westbound',rear_direction_label='Eastbound'`); + await writeFile(path.join(home,"storage/images/fixture.jpg"),await sharp({create:{width:320,height:180,channels:3,background:"#315a85"}}).jpeg().toBuffer()); + child=spawn(process.execPath,[path.join(root,"node_modules/next/dist/bin/next"),"start","-p",String(port),"-H","127.0.0.1"],{ + cwd:root,windowsHide:true,stdio:["ignore","pipe","pipe"], + env:{...process.env,NODE_ENV:"production",NEXT_TELEMETRY_DISABLED:"1",PORT:String(port),HOSTNAME:"127.0.0.1",ALPR_DATA_DIR:home, + DB_HOST:`127.0.0.1:${config.port}`,DB_NAME:config.database,DB_USER:config.user,DB_PASSWORD:config.password,ADMIN_PASSWORD:password, + ALPR_AUTH_FILE_PATH:path.join(home,"auth/auth.json"),ALPR_CONFIG_FILE_PATH:path.join(home,"config/settings.yaml"),MQTT_BROKER:"",PUSHOVER_ENABLED:"false", + ALPR_INTERNAL_ORIGIN:origin,BLUEIRIS_HOST:"",BLUEIRIS_USERNAME:"",BLUEIRIS_PASSWORD:""}, + }); + child.stdout.on("data",chunk=>logs.push(String(chunk)));child.stderr.on("data",chunk=>logs.push(String(chunk))); + let ready=false; + for(let i=0;i<120;i++){ + if(child.exitCode!==null)throw new Error("Test application exited before readiness"); + try{if((await fetch(origin+"/api/health-check")).ok){ready=true;break;}}catch{} + await delay(250); + } + assert.ok(ready,"production app health check"); + const service=new HomeAssistantDisplayService(config.pool); + const grant=await service.create({label:"Browser fixture display",allowedOrigin:parentOrigin,cameraName:"eastlpr",days:1},{username:"fixture"}); + source=origin+`/embed/home-assistant/${grant.id}#token=${grant.token}`; + const shell=await fetch(origin+`/embed/home-assistant/${grant.id}`); + assert.equal(shell.status,200); + assert.notEqual(shell.headers.get("x-frame-options"),"DENY"); + assert.ok(shell.headers.get("content-security-policy").includes(`frame-ancestors ${parentOrigin};`)); + assert.equal((await fetch(origin+`/api/home-assistant/display/${grant.id}`)).status,401); + assert.equal((await fetch(origin+"/api/current-access",{headers:{Authorization:"Bearer "+grant.token}})).status,401); + const normal=await fetch(origin+"/login");assert.equal(normal.headers.get("x-frame-options"),"DENY"); + browser=await chromium.launch({executablePath,headless:true}); + const page=await browser.newPage({viewport:{width:1100,height:900}}); + const requests=[];page.on("request",request=>requests.push(request.url())); + await page.goto(parentOrigin); + const frame=page.frameLocator('iframe'); + await frame.getByRole("heading",{name:"Browser fixture display"}).waitFor(); + await frame.getByRole("heading",{name:"TST5701",exact:true}).waitFor(); + assert.equal(await frame.getByRole("heading",{name:"TST5702",exact:true}).count(),0); + await frame.locator("img").first().waitFor(); + assert.ok(requests.every(url=>!url.includes(grant.token)),"fragment credential is absent from HTTP request URLs"); + assert.equal((await page.context().cookies()).some(cookie=>cookie.name==='session'),false); + await page.screenshot({path:path.join(home,"home-assistant-display.png"),fullPage:true}); + const blocked=await browser.newPage();let policyBlocked=false; + blocked.on("console",message=>{if(/frame-ancestors|Refused to frame/i.test(message.text()))policyBlocked=true;}); + await blocked.goto(`http://127.0.0.1:${blockedParent.address().port}`); + for(let i=0;i<20&&!policyBlocked;i++)await delay(100); + assert.ok(policyBlocked,"an unapproved parent origin is blocked by the browser"); + await service.revoke(grant.id); + await frame.getByRole("button",{name:"Refresh",exact:true}).click(); + await frame.getByRole("status").filter({hasText:"expired or was revoked"}).waitFor(); + assert.equal(await frame.locator("article").count(),0); + assert.equal((await fetch(origin+`/api/home-assistant/display/${grant.id}/image/${config.read.id}`,{headers:{Authorization:"Bearer "+grant.token}})).status,401); + + // Exercise the actual administrator settings and server actions as well. + const admin=await browser.newPage();await admin.goto(origin+"/login"); + await admin.getByLabel("Password",{exact:true}).fill(password); + await admin.getByRole("button",{name:"Sign in",exact:true}).click(); + await admin.waitForURL(url=>url.pathname!=="/login"); + await admin.goto(origin+"/settings/home-assistant"); + await admin.getByLabel("Home Assistant origin",{exact:true}).fill(parentOrigin); + await admin.getByLabel("Display name",{exact:true}).fill("Created through settings"); + await admin.getByRole("button",{name:"Create read-only display link",exact:true}).click(); + const link=admin.getByLabel("Copy this link into a Home Assistant webpage card",{exact:true}); + await link.waitFor();assert.ok((await link.inputValue()).includes("#token=")); + const active=await config.pool.query("SELECT id FROM public.home_assistant_display_grants WHERE label='Created through settings' AND revoked_at IS NULL"); + assert.equal(active.rows.length,1); + const row=admin.locator("div.rounded-md.border.p-4").filter({hasText:"Created through settings"}).last(); + await row.getByRole("button",{name:"Revoke",exact:true}).click(); + await row.getByText("Revoked",{exact:true}).waitFor(); + assert.ok((await config.pool.query("SELECT revoked_at FROM public.home_assistant_display_grants WHERE id=$1",[active.rows[0].id])).rows[0].revoked_at); + await admin.goto(origin+"/settings/blue-iris"); + await admin.getByRole("button",{name:"Test connection",exact:true}).click(); + await admin.getByText(/Found 1 cameras/).waitFor(); + const inventory=await config.pool.query("SELECT display_name FROM public.blue_iris_camera_inventory WHERE short_name='eastlpr' AND present"); + assert.equal(inventory.rows[0].display_name,"East LPR"); + // datetime-local deliberately uses the browser's local timezone, like the product. + const localTime=await admin.evaluate(timestamp=>{const date=new Date(timestamp);const pad=value=>String(value).padStart(2,'0');return date.getFullYear()+'-'+pad(date.getMonth()+1)+'-'+pad(date.getDate())+'T'+pad(date.getHours())+':'+pad(date.getMinutes())+':'+pad(date.getSeconds());},config.read.timestamp.toISOString()); + await admin.getByLabel("Plate-read local time",{exact:true}).fill(localTime); + await admin.getByRole("button",{name:"Find matching alert",exact:true}).click(); + const owner=admin.getByLabel("ALPR read that will own this image",{exact:true});await owner.waitFor(); + assert.equal(await owner.inputValue(),"","ambiguous reads are not selected automatically"); + await owner.selectOption(String(config.read.id)); + await admin.getByRole("button",{name:"Select best vehicle frame",exact:true}).click(); + let selectedStatus; + for(let i=0;i<120;i++){ + selectedStatus=(await config.pool.query("SELECT vehicle_image_status FROM public.plate_reads WHERE id=$1",[config.read.id])).rows[0].vehicle_image_status; + if(["failed","unavailable"].includes(selectedStatus))break; + await delay(250); + } + assert.ok(["failed","unavailable"].includes(selectedStatus),"the explicit read owns the recording request, even though the synthetic server has no video"); + assert.equal(await admin.getByText("No ALPR read from this camera matched the selected time closely enough to own the vehicle image.",{exact:true}).count(),0); + await admin.goto(origin+"/settings/vehicle-intelligence"); + await admin.getByRole("combobox").first().click();await admin.getByRole("option",{name:"eastlpr",exact:true}).click(); + const save=admin.getByRole("button",{name:"Save image source for Westbound",exact:true});await save.waitFor(); + await save.click();await admin.getByText("Saved for Westbound. New eligible plate reads will use this source.",{exact:true}).waitFor(); + const mapping=await config.pool.query("SELECT source_camera_name,source_camera_short_name FROM public.vehicle_overview_pair_profiles WHERE plate_camera_name='eastlpr' AND direction_label='Westbound' AND source_mode='lpr_camera'"); + assert.deepEqual(mapping.rows,[{source_camera_name:"East LPR",source_camera_short_name:"eastlpr"}]); + console.log("Production browser: iframe policy, read-only scope, revocation, administrator display UI, Blue Iris connection/inventory, explicit read selection and LPR source save passed"); + } catch(error) { + await writeFile(path.join(home,"server.log"),logs.join("")); + console.error("Browser fixture diagnostic directory: "+home); + throw error; + } finally { + await browser?.close(); + if(child?.exitCode===null){child.kill();await Promise.race([once(child,"exit"),delay(5000)]);} + await Promise.all([new Promise(resolve=>parent.close(resolve)),new Promise(resolve=>blockedParent.close(resolve)),new Promise(resolve=>blueIris.close(resolve))]); + // Keep screenshots and logs for local QA. The directory contains synthetic data only. + } +} diff --git a/scripts/test-community-bugfixes-local.mjs b/scripts/test-community-bugfixes-local.mjs new file mode 100644 index 0000000..42156b9 --- /dev/null +++ b/scripts/test-community-bugfixes-local.mjs @@ -0,0 +1,52 @@ +import assert from "node:assert/strict"; +import { randomBytes } from "node:crypto"; +import { spawnSync } from "node:child_process"; +import { mkdtemp, writeFile, rm } from "node:fs/promises"; +import os from "node:os"; +import path from "node:path"; +import net from "node:net"; +import { once } from "node:events"; +import { exerciseCommunityBugfixes } from "./test-community-bugfixes-postgres.mjs"; + +const bin = path.resolve(process.argv[2] || "invalid"); +const root = await mkdtemp(path.join(os.tmpdir(),"alpr-community-bugfix-")); +assert.equal(path.dirname(root),os.tmpdir()); +const data = path.join(root,"postgres"); +const secret = randomBytes(32).toString("hex"); +const executable = name => path.join(bin,name + (process.platform === "win32" ? ".exe" : "")); +function run(name,args,options={}) { + const result=spawnSync(executable(name),args,{encoding:"utf8",windowsHide:true,...options}); + if(result.status!==0)throw new Error(`${name} failed: ${result.stderr || result.error || ""}`); +} +const listener=net.createServer();listener.listen(0,"127.0.0.1");await once(listener,"listening"); +const port=listener.address().port;await new Promise(resolve=>listener.close(resolve)); +let started=false; +try { + await writeFile(path.join(root,"password"),secret,{mode:0o600}); + run("initdb",["-D",data,"-U","postgres","--encoding=UTF8","--locale=C","--auth=scram-sha-256","--pwfile="+path.join(root,"password")]); + run("pg_ctl",["-D",data,"-l",path.join(root,"postgres.log"),"-o",`-h 127.0.0.1 -p ${port}`,"-w","start"],{stdio:"ignore"}); + started=true; + let browserTest; + if (process.argv.includes("--browser")) browserTest=(await import("./test-community-browser.mjs")).exerciseBrowser; + if (process.argv.includes("--overview-export")) { + const previous = browserTest; + browserTest = async config => { + await config.pool.query("CREATE TABLE public.codex_integration_test_guard(scope TEXT PRIMARY KEY,guard_token TEXT NOT NULL)"); + await config.pool.query("INSERT INTO public.codex_integration_test_guard VALUES ('overview-export:v1','local-disposable-pg17')"); + const url = `postgresql://postgres:${secret}@127.0.0.1:${port}/${config.database}`; + const result=spawnSync(process.execPath,[path.join(import.meta.dirname,"test-overview-export-postgres.mjs")],{ + windowsHide:true,encoding:"utf8",env:{...process.env,DATABASE_URL:url, + OVERVIEW_EXPORT_POSTGRES_TEST_OPT_IN:"true",OVERVIEW_EXPORT_POSTGRES_TEST_DATABASE:config.database, + OVERVIEW_EXPORT_POSTGRES_TEST_GUARD_TOKEN:"local-disposable-pg17"}, + }); + if(result.status!==0)throw new Error(result.stderr || result.error || "Overview fixture failed"); + console.log(result.stdout.trim()); + await previous?.(config); + }; + } + await exerciseCommunityBugfixes({host:"127.0.0.1",port,user:"postgres",password:secret},{browserTest}); +} finally { + if(started)run("pg_ctl",["-D",data,"-w","-m","fast","stop"],{stdio:"ignore"}); + assert.equal(path.dirname(root),os.tmpdir());assert.ok(path.basename(root).startsWith("alpr-community-bugfix-")); + await rm(root,{recursive:true,force:true,maxRetries:8,retryDelay:300}); +} diff --git a/scripts/test-community-bugfixes-postgres.mjs b/scripts/test-community-bugfixes-postgres.mjs new file mode 100644 index 0000000..f1c7707 --- /dev/null +++ b/scripts/test-community-bugfixes-postgres.mjs @@ -0,0 +1,137 @@ +import assert from "node:assert/strict"; +import { readFile } from "node:fs/promises"; +import { execFileSync } from "node:child_process"; +import { randomBytes } from "node:crypto"; +import { fileURLToPath } from "node:url"; +import path from "node:path"; +import pg from "pg"; +import { compileMigrations } from "./build-migrations.mjs"; +import { BlueIrisVehicleFrameRepository } from "../lib/blue-iris-vehicle-frame-repository.mjs"; +import { HomeAssistantDisplayService, createDisplayReadHandler } from "../lib/home-assistant-display.mjs"; + +const root = fileURLToPath(new URL("../", import.meta.url)); +const releaseFile = (tag, name) => execFileSync("git", ["show", `${tag}:${name}`], { cwd: root, encoding: "utf8", maxBuffer: 4 * 1024 * 1024, windowsHide: true }); +async function transaction(pool, sql) { + const client = await pool.connect(); + try { await client.query("BEGIN"); await client.query(sql); await client.query("COMMIT"); } + catch (error) { await client.query("ROLLBACK"); throw error; } + finally { client.release(); } +} +function assertPreservedRows(actual, before, message) { + assert.equal(actual.length,before.length,message); + for (let i=0;i[key,actual[i].value[key]])); + assert.deepEqual(existing,before[i].value,message); + } +} + +export async function exerciseCommunityBugfixes(server, { browserTest } = {}) { + assert.ok(["127.0.0.1", "localhost", "::1"].includes(server.host), "tests require a loopback PostgreSQL server"); + const admin = new pg.Pool({ ...server, database: "postgres", max: 1 }); + const databases = []; + const { sql, manifest } = await compileMigrations(); + const known = new Set(manifest.map(unit=>unit.version)); + try { + for (const tag of ["v0.1.42", "v0.1.55", "v0.1.58", "v0.1.59", "fresh"]) { + const database = "codex_community_bugfix_" + randomBytes(8).toString("hex"); + assert.match(database, /^codex_community_bugfix_[0-9a-f]{16}$/); + await admin.query(`CREATE DATABASE "${database}"`); + databases.push(database); + const pool = new pg.Pool({ ...server, database, max: 3 }); + try { + await pool.query(tag === "fresh" ? await readFile(new URL("../schema.sql", import.meta.url), "utf8") : releaseFile(tag,"schema.sql")); + await pool.query("SET search_path TO public"); + if (tag !== "fresh") await transaction(pool, releaseFile(tag,"migrations.sql")); + if (tag === "fresh") await transaction(pool, sql); + await pool.query(`INSERT INTO public.plate_reads(plate_number,camera_name,timestamp,image_path) + VALUES ('TST5701','eastlpr',CURRENT_TIMESTAMP,'images/fixture.jpg'), + ('TST5702','Other Camera',CURRENT_TIMESTAMP,'images/other.jpg')`); + const read = (await pool.query("SELECT * FROM public.plate_reads WHERE plate_number='TST5701'")).rows[0]; + await pool.query(`INSERT INTO public.vehicle_direction_observations + (read_id,camera_key,embedding_model,classifier_version,profile_version,status,orientation,orientation_confidence,direction_label) + VALUES ($1,'eastlpr','blue-iris-zone-crossing','blue-iris-zone-crossing-v2-primary',1,'ready','front',1,'Westbound')`, [read.id]); + const snapshot = (await pool.query("SELECT row_to_json(observation) AS value FROM public.vehicle_direction_observations observation ORDER BY read_id")).rows; + const originalRead = (await pool.query("SELECT row_to_json(reads) AS value FROM public.plate_reads reads ORDER BY id")).rows; + await transaction(pool, sql); + assertPreservedRows((await pool.query("SELECT row_to_json(observation) AS value FROM public.vehicle_direction_observations observation ORDER BY read_id")).rows, snapshot, tag + " preserves complete v2 observations"); + assert.equal((await pool.query("SELECT classifier_version FROM public.current_vehicle_direction_observations WHERE read_id=$1",[read.id])).rows[0].classifier_version,"blue-iris-zone-crossing-v2-primary"); + assertPreservedRows((await pool.query("SELECT row_to_json(reads) AS value FROM public.plate_reads reads ORDER BY id")).rows,originalRead, tag + " preserves complete plate reads"); + const ledger = (await pool.query("SELECT version,sql_sha256,applied_at FROM public.schema_migrations ORDER BY version")).rows; + assert.ok(ledger.filter(row=>known.has(row.version)).every(row => /^[0-9a-f]{64}$/.test(row.sql_sha256 || ""))); + // A deliberately invalid retained label must survive replay of the old cleanup. + await pool.query(`INSERT INTO public.vehicle_orientation_labels(read_id,camera_key,embedding_model,orientation,actor_username,actor_display_name) + VALUES ($1,'eastlpr','fixture','front','fixture','Fixture')`,[read.id]); + await transaction(pool, sql); + assert.equal((await pool.query("SELECT count(*)::int AS count FROM public.vehicle_orientation_labels WHERE read_id=$1",[read.id])).rows[0].count,1); + await Promise.all([transaction(pool,sql),transaction(pool,sql)]); + assert.deepEqual((await pool.query("SELECT version,sql_sha256,applied_at FROM public.schema_migrations ORDER BY version")).rows,ledger); + await pool.query("UPDATE public.schema_migrations SET sql_sha256=repeat('0',64) WHERE version='2026110103_preserve_event_directions'"); + await assert.rejects(transaction(pool,sql),/Applied migration checksum differs/); + await pool.query("UPDATE public.schema_migrations SET sql_sha256=$1 WHERE version='2026110103_preserve_event_directions'",[ledger.find(row=>row.version==='2026110103_preserve_event_directions').sql_sha256]); + await assert.rejects(transaction(pool, sql + "; INSERT INTO public.schema_migrations(version,description) VALUES ('test_pending_migration','transaction rollback fixture'); CREATE TABLE public.deliberate_failure_fixture(id int); SELECT 1/0;"),/division by zero/); + assert.equal((await pool.query("SELECT to_regclass('public.deliberate_failure_fixture') AS relation")).rows[0].relation,null); + assert.equal((await pool.query("SELECT 1 FROM public.schema_migrations WHERE version='test_pending_migration'")).rows.length,0); + await pool.query("ALTER TABLE public.vehicle_reid_control RENAME TO fixture_authority_control"); + await assert.rejects(transaction(pool,sql),/Legacy migration baseline is incomplete/); + await pool.query("ALTER TABLE public.fixture_authority_control RENAME TO vehicle_reid_control"); + + const repository = new BlueIrisVehicleFrameRepository(pool); + await repository.syncBlueIrisCameraInventory([{id:"eastlpr",name:"East LPR",enabled:true}]); + const input = {camera:"eastlpr",cameraName:"East LPR",timestamp:read.timestamp,toleranceSeconds:3}; + assert.equal((await repository.findNearestRead(input)).id,read.id,"short-name ALPR read matches the selected display-name camera"); + await repository.syncBlueIrisCameraInventory([{id:"eastlpr",name:"East LPR",enabled:true},{id:"collision",name:"eastlpr",enabled:true}]); + assert.equal(await repository.findNearestRead(input),null,"cross-colliding short/display aliases cannot silently select an owner"); + await repository.syncBlueIrisCameraInventory([{id:"eastlpr",name:"East LPR",enabled:true}]); + assert.equal((await repository.findNearestRead({...input,timestamp:new Date(read.timestamp.getTime()+5000)})),null); + await pool.query("INSERT INTO public.plate_reads(plate_number,camera_name,timestamp) VALUES ('TST5703','eastlpr',$1)",[read.timestamp]); + assert.equal(await repository.findNearestRead(input),null,"multiple candidate owners require an explicit choice"); + assert.equal((await repository.findNearestRead({...input,readId:read.id})).id,read.id); + const other = (await pool.query("SELECT id FROM public.plate_reads WHERE camera_name='Other Camera' LIMIT 1")).rows[0]; + assert.equal(await repository.findNearestRead({...input,readId:other.id}),null,"an explicit ID cannot cross camera bindings"); + const profile=await repository.saveOverviewPairProfile({sourceMode:"lpr_camera",sourceCameraName:"East LPR",sourceCameraShortName:"eastlpr", + plateCameraName:"Logical ALPR",directionLabel:"Westbound",sourceRole:"primary",overviewContext:"street",expectedDeltaMs:0,toleranceMs:1500,priority:0,enabled:true}); + const queued=(await pool.query(`INSERT INTO public.plate_reads(plate_number,camera_name,timestamp,bi_trigger_direction_status,bi_trigger_direction_label, + bi_trigger_direction_profile_version,bi_trigger_direction_algorithm,vehicle_image_queue_kind,vehicle_image_status,vehicle_image_updated_at) + VALUES ('TST5704','Logical ALPR',CURRENT_TIMESTAMP-INTERVAL '30 seconds','ready','Westbound',1,'blue-iris-zone-crossing-v2-primary','overview','pending',CURRENT_TIMESTAMP) RETURNING id`)).rows[0]; + const claim=await repository.claimNextOverviewRead(); + assert.equal(claim.id,queued.id,"an LPR alias with a distinct recording display name can enter the automatic queue"); + assert.equal(claim.overview_source_mode,"lpr_camera");assert.equal(claim.overview_source_camera_short_name,"eastlpr"); + await pool.query("DELETE FROM public.plate_reads WHERE id=$1",[queued.id]); + await pool.query("DELETE FROM public.vehicle_overview_pair_profiles WHERE id=$1",[profile.id]); + + const displays = new HomeAssistantDisplayService(pool); + const grant = await displays.create({label:"Fixture display",allowedOrigin:"http://127.0.0.1:8123",cameraName:"eastlpr",days:1},{username:"fixture"}); + const stored = (await pool.query("SELECT * FROM public.home_assistant_display_grants WHERE id=$1",[grant.id])).rows[0]; + assert.notEqual(stored.token_hash,grant.token); + assert.ok(!(JSON.stringify(await displays.list())).includes(grant.token)); + const auth = await displays.authorize(grant.id,"Bearer "+grant.token); + assert.ok(auth); + const visible = await displays.reads(auth); + assert.ok(visible.reads.length > 0 && visible.reads.every(row=>row.camera_name==='eastlpr')); + assert.equal(await displays.imagePath(auth,other.id),null); + const handler = createDisplayReadHandler({service:displays,readImage:async()=>Buffer.from("fixture-image")}); + assert.equal((await handler(new Request("http://fixture",{headers:{authorization:"Bearer "+grant.token}}),{params:{grantId:grant.id,readId:read.id}})).status,200); + await displays.revoke(grant.id); + assert.equal(await displays.authorize(grant.id,"Bearer "+grant.token),null); + assert.equal(await displays.publicGrant(grant.id),null); + const expired = await displays.create({label:"Expired fixture",allowedOrigin:"http://127.0.0.1:8123",days:1},{username:"fixture"}); + await pool.query("UPDATE public.home_assistant_display_grants SET created_at=CURRENT_TIMESTAMP-INTERVAL '2 days',expires_at=CURRENT_TIMESTAMP-INTERVAL '1 day' WHERE id=$1",[expired.id]); + assert.equal(await displays.authorize(expired.id,"Bearer "+expired.token),null); + if (tag === "fresh" && browserTest) await browserTest({ ...server, database, pool, read }); + console.log(`${tag}: upgrade, preservation, visibility, replay, checksum rejection, atomic failure, camera binding, ambiguity, display scope, expiration and revocation passed`); + } finally { await pool.end(); } + } + } finally { + for (const database of databases.reverse()) { + assert.match(database,/^codex_community_bugfix_[0-9a-f]{16}$/); + await admin.query(`DROP DATABASE "${database}"`); + } + await admin.end(); + } +} + +if (process.argv[1] && path.resolve(process.argv[1]) === fileURLToPath(import.meta.url)) { + const url = new URL(process.env.ALPR_COMMUNITY_TEST_SERVER_URL || "invalid:"); + if (url.protocol !== "postgresql:" || !["127.0.0.1","localhost","[::1]"].includes(url.hostname)) throw new Error("Use an explicitly configured disposable loopback PostgreSQL test server."); + await exerciseCommunityBugfixes({ host:url.hostname.replace(/^\[|\]$/g,""),port:Number(url.port || 5432),user:decodeURIComponent(url.username),password:decodeURIComponent(url.password) }); +} diff --git a/scripts/test-native-reid-upgrade-postgres.mjs b/scripts/test-native-reid-upgrade-postgres.mjs index 2640984..15fa896 100644 --- a/scripts/test-native-reid-upgrade-postgres.mjs +++ b/scripts/test-native-reid-upgrade-postgres.mjs @@ -35,7 +35,7 @@ try { VALUES ($1, 'vehicle_crop', 'test-retired-cache', 'failed', 'images/upgrade-plate.jpg', 'TEST_ONLY')`, [readId]); const derivedRead = await pool.query(`INSERT INTO public.plate_reads (plate_number, camera_name, timestamp) VALUES ('TSTUP02', 'Synthetic upgrade camera', CURRENT_TIMESTAMP) RETURNING id`); - for (const [id, classifier] of [[readId, "blue-iris-zone-crossing-v1"], [derivedRead.rows[0].id, "retired-derived-classifier"]]) { + for (const [id, classifier] of [[readId, "blue-iris-zone-crossing-v1"], [derivedRead.rows[0].id, "vehicle-reid-orientation-knn-v1"]]) { await pool.query(`INSERT INTO public.vehicle_direction_observations (read_id, camera_key, embedding_model, classifier_version, profile_version, status, orientation) VALUES ($1, 'synthetic', 'fixture', $2, 1, 'unknown', 'unknown')`, [id, classifier]); diff --git a/scripts/test-overview-export-postgres.mjs b/scripts/test-overview-export-postgres.mjs index 3572e19..0752028 100644 --- a/scripts/test-overview-export-postgres.mjs +++ b/scripts/test-overview-export-postgres.mjs @@ -9,8 +9,18 @@ import { BlueIrisVehicleFrameRepository } from "../lib/blue-iris-vehicle-frame-r const { Pool } = pg; const connectionString = String(process.env.DATABASE_URL || "").trim(); if (!connectionString) throw new Error("DATABASE_URL is required"); +const expectedDatabase = process.env.OVERVIEW_EXPORT_POSTGRES_TEST_DATABASE; +if (process.env.OVERVIEW_EXPORT_POSTGRES_TEST_OPT_IN !== "true" + || !/^(?:fixture_test|codex_(?:overview_export|community_bugfix)_[0-9a-f]{8,32})$/.test(expectedDatabase || "") + || decodeURIComponent(new URL(connectionString).pathname.slice(1)) !== expectedDatabase) { + throw new Error("Overview export tests require an explicitly named disposable database."); +} const pool = new Pool({ connectionString, max: 5 }); +try { + const guard = await pool.query("SELECT current_database() AS name, scope, guard_token FROM public.codex_integration_test_guard WHERE scope='overview-export:v1'"); + assert.deepEqual(guard.rows, [{name:expectedDatabase,scope:"overview-export:v1",guard_token:process.env.OVERVIEW_EXPORT_POSTGRES_TEST_GUARD_TOKEN}]); +} catch (error) { await pool.end(); throw error; } const migrations = await fs.readFile(new URL("../migrations.sql", import.meta.url), "utf8"); const claimA = "11111111-1111-4111-8111-111111111111"; const claimB = "22222222-2222-4222-8222-222222222222"; @@ -131,6 +141,10 @@ try { `Legacy route ${suffix}`, ], ); + // This transaction models a pre-migration schema. Completed migrations are + // deliberately skipped by the tracked runner; reset only this fixture's + // target marker, and roll it back with the synthetic legacy constraint. + await legacyFallbackClient.query("DELETE FROM public.schema_migrations WHERE version='2026081201_entry_overview_route_payload'"); await legacyFallbackClient.query(migrations); await legacyFallbackClient.query( `INSERT INTO public.vehicle_entry_fallback_decisions ( @@ -178,6 +192,7 @@ try { (LOWER($1), LOWER($2), LOWER($3), 'primary', 'street', 0, 1500, 92, TRUE)`, [`Duplicate Overview ${suffix}`, `Duplicate LPR ${suffix}`, `Duplicate Direction ${suffix}`] ); + await duplicateClient.query("DELETE FROM public.schema_migrations WHERE version='2026081005_entry_overview_primary'"); await assert.rejects( duplicateClient.query(migrations), /Duplicate enabled primary overview profiles/ diff --git a/test/blue-iris-alert-pointer.test.mjs b/test/blue-iris-alert-pointer.test.mjs index 32cdb7a..0dc6a74 100644 --- a/test/blue-iris-alert-pointer.test.mjs +++ b/test/blue-iris-alert-pointer.test.mjs @@ -70,7 +70,7 @@ test("Blue Iris vehicle frames are bounded, read-owned, and exposed as a two-vie assert.match(service, /saveDerivedImage/); assert.doesNotMatch(service, /\.bvr[^\n]*readFile|readFile[^\n]*\.bvr/); assert.match(settings, /Select best vehicle frame/); - assert.match(settings, /new Date\(match\?\.alert\?\.timestamp \|\| timestamp\)/); + assert.match(settings, /new Date\(match\?\.readTimestamp \|\| timestamp\)/); assert.match(table, /Plate capture/); assert.match(table, /Vehicle view/); assert.match(reconciliation, /vehicle_image_path/); diff --git a/test/blue-iris-camera-binding.test.mjs b/test/blue-iris-camera-binding.test.mjs new file mode 100644 index 0000000..82a1d4b --- /dev/null +++ b/test/blue-iris-camera-binding.test.mjs @@ -0,0 +1,16 @@ +import test from "node:test"; +import assert from "node:assert/strict"; +import { resolveBlueIrisCamera, readMatchTolerance } from "../lib/blue-iris-camera-binding.mjs"; + +test("Blue Iris short IDs and display aliases resolve only an unambiguous camera", () => { + const cameras = [{id:"eastlpr",name:"East LPR"}]; + assert.equal(resolveBlueIrisCamera(cameras," eastlpr "),cameras[0]); + assert.equal(resolveBlueIrisCamera(cameras,"East LPR"),cameras[0]); + assert.equal(resolveBlueIrisCamera([...cameras,{id:"other",name:"East LPR"}],"East LPR"),null); + assert.equal(resolveBlueIrisCamera(cameras,"different"),null); +}); +test("ALPR read tolerance is explicit, bounded and independent of the alert window", () => { + assert.equal(readMatchTolerance(),3); + assert.equal(readMatchTolerance(30),30); + for(const value of [0,31,90,NaN,Infinity,"bad"])assert.throws(()=>readMatchTolerance(value)); +}); diff --git a/test/community-migrations.test.mjs b/test/community-migrations.test.mjs new file mode 100644 index 0000000..5e1db97 --- /dev/null +++ b/test/community-migrations.test.mjs @@ -0,0 +1,12 @@ +import assert from "node:assert/strict"; +import test from "node:test"; +import { readFile } from "node:fs/promises"; +import { compileMigrations } from "../scripts/build-migrations.mjs"; + +test("the old updater entry point is the complete, current checksummed bundle", async () => { + const { sql, manifest } = await compileMigrations(); + assert.equal((await readFile(new URL("../migrations.sql", import.meta.url), "utf8")).replace(/\r\n/g,"\n"), sql); + assert.equal(new Set(manifest.map(row => row.version)).size, manifest.length); + assert.ok(manifest.length >= 75); + assert.ok(!sql.includes("\\ir "), "older packages require no external source files"); +}); diff --git a/test/help-manual.test.mjs b/test/help-manual.test.mjs index 58ccaf6..8599bd4 100644 --- a/test/help-manual.test.mjs +++ b/test/help-manual.test.mjs @@ -18,7 +18,7 @@ async function source(path) { } test("the user guide is structured, searchable, and role-aware", () => { - assert.equal(HELP_MANUAL.manualVersion, "3.27"); + assert.equal(HELP_MANUAL.manualVersion, "3.28"); assert.ok(HELP_MANUAL.sections.length >= 24); const ids = HELP_MANUAL.sections.map((section) => section.id); diff --git a/test/home-assistant-display.test.mjs b/test/home-assistant-display.test.mjs new file mode 100644 index 0000000..19eca8c --- /dev/null +++ b/test/home-assistant-display.test.mjs @@ -0,0 +1,42 @@ +import test from "node:test"; +import assert from "node:assert/strict"; +import { createHash } from "node:crypto"; +import { HomeAssistantDisplayService, normalizeDisplayGrant, createDisplayReadHandler } from "../lib/home-assistant-display.mjs"; +import { createMiddlewareHandler } from "../lib/middleware-auth.mjs"; + +const id = "11111111-1111-4111-8111-111111111111", token = "A".repeat(43); +test("display origins cannot inject policy, use wildcard hosts, or contain credentials", () => { + for (const allowedOrigin of ["*", "https://*.example.com", "https://a;script-src.example.com", "https://user:pass@ha.example", "https://ha.example/dashboard", "https://ha.example/#secret", "javascript:alert(1)", "https://ha.example/?a=b"]) { + assert.throws(() => normalizeDisplayGrant({label:"test",allowedOrigin})); + } + assert.equal(normalizeDisplayGrant({label:"test",allowedOrigin:"http://[::1]:8123"}).allowedOrigin,"http://[::1]:8123"); + assert.throws(() => normalizeDisplayGrant({label:"test",allowedOrigin:"http://ha:8123",days:91})); +}); + +test("a display never authorizes missing, incorrect or query-string credentials", async () => { + let queries = 0; + const service = new HomeAssistantDisplayService({query:async () => {queries++;return {rows:[{id,label:"Display",camera_name:"Camera",token_hash:createHash("sha256").update(token).digest("hex")}]};}}); + for (const value of [null,"Bearer wrong", `Bearer ${"B".repeat(43)}`]) assert.equal(await service.authorize(id,value),null); + assert.deepEqual(await service.authorize(id,`Bearer ${token}`),{id,label:"Display",cameraName:"Camera"}); + const handler = createDisplayReadHandler({service}); + const response = await handler(new Request(`http://alpr.invalid/api/home-assistant/display/${id}?token=${token}`),{params:{grantId:id}}); + assert.equal(response.status,401); + assert.equal(response.headers.get("cache-control"),"no-store, private"); + assert.equal(queries,2); +}); + +test("display authorization cannot bypass normal pages, image routes or server actions", async () => { + const middleware = createMiddlewareHandler({next:()=>({status:200}),redirect:url=>({status:307,path:url.pathname}),json:(body,init)=>({body,...init}),fetchImpl:()=>{throw new Error("unexpected verifier");}}); + for (const route of ["/settings", "/dashboard", "/images/plate.jpg", "/api/current-access", `/api/home-assistant/display/${id}/other`]) { + const url = new URL(route,"http://alpr.invalid"); + const response = await middleware({url:url.href,nextUrl:url,method:"GET",cookies:{get:()=>undefined},headers:new Headers({authorization:`Bearer ${token}`})}); + assert.ok([307,401].includes(response.status)); + } + const url = new URL(`/api/home-assistant/display/${id}`,"http://alpr.invalid"); + assert.equal((await middleware({url:url.href,nextUrl:url,method:"POST",cookies:{get:()=>undefined},headers:new Headers({authorization:`Bearer ${token}`})})).status,401); +}); + +test("unauthorized image requests never read storage or execute the data query", async () => { + const handler = createDisplayReadHandler({service:{authorize:async()=>null, imagePath:()=>assert.fail("unexpected data access")},readImage:()=>assert.fail("unexpected storage access")}); + assert.equal((await handler(new Request("http://alpr.invalid"),{params:{grantId:id,readId:"1"}})).status,401); +}); diff --git a/test/native-reid-upgrade-policy.test.mjs b/test/native-reid-upgrade-policy.test.mjs index 4b06452..eeab9a2 100644 --- a/test/native-reid-upgrade-policy.test.mjs +++ b/test/native-reid-upgrade-policy.test.mjs @@ -17,7 +17,7 @@ test("stopped-source evidence counts only unbound non-Blue-Iris direction data", const snapshot = await captureNativeUpgradeSnapshot(query, before); assert.deepEqual(snapshot, { formatVersion: 1, native: false, directionRetirements: { vehicle_orientation_labels: "2", vehicle_direction_observations: "1" } }); - assert.match(calls.at(-1), /classifier_version <> 'blue-iris-zone-crossing-v1' AND source_embedding_id IS NULL/); + assert.match(calls.at(-1), /classifier_version IN \('vehicle-reid-orientation-knn-v1','canonical-crop-orientation-knn-v2'\) AND source_embedding_id IS NULL/); const after = { schema_migrations: 2, vehicle_orientation_labels: 3, vehicle_direction_observations: 3, plate_reads: 24, users: 1, tags: 2 }; assert.deepEqual(compareSavedNativeUpgradeCounts(before, after, snapshot, true).losses, []); diff --git a/test/release-info.test.mjs b/test/release-info.test.mjs index d9ec122..877e263 100644 --- a/test/release-info.test.mjs +++ b/test/release-info.test.mjs @@ -23,30 +23,30 @@ test("release information resolves a commit-pinned deployment image", () => { ALPR_RELEASE_CHANNEL: "staging", }); - assert.equal(release.version, "0.1.59"); - assert.equal(release.manualVersion, "3.27"); - assert.equal(release.manualUpdatedAt, "November 1, 2026"); + assert.equal(release.version, "0.1.60"); + assert.equal(release.manualVersion, "3.28"); + assert.equal(release.manualUpdatedAt, "November 2, 2026"); assert.equal(release.gitSha, "8cd2fa8"); assert.equal(release.channel, "staging"); assert.equal(release.source, "commit-pinned image"); assert.equal(release.readOnly, true); assert.equal( release.notes.title, - "Axis HTTP and MQTT camera inputs" + "Reliable upgrades, Blue Iris setup and Home Assistant displays" ); - assert.equal(release.notes.publishedAt, "2026-11-01"); + assert.equal(release.notes.publishedAt, "2026-11-02"); assert.ok(release.notes.items.length >= 3); const notes = release.notes.items.join(" "); - assert.match(notes, /draggable header dividers/); - assert.match(notes, /Actions compact/); - assert.match(notes, /State\/Country/); - assert.match(notes, /off by default/); - assert.match(notes, /preview and unchanged-plate safeguards/); - assert.match(notes, /LPR camera/i); - assert.match(notes, /Linux or Windows/i); - assert.match(notes, /download percentages/i); - assert.match(notes, /Software Updates/); - assert.match(notes, /PowerShell 5\.1/); + assert.match(notes, /pending database migrations/); + assert.match(notes, /checksums/); + assert.match(notes, /Blue Iris v2 direction observations/); + assert.match(notes, /row-count validation and rollback/); + assert.match(notes, /recording short IDs/); + assert.match(notes, /explicit image owner/); + assert.match(notes, /read-only Home Assistant/); + assert.match(notes, /parent-origin restrictions/); + assert.match(notes, /User Guide 3\.28/); + }); test("an explicit valid SHA overrides the image tag", () => { diff --git a/test/vehicle-asset-attribute.test.mjs b/test/vehicle-asset-attribute.test.mjs index e15fdfc..381683a 100644 --- a/test/vehicle-asset-attribute.test.mjs +++ b/test/vehicle-asset-attribute.test.mjs @@ -229,7 +229,7 @@ test("attribute campaign processes only claimed bounded preview jobs", async () }); test("attribute migration is additive, inert, crop-owned, and immutable", async () => { - const migrations = await source("migrations.sql"); + const migrations = (await source("migrations/0061_2026081506_vehicle_asset_attribute_campaign.sql")).replace(/^-- migration:.*\r?\n/, ""); const marker = migrations.indexOf("2026081506_vehicle_asset_attribute_campaign"); const embeddingMarker = migrations.indexOf("2026081505_vehicle_asset_embedding_campaign"); assert.ok(marker > embeddingMarker); diff --git a/test/vehicle-asset-embedding.test.mjs b/test/vehicle-asset-embedding.test.mjs index 743ccb4..fb7a760 100644 --- a/test/vehicle-asset-embedding.test.mjs +++ b/test/vehicle-asset-embedding.test.mjs @@ -234,7 +234,7 @@ test("campaign service previews and catalogs only claimed bounded jobs", async ( }); test("embedding migration is additive, inert, asset-owned, and immutable", async () => { - const migrations = await source("migrations.sql"); + const migrations = (await source("migrations/0060_2026081505_vehicle_asset_embedding_campaign.sql")).replace(/^-- migration:.*\r?\n/, ""); const marker = migrations.indexOf("2026081505_vehicle_asset_embedding_campaign"); assert.ok(marker > 0); const start = migrations.lastIndexOf("-- Provider-neutral embeddings", marker); diff --git a/test/vehicle-image-asset-foundation.test.mjs b/test/vehicle-image-asset-foundation.test.mjs index 1b40b62..2fc7d35 100644 --- a/test/vehicle-image-asset-foundation.test.mjs +++ b/test/vehicle-image-asset-foundation.test.mjs @@ -550,7 +550,7 @@ test("executor-only repositories cannot bypass the storage cleanup writer lock", test("foundation migration is additive, inert, provider-neutral, and storage-safe", async () => { const [migration, repositorySource, cleanupSource, reconciliationSource] = await Promise.all([ - fs.readFile(path.join(ROOT, "migrations.sql"), "utf8"), + fs.readFile(path.join(ROOT, "migrations", "0051_2026081402_vehicle_image_asset_foundation.sql"), "utf8").then(sql => sql.replace(/^-- migration:.*\r?\n/, "")), fs.readFile(path.join(ROOT, "lib", "vehicle-image-asset-repository.mjs"), "utf8"), fs.readFile(path.join(ROOT, "lib", "storage-cleanup.mjs"), "utf8"), fs.readFile(path.join(ROOT, "lib", "storage-reconciliation-repository.mjs"), "utf8"), diff --git a/test/vehicle-image-crop.test.mjs b/test/vehicle-image-crop.test.mjs index a81a523..fff3657 100644 --- a/test/vehicle-image-crop.test.mjs +++ b/test/vehicle-image-crop.test.mjs @@ -177,7 +177,7 @@ test("vehicle crop failures distinguish invalid evidence, changed sources, and r }); test("vehicle crop migration is additive, inert, and asset owned", async () => { - const migrations = await source("migrations.sql"); + const migrations = (await source("migrations/0055_2026081406_vehicle_image_crop_campaign.sql")).replace(/^-- migration:.*\r?\n/, ""); const marker = migrations.indexOf("2026081406_vehicle_image_crop_campaign"); assert.ok(marker > 0); const sliceStart = migrations.lastIndexOf("-- Asset-owned vehicle crops", marker); diff --git a/test/vehicle-image-source.test.mjs b/test/vehicle-image-source.test.mjs index 7a9bdcd..8f321e7 100644 --- a/test/vehicle-image-source.test.mjs +++ b/test/vehicle-image-source.test.mjs @@ -5,10 +5,10 @@ import { normalizeVehicleImageSource } from "../lib/vehicle-image-source.mjs"; import { BlueIrisVehicleFrameService } from "../lib/blue-iris-vehicle-frame.mjs"; import { isOverviewAssetCandidate, overviewSourceCameraName } from "../lib/vehicle-image-asset-model.mjs"; -test("explicit LPR source binds its own camera at the plate event, without accepting an overview alias", () => { +test("explicit LPR source retains its recording-camera binding at the plate event", () => { const input = { plateCameraName: "My driveway", directionLabel: "Arriving", sourceCameraShortName: "Cam42", sourceMode: "lpr_camera" }; - const result = normalizeVehicleImageSource({ ...input, sourceCameraName: "Wrong camera", expectedDeltaMs: 8000 }); - assert.equal(result.sourceCameraName, "My driveway"); + const result = normalizeVehicleImageSource({ ...input, sourceCameraName: "Blue Iris driveway", expectedDeltaMs: 8000 }); + assert.equal(result.sourceCameraName, "Blue Iris driveway"); assert.equal(result.expectedDeltaMs, 0); for (const invalid of [{ ...input, sourceMode: "invalid" }, { ...input, sourceCameraShortName: "" }, { ...input, toleranceMs: 5000 }, { ...input, sourceMode: "overview", sourceCameraName: "MY DRIVEWAY" }]) { @@ -17,7 +17,7 @@ test("explicit LPR source binds its own camera at the plate event, without accep assert.equal(normalizeVehicleImageSource({ ...input, sourceMode: "overview", sourceCameraName: "Wide view", expectedDeltaMs: -1500 }).expectedDeltaMs, -1500); }); -async function frameFixture({ incomplete = false, plateOnly = false, monochrome = false } = {}) { +async function frameFixture({ incomplete = false, plateOnly = false, monochrome = false, mismatchedCamera = false } = {}) { const buffer = await sharp({ create: { width: 320, height: 200, channels: 3, background: monochrome ? { r: 90, g: 90, b: 90 } : { r: 15, g: 70, b: 160 } } }) .composite([{ input: { create: { width: 200, height: 100, channels: 3, @@ -34,8 +34,8 @@ async function frameFixture({ incomplete = false, plateOnly = false, monochrome const result = await service.processOverviewRead({ read: { id: 7, camera_name: "Driveway LPR", plate_number: "FIXTURE", timestamp: new Date().toISOString(), bi_trigger_direction_label: "Arriving", vehicle_image_claim_token: "11111111-1111-4111-8111-111111111111" }, - profile: { id: 1, source_mode: "lpr_camera", source_camera_name: "Driveway LPR", source_camera_short_name: "Cam42", - expected_delta_ms: 0, tolerance_ms: 250, revision: 1 }, camera: "Cam42", alreadyClaimed: true, + profile: { id: 1, source_mode: "lpr_camera", plate_camera_name: "Driveway LPR", source_camera_name: "Blue Iris driveway", source_camera_short_name: "Cam42", + expected_delta_ms: 0, tolerance_ms: 250, revision: 1 }, camera: mismatchedCamera ? "WrongCamera" : "Cam42", alreadyClaimed: true, }); return { result, ready, failed }; } @@ -47,7 +47,7 @@ test("whole-vehicle LPR recordings retain provenance and enter the existing crop const read = { vehicle_image_status: "ready", vehicle_image_path: ready.framePath, vehicle_image_source_kind: ready.sourceKind, vehicle_image_selection_metadata: ready.selectionMetadata }; assert.ok(isOverviewAssetCandidate(read)); - assert.equal(overviewSourceCameraName(read), "Driveway LPR"); + assert.equal(overviewSourceCameraName(read), "Blue Iris driveway"); assert.ok(ready.detectionBox && ready.imageWidth === 320); }); @@ -59,3 +59,7 @@ test("plate-only, clipped, and monochrome LPR recordings never become identity e assert.equal(failed.retryable, false); } }); + +test("an LPR display alias never overrides the explicit recording short ID", async () => { + await assert.rejects(frameFixture({mismatchedCamera:true}),/explicit recording camera binding/); +});