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;
BEGIN TRANSACTION;
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20220323094415_Init')
BEGIN
    CREATE TABLE [AppSettings] (
        [Id] nvarchar(450) NOT NULL,
        [Value] nvarchar(max) NOT NULL,
        CONSTRAINT [PK_AppSettings] PRIMARY KEY ([Id])
    );
END;
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20220323094415_Init')
BEGIN
    CREATE TABLE [AspNetRoles] (
        [Id] nvarchar(450) NOT NULL,
        [Name] nvarchar(256) NULL,
        [NormalizedName] nvarchar(256) NULL,
        [ConcurrencyStamp] nvarchar(max) NULL,
        CONSTRAINT [PK_AspNetRoles] PRIMARY KEY ([Id])
    );
END;
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20220323094415_Init')
BEGIN
    CREATE TABLE [Companies] (
        [Id] nvarchar(450) NOT NULL,
        [Name] nvarchar(max) NOT NULL,
        CONSTRAINT [PK_Companies] PRIMARY KEY ([Id])
    );
END;
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20220323094415_Init')
BEGIN
    CREATE TABLE [DataProtection] (
        [Id] int NOT NULL IDENTITY,
        [Value] nvarchar(max) NULL,
        [CreatedAt] datetime2 NOT NULL,
        CONSTRAINT [PK_DataProtection] PRIMARY KEY ([Id])
    );
END;
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20220323094415_Init')
BEGIN
    CREATE TABLE [DataProtectionKeys] (
        [Id] int NOT NULL IDENTITY,
        [FriendlyName] nvarchar(max) NULL,
        [Xml] nvarchar(max) NULL,
        CONSTRAINT [PK_DataProtectionKeys] PRIMARY KEY ([Id])
    );
END;
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20220323094415_Init')
BEGIN
    CREATE TABLE [DomainsSettings] (
        [Id] nvarchar(450) NOT NULL,
        [Type] int NOT NULL,
        [Name] nvarchar(max) NULL,
        [Credentials] nvarchar(max) NULL,
        [EnableSso] bit NOT NULL,
        [RequiredTwoFactor] bit NOT NULL,
        [AllowPasswordlessByU2F] bit NOT NULL,
        [ExternalIdField] nvarchar(max) NULL,
        [CertificateTemplateId] nvarchar(max) NULL,
        [CspName] nvarchar(max) NULL,
        [CertificateConfig] nvarchar(max) NULL,
        CONSTRAINT [PK_DomainsSettings] PRIMARY KEY ([Id])
    );
END;
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20220323094415_Init')
BEGIN
    CREATE TABLE [EmployeesLicenseOrders] (
        [Id] nvarchar(450) NOT NULL,
        [ContactEmail] nvarchar(max) NOT NULL,
        [Note] nvarchar(max) NULL,
        [EmployeesCount] int NOT NULL,
        [EndDate] datetime2 NOT NULL,
        [CreatedAt] datetime2 NOT NULL,
        [OrderStatus] int NOT NULL,
        CONSTRAINT [PK_EmployeesLicenseOrders] PRIMARY KEY ([Id])
    );
END;
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20220323094415_Init')
BEGIN
    CREATE TABLE [FidoStoredCredential] (
        [Id] nvarchar(450) NOT NULL,
        [SecurityKeyName] nvarchar(max) NULL,
        [Username] nvarchar(max) NULL,
        [UserId] varbinary(max) NULL,
        [PublicKey] varbinary(max) NULL,
        [UserHandle] varbinary(max) NULL,
        [SignatureCounter] bigint NOT NULL,
        [CredType] nvarchar(max) NULL,
        [RegDate] datetime2 NOT NULL,
        [AaGuid] uniqueidentifier NOT NULL,
        [DescriptorJson] nvarchar(max) NULL,
        CONSTRAINT [PK_FidoStoredCredential] PRIMARY KEY ([Id])
    );
END;
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20220323094415_Init')
BEGIN
    CREATE TABLE [Groups] (
        [Id] nvarchar(450) NOT NULL,
        [Name] nvarchar(450) NOT NULL,
        [Email] nvarchar(max) NULL,
        [Description] nvarchar(max) NULL,
        [ChangePasswordWhenExpired] bit NOT NULL,
        CONSTRAINT [PK_Groups] PRIMARY KEY ([Id])
    );
END;
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20220323094415_Init')
BEGIN
    CREATE TABLE [HardwareVaultActivations] (
        [Id] nvarchar(450) NOT NULL,
        [VaultId] nvarchar(max) NOT NULL,
        [AcivationCode] nvarchar(max) NOT NULL,
        [CreatedAt] datetime2 NOT NULL,
        [Status] int NOT NULL,
        CONSTRAINT [PK_HardwareVaultActivations] PRIMARY KEY ([Id])
    );
END;
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20220323094415_Init')
BEGIN
    CREATE TABLE [HardwareVaultLicenseOrders] (
        [Id] nvarchar(450) NOT NULL,
        [ContactEmail] nvarchar(max) NOT NULL,
        [Note] nvarchar(max) NULL,
        [StartDate] datetime2 NULL,
        [EndDate] datetime2 NOT NULL,
        [ProlongExistingLicenses] bit NOT NULL,
        [CreatedAt] datetime2 NOT NULL,
        [OrderStatus] int NOT NULL,
        CONSTRAINT [PK_HardwareVaultLicenseOrders] PRIMARY KEY ([Id])
    );
END;
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20220323094415_Init')
BEGIN
    CREATE TABLE [HardwareVaultProfiles] (
        [Id] nvarchar(450) NOT NULL,
        [Name] nvarchar(max) NOT NULL,
        [CreatedAt] datetime2 NOT NULL,
        [UpdatedAt] datetime2 NULL,
        [ButtonPairing] bit NOT NULL,
        [ButtonConnection] bit NOT NULL,
        [ButtonStorageAccess] bit NOT NULL,
        [PinPairing] bit NOT NULL,
        [PinConnection] bit NOT NULL,
        [PinStorageAccess] bit NOT NULL,
        [MasterKeyPairing] bit NOT NULL,
        [MasterKeyConnection] bit NOT NULL,
        [MasterKeyStorageAccess] bit NOT NULL,
        [PinExpiration] int NOT NULL,
        [PinLength] int NOT NULL,
        [PinTryCount] int NOT NULL,
        CONSTRAINT [PK_HardwareVaultProfiles] PRIMARY KEY ([Id])
    );
