51 lines
3.3 KiB
TypeScript
51 lines
3.3 KiB
TypeScript
import { canonical, sha } from "./application-writer-profile-canonical.js"
|
|
import { sorted, type Inventory, type Row } from "./catalogue-projection.js"
|
|
import { BUILD57_PROVENANCE, FIXED57_ROLE_TEMPLATE } from "./fixed57-build-definition.js"
|
|
import { RESTORE_WRITER_PROFILE_DEFINITION_SHA256, RESTORE_WRITER_PROFILE_MANIFEST_SHA256, restoreProfileEnrollmentSql } from "../../src/db/database-restore-quarantine-profile.js"
|
|
|
|
const compactedProbeLayout = { relname: "server_probe_executions", removedAttnum: 18 }
|
|
const restoredChecks = [
|
|
{ relname: "server_poll_archives", conname: "server_poll_archives_check",
|
|
definition: "CHECK (((ciphertext_bytes >= 1) AND (ciphertext_bytes <= 131072) AND (ciphertext_bytes = octet_length(ciphertext))))" },
|
|
{ relname: "server_poll_storage_admissions", conname: "server_poll_storage_admissions_owner_id_check",
|
|
definition: "CHECK (((length(owner_id) >= 1) AND (length(owner_id) <= 200) AND (btrim(owner_id) = owner_id) AND (owner_id !~ '[[:cntrl:]]'::text)))" },
|
|
{ relname: "server_poll_storage_admissions", conname: "server_poll_storage_admissions_check1",
|
|
definition: "CHECK (((used_rows >= 0) AND (used_rows <= 4019) AND (used_rows <= ((max_stages * 2) + 3))))" },
|
|
]
|
|
|
|
export const FIXED58_ROLE_TEMPLATE: Readonly<Inventory> = Object.freeze({
|
|
...FIXED57_ROLE_TEMPLATE,
|
|
schemas: sorted(FIXED57_ROLE_TEMPLATE.schemas.map(row => row.nspname === "mmapp_control"
|
|
? { ...row, acl: [...row.acl as string[], "@mm-role:runtime@=U/@mm-role:control@"].sort() } : row)),
|
|
effectiveSchemas: sorted(FIXED57_ROLE_TEMPLATE.effectiveSchemas.map(row => row.nspname === "mmapp_control" && row.rolname === "@mm-role:runtime@"
|
|
? { ...row, usage: true } : row)),
|
|
restoreControl: [{ enrollmentSha256: sha(restoreProfileEnrollmentSql("restore_runtime", "restore_operator")) }],
|
|
restorePortable: [compactedProbeLayout, ...restoredChecks],
|
|
})
|
|
|
|
export function normalizeFixed58RestoreRows(relation: string, group: string, rows: Row[]): Row[] {
|
|
if (relation === compactedProbeLayout.relname && group === "columns" && rows.some(row => row.attnum === compactedProbeLayout.removedAttnum)) {
|
|
// Exact comparison below still checks names, their order, types, ACL and other properties.
|
|
return sorted(rows.map(row => typeof row.attnum === "number" && row.attnum >= compactedProbeLayout.removedAttnum
|
|
? { ...row, attnum: row.attnum + 1 } : row))
|
|
}
|
|
if (group === "constraints" && restoredChecks.some(check => check.relname === relation)) {
|
|
return sorted(rows.map(row => {
|
|
const restored = restoredChecks.find(check => check.relname === relation && check.conname === row.conname && check.definition === row.definition)
|
|
return restored ? { ...row, definition: FIXED57_ROLE_TEMPLATE.constraints.find(original =>
|
|
original.relname === relation && original.conname === restored.conname)!.definition } : row
|
|
}))
|
|
}
|
|
return rows
|
|
}
|
|
|
|
export const BUILD58_PROVENANCE = Object.freeze({
|
|
format: "source58-restore-profile-v1",
|
|
manifestSha256: RESTORE_WRITER_PROFILE_MANIFEST_SHA256,
|
|
templateSha256: RESTORE_WRITER_PROFILE_DEFINITION_SHA256,
|
|
})
|
|
|
|
if (sha(canonical(FIXED58_ROLE_TEMPLATE)) !== BUILD58_PROVENANCE.templateSha256 || BUILD58_PROVENANCE.manifestSha256 !== BUILD57_PROVENANCE.manifestSha256) {
|
|
throw new Error("Fixed58 writer catalogue changed")
|
|
}
|