ensure-lobster-channel-table.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. const mysql = require('mysql2/promise')
  2. const PLUGIN_TABLE_SQL = `
  3. CREATE TABLE IF NOT EXISTS lobster_channel_plugin_config (
  4. id BIGINT AUTO_INCREMENT PRIMARY KEY,
  5. company_id BIGINT NOT NULL COMMENT 'tenant-level config uses 0',
  6. channel_type VARCHAR(30) NOT NULL,
  7. enabled TINYINT DEFAULT 0,
  8. config_json TEXT,
  9. create_time DATETIME DEFAULT CURRENT_TIMESTAMP,
  10. update_time DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  11. UNIQUE KEY uk_company_channel (company_id, channel_type)
  12. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='Lobster channel plugin config'
  13. `
  14. const GRANT_TABLE_SQL = `
  15. CREATE TABLE IF NOT EXISTS lobster_tenant_channel_grant (
  16. id BIGINT AUTO_INCREMENT PRIMARY KEY,
  17. tenant_id BIGINT NOT NULL,
  18. channel_type VARCHAR(30) NOT NULL,
  19. granted TINYINT DEFAULT 1,
  20. create_by VARCHAR(64) DEFAULT '',
  21. create_time DATETIME DEFAULT CURRENT_TIMESTAMP,
  22. update_by VARCHAR(64) DEFAULT '',
  23. update_time DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  24. UNIQUE KEY uk_tenant_channel (tenant_id, channel_type),
  25. KEY idx_tenant (tenant_id)
  26. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='Lobster channel grants by platform admin'
  27. `
  28. const DB_CFG = {
  29. host: 'cq-cdb-8fjmemkb.sql.tencentcdb.com',
  30. port: 27220,
  31. user: 'root',
  32. password: 'Ylrz_1q2w3e4r5t6y',
  33. multipleStatements: true
  34. }
  35. function dbNameFromUrl(url) {
  36. if (!url) return null
  37. const m = String(url).match(/\/([^/?]+)(?:\?|$)/)
  38. return m ? m[1] : null
  39. }
  40. async function ensureTables(database) {
  41. const conn = await mysql.createConnection({ ...DB_CFG, database })
  42. await conn.query(PLUGIN_TABLE_SQL)
  43. await conn.query(GRANT_TABLE_SQL)
  44. const [p] = await conn.query("SHOW TABLES LIKE 'lobster_channel_plugin_config'")
  45. const [g] = await conn.query("SHOW TABLES LIKE 'lobster_tenant_channel_grant'")
  46. await conn.end()
  47. return { database, plugin: p.length > 0, grant: g.length > 0 }
  48. }
  49. async function main() {
  50. const masterConn = await mysql.createConnection({ ...DB_CFG, database: 'ylrz_saas' })
  51. const dbs = new Set(['ylrz_saas'])
  52. try {
  53. const [tenants] = await masterConn.query(
  54. 'SELECT id, tenant_code, db_url FROM tenant_info WHERE db_url IS NOT NULL AND db_url != \'\''
  55. )
  56. for (const t of tenants) {
  57. const name = dbNameFromUrl(t.db_url)
  58. if (name) dbs.add(name)
  59. }
  60. } catch (e) {
  61. console.warn('tenant_info query skipped:', e.message)
  62. }
  63. await masterConn.end()
  64. for (const db of dbs) {
  65. try {
  66. const r = await ensureTables(db)
  67. console.log(`${db}: plugin=${r.plugin ? 'OK' : 'MISSING'} grant=${r.grant ? 'OK' : 'MISSING'}`)
  68. } catch (e) {
  69. console.error(`${db}: ERROR ${e.message}`)
  70. }
  71. }
  72. }
  73. main().catch(e => {
  74. console.error(e)
  75. process.exit(1)
  76. })