const readline = require('readline')
const process = require('process')

exports.up = function (knex) {
    return knex.schema.hasColumn('signups', 'tmestamp').then(hasTimestamp => {
        return knex.schema.hasColumn('signups', 'memo').then(hasMemo => {
            if (!hasTimestamp || !hasMemo) {

                const r1 = readline.createInterface({
                    input: process.stdin,
                    output: process.stdout
                })
                return new Promise((res, rej) => {
                    return knex.schema.dropTable('signups').then(_ => {
                        return knex.schema.createTable('signups', function (table) {
                            table.increments('id').primary()
                            table.unique(['raidid', 'characterid'])
                            table.integer('raidid')
                            table.foreign('raidid').references('id').inTable('raids').onDelete('CASCADE')
                            table.integer('characterid')
                            table.foreign('characterid').references('id').inTable('characters').onDelete('CASCADE')
                            table.boolean('benched').defaultTo('false')
                            table.boolean('late')
                            table.timestamp('timestamp').defaultTo(knex.fn.now())
                            table.string('memo').nullable()
                            res()
                        }).catch(rej)
                    })
                })
            }
        })
    })
}


exports.down = function (knex, Promise) {
    return knex.schema.table('signups', function (table) {
        table.dropColumn('timestamp')
        table.dropColumn('memo')
    })
}