aboutsummaryrefslogtreecommitdiffstats
path: root/database/migrations/1658076326250_correct_token_relations.ts
blob: 1013861e908936049ca7a7f24fe17d150ff7dbe2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import { BaseSchema } from '@adonisjs/lucid/schema'

export default class extends BaseSchema {
  protected tableName = 'tokens'

  public async up(): Promise<void> {
    await this.db.rawQuery('DELETE FROM tokens WHERE user_id NOT IN (SELECT id FROM users)')

    this.schema.alterTable(this.tableName, (table) => {
      table.dropForeign('user_id')

      table.foreign('user_id').references('users.id').onDelete('cascade')
    })
  }

  public async down(): Promise<void> {
    // Don't set it back withouth onDelete as the
    // tests will break.
  }
}