drop-lobster-execution-config-menu.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. const mysql = require('mysql2/promise')
  2. const DB_CFG = {
  3. host: process.env.DB_HOST || 'cq-cdb-8fjmemkb.sql.tencentcdb.com',
  4. port: Number(process.env.DB_PORT || 27220),
  5. user: process.env.DB_USER || 'root',
  6. password: process.env.DB_PASSWORD || 'Ylrz_1q2w3e4r5t6y',
  7. multipleStatements: true
  8. }
  9. const MASTER_DB = process.env.MASTER_DB || 'ylrz_saas'
  10. function dbNameFromUrl(url) {
  11. if (!url) return null
  12. const m = String(url).match(/\/([^/?]+)(?:\?|$)/)
  13. return m ? m[1] : null
  14. }
  15. async function runDrop(conn, label) {
  16. const tables = ['sys_role_menu', 'company_role_menu', 'company_menu', 'sys_menu']
  17. for (const table of tables) {
  18. const [exists] = await conn.query('SHOW TABLES LIKE ?', [table])
  19. if (!exists.length) continue
  20. if (table.includes('role_menu')) {
  21. await conn.query(`DELETE FROM ${table} WHERE menu_id = 29947`)
  22. } else {
  23. await conn.query(
  24. `DELETE FROM ${table} WHERE menu_id = 29947 OR path = 'execution-config' OR component = 'lobster/execution-config/index'`
  25. )
  26. }
  27. }
  28. const count = async (table, where) => {
  29. const [exists] = await conn.query('SHOW TABLES LIKE ?', [table])
  30. if (!exists.length) return 'NA'
  31. const [rows] = await conn.query(`SELECT COUNT(*) AS c FROM ${table} WHERE ${where}`)
  32. return rows[0].c
  33. }
  34. console.log(
  35. `${label}: sys_menu=${await count('sys_menu', "menu_id = 29947 OR path = 'execution-config'")}, ` +
  36. `company_menu=${await count('company_menu', "menu_id = 29947 OR path = 'execution-config'")}`
  37. )
  38. }
  39. async function runMasterTemplateDrop(conn) {
  40. const tables = ['tenant_sys_role_menu', 'tenant_company_role_menu', 'tenant_company_menu', 'tenant_sys_menu']
  41. for (const table of tables) {
  42. const [exists] = await conn.query('SHOW TABLES LIKE ?', [table])
  43. if (!exists.length) continue
  44. if (table.includes('role_menu')) {
  45. await conn.query(`DELETE FROM ${table} WHERE menu_id = 29947`)
  46. } else {
  47. await conn.query(
  48. `DELETE FROM ${table} WHERE menu_id = 29947 OR path = 'execution-config' OR component = 'lobster/execution-config/index'`
  49. )
  50. }
  51. }
  52. const count = async (table, where) => {
  53. const [exists] = await conn.query('SHOW TABLES LIKE ?', [table])
  54. if (!exists.length) return 'NA'
  55. const [rows] = await conn.query(`SELECT COUNT(*) AS c FROM ${table} WHERE ${where}`)
  56. return rows[0].c
  57. }
  58. console.log(
  59. `master templates: tenant_sys_menu=${await count('tenant_sys_menu', "menu_id = 29947 OR path = 'execution-config'")}, ` +
  60. `tenant_company_menu=${await count('tenant_company_menu', "menu_id = 29947 OR path = 'execution-config'")}`
  61. )
  62. }
  63. async function main() {
  64. const masterConn = await mysql.createConnection({ ...DB_CFG, database: MASTER_DB })
  65. const dbs = new Set([MASTER_DB])
  66. try {
  67. await runMasterTemplateDrop(masterConn)
  68. } catch (e) {
  69. console.warn(`master template drop skipped: ${e.message}`)
  70. }
  71. try {
  72. const [tenants] = await masterConn.query(
  73. 'SELECT id, tenant_code, db_url FROM tenant_info WHERE db_url IS NOT NULL AND db_url != \'\''
  74. )
  75. for (const t of tenants) {
  76. const name = dbNameFromUrl(t.db_url)
  77. if (name) dbs.add(name)
  78. }
  79. } catch (e) {
  80. console.warn('tenant_info query skipped:', e.message)
  81. }
  82. await masterConn.end()
  83. for (const db of dbs) {
  84. const conn = await mysql.createConnection({ ...DB_CFG, database: db })
  85. try {
  86. const [hasSys] = await conn.query("SHOW TABLES LIKE 'sys_menu'")
  87. const [hasCom] = await conn.query("SHOW TABLES LIKE 'company_menu'")
  88. if (!hasSys.length && !hasCom.length) {
  89. console.log(`${db}: skip (no tenant menu tables)`)
  90. continue
  91. }
  92. await runDrop(conn, db)
  93. } catch (e) {
  94. console.error(`${db}: ERROR ${e.message}`)
  95. } finally {
  96. await conn.end()
  97. }
  98. }
  99. }
  100. main().catch(err => {
  101. console.error(err)
  102. process.exit(1)
  103. })