From b864471a2c5edabddd3fa8f270d674faa6deaaf1 Mon Sep 17 00:00:00 2001 From: Harminder Virk Date: Sun, 27 Dec 2015 16:51:20 +0530 Subject: [PATCH 01/17] Moved migrations dir creation to before ook --- test/implementation/storage/blog.sqlite3 | Bin 4096 -> 4096 bytes test/unit/commands.spec.js | 5 +---- .../1451215262191_create_users_table.js | 16 ++++++++++++++++ test/unit/storage/schema.sqlite3 | Bin 6144 -> 6144 bytes test/unit/storage/test.sqlite3 | Bin 17408 -> 17408 bytes 5 files changed, 17 insertions(+), 4 deletions(-) create mode 100644 test/unit/migrations/1451215262191_create_users_table.js diff --git a/test/implementation/storage/blog.sqlite3 b/test/implementation/storage/blog.sqlite3 index 3d588eb01457d4d542b8714cb14e8e2501cfa810..7740a4dd12b425bb7c3b8823ef9f5200c53001a2 100644 GIT binary patch delta 63 zcmZorXi%6SE%=^+fq?~znHU(DR!r0}W_-UfVIn)f_Qd;)fseH(J^_=@HgSAs=hU8f I56rs<01XNhsQ>@~ delta 63 zcmZorXi%6SEqINAfq?~znHU(D7EIJJX1umBVIn)faP3^iz{kS13&G^lO&s6ZIfZNI HfO&HO;`|ax diff --git a/test/unit/commands.spec.js b/test/unit/commands.spec.js index 95d8b532..21cbc022 100644 --- a/test/unit/commands.spec.js +++ b/test/unit/commands.spec.js @@ -30,13 +30,10 @@ const Helpers = { } describe('Commands', function () { - before(function () { + before(function (done) { GLOBAL.use = function () { return function () {} } - }) - - after(function (done) { fs.emptyDir(path.join(__dirname, './migrations'), function (error) { if (error) { done(error) diff --git a/test/unit/migrations/1451215262191_create_users_table.js b/test/unit/migrations/1451215262191_create_users_table.js new file mode 100644 index 00000000..f62062bf --- /dev/null +++ b/test/unit/migrations/1451215262191_create_users_table.js @@ -0,0 +1,16 @@ + +'use strict' + +const Schema = use('Schema') + +class NewSchema extends Schema { + + up () { + } + + down () { + } + +} + +module.exports = NewSchema diff --git a/test/unit/storage/schema.sqlite3 b/test/unit/storage/schema.sqlite3 index 22680cc50f2f20413726eeb5140f1b52474c49e9..3cd670f6e2a88252bd519195d6db2db5a3159b63 100644 GIT binary patch delta 22 bcmZoLXfW7tnVngCa^~b~?CKzT5r+T(UKaVr87fV-I8rvP@#0 ze2HIwGb6`;=1nXQndP-7Pv>D`XAorsYRt>b&KBokg!0NVi?YQw31l#HYEQbr82DIw z(uc`~!jj(Fla7OVPk=fUIT;i=SXmh?4GfJe4GoMeEx}R{>ZSQ$6*JY<#^u21G+ zVrLL#1ggr*%+40)VTAI^GK;ds7ICmlVzCEm5agM}Joys4{$@sjbY@QBx+#o-kA>?t zPA(Ld^cJq`0rQpv%~0fIP~>1`Ww10bG_o`_FtW4+OF@)Rk!R&(;N%1=FtD(+u&}T+ zv@`%KSOXz9Ulo2J3^aZpn7eQCK6ObTZ#$T`ee(nLcpgTU&5kV77&l)t)#3vH^_o3t From 45ffd770d56ef457e6f0a03c95926f770fbcbdf0 Mon Sep 17 00:00:00 2001 From: Harminder Virk Date: Sun, 27 Dec 2015 16:54:34 +0530 Subject: [PATCH 02/17] Remove iojs from travis --- .travis.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index a1c17649..fdf2d1a9 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,5 @@ language: node_js node_js: - - "iojs" - "4.0.0" sudo: false install: From ce8bdfb7dbdae1344298350e937dbe4b5015bcb4 Mon Sep 17 00:00:00 2001 From: Harminder Virk Date: Sun, 27 Dec 2015 16:55:50 +0530 Subject: [PATCH 03/17] Added latest version of node to travis build --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index fdf2d1a9..6f6b09d0 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,7 @@ language: node_js node_js: - "4.0.0" + - "5.3.0" sudo: false install: - npm install From 326e18cd62efdbc9d7199eb238fe76f901692cd6 Mon Sep 17 00:00:00 2001 From: Harminder Virk Date: Sun, 27 Dec 2015 16:59:40 +0530 Subject: [PATCH 04/17] Added peer dependencies to dev dependencies Since latest version of npm does not load peer dependencies --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index 77911743..b9c64dc5 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,7 @@ "author": "", "license": "ISC", "devDependencies": { + "adonis-fold": "^1.0.0", "chai": "^3.2.0", "co": "^4.6.0", "coveralls": "^2.11.4", From 65cb2ad51648471cd044effca7278ebbded5e375 Mon Sep 17 00:00:00 2001 From: Harminder Virk Date: Sun, 27 Dec 2015 18:03:50 +0530 Subject: [PATCH 05/17] Commands now met their own dependecies --- package.json | 4 +-- src/Commands/Make.js | 5 ++-- src/Commands/Rollback.js | 5 +++- src/Commands/Run.js | 5 +++- test/implementation/storage/blog.sqlite3 | Bin 4096 -> 4096 bytes test/unit/commands.spec.js | 27 ++++++++++++++---- .../1451215262191_create_users_table.js | 16 ----------- test/unit/storage/schema.sqlite3 | Bin 6144 -> 6144 bytes test/unit/storage/test.sqlite3 | Bin 17408 -> 17408 bytes 9 files changed, 34 insertions(+), 28 deletions(-) delete mode 100644 test/unit/migrations/1451215262191_create_users_table.js diff --git a/package.json b/package.json index 98209c71..bfe69626 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,7 @@ "author": "adonisjs", "license": "MIT", "devDependencies": { - "adonis-fold": "^1.0.0", + "adonis-fold": "file:///Users/harmindervirk/workspace/v2Launch/fold", "chai": "^3.2.0", "co": "^4.6.0", "coveralls": "^2.11.4", @@ -26,7 +26,7 @@ "standard": "^5.4.1" }, "peerDependencies": { - "adonis-fold": "^1.0.0" + "adonis-fold": "file:///Users/harmindervirk/workspace/v2Launch/fold" }, "dependencies": { "auto-loader": "^0.2.0", diff --git a/src/Commands/Make.js b/src/Commands/Make.js index 68ac7777..965a053b 100644 --- a/src/Commands/Make.js +++ b/src/Commands/Make.js @@ -25,11 +25,12 @@ module.exports = NewSchema ` const fs = require('fs') +const Ioc = require('adonis-fold').Ioc class Make { - constructor (Helpers) { - this.helpers = Helpers + constructor () { + this.helpers = Ioc.make('Adonis/Src/Helpers') } /** diff --git a/src/Commands/Rollback.js b/src/Commands/Rollback.js index 93761ee3..08c47beb 100644 --- a/src/Commands/Rollback.js +++ b/src/Commands/Rollback.js @@ -7,10 +7,13 @@ */ const autoLoader = require('auto-loader') +const Ioc = require('adonis-fold').Ioc class Rollback { - constructor (Helpers, Runner) { + constructor () { + const Helpers = Ioc.make('Adonis/Src/Helpers') + const Runner = Ioc.make('Adonis/Src/Runner') this.migrations = Helpers.migrationsPath() this.runner = Runner } diff --git a/src/Commands/Run.js b/src/Commands/Run.js index 41872e1b..c5308d13 100644 --- a/src/Commands/Run.js +++ b/src/Commands/Run.js @@ -7,10 +7,13 @@ */ const autoLoader = require('auto-loader') +const Ioc = require('adonis-fold').Ioc class Run { - constructor (Helpers, Runner) { + constructor () { + const Helpers = Ioc.make('Adonis/Src/Helpers') + const Runner = Ioc.make('Adonis/Src/Runner') this.migrations = Helpers.migrationsPath() this.runner = Runner } diff --git a/test/implementation/storage/blog.sqlite3 b/test/implementation/storage/blog.sqlite3 index 7740a4dd12b425bb7c3b8823ef9f5200c53001a2..bbebbf71cf3d34bb16f2dc1617839eed9fe96662 100644 GIT binary patch delta 65 zcmZorXi%6SCB(tOz`(!)#Y_wgOdBTZ7%_2hY)qKQ&Yvsd&KUSOSHv4k`fcL)&d!-D J;s)lq0RW0b4$}Yt delta 65 zcmZorXi%6SCB*)ofq{VqikTP~m{v^GF=AqWzcFDVJHPhC`;38)wI@CSlg~DBd}rs> Ko_G(;y9WRkC=|B< diff --git a/test/unit/commands.spec.js b/test/unit/commands.spec.js index 21cbc022..383a1c8c 100644 --- a/test/unit/commands.spec.js +++ b/test/unit/commands.spec.js @@ -10,6 +10,7 @@ const Make = require('../../src/Commands/Make') const Run = require('../../src/Commands/Run') +const Ioc = require('adonis-fold').Ioc const Rollback = require('../../src/Commands/Rollback') const chai = require('chai') const path = require('path') @@ -28,7 +29,9 @@ const Helpers = { } } } - +Ioc.bind('Adonis/Src/Helpers', function () { + return Helpers +}) describe('Commands', function () { before(function (done) { GLOBAL.use = function () { @@ -45,7 +48,7 @@ describe('Commands', function () { context('Make', function () { it('should create a file inside migrations directory', function (done) { - const make = new Make(Helpers) + const make = new Make() make.description() make.signature() @@ -70,7 +73,10 @@ describe('Commands', function () { const Runner = { up: function * (files) {} } - const run = new Run(Helpers, Runner) + Ioc.bind('Adonis/Src/Runner', function () { + return Runner + }) + const run = new Run() co(function * () { return yield run.handle({}, {}) @@ -90,7 +96,10 @@ describe('Commands', function () { migrations = files } } - const run = new Run(Helpers, Runner) + Ioc.bind('Adonis/Src/Runner', function () { + return Runner + }) + const run = new Run() run.description() run.signature() @@ -112,7 +121,10 @@ describe('Commands', function () { const Runner = { up: function * (files) {} } - const rollback = new Rollback(Helpers, Runner) + Ioc.bind('Adonis/Src/Runner', function () { + return Runner + }) + const rollback = new Rollback() co(function * () { return yield rollback.handle({}, {}) @@ -132,7 +144,10 @@ describe('Commands', function () { migrations = files } } - const rollback = new Rollback(Helpers, Runner) + Ioc.bind('Adonis/Src/Runner', function () { + return Runner + }) + const rollback = new Rollback() rollback.description() rollback.signature() diff --git a/test/unit/migrations/1451215262191_create_users_table.js b/test/unit/migrations/1451215262191_create_users_table.js deleted file mode 100644 index f62062bf..00000000 --- a/test/unit/migrations/1451215262191_create_users_table.js +++ /dev/null @@ -1,16 +0,0 @@ - -'use strict' - -const Schema = use('Schema') - -class NewSchema extends Schema { - - up () { - } - - down () { - } - -} - -module.exports = NewSchema diff --git a/test/unit/storage/schema.sqlite3 b/test/unit/storage/schema.sqlite3 index 3cd670f6e2a88252bd519195d6db2db5a3159b63..66b1fe99903b24ed33253ef485e7de875be42e6d 100644 GIT binary patch delta 22 ccmZoLXfW7tnVmUT^z7to?CL=J%pwi}0AQa7-v9sr delta 22 bcmZoLXfW7tnVngCa^~b~?CKzT5r+T(UKal zo?Iv_>76U859WCQ%~0fIP~>1`Ww10bG_o`_FtW4+OF@*I%d>JaaB_kb7+6?ZSXfvZ yS{i^A_(90cSA`!41C5Ubb7Lp(QVr87fV-I8rvP@#0 ze2HIwGb6`;=1nXQndP-7Pv>D`XAorsYRt>b&KBokg!0NVi?YQw31l#HYEQbr82DIw z(uc`~!jj(Fla7OVPk=fUIT;i=SXmh?4GfJe4GoMeEx}R{ Date: Sun, 27 Dec 2015 18:07:11 +0530 Subject: [PATCH 06/17] Providers now returns the actual class instead of instance --- providers/SchemaProvider.js | 20 ++++++-------------- src/Commands/Make.js | 4 ++-- src/Commands/Rollback.js | 4 ++-- src/Commands/Run.js | 4 ++-- test/implementation/storage/blog.sqlite3 | Bin 4096 -> 4096 bytes test/unit/commands.spec.js | 12 ++++++------ test/unit/storage/schema.sqlite3 | Bin 6144 -> 6144 bytes test/unit/storage/test.sqlite3 | Bin 17408 -> 17408 bytes 8 files changed, 18 insertions(+), 26 deletions(-) diff --git a/providers/SchemaProvider.js b/providers/SchemaProvider.js index 4d541864..86afb4b5 100644 --- a/providers/SchemaProvider.js +++ b/providers/SchemaProvider.js @@ -15,24 +15,16 @@ class SchemaProvider extends ServiceProvider { return require('../src/Schema') }) - this.app.bind('Adonis/Commands/Make', function (app) { - const Helpers = app.use('Adonis/Src/Helpers') - const Make = require('../src/Commands/Make') - return new Make(Helpers) + this.app.bind('Adonis/Commands/Make', function () { + return require('../src/Commands/Make') }) - this.app.bind('Adonis/Commands/Run', function (app) { - const Helpers = app.use('Adonis/Src/Helpers') - const Runner = app.use('Adonis/Src/Runner') - const Run = require('../src/Commands/Run') - return new Run(Helpers, Runner) + this.app.bind('Adonis/Commands/Run', function () { + return require('../src/Commands/Run') }) - this.app.bind('Adonis/Commands/Rollback', function (app) { - const Helpers = app.use('Adonis/Src/Helpers') - const Runner = app.use('Adonis/Src/Runner') - const Rollback = require('../src/Commands/Rollback') - return new Rollback(Helpers, Runner) + this.app.bind('Adonis/Commands/Rollback', function () { + return require('../src/Commands/Rollback') }) } diff --git a/src/Commands/Make.js b/src/Commands/Make.js index 965a053b..2f533435 100644 --- a/src/Commands/Make.js +++ b/src/Commands/Make.js @@ -39,7 +39,7 @@ class Make { * @return {String} * @public */ - description () { + static get description () { return 'Create a new migration file' } @@ -50,7 +50,7 @@ class Make { * @return {String} * @public */ - signature () { + static get signature () { return '{name}' } diff --git a/src/Commands/Rollback.js b/src/Commands/Rollback.js index 08c47beb..639c5756 100644 --- a/src/Commands/Rollback.js +++ b/src/Commands/Rollback.js @@ -24,7 +24,7 @@ class Rollback { * @return {String} * @public */ - description () { + static get description () { return 'Rollback migrations executed in last batch' } @@ -35,7 +35,7 @@ class Rollback { * @return {String} * @public */ - signature () { + static get signature () { return '{--force?}' } diff --git a/src/Commands/Run.js b/src/Commands/Run.js index c5308d13..b1f984cc 100644 --- a/src/Commands/Run.js +++ b/src/Commands/Run.js @@ -24,7 +24,7 @@ class Run { * @return {String} * @public */ - description () { + static get description () { return 'Run latest migrations' } @@ -35,7 +35,7 @@ class Run { * @return {String} * @public */ - signature () { + static get signature () { return '{--force?}' } diff --git a/test/implementation/storage/blog.sqlite3 b/test/implementation/storage/blog.sqlite3 index bbebbf71cf3d34bb16f2dc1617839eed9fe96662..8ef22fb1e0a24f7cbfa8c8dba619cd83839a584b 100644 GIT binary patch delta 63 zcmZorXi%6SEvU%Az`z2pu delta 238 zcmZqZU~K4MoFFA6^pSyqK@f;pfS8|wfzxK9juDg4$BhZIm>8Kh|7K!k+{E*cSw2_v zHxCm#gD4|VSzcy#wm1(XlvkEnlr6T1gJlwnJy3%n&m`u_m)P|;GYX_LbLNVgGX_4+ z6%C$TC@krnE2v8&G&C@>v;<2*l$*=5ax!pof)yB8SXx+E zSQ=UyfED;b$jw)U9|!}Dj|FpMC+}021oFbcyztEr)Z=*=SvEVeOk>=9%~XpI0P=J> A{r~^~ From 3500fe8b2e5282b0517fb02c443fe213018635d0 Mon Sep 17 00:00:00 2001 From: Harminder Virk Date: Sun, 27 Dec 2015 18:52:52 +0530 Subject: [PATCH 07/17] Now commands are exported objects instead of classes --- src/Commands/Make.js | 79 ++++++++++++++------------------------ src/Commands/Rollback.js | 68 +++++++++++--------------------- src/Commands/Run.js | 67 ++++++++++---------------------- test/unit/commands.spec.js | 17 +++----- 4 files changed, 76 insertions(+), 155 deletions(-) diff --git a/src/Commands/Make.js b/src/Commands/Make.js index 2f533435..3043c614 100644 --- a/src/Commands/Make.js +++ b/src/Commands/Make.js @@ -6,6 +6,9 @@ * MIT Licensed */ +const fs = require('fs') +const Ioc = require('adonis-fold').Ioc + const migrationContent = ` 'use strict' @@ -24,58 +27,32 @@ class NewSchema extends Schema { module.exports = NewSchema ` -const fs = require('fs') -const Ioc = require('adonis-fold').Ioc - -class Make { +let Make = exports = module.exports = {} - constructor () { - this.helpers = Ioc.make('Adonis/Src/Helpers') - } +Make.description = 'Create a new migration file' +Make.signature = '{name}' - /** - * @description returns command description - * @method description - * @return {String} - * @public - */ - static get description () { - return 'Create a new migration file' - } - - /** - * @description command signature to define expectation for - * a given command to ace - * @method signature - * @return {String} - * @public - */ - static get signature () { - return '{name}' - } - - /** - * @description creates a new migration file - * @method handle - * @param {Object} options - * @param {Object} flags - * @return {Object} - * @public - */ - handle (options) { - return new Promise((resolve, reject) => { - const name = `${new Date().getTime()}_${options.name}.js` - const migrationPath = this.helpers.migrationsPath(name) - - fs.writeFile(migrationPath, migrationContent, function (error) { - if (error) { - reject(error) - } else { - resolve(`Created ${name} migration successfully`) - } - }) +/** + * @description creates a new migration file + * @method handle + * @param {Object} options + * @param {Object} flags + * @return {Object} + * @public + */ +Make.handle = function (options) { + const helpers = Ioc.make('Adonis/Src/Helpers') + return new Promise((resolve, reject) => { + + const name = `${new Date().getTime()}_${options.name}.js` + const migrationPath = helpers.migrationsPath(name) + + fs.writeFile(migrationPath, migrationContent, function (error) { + if (error) { + reject(error) + } else { + resolve(`Created ${name} migration successfully`) + } }) - } + }) } - -module.exports = Make diff --git a/src/Commands/Rollback.js b/src/Commands/Rollback.js index 639c5756..824bbb48 100644 --- a/src/Commands/Rollback.js +++ b/src/Commands/Rollback.js @@ -9,53 +9,29 @@ const autoLoader = require('auto-loader') const Ioc = require('adonis-fold').Ioc -class Rollback { +let Rollback = exports = module.exports = {} - constructor () { - const Helpers = Ioc.make('Adonis/Src/Helpers') - const Runner = Ioc.make('Adonis/Src/Runner') - this.migrations = Helpers.migrationsPath() - this.runner = Runner - } - - /** - * @description returns command description - * @method description - * @return {String} - * @public - */ - static get description () { - return 'Rollback migrations executed in last batch' - } - - /** - * @description command signature to define expectation for - * a given command to ace - * @method signature - * @return {String} - * @public - */ - static get signature () { - return '{--force?}' - } +Rollback.description = 'Rollback migrations executed in last batch' +Rollback.signature = '{--force?}' - /** - * @description rollback all migrations using - * runner provider - * @method handle - * @param {Object} options - * @param {Object} flags - * @return {Object} - * @public - */ - * handle (options, flags) { - if (process.env.NODE_ENV === 'production' && !flags.force) { - throw new Error('Cannot run migrations in production') - } - const migrationsFiles = autoLoader.load(this.migrations) - return yield this.runner.down(migrationsFiles) +/** + * @description rollback all migrations using + * runner provider + * @method handle + * @param {Object} options + * @param {Object} flags + * @return {Object} + * @public + */ +Rollback.handle = function * (options, flags) { + + const Helpers = Ioc.make('Adonis/Src/Helpers') + const Runner = Ioc.make('Adonis/Src/Runner') + const migrations = Helpers.migrationsPath() + + if (process.env.NODE_ENV === 'production' && !flags.force) { + throw new Error('Cannot run migrations in production') } - + const migrationsFiles = autoLoader.load(migrations) + return yield Runner.down(migrationsFiles) } - -module.exports = Rollback diff --git a/src/Commands/Run.js b/src/Commands/Run.js index b1f984cc..01b01f31 100644 --- a/src/Commands/Run.js +++ b/src/Commands/Run.js @@ -9,53 +9,28 @@ const autoLoader = require('auto-loader') const Ioc = require('adonis-fold').Ioc -class Run { +let Run = exports = module.exports = {} - constructor () { - const Helpers = Ioc.make('Adonis/Src/Helpers') - const Runner = Ioc.make('Adonis/Src/Runner') - this.migrations = Helpers.migrationsPath() - this.runner = Runner - } - - /** - * @description returns command description - * @method description - * @return {String} - * @public - */ - static get description () { - return 'Run latest migrations' - } - - /** - * @description command signature to define expectation for - * a given command to ace - * @method signature - * @return {String} - * @public - */ - static get signature () { - return '{--force?}' - } +Run.description = 'Run latest migrations' +Run.signature = '{--force?}' - /** - * @description migrate all migrations using runner - * provider - * @method handle - * @param {Object} options - * @param {Object} flags - * @return {Object} - * @public - */ - * handle (options, flags) { - if (process.env.NODE_ENV === 'production' && !flags.force) { - throw new Error('Cannot run migrations in production') - } - const migrationsFiles = autoLoader.load(this.migrations) - return yield this.runner.up(migrationsFiles) +/** + * @description rollback all migrations using + * runner provider + * @method handle + * @param {Object} options + * @param {Object} flags + * @return {Object} + * @public + */ +Run.handle = function * (options, flags) { + const Helpers = Ioc.make('Adonis/Src/Helpers') + const Runner = Ioc.make('Adonis/Src/Runner') + const migrations = Helpers.migrationsPath() + + if (process.env.NODE_ENV === 'production' && !flags.force) { + throw new Error('Cannot run migrations in production') } - + const migrationsFiles = autoLoader.load(migrations) + return yield Runner.up(migrationsFiles) } - -module.exports = Run diff --git a/test/unit/commands.spec.js b/test/unit/commands.spec.js index d483632c..7e1a1380 100644 --- a/test/unit/commands.spec.js +++ b/test/unit/commands.spec.js @@ -48,11 +48,10 @@ describe('Commands', function () { context('Make', function () { it('should create a file inside migrations directory', function (done) { - const make = new Make() expect(Make.description).not.equal(undefined) expect(Make.signature).not.equal(undefined) - make + Make .handle({name: 'create_users_table'}) .then(function () { fs.ensureFile(migName, function (err) { @@ -76,10 +75,8 @@ describe('Commands', function () { Ioc.bind('Adonis/Src/Runner', function () { return Runner }) - const run = new Run() - co(function * () { - return yield run.handle({}, {}) + return yield Run.handle({}, {}) }) .then(function () {}) .catch(function (error) { @@ -99,12 +96,11 @@ describe('Commands', function () { Ioc.bind('Adonis/Src/Runner', function () { return Runner }) - const run = new Run() expect(Run.description).not.equal(undefined) expect(Run.signature).not.equal(undefined) co(function * () { - return yield run.handle({}, {}) + return yield Run.handle({}, {}) }) .then(function () { const basename = path.basename(migName).replace('.js', '') @@ -124,10 +120,8 @@ describe('Commands', function () { Ioc.bind('Adonis/Src/Runner', function () { return Runner }) - const rollback = new Rollback() - co(function * () { - return yield rollback.handle({}, {}) + return yield Rollback.handle({}, {}) }) .then(function () {}) .catch(function (error) { @@ -147,12 +141,11 @@ describe('Commands', function () { Ioc.bind('Adonis/Src/Runner', function () { return Runner }) - const rollback = new Rollback() expect(Rollback.description).not.equal(undefined) expect(Rollback.signature).not.equal(undefined) co(function * () { - return yield rollback.handle({}, {}) + return yield Rollback.handle({}, {}) }) .then(function () { const basename = path.basename(migName).replace('.js', '') From fe1a8e17aa62d99b065b0142f24d33c5e15b32ec Mon Sep 17 00:00:00 2001 From: Harminder Virk Date: Sun, 27 Dec 2015 18:54:27 +0530 Subject: [PATCH 08/17] formatted src files --- src/Commands/Make.js | 1 - src/Commands/Rollback.js | 1 - 2 files changed, 2 deletions(-) diff --git a/src/Commands/Make.js b/src/Commands/Make.js index 3043c614..83efc57a 100644 --- a/src/Commands/Make.js +++ b/src/Commands/Make.js @@ -43,7 +43,6 @@ Make.signature = '{name}' Make.handle = function (options) { const helpers = Ioc.make('Adonis/Src/Helpers') return new Promise((resolve, reject) => { - const name = `${new Date().getTime()}_${options.name}.js` const migrationPath = helpers.migrationsPath(name) diff --git a/src/Commands/Rollback.js b/src/Commands/Rollback.js index 824bbb48..f646d3d9 100644 --- a/src/Commands/Rollback.js +++ b/src/Commands/Rollback.js @@ -24,7 +24,6 @@ Rollback.signature = '{--force?}' * @public */ Rollback.handle = function * (options, flags) { - const Helpers = Ioc.make('Adonis/Src/Helpers') const Runner = Ioc.make('Adonis/Src/Runner') const migrations = Helpers.migrationsPath() From 9e4396f4eda468f090f4e1887c76676034ab5afe Mon Sep 17 00:00:00 2001 From: Harminder Virk Date: Sun, 27 Dec 2015 21:59:51 +0530 Subject: [PATCH 09/17] Updated all tests to create and delete database files Setup and tear down will manage test related database files, helping in removing unneccessary files from commit --- src/Runner/index.js | 5 ++- test/implementation/model-relations.spec.js | 18 ++++++--- test/implementation/model.spec.js | 29 +++++++++----- test/implementation/storage/blog.sqlite3 | Bin 4096 -> 0 bytes test/unit/blueprints/manage.js | 34 +++++++++++++++++ .../blueprints/model-relations-blueprint.js | 33 +++++++++++++--- test/unit/commands.spec.js | 10 +++++ test/unit/database.spec.js | 36 ++++++++++++++---- test/unit/model-relations.spec.js | 16 ++++++-- test/unit/model.spec.js | 21 +++++++--- test/unit/runner.spec.js | 10 +++++ test/unit/staticProxy.spec.js | 22 +++++++---- test/unit/storage/blog.sqlite3 | 0 test/unit/storage/connection.sqlite3 | Bin 65536 -> 0 bytes test/unit/storage/schema.sqlite3 | Bin 6144 -> 0 bytes test/unit/storage/test.sqlite3 | Bin 17408 -> 0 bytes 16 files changed, 187 insertions(+), 47 deletions(-) delete mode 100644 test/implementation/storage/blog.sqlite3 create mode 100644 test/unit/blueprints/manage.js delete mode 100644 test/unit/storage/blog.sqlite3 delete mode 100644 test/unit/storage/connection.sqlite3 delete mode 100644 test/unit/storage/schema.sqlite3 delete mode 100644 test/unit/storage/test.sqlite3 diff --git a/src/Runner/index.js b/src/Runner/index.js index 46666174..55696bb5 100644 --- a/src/Runner/index.js +++ b/src/Runner/index.js @@ -14,9 +14,10 @@ const log = new CatLog('adonis:lucid') class Runner { constructor (Config) { - const config = Config.get('database.connection') + const connection = Config.get('database.connection') + const config = Config.get(`database.${connection}`) this.knex = require('knex')(config) - this.migrationsTable = Config.get('database.migrationsTable') + this.migrationsTable = Config.get('database.migrationsTable', 'adonis_schema') this.lockTable = `${this.migrationsTable}_lock` this.migrations = [] } diff --git a/test/implementation/model-relations.spec.js b/test/implementation/model-relations.spec.js index 5c92c9b0..a72f01c9 100644 --- a/test/implementation/model-relations.spec.js +++ b/test/implementation/model-relations.spec.js @@ -5,6 +5,7 @@ const chai = require('chai') const expect = chai.expect const co = require('co') const Database = require('../../src/Database') +const manageDb = require('../unit/blueprints/manage') const blueprint = require('./blueprints/model-blueprint') const Model = require('../../src/Orm/Proxy/Model') const Ioc = require('adonis-fold').Ioc @@ -55,16 +56,23 @@ Ioc.bind('App/Model/User', function () { describe('Database Implementation', function () { before(function (done) { - blueprint - .setup(db) - .then (function () { - done() - }).catch(done) + manageDb + .make(path.join(__dirname, './storage/blog.sqlite3')) + .then(function () { + return blueprint.setup(db) + }) + .then (function () { + done() + }) + .catch(done) }) after(function (done) { blueprint .tearDown(db) + .then(function () { + return manageDb.remove(path.join(__dirname, './storage/blog.sqlite3')) + }) .then (function () { done() }).catch(done) diff --git a/test/implementation/model.spec.js b/test/implementation/model.spec.js index 1c798ade..b666b929 100644 --- a/test/implementation/model.spec.js +++ b/test/implementation/model.spec.js @@ -4,6 +4,7 @@ const path = require('path') const chai = require('chai') const expect = chai.expect const Database = require('../../src/Database') +const manageDb = require('../unit/blueprints/manage') const blueprint = require('./blueprints/model-blueprint') const Model = require('../../src/Orm/Proxy/Model') @@ -15,7 +16,7 @@ let Config = { return { client: 'sqlite3', connection: { - filename: path.join(__dirname,'./storage/blog.sqlite3') + filename: path.join(__dirname,'./storage/model.sqlite3') }, debug: false } @@ -49,19 +50,27 @@ let postId = null describe('Database Implementation', function () { before(function (done) { - blueprint - .setup(db) - .then (function () { - done() - }).catch(done) + manageDb + .make(path.join(__dirname, './storage/model.sqlite3')) + .then(function () { + return blueprint.setup(db) + }) + .then (function () { + done() + }) + .catch(done) }) after(function (done) { blueprint - .tearDown(db) - .then (function () { - done() - }).catch(done) + .tearDown(db) + .then(function () { + return manageDb.remove(path.join(__dirname, './storage/model.sqlite3')) + }) + .then (function () { + done() + }) + .catch(done) }) diff --git a/test/implementation/storage/blog.sqlite3 b/test/implementation/storage/blog.sqlite3 deleted file mode 100644 index 8ef22fb1e0a24f7cbfa8c8dba619cd83839a584b..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 4096 zcmeH}L2DE-6o6l5cI}qZ(;`E`LX4MPstDHJTV-!OSnWkTEZLn`4V{@xlT7P+D2RW` zWBV^eM1+Eu;(zcZZNcuc2gS=m!puwZ@{-K^GLO7hd%MaZKhUO>j&DK>7=vxj0Xp{q zsC)G$0Lka;HdUdpo5uW_)_C-DWi{;rOU@xVXJ_nezI7b=djcO%TT5A%u}^QD92Hn= z>ueCo^Ou92{Q=+KdA2*?BIpF~36%@3Du-{;@Jc&gHATT|qe^Ke{17KxHcqQ*Y_P=2 zJ>;UXXoi2xB9&ik=#(o^@MCGl@1*HH+1l#g42^U?5!~T>KR*~-CtYJ_tZ5hNFds@M zcup;+O7vUSzB8lFvA}6{|3z{q+t^^QgZuWVpuc%&@uOtzmNR%GQC`Mx8wIm0+GO6 z0-YqymO~y$CMztYH0iaq&*dq{k0la_1QwIPQ@ZrKkJsq3KhXMEnZs?F>q@QPOFOU* Rlmvi}eINDV=Kpdfe*-lE^wt0X diff --git a/test/unit/blueprints/manage.js b/test/unit/blueprints/manage.js new file mode 100644 index 00000000..038d992a --- /dev/null +++ b/test/unit/blueprints/manage.js @@ -0,0 +1,34 @@ +'use strict' + +/** + * adonis-lucid + * Copyright(c) 2015-2015 Harminder Virk + * MIT Licensed +*/ +const fs = require('fs-extra') +module.exports = { + make: function (file) { + return new Promise(function (resolve, reject) { + fs.ensureFile(file, function (error) { + if(error) { + reject(error) + } + else { + resolve() + } + }) + }) + }, + remove: function (file) { + return new Promise(function (resolve, reject) { + fs.remove(file, function (error) { + if(error) { + reject(error) + } + else { + resolve() + } + }) + }) + } +} diff --git a/test/unit/blueprints/model-relations-blueprint.js b/test/unit/blueprints/model-relations-blueprint.js index 0f28b661..2f9835f6 100644 --- a/test/unit/blueprints/model-relations-blueprint.js +++ b/test/unit/blueprints/model-relations-blueprint.js @@ -5,7 +5,7 @@ const Q = require('q') let blueprint = exports = module.exports = {} blueprint.tearDown = function(knex) { - return Q.all([ + const tablesToRemove = [ knex.schema.dropTable('users'), knex.schema.dropTable('phones'), knex.schema.dropTable('authors'), @@ -17,12 +17,19 @@ blueprint.tearDown = function(knex) { knex.schema.dropTable('chat_operators'), knex.schema.dropTable('relation_table'), knex.schema.dropTable('cbooks') - ]) + ] + let result = Q() + tablesToRemove.forEach(function (item) { + result = result.then(function () { + return item + }) + }) + return result } blueprint.setup = function(knex) { - return Q.all([ + const setupTables = [ knex.schema.createTable('users', function (table) { table.increments() @@ -119,7 +126,14 @@ blueprint.setup = function(knex) { table.string('relation_user_id').references('co_id').inTable('chat_operators').onDelete('CASCADE') }) - ]) + ] + let result = Q() + setupTables.forEach(function (item) { + result = result.then(function () { + return item + }) + }) + return result } blueprint.seed = function(knex){ @@ -235,7 +249,7 @@ blueprint.seed = function(knex){ ] - return Q.all([ + const seeds = [ knex.table('users').insert(users), knex.table('phones').insert(phones), knex.table('authors').insert(authors), @@ -247,5 +261,12 @@ blueprint.seed = function(knex){ knex.table('chat_operators').insert(operators), knex.table('cbooks').insert(cbooks), knex.table('relation_table').insert(relational_values) - ]) + ] + let result = Q() + seeds.forEach(function (item) { + result = result.then(function () { + return item + }) + }) + return result } diff --git a/test/unit/commands.spec.js b/test/unit/commands.spec.js index 7e1a1380..dc3cb556 100644 --- a/test/unit/commands.spec.js +++ b/test/unit/commands.spec.js @@ -46,6 +46,16 @@ describe('Commands', function () { }) }) + after(function (done) { + fs.emptyDir(path.join(__dirname, './migrations'), function (error) { + if (error) { + done(error) + } else { + done() + } + }) + }) + context('Make', function () { it('should create a file inside migrations directory', function (done) { expect(Make.description).not.equal(undefined) diff --git a/test/unit/database.spec.js b/test/unit/database.spec.js index c08ede20..54700672 100644 --- a/test/unit/database.spec.js +++ b/test/unit/database.spec.js @@ -10,6 +10,7 @@ const Database = require('../../src/Database') const path = require('path') const chai = require('chai') +const manageDb = require('./blueprints/manage') const expect = chai.expect let alternateConnection = { @@ -38,18 +39,39 @@ let Config = { } describe('Database', function () { + + before(function (done) { + manageDb + .make(path.join(__dirname, './storage/test.sqlite3')) + .then(function () { + return manageDb.make(path.join(__dirname, './storage/connection.sqlite3')) + }) + .then(function () { + done() + }) + .catch(done) + }) + + after(function (done) { + manageDb + .remove(path.join(__dirname, './storage/test.sqlite3')) + .then(function () { + return manageDb.remove(path.join(__dirname, './storage/connection.sqlite3')) + }) + .then(function () { + done() + }) + .catch(done) + }) + it('should make connection with sqlite database', function () { const db = new Database(Config) expect(db.client.config.client).to.equal('sqlite3') }) - it('should be able to switch connections using connection method', function (done) { + it('should be able to switch connections using connection method', function () { const db = new Database(Config) - db.connection('new') - .table('accounts') - .then(function (accounts) { - expect(accounts).to.be.an('array') - done() - }).catch(done) + const newConnection = db.connection('new') + expect(newConnection.client.config.connection.filename).to.equal(path.join(__dirname, './storage/connection.sqlite3')) }) }) diff --git a/test/unit/model-relations.spec.js b/test/unit/model-relations.spec.js index 476feee2..ace5ec23 100644 --- a/test/unit/model-relations.spec.js +++ b/test/unit/model-relations.spec.js @@ -26,6 +26,7 @@ const expect = chai.expect const blueprint = require('./blueprints/model-relations-blueprint') const co = require('co') const Ioc = require('adonis-fold').Ioc +const manageDb = require('./blueprints/manage') const Database = require('../../src/Database') const Model = require('../../src/Orm/Proxy/Model') const _ = require('lodash') @@ -39,7 +40,7 @@ let Config = { return { client: 'sqlite3', connection: { - filename: path.join(__dirname, './storage/test.sqlite3') + filename: path.join(__dirname, './storage/relation.sqlite3') }, debug: false } @@ -57,11 +58,15 @@ const db = new Database(Config) * Tests begins here */ describe('Model Relations', function () { + before(function (done) { - blueprint - .setup(db) + manageDb + .make(path.join(__dirname, './storage/relation.sqlite3')) + .then(function () { + return blueprint.setup(db) + }) .then(function () { - blueprint.seed(db) + return blueprint.seed(db) }) .then(function () { done() @@ -72,6 +77,9 @@ describe('Model Relations', function () { after(function (done) { blueprint .tearDown(db) + .then(function () { + return manageDb.remove(path.join(__dirname, './storage/relation.sqlite3')) + }) .then(function () { done() }) diff --git a/test/unit/model.spec.js b/test/unit/model.spec.js index 52654d45..f4b3d775 100644 --- a/test/unit/model.spec.js +++ b/test/unit/model.spec.js @@ -10,6 +10,7 @@ const path = require('path') const chai = require('chai') const expect = chai.expect const co = require('co') +const manageDb = require('./blueprints/manage') const blueprint = require('./blueprints/model-blueprint') const Database = require('../../src/Database') const Model = require('../../src/Orm/Proxy/Model') @@ -20,7 +21,7 @@ let Config = { return { client: 'sqlite3', connection: { - filename: path.join(__dirname, './storage/test.sqlite3') + filename: path.join(__dirname, './storage/model.sqlite3') }, debug: false } @@ -30,19 +31,27 @@ let Config = { const db = new Database(Config) describe('Model', function () { + before(function (done) { - blueprint - .setup(db) + manageDb + .make(path.join(__dirname, './storage/model.sqlite3')) + .then(function () { + return blueprint.setup(db) + }) .then(function () { - blueprint.seed(db) + return blueprint.seed(db) }).then(function () { - done() - }).catch(done) + done() + }) + .catch(done) }) after(function (done) { blueprint .tearDown(db) + .then(function () { + return manageDb.remove(path.join(__dirname, './storage/model.sqlite3')) + }) .then(function () { done() }).catch(done) diff --git a/test/unit/runner.spec.js b/test/unit/runner.spec.js index 5dd598a1..9ee979b9 100644 --- a/test/unit/runner.spec.js +++ b/test/unit/runner.spec.js @@ -10,6 +10,7 @@ const Runner = require('../../src/Runner') const Schema = require('../../src/Schema') const chai = require('chai') +const manageDb = require('./blueprints/manage') const blueprint = require('./blueprints/schema-blueprint') const path = require('path') const expect = chai.expect @@ -23,6 +24,15 @@ describe('Runner', function () { .catch(done) }) + after(function (done) { + manageDb + .remove(path.join(__dirname, './storage/schema.sqlite3')) + .then(function () { + done() + }) + .catch(done) + }) + beforeEach(function () { const Config = { get: function (name) { diff --git a/test/unit/staticProxy.spec.js b/test/unit/staticProxy.spec.js index 3bc266ad..5e58fcb4 100644 --- a/test/unit/staticProxy.spec.js +++ b/test/unit/staticProxy.spec.js @@ -4,6 +4,7 @@ const path = require('path') const chai = require('chai') const expect = chai.expect const co = require('co') +const manageDb = require('./blueprints/manage') const blueprint = require('./blueprints/model-blueprint') const Database = require('../../src/Database') const StaticProxy = require('../../src/Orm/Proxy/Static') @@ -13,7 +14,7 @@ let Config = { return { client: 'sqlite3', connection: { - filename: path.join(__dirname, './storage/test.sqlite3') + filename: path.join(__dirname, './storage/proxy.sqlite3') }, debug: false } @@ -24,19 +25,26 @@ const db = new Database(Config) describe('StaticProxy', function () { before(function (done) { - blueprint - .setup(db) + manageDb + .make(path.join(__dirname, './storage/proxy.sqlite3')) + .then(function () { + return blueprint.setup(db) + }) .then(function () { blueprint.seed(db) - }).then(function () { - done() - }).catch(done) - + }) + .then(function () { + done() + }) + .catch(done) }) after(function (done) { blueprint .tearDown(db) + .then(function () { + return manageDb.remove(path.join(__dirname, './storage/proxy.sqlite3')) + }) .then(function () { done() }).catch(done) diff --git a/test/unit/storage/blog.sqlite3 b/test/unit/storage/blog.sqlite3 deleted file mode 100644 index e69de29b..00000000 diff --git a/test/unit/storage/connection.sqlite3 b/test/unit/storage/connection.sqlite3 deleted file mode 100644 index 973bf1685355f2a411740fe8cc92df8f38b4d1c2..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 65536 zcmeIuu}Z^G6b9g%B8Y>tQ{j?D1)Y2WV+mS}Rbv!7##*6bThKUL-@#|}IZTJlc9!pO z4~PG9hQoJvTg;kid|tlI>n5IrRY;O>8e<3{X}i~USL?jhg?6sAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBly zK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF z5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk P1PBlyK!5;&e<|<_&QvK+ diff --git a/test/unit/storage/schema.sqlite3 b/test/unit/storage/schema.sqlite3 deleted file mode 100644 index 40bee3d6165dba5478fb5ea77071fb4894eaef62..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6144 zcmeHKPixyS6ql?xA+$XXA4WkK2JzC6H6??x%hD2hSO@FY9dsXXyyX93u z^4&cAIv$;l>FMa;cud_=?b41brY;py@pGO~87nHYFr-(Bh*)w%FZm5+nTmxB5*~4> zc4p4LcKpeb;DRNc!Eoq#z}qj!!TF~mge>JrT0HFe$_w2}HEU1FBEC>>{)V)i{e7JC zf7qw76saGH^Mol8%hV6!;F2xXHs(WHZnRRY5P`0i14)xOh8B_D7pv)XyZA>DWqKV# zulG}aopBlPg-nV1a&wP2uY+30iI9=zKf_I3xo!FwMT1sKJC zxt#dLEQC+)uO7~Tk+(<7jEm|2x#bplK$zSiat9fc3vlz5 zY?1W(kI55)+a$w)VW2(?5KK_}KU_UU=(GOw^L>4oFh~spHD#bY|JM}0fo&M5H3NSE DmIW^J diff --git a/test/unit/storage/test.sqlite3 b/test/unit/storage/test.sqlite3 deleted file mode 100644 index dd742de126c8aa3e01ca50ec8b25d30887e2e73c..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 17408 zcmeHO&2Jk;6rb7gN7f%rLNKcwLZ$`@TTRs@C1Bs0h9IaaN@!Jtgs?XDBw6C@I=kyY z5CSEFzkmZ0Qcu12#F;B+s+L}H4#>%KW(7p|zAzUA1JF1f*(cV?%r%*i^Ao$<8!-Nuk(g$t(fjQUl!}!Z2FF6S6#6pY=5k+i+RUcuCz68 zFKc?jm1S$DTU|2U!sOIcaSTBf)}oF57Iw!Hc2wl9SO974s_=Neifk`m$nO?AHm`}g zfL~{3`tr>5YqR_H7~a04$0gIRI1S;NzT;xgRR_mdzq#_%xR6cW>5EWvRg zifC(?fMht%e+Q>*o(Z>Iy6H(CV&%z0EGn7gJ<%hAEp2mUuSDh|@LEqBNb(c}d|OKxr%uuL zq$u8M9j0!@6RS=65Jj^rB3?(`0*-eKS|VhaP5|jZaiBh+6i@~T`8a*kKm$NYpac-S z59GU{{e1u|l@tOBfx|^0r}dHC&@c?bww9OYP1mgY)|$xCkiBNP%L|SZuyJzZs?Ppo z3wu9@D~Y?H*usnJlo?biJPhYQ zV?PkK&F;fVC53=O;5Z{N$dY6Tw&wA9^Am&4-D~{(bw*2)9PZXpAuuwul|wz?xh(sN z#Mvdnw%Bd9$}X{QkF)S<>njMMP`O?iCC*=~)A z$+Is{z4B_QRpWbE;~ofbQV;s*+QrS$xK4Ck-ekN~E|p5B{01QY_jjKB$+AR{2)Ri|dY4x0g-{~G;~uo4`ehfJYe z`i&3L()8YLP~9;pM|Uj!5qo1kT8MVl!2BHALl0(s5ARj!YF~W{236SnP-A3-emN&i zs^KOpJZGaqh3qFX6So>yx?8_-H`d`^2=;jEo!o<%$(74QxlE$I5%52Bb(zTd&weHB zF&tD<2q*-45`mPKAUTi>O#ms-0q`0P0KxiyjcA|IYveZR0SjO@lk63LcF(I{o1dqW zJ*a-|1TS=3ayz0pLd7o{ydjPA5VO%eV56gpHbpVo%Z`^|-=2y%|6}YM!XC1p*mrQD zl0rZsa0C#@#}ec;*b(1yYBD~Li;p3g%lD*-@2}v)4JnAJo{EHMTRb?qe(f z_VS@O04qNl056zprdM^XhL7`~vOfrXRZ<8j1bQEVG`s{15c5sDW|>*GMKpGc!0LYs z)_?iweF&6<3V~igKu`1$9o%}T!*7waj!u5UQn5aW;+Nh(O=obeV@NMX0OfRu0t_xw zx{fsh``;b(|KEYEN(upm!0|`m83+O%m9K#F>w>@Qxb>Pg1atodIqBaJ^|}*q1N&dg z{};scV{AXh{fC5A!uWqI3n~0>5U^A_S_mlrpYs15-2X?}WAOhSttVKGMj@aO_-_#C z^#4)z3xTgn3IT;cZz5oT&F>6|hAw~j-?>?{>_rH{tI7C3%Kj$sRY@VB5ct0!5T`Ms H_fPx-Jcl*R From d5ebb69cb27c474a028bbe68c53932ec6d6ba28b Mon Sep 17 00:00:00 2001 From: Harminder Virk Date: Sun, 27 Dec 2015 23:28:38 +0530 Subject: [PATCH 10/17] Updated migrations commands output --- src/Commands/Make.js | 4 +++- src/Commands/Rollback.js | 14 +++++++++++++- src/Commands/Run.js | 13 ++++++++++++- 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/Commands/Make.js b/src/Commands/Make.js index 83efc57a..1a15d5c0 100644 --- a/src/Commands/Make.js +++ b/src/Commands/Make.js @@ -42,6 +42,8 @@ Make.signature = '{name}' */ Make.handle = function (options) { const helpers = Ioc.make('Adonis/Src/Helpers') + const Console = Ioc.use('Adonis/Src/Console') + return new Promise((resolve, reject) => { const name = `${new Date().getTime()}_${options.name}.js` const migrationPath = helpers.migrationsPath(name) @@ -50,7 +52,7 @@ Make.handle = function (options) { if (error) { reject(error) } else { - resolve(`Created ${name} migration successfully`) + Console.success(Console.icon('success') + ' created %s migration successfully', name) } }) }) diff --git a/src/Commands/Rollback.js b/src/Commands/Rollback.js index f646d3d9..60c5345c 100644 --- a/src/Commands/Rollback.js +++ b/src/Commands/Rollback.js @@ -26,11 +26,23 @@ Rollback.signature = '{--force?}' Rollback.handle = function * (options, flags) { const Helpers = Ioc.make('Adonis/Src/Helpers') const Runner = Ioc.make('Adonis/Src/Runner') + const Console = Ioc.use('Adonis/Src/Console') const migrations = Helpers.migrationsPath() if (process.env.NODE_ENV === 'production' && !flags.force) { throw new Error('Cannot run migrations in production') } const migrationsFiles = autoLoader.load(migrations) - return yield Runner.down(migrationsFiles) + const response = yield Runner.down(migrationsFiles) + + if(response.status === 'completed') { + Console.success(Console.icon('success') + ' latest migrations batch has been rolled back') + return + } + + if(response.status === 'skipped') { + Console.info(Console.icon('info') + ' already at the last batch') + return + } + } diff --git a/src/Commands/Run.js b/src/Commands/Run.js index 01b01f31..7e2e4e68 100644 --- a/src/Commands/Run.js +++ b/src/Commands/Run.js @@ -26,11 +26,22 @@ Run.signature = '{--force?}' Run.handle = function * (options, flags) { const Helpers = Ioc.make('Adonis/Src/Helpers') const Runner = Ioc.make('Adonis/Src/Runner') + const Console = Ioc.use('Adonis/Src/Console') const migrations = Helpers.migrationsPath() if (process.env.NODE_ENV === 'production' && !flags.force) { throw new Error('Cannot run migrations in production') } const migrationsFiles = autoLoader.load(migrations) - return yield Runner.up(migrationsFiles) + const response = yield Runner.up(migrationsFiles) + + if(response.status === 'completed') { + Console.success(Console.icon('success') + ' database migrated successfully') + return + } + + if(response.status === 'skipped') { + Console.info(Console.icon('info') + ' already the latest version') + return + } } From 0e57b3c77e545a94641c946c0d95e22e65922e92 Mon Sep 17 00:00:00 2001 From: Harminder Virk Date: Sun, 27 Dec 2015 23:49:54 +0530 Subject: [PATCH 11/17] Improved tests for database and migrations command --- src/Commands/Make.js | 1 + src/Commands/Rollback.js | 7 +-- src/Commands/Run.js | 6 +- test/unit/commands.spec.js | 122 ++++++++++++++++++++++++++++++++++++- test/unit/database.spec.js | 12 ++++ 5 files changed, 137 insertions(+), 11 deletions(-) diff --git a/src/Commands/Make.js b/src/Commands/Make.js index 1a15d5c0..c85721ea 100644 --- a/src/Commands/Make.js +++ b/src/Commands/Make.js @@ -53,6 +53,7 @@ Make.handle = function (options) { reject(error) } else { Console.success(Console.icon('success') + ' created %s migration successfully', name) + resolve() } }) }) diff --git a/src/Commands/Rollback.js b/src/Commands/Rollback.js index 60c5345c..a3b9a3ab 100644 --- a/src/Commands/Rollback.js +++ b/src/Commands/Rollback.js @@ -35,14 +35,11 @@ Rollback.handle = function * (options, flags) { const migrationsFiles = autoLoader.load(migrations) const response = yield Runner.down(migrationsFiles) - if(response.status === 'completed') { + if (response.status === 'completed') { Console.success(Console.icon('success') + ' latest migrations batch has been rolled back') - return } - if(response.status === 'skipped') { + if (response.status === 'skipped') { Console.info(Console.icon('info') + ' already at the last batch') - return } - } diff --git a/src/Commands/Run.js b/src/Commands/Run.js index 7e2e4e68..a2420d73 100644 --- a/src/Commands/Run.js +++ b/src/Commands/Run.js @@ -35,13 +35,11 @@ Run.handle = function * (options, flags) { const migrationsFiles = autoLoader.load(migrations) const response = yield Runner.up(migrationsFiles) - if(response.status === 'completed') { + if (response.status === 'completed') { Console.success(Console.icon('success') + ' database migrated successfully') - return } - if(response.status === 'skipped') { + if (response.status === 'skipped') { Console.info(Console.icon('info') + ' already the latest version') - return } } diff --git a/test/unit/commands.spec.js b/test/unit/commands.spec.js index dc3cb556..819559c5 100644 --- a/test/unit/commands.spec.js +++ b/test/unit/commands.spec.js @@ -32,6 +32,21 @@ const Helpers = { Ioc.bind('Adonis/Src/Helpers', function () { return Helpers }) + +Ioc.bind('Adonis/Src/Console', function () { + return { + icon: function () { + return '' + }, + success: function () { + }, + info: function () { + + } + } +}) + + describe('Commands', function () { before(function (done) { GLOBAL.use = function () { @@ -57,10 +72,35 @@ describe('Commands', function () { }) context('Make', function () { + + it('should handle error when unable to create migrations file', function (done) { + expect(Make.description).not.equal(undefined) + expect(Make.signature).not.equal(undefined) + + Ioc.bind('Adonis/Src/Helpers', function () { + return { + migrationsPath: function (name) { + return path.join(__dirname, './mg', name) + } + } + }) + + Make + .handle({name: 'create_users_table'}) + .catch(function (error) { + expect(error.code).to.equal('ENOENT') + done() + }) + }) + it('should create a file inside migrations directory', function (done) { expect(Make.description).not.equal(undefined) expect(Make.signature).not.equal(undefined) + Ioc.bind('Adonis/Src/Helpers', function () { + return Helpers + }) + Make .handle({name: 'create_users_table'}) .then(function () { @@ -80,7 +120,9 @@ describe('Commands', function () { it('should throw error when running in production environment', function (done) { process.env.NODE_ENV = 'production' const Runner = { - up: function * (files) {} + up: function * (files) { + return {status:'completed'} + } } Ioc.bind('Adonis/Src/Runner', function () { return Runner @@ -101,6 +143,7 @@ describe('Commands', function () { const Runner = { up: function * (files) { migrations = files + return {status:'completed'} } } Ioc.bind('Adonis/Src/Runner', function () { @@ -119,13 +162,47 @@ describe('Commands', function () { }) .catch(done) }) + + it('should pass handle skipped status', function (done) { + process.env.NODE_ENV = 'development' + let infoCalled = false + const Runner = { + up: function * (files) { + return {status:'skipped'} + } + } + Ioc.bind('Adonis/Src/Runner', function () { + return Runner + }) + Ioc.bind('Adonis/Src/Console', function () { + return { + icon: function () {}, + info: function () { + infoCalled = true + } + } + }) + expect(Run.description).not.equal(undefined) + expect(Run.signature).not.equal(undefined) + + co(function * () { + return yield Run.handle({}, {}) + }) + .then(function () { + expect(infoCalled).to.equal(true) + done() + }) + .catch(done) + }) }) context('Rollback', function () { it('should throw error when running in production environment', function (done) { process.env.NODE_ENV = 'production' const Runner = { - up: function * (files) {} + up: function * (files) { + return {status:'completed'} + } } Ioc.bind('Adonis/Src/Runner', function () { return Runner @@ -146,11 +223,19 @@ describe('Commands', function () { const Runner = { down: function * (files) { migrations = files + return {status:'completed'} } } Ioc.bind('Adonis/Src/Runner', function () { return Runner }) + Ioc.bind('Adonis/Src/Console', function () { + return { + icon: function () {}, + info: function () {}, + success: function () {} + } + }) expect(Rollback.description).not.equal(undefined) expect(Rollback.signature).not.equal(undefined) @@ -164,5 +249,38 @@ describe('Commands', function () { }) .catch(done) }) + + it('should handle skipped status', function (done) { + process.env.NODE_ENV = 'development' + let infoCalled = false + const Runner = { + down: function * (files) { + return {status:'skipped'} + } + } + Ioc.bind('Adonis/Src/Runner', function () { + return Runner + }) + Ioc.bind('Adonis/Src/Console', function () { + return { + icon: function () {}, + info: function () { + infoCalled = true + }, + success: function () {} + } + }) + expect(Rollback.description).not.equal(undefined) + expect(Rollback.signature).not.equal(undefined) + + co(function * () { + return yield Rollback.handle({}, {}) + }) + .then(function () { + expect(infoCalled).to.equal(true) + done() + }) + .catch(done) + }) }) }) diff --git a/test/unit/database.spec.js b/test/unit/database.spec.js index 54700672..dadd1306 100644 --- a/test/unit/database.spec.js +++ b/test/unit/database.spec.js @@ -64,6 +64,18 @@ describe('Database', function () { .catch(done) }) + it('should throw error when database connection is not defined', function () { + const Config = { + get: function () { + return undefined + } + } + const db = function () { + return new Database(Config) + } + expect(db).to.throw(/Specify connection under config\/database file/) + }) + it('should make connection with sqlite database', function () { const db = new Database(Config) expect(db.client.config.client).to.equal('sqlite3') From 44af4e33d95e2dbfd65fc5165d0e048e28d38731 Mon Sep 17 00:00:00 2001 From: Harminder Virk Date: Tue, 29 Dec 2015 14:21:49 +0530 Subject: [PATCH 12/17] Did clean up ace commands --- src/Commands/Make.js | 37 ++++++++++++++++++++++------------- test/unit/commands.spec.js | 40 ++++++++++++++++++++++---------------- 2 files changed, 47 insertions(+), 30 deletions(-) diff --git a/src/Commands/Make.js b/src/Commands/Make.js index c85721ea..2f24d5f9 100644 --- a/src/Commands/Make.js +++ b/src/Commands/Make.js @@ -32,6 +32,25 @@ let Make = exports = module.exports = {} Make.description = 'Create a new migration file' Make.signature = '{name}' +/** + * @description writes file with content to a given path + * @method writeFile + * @param {String} migrationPath + * @param {String} migrationContent + * @return {Object} + * @public + */ +Make.writeFile = function (migrationPath, migrationContent) { + return new Promise((resolve, reject) => { + fs.writeFile(migrationPath, migrationContent, function (error) { + if (error) { + return reject(error) + } + resolve() + }) + }) +} + /** * @description creates a new migration file * @method handle @@ -40,21 +59,13 @@ Make.signature = '{name}' * @return {Object} * @public */ -Make.handle = function (options) { +Make.handle = function * (options) { const helpers = Ioc.make('Adonis/Src/Helpers') const Console = Ioc.use('Adonis/Src/Console') - return new Promise((resolve, reject) => { - const name = `${new Date().getTime()}_${options.name}.js` - const migrationPath = helpers.migrationsPath(name) + const name = `${new Date().getTime()}_${options.name}.js` + const migrationPath = helpers.migrationsPath(name) - fs.writeFile(migrationPath, migrationContent, function (error) { - if (error) { - reject(error) - } else { - Console.success(Console.icon('success') + ' created %s migration successfully', name) - resolve() - } - }) - }) + yield Make.writeFile(migrationPath, migrationContent) + Console.success(Console.icon('success') + ' created %s migration successfully', name) } diff --git a/test/unit/commands.spec.js b/test/unit/commands.spec.js index 819559c5..94d95e91 100644 --- a/test/unit/commands.spec.js +++ b/test/unit/commands.spec.js @@ -85,12 +85,16 @@ describe('Commands', function () { } }) - Make - .handle({name: 'create_users_table'}) - .catch(function (error) { - expect(error.code).to.equal('ENOENT') - done() - }) + co(function *() { + yield Make.handle({name: 'create_users_table'}) + }) + .then(function (){ + + }) + .catch(function(error) { + expect(error.code).to.equal('ENOENT') + done() + }) }) it('should create a file inside migrations directory', function (done) { @@ -101,18 +105,20 @@ describe('Commands', function () { return Helpers }) - Make - .handle({name: 'create_users_table'}) - .then(function () { - fs.ensureFile(migName, function (err) { - if (err) { - done(err) - } else { - done() - } - }) + co(function * () { + yield Make.handle({name: 'create_users_table'}) + }) + .then(function () { + fs.ensureFile(migName, function (err) { + if (err) { + done(err) + } else { + done() + } }) - .catch(done) + }) + .catch(done) + }) }) From 9e53e00d115c111b994af33c816f239bd8a49c0b Mon Sep 17 00:00:00 2001 From: Harminder Virk Date: Tue, 29 Dec 2015 14:22:24 +0530 Subject: [PATCH 13/17] Closing knex connection after migrations --- src/Runner/index.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Runner/index.js b/src/Runner/index.js index 55696bb5..eefd441b 100644 --- a/src/Runner/index.js +++ b/src/Runner/index.js @@ -323,6 +323,7 @@ class Runner { return '' }) .then((response) => { + this.knex.destroy() const status = _.size(migrated) > 0 ? 'completed' : 'skipped' resolve({migrated, status}) }) @@ -348,6 +349,7 @@ class Runner { } }) .then((response) => { + this.knex.destroy() const status = _.size(migrated) > 0 ? 'completed' : 'skipped' resolve({migrated, status}) }) From 6ae14d2625a695374d4bbc2a4c840159f10d68df Mon Sep 17 00:00:00 2001 From: Harminder Virk Date: Wed, 30 Dec 2015 00:22:17 +0530 Subject: [PATCH 14/17] Removed cz changelog and referencing fold from npm --- .cz.json | 3 --- package.json | 5 ++--- 2 files changed, 2 insertions(+), 6 deletions(-) delete mode 100644 .cz.json diff --git a/.cz.json b/.cz.json deleted file mode 100644 index d32f3fc5..00000000 --- a/.cz.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "path": "node_modules/cz-conventional-changelog/" -} \ No newline at end of file diff --git a/package.json b/package.json index bfe69626..83f55970 100644 --- a/package.json +++ b/package.json @@ -11,11 +11,10 @@ "author": "adonisjs", "license": "MIT", "devDependencies": { - "adonis-fold": "file:///Users/harmindervirk/workspace/v2Launch/fold", + "adonis-fold": "^2.0.0", "chai": "^3.2.0", "co": "^4.6.0", "coveralls": "^2.11.4", - "cz-conventional-changelog": "^1.1.2", "fs-extra": "^0.26.3", "istanbul": "^0.3.20", "loadtest": "^1.2.14", @@ -26,7 +25,7 @@ "standard": "^5.4.1" }, "peerDependencies": { - "adonis-fold": "file:///Users/harmindervirk/workspace/v2Launch/fold" + "adonis-fold": "^2.0.0" }, "dependencies": { "auto-loader": "^0.2.0", From 9092a1f5beb415d52f156c6b21a3604776f95719 Mon Sep 17 00:00:00 2001 From: Harminder Virk Date: Wed, 30 Dec 2015 00:22:48 +0530 Subject: [PATCH 15/17] Forcing travis to rebuild gcc --- .travis.yml | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/.travis.yml b/.travis.yml index 6f6b09d0..c2bc143a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,4 +1,12 @@ language: node_js +env: + - CXX=g++-4.8 +addons: + apt: + sources: + - ubuntu-toolchain-r-test + packages: + - g++-4.8 node_js: - "4.0.0" - "5.3.0" From 1679c23dd35b3f88764e73d31eced15d92d09514 Mon Sep 17 00:00:00 2001 From: Harminder Virk Date: Wed, 30 Dec 2015 00:26:31 +0530 Subject: [PATCH 16/17] Added .gitkeep to storage folder under unit test This was causing sqlite to fail --- test/unit/storage/.gitkeep | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 test/unit/storage/.gitkeep diff --git a/test/unit/storage/.gitkeep b/test/unit/storage/.gitkeep new file mode 100644 index 00000000..e69de29b From 1607304e85c6cad62fa1e91cbda64854184a4662 Mon Sep 17 00:00:00 2001 From: Harminder Virk Date: Wed, 30 Dec 2015 00:27:01 +0530 Subject: [PATCH 17/17] Added .gitkeep to storage folder under unit test This was causing sqlite to fail --- test/implementation/storage/.gitkeep | 0 test/unit/migrations/.gitkeep | 0 2 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 test/implementation/storage/.gitkeep create mode 100644 test/unit/migrations/.gitkeep diff --git a/test/implementation/storage/.gitkeep b/test/implementation/storage/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/test/unit/migrations/.gitkeep b/test/unit/migrations/.gitkeep new file mode 100644 index 00000000..e69de29b