template preparation

This commit is contained in:
Julian Krauser 2024-12-23 14:00:50 +01:00
parent 5f18e614be
commit 6cb8ca0a12
16 changed files with 414 additions and 21 deletions

View file

@ -0,0 +1,75 @@
import { MigrationInterface, QueryRunner, Table, TableForeignKey } from "typeorm";
import { DB_TYPE } from "../env.defaults";
import { templateUsage } from "../entity/templateUsage";
export class TemplateUsage1734949173739 implements MigrationInterface {
name = "TemplateUsage1734949173739";
public async up(queryRunner: QueryRunner): Promise<void> {
const variableType_int = DB_TYPE == "mysql" ? "int" : "integer";
await queryRunner.createTable(
new Table({
name: "template_usage",
columns: [
{ name: "scope", type: "varchar", length: "255", isPrimary: true },
{ name: "headerId", type: variableType_int, isNullable: true },
{ name: "bodyId", type: variableType_int, isNullable: true },
{ name: "footerId", type: variableType_int, isNullable: true },
],
}),
true
);
await queryRunner.manager
.createQueryBuilder()
.insert()
.into(templateUsage)
.values({ scope: "protocol" })
.orIgnore()
.execute();
await queryRunner.createForeignKey(
"template_usage",
new TableForeignKey({
columnNames: ["headerId"],
referencedColumnNames: ["id"],
referencedTableName: "template",
onDelete: "RESTRICT",
onUpdate: "RESTRICT",
})
);
await queryRunner.createForeignKey(
"template_usage",
new TableForeignKey({
columnNames: ["bodyId"],
referencedColumnNames: ["id"],
referencedTableName: "template",
onDelete: "RESTRICT",
onUpdate: "RESTRICT",
})
);
await queryRunner.createForeignKey(
"template_usage",
new TableForeignKey({
columnNames: ["footerId"],
referencedColumnNames: ["id"],
referencedTableName: "template",
onDelete: "RESTRICT",
onUpdate: "RESTRICT",
})
);
}
public async down(queryRunner: QueryRunner): Promise<void> {
const template_usage = await queryRunner.getTable("template_usage");
let foreignKey = template_usage.foreignKeys.find((fk) => fk.columnNames.indexOf("headerId") !== -1);
await queryRunner.dropForeignKey("template_usage", foreignKey);
foreignKey = template_usage.foreignKeys.find((fk) => fk.columnNames.indexOf("bodyId") !== -1);
await queryRunner.dropForeignKey("template_usage", foreignKey);
foreignKey = template_usage.foreignKeys.find((fk) => fk.columnNames.indexOf("footerId") !== -1);
await queryRunner.dropForeignKey("template_usage", foreignKey);
await queryRunner.dropTable("template_usage");
}
}