-
-
Notifications
You must be signed in to change notification settings - Fork 197
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
33dece6
commit dd56409
Showing
5 changed files
with
2,005 additions
and
270 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
'use strict' | ||
|
||
/* | ||
* adonis-lucid | ||
* | ||
* (c) Harminder Virk <virk@adonisjs.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
const { Command } = require('@adonisjs/ace') | ||
const requireAll = require('require-all') | ||
const _ = require('lodash') | ||
const { ioc } = require('@adonisjs/fold') | ||
|
||
class SeedDatabase extends Command { | ||
constructor (Helpers) { | ||
super() | ||
this._seedsPath = Helpers.seedsPath() | ||
} | ||
|
||
/** | ||
* IoC container injections | ||
* | ||
* @method inject | ||
* | ||
* @return {Array} | ||
*/ | ||
static get inject () { | ||
return ['Adonis/Src/Helpers'] | ||
} | ||
|
||
/** | ||
* Returns an object of all schema files | ||
* | ||
* @method _getSeedFiles | ||
* | ||
* @return {Object} | ||
* | ||
* @private | ||
*/ | ||
_getSeedFiles (selectedFiles) { | ||
return requireAll({ | ||
dirname: this._seedsPath, | ||
filters: /(.*)\.js$/, | ||
filter: (fileName) => { | ||
if (!selectedFiles) { | ||
return fileName | ||
} | ||
return _.find(selectedFiles, (file) => file.trim().endsWith(fileName)) | ||
} | ||
}) | ||
} | ||
|
||
/** | ||
* Throws exception when trying to run migrations are | ||
* executed in production and not using force flag. | ||
* | ||
* @method _validateState | ||
* | ||
* @param {Boolean} force | ||
* | ||
* @return {void} | ||
* | ||
* @private | ||
* | ||
* @throws {Error} If NODE_ENV is production | ||
*/ | ||
_validateState (force) { | ||
if (process.env.NODE_ENV === 'production' && !force) { | ||
throw new Error('Cannot run migrations in production. Use --force flag to continue') | ||
} | ||
} | ||
|
||
/** | ||
* Command signature required by ace | ||
* | ||
* @method signature | ||
* | ||
* @return {String} | ||
*/ | ||
static get signature () { | ||
return ` | ||
seed | ||
{ -f, --force: Forcefully seed database in production } | ||
{ --files=@value: Run only selected files } | ||
` | ||
} | ||
|
||
/** | ||
* Command description | ||
* | ||
* @method description | ||
* | ||
* @return {String} | ||
*/ | ||
static get description () { | ||
return 'Seed database using seed files' | ||
} | ||
|
||
/** | ||
* Method called when command is executed. This method will | ||
* require all files from the migrations directory | ||
* and execute all pending schema files | ||
* | ||
* @method handle | ||
* | ||
* @param {Object} args | ||
* @param {Boolean} options.force | ||
* @param {String} options.files | ||
* | ||
* @return {void|Array} | ||
*/ | ||
async handle (args, { force, files }) { | ||
this._validateState(force) | ||
|
||
files = typeof (files) === 'string' ? files.split(',') : null | ||
const allFiles = this._getSeedFiles(files) | ||
|
||
if (!_.size(allFiles)) { | ||
return this.viaAce ? this.info('Nothing to seed') : 'Nothing to seed' | ||
} | ||
|
||
for (const file of _.keys(allFiles)) { | ||
const seedInstance = ioc.make(allFiles[file]) | ||
if (typeof (seedInstance.run) === 'function') { | ||
await seedInstance.run() | ||
} else { | ||
this.warn(`${seedInstance.constructor.name} does not have a run method`) | ||
} | ||
} | ||
} | ||
} | ||
|
||
module.exports = SeedDatabase |
Oops, something went wrong.