Skip to content

Commit

Permalink
Merge pull request #2 from smooth-code/improve-scripts
Browse files Browse the repository at this point in the history
feat: improve scripts
  • Loading branch information
gregberge authored Feb 12, 2018
2 parents a4f0ec9 + e0af8c6 commit 8bf0dbd
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 55 deletions.
9 changes: 7 additions & 2 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,13 @@ function initConfig(env) {
structurePath: knexScriptsConfig.structurePath || 'db/structure.sql',
migrationsPath: join(process.cwd(), 'migrations'),
docker:
commander.docker !== undefined
(commander.docker !== undefined
? commander.docker
: knexScriptsConfig.docker,
: knexScriptsConfig.docker) || false,
dockerService:
(commander.dockerService !== undefined
? commander.dockerService
: knexScriptsConfig.docker) || 'postgres',
}
}

Expand All @@ -95,6 +99,7 @@ function invoke(env) {
chalk`{blue Knex version: {green ${env.modulePackage.version}}}\n`,
)
.option('--docker', 'Use docker.')
.option('--docker-service', 'Docker service name.')
.option('--knexfile [path]', 'Specify the knexfile path.')
.option('--cwd [path]', 'Specify the working directory.')
.option(
Expand Down
23 changes: 11 additions & 12 deletions src/create.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
/* eslint-disable max-len */
import { exec } from 'mz/child_process'
import { preventEnv } from './utils'
import {
preventEnv,
wrapDockerCommand,
getCommand,
getCommandEnv,
} from './utils'

async function create({ docker, env, knexConfig }) {
preventEnv('production', env)
async function create(options) {
preventEnv('production', options.env)

const command = docker
? `docker-compose run postgres createdb --host postgres --username ${
knexConfig.connection.user
} ${knexConfig.connection.database}`
: `createdb --username ${knexConfig.connection.user} ${
knexConfig.connection.database
}`

return exec(command)
const env = getCommandEnv(options)
const command = getCommand(options, 'createdb')
return exec(wrapDockerCommand(options, command), { env })
}

export default create
26 changes: 13 additions & 13 deletions src/drop.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
/* eslint-disable max-len */
import { exec } from 'mz/child_process'
import { preventEnv } from './utils'
import {
preventEnv,
wrapDockerCommand,
getCommand,
getCommandEnv,
} from './utils'

async function drop({ docker, env, knexConfig }) {
preventEnv('production', env)
async function create(options) {
preventEnv('production', options.env)

const command = docker
? `docker-compose run postgres dropdb --host postgres --username ${
knexConfig.connection.user
} ${knexConfig.connection.database} --if-exists`
: `dropdb --username ${knexConfig.connection.user} ${
knexConfig.connection.database
} --if-exists`

return exec(command)
const env = getCommandEnv(options)
const command = getCommand(options, 'dropdb --if-exists')
return exec(wrapDockerCommand(options, command), { env })
}

export default drop
export default create
33 changes: 16 additions & 17 deletions src/dump.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ import { exec } from 'mz/child_process'
import { appendFile, readdir, exists } from 'mz/fs'
import { dirname } from 'path'
import mkdirp from 'mkdirp'
import { requireEnv } from './utils'
import {
requireEnv,
wrapDockerCommand,
getCommand,
getCommandEnv,
} from './utils'

async function getMigrationInserts({ migrationsPath }) {
if (!await exists(migrationsPath)) return ''
Expand All @@ -15,26 +20,20 @@ async function getMigrationInserts({ migrationsPath }) {
.join('')
}

async function dump({
docker,
env,
structurePath,
migrationsPath,
knexConfig,
}) {
requireEnv('development', env)
async function dump(options) {
const { structurePath, migrationsPath } = options
requireEnv('development', options.env)

mkdirp.sync(dirname(structurePath))

const command = docker
? `docker-compose exec postgres pg_dump --schema-only --username ${
knexConfig.connection.user
} ${knexConfig.connection.database} > ${structurePath}`
: `pg_dump --schema-only --username ${knexConfig.connection.user} ${
knexConfig.connection.database
} > ${structurePath}`
const env = getCommandEnv(options)
const command = `${getCommand(
options,
'pg_dump --schema-only',
)} > ${structurePath}`

await exec(wrapDockerCommand(options, command), { env })

await exec(command)
const migrationInserts = await getMigrationInserts({ migrationsPath })
return appendFile(structurePath, `-- Knex migrations\n\n${migrationInserts}`)
}
Expand Down
23 changes: 12 additions & 11 deletions src/load.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
/* eslint-disable max-len */
import { exec } from 'mz/child_process'
import { preventEnv } from './utils'
import {
preventEnv,
wrapDockerCommand,
getCommand,
getCommandEnv,
} from './utils'

async function load({ env, docker, structurePath, knexConfig }) {
preventEnv('production', env)
async function load(options) {
const { structurePath } = options
preventEnv('production', options.env)

const command = docker
? `docker exec -i \`docker-compose ps -q postgres\` psql --username ${
knexConfig.connection.user
} ${knexConfig.connection.database} < ${structurePath}`
: `psql -h localhost --username ${knexConfig.connection.user} ${
knexConfig.connection.database
} < ${structurePath}`
const env = getCommandEnv(options)
const command = `${getCommand(options, 'psql')} < ${structurePath}`

return exec(command)
return exec(wrapDockerCommand(options, command), { env })
}

export default load
37 changes: 37 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,40 @@ export function requireEnv(requiredEnv, env) {
throw new Error(`Only in ${requiredEnv} please!`)
}
}

export function wrapDockerCommand(options, command) {
if (options.docker) {
return `docker-compose run ${options.dockerService} ${command}`
}

return command
}

export function getCommandEnv(options) {
if (options.knexConfig.password) {
return {
...process.env,
PGPASSWORD: options.knexConfig.password,
}
}

return process.env
}

export function getCommand({ docker, knexConfig: { connection } }, command) {
const args = [command]

if (docker) {
args.push('--host postgres')
} else if (connection.host) {
args.push(`--host "${connection.host}"`)
}

if (connection.user) {
args.push(`--username "${connection.user}"`)
}

args.push(connection.database)

return args.join(' ')
}

0 comments on commit 8bf0dbd

Please sign in to comment.