Skip to content

Commit e0af8c6

Browse files
committed
feat: improve scripts
- Support password - Handle custom Docker service name
1 parent a4f0ec9 commit e0af8c6

File tree

6 files changed

+96
-55
lines changed

6 files changed

+96
-55
lines changed

src/cli.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,13 @@ function initConfig(env) {
8282
structurePath: knexScriptsConfig.structurePath || 'db/structure.sql',
8383
migrationsPath: join(process.cwd(), 'migrations'),
8484
docker:
85-
commander.docker !== undefined
85+
(commander.docker !== undefined
8686
? commander.docker
87-
: knexScriptsConfig.docker,
87+
: knexScriptsConfig.docker) || false,
88+
dockerService:
89+
(commander.dockerService !== undefined
90+
? commander.dockerService
91+
: knexScriptsConfig.docker) || 'postgres',
8892
}
8993
}
9094

@@ -95,6 +99,7 @@ function invoke(env) {
9599
chalk`{blue Knex version: {green ${env.modulePackage.version}}}\n`,
96100
)
97101
.option('--docker', 'Use docker.')
102+
.option('--docker-service', 'Docker service name.')
98103
.option('--knexfile [path]', 'Specify the knexfile path.')
99104
.option('--cwd [path]', 'Specify the working directory.')
100105
.option(

src/create.js

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
/* eslint-disable max-len */
22
import { exec } from 'mz/child_process'
3-
import { preventEnv } from './utils'
3+
import {
4+
preventEnv,
5+
wrapDockerCommand,
6+
getCommand,
7+
getCommandEnv,
8+
} from './utils'
49

5-
async function create({ docker, env, knexConfig }) {
6-
preventEnv('production', env)
10+
async function create(options) {
11+
preventEnv('production', options.env)
712

8-
const command = docker
9-
? `docker-compose run postgres createdb --host postgres --username ${
10-
knexConfig.connection.user
11-
} ${knexConfig.connection.database}`
12-
: `createdb --username ${knexConfig.connection.user} ${
13-
knexConfig.connection.database
14-
}`
15-
16-
return exec(command)
13+
const env = getCommandEnv(options)
14+
const command = getCommand(options, 'createdb')
15+
return exec(wrapDockerCommand(options, command), { env })
1716
}
1817

1918
export default create

src/drop.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1+
/* eslint-disable max-len */
12
import { exec } from 'mz/child_process'
2-
import { preventEnv } from './utils'
3+
import {
4+
preventEnv,
5+
wrapDockerCommand,
6+
getCommand,
7+
getCommandEnv,
8+
} from './utils'
39

4-
async function drop({ docker, env, knexConfig }) {
5-
preventEnv('production', env)
10+
async function create(options) {
11+
preventEnv('production', options.env)
612

7-
const command = docker
8-
? `docker-compose run postgres dropdb --host postgres --username ${
9-
knexConfig.connection.user
10-
} ${knexConfig.connection.database} --if-exists`
11-
: `dropdb --username ${knexConfig.connection.user} ${
12-
knexConfig.connection.database
13-
} --if-exists`
14-
15-
return exec(command)
13+
const env = getCommandEnv(options)
14+
const command = getCommand(options, 'dropdb --if-exists')
15+
return exec(wrapDockerCommand(options, command), { env })
1616
}
1717

18-
export default drop
18+
export default create

src/dump.js

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@ import { exec } from 'mz/child_process'
22
import { appendFile, readdir, exists } from 'mz/fs'
33
import { dirname } from 'path'
44
import mkdirp from 'mkdirp'
5-
import { requireEnv } from './utils'
5+
import {
6+
requireEnv,
7+
wrapDockerCommand,
8+
getCommand,
9+
getCommandEnv,
10+
} from './utils'
611

712
async function getMigrationInserts({ migrationsPath }) {
813
if (!await exists(migrationsPath)) return ''
@@ -15,26 +20,20 @@ async function getMigrationInserts({ migrationsPath }) {
1520
.join('')
1621
}
1722

18-
async function dump({
19-
docker,
20-
env,
21-
structurePath,
22-
migrationsPath,
23-
knexConfig,
24-
}) {
25-
requireEnv('development', env)
23+
async function dump(options) {
24+
const { structurePath, migrationsPath } = options
25+
requireEnv('development', options.env)
2626

2727
mkdirp.sync(dirname(structurePath))
2828

29-
const command = docker
30-
? `docker-compose exec postgres pg_dump --schema-only --username ${
31-
knexConfig.connection.user
32-
} ${knexConfig.connection.database} > ${structurePath}`
33-
: `pg_dump --schema-only --username ${knexConfig.connection.user} ${
34-
knexConfig.connection.database
35-
} > ${structurePath}`
29+
const env = getCommandEnv(options)
30+
const command = `${getCommand(
31+
options,
32+
'pg_dump --schema-only',
33+
)} > ${structurePath}`
34+
35+
await exec(wrapDockerCommand(options, command), { env })
3636

37-
await exec(command)
3837
const migrationInserts = await getMigrationInserts({ migrationsPath })
3938
return appendFile(structurePath, `-- Knex migrations\n\n${migrationInserts}`)
4039
}

src/load.js

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
/* eslint-disable max-len */
22
import { exec } from 'mz/child_process'
3-
import { preventEnv } from './utils'
3+
import {
4+
preventEnv,
5+
wrapDockerCommand,
6+
getCommand,
7+
getCommandEnv,
8+
} from './utils'
49

5-
async function load({ env, docker, structurePath, knexConfig }) {
6-
preventEnv('production', env)
10+
async function load(options) {
11+
const { structurePath } = options
12+
preventEnv('production', options.env)
713

8-
const command = docker
9-
? `docker exec -i \`docker-compose ps -q postgres\` psql --username ${
10-
knexConfig.connection.user
11-
} ${knexConfig.connection.database} < ${structurePath}`
12-
: `psql -h localhost --username ${knexConfig.connection.user} ${
13-
knexConfig.connection.database
14-
} < ${structurePath}`
14+
const env = getCommandEnv(options)
15+
const command = `${getCommand(options, 'psql')} < ${structurePath}`
1516

16-
return exec(command)
17+
return exec(wrapDockerCommand(options, command), { env })
1718
}
1819

1920
export default load

src/utils.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,40 @@ export function requireEnv(requiredEnv, env) {
99
throw new Error(`Only in ${requiredEnv} please!`)
1010
}
1111
}
12+
13+
export function wrapDockerCommand(options, command) {
14+
if (options.docker) {
15+
return `docker-compose run ${options.dockerService} ${command}`
16+
}
17+
18+
return command
19+
}
20+
21+
export function getCommandEnv(options) {
22+
if (options.knexConfig.password) {
23+
return {
24+
...process.env,
25+
PGPASSWORD: options.knexConfig.password,
26+
}
27+
}
28+
29+
return process.env
30+
}
31+
32+
export function getCommand({ docker, knexConfig: { connection } }, command) {
33+
const args = [command]
34+
35+
if (docker) {
36+
args.push('--host postgres')
37+
} else if (connection.host) {
38+
args.push(`--host "${connection.host}"`)
39+
}
40+
41+
if (connection.user) {
42+
args.push(`--username "${connection.user}"`)
43+
}
44+
45+
args.push(connection.database)
46+
47+
return args.join(' ')
48+
}

0 commit comments

Comments
 (0)