repair-lobster-profile-schema.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /**
  2. * Add lobster_user_profile.channel_type (and related index) if missing.
  3. * Usage: node scripts/repair-lobster-profile-schema.js [dbName]
  4. */
  5. const mysql = require('mysql2/promise')
  6. const DB_CFG = {
  7. host: 'cq-cdb-8fjmemkb.sql.tencentcdb.com',
  8. port: 27220,
  9. user: 'root',
  10. password: 'Ylrz_1q2w3e4r5t6y',
  11. charset: 'utf8mb4',
  12. multipleStatements: true
  13. }
  14. const TARGET_DB = process.argv[2] || 'fs_tenant_cs1'
  15. async function columnExists(conn, table, column) {
  16. const [rows] = await conn.query(
  17. `SELECT COUNT(*) c FROM INFORMATION_SCHEMA.COLUMNS
  18. WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = ? AND COLUMN_NAME = ?`,
  19. [table, column]
  20. )
  21. return rows[0].c > 0
  22. }
  23. async function indexExists(conn, table, indexName) {
  24. const [rows] = await conn.query(
  25. `SELECT COUNT(*) c FROM INFORMATION_SCHEMA.STATISTICS
  26. WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = ? AND INDEX_NAME = ?`,
  27. [table, indexName]
  28. )
  29. return rows[0].c > 0
  30. }
  31. async function repair(conn) {
  32. const [tables] = await conn.query(`SHOW TABLES LIKE 'lobster_user_profile'`)
  33. if (!tables.length) {
  34. console.log('skip: lobster_user_profile table not found')
  35. return
  36. }
  37. const hasChannel = await columnExists(conn, 'lobster_user_profile', 'channel_type')
  38. if (!hasChannel) {
  39. await conn.query(`
  40. ALTER TABLE lobster_user_profile
  41. ADD COLUMN channel_type varchar(20) NOT NULL DEFAULT 'QW' COMMENT 'channel type QW/WX/IM'
  42. AFTER external_user_id
  43. `)
  44. console.log('added column channel_type')
  45. } else {
  46. console.log('column channel_type already exists')
  47. }
  48. if (await indexExists(conn, 'lobster_user_profile', 'uk_user')) {
  49. const [idx] = await conn.query(`SHOW INDEX FROM lobster_user_profile WHERE Key_name = 'uk_user'`)
  50. const cols = idx.map(r => r.Column_name).join(',')
  51. if (cols === 'company_id,external_user_id' || cols === 'company_id,external_user_id,') {
  52. await conn.query('ALTER TABLE lobster_user_profile DROP INDEX uk_user')
  53. console.log('dropped old uk_user')
  54. }
  55. }
  56. if (!(await indexExists(conn, 'lobster_user_profile', 'uk_user'))) {
  57. await conn.query(`
  58. ALTER TABLE lobster_user_profile
  59. ADD UNIQUE KEY uk_user (company_id, channel_type, external_user_id)
  60. `)
  61. console.log('added uk_user (company_id, channel_type, external_user_id)')
  62. } else {
  63. console.log('index uk_user already ok')
  64. }
  65. }
  66. async function main() {
  67. const conn = await mysql.createConnection({ ...DB_CFG, database: TARGET_DB })
  68. console.log('DB:', TARGET_DB)
  69. await repair(conn)
  70. const [cols] = await conn.query(
  71. `SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS
  72. WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'lobster_user_profile'
  73. ORDER BY ORDINAL_POSITION`
  74. )
  75. console.log('columns:', cols.map(r => r.COLUMN_NAME).join(', '))
  76. await conn.end()
  77. }
  78. main().catch(e => { console.error(e); process.exit(1) })