import { MigrationInterface, QueryRunner, Table, TableForeignKey } from "typeorm"; export class Initial1724317398939 implements MigrationInterface { name = "Initial1724317398939"; public async up(queryRunner: QueryRunner): Promise { await queryRunner.createTable( new Table({ name: "user", columns: [ { name: "id", type: "int", isPrimary: true, isNullable: false, isGenerated: true, generationStrategy: "increment", }, { name: "mail", type: "varchar", length: "255", isNullable: false, }, { name: "username", type: "varchar", length: "255", isNullable: false, }, { name: "secret", type: "varchar", length: "255", isNullable: false, }, ], }), true ); await queryRunner.createTable( new Table({ name: "refresh", columns: [ { name: "id", type: "int", isPrimary: true, isNullable: false, isGenerated: true, generationStrategy: "increment", }, { name: "token", type: "varchar", length: "255", isNullable: false, }, { name: "expiry", type: "datetime", isNullable: false, }, { name: "userId", type: "int", isNullable: true, }, ], }), true ); await queryRunner.createForeignKey( "refresh", new TableForeignKey({ columnNames: ["userId"], referencedColumnNames: ["id"], referencedTableName: "user", onDelete: "No Action", }) ); } public async down(queryRunner: QueryRunner): Promise { const table = await queryRunner.getTable("refresh"); const foreignKey = table.foreignKeys.find((fk) => fk.columnNames.indexOf("userId") !== -1); await queryRunner.dropForeignKey("refresh", foreignKey); await queryRunner.dropTable("refresh"); await queryRunner.dropTable("user"); } }