using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace ZB.MOM.WW.OtOpcUa.Configuration.Migrations
{
    /// <inheritdoc />
    public partial class V3Initial : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "ConfigAuditLog",
                columns: table => new
                {
                    AuditId = table.Column<long>(type: "bigint", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    Timestamp = table.Column<DateTime>(type: "datetime2(3)", nullable: false, defaultValueSql: "SYSUTCDATETIME()"),
                    Principal = table.Column<string>(type: "nvarchar(128)", maxLength: 128, nullable: false),
                    EventType = table.Column<string>(type: "nvarchar(64)", maxLength: 64, nullable: false),
                    ClusterId = table.Column<string>(type: "nvarchar(64)", maxLength: 64, nullable: true),
                    NodeId = table.Column<string>(type: "nvarchar(64)", maxLength: 64, nullable: true),
                    GenerationId = table.Column<long>(type: "bigint", nullable: true),
                    DetailsJson = table.Column<string>(type: "nvarchar(max)", nullable: true),
                    EventId = table.Column<Guid>(type: "uniqueidentifier", nullable: true),
                    CorrelationId = table.Column<Guid>(type: "uniqueidentifier", nullable: true),
                    Outcome = table.Column<string>(type: "nvarchar(16)", maxLength: 16, nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_ConfigAuditLog", x => x.AuditId);
                    table.CheckConstraint("CK_ConfigAuditLog_DetailsJson_IsJson", "DetailsJson IS NULL OR ISJSON(DetailsJson) = 1");
                });

            migrationBuilder.CreateTable(
                name: "ConfigEdit",
                columns: table => new
                {
                    EditId = table.Column<Guid>(type: "uniqueidentifier", nullable: false, defaultValueSql: "NEWSEQUENTIALID()"),
                    EntityType = table.Column<string>(type: "nvarchar(64)", maxLength: 64, nullable: false),
                    EntityId = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
                    FieldsJson = table.Column<string>(type: "nvarchar(max)", nullable: false),
                    ExecutionId = table.Column<Guid>(type: "uniqueidentifier", nullable: true),
                    EditedBy = table.Column<string>(type: "nvarchar(128)", maxLength: 128, nullable: false),
                    EditedAtUtc = table.Column<DateTime>(type: "datetime2(3)", nullable: false, defaultValueSql: "SYSUTCDATETIME()"),
                    SourceNode = table.Column<string>(type: "nvarchar(64)", maxLength: 64, nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_ConfigEdit", x => x.EditId);
                    table.CheckConstraint("CK_ConfigEdit_FieldsJson_IsJson", "ISJSON(FieldsJson) = 1");
                });

            migrationBuilder.CreateTable(
                name: "DataProtectionKeys",
                columns: table => new
                {
                    Id = table.Column<int>(type: "int", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    FriendlyName = table.Column<string>(type: "nvarchar(max)", nullable: true),
                    Xml = table.Column<string>(type: "nvarchar(max)", nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_DataProtectionKeys", x => x.Id);
                });

            migrationBuilder.CreateTable(
                name: "Deployment",
                columns: table => new
                {
                    DeploymentId = table.Column<Guid>(type: "uniqueidentifier", nullable: false, defaultValueSql: "NEWSEQUENTIALID()"),
                    RevisionHash = table.Column<string>(type: "nvarchar(64)", maxLength: 64, nullable: false),
                    Status = table.Column<int>(type: "int", nullable: false),
                    CreatedBy = table.Column<string>(type: "nvarchar(128)", maxLength: 128, nullable: false),
                    CreatedAtUtc = table.Column<DateTime>(type: "datetime2(3)", nullable: false, defaultValueSql: "SYSUTCDATETIME()"),
                    ArtifactBlob = table.Column<byte[]>(type: "varbinary(max)", nullable: false),
                    RowVersion = table.Column<byte[]>(type: "rowversion", rowVersion: true, nullable: false),
                    FailureReason = table.Column<string>(type: "nvarchar(2048)", maxLength: 2048, nullable: true),
                    SealedAtUtc = table.Column<DateTime>(type: "datetime2(3)", nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_Deployment", x => x.DeploymentId);
                });

            migrationBuilder.CreateTable(
                name: "Device",
                columns: table => new
                {
                    DeviceRowId = table.Column<Guid>(type: "uniqueidentifier", nullable: false, defaultValueSql: "NEWSEQUENTIALID()"),
                    DeviceId = table.Column<string>(type: "nvarchar(64)", maxLength: 64, nullable: true),
                    DriverInstanceId = table.Column<string>(type: "nvarchar(64)", maxLength: 64, nullable: false),
                    Name = table.Column<string>(type: "nvarchar(128)", maxLength: 128, nullable: false),
                    Enabled = table.Column<bool>(type: "bit", nullable: false),
                    DeviceConfig = table.Column<string>(type: "nvarchar(max)", nullable: false),
                    RowVersion = table.Column<byte[]>(type: "rowversion", rowVersion: true, nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_Device", x => x.DeviceRowId);
                    table.CheckConstraint("CK_Device_DeviceConfig_IsJson", "ISJSON(DeviceConfig) = 1");
                });

            migrationBuilder.CreateTable(
                name: "DriverHostStatus",
                columns: table => new
                {
                    NodeId = table.Column<string>(type: "nvarchar(64)", maxLength: 64, nullable: false),
                    DriverInstanceId = table.Column<string>(type: "nvarchar(64)", maxLength: 64, nullable: false),
                    HostName = table.Column<string>(type: "nvarchar(256)", maxLength: 256, nullable: false),
                    State = table.Column<string>(type: "nvarchar(16)", maxLength: 16, nullable: false),
                    StateChangedUtc = table.Column<DateTime>(type: "datetime2(3)", nullable: false),
                    LastSeenUtc = table.Column<DateTime>(type: "datetime2(3)", nullable: false),
                    Detail = table.Column<string>(type: "nvarchar(1024)", maxLength: 1024, nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_DriverHostStatus", x => new { x.NodeId, x.DriverInstanceId, x.HostName });
                });

            migrationBuilder.CreateTable(
                name: "DriverInstanceResilienceStatus",
                columns: table => new
                {
                    DriverInstanceId = table.Column<string>(type: "nvarchar(64)", maxLength: 64, nullable: false),
                    HostName = table.Column<string>(type: "nvarchar(256)", maxLength: 256, nullable: false),
                    LastCircuitBreakerOpenUtc = table.Column<DateTime>(type: "datetime2(3)", nullable: true),
                    ConsecutiveFailures = table.Column<int>(type: "int", nullable: false),
                    CurrentBulkheadDepth = table.Column<int>(type: "int", nullable: false),
                    LastRecycleUtc = table.Column<DateTime>(type: "datetime2(3)", nullable: true),
                    BaselineFootprintBytes = table.Column<long>(type: "bigint", nullable: false),
                    CurrentFootprintBytes = table.Column<long>(type: "bigint", nullable: false),
                    LastSampledUtc = table.Column<DateTime>(type: "datetime2(3)", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_DriverInstanceResilienceStatus", x => new { x.DriverInstanceId, x.HostName });
                });

            migrationBuilder.CreateTable(
                name: "Equipment",
                columns: table => new
                {
                    EquipmentRowId = table.Column<Guid>(type: "uniqueidentifier", nullable: false, defaultValueSql: "NEWSEQUENTIALID()"),
                    EquipmentId = table.Column<string>(type: "nvarchar(64)", maxLength: 64, nullable: true),
                    EquipmentUuid = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
                    UnsLineId = table.Column<string>(type: "nvarchar(64)", maxLength: 64, nullable: false),
                    Name = table.Column<string>(type: "nvarchar(32)", maxLength: 32, nullable: false),
                    MachineCode = table.Column<string>(type: "nvarchar(64)", maxLength: 64, nullable: false),
                    ZTag = table.Column<string>(type: "nvarchar(64)", maxLength: 64, nullable: true),
                    SAPID = table.Column<string>(type: "nvarchar(64)", maxLength: 64, nullable: true),
                    Manufacturer = table.Column<string>(type: "nvarchar(64)", maxLength: 64, nullable: true),
                    Model = table.Column<string>(type: "nvarchar(64)", maxLength: 64, nullable: true),
                    SerialNumber = table.Column<string>(type: "nvarchar(64)", maxLength: 64, nullable: true),
                    HardwareRevision = table.Column<string>(type: "nvarchar(32)", maxLength: 32, nullable: true),
                    SoftwareRevision = table.Column<string>(type: "nvarchar(32)", maxLength: 32, nullable: true),
                    YearOfConstruction = table.Column<short>(type: "smallint", nullable: true),
                    AssetLocation = table.Column<string>(type: "nvarchar(256)", maxLength: 256, nullable: true),
                    ManufacturerUri = table.Column<string>(type: "nvarchar(512)", maxLength: 512, nullable: true),
                    DeviceManualUri = table.Column<string>(type: "nvarchar(512)", maxLength: 512, nullable: true),
                    EquipmentClassRef = table.Column<string>(type: "nvarchar(128)", maxLength: 128, nullable: true),
                    Enabled = table.Column<bool>(type: "bit", nullable: false),
                    RowVersion = table.Column<byte[]>(type: "rowversion", rowVersion: true, nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_Equipment", x => x.EquipmentRowId);
                });

            migrationBuilder.CreateTable(
                name: "ExternalIdReservation",
                columns: table => new
                {
                    ReservationId = table.Column<Guid>(type: "uniqueidentifier", nullable: false, defaultValueSql: "NEWSEQUENTIALID()"),
                    Kind = table.Column<string>(type: "nvarchar(16)", maxLength: 16, nullable: false),
                    Value = table.Column<string>(type: "nvarchar(64)", maxLength: 64, nullable: false),
                    EquipmentUuid = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
                    ClusterId = table.Column<string>(type: "nvarchar(64)", maxLength: 64, nullable: false),
                    FirstPublishedAt = table.Column<DateTime>(type: "datetime2(3)", nullable: false, defaultValueSql: "SYSUTCDATETIME()"),
                    FirstPublishedBy = table.Column<string>(type: "nvarchar(128)", maxLength: 128, nullable: false),
                    LastPublishedAt = table.Column<DateTime>(type: "datetime2(3)", nullable: false, defaultValueSql: "SYSUTCDATETIME()"),
                    ReleasedAt = table.Column<DateTime>(type: "datetime2(3)", nullable: true),
                    ReleasedBy = table.Column<string>(type: "nvarchar(128)", maxLength: 128, nullable: true),
                    ReleaseReason = table.Column<string>(type: "nvarchar(512)", maxLength: 512, nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_ExternalIdReservation", x => x.ReservationId);
                });

            migrationBuilder.CreateTable(
                name: "NodeAcl",
                columns: table => new
                {
                    NodeAclRowId = table.Column<Guid>(type: "uniqueidentifier", nullable: false, defaultValueSql: "NEWSEQUENTIALID()"),
                    NodeAclId = table.Column<string>(type: "nvarchar(64)", maxLength: 64, nullable: true),
                    ClusterId = table.Column<string>(type: "nvarchar(64)", maxLength: 64, nullable: false),
                    LdapGroup = table.Column<string>(type: "nvarchar(256)", maxLength: 256, nullable: false),
                    ScopeKind = table.Column<string>(type: "nvarchar(16)", maxLength: 16, nullable: false),
                    ScopeId = table.Column<string>(type: "nvarchar(64)", maxLength: 64, nullable: true),
                    PermissionFlags = table.Column<int>(type: "int", nullable: false),
                    Notes = table.Column<string>(type: "nvarchar(512)", maxLength: 512, nullable: true),
                    RowVersion = table.Column<byte[]>(type: "rowversion", rowVersion: true, nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_NodeAcl", x => x.NodeAclRowId);
                });

            migrationBuilder.CreateTable(
                name: "PollGroup",
                columns: table => new
                {
                    PollGroupRowId = table.Column<Guid>(type: "uniqueidentifier", nullable: false, defaultValueSql: "NEWSEQUENTIALID()"),
                    PollGroupId = table.Column<string>(type: "nvarchar(64)", maxLength: 64, nullable: true),
                    DriverInstanceId = table.Column<string>(type: "nvarchar(64)", maxLength: 64, nullable: false),
                    Name = table.Column<string>(type: "nvarchar(128)", maxLength: 128, nullable: false),
                    IntervalMs = table.Column<int>(type: "int", nullable: false),
                    RowVersion = table.Column<byte[]>(type: "rowversion", rowVersion: true, nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_PollGroup", x => x.PollGroupRowId);
                    table.CheckConstraint("CK_PollGroup_IntervalMs_Min", "IntervalMs >= 50");
                });

            migrationBuilder.CreateTable(
                name: "Script",
                columns: table => new
                {
                    ScriptRowId = table.Column<Guid>(type: "uniqueidentifier", nullable: false, defaultValueSql: "NEWSEQUENTIALID()"),
                    ScriptId = table.Column<string>(type: "nvarchar(64)", maxLength: 64, nullable: true),
                    Name = table.Column<string>(type: "nvarchar(128)", maxLength: 128, nullable: false),
                    SourceCode = table.Column<string>(type: "nvarchar(max)", nullable: false),
                    SourceHash = table.Column<string>(type: "nvarchar(64)", maxLength: 64, nullable: false),
                    Language = table.Column<string>(type: "nvarchar(16)", maxLength: 16, nullable: false),
                    RowVersion = table.Column<byte[]>(type: "rowversion", rowVersion: true, nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_Script", x => x.ScriptRowId);
                });

            migrationBuilder.CreateTable(
                name: "ScriptedAlarm",
                columns: table => new
                {
                    ScriptedAlarmRowId = table.Column<Guid>(type: "uniqueidentifier", nullable: false, defaultValueSql: "NEWSEQUENTIALID()"),
                    ScriptedAlarmId = table.Column<string>(type: "nvarchar(64)", maxLength: 64, nullable: true),
                    EquipmentId = table.Column<string>(type: "nvarchar(64)", maxLength: 64, nullable: false),
                    Name = table.Column<string>(type: "nvarchar(128)", maxLength: 128, nullable: false),
                    AlarmType = table.Column<string>(type: "nvarchar(32)", maxLength: 32, nullable: false),
                    Severity = table.Column<int>(type: "int", nullable: false),
                    MessageTemplate = table.Column<string>(type: "nvarchar(1024)", maxLength: 1024, nullable: false),
                    PredicateScriptId = table.Column<string>(type: "nvarchar(64)", maxLength: 64, nullable: false),
                    HistorizeToAveva = table.Column<bool>(type: "bit", nullable: false),
                    Retain = table.Column<bool>(type: "bit", nullable: false),
                    Enabled = table.Column<bool>(type: "bit", nullable: false),
                    RowVersion = table.Column<byte[]>(type: "rowversion", rowVersion: true, nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_ScriptedAlarm", x => x.ScriptedAlarmRowId);
                    table.CheckConstraint("CK_ScriptedAlarm_AlarmType", "AlarmType IN ('AlarmCondition','LimitAlarm','OffNormalAlarm','DiscreteAlarm')");
                    table.CheckConstraint("CK_ScriptedAlarm_Severity_Range", "Severity BETWEEN 1 AND 1000");
                });

            migrationBuilder.CreateTable(
                name: "ScriptedAlarmState",
                columns: table => new
                {
                    ScriptedAlarmId = table.Column<string>(type: "nvarchar(64)", maxLength: 64, nullable: false),
                    EnabledState = table.Column<string>(type: "nvarchar(16)", maxLength: 16, nullable: false),
                    AckedState = table.Column<string>(type: "nvarchar(16)", maxLength: 16, nullable: false),
                    ConfirmedState = table.Column<string>(type: "nvarchar(16)", maxLength: 16, nullable: false),
                    ShelvingState = table.Column<string>(type: "nvarchar(16)", maxLength: 16, nullable: false),
                    ShelvingExpiresUtc = table.Column<DateTime>(type: "datetime2(3)", nullable: true),
                    LastAckUser = table.Column<string>(type: "nvarchar(128)", maxLength: 128, nullable: true),
                    LastAckComment = table.Column<string>(type: "nvarchar(1024)", maxLength: 1024, nullable: true),
                    LastAckUtc = table.Column<DateTime>(type: "datetime2(3)", nullable: true),
                    LastConfirmUser = table.Column<string>(type: "nvarchar(128)", maxLength: 128, nullable: true),
                    LastConfirmComment = table.Column<string>(type: "nvarchar(1024)", maxLength: 1024, nullable: true),
                    LastConfirmUtc = table.Column<DateTime>(type: "datetime2(3)", nullable: true),
                    CommentsJson = table.Column<string>(type: "nvarchar(max)", nullable: false),
                    UpdatedAtUtc = table.Column<DateTime>(type: "datetime2(3)", nullable: false, defaultValueSql: "SYSUTCDATETIME()")
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_ScriptedAlarmState", x => x.ScriptedAlarmId);
                    table.CheckConstraint("CK_ScriptedAlarmState_CommentsJson_IsJson", "ISJSON(CommentsJson) = 1");
                });

            migrationBuilder.CreateTable(
                name: "ServerCluster",
                columns: table => new
                {
                    ClusterId = table.Column<string>(type: "nvarchar(64)", maxLength: 64, nullable: false),
                    Name = table.Column<string>(type: "nvarchar(128)", maxLength: 128, nullable: false),
                    Enterprise = table.Column<string>(type: "nvarchar(32)", maxLength: 32, nullable: false),
                    Site = table.Column<string>(type: "nvarchar(32)", maxLength: 32, nullable: false),
                    NodeCount = table.Column<byte>(type: "tinyint", nullable: false),
                    RedundancyMode = table.Column<string>(type: "nvarchar(16)", maxLength: 16, nullable: false),
                    Enabled = table.Column<bool>(type: "bit", nullable: false),
                    Notes = table.Column<string>(type: "nvarchar(1024)", maxLength: 1024, nullable: true),
                    CreatedAt = table.Column<DateTime>(type: "datetime2(3)", nullable: false, defaultValueSql: "SYSUTCDATETIME()"),
                    CreatedBy = table.Column<string>(type: "nvarchar(128)", maxLength: 128, nullable: false),
                    ModifiedAt = table.Column<DateTime>(type: "datetime2(3)", nullable: true),
                    ModifiedBy = table.Column<string>(type: "nvarchar(128)", maxLength: 128, nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_ServerCluster", x => x.ClusterId);
                    table.CheckConstraint("CK_ServerCluster_RedundancyMode_NodeCount", "((NodeCount = 1 AND RedundancyMode = 'None') OR (NodeCount = 2 AND RedundancyMode IN ('Warm', 'Hot')))");
                });

            migrationBuilder.CreateTable(
                name: "Tag",
                columns: table => new
                {
                    TagRowId = table.Column<Guid>(type: "uniqueidentifier", nullable: false, defaultValueSql: "NEWSEQUENTIALID()"),
                    TagId = table.Column<string>(type: "nvarchar(64)", maxLength: 64, nullable: true),
                    DeviceId = table.Column<string>(type: "nvarchar(64)", maxLength: 64, nullable: false),
                    TagGroupId = table.Column<string>(type: "nvarchar(64)", maxLength: 64, nullable: true),
                    Name = table.Column<string>(type: "nvarchar(128)", maxLength: 128, nullable: false),
                    DataType = table.Column<string>(type: "nvarchar(32)", maxLength: 32, nullable: false),
                    AccessLevel = table.Column<string>(type: "nvarchar(16)", maxLength: 16, nullable: false),
                    WriteIdempotent = table.Column<bool>(type: "bit", nullable: false),
                    PollGroupId = table.Column<string>(type: "nvarchar(64)", maxLength: 64, nullable: true),
                    TagConfig = table.Column<string>(type: "nvarchar(max)", nullable: false),
                    RowVersion = table.Column<byte[]>(type: "rowversion", rowVersion: true, nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_Tag", x => x.TagRowId);
                    table.CheckConstraint("CK_Tag_TagConfig_IsJson", "ISJSON(TagConfig) = 1");
                });

            migrationBuilder.CreateTable(
                name: "TagGroup",
                columns: table => new
                {
                    TagGroupRowId = table.Column<Guid>(type: "uniqueidentifier", nullable: false, defaultValueSql: "NEWSEQUENTIALID()"),
                    TagGroupId = table.Column<string>(type: "nvarchar(64)", maxLength: 64, nullable: true),
                    DeviceId = table.Column<string>(type: "nvarchar(64)", maxLength: 64, nullable: false),
                    ParentTagGroupId = table.Column<string>(type: "nvarchar(64)", maxLength: 64, nullable: true),
                    Name = table.Column<string>(type: "nvarchar(128)", maxLength: 128, nullable: false),
                    SortOrder = table.Column<int>(type: "int", nullable: false),
                    RowVersion = table.Column<byte[]>(type: "rowversion", rowVersion: true, nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_TagGroup", x => x.TagGroupRowId);
                });

            migrationBuilder.CreateTable(
                name: "UnsLine",
                columns: table => new
                {
                    UnsLineRowId = table.Column<Guid>(type: "uniqueidentifier", nullable: false, defaultValueSql: "NEWSEQUENTIALID()"),
                    UnsLineId = table.Column<string>(type: "nvarchar(64)", maxLength: 64, nullable: true),
                    UnsAreaId = table.Column<string>(type: "nvarchar(64)", maxLength: 64, nullable: false),
                    Name = table.Column<string>(type: "nvarchar(32)", maxLength: 32, nullable: false),
                    Notes = table.Column<string>(type: "nvarchar(512)", maxLength: 512, nullable: true),
                    RowVersion = table.Column<byte[]>(type: "rowversion", rowVersion: true, nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_UnsLine", x => x.UnsLineRowId);
                });

            migrationBuilder.CreateTable(
                name: "UnsTagReference",
                columns: table => new
                {
                    UnsTagReferenceRowId = table.Column<Guid>(type: "uniqueidentifier", nullable: false, defaultValueSql: "NEWSEQUENTIALID()"),
                    UnsTagReferenceId = table.Column<string>(type: "nvarchar(64)", maxLength: 64, nullable: true),
                    EquipmentId = table.Column<string>(type: "nvarchar(64)", maxLength: 64, nullable: false),
                    TagId = table.Column<string>(type: "nvarchar(64)", maxLength: 64, nullable: false),
                    DisplayNameOverride = table.Column<string>(type: "nvarchar(128)", maxLength: 128, nullable: true),
                    SortOrder = table.Column<int>(type: "int", nullable: false),
                    RowVersion = table.Column<byte[]>(type: "rowversion", rowVersion: true, nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_UnsTagReference", x => x.UnsTagReferenceRowId);
                });

            migrationBuilder.CreateTable(
                name: "VirtualTag",
                columns: table => new
                {
                    VirtualTagRowId = table.Column<Guid>(type: "uniqueidentifier", nullable: false, defaultValueSql: "NEWSEQUENTIALID()"),
                    VirtualTagId = table.Column<string>(type: "nvarchar(64)", maxLength: 64, nullable: true),
                    EquipmentId = table.Column<string>(type: "nvarchar(64)", maxLength: 64, nullable: false),
                    Name = table.Column<string>(type: "nvarchar(128)", maxLength: 128, nullable: false),
                    DataType = table.Column<string>(type: "nvarchar(32)", maxLength: 32, nullable: false),
                    ScriptId = table.Column<string>(type: "nvarchar(64)", maxLength: 64, nullable: false),
                    ChangeTriggered = table.Column<bool>(type: "bit", nullable: false),
                    TimerIntervalMs = table.Column<int>(type: "int", nullable: true),
                    Historize = table.Column<bool>(type: "bit", nullable: false),
                    Enabled = table.Column<bool>(type: "bit", nullable: false),
                    RowVersion = table.Column<byte[]>(type: "rowversion", rowVersion: true, nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_VirtualTag", x => x.VirtualTagRowId);
                    table.CheckConstraint("CK_VirtualTag_TimerInterval_Min", "TimerIntervalMs IS NULL OR TimerIntervalMs >= 50");
                    table.CheckConstraint("CK_VirtualTag_Trigger_AtLeastOne", "ChangeTriggered = 1 OR TimerIntervalMs IS NOT NULL");
                });

            migrationBuilder.CreateTable(
                name: "ClusterNode",
                columns: table => new
                {
                    NodeId = table.Column<string>(type: "nvarchar(64)", maxLength: 64, nullable: false),
                    ClusterId = table.Column<string>(type: "nvarchar(64)", maxLength: 64, nullable: false),
                    Host = table.Column<string>(type: "nvarchar(255)", maxLength: 255, nullable: false),
                    OpcUaPort = table.Column<int>(type: "int", nullable: false),
                    DashboardPort = table.Column<int>(type: "int", nullable: false),
                    ApplicationUri = table.Column<string>(type: "nvarchar(256)", maxLength: 256, nullable: false),
                    ServiceLevelBase = table.Column<byte>(type: "tinyint", nullable: false),
                    DriverConfigOverridesJson = table.Column<string>(type: "nvarchar(max)", nullable: true),
                    Enabled = table.Column<bool>(type: "bit", nullable: false),
                    LastSeenAt = table.Column<DateTime>(type: "datetime2(3)", nullable: true),
                    CreatedAt = table.Column<DateTime>(type: "datetime2(3)", nullable: false, defaultValueSql: "SYSUTCDATETIME()"),
                    CreatedBy = table.Column<string>(type: "nvarchar(128)", maxLength: 128, nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_ClusterNode", x => x.NodeId);
                    table.ForeignKey(
                        name: "FK_ClusterNode_ServerCluster_ClusterId",
                        column: x => x.ClusterId,
                        principalTable: "ServerCluster",
                        principalColumn: "ClusterId",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.CreateTable(
                name: "DriverInstance",
                columns: table => new
                {
                    DriverInstanceRowId = table.Column<Guid>(type: "uniqueidentifier", nullable: false, defaultValueSql: "NEWSEQUENTIALID()"),
                    DriverInstanceId = table.Column<string>(type: "nvarchar(64)", maxLength: 64, nullable: true),
                    ClusterId = table.Column<string>(type: "nvarchar(64)", maxLength: 64, nullable: false),
                    RawFolderId = table.Column<string>(type: "nvarchar(64)", maxLength: 64, nullable: true),
                    Name = table.Column<string>(type: "nvarchar(128)", maxLength: 128, nullable: false),
                    DriverType = table.Column<string>(type: "nvarchar(32)", maxLength: 32, nullable: false),
                    Enabled = table.Column<bool>(type: "bit", nullable: false),
                    DriverConfig = table.Column<string>(type: "nvarchar(max)", nullable: false),
                    ResilienceConfig = table.Column<string>(type: "nvarchar(max)", nullable: true),
                    RowVersion = table.Column<byte[]>(type: "rowversion", rowVersion: true, nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_DriverInstance", x => x.DriverInstanceRowId);
                    table.CheckConstraint("CK_DriverInstance_DriverConfig_IsJson", "ISJSON(DriverConfig) = 1");
                    table.CheckConstraint("CK_DriverInstance_ResilienceConfig_IsJson", "ResilienceConfig IS NULL OR ISJSON(ResilienceConfig) = 1");
                    table.ForeignKey(
                        name: "FK_DriverInstance_ServerCluster_ClusterId",
                        column: x => x.ClusterId,
                        principalTable: "ServerCluster",
                        principalColumn: "ClusterId",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.CreateTable(
                name: "LdapGroupRoleMapping",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
                    LdapGroup = table.Column<string>(type: "nvarchar(512)", maxLength: 512, nullable: false),
                    Role = table.Column<string>(type: "nvarchar(32)", maxLength: 32, nullable: false),
                    ClusterId = table.Column<string>(type: "nvarchar(64)", maxLength: 64, nullable: true),
                    IsSystemWide = table.Column<bool>(type: "bit", nullable: false),
                    CreatedAtUtc = table.Column<DateTime>(type: "datetime2(3)", nullable: false),
                    Notes = table.Column<string>(type: "nvarchar(512)", maxLength: 512, nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_LdapGroupRoleMapping", x => x.Id);
                    table.ForeignKey(
                        name: "FK_LdapGroupRoleMapping_ServerCluster_ClusterId",
                        column: x => x.ClusterId,
                        principalTable: "ServerCluster",
                        principalColumn: "ClusterId",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "RawFolder",
                columns: table => new
                {
                    RawFolderRowId = table.Column<Guid>(type: "uniqueidentifier", nullable: false, defaultValueSql: "NEWSEQUENTIALID()"),
                    RawFolderId = table.Column<string>(type: "nvarchar(64)", maxLength: 64, nullable: true),
                    ClusterId = table.Column<string>(type: "nvarchar(64)", maxLength: 64, nullable: false),
                    ParentRawFolderId = table.Column<string>(type: "nvarchar(64)", maxLength: 64, nullable: true),
                    Name = table.Column<string>(type: "nvarchar(128)", maxLength: 128, nullable: false),
                    SortOrder = table.Column<int>(type: "int", nullable: false),
                    RowVersion = table.Column<byte[]>(type: "rowversion", rowVersion: true, nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_RawFolder", x => x.RawFolderRowId);
                    table.ForeignKey(
                        name: "FK_RawFolder_ServerCluster_ClusterId",
                        column: x => x.ClusterId,
                        principalTable: "ServerCluster",
                        principalColumn: "ClusterId",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.CreateTable(
                name: "UnsArea",
                columns: table => new
                {
                    UnsAreaRowId = table.Column<Guid>(type: "uniqueidentifier", nullable: false, defaultValueSql: "NEWSEQUENTIALID()"),
                    UnsAreaId = table.Column<string>(type: "nvarchar(64)", maxLength: 64, nullable: true),
                    ClusterId = table.Column<string>(type: "nvarchar(64)", maxLength: 64, nullable: false),
                    Name = table.Column<string>(type: "nvarchar(32)", maxLength: 32, nullable: false),
                    Notes = table.Column<string>(type: "nvarchar(512)", maxLength: 512, nullable: true),
                    RowVersion = table.Column<byte[]>(type: "rowversion", rowVersion: true, nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_UnsArea", x => x.UnsAreaRowId);
                    table.ForeignKey(
                        name: "FK_UnsArea_ServerCluster_ClusterId",
                        column: x => x.ClusterId,
                        principalTable: "ServerCluster",
                        principalColumn: "ClusterId",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.CreateTable(
                name: "ClusterNodeCredential",
                columns: table => new
                {
                    CredentialId = table.Column<Guid>(type: "uniqueidentifier", nullable: false, defaultValueSql: "NEWSEQUENTIALID()"),
                    NodeId = table.Column<string>(type: "nvarchar(64)", maxLength: 64, nullable: false),
                    Kind = table.Column<string>(type: "nvarchar(32)", maxLength: 32, nullable: false),
                    Value = table.Column<string>(type: "nvarchar(512)", maxLength: 512, nullable: false),
                    Enabled = table.Column<bool>(type: "bit", nullable: false),
                    RotatedAt = table.Column<DateTime>(type: "datetime2(3)", nullable: true),
                    CreatedAt = table.Column<DateTime>(type: "datetime2(3)", nullable: false, defaultValueSql: "SYSUTCDATETIME()"),
                    CreatedBy = table.Column<string>(type: "nvarchar(128)", maxLength: 128, nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_ClusterNodeCredential", x => x.CredentialId);
                    table.ForeignKey(
                        name: "FK_ClusterNodeCredential_ClusterNode_NodeId",
                        column: x => x.NodeId,
                        principalTable: "ClusterNode",
                        principalColumn: "NodeId",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.CreateTable(
                name: "NodeDeploymentState",
                columns: table => new
                {
                    NodeId = table.Column<string>(type: "nvarchar(64)", maxLength: 64, nullable: false),
                    DeploymentId = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
                    Status = table.Column<int>(type: "int", nullable: false),
                    StartedAtUtc = table.Column<DateTime>(type: "datetime2(3)", nullable: false, defaultValueSql: "SYSUTCDATETIME()"),
                    AppliedAtUtc = table.Column<DateTime>(type: "datetime2(3)", nullable: true),
                    FailureReason = table.Column<string>(type: "nvarchar(2048)", maxLength: 2048, nullable: true),
                    RowVersion = table.Column<byte[]>(type: "rowversion", rowVersion: true, nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_NodeDeploymentState", x => new { x.NodeId, x.DeploymentId });
                    table.ForeignKey(
                        name: "FK_NodeDeploymentState_ClusterNode_NodeId",
                        column: x => x.NodeId,
                        principalTable: "ClusterNode",
                        principalColumn: "NodeId",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_NodeDeploymentState_Deployment_DeploymentId",
                        column: x => x.DeploymentId,
                        principalTable: "Deployment",
                        principalColumn: "DeploymentId",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateIndex(
                name: "IX_ClusterNode_ClusterId",
                table: "ClusterNode",
                column: "ClusterId");

            migrationBuilder.CreateIndex(
                name: "UX_ClusterNode_ApplicationUri",
                table: "ClusterNode",
                column: "ApplicationUri",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_ClusterNodeCredential_NodeId",
                table: "ClusterNodeCredential",
                columns: new[] { "NodeId", "Enabled" });

            migrationBuilder.CreateIndex(
                name: "UX_ClusterNodeCredential_Value",
                table: "ClusterNodeCredential",
                columns: new[] { "Kind", "Value" },
                unique: true,
                filter: "[Enabled] = 1");

            migrationBuilder.CreateIndex(
                name: "IX_ConfigAuditLog_Cluster_Time",
                table: "ConfigAuditLog",
                columns: new[] { "ClusterId", "Timestamp" },
                descending: new[] { false, true });

            migrationBuilder.CreateIndex(
                name: "IX_ConfigAuditLog_Generation",
                table: "ConfigAuditLog",
                column: "GenerationId",
                filter: "[GenerationId] IS NOT NULL");

            migrationBuilder.CreateIndex(
                name: "UX_ConfigAuditLog_EventId",
                table: "ConfigAuditLog",
                column: "EventId",
                unique: true,
                filter: "[EventId] IS NOT NULL");

            migrationBuilder.CreateIndex(
                name: "IX_ConfigEdit_EditedAt",
                table: "ConfigEdit",
                column: "EditedAtUtc");

            migrationBuilder.CreateIndex(
                name: "IX_ConfigEdit_Entity",
                table: "ConfigEdit",
                columns: new[] { "EntityType", "EntityId" });

            migrationBuilder.CreateIndex(
                name: "IX_ConfigEdit_Execution",
                table: "ConfigEdit",
                column: "ExecutionId",
                filter: "[ExecutionId] IS NOT NULL");

            migrationBuilder.CreateIndex(
                name: "IX_Deployment_CreatedAt",
                table: "Deployment",
                column: "CreatedAtUtc");

            migrationBuilder.CreateIndex(
                name: "IX_Deployment_Status",
                table: "Deployment",
                column: "Status");

            migrationBuilder.CreateIndex(
                name: "IX_Device_Driver",
                table: "Device",
                column: "DriverInstanceId");

            migrationBuilder.CreateIndex(
                name: "UX_Device_Driver_Name",
                table: "Device",
                columns: new[] { "DriverInstanceId", "Name" },
                unique: true);

            migrationBuilder.CreateIndex(
                name: "UX_Device_LogicalId",
                table: "Device",
                column: "DeviceId",
                unique: true,
                filter: "[DeviceId] IS NOT NULL");

            migrationBuilder.CreateIndex(
                name: "IX_DriverHostStatus_LastSeen",
                table: "DriverHostStatus",
                column: "LastSeenUtc");

            migrationBuilder.CreateIndex(
                name: "IX_DriverHostStatus_Node",
                table: "DriverHostStatus",
                column: "NodeId");

            migrationBuilder.CreateIndex(
                name: "IX_DriverInstance_Cluster",
                table: "DriverInstance",
                column: "ClusterId");

            migrationBuilder.CreateIndex(
                name: "IX_DriverInstance_RawFolder",
                table: "DriverInstance",
                column: "RawFolderId");

            migrationBuilder.CreateIndex(
                name: "UX_DriverInstance_ClusterRoot_Name",
                table: "DriverInstance",
                columns: new[] { "ClusterId", "Name" },
                unique: true,
                filter: "[RawFolderId] IS NULL");

            migrationBuilder.CreateIndex(
                name: "UX_DriverInstance_Folder_Name",
                table: "DriverInstance",
                columns: new[] { "ClusterId", "RawFolderId", "Name" },
                unique: true,
                filter: "[RawFolderId] IS NOT NULL");

            migrationBuilder.CreateIndex(
                name: "UX_DriverInstance_LogicalId",
                table: "DriverInstance",
                column: "DriverInstanceId",
                unique: true,
                filter: "[DriverInstanceId] IS NOT NULL");

            migrationBuilder.CreateIndex(
                name: "IX_DriverResilience_LastSampled",
                table: "DriverInstanceResilienceStatus",
                column: "LastSampledUtc");

            migrationBuilder.CreateIndex(
                name: "IX_Equipment_Line",
                table: "Equipment",
                column: "UnsLineId");

            migrationBuilder.CreateIndex(
                name: "IX_Equipment_MachineCode",
                table: "Equipment",
                column: "MachineCode");

            migrationBuilder.CreateIndex(
                name: "IX_Equipment_SAPID",
                table: "Equipment",
                column: "SAPID",
                filter: "[SAPID] IS NOT NULL");

            migrationBuilder.CreateIndex(
                name: "IX_Equipment_ZTag",
                table: "Equipment",
                column: "ZTag",
                filter: "[ZTag] IS NOT NULL");

            migrationBuilder.CreateIndex(
                name: "UX_Equipment_LinePath",
                table: "Equipment",
                columns: new[] { "UnsLineId", "Name" },
                unique: true);

            migrationBuilder.CreateIndex(
                name: "UX_Equipment_LogicalId",
                table: "Equipment",
                column: "EquipmentId",
                unique: true,
                filter: "[EquipmentId] IS NOT NULL");

            migrationBuilder.CreateIndex(
                name: "UX_Equipment_Uuid",
                table: "Equipment",
                column: "EquipmentUuid",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_ExternalIdReservation_Equipment",
                table: "ExternalIdReservation",
                column: "EquipmentUuid");

            migrationBuilder.CreateIndex(
                name: "UX_ExternalIdReservation_KindValue_Active",
                table: "ExternalIdReservation",
                columns: new[] { "Kind", "Value" },
                unique: true,
                filter: "[ReleasedAt] IS NULL");

            migrationBuilder.CreateIndex(
                name: "IX_LdapGroupRoleMapping_ClusterId",
                table: "LdapGroupRoleMapping",
                column: "ClusterId");

            migrationBuilder.CreateIndex(
                name: "IX_LdapGroupRoleMapping_Group",
                table: "LdapGroupRoleMapping",
                column: "LdapGroup");

            migrationBuilder.CreateIndex(
                name: "UX_LdapGroupRoleMapping_Group_Cluster",
                table: "LdapGroupRoleMapping",
                columns: new[] { "LdapGroup", "ClusterId" },
                unique: true,
                filter: "[ClusterId] IS NOT NULL");

            migrationBuilder.CreateIndex(
                name: "IX_NodeAcl_Cluster",
                table: "NodeAcl",
                column: "ClusterId");

            migrationBuilder.CreateIndex(
                name: "IX_NodeAcl_Group",
                table: "NodeAcl",
                column: "LdapGroup");

            migrationBuilder.CreateIndex(
                name: "IX_NodeAcl_Scope",
                table: "NodeAcl",
                columns: new[] { "ScopeKind", "ScopeId" },
                filter: "[ScopeId] IS NOT NULL");

            migrationBuilder.CreateIndex(
                name: "UX_NodeAcl_GroupScope",
                table: "NodeAcl",
                columns: new[] { "ClusterId", "LdapGroup", "ScopeKind", "ScopeId" },
                unique: true,
                filter: "[ScopeId] IS NOT NULL");

            migrationBuilder.CreateIndex(
                name: "UX_NodeAcl_LogicalId",
                table: "NodeAcl",
                column: "NodeAclId",
                unique: true,
                filter: "[NodeAclId] IS NOT NULL");

            migrationBuilder.CreateIndex(
                name: "IX_NodeDeploymentState_Deployment",
                table: "NodeDeploymentState",
                column: "DeploymentId");

            migrationBuilder.CreateIndex(
                name: "IX_NodeDeploymentState_Status",
                table: "NodeDeploymentState",
                column: "Status");

            migrationBuilder.CreateIndex(
                name: "IX_PollGroup_Driver",
                table: "PollGroup",
                column: "DriverInstanceId");

            migrationBuilder.CreateIndex(
                name: "UX_PollGroup_LogicalId",
                table: "PollGroup",
                column: "PollGroupId",
                unique: true,
                filter: "[PollGroupId] IS NOT NULL");

            migrationBuilder.CreateIndex(
                name: "IX_RawFolder_Cluster",
                table: "RawFolder",
                column: "ClusterId");

            migrationBuilder.CreateIndex(
                name: "UX_RawFolder_ClusterRoot_Name",
                table: "RawFolder",
                columns: new[] { "ClusterId", "Name" },
                unique: true,
                filter: "[ParentRawFolderId] IS NULL");

            migrationBuilder.CreateIndex(
                name: "UX_RawFolder_LogicalId",
                table: "RawFolder",
                column: "RawFolderId",
                unique: true,
                filter: "[RawFolderId] IS NOT NULL");

            migrationBuilder.CreateIndex(
                name: "UX_RawFolder_Parent_Name",
                table: "RawFolder",
                columns: new[] { "ClusterId", "ParentRawFolderId", "Name" },
                unique: true,
                filter: "[ParentRawFolderId] IS NOT NULL");

            migrationBuilder.CreateIndex(
                name: "IX_Script_SourceHash",
                table: "Script",
                column: "SourceHash");

            migrationBuilder.CreateIndex(
                name: "UX_Script_LogicalId",
                table: "Script",
                column: "ScriptId",
                unique: true,
                filter: "[ScriptId] IS NOT NULL");

            migrationBuilder.CreateIndex(
                name: "IX_ScriptedAlarm_Script",
                table: "ScriptedAlarm",
                column: "PredicateScriptId");

            migrationBuilder.CreateIndex(
                name: "UX_ScriptedAlarm_EquipmentPath",
                table: "ScriptedAlarm",
                columns: new[] { "EquipmentId", "Name" },
                unique: true);

            migrationBuilder.CreateIndex(
                name: "UX_ScriptedAlarm_LogicalId",
                table: "ScriptedAlarm",
                column: "ScriptedAlarmId",
                unique: true,
                filter: "[ScriptedAlarmId] IS NOT NULL");

            migrationBuilder.CreateIndex(
                name: "IX_ServerCluster_Site",
                table: "ServerCluster",
                column: "Site");

            migrationBuilder.CreateIndex(
                name: "UX_ServerCluster_Name",
                table: "ServerCluster",
                column: "Name",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_Tag_Device",
                table: "Tag",
                columns: new[] { "DeviceId", "TagGroupId" });

            migrationBuilder.CreateIndex(
                name: "UX_Tag_Device_Name",
                table: "Tag",
                columns: new[] { "DeviceId", "Name" },
                unique: true,
                filter: "[TagGroupId] IS NULL");

            migrationBuilder.CreateIndex(
                name: "UX_Tag_Group_Name",
                table: "Tag",
                columns: new[] { "DeviceId", "TagGroupId", "Name" },
                unique: true,
                filter: "[TagGroupId] IS NOT NULL");

            migrationBuilder.CreateIndex(
                name: "UX_Tag_LogicalId",
                table: "Tag",
                column: "TagId",
                unique: true,
                filter: "[TagId] IS NOT NULL");

            migrationBuilder.CreateIndex(
                name: "IX_TagGroup_Device",
                table: "TagGroup",
                column: "DeviceId");

            migrationBuilder.CreateIndex(
                name: "UX_TagGroup_DeviceRoot_Name",
                table: "TagGroup",
                columns: new[] { "DeviceId", "Name" },
                unique: true,
                filter: "[ParentTagGroupId] IS NULL");

            migrationBuilder.CreateIndex(
                name: "UX_TagGroup_LogicalId",
                table: "TagGroup",
                column: "TagGroupId",
                unique: true,
                filter: "[TagGroupId] IS NOT NULL");

            migrationBuilder.CreateIndex(
                name: "UX_TagGroup_Parent_Name",
                table: "TagGroup",
                columns: new[] { "DeviceId", "ParentTagGroupId", "Name" },
                unique: true,
                filter: "[ParentTagGroupId] IS NOT NULL");

            migrationBuilder.CreateIndex(
                name: "IX_UnsArea_Cluster",
                table: "UnsArea",
                column: "ClusterId");

            migrationBuilder.CreateIndex(
                name: "UX_UnsArea_ClusterName",
                table: "UnsArea",
                columns: new[] { "ClusterId", "Name" },
                unique: true);

            migrationBuilder.CreateIndex(
                name: "UX_UnsArea_LogicalId",
                table: "UnsArea",
                column: "UnsAreaId",
                unique: true,
                filter: "[UnsAreaId] IS NOT NULL");

            migrationBuilder.CreateIndex(
                name: "IX_UnsLine_Area",
                table: "UnsLine",
                column: "UnsAreaId");

            migrationBuilder.CreateIndex(
                name: "UX_UnsLine_AreaName",
                table: "UnsLine",
                columns: new[] { "UnsAreaId", "Name" },
                unique: true);

            migrationBuilder.CreateIndex(
                name: "UX_UnsLine_LogicalId",
                table: "UnsLine",
                column: "UnsLineId",
                unique: true,
                filter: "[UnsLineId] IS NOT NULL");

            migrationBuilder.CreateIndex(
                name: "IX_UnsTagReference_Equipment",
                table: "UnsTagReference",
                column: "EquipmentId");

            migrationBuilder.CreateIndex(
                name: "IX_UnsTagReference_Tag",
                table: "UnsTagReference",
                column: "TagId");

            migrationBuilder.CreateIndex(
                name: "UX_UnsTagReference_Equip_Tag",
                table: "UnsTagReference",
                columns: new[] { "EquipmentId", "TagId" },
                unique: true);

            migrationBuilder.CreateIndex(
                name: "UX_UnsTagReference_LogicalId",
                table: "UnsTagReference",
                column: "UnsTagReferenceId",
                unique: true,
                filter: "[UnsTagReferenceId] IS NOT NULL");

            migrationBuilder.CreateIndex(
                name: "IX_VirtualTag_Script",
                table: "VirtualTag",
                column: "ScriptId");

            migrationBuilder.CreateIndex(
                name: "UX_VirtualTag_EquipmentPath",
                table: "VirtualTag",
                columns: new[] { "EquipmentId", "Name" },
                unique: true);

            migrationBuilder.CreateIndex(
                name: "UX_VirtualTag_LogicalId",
                table: "VirtualTag",
                column: "VirtualTagId",
                unique: true,
                filter: "[VirtualTagId] IS NOT NULL");

            // Hand-authored SQL squashed in from the retired per-migration proc + grant files
            // (see V3Initial.StoredProcedures.cs for the full rationale + the v3 rewrites).
            CreateStoredProcedures(migrationBuilder);
            CreateAuthorizationGrants(migrationBuilder);
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            DropAuthorizationGrants(migrationBuilder);
            DropStoredProcedures(migrationBuilder);

            migrationBuilder.DropTable(
                name: "ClusterNodeCredential");

            migrationBuilder.DropTable(
                name: "ConfigAuditLog");

            migrationBuilder.DropTable(
                name: "ConfigEdit");

            migrationBuilder.DropTable(
                name: "DataProtectionKeys");

            migrationBuilder.DropTable(
                name: "Device");

            migrationBuilder.DropTable(
                name: "DriverHostStatus");

            migrationBuilder.DropTable(
                name: "DriverInstance");

            migrationBuilder.DropTable(
                name: "DriverInstanceResilienceStatus");

            migrationBuilder.DropTable(
                name: "Equipment");

            migrationBuilder.DropTable(
                name: "ExternalIdReservation");

            migrationBuilder.DropTable(
                name: "LdapGroupRoleMapping");

            migrationBuilder.DropTable(
                name: "NodeAcl");

            migrationBuilder.DropTable(
                name: "NodeDeploymentState");

            migrationBuilder.DropTable(
                name: "PollGroup");

            migrationBuilder.DropTable(
                name: "RawFolder");

            migrationBuilder.DropTable(
                name: "Script");

            migrationBuilder.DropTable(
                name: "ScriptedAlarm");

            migrationBuilder.DropTable(
                name: "ScriptedAlarmState");

            migrationBuilder.DropTable(
                name: "Tag");

            migrationBuilder.DropTable(
                name: "TagGroup");

            migrationBuilder.DropTable(
                name: "UnsArea");

            migrationBuilder.DropTable(
                name: "UnsLine");

            migrationBuilder.DropTable(
                name: "UnsTagReference");

            migrationBuilder.DropTable(
                name: "VirtualTag");

            migrationBuilder.DropTable(
                name: "ClusterNode");

            migrationBuilder.DropTable(
                name: "Deployment");

            migrationBuilder.DropTable(
                name: "ServerCluster");
        }
    }
}