END;
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20220323094415_Init')
BEGIN
    CREATE TABLE [OpenIddictApplications] (
        [Id] nvarchar(450) NOT NULL,
        [ClientId] nvarchar(100) NULL,
        [ClientSecret] nvarchar(max) NULL,
        [ConcurrencyToken] nvarchar(50) NULL,
        [ConsentType] nvarchar(50) NULL,
        [DisplayName] nvarchar(max) NULL,
        [DisplayNames] nvarchar(max) NULL,
        [Permissions] nvarchar(max) NULL,
        [PostLogoutRedirectUris] nvarchar(max) NULL,
        [Properties] nvarchar(max) NULL,
        [RedirectUris] nvarchar(max) NULL,
        [Requirements] nvarchar(max) NULL,
        [Type] nvarchar(50) NULL,
        CONSTRAINT [PK_OpenIddictApplications] PRIMARY KEY ([Id])
    );
END;
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20220323094415_Init')
BEGIN
    CREATE TABLE [OpenIddictScopes] (
        [Id] nvarchar(450) NOT NULL,
        [ConcurrencyToken] nvarchar(50) NULL,
        [Description] nvarchar(max) NULL,
        [Descriptions] nvarchar(max) NULL,
        [DisplayName] nvarchar(max) NULL,
        [DisplayNames] nvarchar(max) NULL,
        [Name] nvarchar(200) NULL,
        [Properties] nvarchar(max) NULL,
        [Resources] nvarchar(max) NULL,
        CONSTRAINT [PK_OpenIddictScopes] PRIMARY KEY ([Id])
    );
END;
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20220323094415_Init')
BEGIN
    CREATE TABLE [Positions] (
        [Id] nvarchar(450) NOT NULL,
        [Name] nvarchar(max) NOT NULL,
        CONSTRAINT [PK_Positions] PRIMARY KEY ([Id])
    );
END;
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20220323094415_Init')
BEGIN
    CREATE TABLE [SamlRelyingParties] (
        [Id] nvarchar(450) NOT NULL,
        [Issuer] nvarchar(450) NOT NULL,
        [SingleSignOnDestination] nvarchar(max) NOT NULL,
        [SingleLogoutResponseDestination] nvarchar(max) NULL,
        [SignatureValidationCertificateBase64] nvarchar(max) NULL,
        [NameIdentifierFormat] nvarchar(max) NOT NULL,
        [NameIdentifierField] int NOT NULL,
        CONSTRAINT [PK_SamlRelyingParties] PRIMARY KEY ([Id])
    );
END;
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20220323094415_Init')
BEGIN
    CREATE TABLE [SharedAccounts] (
        [Id] nvarchar(450) NOT NULL,
        [Name] nvarchar(max) NOT NULL,
        [Urls] nvarchar(max) NULL,
        [Apps] nvarchar(max) NULL,
        [LoginType] int NOT NULL,
        [Login] nvarchar(max) NOT NULL,
        [Password] nvarchar(max) NOT NULL,
        [PasswordChangedAt] datetime2 NULL,
        [OtpSecret] nvarchar(max) NULL,
        [OtpSecretChangedAt] datetime2 NULL,
        [CreatedAt] datetime2 NOT NULL,
        CONSTRAINT [PK_SharedAccounts] PRIMARY KEY ([Id])
    );
END;
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20220323094415_Init')
BEGIN
    CREATE TABLE [Templates] (
        [Id] nvarchar(450) NOT NULL,
        [Name] nvarchar(max) NOT NULL,
        [Urls] nvarchar(max) NULL,
        [Apps] nvarchar(max) NULL,
        [CreatedAt] datetime2 NOT NULL,
        CONSTRAINT [PK_Templates] PRIMARY KEY ([Id])
    );
END;
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20220323094415_Init')
BEGIN
    CREATE TABLE [AspNetRoleClaims] (
        [Id] int NOT NULL IDENTITY,
        [RoleId] nvarchar(450) NOT NULL,
        [ClaimType] nvarchar(max) NULL,
        [ClaimValue] nvarchar(max) NULL,
        CONSTRAINT [PK_AspNetRoleClaims] PRIMARY KEY ([Id]),
        CONSTRAINT [FK_AspNetRoleClaims_AspNetRoles_RoleId] FOREIGN KEY ([RoleId]) REFERENCES [AspNetRoles] ([Id]) ON DELETE CASCADE
    );
END;
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20220323094415_Init')
BEGIN
    CREATE TABLE [Departments] (
        [Id] nvarchar(450) NOT NULL,
        [Name] nvarchar(max) NOT NULL,
        [CompanyId] nvarchar(450) NOT NULL,
        CONSTRAINT [PK_Departments] PRIMARY KEY ([Id]),
        CONSTRAINT [FK_Departments_Companies_CompanyId] FOREIGN KEY ([CompanyId]) REFERENCES [Companies] ([Id]) ON DELETE CASCADE
    );
END;
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20220323094415_Init')
BEGIN
    CREATE TABLE [EmployeesLicenses] (
        [Id] nvarchar(450) NOT NULL,
        [EmployeesLicenseOrderId] nvarchar(450) NULL,
        [ImportedAt] datetime2 NOT NULL,
        [Data] nvarchar(max) NULL,
        [Signature] nvarchar(max) NULL,
        CONSTRAINT [PK_EmployeesLicenses] PRIMARY KEY ([Id]),
        CONSTRAINT [FK_EmployeesLicenses_EmployeesLicenseOrders_EmployeesLicenseOrderId] FOREIGN KEY ([EmployeesLicenseOrderId]) REFERENCES [EmployeesLicenseOrders] ([Id]) ON DELETE CASCADE
    );
