Advertisement
Guest User

Untitled

a guest
Mar 19th, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.01 KB | None | 0 0
  1. #!/usr/bin/env node
  2.  
  3. const pgp = require('pg-promise')()
  4. const fs = require('fs')
  5. const _ = require('lodash')
  6. const Bluebird = require('bluebird')
  7. const environment = require('../lib/environment')
  8.  
  9. const connection = pgp({
  10. user: environment.postgres.user,
  11. password: environment.postgres.password,
  12. database: 'jellyfish',
  13. port: environment.postgres.port,
  14. host: environment.postgres.host
  15. })
  16.  
  17. const run = async (skip, limit) => {
  18. const results = await connection.any(`
  19. SELECT * FROM cards WHERE linked_at IS NULL OFFSET ${skip} LIMIT ${limit}`)
  20.  
  21. console.log(`Limit ${limit} on Offset ${skip}, got ${results.length} results`)
  22.  
  23. if (results.length === 0) {
  24. await connection.$pool.end()
  25. return
  26. }
  27.  
  28. const ids = results.map((row) => {
  29. return `'${row.id}'`
  30. })
  31.  
  32. console.time('updating')
  33.  
  34. await connection.any(`
  35. UPDATE cards SET linked_at = '{}'::jsonb WHERE id IN (${ids.join(', ')})
  36. `)
  37. console.timeEnd('updating')
  38.  
  39. await run(skip + limit, limit)
  40. }
  41.  
  42. run(0, 100).catch((error) => {
  43. console.error(error)
  44. process.exit(1)
  45. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement