ff-admin-server/src/migrations/1749296262915-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 InternalException from "../exceptions/internalException";
2025-06-07 15:20:20 +02:00
import BackupHelper from "../helpers/backupHelper";
import { getTypeByORM, isIncrementPrimary, getDefaultByORM } from "./ormHelper";
2025-01-29 18:10:41 +01:00
2025-06-07 15:20:20 +02:00
export class BackupAndResetDatabase1749296262915 implements MigrationInterface {
name = "BackupAndResetDatabase1749296262915";
2025-01-29 18:10:41 +01:00
public async up(queryRunner: QueryRunner): Promise<void> {
2025-06-07 15:20:20 +02:00
let migrations = await queryRunner.query("SELECT name FROM migrations");
2025-01-31 13:58:07 +01:00
if (
(await queryRunner.hasTable("user")) &&
2025-06-07 15:20:20 +02:00
migrations.findIndex((m: any) => m.name == "MemberExtendData1748953828644") == -1
2025-01-31 13:58:07 +01:00
) {
2025-01-31 11:25:00 +01:00
throw new InternalException(
2025-06-07 15:20:20 +02:00
"Cannot update due to skiped version. Update to v1.6.0 Version first to prevent data loss and get access to the newer Versions."
2025-01-31 11:25:00 +01:00
);
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", ...getTypeByORM("int"), ...isIncrementPrimary },
{ name: "timestamp", ...getTypeByORM("bigint") },
{ name: "name", ...getTypeByORM("varchar") },
2025-01-29 18:10:41 +01:00
],
}),
true
);
await queryRunner.createTable(
new Table({
name: "typeorm_metadata",
columns: [
{ name: "type", ...getTypeByORM("varchar") },
{ name: "database", ...getTypeByORM("varchar", true), default: getDefaultByORM("null") },
{ name: "schema", ...getTypeByORM("varchar", true), default: getDefaultByORM("null") },
{ name: "table", ...getTypeByORM("varchar", true), default: getDefaultByORM("null") },
{ name: "name", ...getTypeByORM("varchar", true), default: getDefaultByORM("null") },
{ name: "value", ...getTypeByORM("text", true), default: getDefaultByORM("null") },
2025-01-29 18:10:41 +01:00
],
}),
true
);
}
public async down(queryRunner: QueryRunner): Promise<void> {}
}