aboutsummaryrefslogtreecommitdiffstats
path: root/database/migrations/1658076326250_correct_token_relations.ts
blob: a083c8840ffa2c81fda23661e71237a2af77c19a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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.
  }
}