From 97482553d9d3004cbee0f273e7cfe29b501862ea Mon Sep 17 00:00:00 2001 From: John Datserakis Date: Sat, 2 Sep 2017 00:39:15 -0400 Subject: [PATCH] disabling knex seeds and migrations on production. --- package.json | 2 +- readme.md | 9 +++-- src/controllers/UserActionController.js | 34 ++++++++++--------- .../20170826111515_create_users_table.js | 9 ++++- ...170826132549_create_refresh_token_table.js | 9 ++++- .../20170826132806_create_notes_table.js | 9 ++++- src/db/seeds/dev/seed_notes.js | 4 +++ src/db/seeds/dev/seed_users.js | 8 +++++ 8 files changed, 61 insertions(+), 23 deletions(-) diff --git a/package.json b/package.json index 3488cfe..3d6e5bb 100644 --- a/package.json +++ b/package.json @@ -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, diff --git a/readme.md b/readme.md index 6d8c883..3cec354 100644 --- a/readme.md +++ b/readme.md @@ -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 ## diff --git a/src/controllers/UserActionController.js b/src/controllers/UserActionController.js index be69deb..8287637 100644 --- a/src/controllers/UserActionController.js +++ b/src/controllers/UserActionController.js @@ -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. diff --git a/src/db/migrations/20170826111515_create_users_table.js b/src/db/migrations/20170826111515_create_users_table.js index 22a571a..2e3c1dd 100644 --- a/src/db/migrations/20170826111515_create_users_table.js +++ b/src/db/migrations/20170826111515_create_users_table.js @@ -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() @@ -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') + } } diff --git a/src/db/migrations/20170826132549_create_refresh_token_table.js b/src/db/migrations/20170826132549_create_refresh_token_table.js index bb3ef11..dd966ae 100644 --- a/src/db/migrations/20170826132549_create_refresh_token_table.js +++ b/src/db/migrations/20170826132549_create_refresh_token_table.js @@ -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() @@ -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') + } } diff --git a/src/db/migrations/20170826132806_create_notes_table.js b/src/db/migrations/20170826132806_create_notes_table.js index 3c656e0..d8f5aee 100644 --- a/src/db/migrations/20170826132806_create_notes_table.js +++ b/src/db/migrations/20170826132806_create_notes_table.js @@ -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() @@ -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') + } } diff --git a/src/db/seeds/dev/seed_notes.js b/src/db/seeds/dev/seed_notes.js index 4446064..8d3b202 100644 --- a/src/db/seeds/dev/seed_notes.js +++ b/src/db/seeds/dev/seed_notes.js @@ -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) { diff --git a/src/db/seeds/dev/seed_users.js b/src/db/seeds/dev/seed_users.js index 5b63a9a..eda9a45 100644 --- a/src/db/seeds/dev/seed_users.js +++ b/src/db/seeds/dev/seed_users.js @@ -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'