ff-admin-server/src/migrations/1728563204766-protocolTables.ts

155 lines
5.7 KiB
TypeScript
Raw Normal View History

2024-10-10 14:44:41 +02:00
import { MigrationInterface, QueryRunner, Table, TableForeignKey } from "typeorm";
import { DB_TYPE } from "../env.defaults";
export class ProtocolTables1728563204766 implements MigrationInterface {
name = "ProtocolTables1728563204766";
public async up(queryRunner: QueryRunner): Promise<void> {
const variableType_int = DB_TYPE == "mysql" ? "int" : "integer";
await queryRunner.createTable(
new Table({
name: "protocol_agenda",
columns: [
{ name: "id", type: variableType_int, isPrimary: true, isGenerated: true, generationStrategy: "increment" },
{ name: "topic", type: "varchar", length: "255", isNullable: false },
{ name: "context", type: "varchar", length: "255", default: "''", isNullable: false },
{ name: "protocolId", type: variableType_int, isNullable: false },
],
}),
true
);
await queryRunner.createTable(
new Table({
name: "protocol_decisions",
columns: [
{ name: "id", type: variableType_int, isPrimary: true, isGenerated: true, generationStrategy: "increment" },
{ name: "topic", type: "varchar", length: "255", isNullable: false },
{ name: "context", type: "varchar", length: "255", default: "''", isNullable: false },
{ name: "protocolId", type: variableType_int, isNullable: false },
],
}),
true
);
await queryRunner.createTable(
new Table({
name: "protocol_presence",
columns: [
{ name: "memberId", type: variableType_int, isPrimary: true, isNullable: false },
{ name: "protocolId", type: variableType_int, isPrimary: true, isNullable: false },
],
}),
true
);
await queryRunner.createTable(
new Table({
name: "protocol_votings",
columns: [
{ name: "id", type: variableType_int, isPrimary: true, isGenerated: true, generationStrategy: "increment" },
{ name: "topic", type: "varchar", length: "255", isNullable: false },
{ name: "context", type: "varchar", length: "255", default: "''", isNullable: false },
{ name: "favour", type: variableType_int, default: 0, isNullable: false },
{ name: "abstain", type: variableType_int, default: 0, isNullable: false },
{ name: "against", type: variableType_int, default: 0, isNullable: false },
{ name: "protocolId", type: variableType_int, isNullable: false },
],
}),
true
);
await queryRunner.createForeignKey(
"protocol_agenda",
new TableForeignKey({
columnNames: ["protocolId"],
referencedColumnNames: ["id"],
referencedTableName: "protocol",
onDelete: "CASCADE",
onUpdate: "RESTRICT",
})
);
await queryRunner.createForeignKey(
"protocol_decisions",
new TableForeignKey({
columnNames: ["protocolId"],
referencedColumnNames: ["id"],
referencedTableName: "protocol",
onDelete: "CASCADE",
onUpdate: "RESTRICT",
})
);
await queryRunner.createForeignKey(
"protocol_votings",
new TableForeignKey({
columnNames: ["protocolId"],
referencedColumnNames: ["id"],
referencedTableName: "protocol",
onDelete: "CASCADE",
onUpdate: "RESTRICT",
})
);
await queryRunner.createForeignKey(
"protocol_presence",
new TableForeignKey({
columnNames: ["protocolId"],
referencedColumnNames: ["id"],
referencedTableName: "protocol",
onDelete: "CASCADE",
onUpdate: "RESTRICT",
})
);
await queryRunner.createForeignKey(
"protocol_presence",
new TableForeignKey({
columnNames: ["memberId"],
referencedColumnNames: ["id"],
referencedTableName: "member",
onDelete: "CASCADE",
onUpdate: "RESTRICT",
})
);
}
public async down(queryRunner: QueryRunner): Promise<void> {
const tableProtocolVotings = await queryRunner.getTable("protocol_votings");
const foreignKeyProtocolVotings = tableProtocolVotings.foreignKeys.find(
(fk) => fk.columnNames.indexOf("protocolId") !== -1
);
await queryRunner.dropForeignKey("protocol_votings", foreignKeyProtocolVotings);
const tableProtocolDecisions = await queryRunner.getTable("protocol_decisions");
const foreignKeyProtocolDecisions = tableProtocolDecisions.foreignKeys.find(
(fk) => fk.columnNames.indexOf("protocolId") !== -1
);
await queryRunner.dropForeignKey("protocol_decisions", foreignKeyProtocolDecisions);
const tableProtocolAgenda = await queryRunner.getTable("protocol_agenda");
const foreignKeyProtocolAgenda = tableProtocolAgenda.foreignKeys.find(
(fk) => fk.columnNames.indexOf("protocolId") !== -1
);
await queryRunner.dropForeignKey("protocol_agenda", foreignKeyProtocolAgenda);
const tableProtocolPresence_protcol = await queryRunner.getTable("protocol_presence");
const foreignKeyProtocolPresence_protcol = tableProtocolPresence_protcol.foreignKeys.find(
(fk) => fk.columnNames.indexOf("protocolId") !== -1
);
await queryRunner.dropForeignKey("protocol_presence", foreignKeyProtocolPresence_protcol);
const tableProtocolPresence_member = await queryRunner.getTable("protocol_presence");
const foreignKeyProtocolPresence_member = tableProtocolPresence_member.foreignKeys.find(
(fk) => fk.columnNames.indexOf("memberId") !== -1
);
await queryRunner.dropForeignKey("protocol_presence", foreignKeyProtocolPresence_member);
await queryRunner.dropTable("protocol_votings");
await queryRunner.dropTable("protocol_presence");
await queryRunner.dropTable("protocol_decisions");
await queryRunner.dropTable("protocol_agenda");
}
}