import { MigrationInterface, QueryRunner, Table, TableForeignKey } from "typeorm"; import { DB_TYPE } from "../env.defaults"; export class Protocol1729347911107 implements MigrationInterface { name = "Protocol1729347911107"; public async up(queryRunner: QueryRunner): Promise { const variableType_int = DB_TYPE == "mysql" ? "int" : "integer"; await queryRunner.createTable( new Table({ name: "protocol", columns: [ { name: "id", type: variableType_int, isPrimary: true, isGenerated: true, generationStrategy: "increment" }, { name: "title", type: "varchar", length: "255", isNullable: false }, { name: "date", type: "date", isNullable: false }, { name: "starttime", type: "timestamp", isNullable: true }, { name: "endtime", type: "timestamp", isNullable: true }, { name: "summary", type: "text", isNullable: true }, ], }), true ); 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: "text", default: "''", isNullable: false }, { name: "protocolId", type: variableType_int, isNullable: false }, ], }), true ); await queryRunner.createTable( new Table({ name: "protocol_decision", columns: [ { name: "id", type: variableType_int, isPrimary: true, isGenerated: true, generationStrategy: "increment" }, { name: "topic", type: "varchar", length: "255", isNullable: false }, { name: "context", type: "text", 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_voting", columns: [ { name: "id", type: variableType_int, isPrimary: true, isGenerated: true, generationStrategy: "increment" }, { name: "topic", type: "varchar", length: "255", isNullable: false }, { name: "context", type: "text", 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.createTable( new Table({ name: "protocol_printout", columns: [ { name: "id", type: variableType_int, isPrimary: true, isGenerated: true, generationStrategy: "increment" }, { name: "title", type: "varchar", length: "255", isNullable: false }, { name: "iteration", type: variableType_int, default: 1, isNullable: false }, { name: "filename", type: "varchar", length: "255", isNullable: false }, { name: "createdAt", type: "datetime(6)", isNullable: false, default: "CURRENT_TIMESTAMP(6)" }, { 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_decision", new TableForeignKey({ columnNames: ["protocolId"], referencedColumnNames: ["id"], referencedTableName: "protocol", onDelete: "CASCADE", onUpdate: "RESTRICT", }) ); await queryRunner.createForeignKey( "protocol_voting", 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", }) ); await queryRunner.createForeignKey( "protocol_printout", new TableForeignKey({ columnNames: ["protocolId"], referencedColumnNames: ["id"], referencedTableName: "protocol", onDelete: "CASCADE", onUpdate: "RESTRICT", }) ); } public async down(queryRunner: QueryRunner): Promise { const tableProtocolVotings = await queryRunner.getTable("protocol_voting"); const foreignKeyProtocolVotings = tableProtocolVotings.foreignKeys.find( (fk) => fk.columnNames.indexOf("protocolId") !== -1 ); await queryRunner.dropForeignKey("protocol_voting", foreignKeyProtocolVotings); const tableProtocolDecisions = await queryRunner.getTable("protocol_decision"); const foreignKeyProtocolDecisions = tableProtocolDecisions.foreignKeys.find( (fk) => fk.columnNames.indexOf("protocolId") !== -1 ); await queryRunner.dropForeignKey("protocol_decision", 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); const tableProtocolPrintout = await queryRunner.getTable("protocol_printout"); const foreignKeyProtocolPrintout = tableProtocolPrintout.foreignKeys.find( (fk) => fk.columnNames.indexOf("protocolId") !== -1 ); await queryRunner.dropForeignKey("protocol_printout", foreignKeyProtocolPrintout); await queryRunner.dropTable("protocol_printout"); await queryRunner.dropTable("protocol_voting"); await queryRunner.dropTable("protocol_presence"); await queryRunner.dropTable("protocol_decision"); await queryRunner.dropTable("protocol_agenda"); await queryRunner.dropTable("protocol"); } }