Created
October 18, 2023 17:33
-
-
Save darioielardi/f3a02ac89f81b724e64937644194d7e1 to your computer and use it in GitHub Desktop.
tours publishedAt 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 { Client } = pg; | |
const dbUrl = 'postgresql://USERNAME:PASSWORD@127.0.0.1:15432'; | |
const buynana = new Client(`${dbUrl}/api_buynana_co`); | |
const coordinators = new Client(`${dbUrl}/api_coordinators_co`); | |
async function main() { | |
await Promise.all([buynana.connect(), coordinators.connect()]); | |
const tours = await coordinators.query('SELECT id, "createdAt" FROM tours;'); | |
console.log(`Found ${tours.rows.length} tours in coordinators database.`); | |
const now = new Date(); | |
// for each corresponding tour in the buynana database update publishedAt | |
// with the value of createdAt from the coordinators database | |
const result = await buynana.query(` | |
UPDATE tour | |
SET "publishedAt" = CASE | |
${tours.rows | |
.map( | |
(tour) => | |
`WHEN id = '${tour.id}' THEN '${tour.createdAt.toISOString()}'` | |
) | |
.join('\n')} | |
ELSE "publishedAt" | |
END; | |
`); | |
console.log(`Updated tours: ${result.rowCount} - ( ${new Date() - now} ms )`); | |
} | |
await main() | |
.then(() => { | |
process.exit(0); | |
}) | |
.catch((error) => { | |
console.error(error); | |
process.exit(1); | |
}) | |
.finally(async () => { | |
await Promise.all([buynana.end(), coordinators.end()]); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment