From 51d08095363bdcd1323ebf9c89792e3696379053 Mon Sep 17 00:00:00 2001 From: Natalia Portillo Date: Thu, 13 Nov 2025 03:09:36 +0000 Subject: [PATCH] fix: Update foreign key constraints handling in migration for CurrenciesPegging and CurrenciesInflation --- .../20200807234824_FixDumpRequiredFields.cs | 76 ++++++++++++++++++- 1 file changed, 75 insertions(+), 1 deletion(-) diff --git a/Marechai.Database/Migrations/20200807234824_FixDumpRequiredFields.cs b/Marechai.Database/Migrations/20200807234824_FixDumpRequiredFields.cs index b06e6685..3a7864e1 100644 --- a/Marechai.Database/Migrations/20200807234824_FixDumpRequiredFields.cs +++ b/Marechai.Database/Migrations/20200807234824_FixDumpRequiredFields.cs @@ -1,4 +1,4 @@ -using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Migrations; namespace Marechai.Database.Migrations { @@ -15,6 +15,46 @@ namespace Marechai.Database.Migrations migrationBuilder.AlterColumn("Dumper", "Dumps", nullable: false, oldClrType: typeof(string), oldType: "varchar(255) CHARACTER SET utf8mb4", oldNullable: true); + // MySQL/MariaDB: Disable foreign key checks to allow column modifications + migrationBuilder.Sql("SET FOREIGN_KEY_CHECKS=0"); + + // Drop foreign key constraints only if they exist before modifying columns to make them nullable + migrationBuilder.Sql(@" + IF EXISTS ( + SELECT 1 FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS + WHERE CONSTRAINT_SCHEMA = DATABASE() + AND TABLE_NAME = 'CurrenciesPegging' + AND CONSTRAINT_NAME = 'FK_CurrenciesPegging_Iso4217_SourceCode' + ) + THEN + ALTER TABLE `CurrenciesPegging` DROP FOREIGN KEY `FK_CurrenciesPegging_Iso4217_SourceCode`; + END IF; + "); + + migrationBuilder.Sql(@" + IF EXISTS ( + SELECT 1 FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS + WHERE CONSTRAINT_SCHEMA = DATABASE() + AND TABLE_NAME = 'CurrenciesPegging' + AND CONSTRAINT_NAME = 'FK_CurrenciesPegging_Iso4217_DestinationCode' + ) + THEN + ALTER TABLE `CurrenciesPegging` DROP FOREIGN KEY `FK_CurrenciesPegging_Iso4217_DestinationCode`; + END IF; + "); + + migrationBuilder.Sql(@" + IF EXISTS ( + SELECT 1 FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS + WHERE CONSTRAINT_SCHEMA = DATABASE() + AND TABLE_NAME = 'CurrenciesInflation' + AND CONSTRAINT_NAME = 'FK_CurrenciesInflation_Iso4217_CurrencyCode' + ) + THEN + ALTER TABLE `CurrenciesInflation` DROP FOREIGN KEY `FK_CurrenciesInflation_Iso4217_CurrencyCode`; + END IF; + "); + migrationBuilder.AlterColumn("SourceCode", "CurrenciesPegging", nullable: true, oldClrType: typeof(string), oldType: "varchar(3) CHARACTER SET utf8mb4"); @@ -26,6 +66,9 @@ namespace Marechai.Database.Migrations migrationBuilder.AlterColumn("CurrencyCode", "CurrenciesInflation", nullable: true, oldClrType: typeof(string), oldType: "varchar(3) CHARACTER SET utf8mb4"); + + // Re-enable foreign key checks + migrationBuilder.Sql("SET FOREIGN_KEY_CHECKS=1"); } protected override void Down(MigrationBuilder migrationBuilder) @@ -39,6 +82,9 @@ namespace Marechai.Database.Migrations migrationBuilder.AlterColumn("Dumper", "Dumps", "varchar(255) CHARACTER SET utf8mb4", nullable: true, oldClrType: typeof(string)); + // MySQL/MariaDB: Disable foreign key checks to allow column modifications + migrationBuilder.Sql("SET FOREIGN_KEY_CHECKS=0"); + migrationBuilder.AlterColumn("SourceCode", "CurrenciesPegging", "varchar(3) CHARACTER SET utf8mb4", nullable: false, oldClrType: typeof(string), oldNullable: true); @@ -49,6 +95,34 @@ namespace Marechai.Database.Migrations migrationBuilder.AlterColumn("CurrencyCode", "CurrenciesInflation", "varchar(3) CHARACTER SET utf8mb4", nullable: false, oldClrType: typeof(string), oldNullable: true); + + // Recreate foreign key constraints for Down operation (when reverting to NOT NULL) + migrationBuilder.AddForeignKey( + name: "FK_CurrenciesPegging_Iso4217_SourceCode", + table: "CurrenciesPegging", + column: "SourceCode", + principalTable: "Iso4217", + principalColumn: "Code", + onDelete: ReferentialAction.Cascade); + + migrationBuilder.AddForeignKey( + name: "FK_CurrenciesPegging_Iso4217_DestinationCode", + table: "CurrenciesPegging", + column: "DestinationCode", + principalTable: "Iso4217", + principalColumn: "Code", + onDelete: ReferentialAction.Cascade); + + migrationBuilder.AddForeignKey( + name: "FK_CurrenciesInflation_Iso4217_CurrencyCode", + table: "CurrenciesInflation", + column: "CurrencyCode", + principalTable: "Iso4217", + principalColumn: "Code", + onDelete: ReferentialAction.Cascade); + + // Re-enable foreign key checks + migrationBuilder.Sql("SET FOREIGN_KEY_CHECKS=1"); } } } \ No newline at end of file