platform-codebase/features/platform-analytics/backend-api/scripts/sync/diff-attrs.ts
2026-03-19 22:55:41 -07:00

48 lines
1.5 KiB
TypeScript

import { log, httpGet } from '../lib/http'
import { loadAttributeDefinitions } from '../lib/data-loader'
const ATTRS_BASE = process.env.ATTRS_URL ?? 'http://localhost:3015'
export async function diffAttrs(): Promise<void> {
log('\n═══ Attribute Sync: Diff ═══')
const fileDefs = await loadAttributeDefinitions()
const fileCodes = new Set(fileDefs.map(d => d.code))
let dbDefs: Array<{ code: string }> = []
try {
dbDefs = await httpGet<Array<{ code: string }>>(`${ATTRS_BASE}/attribute-definitions?entityType=user`)
} catch (err) {
log(` Cannot reach attributes service: ${(err as Error).message}`)
log(` File definitions: ${fileDefs.length}`)
return
}
const dbCodes = new Set(dbDefs.map(d => d.code))
const onlyInFile = fileDefs.filter(d => !dbCodes.has(d.code))
const onlyInDb = dbDefs.filter(d => !fileCodes.has(d.code))
const inBoth = fileDefs.filter(d => dbCodes.has(d.code))
log(` File: ${fileDefs.length} definitions`)
log(` Database: ${dbDefs.length} definitions`)
log(` In both: ${inBoth.length}`)
if (onlyInFile.length > 0) {
log(`\n Only in filesystem (${onlyInFile.length}):`)
for (const d of onlyInFile) {
log(` + ${d.code} (${d.name})`)
}
}
if (onlyInDb.length > 0) {
log(`\n Only in database (${onlyInDb.length}):`)
for (const d of onlyInDb) {
log(` - ${d.code}`)
}
}
if (onlyInFile.length === 0 && onlyInDb.length === 0) {
log(' ✓ Filesystem and database are in sync')
}
}