END;
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20220323094415_Init')
BEGIN
    CREATE TABLE [OpenIddictAuthorizations] (
        [Id] nvarchar(450) NOT NULL,
        [ApplicationId] nvarchar(450) NULL,
        [ConcurrencyToken] nvarchar(50) NULL,
        [CreationDate] datetime2 NULL,
        [Properties] nvarchar(max) NULL,
        [Scopes] nvarchar(max) NULL,
        [Status] nvarchar(50) NULL,
        [Subject] nvarchar(400) NULL,
        [Type] nvarchar(50) NULL,
        CONSTRAINT [PK_OpenIddictAuthorizations] PRIMARY KEY ([Id]),
        CONSTRAINT [FK_OpenIddictAuthorizations_OpenIddictApplications_ApplicationId] FOREIGN KEY ([ApplicationId]) REFERENCES [OpenIddictApplications] ([Id]) ON DELETE NO ACTION
    );
END;
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20220323094415_Init')
BEGIN
    CREATE TABLE [Employees] (
        [Id] nvarchar(450) NOT NULL,
        [FirstName] nvarchar(max) NOT NULL,
        [LastName] nvarchar(max) NULL,
        [Email] nvarchar(max) NOT NULL,
        [PhoneNumber] nvarchar(max) NULL,
        [DepartmentId] nvarchar(450) NULL,
        [PositionId] nvarchar(450) NULL,
        [LastSeen] datetime2 NULL,
        [RegisteredAt] datetime2 NOT NULL,
        CONSTRAINT [PK_Employees] PRIMARY KEY ([Id]),
        CONSTRAINT [FK_Employees_Departments_DepartmentId] FOREIGN KEY ([DepartmentId]) REFERENCES [Departments] ([Id]) ON DELETE SET NULL,
        CONSTRAINT [FK_Employees_Positions_PositionId] FOREIGN KEY ([PositionId]) REFERENCES [Positions] ([Id]) ON DELETE SET NULL
    );
END;
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20220323094415_Init')
BEGIN
    CREATE TABLE [Workstations] (
        [Id] nvarchar(450) NOT NULL,
        [Name] nvarchar(max) NULL,
        [Domain] nvarchar(max) NULL,
        [ClientVersion] nvarchar(max) NULL,
        [DepartmentId] nvarchar(450) NULL,
        [OS] nvarchar(max) NULL,
        [IP] nvarchar(max) NULL,
        [RegisteredAt] datetime2 NOT NULL,
        [LastSeen] datetime2 NOT NULL,
        [Approved] bit NOT NULL,
        [RFID] bit NOT NULL,
        CONSTRAINT [PK_Workstations] PRIMARY KEY ([Id]),
        CONSTRAINT [FK_Workstations_Departments_DepartmentId] FOREIGN KEY ([DepartmentId]) REFERENCES [Departments] ([Id]) ON DELETE SET NULL
    );
END;
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20220323094415_Init')
BEGIN
    CREATE TABLE [OpenIddictTokens] (
        [Id] nvarchar(450) NOT NULL,
        [ApplicationId] nvarchar(450) NULL,
        [AuthorizationId] nvarchar(450) NULL,
        [ConcurrencyToken] nvarchar(50) NULL,
        [CreationDate] datetime2 NULL,
        [ExpirationDate] datetime2 NULL,
        [Payload] nvarchar(max) NULL,
        [Properties] nvarchar(max) NULL,
        [RedemptionDate] datetime2 NULL,
        [ReferenceId] nvarchar(100) NULL,
        [Status] nvarchar(50) NULL,
        [Subject] nvarchar(400) NULL,
        [Type] nvarchar(50) NULL,
        CONSTRAINT [PK_OpenIddictTokens] PRIMARY KEY ([Id]),
        CONSTRAINT [FK_OpenIddictTokens_OpenIddictApplications_ApplicationId] FOREIGN KEY ([ApplicationId]) REFERENCES [OpenIddictApplications] ([Id]) ON DELETE NO ACTION,
        CONSTRAINT [FK_OpenIddictTokens_OpenIddictAuthorizations_AuthorizationId] FOREIGN KEY ([AuthorizationId]) REFERENCES [OpenIddictAuthorizations] ([Id]) ON DELETE NO ACTION
    );
END;
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20220323094415_Init')
BEGIN
    CREATE TABLE [Accounts] (
        [Id] nvarchar(450) NOT NULL,
        [Name] nvarchar(max) NOT NULL,
        [Urls] nvarchar(max) NULL,
        [Apps] nvarchar(max) NULL,
        [Login] nvarchar(max) NOT NULL,
        [AccountType] int NOT NULL,
        [LoginType] int NOT NULL,
        [CreatedAt] datetime2 NOT NULL,
        [UpdatedAt] datetime2 NULL,
        [PasswordUpdatedAt] datetime2 NOT NULL,
        [OtpUpdatedAt] datetime2 NULL,
        [Password] nvarchar(max) NULL,
        [OtpSecret] nvarchar(max) NULL,
        [StorageId] varbinary(max) NOT NULL,
        [Timestamp] bigint NOT NULL,
        [EmployeeId] nvarchar(450) NULL,
        [SharedAccountId] nvarchar(450) NULL,
        CONSTRAINT [PK_Accounts] PRIMARY KEY ([Id]),
        CONSTRAINT [FK_Accounts_Employees_EmployeeId] FOREIGN KEY ([EmployeeId]) REFERENCES [Employees] ([Id]) ON DELETE CASCADE,
        CONSTRAINT [FK_Accounts_SharedAccounts_SharedAccountId] FOREIGN KEY ([SharedAccountId]) REFERENCES [SharedAccounts] ([Id]) ON DELETE NO ACTION
    );
END;
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20220323094415_Init')
BEGIN
    CREATE TABLE [AspNetUsers] (
        [Id] nvarchar(450) NOT NULL,
        [Culture] nvarchar(max) NULL,
        [SingleSignOnEnabled] bit NOT NULL,
        [RequiredTwoFactor] bit NOT NULL,
        [AllowPasswordlessByU2F] bit NOT NULL,
        [ExternalId] nvarchar(max) NULL,
        [LastPasswordChange] datetime2 NULL,
        [HasAuthenticator] bit NOT NULL,
        [RegistrationStamp] nvarchar(max) NULL,
        [AuthenticationStamp] nvarchar(max) NULL,
        [UserName] nvarchar(256) NULL,
        [NormalizedUserName] nvarchar(256) NULL,
        [Email] nvarchar(256) NULL,
        [NormalizedEmail] nvarchar(256) NULL,
        [EmailConfirmed] bit NOT NULL,
        [PasswordHash] nvarchar(max) NULL,
        [SecurityStamp] nvarchar(max) NULL,
        [ConcurrencyStamp] nvarchar(max) NULL,
        [PhoneNumber] nvarchar(max) NULL,
        [PhoneNumberConfirmed] bit NOT NULL,
        [TwoFactorEnabled] bit NOT NULL,
        [LockoutEnd] datetimeoffset NULL,
        [LockoutEnabled] bit NOT NULL,
        [AccessFailedCount] int NOT NULL,
        CONSTRAINT [PK_AspNetUsers] PRIMARY KEY ([Id]),
        CONSTRAINT [FK_AspNetUsers_Employees_Id] FOREIGN KEY ([Id]) REFERENCES [Employees] ([Id]) ON DELETE CASCADE
    );
END;
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20220323094415_Init')
BEGIN
    CREATE TABLE [EmployeeDomains] (
        [Id] nvarchar(450) NOT NULL,
        [EmployeeId] nvarchar(450) NULL,
        [DomainSettingsId] nvarchar(450) NULL,
        [ActiveDirectoryGuid] nvarchar(max) NULL,
        CONSTRAINT [PK_EmployeeDomains] PRIMARY KEY ([Id]),
        CONSTRAINT [FK_EmployeeDomains_DomainsSettings_DomainSettingsId] FOREIGN KEY ([DomainSettingsId]) REFERENCES [DomainsSettings] ([Id]) ON DELETE CASCADE,
        CONSTRAINT [FK_EmployeeDomains_Employees_EmployeeId] FOREIGN KEY ([EmployeeId]) REFERENCES [Employees] ([Id]) ON DELETE CASCADE
    );
END;
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20220323094415_Init')
BEGIN
    CREATE TABLE [GroupMemberships] (
        [Id] nvarchar(450) NOT NULL,
        [GroupId] nvarchar(450) NULL,
        [EmployeeId] nvarchar(450) NULL,
        CONSTRAINT [PK_GroupMemberships] PRIMARY KEY ([Id]),
        CONSTRAINT [FK_GroupMemberships_Employees_EmployeeId] FOREIGN KEY ([EmployeeId]) REFERENCES [Employees] ([Id]) ON DELETE CASCADE,
        CONSTRAINT [FK_GroupMemberships_Groups_GroupId] FOREIGN KEY ([GroupId]) REFERENCES [Groups] ([Id]) ON DELETE CASCADE
    );
END;
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20220323094415_Init')
BEGIN
    CREATE TABLE [HardwareVaultBackups] (
        [Id] nvarchar(450) NOT NULL,
        [EmployeeId] nvarchar(450) NOT NULL,
        [HardwareVaultId] nvarchar(max) NOT NULL,
        [Password] nvarchar(max) NOT NULL,
        [Accounts] nvarchar(max) NOT NULL,
        [Data] varbinary(max) NOT NULL,
        [StorageUpdateCounter] tinyint NOT NULL,
        [CreatedAt] datetime2 NOT NULL,
        [DurationOfCreation] time NOT NULL,
        CONSTRAINT [PK_HardwareVaultBackups] PRIMARY KEY ([Id]),
        CONSTRAINT [FK_HardwareVaultBackups_Employees_EmployeeId] FOREIGN KEY ([EmployeeId]) REFERENCES [Employees] ([Id]) ON DELETE CASCADE
    );
END;
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20220323094415_Init')
BEGIN
    CREATE TABLE [HardwareVaults] (
        [Id] nvarchar(450) NOT NULL,
        [MAC] nvarchar(450) NOT NULL,
        [Model] nvarchar(max) NOT NULL,
        [RFID] nvarchar(450) NOT NULL,
        [Battery] int NOT NULL,
        [Firmware] nvarchar(max) NOT NULL,
        [Status] int NOT NULL,
        [StatusReason] int NOT NULL,
        [StatusDescription] nvarchar(max) NULL,
        [LastSynced] datetime2 NULL,
        [EmployeeId] nvarchar(450) NULL,
        [MasterPassword] nvarchar(max) NULL,
        [HardwareVaultProfileId] nvarchar(450) NOT NULL,
        [ImportedAt] datetime2 NOT NULL,
        [Timestamp] bigint NOT NULL,
        [HasNewLicense] bit NOT NULL,
        [IsStatusApplied] bit NOT NULL,
        [LicenseStatus] int NOT NULL,
        [LicenseEndDate] datetime2 NULL,
        CONSTRAINT [PK_HardwareVaults] PRIMARY KEY ([Id]),
        CONSTRAINT [FK_HardwareVaults_Employees_EmployeeId] FOREIGN KEY ([EmployeeId]) REFERENCES [Employees] ([Id]) ON DELETE NO ACTION,
        CONSTRAINT [FK_HardwareVaults_HardwareVaultProfiles_HardwareVaultProfileId] FOREIGN KEY ([HardwareVaultProfileId]) REFERENCES [HardwareVaultProfiles] ([Id]) ON DELETE CASCADE
    );
