Fix SurahExplicitIds migration for SQL Server IDENTITY removal.
build-and-deploy / deploy (push) Successful in 2m39s
build-and-deploy / deploy (push) Successful in 2m39s
Recreate Surahs table without IDENTITY instead of unsupported ALTER COLUMN.
This commit is contained in:
+71
-16
@@ -10,27 +10,82 @@ namespace TafsirQuran.Infrastructure.Persistence.Migrations
|
|||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
protected override void Up(MigrationBuilder migrationBuilder)
|
protected override void Up(MigrationBuilder migrationBuilder)
|
||||||
{
|
{
|
||||||
migrationBuilder.AlterColumn<int>(
|
// SQL Server cannot ALTER IDENTITY off a column; recreate Surahs without identity.
|
||||||
name: "Id",
|
migrationBuilder.Sql("""
|
||||||
table: "Surahs",
|
IF OBJECT_ID(N'[FK_Ayahs_Surahs_SurahId]', N'F') IS NOT NULL
|
||||||
type: "int",
|
ALTER TABLE [Ayahs] DROP CONSTRAINT [FK_Ayahs_Surahs_SurahId];
|
||||||
nullable: false,
|
IF OBJECT_ID(N'[FK_Episodes_Surahs_SurahId]', N'F') IS NOT NULL
|
||||||
oldClrType: typeof(int),
|
ALTER TABLE [Episodes] DROP CONSTRAINT [FK_Episodes_Surahs_SurahId];
|
||||||
oldType: "int")
|
|
||||||
.OldAnnotation("SqlServer:Identity", "1, 1");
|
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;
|
||||||
|
""");
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
protected override void Down(MigrationBuilder migrationBuilder)
|
protected override void Down(MigrationBuilder migrationBuilder)
|
||||||
{
|
{
|
||||||
migrationBuilder.AlterColumn<int>(
|
migrationBuilder.Sql("""
|
||||||
name: "Id",
|
IF OBJECT_ID(N'[FK_Ayahs_Surahs_SurahId]', N'F') IS NOT NULL
|
||||||
table: "Surahs",
|
ALTER TABLE [Ayahs] DROP CONSTRAINT [FK_Ayahs_Surahs_SurahId];
|
||||||
type: "int",
|
IF OBJECT_ID(N'[FK_Episodes_Surahs_SurahId]', N'F') IS NOT NULL
|
||||||
nullable: false,
|
ALTER TABLE [Episodes] DROP CONSTRAINT [FK_Episodes_Surahs_SurahId];
|
||||||
oldClrType: typeof(int),
|
|
||||||
oldType: "int")
|
IF OBJECT_ID(N'[Surahs]', N'U') IS NOT NULL
|
||||||
.Annotation("SqlServer:Identity", "1, 1");
|
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;
|
||||||
|
""");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user