Files
khadhroony-bot3/kb-store/migrations/0003_processing_ledger.sql
2026-07-23 18:25:10 +02:00

42 lines
2.2 KiB
SQL

-- file: kb-store/migrations/0003_processing_ledger.sql
-- version: 1
-- Current processing ledger for canonical to core extraction and later pipeline stages.
-- This migration uses the current PostgreSQL schema from the active profile.
CREATE TABLE IF NOT EXISTS kb_sol_ops_processing_ledger (
id BIGSERIAL,
stage TEXT NOT NULL,
processor_name TEXT NOT NULL,
processor_version TEXT NOT NULL,
input_key TEXT NOT NULL,
input_hash TEXT NOT NULL,
status TEXT NOT NULL,
attempt_count INTEGER NOT NULL DEFAULT 0,
started_at TIMESTAMPTZ,
finished_at TIMESTAMPTZ,
error_code TEXT,
error_message TEXT,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
CONSTRAINT pk_kb_sol_ops_processing_ledger PRIMARY KEY (id),
CONSTRAINT ck_kb_sol_ops_processing_ledger_stage_not_empty CHECK (length(btrim(stage)) > 0),
CONSTRAINT ck_kb_sol_ops_processing_ledger_processor_name_not_empty CHECK (length(btrim(processor_name)) > 0),
CONSTRAINT ck_kb_sol_ops_processing_ledger_processor_version_not_empty CHECK (length(btrim(processor_version)) > 0),
CONSTRAINT ck_kb_sol_ops_processing_ledger_input_key_not_empty CHECK (length(btrim(input_key)) > 0),
CONSTRAINT ck_kb_sol_ops_processing_ledger_input_hash_not_empty CHECK (length(btrim(input_hash)) > 0),
CONSTRAINT ck_kb_sol_ops_processing_ledger_status CHECK (status IN ('running', 'succeeded', 'failed')),
CONSTRAINT ck_kb_sol_ops_processing_ledger_attempt_count_non_negative CHECK (attempt_count >= 0),
CONSTRAINT ck_kb_sol_ops_processing_ledger_error_code_not_empty CHECK (error_code IS NULL OR length(btrim(error_code)) > 0),
CONSTRAINT ck_kb_sol_ops_processing_ledger_error_message_not_empty CHECK (error_message IS NULL OR length(btrim(error_message)) > 0)
);
CREATE UNIQUE INDEX IF NOT EXISTS ux_kb_sol_ops_processing_ledger_identity
ON kb_sol_ops_processing_ledger (stage, processor_name, processor_version, input_key);
CREATE INDEX IF NOT EXISTS ix_kb_sol_ops_processing_ledger_status
ON kb_sol_ops_processing_ledger (stage, processor_name, status, updated_at);
CREATE INDEX IF NOT EXISTS ix_kb_sol_ops_processing_ledger_input_hash
ON kb_sol_ops_processing_ledger (input_hash);