Created
January 23, 2018 17:36
-
-
Save atvanguard/810fcec85d4ad116e3267455894e039e to your computer and use it in GitHub Desktop.
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 { Client } from 'pg'; | |
const copyTo = require('pg-copy-streams').to; | |
const copyFrom = require('pg-copy-streams').from; | |
const fromDb = new Client({ | |
host: 'oldDb.rds.amazonaws.com', | |
user: 'username', | |
password: 'password', | |
database: 'api_logs' | |
}); | |
const toDb = new Client({ | |
host: 'newDb.rds.amazonaws.com', | |
user: 'username', | |
password: 'password', | |
database: 'api_logs' | |
}); | |
// query to select the api logs for the month of september | |
let query1 = "COPY (SELECT id, api_key, statuscode, latency, method, time FROM logs WHERE time >= '2017-09-01T00:00:00' AND time < '2017-10-01T00:00:00') TO STDOUT"; | |
// query to map the data retrieved above to specific columns | |
let query2 = "COPY logs_201709 (id, api_key, statuscode, latency, method, time) FROM STDIN"; | |
fromDb.connect().then(() => toDb.connect()).then(() => { | |
let fromStream = fromDb.query(copyTo(query1)); | |
let toStream = toDb.query(copyFrom(query2)); | |
// pipe data from source to destination DB | |
fromStream.pipe(toStream); | |
fromStream.on('end', done); | |
fromStream.on('error',done); | |
}); | |
let done = (err) => { | |
if (err) console.log('err', err); | |
console.log('Completed'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment