ff-admin-server/src/migrations/1738166124200-BackupAndResetDatabase.ts

55 lines
2.1 KiB
TypeScript
Raw Normal View History

2025-01-29 18:10:41 +01:00
import { MigrationInterface, QueryRunner, Table } from "typeorm";
import BackupHelper from "../helpers/backupHelper";
2025-01-30 15:58:34 +01:00
import { getTypeByORM } from "./ormHelper";
2025-01-29 18:10:41 +01:00
import InternalException from "../exceptions/internalException";
export class BackupAndResetDatabase1738166124200 implements MigrationInterface {
name = "BackupAndResetDatabase1738166124200";
public async up(queryRunner: QueryRunner): Promise<void> {
2025-01-30 15:58:34 +01:00
if ((await queryRunner.hasTable("user")) && !(await queryRunner.hasTable("salutation"))) {
2025-01-29 18:10:41 +01:00
throw new InternalException("Cannot update due to skiped version, resulting in data loss");
2025-01-30 15:58:34 +01:00
}
2025-01-29 18:10:41 +01:00
2025-01-30 15:58:34 +01:00
if (await queryRunner.hasTable("user")) {
await BackupHelper.createBackup({ collectIds: false });
}
2025-01-29 18:10:41 +01:00
await queryRunner.clearDatabase();
await queryRunner.createTable(
new Table({
name: "migrations",
columns: [
{
name: "id",
type: getTypeByORM("int"),
isPrimary: true,
isGenerated: true,
generationStrategy: "increment",
},
{ name: "timestamp", type: getTypeByORM("bigint"), isNullable: false },
{ name: "name", type: getTypeByORM("varchar"), length: "255", isNullable: false },
],
}),
true
);
await queryRunner.createTable(
new Table({
name: "typeorm_metadata",
columns: [
{ name: "type", type: getTypeByORM("varchar"), length: "255", isNullable: false },
{ name: "database", type: getTypeByORM("varchar"), length: "255", isNullable: true, default: null },
{ name: "schema", type: getTypeByORM("varchar"), length: "255", isNullable: true, default: null },
{ name: "table", type: getTypeByORM("varchar"), length: "255", isNullable: true, default: null },
{ name: "name", type: getTypeByORM("varchar"), length: "255", isNullable: true, default: null },
{ name: "value", type: getTypeByORM("text"), length: "255", isNullable: true, default: null },
],
}),
true
);
}
public async down(queryRunner: QueryRunner): Promise<void> {}
}