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;
+ """);
}
}
}