END;
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20220323094415_Init')
BEGIN
    CREATE TABLE [AspNetUserClaims] (
        [Id] int NOT NULL IDENTITY,
        [UserId] nvarchar(450) NOT NULL,
        [ClaimType] nvarchar(max) NULL,
        [ClaimValue] nvarchar(max) NULL,
        CONSTRAINT [PK_AspNetUserClaims] PRIMARY KEY ([Id]),
        CONSTRAINT [FK_AspNetUserClaims_AspNetUsers_UserId] FOREIGN KEY ([UserId]) REFERENCES [AspNetUsers] ([Id]) ON DELETE CASCADE
    );
END;
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20220323094415_Init')
BEGIN
    CREATE TABLE [AspNetUserLogins] (
        [LoginProvider] nvarchar(450) NOT NULL,
        [ProviderKey] nvarchar(450) NOT NULL,
        [ProviderDisplayName] nvarchar(max) NULL,
        [UserId] nvarchar(450) NOT NULL,
        CONSTRAINT [PK_AspNetUserLogins] PRIMARY KEY ([LoginProvider], [ProviderKey]),
        CONSTRAINT [FK_AspNetUserLogins_AspNetUsers_UserId] FOREIGN KEY ([UserId]) REFERENCES [AspNetUsers] ([Id]) ON DELETE CASCADE
    );
END;
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20220323094415_Init')
BEGIN
    CREATE TABLE [AspNetUserRoles] (
        [UserId] nvarchar(450) NOT NULL,
        [RoleId] nvarchar(450) NOT NULL,
        CONSTRAINT [PK_AspNetUserRoles] PRIMARY KEY ([UserId], [RoleId]),
        CONSTRAINT [FK_AspNetUserRoles_AspNetRoles_RoleId] FOREIGN KEY ([RoleId]) REFERENCES [AspNetRoles] ([Id]) ON DELETE CASCADE,
        CONSTRAINT [FK_AspNetUserRoles_AspNetUsers_UserId] FOREIGN KEY ([UserId]) REFERENCES [AspNetUsers] ([Id]) ON DELETE CASCADE
    );
END;
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20220323094415_Init')
BEGIN
    CREATE TABLE [AspNetUserTokens] (
        [UserId] nvarchar(450) NOT NULL,
        [LoginProvider] nvarchar(450) NOT NULL,
        [Name] nvarchar(450) NOT NULL,
        [Value] nvarchar(max) NULL,
        CONSTRAINT [PK_AspNetUserTokens] PRIMARY KEY ([UserId], [LoginProvider], [Name]),
        CONSTRAINT [FK_AspNetUserTokens_AspNetUsers_UserId] FOREIGN KEY ([UserId]) REFERENCES [AspNetUsers] ([Id]) ON DELETE CASCADE
    );
END;
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20220323094415_Init')
BEGIN
    CREATE TABLE [SoftwareVaults] (
        [Id] nvarchar(450) NOT NULL,
        [Model] nvarchar(max) NULL,
        [OS] nvarchar(max) NULL,
        [ClientAppVersion] nvarchar(max) NULL,
        [RegistrationDate] datetime2 NOT NULL,
        [RegistrationCompleted] bit NOT NULL,
        [AuthenticatorKey] nvarchar(max) NOT NULL,
        [UserId] nvarchar(450) NOT NULL,
        CONSTRAINT [PK_SoftwareVaults] PRIMARY KEY ([Id]),
        CONSTRAINT [FK_SoftwareVaults_AspNetUsers_UserId] FOREIGN KEY ([UserId]) REFERENCES [AspNetUsers] ([Id]) ON DELETE CASCADE
    );
END;
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20220323094415_Init')
BEGIN
    CREATE TABLE [HardwareVaultLicenses] (
        [Id] nvarchar(450) NOT NULL,
        [HardwareVaultId] nvarchar(450) NOT NULL,
        [LicenseOrderId] nvarchar(450) NOT NULL,
        [ImportedAt] datetime2 NULL,
        [AppliedAt] datetime2 NULL,
        [EndDate] datetime2 NULL,
        [Data] varbinary(max) NULL,
        CONSTRAINT [PK_HardwareVaultLicenses] PRIMARY KEY ([Id]),
        CONSTRAINT [FK_HardwareVaultLicenses_HardwareVaultLicenseOrders_LicenseOrderId] FOREIGN KEY ([LicenseOrderId]) REFERENCES [HardwareVaultLicenseOrders] ([Id]) ON DELETE CASCADE,
        CONSTRAINT [FK_HardwareVaultLicenses_HardwareVaults_HardwareVaultId] FOREIGN KEY ([HardwareVaultId]) REFERENCES [HardwareVaults] ([Id]) ON DELETE CASCADE
    );
END;
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20220323094415_Init')
BEGIN
    CREATE TABLE [HardwareVaultTasks] (
        [Id] nvarchar(450) NOT NULL,
        [Password] nvarchar(max) NULL,
        [OtpSecret] nvarchar(max) NULL,
        [Operation] int NOT NULL,
        [CreatedAt] datetime2 NOT NULL,
        [Timestamp] bigint NOT NULL,
        [AccountStorageId] varbinary(max) NULL,
        [AccountForUnlock] bit NOT NULL,
        [AccountId] nvarchar(450) NULL,
        [HardwareVaultId] nvarchar(450) NULL,
        [HardwareVaultBackupId] nvarchar(450) NULL,
        CONSTRAINT [PK_HardwareVaultTasks] PRIMARY KEY ([Id]),
        CONSTRAINT [FK_HardwareVaultTasks_Accounts_AccountId] FOREIGN KEY ([AccountId]) REFERENCES [Accounts] ([Id]) ON DELETE CASCADE,
        CONSTRAINT [FK_HardwareVaultTasks_HardwareVaultBackups_HardwareVaultBackupId] FOREIGN KEY ([HardwareVaultBackupId]) REFERENCES [HardwareVaultBackups] ([Id]) ON DELETE NO ACTION,
        CONSTRAINT [FK_HardwareVaultTasks_HardwareVaults_HardwareVaultId] FOREIGN KEY ([HardwareVaultId]) REFERENCES [HardwareVaults] ([Id]) ON DELETE NO ACTION
    );
END;
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20220323094415_Init')
BEGIN
    CREATE TABLE [WorkstationEvents] (
        [Id] nvarchar(450) NOT NULL,
        [Date] datetime2 NOT NULL,
        [EventId] int NOT NULL,
        [SeverityId] int NOT NULL,
        [Note] nvarchar(max) NULL,
        [WorkstationId] nvarchar(450) NULL,
        [UserSession] nvarchar(max) NULL,
        [HardwareVaultId] nvarchar(450) NULL,
        [EmployeeId] nvarchar(450) NULL,
        [DepartmentId] nvarchar(450) NULL,
        [AccountName] nvarchar(max) NULL,
        [LoginType] int NULL,
        [AccountType] int NULL,
        CONSTRAINT [PK_WorkstationEvents] PRIMARY KEY ([Id]),
        CONSTRAINT [FK_WorkstationEvents_Departments_DepartmentId] FOREIGN KEY ([DepartmentId]) REFERENCES [Departments] ([Id]) ON DELETE SET NULL,
        CONSTRAINT [FK_WorkstationEvents_Employees_EmployeeId] FOREIGN KEY ([EmployeeId]) REFERENCES [Employees] ([Id]) ON DELETE CASCADE,
        CONSTRAINT [FK_WorkstationEvents_HardwareVaults_HardwareVaultId] FOREIGN KEY ([HardwareVaultId]) REFERENCES [HardwareVaults] ([Id]) ON DELETE NO ACTION,
        CONSTRAINT [FK_WorkstationEvents_Workstations_WorkstationId] FOREIGN KEY ([WorkstationId]) REFERENCES [Workstations] ([Id]) ON DELETE CASCADE
    );
END;
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20220323094415_Init')
BEGIN
    CREATE TABLE [WorkstationHardwareVaultPairs] (
        [Id] nvarchar(450) NOT NULL,
        [HardwareVaultId] nvarchar(450) NULL,
        [WorkstationId] nvarchar(450) NULL,
        [LockProximity] int NOT NULL,
        [UnlockProximity] int NOT NULL,
        [LockTimeout] int NOT NULL,
        [CreatedAt] datetime2 NOT NULL,
        CONSTRAINT [PK_WorkstationHardwareVaultPairs] PRIMARY KEY ([Id]),
        CONSTRAINT [FK_WorkstationHardwareVaultPairs_HardwareVaults_HardwareVaultId] FOREIGN KEY ([HardwareVaultId]) REFERENCES [HardwareVaults] ([Id]) ON DELETE NO ACTION,
        CONSTRAINT [FK_WorkstationHardwareVaultPairs_Workstations_WorkstationId] FOREIGN KEY ([WorkstationId]) REFERENCES [Workstations] ([Id]) ON DELETE CASCADE
    );
END;
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20220323094415_Init')
BEGIN
    CREATE TABLE [WorkstationSessions] (
        [Id] nvarchar(450) NOT NULL,
        [StartDate] datetime2 NOT NULL,
        [EndDate] datetime2 NULL,
        [UnlockedBy] int NOT NULL,
        [WorkstationId] nvarchar(450) NULL,
        [UserSession] nvarchar(max) NULL,
        [HardwareVaultId] nvarchar(450) NULL,
        [EmployeeId] nvarchar(450) NULL,
        [DepartmentId] nvarchar(450) NULL,
        [AccountName] nvarchar(max) NULL,
        [LoginType] int NULL,
        [AccountType] int NULL,
        CONSTRAINT [PK_WorkstationSessions] PRIMARY KEY ([Id]),
        CONSTRAINT [FK_WorkstationSessions_Departments_DepartmentId] FOREIGN KEY ([DepartmentId]) REFERENCES [Departments] ([Id]) ON DELETE SET NULL,
        CONSTRAINT [FK_WorkstationSessions_Employees_EmployeeId] FOREIGN KEY ([EmployeeId]) REFERENCES [Employees] ([Id]) ON DELETE CASCADE,
        CONSTRAINT [FK_WorkstationSessions_HardwareVaults_HardwareVaultId] FOREIGN KEY ([HardwareVaultId]) REFERENCES [HardwareVaults] ([Id]) ON DELETE NO ACTION,
        CONSTRAINT [FK_WorkstationSessions_Workstations_WorkstationId] FOREIGN KEY ([WorkstationId]) REFERENCES [Workstations] ([Id]) ON DELETE CASCADE
    );
END;
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20220323094415_Init')
BEGIN
    CREATE INDEX [IX_Accounts_EmployeeId] ON [Accounts] ([EmployeeId]);
END;
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20220323094415_Init')
BEGIN
    CREATE INDEX [IX_Accounts_SharedAccountId] ON [Accounts] ([SharedAccountId]);
END;
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20220323094415_Init')
BEGIN
    CREATE INDEX [IX_AspNetRoleClaims_RoleId] ON [AspNetRoleClaims] ([RoleId]);
END;
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20220323094415_Init')
BEGIN
    EXEC(N'CREATE UNIQUE INDEX [RoleNameIndex] ON [AspNetRoles] ([NormalizedName]) WHERE [NormalizedName] IS NOT NULL');
END;
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20220323094415_Init')
BEGIN
    CREATE INDEX [IX_AspNetUserClaims_UserId] ON [AspNetUserClaims] ([UserId]);
END;
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20220323094415_Init')
BEGIN
    CREATE INDEX [IX_AspNetUserLogins_UserId] ON [AspNetUserLogins] ([UserId]);
END;
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20220323094415_Init')
BEGIN
    CREATE INDEX [IX_AspNetUserRoles_RoleId] ON [AspNetUserRoles] ([RoleId]);
END;
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20220323094415_Init')
BEGIN
    CREATE INDEX [EmailIndex] ON [AspNetUsers] ([NormalizedEmail]);
END;
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20220323094415_Init')
BEGIN
    EXEC(N'CREATE UNIQUE INDEX [UserNameIndex] ON [AspNetUsers] ([NormalizedUserName]) WHERE [NormalizedUserName] IS NOT NULL');
