Skip to content

Commit 860babc

Browse files
authored
Merge pull request #17 from jameshy/support-pgdump-arguments
add support for pg_dump command arguments
2 parents ae4b5e3 + 2cf12ab commit 860babc

File tree

4 files changed

+83
-11
lines changed

4 files changed

+83
-11
lines changed

lib/config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const path = require('path')
22

3+
// default config that is overridden by the Lambda event
34
module.exports = {
45
S3_REGION: 'eu-west-1',
56
PGDUMP_PATH: path.join(__dirname, '../bin/postgres-11.6'),

lib/handler.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ async function handler(event) {
3939
}
4040
catch (error) {
4141
// log the error and rethrow for Lambda
42-
if (process.env.NODE_ENV !== "test") {
42+
if (process.env.NODE_ENV !== 'test') {
4343
console.error(error)
4444
}
4545
throw error

lib/pgdump.js

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,47 @@ const through2 = require('through2')
33
const path = require('path')
44
const fs = require('fs')
55

6-
function spawnPgDump(config) {
6+
function spawnPgDump(pgdumpDir, args, env) {
77
const pgDumpPath = path.join(
8-
config.PGDUMP_PATH,
8+
pgdumpDir,
99
'pg_dump'
1010
)
1111
if (!fs.existsSync(pgDumpPath)) {
1212
throw new Error('pg_dump not found at ' + pgDumpPath)
1313
}
14-
const env = { ...config, LD_LIBRARY_PATH: config.PGDUMP_PATH }
15-
return spawn(pgDumpPath, ['-Fc', '-Z 1'], {
14+
15+
return spawn(pgDumpPath, args, {
1616
env
1717
})
1818
}
1919

20+
function buildArgs(config) {
21+
let args = ['-Fc', '-Z1']
22+
const extraArgs = config.PGDUMP_ARGS
23+
24+
if (typeof extraArgs === 'string') {
25+
const splitArgs = extraArgs.split(' ')
26+
args = args.concat(splitArgs)
27+
}
28+
else if (Array.isArray(extraArgs)) {
29+
args = args.concat(extraArgs)
30+
}
31+
32+
return args
33+
}
34+
2035
function pgdump(config, pgDumpSpawnFn = spawnPgDump) {
2136
return new Promise((resolve, reject) => {
2237
let headerChecked = false
2338
let stderr = ''
2439

2540
// spawn pg_dump process
26-
const process = pgDumpSpawnFn(config)
41+
const args = buildArgs(config)
42+
const env = { ...config, LD_LIBRARY_PATH: config.PGDUMP_PATH }
43+
const process = pgDumpSpawnFn(config.PGDUMP_PATH, args, env)
2744

2845
// hook into the process
29-
process.stderr.on('data', (data) => {
46+
process.stderr.on('data', data => {
3047
stderr += data.toString('utf8')
3148
})
3249

test/pgdump.js

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const fs = require('fs')
33
const mockSpawn = require('mock-spawn')
44
const chai = require('chai')
55
const chaiAsPromised = require('chai-as-promised')
6+
const sinon = require('sinon')
67

78
chai.use(chaiAsPromised)
89
const { expect } = chai
@@ -27,14 +28,67 @@ describe('pgdump', () => {
2728
)
2829
})
2930

31+
it('should call pg_dump with some default args', async () => {
32+
const pgdumpProcess = mockSpawn()()
33+
const pgDumpFn = sinon.fake.returns(pgdumpProcess)
34+
const config = {}
35+
const p = pgdump(config, pgDumpFn)
36+
pgdumpProcess.stdout.write('PGDMP - data - data')
37+
pgdumpProcess.emit('close', 0)
38+
await p
39+
40+
expect(pgDumpFn.calledOnce).to.be.true
41+
const pgDumpArgs = pgDumpFn.getCall(0).args[1]
42+
expect(pgDumpArgs).to.deep.equal(['-Fc', '-Z1'])
43+
})
44+
45+
it('should call pg_dump with provided extra arguments as array', async () => {
46+
const pgdumpProcess = mockSpawn()()
47+
const pgDumpFn = sinon.fake.returns(pgdumpProcess)
48+
const config = {
49+
PGDUMP_ARGS: ['--exclude-table=ignored-table', '-N', 'public']
50+
}
51+
const p = pgdump(config, pgDumpFn)
52+
pgdumpProcess.stdout.write('PGDMP - data - data')
53+
pgdumpProcess.emit('close', 0)
54+
await p
55+
56+
expect(pgDumpFn.calledOnce).to.be.true
57+
const pgDumpArgs = pgDumpFn.getCall(0).args[1]
58+
59+
expect(
60+
pgDumpArgs
61+
).to.deep.equal(['-Fc', '-Z1', '--exclude-table=ignored-table', '-N', 'public'])
62+
})
63+
64+
it('should call pg_dump with provided extra arguments as string', async () => {
65+
const pgdumpProcess = mockSpawn()()
66+
const pgDumpFn = sinon.fake.returns(pgdumpProcess)
67+
const config = {
68+
PGDUMP_ARGS: '--exclude-table=ignored-table -N public'
69+
}
70+
71+
const p = pgdump(config, pgDumpFn)
72+
pgdumpProcess.stdout.write('PGDMP - data - data')
73+
pgdumpProcess.emit('close', 0)
74+
await p
75+
76+
expect(pgDumpFn.calledOnce).to.be.true
77+
const pgDumpArgs = pgDumpFn.getCall(0).args[1]
78+
79+
expect(
80+
pgDumpArgs
81+
).to.deep.equal(['-Fc', '-Z1', '--exclude-table=ignored-table', '-N', 'public'])
82+
})
83+
3084
it('should stream correctly', async () => {
31-
const mySpawn = mockSpawn()()
85+
const pgdumpProcess = mockSpawn()()
3286

33-
const pgDumpFn = () => mySpawn
87+
const pgDumpFn = () => pgdumpProcess
3488
const config = {}
3589
const p = pgdump(config, pgDumpFn)
36-
mySpawn.stdout.write('PGDMP - data - data')
37-
mySpawn.emit('close', 0)
90+
pgdumpProcess.stdout.write('PGDMP - data - data')
91+
pgdumpProcess.emit('close', 0)
3892

3993
const buffer = await p
4094

0 commit comments

Comments
 (0)