247 lines
9.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { loadEnvConfig } from '@next/env'
loadEnvConfig(process.cwd())
export const BOM_REFERENCE_SYNC_CONFIRM = 'sync-bom-cost-reference'
type CliOptions = {
apply: boolean
allowLargeRemoval: boolean
confirm: string
expectedDatabaseIdentity: string
expectedSourceDatabaseIdentity: string
maxPages: number | undefined
maxRetries: number | undefined
startAfterId: string
resumeBatchId: string
help: boolean
}
function parseIntegerOption(argument: string, prefix: string, bounds: { min: number; max: number }) {
const value = Number(argument.slice(prefix.length))
if (!Number.isSafeInteger(value) || value < bounds.min || value > bounds.max) {
throw new Error(`${prefix.slice(0, -1)} must be an integer between ${bounds.min} and ${bounds.max}`)
}
return value
}
function parseArgs(argv: string[]): CliOptions {
const options: CliOptions = {
apply: false,
allowLargeRemoval: false,
confirm: '',
expectedDatabaseIdentity: '',
expectedSourceDatabaseIdentity: '',
maxPages: undefined,
maxRetries: undefined,
startAfterId: '0',
resumeBatchId: '',
help: false,
}
for (const arg of argv) {
if (arg === '--apply') options.apply = true
else if (arg === '--dry-run') options.apply = false
else if (arg === '--allow-large-removal') options.allowLargeRemoval = true
else if (arg === '--help' || arg === '-h') options.help = true
else if (arg.startsWith('--confirm=')) options.confirm = arg.slice('--confirm='.length)
else if (arg.startsWith('--expected-database-identity=')) {
options.expectedDatabaseIdentity = arg.slice('--expected-database-identity='.length)
} else if (arg.startsWith('--expected-source-database-identity=')) {
options.expectedSourceDatabaseIdentity = arg.slice('--expected-source-database-identity='.length)
} else if (arg.startsWith('--max-pages=')) {
options.maxPages = parseIntegerOption(arg, '--max-pages=', { min: 1, max: 10_000 })
} else if (arg.startsWith('--max-retries=')) {
options.maxRetries = parseIntegerOption(arg, '--max-retries=', { min: 0, max: 10 })
} else if (arg.startsWith('--start-after-id=')) {
const value = arg.slice('--start-after-id='.length).trim()
if (!/^\d+$/.test(value)) throw new Error('--start-after-id must be a non-negative integer')
options.startAfterId = BigInt(value).toString()
} else if (arg.startsWith('--resume-batch-id=')) {
const value = arg.slice('--resume-batch-id='.length).trim()
if (!value || value.length > 200 || !/^[A-Za-z0-9_-]+$/.test(value)) {
throw new Error('--resume-batch-id is invalid')
}
options.resumeBatchId = value
} else {
throw new Error(`Unknown argument: ${arg}`)
}
}
if (argv.includes('--apply') && argv.includes('--dry-run')) {
throw new Error('Cannot pass --apply and --dry-run together')
}
if (!options.apply && options.allowLargeRemoval) {
throw new Error('--allow-large-removal requires --apply')
}
if (options.apply && options.maxPages !== undefined) {
throw new Error('--max-pages is dry-run only; apply must scan the complete source')
}
if (options.apply && options.startAfterId !== '0') {
throw new Error('--start-after-id is dry-run only; apply must start at zero')
}
if (!options.apply && options.resumeBatchId) {
throw new Error('--resume-batch-id requires --apply')
}
return options
}
function databaseIdentityFromUrl(databaseUrl: string) {
const parsed = new URL(databaseUrl)
return [
parsed.protocol.replace(/:$/, ''),
decodeURIComponent(parsed.username || ''),
parsed.hostname,
parsed.port || '',
parsed.pathname.replace(/^\//, ''),
parsed.searchParams.get('schema') ?? '',
].join('|')
}
export function sourceDatabaseIdentity(env: NodeJS.ProcessEnv = process.env) {
const host = (
env.BOM_COSTING_UPSTREAM_HOST ||
env.JST_PLANNING_SQLSERVER_HOST ||
env.JST_REPORT_DB_HOST ||
''
).trim()
const database = (
env.BOM_COSTING_UPSTREAM_DATABASE ||
env.JST_PLANNING_SQLSERVER_DATABASE ||
env.JST_REPORT_DB_NAME ||
''
).trim()
const user = (
env.BOM_COSTING_UPSTREAM_USER ||
env.JST_PLANNING_SQLSERVER_USERNAME ||
env.JST_REPORT_DB_USER ||
''
).trim()
if (!host || !database || !user) return ''
return [
'sqlserver',
user,
host,
(
env.BOM_COSTING_UPSTREAM_PORT ||
env.JST_PLANNING_SQLSERVER_PORT ||
env.JST_REPORT_DB_PORT ||
'1433'
).trim(),
database,
(
env.BOM_COSTING_UPSTREAM_SCHEMA ||
env.JST_PLANNING_SQLSERVER_SCHEMA ||
env.JST_REPORT_DB_SCHEMA ||
'rpt'
).trim(),
(env.BOM_COSTING_UPSTREAM_OBJECT || 'pm_bomBaseInfo').trim(),
].join('|')
}
export function assertApplyGuards(
options: CliOptions,
env: NodeJS.ProcessEnv = process.env,
) {
if (!options.apply) return
if (options.confirm !== BOM_REFERENCE_SYNC_CONFIRM) {
throw new Error(
`真实写库必须显式传入 --confirm=${BOM_REFERENCE_SYNC_CONFIRM}。`,
)
}
const databaseUrl = env.DATABASE_URL?.trim() ?? ''
if (!databaseUrl) throw new Error('真实写库必须先配置 DATABASE_URL。')
const actualDatabaseIdentity = databaseIdentityFromUrl(databaseUrl)
if (!options.expectedDatabaseIdentity) {
throw new Error(
`真实写库必须显式传入 --expected-database-identity=${actualDatabaseIdentity}。`,
)
}
if (options.expectedDatabaseIdentity !== actualDatabaseIdentity) {
throw new Error('目标 PostgreSQL 身份与 DATABASE_URL 不一致,禁止写库。')
}
const actualSourceIdentity = sourceDatabaseIdentity(env)
if (!actualSourceIdentity) {
throw new Error(
'真实写库必须先配置 BOM_COSTING_UPSTREAM_*、' +
'JST_PLANNING_SQLSERVER_* 或 JST_REPORT_DB_*。',
)
}
if (!options.expectedSourceDatabaseIdentity) {
throw new Error(
`真实写库必须显式传入 --expected-source-database-identity=${actualSourceIdentity}。`,
)
}
if (options.expectedSourceDatabaseIdentity !== actualSourceIdentity) {
throw new Error('来源 SQL Server 身份与当前上游配置不一致,禁止写库。')
}
}
function printHelp() {
console.log(`
Usage:
npm run sync:bom-cost-reference -- --dry-run
npm run sync:bom-cost-reference -- --dry-run --max-pages=2 --start-after-id=0
npm run sync:bom-cost-reference -- --apply --confirm=${BOM_REFERENCE_SYNC_CONFIRM} --expected-database-identity=... --expected-source-database-identity=...
npm run sync:bom-cost-reference -- --apply --resume-batch-id=<failed batch id> --confirm=${BOM_REFERENCE_SYNC_CONFIRM} --expected-database-identity=... --expected-source-database-identity=...
默认只做低压只读预览。默认每页 100 行,并在完整页之间等待 250ms;可用 BOM_COSTING_REFERENCE_PAGE_SIZE / BOM_COSTING_REFERENCE_PAGE_DELAY_MS 做受限调整,不提供并发。
只读预览可用 --max-pages 限定页数、用 --start-after-id 从已记录游标续查;完整页触顶时结果会标记 complete=false。
分页读取默认自动重试 2 次,可用 --max-retries=0..10 调整。apply 按页写入不可见新快照,全部成功后切换;失败批次可用 --resume-batch-id 从持久化游标继续,旧成功快照始终可读。
真实写库还会校验 SQL Server 登录是否没有 INSERT/UPDATE/DELETE/EXECUTE/DDL 权限。
来源配置优先使用 BOM_COSTING_UPSTREAM_*;未设置时优先复用现有
JST_PLANNING_SQLSERVER_* 的 report_reader,最后才兼容 JST_REPORT_DB_*:
BOM_COSTING_UPSTREAM_HOST / DATABASE / USER / PASSWORD
BOM_COSTING_UPSTREAM_SCHEMA=rpt
BOM_COSTING_UPSTREAM_OBJECT=pm_bomBaseInfo
BOM_COSTING_UPSTREAM_ID_COLUMN=Id
`.trim())
}
async function main() {
const options = parseArgs(process.argv.slice(2))
if (options.help) {
printHelp()
return
}
assertApplyGuards(options)
const [
{ readUpstreamBomReferenceConfig, MssqlUpstreamBomReferenceSource },
{ PrismaBomReferenceMirrorStore, PostgresBomReferenceSyncLock },
{ UpstreamBomReferenceSyncService },
{ prisma },
] = await Promise.all([
import('../src/server/services/bom-costing/upstream-bom-reference-source'),
import('../src/server/services/bom-costing/upstream-bom-reference-store'),
import('../src/server/services/bom-costing/upstream-bom-reference-sync.service'),
import('../src/lib/prisma'),
])
const source = new MssqlUpstreamBomReferenceSource(readUpstreamBomReferenceConfig())
const service = new UpstreamBomReferenceSyncService(
source,
new PrismaBomReferenceMirrorStore(prisma),
new PostgresBomReferenceSyncLock(),
)
try {
const result = await service.sync({
apply: options.apply,
allowLargeRemoval: options.allowLargeRemoval,
maxPages: options.maxPages,
maxRetries: options.maxRetries,
startAfterId: options.startAfterId,
resumeBatchId: options.resumeBatchId,
onPage: (progress) => console.log(JSON.stringify({ progress })),
})
console.log(JSON.stringify(result, null, 2))
} finally {
await prisma.$disconnect()
}
}
void main().catch((error) => {
console.error(`BOM reference sync failed: ${error instanceof Error ? error.message : String(error)}`)
process.exitCode = 1
})