2.5.1.js 1.22 KB
Newer Older
1
exports.up = async knex => {
2
  // Check for users using disabled strategies
3
  let protectedStrategies = []
4
  const disabledStrategies = await knex('authentication').where('isEnabled', false)
5 6 7 8 9 10
  if (disabledStrategies) {
    const incompatibleUsers = await knex('users').distinct('providerKey').whereIn('providerKey', disabledStrategies.map(s => s.key))
    if (incompatibleUsers && incompatibleUsers.length > 0) {
      protectedStrategies = incompatibleUsers.map(u => u.providerKey)
    }
  }
11

12 13 14 15
  // Delete disabled strategies
  await knex('authentication').whereNotIn('key', protectedStrategies).andWhere('isEnabled', false).del()

  // Update table schema
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
  await knex.schema
    .alterTable('authentication', table => {
      table.integer('order').unsigned().notNullable().defaultTo(0)
      table.string('strategyKey').notNullable().defaultTo('')
      table.string('displayName').notNullable().defaultTo('')
    })

  // Fix pre-2.5 strategies
  const strategies = await knex('authentication')
  let idx = 1
  for (const strategy of strategies) {
    await knex('authentication').where('key', strategy.key).update({
      strategyKey: strategy.key,
      order: (strategy.key === 'local') ? 0 : idx++
    })
  }
}

exports.down = knex => { }