|
| 1 | +/* |
| 2 | + * Copyright (c) 2023, VRAI Labs and/or its affiliates. All rights reserved. |
| 3 | + * |
| 4 | + * This software is licensed under the Apache License, Version 2.0 (the |
| 5 | + * "License") as published by the Apache Software Foundation. |
| 6 | + * |
| 7 | + * You may not use this file except in compliance with the License. You may |
| 8 | + * obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations |
| 14 | + * under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +const libphonenumber = require('libphonenumber-js/max'); |
| 18 | + |
| 19 | +// Update the following credentials before running the script |
| 20 | +const DB_HOST = ""; |
| 21 | +const DB_USER = ""; |
| 22 | +const DB_PASSWORD = ""; |
| 23 | +const DB_NAME = ""; |
| 24 | +const CLIENT = ""; // Use "pg" for PostgreSQL and "mysql2" for MySQL DB |
| 25 | + |
| 26 | +const MIN_POOL_SIZE = 0; |
| 27 | +const MAX_POOL_SIZE = 5; |
| 28 | +const QUERY_TIMEOUT = 60000; |
| 29 | + |
| 30 | +if (!DB_HOST || !CLIENT) { |
| 31 | + console.error('Please update the DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE and CLIENT variables before running the script.'); |
| 32 | + return; |
| 33 | +} |
| 34 | + |
| 35 | +const knex = require('knex')({ |
| 36 | + client: CLIENT, |
| 37 | + connection: { |
| 38 | + host: DB_HOST, |
| 39 | + user: DB_USER, |
| 40 | + password: DB_PASSWORD, |
| 41 | + database: DB_NAME, |
| 42 | + }, |
| 43 | + pool: { min: MIN_POOL_SIZE, max: MAX_POOL_SIZE } |
| 44 | +}); |
| 45 | + |
| 46 | +function getUpdatePromise(table, entry, normalizedPhoneNumber) { |
| 47 | + if (table === 'passwordless_devices') { |
| 48 | + return knex.raw(`UPDATE ${table} SET phone_number = ? WHERE app_id = ? AND tenant_id = ? AND device_id_hash = ?`, [normalizedPhoneNumber, entry.app_id, entry.tenant_id, entry.device_id_hash]).timeout(QUERY_TIMEOUT, { cancel: true }); |
| 49 | + } else if (table === 'passwordless_users') { |
| 50 | + // Since passwordless_users and passwordless_user_to_tenant are consistent. We can update both tables at the same time. For consistency, we will use a transaction. |
| 51 | + return knex.transaction(async trx => { |
| 52 | + await trx.raw(`UPDATE passwordless_users SET phone_number = ? WHERE app_id = ? AND user_id = ?`, [normalizedPhoneNumber, entry.app_id, entry.user_id]).timeout(QUERY_TIMEOUT, { cancel: true }); |
| 53 | + await trx.raw(`UPDATE passwordless_user_to_tenant SET phone_number = ? WHERE app_id = ? AND user_id = ?`, [normalizedPhoneNumber, entry.app_id, entry.user_id]).timeout(QUERY_TIMEOUT, { cancel: true }); |
| 54 | + }); |
| 55 | + } else { |
| 56 | + throw new Error(`Invalid table name: ${table}`); |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +function getNormalizedPhoneNumber(phoneNumber) { |
| 61 | + try { |
| 62 | + return libphonenumber.parsePhoneNumber(phoneNumber, { extract: false }).format('E.164'); |
| 63 | + } catch (error) { |
| 64 | + return null; |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +async function updatePhoneNumbers(table) { |
| 69 | + const batchSize = 1000; |
| 70 | + let offset = 0; |
| 71 | + let totalUpdatedRows = 0; |
| 72 | + |
| 73 | + try { |
| 74 | + let totalRows = await knex.raw(`SELECT COUNT(*) as count FROM ${table} WHERE phone_number is NOT NULL`); |
| 75 | + totalRows = totalRows.rows ? totalRows.rows[0].count : totalRows[0][0].count; |
| 76 | + |
| 77 | + while (true) { |
| 78 | + const entries = await knex.raw(`SELECT * FROM ${table} WHERE phone_number is NOT NULL LIMIT ${batchSize} OFFSET ${offset}`); |
| 79 | + // In PostgreSQL, all rows are returned in `entries.rows`, whereas in MySQL, they can be found in `entries[0]`. |
| 80 | + const rows = entries.rows ? entries.rows : entries[0]; |
| 81 | + |
| 82 | + const batchUpdates = []; |
| 83 | + |
| 84 | + for (const entry of rows) { |
| 85 | + const currentPhoneNumber = entry.phone_number; |
| 86 | + const normalizedPhoneNumber = getNormalizedPhoneNumber(currentPhoneNumber); |
| 87 | + |
| 88 | + if (normalizedPhoneNumber && normalizedPhoneNumber !== currentPhoneNumber) { |
| 89 | + const updatePromise = getUpdatePromise(table, entry, normalizedPhoneNumber); |
| 90 | + batchUpdates.push(updatePromise); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + await Promise.all(batchUpdates); |
| 95 | + |
| 96 | + offset += rows.length; |
| 97 | + totalUpdatedRows += batchUpdates.length; |
| 98 | + |
| 99 | + console.log(`Processed ${offset} out of ${totalRows} rows in table ${table}; ${totalUpdatedRows} rows updated`); |
| 100 | + |
| 101 | + if (rows.length < batchSize) { |
| 102 | + break; |
| 103 | + } |
| 104 | + } |
| 105 | + } catch (error) { |
| 106 | + console.error(`Error normalising phone numbers for table ${table}: Retry running the script and if the error persists after retrying then create an issue at https://github.com/supertokens/supertokens-core/issues`); |
| 107 | + throw error; |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +async function runScript() { |
| 112 | + const tables = ['passwordless_users', 'passwordless_devices']; |
| 113 | + |
| 114 | + try { |
| 115 | + for (const table of tables) { |
| 116 | + await updatePhoneNumbers(table); |
| 117 | + console.log(`\n\n\n`); |
| 118 | + } |
| 119 | + console.log('Finished normalising phone numbers!'); |
| 120 | + } catch (error) { |
| 121 | + console.error(error); |
| 122 | + } finally { |
| 123 | + knex.destroy(); |
| 124 | + } |
| 125 | + |
| 126 | +} |
| 127 | + |
| 128 | +runScript(); |
0 commit comments