apply-execution-config-dedup-columns.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. const mysql = require('mysql2/promise')
  2. const DB = process.argv[2] || 'fs_tenant_cs1'
  3. const cfg = {
  4. host: 'cq-cdb-8fjmemkb.sql.tencentcdb.com',
  5. port: 27220,
  6. user: 'root',
  7. password: 'Ylrz_1q2w3e4r5t6y',
  8. database: DB,
  9. charset: 'utf8mb4',
  10. multipleStatements: true
  11. }
  12. const COLUMNS = [
  13. {
  14. name: 'message_dedup_enabled',
  15. sql: "ADD COLUMN message_dedup_enabled tinyint NOT NULL DEFAULT 1 COMMENT 'message dedup enabled' AFTER quality_multiturn_min_avg_score_percent"
  16. },
  17. {
  18. name: 'message_dedup_exact_window_size',
  19. sql: "ADD COLUMN message_dedup_exact_window_size int NOT NULL DEFAULT 5 COMMENT 'exact dedup window size' AFTER message_dedup_enabled"
  20. },
  21. {
  22. name: 'message_dedup_semantic_threshold',
  23. sql: "ADD COLUMN message_dedup_semantic_threshold decimal(4,2) NOT NULL DEFAULT 0.85 COMMENT 'semantic threshold' AFTER message_dedup_exact_window_size"
  24. },
  25. {
  26. name: 'message_dedup_window_duration_seconds',
  27. sql: "ADD COLUMN message_dedup_window_duration_seconds int NOT NULL DEFAULT 300 COMMENT 'dedup window seconds' AFTER message_dedup_semantic_threshold"
  28. }
  29. ]
  30. async function colExists(c, col) {
  31. const [r] = await c.query(
  32. 'SELECT COUNT(*) cnt FROM information_schema.COLUMNS WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME=? AND COLUMN_NAME=?',
  33. ['lobster_execution_config', col]
  34. )
  35. return r[0].cnt > 0
  36. }
  37. async function tableExists(c) {
  38. const [r] = await c.query(
  39. "SELECT COUNT(*) cnt FROM information_schema.TABLES WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='lobster_execution_config'"
  40. )
  41. return r[0].cnt > 0
  42. }
  43. async function main() {
  44. const c = await mysql.createConnection(cfg)
  45. console.log('Patch lobster_execution_config message_dedup columns on', DB)
  46. if (!(await tableExists(c))) {
  47. console.error('Table lobster_execution_config not found')
  48. process.exit(1)
  49. }
  50. for (const col of COLUMNS) {
  51. if (await colExists(c, col.name)) {
  52. console.log(' skip', col.name, '(exists)')
  53. } else {
  54. await c.query(`ALTER TABLE lobster_execution_config ${col.sql}`)
  55. console.log(' added', col.name)
  56. }
  57. }
  58. const [cols] = await c.query(
  59. "SELECT COLUMN_NAME FROM information_schema.COLUMNS WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='lobster_execution_config' AND COLUMN_NAME LIKE 'message_dedup%' ORDER BY ORDINAL_POSITION"
  60. )
  61. console.log('message_dedup columns:', cols.map(r => r.COLUMN_NAME).join(', '))
  62. await c.end()
  63. }
  64. main().catch(e => { console.error(e); process.exit(1) })