From 81715bf0ca8ffc5a6e142d3e5e3036aeeeebe60f Mon Sep 17 00:00:00 2001 From: admin Date: Fri, 9 Oct 2026 23:02:31 +0330 Subject: [PATCH] Fix SurahExplicitIds migration for SQL Server IDENTITY removal. Recreate Surahs table without IDENTITY instead of unsupported ALTER COLUMN. --- .../20261009192702_SurahExplicitIds.cs | 87 +++++++++++++++---- 1 file changed, 71 insertions(+), 16 deletions(-) diff --git a/src/TafsirQuran.Infrastructure/Persistence/Migrations/20261009192702_SurahExplicitIds.cs b/src/TafsirQuran.Infrastructure/Persistence/Migrations/20261009192702_SurahExplicitIds.cs index 3d1b954..66d3b5b 100644 --- a/src/TafsirQuran.Infrastructure/Persistence/Migrations/20261009192702_SurahExplicitIds.cs +++ b/src/TafsirQuran.Infrastructure/Persistence/Migrations/20261009192702_SurahExplicitIds.cs @@ -10,27 +10,82 @@ namespace TafsirQuran.Infrastructure.Persistence.Migrations /// protected override void Up(MigrationBuilder migrationBuilder) { - migrationBuilder.AlterColumn( - name: "Id", - table: "Surahs", - type: "int", - nullable: false, - oldClrType: typeof(int), - oldType: "int") - .OldAnnotation("SqlServer:Identity", "1, 1"); + // SQL Server cannot ALTER IDENTITY off a column; recreate Surahs without identity. + migrationBuilder.Sql(""" + IF OBJECT_ID(N'[FK_Ayahs_Surahs_SurahId]', N'F') IS NOT NULL + ALTER TABLE [Ayahs] DROP CONSTRAINT [FK_Ayahs_Surahs_SurahId]; + IF OBJECT_ID(N'[FK_Episodes_Surahs_SurahId]', N'F') IS NOT NULL + ALTER TABLE [Episodes] DROP CONSTRAINT [FK_Episodes_Surahs_SurahId]; + + IF OBJECT_ID(N'[Surahs]', N'U') IS NOT NULL + BEGIN + SELECT * INTO [#SurahsBackup] FROM [Surahs]; + DROP TABLE [Surahs]; + END + + CREATE TABLE [Surahs] ( + [Id] int NOT NULL, + [Number] int NOT NULL, + [NameAr] nvarchar(80) NOT NULL, + [NameFa] nvarchar(80) NOT NULL, + CONSTRAINT [PK_Surahs] PRIMARY KEY ([Id]) + ); + + CREATE UNIQUE INDEX [IX_Surahs_Number] ON [Surahs] ([Number]); + + IF OBJECT_ID(N'tempdb..#SurahsBackup') IS NOT NULL + BEGIN + INSERT INTO [Surahs] ([Id], [Number], [NameAr], [NameFa]) + SELECT [Id], [Number], [NameAr], [NameFa] FROM [#SurahsBackup]; + DROP TABLE [#SurahsBackup]; + END + + ALTER TABLE [Ayahs] WITH CHECK ADD CONSTRAINT [FK_Ayahs_Surahs_SurahId] + FOREIGN KEY ([SurahId]) REFERENCES [Surahs] ([Id]) ON DELETE CASCADE; + ALTER TABLE [Episodes] WITH CHECK ADD CONSTRAINT [FK_Episodes_Surahs_SurahId] + FOREIGN KEY ([SurahId]) REFERENCES [Surahs] ([Id]) ON DELETE CASCADE; + """); } /// protected override void Down(MigrationBuilder migrationBuilder) { - migrationBuilder.AlterColumn( - name: "Id", - table: "Surahs", - type: "int", - nullable: false, - oldClrType: typeof(int), - oldType: "int") - .Annotation("SqlServer:Identity", "1, 1"); + migrationBuilder.Sql(""" + IF OBJECT_ID(N'[FK_Ayahs_Surahs_SurahId]', N'F') IS NOT NULL + ALTER TABLE [Ayahs] DROP CONSTRAINT [FK_Ayahs_Surahs_SurahId]; + IF OBJECT_ID(N'[FK_Episodes_Surahs_SurahId]', N'F') IS NOT NULL + ALTER TABLE [Episodes] DROP CONSTRAINT [FK_Episodes_Surahs_SurahId]; + + IF OBJECT_ID(N'[Surahs]', N'U') IS NOT NULL + BEGIN + SELECT * INTO [#SurahsBackup] FROM [Surahs]; + DROP TABLE [Surahs]; + END + + CREATE TABLE [Surahs] ( + [Id] int NOT NULL IDENTITY(1,1), + [Number] int NOT NULL, + [NameAr] nvarchar(80) NOT NULL, + [NameFa] nvarchar(80) NOT NULL, + CONSTRAINT [PK_Surahs] PRIMARY KEY ([Id]) + ); + + CREATE UNIQUE INDEX [IX_Surahs_Number] ON [Surahs] ([Number]); + + IF OBJECT_ID(N'tempdb..#SurahsBackup') IS NOT NULL + BEGIN + SET IDENTITY_INSERT [Surahs] ON; + INSERT INTO [Surahs] ([Id], [Number], [NameAr], [NameFa]) + SELECT [Id], [Number], [NameAr], [NameFa] FROM [#SurahsBackup]; + SET IDENTITY_INSERT [Surahs] OFF; + DROP TABLE [#SurahsBackup]; + END + + ALTER TABLE [Ayahs] WITH CHECK ADD CONSTRAINT [FK_Ayahs_Surahs_SurahId] + FOREIGN KEY ([SurahId]) REFERENCES [Surahs] ([Id]) ON DELETE CASCADE; + ALTER TABLE [Episodes] WITH CHECK ADD CONSTRAINT [FK_Episodes_Surahs_SurahId] + FOREIGN KEY ([SurahId]) REFERENCES [Surahs] ([Id]) ON DELETE CASCADE; + """); } } }