Skip to content

Commit

Permalink
feat(seed): auto close db on when seeder finishes
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Dec 14, 2017
1 parent 1cd50ae commit edd7640
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
17 changes: 14 additions & 3 deletions commands/Seed.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ const { ioc } = require('@adonisjs/fold')
const prettyHrTime = require('pretty-hrtime')

class SeedDatabase extends Command {
constructor (Helpers) {
constructor (Helpers, Database) {
super()
this._seedsPath = Helpers.seedsPath()
this.Database = Database
}

/**
Expand All @@ -29,7 +30,7 @@ class SeedDatabase extends Command {
* @return {Array}
*/
static get inject () {
return ['Adonis/Src/Helpers']
return ['Adonis/Src/Helpers', 'Adonis/Src/Database']
}

/**
Expand Down Expand Up @@ -85,6 +86,7 @@ class SeedDatabase extends Command {
return `
seed
{ -f, --force: Forcefully seed database in production }
{ -a, --keep-alive: Do not close database connection when seeder.run finishes }
{ --files=@value: Run only selected files }
`
}
Expand All @@ -110,10 +112,11 @@ class SeedDatabase extends Command {
* @param {Object} args
* @param {Boolean} options.force
* @param {String} options.files
* @param {String} options.keepAlive
*
* @return {void|Array}
*/
async handle (args, { force, files }) {
async handle (args, { force, files, keepAlive }) {
try {
this._validateState(force)

Expand All @@ -137,6 +140,14 @@ class SeedDatabase extends Command {

const endTime = process.hrtime(startTime)
this.success(`Seeded database in ${prettyHrTime(endTime)}`)

/**
* Close the connection when seeder are executed and keep alive is
* not passed
*/
if (!keepAlive) {
this.Database.close()
}
} catch (error) {
console.log(error)
}
Expand Down
2 changes: 1 addition & 1 deletion test/unit/lucid.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1743,7 +1743,7 @@ test.group('Model', (group) => {
class User extends Model {
static castDates (field, value) {
const formattedValue = value.format('YYYY')
casting.push({ key, value: formattedValue })
casting.push({ key: field, value: formattedValue })
return formattedValue
}
}
Expand Down

0 comments on commit edd7640

Please sign in to comment.