ff-admin-server/src/migrations/1724317398939-initial.ts

94 lines
2.3 KiB
TypeScript
Raw Normal View History

2024-08-25 15:36:25 +00:00
import { MigrationInterface, QueryRunner, Table, TableForeignKey } from "typeorm";
export class Initial1724317398939 implements MigrationInterface {
2024-08-25 15:36:25 +00:00
name = "Initial1724317398939";
2024-08-25 15:36:25 +00:00
public async up(queryRunner: QueryRunner): Promise<void> {
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
);
2024-08-25 15:36:25 +00:00
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
);
2024-08-25 15:36:25 +00:00
await queryRunner.createForeignKey(
"refresh",
new TableForeignKey({
columnNames: ["userId"],
referencedColumnNames: ["id"],
referencedTableName: "user",
onDelete: "No Action",
})
);
}
public async down(queryRunner: QueryRunner): Promise<void> {
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");
}
}