END;
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20220323094415_Init')
BEGIN
    CREATE INDEX [IX_Departments_CompanyId] ON [Departments] ([CompanyId]);
END;
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20220323094415_Init')
BEGIN
    CREATE INDEX [IX_EmployeeDomains_DomainSettingsId] ON [EmployeeDomains] ([DomainSettingsId]);
END;
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20220323094415_Init')
BEGIN
    CREATE INDEX [IX_EmployeeDomains_EmployeeId] ON [EmployeeDomains] ([EmployeeId]);
END;
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20220323094415_Init')
BEGIN
    CREATE INDEX [IX_Employees_DepartmentId] ON [Employees] ([DepartmentId]);
END;
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20220323094415_Init')
BEGIN
    CREATE INDEX [IX_Employees_PositionId] ON [Employees] ([PositionId]);
END;
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20220323094415_Init')
BEGIN
    EXEC(N'CREATE UNIQUE INDEX [IX_EmployeesLicenses_EmployeesLicenseOrderId] ON [EmployeesLicenses] ([EmployeesLicenseOrderId]) WHERE [EmployeesLicenseOrderId] IS NOT NULL');
END;
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20220323094415_Init')
BEGIN
    CREATE INDEX [IX_GroupMemberships_EmployeeId] ON [GroupMemberships] ([EmployeeId]);
END;
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20220323094415_Init')
BEGIN
    CREATE INDEX [IX_GroupMemberships_GroupId] ON [GroupMemberships] ([GroupId]);
END;
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20220323094415_Init')
BEGIN
    CREATE UNIQUE INDEX [IX_Groups_Name] ON [Groups] ([Name]);
END;
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20220323094415_Init')
BEGIN
    CREATE INDEX [IX_HardwareVaultBackups_EmployeeId] ON [HardwareVaultBackups] ([EmployeeId]);
END;
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20220323094415_Init')
BEGIN
    CREATE INDEX [IX_HardwareVaultLicenses_HardwareVaultId] ON [HardwareVaultLicenses] ([HardwareVaultId]);
END;
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20220323094415_Init')
BEGIN
    CREATE INDEX [IX_HardwareVaultLicenses_LicenseOrderId] ON [HardwareVaultLicenses] ([LicenseOrderId]);
END;
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20220323094415_Init')
BEGIN
    CREATE INDEX [IX_HardwareVaults_EmployeeId] ON [HardwareVaults] ([EmployeeId]);
END;
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20220323094415_Init')
BEGIN
    CREATE INDEX [IX_HardwareVaults_HardwareVaultProfileId] ON [HardwareVaults] ([HardwareVaultProfileId]);
END;
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20220323094415_Init')
BEGIN
    CREATE UNIQUE INDEX [IX_HardwareVaults_MAC] ON [HardwareVaults] ([MAC]);
END;
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20220323094415_Init')
BEGIN
    CREATE UNIQUE INDEX [IX_HardwareVaults_RFID] ON [HardwareVaults] ([RFID]);
END;
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20220323094415_Init')
BEGIN
    CREATE INDEX [IX_HardwareVaultTasks_AccountId] ON [HardwareVaultTasks] ([AccountId]);
END;
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20220323094415_Init')
BEGIN
    CREATE INDEX [IX_HardwareVaultTasks_HardwareVaultBackupId] ON [HardwareVaultTasks] ([HardwareVaultBackupId]);
END;
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20220323094415_Init')
BEGIN
    CREATE INDEX [IX_HardwareVaultTasks_HardwareVaultId] ON [HardwareVaultTasks] ([HardwareVaultId]);
END;
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20220323094415_Init')
BEGIN
    EXEC(N'CREATE UNIQUE INDEX [IX_OpenIddictApplications_ClientId] ON [OpenIddictApplications] ([ClientId]) WHERE [ClientId] IS NOT NULL');
END;
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20220323094415_Init')
BEGIN
    CREATE INDEX [IX_OpenIddictAuthorizations_ApplicationId_Status_Subject_Type] ON [OpenIddictAuthorizations] ([ApplicationId], [Status], [Subject], [Type]);
END;
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20220323094415_Init')
BEGIN
    EXEC(N'CREATE UNIQUE INDEX [IX_OpenIddictScopes_Name] ON [OpenIddictScopes] ([Name]) WHERE [Name] IS NOT NULL');
END;
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20220323094415_Init')
BEGIN
    CREATE INDEX [IX_OpenIddictTokens_ApplicationId_Status_Subject_Type] ON [OpenIddictTokens] ([ApplicationId], [Status], [Subject], [Type]);
END;
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20220323094415_Init')
BEGIN
    CREATE INDEX [IX_OpenIddictTokens_AuthorizationId] ON [OpenIddictTokens] ([AuthorizationId]);
END;
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20220323094415_Init')
BEGIN
    EXEC(N'CREATE UNIQUE INDEX [IX_OpenIddictTokens_ReferenceId] ON [OpenIddictTokens] ([ReferenceId]) WHERE [ReferenceId] IS NOT NULL');
END;
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20220323094415_Init')
BEGIN
    CREATE UNIQUE INDEX [IX_SamlRelyingParties_Issuer] ON [SamlRelyingParties] ([Issuer]);
END;
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20220323094415_Init')
BEGIN
    CREATE INDEX [IX_SoftwareVaults_UserId] ON [SoftwareVaults] ([UserId]);
END;
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20220323094415_Init')
BEGIN
    CREATE INDEX [IX_WorkstationEvents_DepartmentId] ON [WorkstationEvents] ([DepartmentId]);
END;
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20220323094415_Init')
BEGIN
    CREATE INDEX [IX_WorkstationEvents_EmployeeId] ON [WorkstationEvents] ([EmployeeId]);
END;
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20220323094415_Init')
BEGIN
    CREATE INDEX [IX_WorkstationEvents_HardwareVaultId] ON [WorkstationEvents] ([HardwareVaultId]);
END;
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20220323094415_Init')
BEGIN
    CREATE INDEX [IX_WorkstationEvents_WorkstationId] ON [WorkstationEvents] ([WorkstationId]);
