diff options
Diffstat (limited to 'database/migrations/1658076326250_correct_token_relations.ts')
-rw-r--r-- | database/migrations/1658076326250_correct_token_relations.ts | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/database/migrations/1658076326250_correct_token_relations.ts b/database/migrations/1658076326250_correct_token_relations.ts new file mode 100644 index 0000000..5486657 --- /dev/null +++ b/database/migrations/1658076326250_correct_token_relations.ts | |||
@@ -0,0 +1,22 @@ | |||
1 | import BaseSchema from '@ioc:Adonis/Lucid/Schema'; | ||
2 | |||
3 | export default class extends BaseSchema { | ||
4 | protected tableName = 'tokens'; | ||
5 | |||
6 | public async up(): Promise<void> { | ||
7 | await this.db.rawQuery( | ||
8 | 'DELETE FROM tokens WHERE user_id NOT IN (SELECT id FROM users)', | ||
9 | ); | ||
10 | |||
11 | this.schema.alterTable(this.tableName, table => { | ||
12 | table.dropForeign('user_id'); | ||
13 | |||
14 | table.foreign('user_id').references('users.id').onDelete('cascade'); | ||
15 | }); | ||
16 | } | ||
17 | |||
18 | public async down(): Promise<void> { | ||
19 | // Don't set it back withouth onDelete as the | ||
20 | // tests will break. | ||
21 | } | ||
22 | } | ||