CREATE TABLE IF NOT EXISTS document_diagnostic_outputs ( id SERIAL PRIMARY KEY, document_id INTEGER NOT NULL REFERENCES documents(id) ON DELETE CASCADE, engine VARCHAR(80) NOT NULL, output_type VARCHAR(80) NOT NULL, version_number INTEGER NOT NULL, file_path TEXT, status VARCHAR(40) NOT NULL DEFAULT 'created', error_message TEXT, metadata_json JSONB DEFAULT '{}'::jsonb, is_selected BOOLEAN NOT NULL DEFAULT false, created_at TIMESTAMP WITHOUT TIME ZONE DEFAULT NOW(), updated_at TIMESTAMP WITHOUT TIME ZONE DEFAULT NOW(), UNIQUE(document_id, engine, output_type, version_number) ); CREATE INDEX IF NOT EXISTS ix_document_diagnostic_outputs_document_id ON document_diagnostic_outputs(document_id); CREATE INDEX IF NOT EXISTS ix_document_diagnostic_outputs_lookup ON document_diagnostic_outputs(document_id, engine, output_type);