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

61 lines
2.3 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-31 13:58:07 +01:00
let migrations = await queryRunner.query("SELECT `name` FROM `migrations`");
if (
(await queryRunner.hasTable("user")) &&
migrations.findIndex((m: any) => m.name == "MoveSendNewsletterFlag1737816852011") == -1
) {
2025-01-31 11:25:00 +01:00
throw new InternalException(
"Cannot update due to skiped version. Update to v1.2.2 Version first to prevent data loss and get access to the newer Versions."
);
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> {}
}