Created
November 15, 2023 16:56
-
-
Save darioielardi/ac516533fee0805dcb795ce53315de12 to your computer and use it in GitHub Desktop.
tour booking price data migration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pg from 'pg'; | |
const dbHost = process.env.DB_HOST; | |
const dbPort = process.env.DB_PORT; | |
const dbUser = process.env.DB_USER; | |
const dbPassword = process.env.DB_PASSWORD; | |
const country = process.env.COUNTRY; | |
const { Client } = pg; | |
const dbUrl = `postgresql://${dbUser}:${dbPassword}@${dbHost}:${dbPort}`; | |
const buynana = new Client(`${dbUrl}/api_buynana_co`); | |
const booking = new Client(`${dbUrl}/api_booking_${country}`); | |
async function main() { | |
await Promise.all([buynana.connect(), booking.connect()]); | |
const tours = await booking.query<{ id: string; price: number }>(` | |
SELECT t.id, si.price from tour t join sellableitem si on t."sellableItem" = si.id; | |
`); | |
console.log(`Found ${tours.rowCount} tours for country "${country}"`); | |
const now = new Date(); | |
const result = await buynana.query(` | |
UPDATE tour | |
SET "priceForPax" = CASE | |
${tours.rows | |
.map(tour => `WHEN id = '${tour.id}' THEN ${tour.price}`) | |
.join('\n')} | |
ELSE "priceForPax" | |
END | |
WHERE "id" IN (${tours.rows.map(tour => `'${tour.id}'`).join(',')}); | |
`); | |
console.log( | |
`Updated ${result.rowCount} tours in ${ | |
new Date().getTime() - now.getTime() | |
}ms` | |
); | |
} | |
await main() | |
.then(() => process.exit(0)) | |
.catch(err => { | |
console.error(err); | |
process.exit(1); | |
}) | |
.finally(async () => { | |
await Promise.all([buynana.end(), booking.end()]); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment