Skip to content

Commit

Permalink
fix(migrations): use hasTable and createTable
Browse files Browse the repository at this point in the history
using createTableIfNotExists, kinds of breaks in postgres, check related issue

Closes #172
  • Loading branch information
thetutlage committed Oct 2, 2017
1 parent 843a032 commit f12d51b
Showing 1 changed file with 26 additions and 12 deletions.
38 changes: 26 additions & 12 deletions src/Migration/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,23 @@ class Migration {
*
* @private
*/
_makeMigrationsTable () {
return this.db.schema.createTableIfNotExists(this._migrationsTable, (table) => {
table.increments()
table.string('name')
table.integer('batch')
table.timestamp('migration_time').defaultsTo(this.db.fn.now())
})
async _makeMigrationsTable () {
const hasTable = await this.db.schema.hasTable(this._migrationsTable)

/**
* We need to run 2 queries, since `createTableIfNotExists` doesn't
* work with postgres. Check following issue for more info.
*
* https://github.com/adonisjs/adonis-lucid/issues/172
*/
if (!hasTable) {
await this.db.schema.createTable(this._migrationsTable, (table) => {
table.increments()
table.string('name')
table.integer('batch')
table.timestamp('migration_time').defaultsTo(this.db.fn.now())
})
}
}

/**
Expand All @@ -61,11 +71,15 @@ class Migration {
*
* @private
*/
_makeLockTable () {
return this.db.schema.createTableIfNotExists(this._lockTable, (table) => {
table.increments()
table.boolean('is_locked')
})
async _makeLockTable () {
const hasTable = await this.db.schema.hasTable(this._lockTable)

if (!hasTable) {
await this.db.schema.createTable(this._lockTable, (table) => {
table.increments()
table.boolean('is_locked')
})
}
}

/**
Expand Down

0 comments on commit f12d51b

Please sign in to comment.