From d411e3f1aca46418593fa9a413669914a4938247 Mon Sep 17 00:00:00 2001 From: Aranyajit Date: Tue, 30 Jan 2024 20:33:09 +0530 Subject: [PATCH 1/5] process remaining uuids that failed first time --- scripts/process-remaining-ubahn-uuids.js | 63 ++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 scripts/process-remaining-ubahn-uuids.js diff --git a/scripts/process-remaining-ubahn-uuids.js b/scripts/process-remaining-ubahn-uuids.js new file mode 100644 index 0000000..864838c --- /dev/null +++ b/scripts/process-remaining-ubahn-uuids.js @@ -0,0 +1,63 @@ +const models = require('../src/models') +const tcUserId = require('./common/tcUserId') +const Sequelize = require('sequelize') +const _ = require('lodash') +const helper = require('../src/common/helper') + +// read cli arguments, pass the table name +const tableName = process.argv[2] +// read cli arguments, pass the column names to update +const columnNames = process.argv[3] + +const processRemainingUUIDs = async (tableName, columnNames) => { + const dbUrl = process.env.UBAHN_DB_URL + const MODE = process.env.MODE || 'test' + + if (_.isUndefined(dbUrl) || _.isNull(dbUrl)) { + console.log('Ubahn DB URL not set, exiting!') + process.exit(0) + } + + for (const columnName of _.split(columnNames, ',')) { + const query = `SELECT DISTINCT ${columnName} FROM bookings.${tableName} WHERE LENGTH(${columnName}) > 9 AND ${columnName} <> '00000000-0000-0000-0000-000000000000';` + let results = await models.sequelize.query(query, { type: Sequelize.QueryTypes.SELECT }) + + if (results.length > 0) { + results = _.uniq(_.map(_.filter(results, val => toString(val[`${columnName}`]).length > 9), val => val[`${columnName}`])) + console.log(`result: ${JSON.stringify(results)}`) + + const ubahnConn = await tcUserId.getUbahnDatabaseConnection(dbUrl) + const uuidToHandleMap = await tcUserId.getUserUbahnUUIDToHandleMap(ubahnConn, results) + + const handleToIDMap = {} + const batches = _.chunk(Object.values(uuidToHandleMap), 30) + for (const batch of batches) { + const memberAPIRes = await helper.getMemberDetailsByHandles(batch) + _.forEach(memberAPIRes, member => { + handleToIDMap[member.handleLower] = member.userId + }) + } + + let sql = '' + for (const [key, value] of Object.entries(uuidToHandleMap)) { + if (!_.isUndefined(handleToIDMap(value.toLowerCase()))) { + sql += `UPDATE bookings.${tableName} SET ${columnName} = ${handleToIDMap(value)} WHERE ${columnName} = ${key};` + } + } + console.log(`UPDATE statements: ${sql}`) + if (MODE !== test) { + await models.sequelize.query(sql, { type: Sequelize.QueryTypes.UPDATE }) + } + } else { + console.log(`No data eligible to be updated for table: ${tableName} against column ${columnName}`) + } + } +} + +processRemainingUUIDs(tableName, columnNames).then(res => { + console.log(`Processed remaining records for model '${tableName}' against columns: ${columnNames}`) + process.exit(0) +}).catch(err => { + console.error(`${JSON.stringify(err)}`) + process.exit(1) +}) From 7714d432d099ced98cdec52eb698c78242712a45 Mon Sep 17 00:00:00 2001 From: Aranyajit Date: Tue, 30 Jan 2024 20:36:59 +0530 Subject: [PATCH 2/5] fix typo resulting in bug --- scripts/process-remaining-ubahn-uuids.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/process-remaining-ubahn-uuids.js b/scripts/process-remaining-ubahn-uuids.js index 864838c..b59a3f5 100644 --- a/scripts/process-remaining-ubahn-uuids.js +++ b/scripts/process-remaining-ubahn-uuids.js @@ -40,7 +40,7 @@ const processRemainingUUIDs = async (tableName, columnNames) => { let sql = '' for (const [key, value] of Object.entries(uuidToHandleMap)) { - if (!_.isUndefined(handleToIDMap(value.toLowerCase()))) { + if (!_.isUndefined(handleToIDMap[value.toLowerCase()])) { sql += `UPDATE bookings.${tableName} SET ${columnName} = ${handleToIDMap(value)} WHERE ${columnName} = ${key};` } } From 69ba5c8a17ff8cc7ee2dababc64beb3cd463c260 Mon Sep 17 00:00:00 2001 From: Aranyajit Date: Tue, 30 Jan 2024 20:37:57 +0530 Subject: [PATCH 3/5] fix typo resulting in bug --- scripts/process-remaining-ubahn-uuids.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/process-remaining-ubahn-uuids.js b/scripts/process-remaining-ubahn-uuids.js index b59a3f5..261106b 100644 --- a/scripts/process-remaining-ubahn-uuids.js +++ b/scripts/process-remaining-ubahn-uuids.js @@ -41,7 +41,7 @@ const processRemainingUUIDs = async (tableName, columnNames) => { let sql = '' for (const [key, value] of Object.entries(uuidToHandleMap)) { if (!_.isUndefined(handleToIDMap[value.toLowerCase()])) { - sql += `UPDATE bookings.${tableName} SET ${columnName} = ${handleToIDMap(value)} WHERE ${columnName} = ${key};` + sql += `UPDATE bookings.${tableName} SET ${columnName} = ${handleToIDMap[value]} WHERE ${columnName} = ${key};` } } console.log(`UPDATE statements: ${sql}`) From 5e0237b2ece72158159e6e05052ef05a1ed9218c Mon Sep 17 00:00:00 2001 From: Aranyajit Date: Tue, 30 Jan 2024 20:39:13 +0530 Subject: [PATCH 4/5] check lowercase handles --- scripts/process-remaining-ubahn-uuids.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/process-remaining-ubahn-uuids.js b/scripts/process-remaining-ubahn-uuids.js index 261106b..0238fae 100644 --- a/scripts/process-remaining-ubahn-uuids.js +++ b/scripts/process-remaining-ubahn-uuids.js @@ -41,7 +41,7 @@ const processRemainingUUIDs = async (tableName, columnNames) => { let sql = '' for (const [key, value] of Object.entries(uuidToHandleMap)) { if (!_.isUndefined(handleToIDMap[value.toLowerCase()])) { - sql += `UPDATE bookings.${tableName} SET ${columnName} = ${handleToIDMap[value]} WHERE ${columnName} = ${key};` + sql += `UPDATE bookings.${tableName} SET ${columnName} = ${handleToIDMap[value.toLowerCase()]} WHERE ${columnName} = ${key};` } } console.log(`UPDATE statements: ${sql}`) From aded59ce3721dc645aaeea7efca6694bd7d60d00 Mon Sep 17 00:00:00 2001 From: Aranyajit Date: Tue, 30 Jan 2024 21:09:09 +0530 Subject: [PATCH 5/5] surround string with quotes --- scripts/process-remaining-ubahn-uuids.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/process-remaining-ubahn-uuids.js b/scripts/process-remaining-ubahn-uuids.js index 0238fae..ea7de5a 100644 --- a/scripts/process-remaining-ubahn-uuids.js +++ b/scripts/process-remaining-ubahn-uuids.js @@ -41,7 +41,7 @@ const processRemainingUUIDs = async (tableName, columnNames) => { let sql = '' for (const [key, value] of Object.entries(uuidToHandleMap)) { if (!_.isUndefined(handleToIDMap[value.toLowerCase()])) { - sql += `UPDATE bookings.${tableName} SET ${columnName} = ${handleToIDMap[value.toLowerCase()]} WHERE ${columnName} = ${key};` + sql += `UPDATE bookings.${tableName} SET ${columnName} = '${handleToIDMap[value.toLowerCase()]}' WHERE ${columnName} = '${key}';` } } console.log(`UPDATE statements: ${sql}`)