来源仓库: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 经哈希比对与线上同值), 整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。 供应商系统后续开发自本提交开始。
142 lines
4.4 KiB
JavaScript
142 lines
4.4 KiB
JavaScript
#!/usr/bin/env node
|
|
/**
|
|
* 清空商品管理数据(skus + product_catalog 商品行),保留分类,便于重新导入。
|
|
*
|
|
* 用法:
|
|
* node prod-purge-skus.mjs --dry-run
|
|
* node prod-purge-skus.mjs --execute
|
|
* node prod-purge-skus.mjs --execute --branch BR001
|
|
*/
|
|
import pg from 'pg';
|
|
import dotenv from 'dotenv';
|
|
import path from 'path';
|
|
import { fileURLToPath } from 'url';
|
|
|
|
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
dotenv.config({ path: path.join(__dirname, '..', '.env') });
|
|
|
|
const execute = process.argv.includes('--execute');
|
|
const dryRun = !execute;
|
|
const branchArgIdx = process.argv.indexOf('--branch');
|
|
const branchCode = branchArgIdx >= 0 ? process.argv[branchArgIdx + 1] : null;
|
|
|
|
/** 先删引用 skus 的子表,再删商品本体 */
|
|
const SKU_DEPENDENT_TABLES = [
|
|
'picking_scan_logs',
|
|
'picking_items',
|
|
'inspection_returns',
|
|
'inspection_loss_confirmations',
|
|
'processing_logs',
|
|
'processing_tasks',
|
|
'inspections',
|
|
'product_substitutions',
|
|
'bid_prices',
|
|
'customer_quote_sheets',
|
|
'customer_quote_versions',
|
|
'customer_skus',
|
|
'customer_sku_aliases',
|
|
'customer_image_aliases',
|
|
'sku_suppliers',
|
|
'sku_aliases',
|
|
'inventory_logs',
|
|
'inventory',
|
|
'purchase_orders',
|
|
'order_items',
|
|
'supplier_preparations',
|
|
'market_prices',
|
|
];
|
|
|
|
async function countRows(client, sql, params = []) {
|
|
const { rows } = await client.query(sql, params);
|
|
return Number(rows[0]?.c ?? 0);
|
|
}
|
|
|
|
async function main() {
|
|
const pool = new pg.Pool({ connectionString: process.env.DATABASE_URL });
|
|
const client = await pool.connect();
|
|
|
|
try {
|
|
const skuFilter = branchCode ? 'WHERE branch_code = $1' : '';
|
|
const skuParams = branchCode ? [branchCode] : [];
|
|
|
|
const counts = {
|
|
skus: await countRows(client, `SELECT COUNT(*)::int AS c FROM skus ${skuFilter}`, skuParams),
|
|
product_catalog_products: await countRows(
|
|
client,
|
|
`SELECT COUNT(*)::int AS c FROM product_catalog WHERE item_type = 'product' ${branchCode ? 'AND branch_code = $1' : ''}`,
|
|
skuParams
|
|
),
|
|
order_items: await countRows(client, `SELECT COUNT(*)::int AS c FROM order_items`),
|
|
customer_skus: await countRows(client, `SELECT COUNT(*)::int AS c FROM customer_skus`),
|
|
};
|
|
|
|
console.log(JSON.stringify({ dryRun, branchCode, before: counts }, null, 2));
|
|
|
|
if (dryRun) {
|
|
console.log('\n[dry-run] 未执行删除。确认后加 --execute');
|
|
return;
|
|
}
|
|
|
|
await client.query('BEGIN');
|
|
await client
|
|
.query('ALTER TABLE product_catalog DISABLE TRIGGER trg_sync_product_catalog_to_legacy')
|
|
.catch(() => {});
|
|
|
|
const deleted = {};
|
|
for (const table of SKU_DEPENDENT_TABLES) {
|
|
const exists = await client.query(
|
|
`SELECT 1 FROM information_schema.tables WHERE table_schema = 'public' AND table_name = $1`,
|
|
[table]
|
|
);
|
|
if (!exists.rowCount) continue;
|
|
const result = await client.query(`DELETE FROM ${table}`);
|
|
deleted[table] = result.rowCount;
|
|
}
|
|
|
|
if (branchCode) {
|
|
deleted.skus = (
|
|
await client.query(`DELETE FROM skus WHERE branch_code = $1`, [branchCode])
|
|
).rowCount;
|
|
deleted.product_catalog_products = (
|
|
await client.query(
|
|
`DELETE FROM product_catalog WHERE item_type = 'product' AND branch_code = $1`,
|
|
[branchCode]
|
|
)
|
|
).rowCount;
|
|
} else {
|
|
deleted.skus = (await client.query(`DELETE FROM skus`)).rowCount;
|
|
deleted.product_catalog_products = (
|
|
await client.query(`DELETE FROM product_catalog WHERE item_type = 'product'`)
|
|
).rowCount;
|
|
}
|
|
|
|
await client
|
|
.query('ALTER TABLE product_catalog ENABLE TRIGGER trg_sync_product_catalog_to_legacy')
|
|
.catch(() => {});
|
|
await client.query('COMMIT');
|
|
|
|
const after = {
|
|
skus: await countRows(client, `SELECT COUNT(*)::int AS c FROM skus ${skuFilter}`, skuParams),
|
|
product_catalog_products: await countRows(
|
|
client,
|
|
`SELECT COUNT(*)::int AS c FROM product_catalog WHERE item_type = 'product' ${branchCode ? 'AND branch_code = $1' : ''}`,
|
|
skuParams
|
|
),
|
|
};
|
|
|
|
console.log(JSON.stringify({ deleted, after }, null, 2));
|
|
console.log('\n商品数据已清空,可在「商品管理」重新导入。');
|
|
} catch (error) {
|
|
await client.query('ROLLBACK').catch(() => {});
|
|
throw error;
|
|
} finally {
|
|
client.release();
|
|
await pool.end();
|
|
}
|
|
}
|
|
|
|
main().catch((error) => {
|
|
console.error(error);
|
|
process.exit(1);
|
|
});
|