来源仓库:jxc/ai-fresh-delivery 来源提交:f0b1a180e2deb7e4673b9d1c8542619f7bc0f73d 快照树: 09f679334ff38f0ff25e907961c94a8223bcaedd(与来源 HEAD 工作树逐字节一致,3801 个文件) 按约定本仓库不带任何历史提交:来源仓库的历史中含真实凭据 (生产 SSH 私钥 deploy/id_rsa_tencent、server/.env、.env.tencent/.env.production/.env.gitea、 deploy/小程序AppSecret.txt,其中 MP_APPSECRET / S3 AccessKey 经哈希比对与线上同值), 整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。 供应商系统后续开发自本提交开始。
119 lines
4.1 KiB
JavaScript
119 lines
4.1 KiB
JavaScript
#!/usr/bin/env node
|
|
/**
|
|
* Broader scan: UPDATE/DELETE where FROM/USING JOIN ON references the target
|
|
* table name or alias.
|
|
*/
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
|
|
function walk(d, acc = []) {
|
|
for (const e of fs.readdirSync(d, { withFileTypes: true })) {
|
|
const p = path.join(d, e.name);
|
|
if (e.isDirectory()) {
|
|
if (['node_modules', 'dist', '.git'].includes(e.name)) continue;
|
|
walk(p, acc);
|
|
} else if (/\.(ts|js|sql|cjs|mjs)$/.test(e.name)) acc.push(p);
|
|
}
|
|
return acc;
|
|
}
|
|
|
|
function chunks(file, text) {
|
|
if (file.endsWith('.sql')) return [{ text, start: 0 }];
|
|
const out = [];
|
|
const re = /`([\s\S]*?)`/g;
|
|
let m;
|
|
while ((m = re.exec(text))) out.push({ text: m[1], start: m.index });
|
|
return out;
|
|
}
|
|
|
|
const roots = ['server/src', 'server/scripts', 'scripts', 'migrations'];
|
|
const files = roots.flatMap((r) => (fs.existsSync(r) ? walk(r) : []));
|
|
const findings = [];
|
|
|
|
function checkStmt(kind, targetRaw, alias, body, meta) {
|
|
const target = targetRaw.includes('.') ? targetRaw.split('.').pop() : targetRaw;
|
|
const usingOrFrom = body.match(
|
|
kind === 'DELETE'
|
|
? /\bUSING\b([\s\S]*?)(?=\bWHERE\b|\bRETURNING\b|$)/i
|
|
: /\bFROM\b([\s\S]*?)(?=\bWHERE\b|\bRETURNING\b|$)/i
|
|
);
|
|
if (!usingOrFrom) return;
|
|
const scope = usingOrFrom[1];
|
|
if (!/\bJOIN\b/i.test(scope)) return;
|
|
|
|
const names = [alias, target].filter(Boolean);
|
|
const onParts = [
|
|
...scope.matchAll(
|
|
/\bON\b([\s\S]*?)(?=\b(?:INNER|LEFT|RIGHT|FULL|CROSS)?\s*JOIN\b|\bWHERE\b|$)/gi
|
|
),
|
|
].map((x) => x[1]);
|
|
|
|
for (const on of onParts) {
|
|
for (const name of names) {
|
|
// alias.col OR target.col (when no alias, table name in ON is often invalid)
|
|
const re = new RegExp(`\\b${name}\\.`, 'i');
|
|
if (re.test(on)) {
|
|
findings.push({
|
|
...meta,
|
|
kind,
|
|
target,
|
|
alias: alias || null,
|
|
hit: name,
|
|
sample: on.trim().replace(/\s+/g, ' ').slice(0, 160),
|
|
});
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
for (const file of files) {
|
|
const text = fs.readFileSync(file, 'utf8');
|
|
const isMigration = file.includes(`${path.sep}migrations${path.sep}`) || file.includes('/migrations/');
|
|
const isScript =
|
|
file.includes(`${path.sep}scripts${path.sep}`) || file.includes('/scripts/');
|
|
for (const chunk of chunks(file, text)) {
|
|
const sql = chunk.text;
|
|
const updateRe =
|
|
/UPDATE\s+(?:ONLY\s+)?([a-zA-Z_][\w.]*)(?:\s+([a-zA-Z_]\w*))?\b([\s\S]*?)(?=;|$)/gi;
|
|
let m;
|
|
while ((m = updateRe.exec(sql))) {
|
|
const [, target, aliasMaybe, body] = m;
|
|
// aliasMaybe might be SET — treat SET as no alias
|
|
const alias = aliasMaybe && !/^SET$/i.test(aliasMaybe) ? aliasMaybe : null;
|
|
const rest = alias ? body : `${aliasMaybe || ''} ${body}`;
|
|
if (!/\bFROM\b/i.test(rest) || !/\bJOIN\b/i.test(rest)) continue;
|
|
const line = text.slice(0, chunk.start + m.index).split(/\n/).length;
|
|
checkStmt('UPDATE', target, alias, rest, {
|
|
file: file.replace(/\\/g, '/'),
|
|
line,
|
|
scope: isMigration ? 'migration' : isScript ? 'script' : 'runtime',
|
|
});
|
|
}
|
|
|
|
const deleteRe =
|
|
/DELETE\s+FROM\s+([a-zA-Z_][\w.]*)(?:\s+([a-zA-Z_]\w*))?\b([\s\S]*?)(?=;|$)/gi;
|
|
while ((m = deleteRe.exec(sql))) {
|
|
const [, target, aliasMaybe, body] = m;
|
|
const alias = aliasMaybe && !/^(USING|WHERE)$/i.test(aliasMaybe) ? aliasMaybe : null;
|
|
const rest = alias ? body : `${aliasMaybe || ''} ${body}`;
|
|
if (!/\bUSING\b/i.test(rest) || !/\bJOIN\b/i.test(rest)) continue;
|
|
const line = text.slice(0, chunk.start + m.index).split(/\n/).length;
|
|
checkStmt('DELETE', target, alias, rest, {
|
|
file: file.replace(/\\/g, '/'),
|
|
line,
|
|
scope: isMigration ? 'migration' : isScript ? 'script' : 'runtime',
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
const runtime = findings.filter((f) => f.scope === 'runtime');
|
|
const other = findings.filter((f) => f.scope !== 'runtime');
|
|
console.log('=== RUNTIME ===');
|
|
console.log(JSON.stringify(runtime, null, 2));
|
|
console.log('runtime_count', runtime.length);
|
|
console.log('\n=== MIGRATION/SCRIPT (historical, not live path) ===');
|
|
console.log(JSON.stringify(other, null, 2));
|
|
console.log('other_count', other.length);
|