Security Scan (SIAX Cloud) / security (push) Successful in 16s
CI (SIAX Cloud) / security (push) Successful in 17s
CI (SIAX Cloud) / integration (push) Successful in 29s
CI (SIAX Cloud) / quality (push) Successful in 48s
Security Scan (SIAX Cloud) / security (pull_request) Successful in 15s
CI (SIAX Cloud) / security (pull_request) Successful in 28s
CI (SIAX Cloud) / quality (pull_request) Successful in 48s
CI (SIAX Cloud) / integration (pull_request) Successful in 55s
Python Engine CI (SIAX Cloud) / python-engine (push) Failing after 4m52s
Python Engine CI (SIAX Cloud) / python-engine (pull_request) Failing after 4m48s
Slutgate inför merge (per fortsättningsplan): Kontrakt: CreateDatasetVersionInput.artifact_refs → readonly string[] (anropare får skicka 'as const'; servicen muterar aldrig); postgres.js tx.array() får muterbar kopia vid INSERT. Testkorrigeringar (stale mot starkare implementation — aldrig svagare): - entity-resolution-http: request(body, undefined) triggade default-param och skickade GILTIGT token → 200. Tomt token = 'utan token' → 401 fail-closed. - entity-resolution: /two records/ → /between 2 and 5000/ (bounds-aware validering) - search-service: omformat purpose-filter-test + NY negativ kontroll — rebuild avvisar dokument utan purpose_tags (fail closed, generation discarded) - dataset-postgres.realinfra: sätter DATABASE_URL i setup så den durabla AUD0-outboxen aktiveras (testet ljög annars om durabilitet) — 5/5 mot live Postgres STATUS.md: AUD0-klient + lineage-rader (outbox nu real-infra testad), gap-listan (persistence/routes/outbox stängda, exakt vad som återstår), P065 STÄNGD med closure-record (thin adapter, v3-semantik, hard-negative transitivitet, fail-closed 401 — 4/4 planpunkter verifierade). Verifierat: typecheck EXIT 0 · 945/945 unit · 5/5 real-Postgres · next build EXIT 0
178 lines
7.5 KiB
TypeScript
178 lines
7.5 KiB
TypeScript
import { resolveStoreMode } from "../platform/dependency-mode";
|
|
import { SiaxError } from "../platform/errors";
|
|
import type { CreateDatasetVersionInput, DatasetVersion } from "../types/dataset-version";
|
|
import { getRepositorySql, repositoryDatabaseUrl, type RepositorySql } from "./db";
|
|
|
|
export interface DatasetVersionRepository {
|
|
create(input: CreateDatasetVersionInput): Promise<DatasetVersion>;
|
|
list(datasetId: string, tenantId: string): Promise<DatasetVersion[]>;
|
|
get(datasetId: string, tenantId: string, version: number): Promise<DatasetVersion>;
|
|
}
|
|
|
|
interface VersionRow {
|
|
dataset_id: string;
|
|
version: number;
|
|
tenant_id: string;
|
|
project_id: string;
|
|
artifact_refs: string[];
|
|
schema_version: string;
|
|
record_count: string | number | null;
|
|
description: string | null;
|
|
created_at: string | Date;
|
|
created_by: string;
|
|
correlation_id: string | null;
|
|
idempotency_key: string | null;
|
|
}
|
|
|
|
function toVersion(row: VersionRow): DatasetVersion {
|
|
return {
|
|
dataset_id: row.dataset_id,
|
|
version: Number(row.version),
|
|
tenant_id: row.tenant_id,
|
|
project_id: row.project_id,
|
|
artifact_refs: [...row.artifact_refs],
|
|
schema_version: row.schema_version,
|
|
...(row.record_count == null ? {} : { record_count: Number(row.record_count) }),
|
|
...(row.description ? { description: row.description } : {}),
|
|
created_at: row.created_at instanceof Date ? row.created_at.toISOString() : new Date(row.created_at).toISOString(),
|
|
created_by: row.created_by,
|
|
...(row.correlation_id ? { correlation_id: row.correlation_id } : {}),
|
|
};
|
|
}
|
|
|
|
function normalizedCount(value: string | number | null | undefined): number | undefined {
|
|
return value == null ? undefined : Number(value);
|
|
}
|
|
|
|
function sameInput(row: VersionRow | DatasetVersion, input: CreateDatasetVersionInput): boolean {
|
|
return row.dataset_id === input.dataset_id
|
|
&& row.tenant_id === input.tenant_id
|
|
&& row.project_id === input.project_id
|
|
&& row.schema_version === input.schema_version
|
|
&& normalizedCount(row.record_count) === normalizedCount(input.record_count)
|
|
&& (row.description ?? undefined) === (input.description ?? undefined)
|
|
&& row.created_by === input.created_by
|
|
&& JSON.stringify(row.artifact_refs) === JSON.stringify(input.artifact_refs);
|
|
}
|
|
|
|
export class InMemoryDatasetVersionRepository implements DatasetVersionRepository {
|
|
private readonly versions = new Map<string, DatasetVersion[]>();
|
|
private readonly idempotency = new Map<string, DatasetVersion>();
|
|
|
|
async create(input: CreateDatasetVersionInput): Promise<DatasetVersion> {
|
|
if (input.idempotency_key) {
|
|
const existing = this.idempotency.get(`${input.tenant_id}:${input.idempotency_key}`);
|
|
if (existing) {
|
|
if (!sameInput(existing, input)) {
|
|
throw new SiaxError("CONFLICT", "0pt.dataset", "idempotency key reused with different dataset-version input");
|
|
}
|
|
return { ...existing, artifact_refs: [...existing.artifact_refs] };
|
|
}
|
|
}
|
|
const list = this.versions.get(input.dataset_id) ?? [];
|
|
const version: DatasetVersion = {
|
|
dataset_id: input.dataset_id,
|
|
version: (list[list.length - 1]?.version ?? 0) + 1,
|
|
tenant_id: input.tenant_id,
|
|
project_id: input.project_id,
|
|
artifact_refs: [...input.artifact_refs],
|
|
schema_version: input.schema_version,
|
|
...(input.record_count === undefined ? {} : { record_count: input.record_count }),
|
|
...(input.description ? { description: input.description } : {}),
|
|
created_at: new Date().toISOString(),
|
|
created_by: input.created_by,
|
|
...(input.correlation_id ? { correlation_id: input.correlation_id } : {}),
|
|
};
|
|
list.push(version);
|
|
this.versions.set(input.dataset_id, list);
|
|
if (input.idempotency_key) this.idempotency.set(`${input.tenant_id}:${input.idempotency_key}`, version);
|
|
return { ...version, artifact_refs: [...version.artifact_refs] };
|
|
}
|
|
|
|
async list(datasetId: string, tenantId: string): Promise<DatasetVersion[]> {
|
|
return (this.versions.get(datasetId) ?? [])
|
|
.filter((version) => version.tenant_id === tenantId)
|
|
.map((version) => ({ ...version, artifact_refs: [...version.artifact_refs] }));
|
|
}
|
|
|
|
async get(datasetId: string, tenantId: string, version: number): Promise<DatasetVersion> {
|
|
const found = (await this.list(datasetId, tenantId)).find((item) => item.version === version);
|
|
if (!found) throw new SiaxError("NOT_FOUND", "0pt.dataset", `dataset ${datasetId} has no version ${version}`);
|
|
return found;
|
|
}
|
|
}
|
|
|
|
export class PostgresDatasetVersionRepository implements DatasetVersionRepository {
|
|
constructor(private readonly sql: RepositorySql) {}
|
|
|
|
async create(input: CreateDatasetVersionInput): Promise<DatasetVersion> {
|
|
return this.sql.begin(async (tx) => {
|
|
const parent = await tx<{ dataset_id: string; tenant_id: string }[]>`
|
|
SELECT dataset_id, tenant_id
|
|
FROM opt_datasets
|
|
WHERE dataset_id = ${input.dataset_id}
|
|
FOR UPDATE`;
|
|
if (!parent[0] || parent[0].tenant_id !== input.tenant_id) {
|
|
throw new SiaxError("NOT_FOUND", "0pt.dataset", `dataset ${input.dataset_id} not found`);
|
|
}
|
|
|
|
if (input.idempotency_key) {
|
|
const existing = await tx<VersionRow[]>`
|
|
SELECT * FROM opt_dataset_versions
|
|
WHERE tenant_id = ${input.tenant_id}
|
|
AND idempotency_key = ${input.idempotency_key}
|
|
LIMIT 1`;
|
|
if (existing[0]) {
|
|
if (!sameInput(existing[0], input)) {
|
|
throw new SiaxError("CONFLICT", "0pt.dataset", "idempotency key reused with different dataset-version input");
|
|
}
|
|
return toVersion(existing[0]);
|
|
}
|
|
}
|
|
|
|
const next = await tx<{ version: number }[]>`
|
|
SELECT COALESCE(max(version), 0)::int + 1 AS version
|
|
FROM opt_dataset_versions
|
|
WHERE dataset_id = ${input.dataset_id}`;
|
|
const version = next[0]?.version ?? 1;
|
|
|
|
const rows = await tx<VersionRow[]>`
|
|
INSERT INTO opt_dataset_versions
|
|
(dataset_id, version, tenant_id, project_id, artifact_refs,
|
|
schema_version, record_count, description, created_by,
|
|
correlation_id, idempotency_key)
|
|
VALUES
|
|
(${input.dataset_id}, ${version}, ${input.tenant_id}, ${input.project_id},
|
|
${tx.array([...input.artifact_refs])}, ${input.schema_version}, ${input.record_count ?? null},
|
|
${input.description ?? null}, ${input.created_by}, ${input.correlation_id ?? null},
|
|
${input.idempotency_key ?? null})
|
|
RETURNING *`;
|
|
return toVersion(rows[0]);
|
|
});
|
|
}
|
|
|
|
async list(datasetId: string, tenantId: string): Promise<DatasetVersion[]> {
|
|
const rows = await this.sql<VersionRow[]>`
|
|
SELECT * FROM opt_dataset_versions
|
|
WHERE dataset_id = ${datasetId} AND tenant_id = ${tenantId}
|
|
ORDER BY version ASC`;
|
|
return rows.map(toVersion);
|
|
}
|
|
|
|
async get(datasetId: string, tenantId: string, version: number): Promise<DatasetVersion> {
|
|
const rows = await this.sql<VersionRow[]>`
|
|
SELECT * FROM opt_dataset_versions
|
|
WHERE dataset_id = ${datasetId} AND tenant_id = ${tenantId} AND version = ${version}
|
|
LIMIT 1`;
|
|
if (!rows[0]) throw new SiaxError("NOT_FOUND", "0pt.dataset", `dataset ${datasetId} has no version ${version}`);
|
|
return toVersion(rows[0]);
|
|
}
|
|
}
|
|
|
|
export function createDatasetVersionRepository(databaseUrl?: string): DatasetVersionRepository {
|
|
const mode = resolveStoreMode("0pt.dataset_versions", databaseUrl ?? repositoryDatabaseUrl());
|
|
return mode.mode === "postgres"
|
|
? new PostgresDatasetVersionRepository(getRepositorySql(mode.url))
|
|
: new InMemoryDatasetVersionRepository();
|
|
}
|