Skip to content

Commit

Permalink
disabling knex seeds and migrations on production.
Browse files Browse the repository at this point in the history
  • Loading branch information
johndatserakis committed Sep 2, 2017
1 parent 63a2fa9 commit 9748255
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 23 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "koa-vue-notes-api",
"version": "1.2.4",
"version": "1.2.5",
"description": "A SPA using Koa as the backend and Vue as the frontend.",
"author": "John Datserakis",
"private": false,
Expand Down
9 changes: 6 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,18 @@ npm run pretty
npm run test

## knex migrations ##
## note - all knex migrate/rollback/seed calls must be prefaced
## with the setting of NODE_ENV - also, seeds and rollbacks are
## disabled in production

# migrate latest
knex migrate:latest
NODE_ENV=development knex migrate:latest

# rollback
knex migrate:rollback
NODE_ENV=developmentknex migrate:rollback

# run all seeds
knex seed:run
NODE_ENV=development knex seed:run

## knex seed and migration make commands ##

Expand Down
34 changes: 18 additions & 16 deletions src/controllers/UserActionController.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,22 +111,24 @@ class UserController {

//Let's send a welcome email.
if (process.env.NODE_ENV !== 'testing') {
let email = await fse.readFile(
'./src/email/welcome.html',
'utf8'
)
const emailData = {
to: request.email,
from: process.env.APP_EMAIL,
subject: 'Welcome To Koa-Vue-Notes-Api',
html: email,
categories: ['koa-vue-notes-api-new-user'],
substitutions: {
appName: process.env.APP_NAME,
appEmail: process.env.APP_EMAIL,
},
}
await sgMail.send(emailData)
//Let's turn off welcome emails for the moment

// let email = await fse.readFile(
// './src/email/welcome.html',
// 'utf8'
// )
// const emailData = {
// to: request.email,
// from: process.env.APP_EMAIL,
// subject: 'Welcome To Koa-Vue-Notes-Api',
// html: email,
// categories: ['koa-vue-notes-api-new-user'],
// substitutions: {
// appName: process.env.APP_NAME,
// appEmail: process.env.APP_EMAIL,
// },
// }
// await sgMail.send(emailData)
}

//And return our response.
Expand Down
9 changes: 8 additions & 1 deletion src/db/migrations/20170826111515_create_users_table.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
//I only want migrations, rollbacks, and seeds to run when the NODE_ENV is specified
//in the knex seed/migrate command. Knex will error out if it is not specified.
if (!process.env.NODE_ENV) { return; }

exports.up = function(knex, Promise) {
return knex.schema.createTable('users', function(table) {
table.increments('id').primary()
Expand Down Expand Up @@ -35,5 +39,8 @@ exports.up = function(knex, Promise) {
}

exports.down = function(knex, Promise) {
return knex.schema.dropTableIfExists('users')
//We never want to drop tables in production
if (process.env.NODE_ENV !== 'production') {
return knex.schema.dropTableIfExists('users')
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
//I only want migrations, rollbacks, and seeds to run when the NODE_ENV is specified
//in the knex seed/migrate command. Knex will error out if it is not specified.
if (!process.env.NODE_ENV) { return; }

exports.up = function(knex, Promise) {
return knex.schema.createTable('refresh_tokens', function(table) {
table.increments('id').primary()
Expand All @@ -16,5 +20,8 @@ exports.up = function(knex, Promise) {
}

exports.down = function(knex, Promise) {
return knex.schema.dropTableIfExists('refresh_tokens')
//We never want to drop tables in production
if (process.env.NODE_ENV !== 'production') {
return knex.schema.dropTableIfExists('refresh_tokens')
}
}
9 changes: 8 additions & 1 deletion src/db/migrations/20170826132806_create_notes_table.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
//I only want migrations, rollbacks, and seeds to run when the NODE_ENV is specified
//in the knex seed/migrate command. Knex will error out if it is not specified.
if (!process.env.NODE_ENV) { return; }

exports.up = function(knex, Promise) {
return knex.schema.createTable('notes', function(table) {
table.increments('id').primary()
Expand All @@ -11,5 +15,8 @@ exports.up = function(knex, Promise) {
}

exports.down = function(knex, Promise) {
return knex.schema.dropTableIfExists('notes')
//We never want to drop tables in production
if (process.env.NODE_ENV !== 'production') {
return knex.schema.dropTableIfExists('notes')
}
}
4 changes: 4 additions & 0 deletions src/db/seeds/dev/seed_notes.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
//I only want migrations, rollbacks, and seeds to run when the NODE_ENV is specified
//in the knex seed/migrate command. Knex will error out if it is not specified.
if (!process.env.NODE_ENV) { return; }

const faker = require('faker')

exports.seed = async function(knex, Promise) {
Expand Down
8 changes: 8 additions & 0 deletions src/db/seeds/dev/seed_users.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
//I only want migrations, rollbacks, and seeds to run when the NODE_ENV is specified
//in the knex seed/migrate command. Knex will error out if it is not specified.
if (!process.env.NODE_ENV) { return; }

const faker = require('faker')
const bcrypt = require('bcrypt')

exports.seed = async function(knex, Promise) {
//I only want migrations, rollbacks, and seeds to run when the NODE_ENV is specified
//in the knex seed/migrate command
if (!process.env.NODE_ENV) { return; }

//Make 10 users using faker. Note: we're also bcrypting
//the passwords to make it exactly like the real app. All their
//passwords will be 'secret'
Expand Down

0 comments on commit 9748255

Please sign in to comment.