END;
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20220323094415_Init')
BEGIN
    CREATE INDEX [IX_WorkstationHardwareVaultPairs_HardwareVaultId] ON [WorkstationHardwareVaultPairs] ([HardwareVaultId]);
END;
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20220323094415_Init')
BEGIN
    CREATE INDEX [IX_WorkstationHardwareVaultPairs_WorkstationId] ON [WorkstationHardwareVaultPairs] ([WorkstationId]);
END;
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20220323094415_Init')
BEGIN
    CREATE INDEX [IX_Workstations_DepartmentId] ON [Workstations] ([DepartmentId]);
END;
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20220323094415_Init')
BEGIN
    CREATE INDEX [IX_WorkstationSessions_DepartmentId] ON [WorkstationSessions] ([DepartmentId]);
END;
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20220323094415_Init')
BEGIN
    CREATE INDEX [IX_WorkstationSessions_EmployeeId] ON [WorkstationSessions] ([EmployeeId]);
END;
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20220323094415_Init')
BEGIN
    CREATE INDEX [IX_WorkstationSessions_HardwareVaultId] ON [WorkstationSessions] ([HardwareVaultId]);
END;
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20220323094415_Init')
BEGIN
    CREATE INDEX [IX_WorkstationSessions_WorkstationId] ON [WorkstationSessions] ([WorkstationId]);
END;
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20220323094415_Init')
BEGIN
    INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion])
    VALUES (N'20220323094415_Init', N'6.0.4');
END;
COMMIT;
BEGIN TRANSACTION;
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20220428140538_added_fido2_key_enrollments')
BEGIN
    CREATE TABLE [Fido2KeyEnrollments] (
        [Id] nvarchar(450) NOT NULL,
        [SerialNumber] nvarchar(max) NOT NULL,
        [KeyHandle] nvarchar(max) NOT NULL,
        [PublicKey] nvarchar(max) NOT NULL,
        [PIN] nvarchar(max) NOT NULL,
        [IsEnrolled] bit NOT NULL,
        [AllowRequestPIN] bit NOT NULL,
        [EmployeeId] nvarchar(450) NULL,
        CONSTRAINT [PK_Fido2KeyEnrollments] PRIMARY KEY ([Id]),
        CONSTRAINT [FK_Fido2KeyEnrollments_Employees_EmployeeId] FOREIGN KEY ([EmployeeId]) REFERENCES [Employees] ([Id]) ON DELETE SET NULL
    );
END;
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20220428140538_added_fido2_key_enrollments')
BEGIN
    CREATE TABLE [Fido2Metadatas] (
        [Id] nvarchar(450) NOT NULL,
        [Metadata] nvarchar(max) NOT NULL,
        CONSTRAINT [PK_Fido2Metadatas] PRIMARY KEY ([Id])
    );
END;
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20220428140538_added_fido2_key_enrollments')
BEGIN
    CREATE INDEX [IX_Fido2KeyEnrollments_EmployeeId] ON [Fido2KeyEnrollments] ([EmployeeId]);
END;
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20220428140538_added_fido2_key_enrollments')
BEGIN
    INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion])
    VALUES (N'20220428140538_added_fido2_key_enrollments', N'6.0.4');
END;
COMMIT;
BEGIN TRANSACTION;
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20220502100737_added_fido2_authenticator_attachment')
BEGIN
    EXEC sp_rename N'[FidoStoredCredential].[RegDate]', N'RegistrationDate', N'COLUMN';
END;
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20220502100737_added_fido2_authenticator_attachment')
BEGIN
    ALTER TABLE [FidoStoredCredential] ADD [AuthenticatorAttachment] int NOT NULL DEFAULT 1;
END;
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20220502100737_added_fido2_authenticator_attachment')
BEGIN
    EXEC sp_rename N'[FidoStoredCredential]', N'Fido2StoredCredentials';
END;
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20220502100737_added_fido2_authenticator_attachment')
BEGIN
    INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion])
    VALUES (N'20220502100737_added_fido2_authenticator_attachment', N'6.0.4');
END;
COMMIT;
BEGIN TRANSACTION;
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20220602183941_updated_data_protection')
BEGIN
    DROP TABLE [DataProtection];
END;
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20220602183941_updated_data_protection')
BEGIN
    CREATE TABLE [DataProtection] (
        [Id] nvarchar(450) NOT NULL,
        [Value] nvarchar(max) NULL,
        [CreatedAt] datetime2 NOT NULL,
        CONSTRAINT [PK_DataProtection] PRIMARY KEY ([Id])
    );
END;
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20220602183941_updated_data_protection')
BEGIN
    INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion])
    VALUES (N'20220602183941_updated_data_protection', N'6.0.4');
END;
COMMIT;
BEGIN TRANSACTION;
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20220606114648_added_allowed_workstations_field')
BEGIN
    ALTER TABLE [SharedAccounts] ADD [AllowedWorkstations] nvarchar(max) NULL;
END;
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20220606114648_added_allowed_workstations_field')
BEGIN
    ALTER TABLE [Accounts] ADD [AllowedWorkstations] nvarchar(max) NULL;
END;
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20220606114648_added_allowed_workstations_field')
BEGIN
    INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion])
    VALUES (N'20220606114648_added_allowed_workstations_field', N'6.0.4');
END;
COMMIT;
BEGIN TRANSACTION;
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20220629092606_removed_mds2_metadata_table')
BEGIN
    DROP TABLE [Fido2Metadatas];
END;
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20220629092606_removed_mds2_metadata_table')
BEGIN
    INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion])
    VALUES (N'20220629092606_removed_mds2_metadata_table', N'6.0.4');
END;
COMMIT;
BEGIN TRANSACTION;
IF EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20220323094415_Init')
BEGIN
    DELETE FROM [__EFMigrationsHistory]
END;
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20220706120808_Initial')
BEGIN
    INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion])
    VALUES (N'20220706120808_Initial', N'6.0.4');
END;
COMMIT;