IF OBJECT_ID(N'[__EFMigrationsHistory]') IS NULL
BEGIN
    CREATE TABLE [__EFMigrationsHistory] (
        [MigrationId] nvarchar(150) NOT NULL,
        [ProductVersion] nvarchar(32) NOT NULL,
        CONSTRAINT [PK___EFMigrationsHistory] PRIMARY KEY ([MigrationId])
    );
END;
GO

BEGIN TRANSACTION;
GO

IF NOT EXISTS (
    SELECT * FROM [__EFMigrationsHistory]
    WHERE [MigrationId] = N'20230619091215_InitialMigration'
)
BEGIN
    CREATE TABLE [Operations] (
        [Id] uniqueidentifier NOT NULL,
        [AcceptedFormat] int NULL,
        [ResultDataType] int NULL,
        [ResultType] nvarchar(max) NULL,
        [TotalResultPages] int NOT NULL,
        [TotalResultCount] int NOT NULL,
        [ResultPageSize] int NOT NULL,
        [Name] nvarchar(max) NULL,
        [Created] datetimeoffset NOT NULL,
        [LastUpdate] datetimeoffset NOT NULL,
        [Timestamp] rowversion NULL,
        [Status] int NOT NULL,
        [StatusMessage] nvarchar(max) NULL,
        CONSTRAINT [PK_Operations] PRIMARY KEY ([Id])
    );
END;
GO

IF NOT EXISTS (
    SELECT * FROM [__EFMigrationsHistory]
    WHERE [MigrationId] = N'20230619091215_InitialMigration'
)
BEGIN
    CREATE TABLE [OperationResults] (
        [Id] uniqueidentifier NOT NULL,
        [OperationId] uniqueidentifier NOT NULL,
        [PageNo] int NOT NULL,
        [ResultData] nvarchar(max) NOT NULL,
        CONSTRAINT [PK_OperationResults] PRIMARY KEY ([Id]),
        CONSTRAINT [FK_OperationResults_Operations_OperationId] FOREIGN KEY ([OperationId]) REFERENCES [Operations] ([Id]) ON DELETE CASCADE
    );
END;
GO

IF NOT EXISTS (
    SELECT * FROM [__EFMigrationsHistory]
    WHERE [MigrationId] = N'20230619091215_InitialMigration'
)
BEGIN
    CREATE TABLE [OperationTasks] (
        [Id] uniqueidentifier NOT NULL,
        [OperationId] uniqueidentifier NOT NULL,
        [ParentTaskId] uniqueidentifier NOT NULL,
        [Name] nvarchar(max) NULL,
        [Created] datetimeoffset NOT NULL,
        [LastUpdate] datetimeoffset NOT NULL,
        [Timestamp] rowversion NULL,
        [Status] int NOT NULL,
        [StatusMessage] nvarchar(max) NULL,
        CONSTRAINT [PK_OperationTasks] PRIMARY KEY ([Id]),
        CONSTRAINT [FK_OperationTasks_Operations_OperationId] FOREIGN KEY ([OperationId]) REFERENCES [Operations] ([Id]) ON DELETE CASCADE
    );
END;
GO

IF NOT EXISTS (
    SELECT * FROM [__EFMigrationsHistory]
    WHERE [MigrationId] = N'20230619091215_InitialMigration'
)
BEGIN
    CREATE INDEX [IX_OperationResults_OperationId] ON [OperationResults] ([OperationId]);
END;
GO

IF NOT EXISTS (
    SELECT * FROM [__EFMigrationsHistory]
    WHERE [MigrationId] = N'20230619091215_InitialMigration'
)
BEGIN
    CREATE INDEX [IX_OperationTasks_OperationId] ON [OperationTasks] ([OperationId]);
END;
GO

IF NOT EXISTS (
    SELECT * FROM [__EFMigrationsHistory]
    WHERE [MigrationId] = N'20230619091215_InitialMigration'
)
BEGIN
    INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion])
    VALUES (N'20230619091215_InitialMigration', N'8.0.20');
END;
GO

COMMIT;
GO

BEGIN TRANSACTION;
GO

IF NOT EXISTS (
    SELECT * FROM [__EFMigrationsHistory]
    WHERE [MigrationId] = N'20251015143210_UpgradeDotnet'
)
BEGIN
    INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion])
    VALUES (N'20251015143210_UpgradeDotnet', N'8.0.20');
END;
GO

COMMIT;
GO

