T-74: M1.1i — Pass 3/4/6 match & conflict report: pure in-memory Markdown + CSV generator and override seed-SQL template (no DB I/O)
This commit is contained in:
parent
cbabb17094
commit
83ec301220
3 changed files with 251 additions and 43 deletions
|
|
@ -998,7 +998,7 @@ export function generateOverrideSeedSql(rows: readonly OverrideSeedRow[]): strin
|
|||
[
|
||||
'-- Security master override seed (M1.1i)',
|
||||
'-- Generated deterministically by apps/stock/data-pipeline/src/security-master/match-report.ts.',
|
||||
'-- Resolves target listings by natural key in the active search_path; idempotent via ON CONFLICT DO NOTHING.',
|
||||
'-- Resolves target listings by natural key (requires a unique match; 0 matches is a no-op, >1 match raises cardinality_violation); idempotent via ON CONFLICT DO NOTHING.',
|
||||
].join('\n'),
|
||||
];
|
||||
|
||||
|
|
@ -1012,18 +1012,22 @@ export function generateOverrideSeedSql(rows: readonly OverrideSeedRow[]): strin
|
|||
` ${escapeSqlLiteral(row.sourceId)},`,
|
||||
` DATE '${row.validFrom}',`,
|
||||
` ${validToSql},`,
|
||||
' l.listing_id,',
|
||||
' matched.listing_id,',
|
||||
` ${escapeSqlLiteral(row.reason)},`,
|
||||
` ${escapeSqlLiteral(row.createdBy)}`,
|
||||
'FROM listing l',
|
||||
'JOIN instrument i ON i.instrument_id = l.instrument_id',
|
||||
`WHERE i.primary_isin = ${escapeSqlLiteral(row.primaryIsin)}`,
|
||||
` AND l.mic = ${escapeSqlLiteral(row.mic)}`,
|
||||
` AND l.ticker_root = ${escapeSqlLiteral(row.tickerRoot)}`,
|
||||
` AND l.class_suffix = ${escapeSqlLiteral(row.classSuffix)}`,
|
||||
` AND l.currency = ${escapeSqlLiteral(row.currency)}`,
|
||||
'ORDER BY l.listing_id',
|
||||
'LIMIT 1',
|
||||
'FROM (',
|
||||
' SELECT (',
|
||||
' SELECT l.listing_id',
|
||||
' FROM listing l',
|
||||
' JOIN instrument i ON i.instrument_id = l.instrument_id',
|
||||
` WHERE i.primary_isin = ${escapeSqlLiteral(row.primaryIsin)}`,
|
||||
` AND l.mic = ${escapeSqlLiteral(row.mic)}`,
|
||||
` AND l.ticker_root = ${escapeSqlLiteral(row.tickerRoot)}`,
|
||||
` AND l.class_suffix = ${escapeSqlLiteral(row.classSuffix)}`,
|
||||
` AND l.currency = ${escapeSqlLiteral(row.currency)}`,
|
||||
' ) AS listing_id',
|
||||
') AS matched',
|
||||
'WHERE matched.listing_id IS NOT NULL',
|
||||
'ON CONFLICT (source, source_id, valid_from) DO NOTHING;',
|
||||
].join('\n')
|
||||
);
|
||||
|
|
@ -1032,8 +1036,41 @@ export function generateOverrideSeedSql(rows: readonly OverrideSeedRow[]): strin
|
|||
return `${sections.join('\n\n')}\n`;
|
||||
}
|
||||
|
||||
function sanitizeTemplateSingleLine(value: string): string {
|
||||
return value.replaceAll('\0', '').replaceAll(/\r\n|\r|\n/g, ' ');
|
||||
function formatTemplateCommentValue(value: string): string {
|
||||
return value
|
||||
.replaceAll('\\', '\\\\')
|
||||
.replaceAll('\0', '\\u0000')
|
||||
.replaceAll('\r', '\\r')
|
||||
.replaceAll('\n', '\\n');
|
||||
}
|
||||
|
||||
function formatSingleLineSqlLiteral(value: string): string {
|
||||
if (value.includes('\0')) {
|
||||
throw new Error('SQL literal value must not contain NUL bytes');
|
||||
}
|
||||
if (!value.includes('\r') && !value.includes('\n')) {
|
||||
return escapeSqlLiteral(value);
|
||||
}
|
||||
const escaped = value
|
||||
.replaceAll('\\', '\\\\')
|
||||
.replaceAll("'", "''")
|
||||
.replaceAll('\r', '\\r')
|
||||
.replaceAll('\n', '\\n');
|
||||
return `E'${escaped}'`;
|
||||
}
|
||||
|
||||
function getUnstorableTemplateIdReasons(value: string, maxLength: number): string[] {
|
||||
const reasons: string[] = [];
|
||||
if (value.includes('\0')) {
|
||||
reasons.push('contains NUL byte');
|
||||
}
|
||||
if (value.trim().length === 0) {
|
||||
reasons.push('is empty or whitespace-only');
|
||||
}
|
||||
if (value.length > maxLength) {
|
||||
reasons.push(`length (${value.length}) exceeds ${maxLength}-character limit`);
|
||||
}
|
||||
return reasons;
|
||||
}
|
||||
|
||||
function commentOutSqlBlock(lines: readonly string[]): string {
|
||||
|
|
@ -1054,6 +1091,7 @@ export function generateOverrideTemplateSql(report: MatchReport): string {
|
|||
'-- Security master override review template (M1.1i)',
|
||||
'-- Generated deterministically by apps/stock/data-pipeline/src/security-master/match-report.ts.',
|
||||
'-- Review open conflicts below, uncomment a statement, and fill in the target listing natural key to apply.',
|
||||
'-- Requires a unique natural-key match (0 matches is a no-op, >1 match raises cardinality_violation).',
|
||||
'-- Nothing in this file is auto-applied.',
|
||||
].join('\n'),
|
||||
];
|
||||
|
|
@ -1064,35 +1102,67 @@ export function generateOverrideTemplateSql(report: MatchReport): string {
|
|||
}
|
||||
|
||||
for (const conflict of sortedConflicts) {
|
||||
const safeSource = sanitizeTemplateSingleLine(conflict.source);
|
||||
const safeSourceId = sanitizeTemplateSingleLine(conflict.sourceId);
|
||||
const safeKind = sanitizeTemplateSingleLine(conflict.conflictKind);
|
||||
const safePass = sanitizeTemplateSingleLine(conflict.pass);
|
||||
const commentSource = formatTemplateCommentValue(conflict.source);
|
||||
const commentSourceId = formatTemplateCommentValue(conflict.sourceId);
|
||||
const commentKind = formatTemplateCommentValue(conflict.conflictKind);
|
||||
const commentPass = formatTemplateCommentValue(conflict.pass);
|
||||
|
||||
sections.push(
|
||||
commentOutSqlBlock([
|
||||
`Conflict: pass=${safePass} kind=${safeKind} source=${safeSource} source_id=${safeSourceId}`,
|
||||
'INSERT INTO override (source, source_id, valid_from, valid_to, listing_id, reason, created_by)',
|
||||
'SELECT',
|
||||
` ${escapeSqlLiteral(safeSource)},`,
|
||||
` ${escapeSqlLiteral(safeSourceId)},`,
|
||||
` DATE '${DEFAULT_OVERRIDE_VALID_FROM}',`,
|
||||
' NULL,',
|
||||
' l.listing_id,',
|
||||
` ${escapeSqlLiteral(`TODO: explain resolution for ${safeKind}`)},`,
|
||||
` ${escapeSqlLiteral('TODO: reviewer')}`,
|
||||
'FROM listing l',
|
||||
'JOIN instrument i ON i.instrument_id = l.instrument_id',
|
||||
"WHERE i.primary_isin = 'TODO_ISIN_12'",
|
||||
" AND l.mic = 'TODO_MIC'",
|
||||
" AND l.ticker_root = 'TODO_TICKER'",
|
||||
" AND l.class_suffix = ''",
|
||||
" AND l.currency = 'USD'",
|
||||
'ORDER BY l.listing_id',
|
||||
'LIMIT 1',
|
||||
'ON CONFLICT (source, source_id, valid_from) DO NOTHING;',
|
||||
])
|
||||
const sourceIssues = getUnstorableTemplateIdReasons(conflict.source, MAX_OVERRIDE_SOURCE_LENGTH);
|
||||
const sourceIdIssues = getUnstorableTemplateIdReasons(
|
||||
conflict.sourceId,
|
||||
MAX_OVERRIDE_SOURCE_ID_LENGTH
|
||||
);
|
||||
|
||||
const sourceSqlLiteral =
|
||||
sourceIssues.length === 0
|
||||
? formatSingleLineSqlLiteral(conflict.source)
|
||||
: "'TODO_CORRECT_SOURCE'";
|
||||
const sourceIdSqlLiteral =
|
||||
sourceIdIssues.length === 0
|
||||
? formatSingleLineSqlLiteral(conflict.sourceId)
|
||||
: "'TODO_CORRECT_SOURCE_ID'";
|
||||
|
||||
const blockLines: string[] = [
|
||||
`Conflict: pass=${commentPass} kind=${commentKind} source=${commentSource} source_id=${commentSourceId}`,
|
||||
];
|
||||
if (sourceIssues.length > 0) {
|
||||
blockLines.push(
|
||||
`WARNING: Cannot store raw source in PostgreSQL (${sourceIssues.join('; ')}); correct source before applying.`
|
||||
);
|
||||
}
|
||||
if (sourceIdIssues.length > 0) {
|
||||
blockLines.push(
|
||||
`WARNING: Cannot store raw source_id in PostgreSQL (${sourceIdIssues.join('; ')}); correct source_id before applying.`
|
||||
);
|
||||
}
|
||||
|
||||
blockLines.push(
|
||||
'INSERT INTO override (source, source_id, valid_from, valid_to, listing_id, reason, created_by)',
|
||||
'SELECT',
|
||||
` ${sourceSqlLiteral},`,
|
||||
` ${sourceIdSqlLiteral},`,
|
||||
` DATE '${DEFAULT_OVERRIDE_VALID_FROM}',`,
|
||||
' NULL,',
|
||||
' matched.listing_id,',
|
||||
` ${formatSingleLineSqlLiteral(`TODO: explain resolution for ${commentKind}`)},`,
|
||||
` ${escapeSqlLiteral('TODO: reviewer')}`,
|
||||
'FROM (',
|
||||
' SELECT (',
|
||||
' SELECT l.listing_id',
|
||||
' FROM listing l',
|
||||
' JOIN instrument i ON i.instrument_id = l.instrument_id',
|
||||
" WHERE i.primary_isin = 'TODO_ISIN_12'",
|
||||
" AND l.mic = 'TODO_MIC'",
|
||||
" AND l.ticker_root = 'TODO_TICKER'",
|
||||
" AND l.class_suffix = ''",
|
||||
" AND l.currency = 'USD'",
|
||||
' ) AS listing_id',
|
||||
') AS matched',
|
||||
'WHERE matched.listing_id IS NOT NULL',
|
||||
'ON CONFLICT (source, source_id, valid_from) DO NOTHING;'
|
||||
);
|
||||
|
||||
sections.push(commentOutSqlBlock(blockLines));
|
||||
}
|
||||
|
||||
return `${sections.join('\n\n')}\n`;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue