Skip to content

Instantly share code, notes, and snippets.

@jschr
Last active February 7, 2020 07:55
Show Gist options
  • Save jschr/873a4ad8591760202c20ea702e6d3c99 to your computer and use it in GitHub Desktop.
Save jschr/873a4ad8591760202c20ea702e6d3c99 to your computer and use it in GitHub Desktop.
.ebextension file and package.json for migrating a db with knex migrate cli and a single docker container beanstalk app
container_commands:
migrate_db:
command: >
docker run -e "DB_HOST=${DB_HOST}" -e "DB_PORT=${DB_PORT}" -e "DB_NAME=${DB_NAME}" -e "DB_USER=${DB_USER}" -e "DB_PASSWORD=${DB_PASSWORD}" aws_beanstalk/staging-app:latest npm run db:migration:run
leader_only: true
import * as path from 'path'
import * as dotenv from 'dotenv'
import * as Knex from 'knex'
dotenv.config({ path: path.resolve(__dirname, '../../.env') })
const config: Knex.Config = {
client: 'pg',
connection: {
host: process.env.DB_HOST,
port: process.env.DB_PORT,
database: process.env.DB_NAME,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD
},
migrations: {
tableName: 'migrations'
}
}
// using module.exports to be compatible with knex migrate cli
// this also means we need to use require('./config') to import
module.exports = config
{
"scripts": {
"db:migration:run": "node node_modules/knex/bin/cli.js migrate:latest --knexfile build/database/config.js",
"db:migration:rollback": "node node_modules/knex/bin/cli.js migrate:rollback --knexfile build/database/config.js",
"db:seed:run": "node node_modules/knex/bin/cli.js seed:run --knexfile build/database/config.js"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment