import { createHash } from 'node:crypto'; import { execFileSync } from 'node:child_process'; const apply = process.argv.includes('--apply'); const tenant = process.env.AIHR_TENANT_ID || '000000'; const sql = `insert ignore into aihr_knowledge_fragment_locator (tenant_id, fragment_id, knowledge_id, doc_id, attachment_id, source_kind, paragraph_start, paragraph_end, locator_version, create_time, update_time) select f.tenant_id,f.id,f.knowledge_id,f.doc_id,a.id, case when lower(a.name) regexp '\\\\.(pdf)$' then 'PDF' when lower(a.name) regexp '\\\\.(doc|docx)$' then 'DOCX' when lower(a.name) regexp '\\\\.(ppt|pptx)$' then 'PPTX' when lower(a.name) regexp '\\\\.(xls|xlsx)$' then 'XLSX' when lower(a.name) regexp '\\\\.(png|jpg|jpeg|gif|webp|bmp)$' then 'IMAGE' when lower(a.name) regexp '\\\\.(mp4|mov|avi|mkv|webm|m4v)$' then 'VIDEO' else 'TEXT' end,f.idx,f.idx,'backfill-v1',now(),now() from aihr_knowledge_fragment f join aihr_knowledge_attach a on a.tenant_id=f.tenant_id and a.knowledge_id=f.knowledge_id and a.doc_id=f.doc_id and a.status=2 left join aihr_knowledge_fragment_locator l on l.tenant_id=f.tenant_id and l.fragment_id=f.id where f.tenant_id='${tenant.replaceAll("'", "")}' and l.id is null order by f.id limit 1000;`; const hash = createHash('sha256').update(sql).digest('hex'); if (!apply) { console.log(JSON.stringify({ mode: 'plan', tenant, authorization: `MIGRATE_DB:${hash}`, writes: false }, null, 2)); process.exit(0); } if (process.env.AIHR_MIGRATE_AUTH !== `MIGRATE_DB:${hash}`) throw new Error('missing exact MIGRATE_DB authorization'); execFileSync('docker', ['exec', '-i', 'wygj-mysql', 'mysql', '-uroot', '-proot', '--default-character-set=utf8mb4', 'ry-vue', '-e', sql], { stdio: 'inherit' });