cleanup-dead-fields.ts 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /**
  2. * 删除前后端均不使用的死字段
  3. *
  4. * GroupChat: guid, lastActivityAt, hasDocument (avatarUrl 已在 test-delete-field 中删除)
  5. * Store: managerId, managerName, status
  6. *
  7. * 用法: cd backend && npx tsx scripts/cleanup-dead-fields.ts
  8. */
  9. import 'dotenv/config';
  10. import Parse from '../src/db/parse-client.js';
  11. interface CleanupTask {
  12. className: string;
  13. fields: string[];
  14. }
  15. const TASKS: CleanupTask[] = [
  16. {
  17. className: 'GroupChat',
  18. fields: ['guid', 'lastActivityAt', 'hasDocument'],
  19. },
  20. {
  21. className: 'Store',
  22. fields: ['managerId', 'managerName', 'status'],
  23. },
  24. ];
  25. async function main(): Promise<void> {
  26. console.log('=== 清理死字段 ===\n');
  27. // 先打印当前 Schema
  28. const beforeSchemas = await Parse.Schema.all({ useMasterKey: true });
  29. for (const task of TASKS) {
  30. const before = beforeSchemas.find((s: any) => s.className === task.className);
  31. if (!before) {
  32. console.log(`⚠️ ${task.className} 表不存在,跳过`);
  33. continue;
  34. }
  35. const existingFields = Object.keys(before.fields).filter((k: string) => !k.startsWith('_'));
  36. const deletable = task.fields.filter(f => existingFields.includes(f));
  37. if (deletable.length === 0) {
  38. console.log(`${task.className}: 目标字段均不存在,跳过`);
  39. continue;
  40. }
  41. const schema = new Parse.Schema(task.className);
  42. for (const field of deletable) {
  43. schema.deleteField(field);
  44. }
  45. await schema.update({ useMasterKey: true });
  46. console.log(`${task.className}: ✅ 已删除 ${deletable.join(', ')}`);
  47. }
  48. // 验证
  49. console.log('\n--- 验证 ---');
  50. const afterSchemas = await Parse.Schema.all({ useMasterKey: true });
  51. for (const task of TASKS) {
  52. const after = afterSchemas.find((s: any) => s.className === task.className);
  53. if (!after) continue;
  54. const remaining = Object.keys(after.fields).filter((k: string) => !k.startsWith('_')).sort();
  55. const stillThere = task.fields.filter(f => remaining.includes(f));
  56. console.log(`${task.className}: ${stillThere.length > 0 ? '❌ 残留 ' + stillThere.join(', ') : '✅ 全部清除'} — 现有字段: ${remaining.join(', ')}`);
  57. }
  58. console.log('\n✅ 清理完成!');
  59. }
  60. main().catch((err) => {
  61. console.error('❌ 失败:', err);
  62. process.exit(1);
  63. });