DROP PROCEDURE IF EXISTS `POMELO_BEFORE_DROP_PRIMARY_KEY`;
 
CREATE PROCEDURE `POMELO_BEFORE_DROP_PRIMARY_KEY`(IN `SCHEMA_NAME_ARGUMENT` VARCHAR(255), IN `TABLE_NAME_ARGUMENT` VARCHAR(255))
BEGIN
	DECLARE HAS_AUTO_INCREMENT_ID TINYINT(1);
	DECLARE PRIMARY_KEY_COLUMN_NAME VARCHAR(255);
	DECLARE PRIMARY_KEY_TYPE VARCHAR(255);
	DECLARE SQL_EXP VARCHAR(1000);
	SELECT COUNT(*)
		INTO HAS_AUTO_INCREMENT_ID
		FROM `information_schema`.`COLUMNS`
		WHERE `TABLE_SCHEMA` = (SELECT IFNULL(SCHEMA_NAME_ARGUMENT, SCHEMA()))
			AND `TABLE_NAME` = TABLE_NAME_ARGUMENT
			AND `Extra` = 'auto_increment'
			AND `COLUMN_KEY` = 'PRI'
			LIMIT 1;
	IF HAS_AUTO_INCREMENT_ID THEN
		SELECT `COLUMN_TYPE`
			INTO PRIMARY_KEY_TYPE
			FROM `information_schema`.`COLUMNS`
			WHERE `TABLE_SCHEMA` = (SELECT IFNULL(SCHEMA_NAME_ARGUMENT, SCHEMA()))
				AND `TABLE_NAME` = TABLE_NAME_ARGUMENT
				AND `COLUMN_KEY` = 'PRI'
			LIMIT 1;
		SELECT `COLUMN_NAME`
			INTO PRIMARY_KEY_COLUMN_NAME
			FROM `information_schema`.`COLUMNS`
			WHERE `TABLE_SCHEMA` = (SELECT IFNULL(SCHEMA_NAME_ARGUMENT, SCHEMA()))
				AND `TABLE_NAME` = TABLE_NAME_ARGUMENT
				AND `COLUMN_KEY` = 'PRI'
			LIMIT 1;
		SET SQL_EXP = CONCAT('ALTER TABLE `', (SELECT IFNULL(SCHEMA_NAME_ARGUMENT, SCHEMA())), '`.`', TABLE_NAME_ARGUMENT, '` MODIFY COLUMN `', PRIMARY_KEY_COLUMN_NAME, '` ', PRIMARY_KEY_TYPE, ' NOT NULL;');
		SET @SQL_EXP = SQL_EXP;
		PREPARE SQL_EXP_EXECUTE FROM @SQL_EXP;
		EXECUTE SQL_EXP_EXECUTE;
		DEALLOCATE PREPARE SQL_EXP_EXECUTE;
	END IF;
END 
 ;
DROP PROCEDURE IF EXISTS `POMELO_AFTER_ADD_PRIMARY_KEY`;
 
CREATE PROCEDURE `POMELO_AFTER_ADD_PRIMARY_KEY`(IN `SCHEMA_NAME_ARGUMENT` VARCHAR(255), IN `TABLE_NAME_ARGUMENT` VARCHAR(255), IN `COLUMN_NAME_ARGUMENT` VARCHAR(255))
BEGIN
	DECLARE HAS_AUTO_INCREMENT_ID INT(11);
	DECLARE PRIMARY_KEY_COLUMN_NAME VARCHAR(255);
	DECLARE PRIMARY_KEY_TYPE VARCHAR(255);
	DECLARE SQL_EXP VARCHAR(1000);
	SELECT COUNT(*)
		INTO HAS_AUTO_INCREMENT_ID
		FROM `information_schema`.`COLUMNS`
		WHERE `TABLE_SCHEMA` = (SELECT IFNULL(SCHEMA_NAME_ARGUMENT, SCHEMA()))
			AND `TABLE_NAME` = TABLE_NAME_ARGUMENT
			AND `COLUMN_NAME` = COLUMN_NAME_ARGUMENT
			AND `COLUMN_TYPE` LIKE '%int%'
			AND `COLUMN_KEY` = 'PRI';
	IF HAS_AUTO_INCREMENT_ID THEN
		SELECT `COLUMN_TYPE`
			INTO PRIMARY_KEY_TYPE
			FROM `information_schema`.`COLUMNS`
			WHERE `TABLE_SCHEMA` = (SELECT IFNULL(SCHEMA_NAME_ARGUMENT, SCHEMA()))
				AND `TABLE_NAME` = TABLE_NAME_ARGUMENT
				AND `COLUMN_NAME` = COLUMN_NAME_ARGUMENT
				AND `COLUMN_TYPE` LIKE '%int%'
				AND `COLUMN_KEY` = 'PRI';
		SELECT `COLUMN_NAME`
			INTO PRIMARY_KEY_COLUMN_NAME
			FROM `information_schema`.`COLUMNS`
			WHERE `TABLE_SCHEMA` = (SELECT IFNULL(SCHEMA_NAME_ARGUMENT, SCHEMA()))
				AND `TABLE_NAME` = TABLE_NAME_ARGUMENT
				AND `COLUMN_NAME` = COLUMN_NAME_ARGUMENT
				AND `COLUMN_TYPE` LIKE '%int%'
				AND `COLUMN_KEY` = 'PRI';
		SET SQL_EXP = CONCAT('ALTER TABLE `', (SELECT IFNULL(SCHEMA_NAME_ARGUMENT, SCHEMA())), '`.`', TABLE_NAME_ARGUMENT, '` MODIFY COLUMN `', PRIMARY_KEY_COLUMN_NAME, '` ', PRIMARY_KEY_TYPE, ' NOT NULL AUTO_INCREMENT;');
		SET @SQL_EXP = SQL_EXP;
		PREPARE SQL_EXP_EXECUTE FROM @SQL_EXP;
		EXECUTE SQL_EXP_EXECUTE;
		DEALLOCATE PREPARE SQL_EXP_EXECUTE;
	END IF;
END 
 ;
CREATE TABLE IF NOT EXISTS `__EFMigrationsHistory` (
    `MigrationId` varchar(150) CHARACTER SET utf8mb4 NOT NULL,
    `ProductVersion` varchar(32) CHARACTER SET utf8mb4 NOT NULL,
    CONSTRAINT `PK___EFMigrationsHistory` PRIMARY KEY (`MigrationId`)
) CHARACTER SET=utf8mb4;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20190805123858_init') THEN
    CREATE TABLE `AppSettings` (
        `Key` varchar(255) NOT NULL,
        `Value` longtext NULL,
        CONSTRAINT `PK_AppSettings` PRIMARY KEY (`Key`)
    );
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20190805123858_init') THEN
    CREATE TABLE `AspNetRoles` (
        `Id` varchar(255) NOT NULL,
        `Name` varchar(256) NULL,
        `NormalizedName` varchar(256) NULL,
        `ConcurrencyStamp` longtext NULL,
        CONSTRAINT `PK_AspNetRoles` PRIMARY KEY (`Id`)
    );
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20190805123858_init') THEN
    CREATE TABLE `AspNetUsers` (
        `Id` varchar(255) NOT NULL,
        `UserName` varchar(256) NULL,
        `NormalizedUserName` varchar(256) NULL,
        `Email` varchar(256) NULL,
        `NormalizedEmail` varchar(256) NULL,
        `EmailConfirmed` tinyint(1) NOT NULL,
        `PasswordHash` longtext NULL,
        `SecurityStamp` longtext NULL,
        `ConcurrencyStamp` longtext NULL,
        `PhoneNumber` longtext NULL,
        `PhoneNumberConfirmed` tinyint(1) NOT NULL,
        `TwoFactorEnabled` tinyint(1) NOT NULL,
        `LockoutEnd` datetime(6) NULL,
        `LockoutEnabled` tinyint(1) NOT NULL,
        `AccessFailedCount` int NOT NULL,
        CONSTRAINT `PK_AspNetUsers` PRIMARY KEY (`Id`)
    );
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20190805123858_init') THEN
    CREATE TABLE `Companies` (
        `Id` varchar(255) NOT NULL,
        `Name` longtext NOT NULL,
        CONSTRAINT `PK_Companies` PRIMARY KEY (`Id`)
    );
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20190805123858_init') THEN
    CREATE TABLE `Notifications` (
        `Id` varchar(255) NOT NULL,
        `NotifyId` int NOT NULL,
        `CreatedAt` datetime(6) NOT NULL,
        `Message` longtext NULL,
        `Url` longtext NULL,
        CONSTRAINT `PK_Notifications` PRIMARY KEY (`Id`)
    );
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20190805123858_init') THEN
    CREATE TABLE `Positions` (
        `Id` varchar(255) NOT NULL,
        `Name` longtext NOT NULL,
        CONSTRAINT `PK_Positions` PRIMARY KEY (`Id`)
    );
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20190805123858_init') THEN
    CREATE TABLE `SharedAccounts` (
        `Id` varchar(255) NOT NULL,
        `Name` longtext NOT NULL,
        `Urls` longtext NULL,
        `Apps` longtext NULL,
        `Login` longtext NOT NULL,
        `Password` longtext NULL,
        `PasswordChangedAt` datetime(6) NULL,
        `OtpSecret` longtext NULL,
        `OtpSecretChangedAt` datetime(6) NULL,
        `Deleted` tinyint(1) NOT NULL,
        CONSTRAINT `PK_SharedAccounts` PRIMARY KEY (`Id`)
    );
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20190805123858_init') THEN
    CREATE TABLE `Templates` (
        `Id` varchar(255) NOT NULL,
        `Name` longtext NOT NULL,
        `Urls` longtext NULL,
        `Apps` longtext NULL,
        `Deleted` tinyint(1) NOT NULL,
        CONSTRAINT `PK_Templates` PRIMARY KEY (`Id`)
    );
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20190805123858_init') THEN
    CREATE TABLE `AspNetRoleClaims` (
        `Id` int NOT NULL AUTO_INCREMENT,
        `RoleId` varchar(255) NOT NULL,
        `ClaimType` longtext NULL,
        `ClaimValue` longtext NULL,
        CONSTRAINT `PK_AspNetRoleClaims` PRIMARY KEY (`Id`),
        CONSTRAINT `FK_AspNetRoleClaims_AspNetRoles_RoleId` FOREIGN KEY (`RoleId`) REFERENCES `AspNetRoles` (`Id`) ON DELETE CASCADE
    );
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20190805123858_init') THEN
    CREATE TABLE `AspNetUserClaims` (
        `Id` int NOT NULL AUTO_INCREMENT,
        `UserId` varchar(255) NOT NULL,
        `ClaimType` longtext NULL,
        `ClaimValue` longtext NULL,
        CONSTRAINT `PK_AspNetUserClaims` PRIMARY KEY (`Id`),
        CONSTRAINT `FK_AspNetUserClaims_AspNetUsers_UserId` FOREIGN KEY (`UserId`) REFERENCES `AspNetUsers` (`Id`) ON DELETE CASCADE
    );
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20190805123858_init') THEN
    CREATE TABLE `AspNetUserLogins` (
        `LoginProvider` varchar(255) NOT NULL,
        `ProviderKey` varchar(255) NOT NULL,
        `ProviderDisplayName` longtext NULL,
        `UserId` varchar(255) 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;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20190805123858_init') THEN
    CREATE TABLE `AspNetUserRoles` (
        `UserId` varchar(255) NOT NULL,
        `RoleId` varchar(255) 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;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20190805123858_init') THEN
    CREATE TABLE `AspNetUserTokens` (
        `UserId` varchar(255) NOT NULL,
        `LoginProvider` varchar(255) NOT NULL,
        `Name` varchar(255) NOT NULL,
        `Value` longtext 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;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20190805123858_init') THEN
    CREATE TABLE `Departments` (
        `Id` varchar(255) NOT NULL,
        `CompanyId` varchar(255) NULL,
        `Name` longtext NOT NULL,
        CONSTRAINT `PK_Departments` PRIMARY KEY (`Id`),
        CONSTRAINT `FK_Departments_Companies_CompanyId` FOREIGN KEY (`CompanyId`) REFERENCES `Companies` (`Id`) ON DELETE RESTRICT
    );
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20190805123858_init') THEN
    CREATE TABLE `Employees` (
        `Id` varchar(255) NOT NULL,
        `FirstName` longtext NOT NULL,
        `LastName` longtext NOT NULL,
        `Email` longtext NOT NULL,
        `PhoneNumber` longtext NULL,
        `DepartmentId` varchar(255) NOT NULL,
        `PositionId` varchar(255) NOT NULL,
        `LastSeen` datetime(6) NULL,
        CONSTRAINT `PK_Employees` PRIMARY KEY (`Id`),
        CONSTRAINT `FK_Employees_Departments_DepartmentId` FOREIGN KEY (`DepartmentId`) REFERENCES `Departments` (`Id`) ON DELETE CASCADE,
        CONSTRAINT `FK_Employees_Positions_PositionId` FOREIGN KEY (`PositionId`) REFERENCES `Positions` (`Id`) ON DELETE CASCADE
    );
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20190805123858_init') THEN
    CREATE TABLE `Workstations` (
        `Id` varchar(255) NOT NULL,
        `Name` longtext NULL,
        `Domain` longtext NULL,
        `ClientVersion` longtext NULL,
        `DepartmentId` varchar(255) NULL,
        `OS` longtext NULL,
        `IP` longtext NULL,
        `LastSeen` datetime(6) NOT NULL,
        `Approved` tinyint(1) NOT NULL,
        `LockProximity` int NOT NULL,
        `UnlockProximity` int NOT NULL,
        `LockTimeout` int NOT NULL,
        CONSTRAINT `PK_Workstations` PRIMARY KEY (`Id`),
        CONSTRAINT `FK_Workstations_Departments_DepartmentId` FOREIGN KEY (`DepartmentId`) REFERENCES `Departments` (`Id`) ON DELETE RESTRICT
    );
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20190805123858_init') THEN
    CREATE TABLE `Devices` (
        `Id` varchar(255) NOT NULL,
        `MAC` longtext NULL,
        `Model` longtext NULL,
        `RFID` longtext NULL,
        `Battery` int NOT NULL,
        `Firmware` longtext NULL,
        `LastSynced` datetime(6) NULL,
        `EmployeeId` varchar(255) NULL,
        `PrimaryAccountId` longtext NULL,
        `MasterPassword` longtext NULL,
        `ImportedAt` datetime(6) NOT NULL,
        `UsePin` tinyint(1) NOT NULL,
        CONSTRAINT `PK_Devices` PRIMARY KEY (`Id`),
        CONSTRAINT `FK_Devices_Employees_EmployeeId` FOREIGN KEY (`EmployeeId`) REFERENCES `Employees` (`Id`) ON DELETE RESTRICT
    );
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20190805123858_init') THEN
    CREATE TABLE `DeviceAccounts` (
        `Id` varchar(255) NOT NULL,
        `Name` longtext NOT NULL,
        `Urls` longtext NULL,
        `Apps` longtext NULL,
        `Login` longtext NOT NULL,
        `Type` int NOT NULL,
        `Status` int NOT NULL,
        `LastSyncedAt` datetime(6) NULL,
        `CreatedAt` datetime(6) NOT NULL,
        `UpdatedAt` datetime(6) NULL,
        `PasswordUpdatedAt` datetime(6) NOT NULL,
        `OtpUpdatedAt` datetime(6) NULL,
        `Deleted` tinyint(1) NOT NULL,
        `IdFromDevice` smallint unsigned NOT NULL,
        `EmployeeId` varchar(255) NULL,
        `DeviceId` varchar(255) NULL,
        `SharedAccountId` varchar(255) NULL,
        CONSTRAINT `PK_DeviceAccounts` PRIMARY KEY (`Id`),
        CONSTRAINT `FK_DeviceAccounts_Devices_DeviceId` FOREIGN KEY (`DeviceId`) REFERENCES `Devices` (`Id`) ON DELETE RESTRICT,
        CONSTRAINT `FK_DeviceAccounts_Employees_EmployeeId` FOREIGN KEY (`EmployeeId`) REFERENCES `Employees` (`Id`) ON DELETE RESTRICT,
        CONSTRAINT `FK_DeviceAccounts_SharedAccounts_SharedAccountId` FOREIGN KEY (`SharedAccountId`) REFERENCES `SharedAccounts` (`Id`) ON DELETE RESTRICT
    );
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20190805123858_init') THEN
    CREATE TABLE `WorkstationBindings` (
        `Id` varchar(255) NOT NULL,
        `DeviceId` varchar(255) NULL,
        `WorkstationId` varchar(255) NULL,
        `AllowRfid` tinyint(1) NOT NULL,
        `AllowBleTap` tinyint(1) NOT NULL,
        `AllowProximity` tinyint(1) NOT NULL,
        CONSTRAINT `PK_WorkstationBindings` PRIMARY KEY (`Id`),
        CONSTRAINT `FK_WorkstationBindings_Devices_DeviceId` FOREIGN KEY (`DeviceId`) REFERENCES `Devices` (`Id`) ON DELETE RESTRICT,
        CONSTRAINT `FK_WorkstationBindings_Workstations_WorkstationId` FOREIGN KEY (`WorkstationId`) REFERENCES `Workstations` (`Id`) ON DELETE RESTRICT
    );
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20190805123858_init') THEN
    CREATE TABLE `DeviceTasks` (
        `Id` varchar(255) NOT NULL,
        `Name` longtext NULL,
        `Urls` longtext NULL,
        `Apps` longtext NULL,
        `Login` longtext NULL,
        `Password` longtext NULL,
        `OtpSecret` longtext NULL,
        `Operation` int NOT NULL,
        `CreatedAt` datetime(6) NOT NULL,
        `DeviceId` longtext NULL,
        `DeviceAccountId` varchar(255) NULL,
        CONSTRAINT `PK_DeviceTasks` PRIMARY KEY (`Id`),
        CONSTRAINT `FK_DeviceTasks_DeviceAccounts_DeviceAccountId` FOREIGN KEY (`DeviceAccountId`) REFERENCES `DeviceAccounts` (`Id`) ON DELETE RESTRICT
    );
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20190805123858_init') THEN
    CREATE TABLE `WorkstationEvents` (
        `Id` varchar(255) NOT NULL,
        `Date` datetime(6) NOT NULL,
        `EventId` int NOT NULL,
        `SeverityId` int NOT NULL,
        `Note` longtext NULL,
        `WorkstationId` varchar(255) NULL,
        `UserSession` longtext NULL,
        `DeviceId` varchar(255) NULL,
        `EmployeeId` varchar(255) NULL,
        `DepartmentId` varchar(255) NULL,
        `DeviceAccountId` varchar(255) NULL,
        CONSTRAINT `PK_WorkstationEvents` PRIMARY KEY (`Id`),
        CONSTRAINT `FK_WorkstationEvents_Departments_DepartmentId` FOREIGN KEY (`DepartmentId`) REFERENCES `Departments` (`Id`) ON DELETE RESTRICT,
        CONSTRAINT `FK_WorkstationEvents_DeviceAccounts_DeviceAccountId` FOREIGN KEY (`DeviceAccountId`) REFERENCES `DeviceAccounts` (`Id`) ON DELETE RESTRICT,
        CONSTRAINT `FK_WorkstationEvents_Devices_DeviceId` FOREIGN KEY (`DeviceId`) REFERENCES `Devices` (`Id`) ON DELETE RESTRICT,
        CONSTRAINT `FK_WorkstationEvents_Employees_EmployeeId` FOREIGN KEY (`EmployeeId`) REFERENCES `Employees` (`Id`) ON DELETE RESTRICT,
        CONSTRAINT `FK_WorkstationEvents_Workstations_WorkstationId` FOREIGN KEY (`WorkstationId`) REFERENCES `Workstations` (`Id`) ON DELETE RESTRICT
    );
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20190805123858_init') THEN
    CREATE TABLE `WorkstationSessions` (
        `Id` varchar(255) NOT NULL,
        `StartDate` datetime(6) NOT NULL,
        `EndDate` datetime(6) NULL,
        `UnlockedBy` int NOT NULL,
        `WorkstationId` varchar(255) NULL,
        `UserSession` longtext NULL,
        `DeviceId` varchar(255) NULL,
        `EmployeeId` varchar(255) NULL,
        `DepartmentId` varchar(255) NULL,
        `DeviceAccountId` varchar(255) NULL,
        CONSTRAINT `PK_WorkstationSessions` PRIMARY KEY (`Id`),
        CONSTRAINT `FK_WorkstationSessions_Departments_DepartmentId` FOREIGN KEY (`DepartmentId`) REFERENCES `Departments` (`Id`) ON DELETE RESTRICT,
        CONSTRAINT `FK_WorkstationSessions_DeviceAccounts_DeviceAccountId` FOREIGN KEY (`DeviceAccountId`) REFERENCES `DeviceAccounts` (`Id`) ON DELETE RESTRICT,
        CONSTRAINT `FK_WorkstationSessions_Devices_DeviceId` FOREIGN KEY (`DeviceId`) REFERENCES `Devices` (`Id`) ON DELETE RESTRICT,
        CONSTRAINT `FK_WorkstationSessions_Employees_EmployeeId` FOREIGN KEY (`EmployeeId`) REFERENCES `Employees` (`Id`) ON DELETE RESTRICT,
        CONSTRAINT `FK_WorkstationSessions_Workstations_WorkstationId` FOREIGN KEY (`WorkstationId`) REFERENCES `Workstations` (`Id`) ON DELETE RESTRICT
    );
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20190805123858_init') THEN
    CREATE INDEX `IX_AspNetRoleClaims_RoleId` ON `AspNetRoleClaims` (`RoleId`);
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20190805123858_init') THEN
    CREATE UNIQUE INDEX `RoleNameIndex` ON `AspNetRoles` (`NormalizedName`);
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20190805123858_init') THEN
    CREATE INDEX `IX_AspNetUserClaims_UserId` ON `AspNetUserClaims` (`UserId`);
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20190805123858_init') THEN
    CREATE INDEX `IX_AspNetUserLogins_UserId` ON `AspNetUserLogins` (`UserId`);
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20190805123858_init') THEN
    CREATE INDEX `IX_AspNetUserRoles_RoleId` ON `AspNetUserRoles` (`RoleId`);
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20190805123858_init') THEN
    CREATE INDEX `EmailIndex` ON `AspNetUsers` (`NormalizedEmail`);
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20190805123858_init') THEN
    CREATE UNIQUE INDEX `UserNameIndex` ON `AspNetUsers` (`NormalizedUserName`);
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20190805123858_init') THEN
    CREATE INDEX `IX_Departments_CompanyId` ON `Departments` (`CompanyId`);
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20190805123858_init') THEN
    CREATE INDEX `IX_DeviceAccounts_DeviceId` ON `DeviceAccounts` (`DeviceId`);
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20190805123858_init') THEN
    CREATE INDEX `IX_DeviceAccounts_EmployeeId` ON `DeviceAccounts` (`EmployeeId`);
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20190805123858_init') THEN
    CREATE INDEX `IX_DeviceAccounts_SharedAccountId` ON `DeviceAccounts` (`SharedAccountId`);
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20190805123858_init') THEN
    CREATE INDEX `IX_Devices_EmployeeId` ON `Devices` (`EmployeeId`);
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20190805123858_init') THEN
    CREATE INDEX `IX_DeviceTasks_DeviceAccountId` ON `DeviceTasks` (`DeviceAccountId`);
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20190805123858_init') THEN
    CREATE INDEX `IX_Employees_DepartmentId` ON `Employees` (`DepartmentId`);
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20190805123858_init') THEN
    CREATE INDEX `IX_Employees_PositionId` ON `Employees` (`PositionId`);
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20190805123858_init') THEN
    CREATE INDEX `IX_WorkstationBindings_DeviceId` ON `WorkstationBindings` (`DeviceId`);
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20190805123858_init') THEN
    CREATE INDEX `IX_WorkstationBindings_WorkstationId` ON `WorkstationBindings` (`WorkstationId`);
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20190805123858_init') THEN
    CREATE INDEX `IX_WorkstationEvents_DepartmentId` ON `WorkstationEvents` (`DepartmentId`);
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20190805123858_init') THEN
    CREATE INDEX `IX_WorkstationEvents_DeviceAccountId` ON `WorkstationEvents` (`DeviceAccountId`);
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20190805123858_init') THEN
    CREATE INDEX `IX_WorkstationEvents_DeviceId` ON `WorkstationEvents` (`DeviceId`);
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20190805123858_init') THEN
    CREATE INDEX `IX_WorkstationEvents_EmployeeId` ON `WorkstationEvents` (`EmployeeId`);
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20190805123858_init') THEN
    CREATE INDEX `IX_WorkstationEvents_WorkstationId` ON `WorkstationEvents` (`WorkstationId`);
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20190805123858_init') THEN
    CREATE INDEX `IX_Workstations_DepartmentId` ON `Workstations` (`DepartmentId`);
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20190805123858_init') THEN
    CREATE INDEX `IX_WorkstationSessions_DepartmentId` ON `WorkstationSessions` (`DepartmentId`);
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20190805123858_init') THEN
    CREATE INDEX `IX_WorkstationSessions_DeviceAccountId` ON `WorkstationSessions` (`DeviceAccountId`);
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20190805123858_init') THEN
    CREATE INDEX `IX_WorkstationSessions_DeviceId` ON `WorkstationSessions` (`DeviceId`);
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20190805123858_init') THEN
    CREATE INDEX `IX_WorkstationSessions_EmployeeId` ON `WorkstationSessions` (`EmployeeId`);
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20190805123858_init') THEN
    CREATE INDEX `IX_WorkstationSessions_WorkstationId` ON `WorkstationSessions` (`WorkstationId`);
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20190805123858_init') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20190805123858_init', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20190806115356_added_deviceAccessProfiles_table') THEN
    ALTER TABLE `Devices` ADD `DeviceAccessProfileId` varchar(255) NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20190806115356_added_deviceAccessProfiles_table') THEN
    CREATE TABLE `DeviceAccessProfiles` (
        `Id` varchar(255) NOT NULL,
        `Name` longtext NOT NULL,
        `CreatedAt` datetime(6) NOT NULL,
        `UpdatedAt` datetime(6) NULL,
        `ButtonBonding` tinyint(1) NOT NULL,
        `ButtonConnection` tinyint(1) NOT NULL,
        `ButtonNewChannel` tinyint(1) NOT NULL,
        `ButtonNewLink` tinyint(1) NOT NULL,
        `PinBonding` tinyint(1) NOT NULL,
        `PinConnection` tinyint(1) NOT NULL,
        `PinNewChannel` tinyint(1) NOT NULL,
        `PinNewLink` tinyint(1) NOT NULL,
        `MasterKeyBonding` tinyint(1) NOT NULL,
        `MasterKeyConnection` tinyint(1) NOT NULL,
        `MasterKeyNewChannel` tinyint(1) NOT NULL,
        `MasterKeyNewLink` tinyint(1) NOT NULL,
        `PinExpiration` int NOT NULL,
        `PinLength` int NOT NULL,
        `PinTryCount` int NOT NULL,
        `ButtonExpirationTimeout` int NOT NULL,
        CONSTRAINT `PK_DeviceAccessProfiles` PRIMARY KEY (`Id`)
    );
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20190806115356_added_deviceAccessProfiles_table') THEN
    CREATE INDEX `IX_Devices_DeviceAccessProfileId` ON `Devices` (`DeviceAccessProfileId`);
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20190806115356_added_deviceAccessProfiles_table') THEN
    ALTER TABLE `Devices` ADD CONSTRAINT `FK_Devices_DeviceAccessProfiles_DeviceAccessProfileId` FOREIGN KEY (`DeviceAccessProfileId`) REFERENCES `DeviceAccessProfiles` (`Id`) ON DELETE RESTRICT;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20190806115356_added_deviceAccessProfiles_table') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20190806115356_added_deviceAccessProfiles_table', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20190809121132_added_acceessProfileId_field') THEN
    ALTER TABLE `Devices` DROP FOREIGN KEY `FK_Devices_DeviceAccessProfiles_DeviceAccessProfileId`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20190809121132_added_acceessProfileId_field') THEN
    ALTER TABLE `Devices` RENAME COLUMN `DeviceAccessProfileId` TO `AcceessProfileId`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20190809121132_added_acceessProfileId_field') THEN
    ALTER TABLE `Devices` RENAME INDEX `IX_Devices_DeviceAccessProfileId` TO `IX_Devices_AcceessProfileId`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20190809121132_added_acceessProfileId_field') THEN
    ALTER TABLE `Devices` ADD CONSTRAINT `FK_Devices_DeviceAccessProfiles_AcceessProfileId` FOREIGN KEY (`AcceessProfileId`) REFERENCES `DeviceAccessProfiles` (`Id`) ON DELETE RESTRICT;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20190809121132_added_acceessProfileId_field') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20190809121132_added_acceessProfileId_field', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20190812120659_added_deviceState_field') THEN
    ALTER TABLE `Devices` ADD `State` int NOT NULL DEFAULT 0;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20190812120659_added_deviceState_field') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20190812120659_added_deviceState_field', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20190814132831_added_masterKeyExpiration_field') THEN
    ALTER TABLE `DeviceAccessProfiles` RENAME COLUMN `ButtonExpirationTimeout` TO `MasterKeyExpiration`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20190814132831_added_masterKeyExpiration_field') THEN
    ALTER TABLE `DeviceAccessProfiles` ADD `ButtonExpiration` int NOT NULL DEFAULT 0;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20190814132831_added_masterKeyExpiration_field') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20190814132831_added_masterKeyExpiration_field', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20190904055348_rebase_ws_bindings_to_ws_proximity') THEN
    DROP TABLE `WorkstationBindings`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20190904055348_rebase_ws_bindings_to_ws_proximity') THEN
    CREATE TABLE `WorkstationProximityDevices` (
        `Id` varchar(255) NOT NULL,
        `DeviceId` varchar(255) NULL,
        `WorkstationId` varchar(255) NULL,
        `LockProximity` int NOT NULL,
        `UnlockProximity` int NOT NULL,
        `LockTimeout` int NOT NULL,
        CONSTRAINT `PK_WorkstationProximityDevices` PRIMARY KEY (`Id`),
        CONSTRAINT `FK_WorkstationProximityDevices_Devices_DeviceId` FOREIGN KEY (`DeviceId`) REFERENCES `Devices` (`Id`) ON DELETE RESTRICT,
        CONSTRAINT `FK_WorkstationProximityDevices_Workstations_WorkstationId` FOREIGN KEY (`WorkstationId`) REFERENCES `Workstations` (`Id`) ON DELETE RESTRICT
    );
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20190904055348_rebase_ws_bindings_to_ws_proximity') THEN
    CREATE INDEX `IX_WorkstationProximityDevices_DeviceId` ON `WorkstationProximityDevices` (`DeviceId`);
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20190904055348_rebase_ws_bindings_to_ws_proximity') THEN
    CREATE INDEX `IX_WorkstationProximityDevices_WorkstationId` ON `WorkstationProximityDevices` (`WorkstationId`);
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20190904055348_rebase_ws_bindings_to_ws_proximity') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20190904055348_rebase_ws_bindings_to_ws_proximity', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20190904092141_update_workstation_table') THEN
    ALTER TABLE `Workstations` DROP COLUMN `LockProximity`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20190904092141_update_workstation_table') THEN
    ALTER TABLE `Workstations` DROP COLUMN `LockTimeout`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20190904092141_update_workstation_table') THEN
    ALTER TABLE `Workstations` DROP COLUMN `UnlockProximity`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20190904092141_update_workstation_table') THEN
    ALTER TABLE `Workstations` ADD `RFID` tinyint(1) NOT NULL DEFAULT FALSE;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20190904092141_update_workstation_table') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20190904092141_update_workstation_table', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20190910142724_added_identity_provider_table') THEN
    ALTER TABLE `Departments` DROP FOREIGN KEY `FK_Departments_Companies_CompanyId`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20190910142724_added_identity_provider_table') THEN
    ALTER TABLE `Departments` MODIFY COLUMN `CompanyId` varchar(255) NOT NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20190910142724_added_identity_provider_table') THEN
    CREATE TABLE `IdentityProvider` (
        `Id` varchar(255) NOT NULL,
        `Enabled` tinyint(1) NOT NULL,
        `Url` longtext NULL,
        CONSTRAINT `PK_IdentityProvider` PRIMARY KEY (`Id`)
    );
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20190910142724_added_identity_provider_table') THEN
    ALTER TABLE `Departments` ADD CONSTRAINT `FK_Departments_Companies_CompanyId` FOREIGN KEY (`CompanyId`) REFERENCES `Companies` (`Id`) ON DELETE CASCADE;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20190910142724_added_identity_provider_table') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20190910142724_added_identity_provider_table', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20190910145249_rename_identity_provider_table') THEN
    DROP TABLE `IdentityProvider`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20190910145249_rename_identity_provider_table') THEN
    CREATE TABLE `SamlIdentityProvider` (
        `Id` varchar(255) NOT NULL,
        `Enabled` tinyint(1) NOT NULL,
        `Url` longtext NULL,
        CONSTRAINT `PK_SamlIdentityProvider` PRIMARY KEY (`Id`)
    );
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20190910145249_rename_identity_provider_table') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20190910145249_rename_identity_provider_table', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20190911083844_added_samlidp_field') THEN
    ALTER TABLE `Employees` ADD `SamlIdp` longtext NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20190911083844_added_samlidp_field') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20190911083844_added_samlidp_field', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20190912134003_rename_field_samlidp') THEN
    ALTER TABLE `Employees` RENAME COLUMN `SamlIdp` TO `SamlIdpDevice`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20190912134003_rename_field_samlidp') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20190912134003_rename_field_samlidp', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20190916092015_added_fields_aspnetusers') THEN
    ALTER TABLE `AspNetUsers` ADD `FirstName` longtext NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20190916092015_added_fields_aspnetusers') THEN
    ALTER TABLE `AspNetUsers` ADD `LastName` longtext NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20190916092015_added_fields_aspnetusers') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20190916092015_added_fields_aspnetusers', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20190918090743_udapte_idp_url_field') THEN
    ALTER TABLE `SamlIdentityProvider` MODIFY COLUMN `Url` longtext NOT NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20190918090743_udapte_idp_url_field') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20190918090743_udapte_idp_url_field', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20190919142145_added_data_protection') THEN
    CREATE TABLE `DataProtection` (
        `Id` varchar(255) NOT NULL,
        `Value` longtext NULL,
        CONSTRAINT `PK_DataProtection` PRIMARY KEY (`Id`)
    );
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20190919142145_added_data_protection') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20190919142145_added_data_protection', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20190919145102_remove_appsettings') THEN
    DROP TABLE `AppSettings`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20190919145102_remove_appsettings') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20190919145102_remove_appsettings', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20190920054930_update_notify') THEN
    ALTER TABLE `Notifications` DROP COLUMN `NotifyId`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20190920054930_update_notify') THEN
    ALTER TABLE `Notifications` ADD `Type` int NOT NULL DEFAULT 0;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20190920054930_update_notify') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20190920054930_update_notify', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20190920130217_idp_update') THEN
    ALTER TABLE `Employees` DROP COLUMN `SamlIdpDevice`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20190920130217_idp_update') THEN
    ALTER TABLE `AspNetUsers` ADD `DeviceId` longtext NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20190920130217_idp_update') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20190920130217_idp_update', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20191021085306_added_acc_kind') THEN
    ALTER TABLE `SharedAccounts` ADD `Kind` int NOT NULL DEFAULT 0;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20191021085306_added_acc_kind') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20191021085306_added_acc_kind', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20191021145050_added_device_acc_kind') THEN
    ALTER TABLE `DeviceAccounts` ADD `Kind` int NOT NULL DEFAULT 0;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20191021145050_added_device_acc_kind') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20191021145050_added_device_acc_kind', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20191024084055_modify_device_access_profile') THEN
    ALTER TABLE `DeviceAccessProfiles` DROP COLUMN `ButtonExpiration`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20191024084055_modify_device_access_profile') THEN
    ALTER TABLE `DeviceAccessProfiles` DROP COLUMN `ButtonNewLink`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20191024084055_modify_device_access_profile') THEN
    ALTER TABLE `DeviceAccessProfiles` DROP COLUMN `MasterKeyExpiration`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20191024084055_modify_device_access_profile') THEN
    ALTER TABLE `DeviceAccessProfiles` DROP COLUMN `MasterKeyNewLink`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20191024084055_modify_device_access_profile') THEN
    ALTER TABLE `DeviceAccessProfiles` DROP COLUMN `PinNewLink`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20191024084055_modify_device_access_profile') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20191024084055_modify_device_access_profile', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20191101094636_remove_usepin_field') THEN
    ALTER TABLE `Devices` DROP COLUMN `UsePin`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20191101094636_remove_usepin_field') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20191101094636_remove_usepin_field', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20191104075710_remove_notification') THEN
    DROP TABLE `Notifications`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20191104075710_remove_notification') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20191104075710_remove_notification', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20191104081917_rename_workstation_proximity') THEN
    DROP TABLE `WorkstationProximityDevices`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20191104081917_rename_workstation_proximity') THEN
    CREATE TABLE `ProximityDevices` (
        `Id` varchar(255) NOT NULL,
        `DeviceId` varchar(255) NULL,
        `WorkstationId` varchar(255) NULL,
        `LockProximity` int NOT NULL,
        `UnlockProximity` int NOT NULL,
        `LockTimeout` int NOT NULL,
        CONSTRAINT `PK_ProximityDevices` PRIMARY KEY (`Id`),
        CONSTRAINT `FK_ProximityDevices_Devices_DeviceId` FOREIGN KEY (`DeviceId`) REFERENCES `Devices` (`Id`) ON DELETE RESTRICT,
        CONSTRAINT `FK_ProximityDevices_Workstations_WorkstationId` FOREIGN KEY (`WorkstationId`) REFERENCES `Workstations` (`Id`) ON DELETE RESTRICT
    );
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20191104081917_rename_workstation_proximity') THEN
    CREATE INDEX `IX_ProximityDevices_DeviceId` ON `ProximityDevices` (`DeviceId`);
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20191104081917_rename_workstation_proximity') THEN
    CREATE INDEX `IX_ProximityDevices_WorkstationId` ON `ProximityDevices` (`WorkstationId`);
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20191104081917_rename_workstation_proximity') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20191104081917_rename_workstation_proximity', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20191127084940_rename_devicetask_fields') THEN
    ALTER TABLE `DeviceTasks` RENAME COLUMN `Urls` TO `OldUrls`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20191127084940_rename_devicetask_fields') THEN
    ALTER TABLE `DeviceTasks` RENAME COLUMN `Name` TO `OldName`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20191127084940_rename_devicetask_fields') THEN
    ALTER TABLE `DeviceTasks` RENAME COLUMN `Login` TO `OldLogin`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20191127084940_rename_devicetask_fields') THEN
    ALTER TABLE `DeviceTasks` RENAME COLUMN `Apps` TO `OldApps`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20191127084940_rename_devicetask_fields') THEN
    ALTER TABLE `Devices` MODIFY COLUMN `RFID` longtext NOT NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20191127084940_rename_devicetask_fields') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20191127084940_rename_devicetask_fields', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20191202130106_added_valuehash_field') THEN
    ALTER TABLE `DataProtection` ADD `ValueHash` longtext NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20191202130106_added_valuehash_field') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20191202130106_added_valuehash_field', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20191210140809_added_device_license') THEN
    CREATE TABLE `DeviceLicenses` (
        `Id` varchar(255) NOT NULL,
        `LicenseOrderId` longtext NOT NULL,
        `DeviceId` longtext NOT NULL,
        `CreatedAt` datetime(6) NOT NULL,
        `ImportedAt` datetime(6) NOT NULL,
        `LicenseEndDate` datetime(6) NOT NULL,
        `Capabilities` int NOT NULL,
        `AppliedAt` datetime(6) NOT NULL,
        `Data` longblob NOT NULL,
        CONSTRAINT `PK_DeviceLicenses` PRIMARY KEY (`Id`)
    );
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20191210140809_added_device_license') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20191210140809_added_device_license', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20191211073109_added_hasnewlicense_field') THEN
    ALTER TABLE `Devices` ADD `HasNewLicense` tinyint(1) NOT NULL DEFAULT FALSE;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20191211073109_added_hasnewlicense_field') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20191211073109_added_hasnewlicense_field', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20191211074606_update_devicelicense') THEN
    ALTER TABLE `DeviceLicenses` MODIFY COLUMN `LicenseOrderId` longtext NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20191211074606_update_devicelicense') THEN
    ALTER TABLE `DeviceLicenses` MODIFY COLUMN `DeviceId` longtext NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20191211074606_update_devicelicense') THEN
    ALTER TABLE `DeviceLicenses` MODIFY COLUMN `Data` longblob NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20191211074606_update_devicelicense') THEN
    ALTER TABLE `DeviceLicenses` MODIFY COLUMN `AppliedAt` datetime(6) NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20191211074606_update_devicelicense') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20191211074606_update_devicelicense', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20191213073708_added_licenses_tables') THEN
    ALTER TABLE `DeviceLicenses` DROP COLUMN `Capabilities`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20191213073708_added_licenses_tables') THEN
    ALTER TABLE `DeviceLicenses` DROP COLUMN `CreatedAt`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20191213073708_added_licenses_tables') THEN
    ALTER TABLE `DeviceLicenses` DROP COLUMN `LicenseEndDate`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20191213073708_added_licenses_tables') THEN
    ALTER TABLE `Devices` ADD `LicenseEndDate` datetime(6) NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20191213073708_added_licenses_tables') THEN
    ALTER TABLE `Devices` ADD `LicenseStatus` int NOT NULL DEFAULT 0;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20191213073708_added_licenses_tables') THEN
    ALTER TABLE `DeviceLicenses` MODIFY COLUMN `LicenseOrderId` longtext NOT NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20191213073708_added_licenses_tables') THEN
    ALTER TABLE `DeviceLicenses` MODIFY COLUMN `ImportedAt` datetime(6) NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20191213073708_added_licenses_tables') THEN
    ALTER TABLE `DeviceLicenses` MODIFY COLUMN `DeviceId` longtext NOT NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20191213073708_added_licenses_tables') THEN
    ALTER TABLE `DeviceLicenses` ADD `EndDate` datetime(6) NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20191213073708_added_licenses_tables') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20191213073708_added_licenses_tables', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20191213074057_added_licenseorders') THEN
    CREATE TABLE `LicenseOrders` (
        `Id` varchar(255) NOT NULL,
        `ContactEmail` longtext NOT NULL,
        `Note` longtext NULL,
        `StartDate` datetime(6) NOT NULL,
        `EndDate` datetime(6) NOT NULL,
        `ProlongExistingLicenses` tinyint(1) NOT NULL,
        `CreatedAt` datetime(6) NOT NULL,
        `OrderStatus` int NOT NULL,
        CONSTRAINT `PK_LicenseOrders` PRIMARY KEY (`Id`)
    );
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20191213074057_added_licenseorders') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20191213074057_added_licenseorders', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20191227110726_data_protection') THEN
    ALTER TABLE `DataProtection` DROP COLUMN `ValueHash`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20191227110726_data_protection') THEN
    ALTER TABLE `DataProtection` MODIFY COLUMN `Id` int NOT NULL AUTO_INCREMENT;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20191227110726_data_protection') THEN
    ALTER TABLE `DataProtection` ADD `CreatedAt` datetime(6) NOT NULL DEFAULT '0001-01-01 00:00:00';
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20191227110726_data_protection') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20191227110726_data_protection', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200120120632_added_appsettings_table') THEN
    CREATE TABLE `AppSettings` (
        `Id` varchar(255) NOT NULL,
        `Value` longtext CHARACTER SET utf8mb4 NOT NULL,
        CONSTRAINT `PK_AppSettings` PRIMARY KEY (`Id`)
    );
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200120120632_added_appsettings_table') THEN
    CREATE TABLE `SummaryByDayAndEmployee` (
        `Date` datetime(6) NOT NULL,
        `Employee` longtext CHARACTER SET utf8mb4 NULL,
        `Company` longtext CHARACTER SET utf8mb4 NULL,
        `Department` longtext CHARACTER SET utf8mb4 NULL,
        `WorkstationsCount` int NOT NULL,
        `AvgSessionsDuration` time(6) NOT NULL,
        `SessionsCount` int NOT NULL,
        `TotalSessionsDuration` time(6) NOT NULL
    );
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200120120632_added_appsettings_table') THEN
    CREATE TABLE `SummaryByDepartments` (
        `Company` longtext CHARACTER SET utf8mb4 NULL,
        `Department` longtext CHARACTER SET utf8mb4 NULL,
        `EmployeesCount` int NOT NULL,
        `WorkstationsCount` int NOT NULL,
        `TotalSessionsCount` int NOT NULL,
        `TotalSessionsDuration` time(6) NOT NULL,
        `AvgSessionsDuration` time(6) NOT NULL,
        `AvgTotalDuartionByEmployee` time(6) NOT NULL,
        `AvgTotalSessionsCountByEmployee` decimal(65,30) NOT NULL
    );
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200120120632_added_appsettings_table') THEN
    CREATE TABLE `SummaryByEmployees` (
        `Employee` longtext CHARACTER SET utf8mb4 NULL,
        `Company` longtext CHARACTER SET utf8mb4 NULL,
        `Department` longtext CHARACTER SET utf8mb4 NULL,
        `WorkstationsCount` int NOT NULL,
        `WorkingDaysCount` int NOT NULL,
        `TotalSessionsCount` int NOT NULL,
        `TotalSessionsDuration` time(6) NOT NULL,
        `AvgSessionsDuration` time(6) NOT NULL,
        `AvgSessionsCountPerDay` decimal(65,30) NOT NULL,
        `AvgWorkingHoursPerDay` time(6) NOT NULL
    );
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200120120632_added_appsettings_table') THEN
    CREATE TABLE `SummaryByWorkstations` (
        `Workstation` longtext CHARACTER SET utf8mb4 NULL,
        `EmployeesCount` int NOT NULL,
        `TotalSessionsCount` int NOT NULL,
        `TotalSessionsDuration` time(6) NOT NULL,
        `AvgSessionsDuration` time(6) NOT NULL,
        `AvgTotalDuartionByEmployee` time(6) NOT NULL,
        `AvgTotalSessionsCountByEmployee` decimal(65,30) NOT NULL
    );
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200120120632_added_appsettings_table') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20200120120632_added_appsettings_table', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200210142044_update_licenseorders_table') THEN
    ALTER TABLE `LicenseOrders` MODIFY COLUMN `StartDate` datetime(6) NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200210142044_update_licenseorders_table') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20200210142044_update_licenseorders_table', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200213124427_update_device_table') THEN
    ALTER TABLE `Devices` DROP FOREIGN KEY `FK_Devices_DeviceAccessProfiles_AcceessProfileId`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200213124427_update_device_table') THEN
    ALTER TABLE `Devices` MODIFY COLUMN `AcceessProfileId` varchar(255) NOT NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200213124427_update_device_table') THEN
    ALTER TABLE `Devices` ADD CONSTRAINT `FK_Devices_DeviceAccessProfiles_AcceessProfileId` FOREIGN KEY (`AcceessProfileId`) REFERENCES `DeviceAccessProfiles` (`Id`) ON DELETE CASCADE;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200213124427_update_device_table') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20200213124427_update_device_table', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200214091514_update_device_model') THEN
    ALTER TABLE `Devices` MODIFY COLUMN `RFID` varchar(255) NOT NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200214091514_update_device_model') THEN
    ALTER TABLE `Devices` MODIFY COLUMN `Model` longtext CHARACTER SET utf8mb4 NOT NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200214091514_update_device_model') THEN
    ALTER TABLE `Devices` MODIFY COLUMN `MAC` varchar(255) NOT NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200214091514_update_device_model') THEN
    ALTER TABLE `Devices` MODIFY COLUMN `Firmware` longtext CHARACTER SET utf8mb4 NOT NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200214091514_update_device_model') THEN
    CREATE UNIQUE INDEX `IX_Devices_MAC` ON `Devices` (`MAC`);
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200214091514_update_device_model') THEN
    CREATE UNIQUE INDEX `IX_Devices_RFID` ON `Devices` (`RFID`);
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200214091514_update_device_model') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20200214091514_update_device_model', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200220095623_added_groups') THEN
    CREATE TABLE `Groups` (
        `Id` varchar(255) NOT NULL,
        `Name` varchar(255) NOT NULL,
        `Email` longtext CHARACTER SET utf8mb4 NULL,
        `Description` longtext CHARACTER SET utf8mb4 NULL,
        CONSTRAINT `PK_Groups` PRIMARY KEY (`Id`)
    );
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200220095623_added_groups') THEN
    CREATE TABLE `GroupMemberships` (
        `Id` varchar(255) CHARACTER SET utf8mb4 NOT NULL,
        `GroupId` varchar(255) CHARACTER SET utf8mb4 NULL,
        `EmployeeId` varchar(255) CHARACTER SET utf8mb4 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;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200220095623_added_groups') THEN
    CREATE UNIQUE INDEX `IX_Groups_Name` ON `Groups` (`Name`);
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200220095623_added_groups') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20200220095623_added_groups', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200228071526_update_group_membership') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20200228071526_update_group_membership', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200228144907_update_employee_fields') THEN
    ALTER TABLE `Employees` DROP FOREIGN KEY `FK_Employees_Departments_DepartmentId`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200228144907_update_employee_fields') THEN
    ALTER TABLE `Employees` DROP FOREIGN KEY `FK_Employees_Positions_PositionId`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200228144907_update_employee_fields') THEN
    ALTER TABLE `Employees` MODIFY COLUMN `PositionId` varchar(255) NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200228144907_update_employee_fields') THEN
    ALTER TABLE `Employees` MODIFY COLUMN `LastName` longtext CHARACTER SET utf8mb4 NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200228144907_update_employee_fields') THEN
    ALTER TABLE `Employees` MODIFY COLUMN `Email` longtext CHARACTER SET utf8mb4 NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200228144907_update_employee_fields') THEN
    ALTER TABLE `Employees` MODIFY COLUMN `DepartmentId` varchar(255) NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200228144907_update_employee_fields') THEN
    ALTER TABLE `Employees` ADD CONSTRAINT `FK_Employees_Departments_DepartmentId` FOREIGN KEY (`DepartmentId`) REFERENCES `Departments` (`Id`) ON DELETE RESTRICT;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200228144907_update_employee_fields') THEN
    ALTER TABLE `Employees` ADD CONSTRAINT `FK_Employees_Positions_PositionId` FOREIGN KEY (`PositionId`) REFERENCES `Positions` (`Id`) ON DELETE RESTRICT;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200228144907_update_employee_fields') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20200228144907_update_employee_fields', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200228145524_added_employee_firstname_and_lastname_isunique') THEN
    ALTER TABLE `Employees` MODIFY COLUMN `LastName` varchar(255) NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200228145524_added_employee_firstname_and_lastname_isunique') THEN
    ALTER TABLE `Employees` MODIFY COLUMN `FirstName` varchar(255) NOT NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200228145524_added_employee_firstname_and_lastname_isunique') THEN
    CREATE UNIQUE INDEX `IX_Employees_FirstName_LastName` ON `Employees` (`FirstName`, `LastName`);
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200228145524_added_employee_firstname_and_lastname_isunique') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20200228145524_added_employee_firstname_and_lastname_isunique', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200306065218_added_fk_to_groupMembership') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20200306065218_added_fk_to_groupMembership', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200306071530_updated_groupMembership') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20200306071530_updated_groupMembership', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200306072133_updated_groupMembership_employee') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20200306072133_updated_groupMembership_employee', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200313073434_move_to_new_accounts_scheme') THEN
    ALTER TABLE `DeviceAccounts` DROP FOREIGN KEY `FK_DeviceAccounts_Devices_DeviceId`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200313073434_move_to_new_accounts_scheme') THEN
    ALTER TABLE `DeviceTasks` DROP FOREIGN KEY `FK_DeviceTasks_DeviceAccounts_DeviceAccountId`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200313073434_move_to_new_accounts_scheme') THEN
    ALTER TABLE `DeviceTasks` DROP INDEX `IX_DeviceTasks_DeviceAccountId`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200313073434_move_to_new_accounts_scheme') THEN
    ALTER TABLE `DeviceAccounts` DROP INDEX `IX_DeviceAccounts_DeviceId`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200313073434_move_to_new_accounts_scheme') THEN
    ALTER TABLE `DeviceTasks` DROP COLUMN `DeviceAccountId`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200313073434_move_to_new_accounts_scheme') THEN
    ALTER TABLE `DeviceTasks` DROP COLUMN `OldApps`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200313073434_move_to_new_accounts_scheme') THEN
    ALTER TABLE `DeviceTasks` DROP COLUMN `OldLogin`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200313073434_move_to_new_accounts_scheme') THEN
    ALTER TABLE `DeviceTasks` DROP COLUMN `OldName`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200313073434_move_to_new_accounts_scheme') THEN
    ALTER TABLE `DeviceTasks` DROP COLUMN `OldUrls`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200313073434_move_to_new_accounts_scheme') THEN
    ALTER TABLE `Devices` DROP COLUMN `PrimaryAccountId`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200313073434_move_to_new_accounts_scheme') THEN
    ALTER TABLE `DeviceAccounts` DROP COLUMN `DeviceId`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200313073434_move_to_new_accounts_scheme') THEN
    ALTER TABLE `DeviceAccounts` DROP COLUMN `LastSyncedAt`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200313073434_move_to_new_accounts_scheme') THEN
    ALTER TABLE `DeviceAccounts` DROP COLUMN `Status`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200313073434_move_to_new_accounts_scheme') THEN
    ALTER TABLE `Employees` ADD `PrimaryAccountId` varchar(255) NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200313073434_move_to_new_accounts_scheme') THEN
    ALTER TABLE `DeviceTasks` ADD `AccountId` varchar(255) NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200313073434_move_to_new_accounts_scheme') THEN
    ALTER TABLE `Devices` ADD `NeedSync` tinyint(1) NOT NULL DEFAULT FALSE;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200313073434_move_to_new_accounts_scheme') THEN
    CREATE UNIQUE INDEX `IX_Employees_PrimaryAccountId` ON `Employees` (`PrimaryAccountId`);
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200313073434_move_to_new_accounts_scheme') THEN
    CREATE INDEX `IX_DeviceTasks_AccountId` ON `DeviceTasks` (`AccountId`);
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200313073434_move_to_new_accounts_scheme') THEN
    ALTER TABLE `DeviceTasks` ADD CONSTRAINT `FK_DeviceTasks_DeviceAccounts_AccountId` FOREIGN KEY (`AccountId`) REFERENCES `DeviceAccounts` (`Id`) ON DELETE RESTRICT;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200313073434_move_to_new_accounts_scheme') THEN
    ALTER TABLE `Employees` ADD CONSTRAINT `FK_Employees_DeviceAccounts_PrimaryAccountId` FOREIGN KEY (`PrimaryAccountId`) REFERENCES `DeviceAccounts` (`Id`) ON DELETE RESTRICT;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200313073434_move_to_new_accounts_scheme') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20200313073434_move_to_new_accounts_scheme', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200316123230_rename_deviceaccounts_table_remove_idp_table') THEN
    ALTER TABLE `DeviceAccounts` DROP FOREIGN KEY `FK_DeviceAccounts_Employees_EmployeeId`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200316123230_rename_deviceaccounts_table_remove_idp_table') THEN
    ALTER TABLE `DeviceAccounts` DROP FOREIGN KEY `FK_DeviceAccounts_SharedAccounts_SharedAccountId`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200316123230_rename_deviceaccounts_table_remove_idp_table') THEN
    ALTER TABLE `DeviceTasks` DROP FOREIGN KEY `FK_DeviceTasks_DeviceAccounts_AccountId`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200316123230_rename_deviceaccounts_table_remove_idp_table') THEN
    ALTER TABLE `Employees` DROP FOREIGN KEY `FK_Employees_DeviceAccounts_PrimaryAccountId`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200316123230_rename_deviceaccounts_table_remove_idp_table') THEN
    ALTER TABLE `WorkstationEvents` DROP FOREIGN KEY `FK_WorkstationEvents_DeviceAccounts_DeviceAccountId`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200316123230_rename_deviceaccounts_table_remove_idp_table') THEN
    ALTER TABLE `WorkstationSessions` DROP FOREIGN KEY `FK_WorkstationSessions_DeviceAccounts_DeviceAccountId`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200316123230_rename_deviceaccounts_table_remove_idp_table') THEN
    DROP TABLE `SamlIdentityProvider`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200316123230_rename_deviceaccounts_table_remove_idp_table') THEN
    CALL POMELO_BEFORE_DROP_PRIMARY_KEY(NULL, 'DeviceAccounts');
    ALTER TABLE `DeviceAccounts` DROP PRIMARY KEY;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200316123230_rename_deviceaccounts_table_remove_idp_table') THEN
    ALTER TABLE `AspNetUsers` DROP COLUMN `DeviceId`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200316123230_rename_deviceaccounts_table_remove_idp_table') THEN
    ALTER TABLE `DeviceAccounts` RENAME `Accounts`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200316123230_rename_deviceaccounts_table_remove_idp_table') THEN
    ALTER TABLE `Accounts` RENAME INDEX `IX_DeviceAccounts_SharedAccountId` TO `IX_Accounts_SharedAccountId`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200316123230_rename_deviceaccounts_table_remove_idp_table') THEN
    ALTER TABLE `Accounts` RENAME INDEX `IX_DeviceAccounts_EmployeeId` TO `IX_Accounts_EmployeeId`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200316123230_rename_deviceaccounts_table_remove_idp_table') THEN
    ALTER TABLE `Accounts` ADD CONSTRAINT `PK_Accounts` PRIMARY KEY (`Id`);
    CALL POMELO_AFTER_ADD_PRIMARY_KEY(NULL, 'Accounts', 'Id');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200316123230_rename_deviceaccounts_table_remove_idp_table') THEN
    ALTER TABLE `Accounts` ADD CONSTRAINT `FK_Accounts_Employees_EmployeeId` FOREIGN KEY (`EmployeeId`) REFERENCES `Employees` (`Id`) ON DELETE RESTRICT;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200316123230_rename_deviceaccounts_table_remove_idp_table') THEN
    ALTER TABLE `Accounts` ADD CONSTRAINT `FK_Accounts_SharedAccounts_SharedAccountId` FOREIGN KEY (`SharedAccountId`) REFERENCES `SharedAccounts` (`Id`) ON DELETE RESTRICT;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200316123230_rename_deviceaccounts_table_remove_idp_table') THEN
    ALTER TABLE `DeviceTasks` ADD CONSTRAINT `FK_DeviceTasks_Accounts_AccountId` FOREIGN KEY (`AccountId`) REFERENCES `Accounts` (`Id`) ON DELETE RESTRICT;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200316123230_rename_deviceaccounts_table_remove_idp_table') THEN
    ALTER TABLE `Employees` ADD CONSTRAINT `FK_Employees_Accounts_PrimaryAccountId` FOREIGN KEY (`PrimaryAccountId`) REFERENCES `Accounts` (`Id`) ON DELETE RESTRICT;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200316123230_rename_deviceaccounts_table_remove_idp_table') THEN
    ALTER TABLE `WorkstationEvents` ADD CONSTRAINT `FK_WorkstationEvents_Accounts_DeviceAccountId` FOREIGN KEY (`DeviceAccountId`) REFERENCES `Accounts` (`Id`) ON DELETE RESTRICT;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200316123230_rename_deviceaccounts_table_remove_idp_table') THEN
    ALTER TABLE `WorkstationSessions` ADD CONSTRAINT `FK_WorkstationSessions_Accounts_DeviceAccountId` FOREIGN KEY (`DeviceAccountId`) REFERENCES `Accounts` (`Id`) ON DELETE RESTRICT;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200316123230_rename_deviceaccounts_table_remove_idp_table') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20200316123230_rename_deviceaccounts_table_remove_idp_table', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200325101305_added_table_software_vaults') THEN
    CREATE TABLE `SoftwareVaults` (
        `Id` varchar(255) NOT NULL,
        `OS` longtext CHARACTER SET utf8mb4 NOT NULL,
        `Model` longtext CHARACTER SET utf8mb4 NOT NULL,
        `ClientAppVersion` longtext CHARACTER SET utf8mb4 NOT NULL,
        `CreatedAt` datetime(6) NOT NULL,
        `Status` int NOT NULL,
        `EmployeeId` varchar(255) NOT NULL,
        `HasNewLicense` tinyint(1) NOT NULL,
        `LicenseEndDate` datetime(6) NULL,
        `LicenseStatus` int NOT NULL,
        `NeedSync` tinyint(1) NOT NULL,
        CONSTRAINT `PK_SoftwareVaults` PRIMARY KEY (`Id`),
        CONSTRAINT `FK_SoftwareVaults_Employees_EmployeeId` FOREIGN KEY (`EmployeeId`) REFERENCES `Employees` (`Id`) ON DELETE CASCADE
    );
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200325101305_added_table_software_vaults') THEN
    CREATE INDEX `IX_SoftwareVaults_EmployeeId` ON `SoftwareVaults` (`EmployeeId`);
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200325101305_added_table_software_vaults') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20200325101305_added_table_software_vaults', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200325112421_added_table_software_vault_invitations') THEN
    CREATE TABLE `SoftwareVaultInvitation` (
        `Id` varchar(255) NOT NULL,
        `EmployeeId` varchar(255) NOT NULL,
        `Status` int NOT NULL,
        `CreatedAt` datetime(6) NOT NULL,
        `ValidTo` datetime(6) NOT NULL,
        `AcceptedAt` datetime(6) NULL,
        `SoftwareVaultId` varchar(255) NULL,
        `ActivationCode` int NOT NULL,
        CONSTRAINT `PK_SoftwareVaultInvitation` PRIMARY KEY (`Id`),
        CONSTRAINT `FK_SoftwareVaultInvitation_Employees_EmployeeId` FOREIGN KEY (`EmployeeId`) REFERENCES `Employees` (`Id`) ON DELETE CASCADE,
        CONSTRAINT `FK_SoftwareVaultInvitation_SoftwareVaults_SoftwareVaultId` FOREIGN KEY (`SoftwareVaultId`) REFERENCES `SoftwareVaults` (`Id`) ON DELETE RESTRICT
    );
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200325112421_added_table_software_vault_invitations') THEN
    CREATE INDEX `IX_SoftwareVaultInvitation_EmployeeId` ON `SoftwareVaultInvitation` (`EmployeeId`);
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200325112421_added_table_software_vault_invitations') THEN
    CREATE INDEX `IX_SoftwareVaultInvitation_SoftwareVaultId` ON `SoftwareVaultInvitation` (`SoftwareVaultId`);
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200325112421_added_table_software_vault_invitations') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20200325112421_added_table_software_vault_invitations', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200326200554_table_software_vault_invitations') THEN
    ALTER TABLE `SoftwareVaultInvitation` DROP FOREIGN KEY `FK_SoftwareVaultInvitation_Employees_EmployeeId`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200326200554_table_software_vault_invitations') THEN
    ALTER TABLE `SoftwareVaultInvitation` DROP FOREIGN KEY `FK_SoftwareVaultInvitation_SoftwareVaults_SoftwareVaultId`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200326200554_table_software_vault_invitations') THEN
    CALL POMELO_BEFORE_DROP_PRIMARY_KEY(NULL, 'SoftwareVaultInvitation');
    ALTER TABLE `SoftwareVaultInvitation` DROP PRIMARY KEY;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200326200554_table_software_vault_invitations') THEN
    ALTER TABLE `SoftwareVaultInvitation` RENAME `SoftwareVaultInvitations`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200326200554_table_software_vault_invitations') THEN
    ALTER TABLE `SoftwareVaultInvitations` RENAME INDEX `IX_SoftwareVaultInvitation_SoftwareVaultId` TO `IX_SoftwareVaultInvitations_SoftwareVaultId`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200326200554_table_software_vault_invitations') THEN
    ALTER TABLE `SoftwareVaultInvitations` RENAME INDEX `IX_SoftwareVaultInvitation_EmployeeId` TO `IX_SoftwareVaultInvitations_EmployeeId`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200326200554_table_software_vault_invitations') THEN
    ALTER TABLE `SoftwareVaultInvitations` ADD CONSTRAINT `PK_SoftwareVaultInvitations` PRIMARY KEY (`Id`);
    CALL POMELO_AFTER_ADD_PRIMARY_KEY(NULL, 'SoftwareVaultInvitations', 'Id');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200326200554_table_software_vault_invitations') THEN
    ALTER TABLE `SoftwareVaultInvitations` ADD CONSTRAINT `FK_SoftwareVaultInvitations_Employees_EmployeeId` FOREIGN KEY (`EmployeeId`) REFERENCES `Employees` (`Id`) ON DELETE CASCADE;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200326200554_table_software_vault_invitations') THEN
    ALTER TABLE `SoftwareVaultInvitations` ADD CONSTRAINT `FK_SoftwareVaultInvitations_SoftwareVaults_SoftwareVaultId` FOREIGN KEY (`SoftwareVaultId`) REFERENCES `SoftwareVaults` (`Id`) ON DELETE RESTRICT;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200326200554_table_software_vault_invitations') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20200326200554_table_software_vault_invitations', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200417164313_fixed_employee_fk') THEN
    ALTER TABLE `Employees` DROP FOREIGN KEY `FK_Employees_Accounts_PrimaryAccountId`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200417164313_fixed_employee_fk') THEN
    ALTER TABLE `Employees` DROP INDEX `IX_Employees_PrimaryAccountId`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200417164313_fixed_employee_fk') THEN
    ALTER TABLE `Employees` MODIFY COLUMN `PrimaryAccountId` longtext CHARACTER SET utf8mb4 NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200417164313_fixed_employee_fk') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20200417164313_fixed_employee_fk', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200421160151_added_HardwareVaultActivation') THEN
    CREATE TABLE `HardwareVaultActivations` (
        `Id` varchar(255) NOT NULL,
        `DeviceId` longtext CHARACTER SET utf8mb4 NOT NULL,
        `AcivationCode` longtext CHARACTER SET utf8mb4 NOT NULL,
        `WrongAttemptsCount` int NOT NULL,
        `LastWrongAttempt` datetime(6) NULL,
        `CreatedAt` datetime(6) NOT NULL,
        `Status` int NOT NULL,
        CONSTRAINT `PK_HardwareVaultActivations` PRIMARY KEY (`Id`)
    );
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200421160151_added_HardwareVaultActivation') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20200421160151_added_HardwareVaultActivation', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200421162239_added_new_fields_StatusReason_and_StatusDescription') THEN
    ALTER TABLE `Devices` DROP COLUMN `State`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200421162239_added_new_fields_StatusReason_and_StatusDescription') THEN
    ALTER TABLE `Devices` ADD `Status` int NOT NULL DEFAULT 0;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200421162239_added_new_fields_StatusReason_and_StatusDescription') THEN
    ALTER TABLE `Devices` ADD `StatusDescription` longtext CHARACTER SET utf8mb4 NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200421162239_added_new_fields_StatusReason_and_StatusDescription') THEN
    ALTER TABLE `Devices` ADD `StatusReason` int NOT NULL DEFAULT 0;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200421162239_added_new_fields_StatusReason_and_StatusDescription') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20200421162239_added_new_fields_StatusReason_and_StatusDescription', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200423100528_update_HardwareVaultActivation_table') THEN
    ALTER TABLE `HardwareVaultActivations` DROP COLUMN `DeviceId`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200423100528_update_HardwareVaultActivation_table') THEN
    ALTER TABLE `HardwareVaultActivations` DROP COLUMN `LastWrongAttempt`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200423100528_update_HardwareVaultActivation_table') THEN
    ALTER TABLE `HardwareVaultActivations` DROP COLUMN `WrongAttemptsCount`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200423100528_update_HardwareVaultActivation_table') THEN
    ALTER TABLE `HardwareVaultActivations` ADD `VaultId` longtext CHARACTER SET utf8mb4 NOT NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200423100528_update_HardwareVaultActivation_table') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20200423100528_update_HardwareVaultActivation_table', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200429105510_rename_device_to_hardwarevault') THEN
    ALTER TABLE `WorkstationEvents` DROP FOREIGN KEY `FK_WorkstationEvents_Accounts_DeviceAccountId`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200429105510_rename_device_to_hardwarevault') THEN
    ALTER TABLE `WorkstationEvents` DROP FOREIGN KEY `FK_WorkstationEvents_Devices_DeviceId`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200429105510_rename_device_to_hardwarevault') THEN
    ALTER TABLE `WorkstationSessions` DROP FOREIGN KEY `FK_WorkstationSessions_Accounts_DeviceAccountId`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200429105510_rename_device_to_hardwarevault') THEN
    ALTER TABLE `WorkstationSessions` DROP FOREIGN KEY `FK_WorkstationSessions_Devices_DeviceId`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200429105510_rename_device_to_hardwarevault') THEN
    ALTER TABLE `Devices` DROP FOREIGN KEY `FK_Devices_DeviceAccessProfiles_AcceessProfileId`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200429105510_rename_device_to_hardwarevault') THEN
    ALTER TABLE `Devices` DROP FOREIGN KEY `FK_Devices_Employees_EmployeeId`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200429105510_rename_device_to_hardwarevault') THEN
    ALTER TABLE `DeviceTasks` DROP FOREIGN KEY `FK_DeviceTasks_Accounts_AccountId`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200429105510_rename_device_to_hardwarevault') THEN
    ALTER TABLE `ProximityDevices` DROP FOREIGN KEY `FK_ProximityDevices_Devices_DeviceId`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200429105510_rename_device_to_hardwarevault') THEN
    ALTER TABLE `ProximityDevices` DROP FOREIGN KEY `FK_ProximityDevices_Workstations_WorkstationId`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200429105510_rename_device_to_hardwarevault') THEN
    ALTER TABLE `WorkstationSessions` DROP INDEX `IX_WorkstationSessions_DeviceAccountId`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200429105510_rename_device_to_hardwarevault') THEN
    ALTER TABLE `WorkstationSessions` DROP INDEX `IX_WorkstationSessions_DeviceId`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200429105510_rename_device_to_hardwarevault') THEN
    ALTER TABLE `WorkstationEvents` DROP INDEX `IX_WorkstationEvents_DeviceAccountId`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200429105510_rename_device_to_hardwarevault') THEN
    ALTER TABLE `WorkstationEvents` DROP INDEX `IX_WorkstationEvents_DeviceId`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200429105510_rename_device_to_hardwarevault') THEN
    ALTER TABLE `Devices` DROP INDEX `IX_Devices_EmployeeId`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200429105510_rename_device_to_hardwarevault') THEN
    ALTER TABLE `Devices` DROP INDEX `IX_Devices_AcceessProfileId`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200429105510_rename_device_to_hardwarevault') THEN
    ALTER TABLE `Devices` DROP INDEX `IX_Devices_MAC`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200429105510_rename_device_to_hardwarevault') THEN
    ALTER TABLE `Devices` DROP INDEX `IX_Devices_RFID`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200429105510_rename_device_to_hardwarevault') THEN
    ALTER TABLE `DeviceTasks` DROP INDEX `IX_DeviceTasks_AccountId`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200429105510_rename_device_to_hardwarevault') THEN
    ALTER TABLE `ProximityDevices` DROP INDEX `IX_ProximityDevices_DeviceId`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200429105510_rename_device_to_hardwarevault') THEN
    ALTER TABLE `ProximityDevices` DROP INDEX `IX_ProximityDevices_WorkstationId`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200429105510_rename_device_to_hardwarevault') THEN
    ALTER TABLE `DeviceLicenses` RENAME `HardwareVaultLicenses`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200429105510_rename_device_to_hardwarevault') THEN
    ALTER TABLE `DeviceTasks` RENAME `HardwareVaultTasks`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200429105510_rename_device_to_hardwarevault') THEN
    ALTER TABLE `ProximityDevices` RENAME `WorkstationProximityVaults`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200429105510_rename_device_to_hardwarevault') THEN
    ALTER TABLE `Devices` RENAME `HardwareVaults`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200429105510_rename_device_to_hardwarevault') THEN
    ALTER TABLE `DeviceAccessProfiles` RENAME `HardwareVaultProfiles`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200429105510_rename_device_to_hardwarevault') THEN
    ALTER TABLE `WorkstationSessions` RENAME COLUMN `DeviceAccountId` TO `AccountId`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200429105510_rename_device_to_hardwarevault') THEN
    ALTER TABLE `WorkstationSessions` RENAME COLUMN `DeviceId` TO `HardwareVaultId`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200429105510_rename_device_to_hardwarevault') THEN
    ALTER TABLE `WorkstationEvents` RENAME COLUMN `DeviceAccountId` TO `AccountId`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200429105510_rename_device_to_hardwarevault') THEN
    ALTER TABLE `WorkstationEvents` RENAME COLUMN `DeviceId` TO `HardwareVaultId`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200429105510_rename_device_to_hardwarevault') THEN
    ALTER TABLE `HardwareVaults` RENAME COLUMN `AcceessProfileId` TO `HardwareVaultProfileId`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200429105510_rename_device_to_hardwarevault') THEN
    ALTER TABLE `Templates` DROP COLUMN `Deleted`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200429105510_rename_device_to_hardwarevault') THEN
    ALTER TABLE `HardwareVaultLicenses` RENAME COLUMN `DeviceId` TO `HardwareVaultId`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200429105510_rename_device_to_hardwarevault') THEN
    ALTER TABLE `WorkstationProximityVaults` RENAME COLUMN `DeviceId` TO `HardwareVaultId`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200429105510_rename_device_to_hardwarevault') THEN
    ALTER TABLE `HardwareVaultTasks` RENAME COLUMN `DeviceId` TO `HardwareVaultId`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200429105510_rename_device_to_hardwarevault') THEN
    CREATE INDEX `IX_WorkstationSessions_AccountId` ON `WorkstationSessions` (`AccountId`);
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200429105510_rename_device_to_hardwarevault') THEN
    CREATE INDEX `IX_WorkstationSessions_HardwareVaultId` ON `WorkstationSessions` (`HardwareVaultId`);
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200429105510_rename_device_to_hardwarevault') THEN
    CREATE INDEX `IX_WorkstationEvents_AccountId` ON `WorkstationEvents` (`AccountId`);
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200429105510_rename_device_to_hardwarevault') THEN
    CREATE INDEX `IX_WorkstationEvents_HardwareVaultId` ON `WorkstationEvents` (`HardwareVaultId`);
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200429105510_rename_device_to_hardwarevault') THEN
    CREATE INDEX `IX_HardwareVaults_EmployeeId` ON `HardwareVaults` (`EmployeeId`);
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200429105510_rename_device_to_hardwarevault') THEN
    CREATE INDEX `IX_HardwareVaults_HardwareVaultProfileId` ON `HardwareVaults` (`HardwareVaultProfileId`);
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200429105510_rename_device_to_hardwarevault') THEN
    CREATE UNIQUE INDEX `IX_HardwareVaults_MAC` ON `HardwareVaults` (`MAC`);
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200429105510_rename_device_to_hardwarevault') THEN
    CREATE UNIQUE INDEX `IX_HardwareVaults_RFID` ON `HardwareVaults` (`RFID`);
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200429105510_rename_device_to_hardwarevault') THEN
    CREATE INDEX `IX_HardwareVaultTasks_AccountId` ON `HardwareVaultTasks` (`AccountId`);
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200429105510_rename_device_to_hardwarevault') THEN
    CREATE INDEX `IX_WorkstationProximityVaults_HardwareVaultId` ON `WorkstationProximityVaults` (`HardwareVaultId`);
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200429105510_rename_device_to_hardwarevault') THEN
    CREATE INDEX `IX_WorkstationProximityVaults_WorkstationId` ON `WorkstationProximityVaults` (`WorkstationId`);
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200429105510_rename_device_to_hardwarevault') THEN
    ALTER TABLE `WorkstationEvents` ADD CONSTRAINT `FK_WorkstationEvents_Accounts_AccountId` FOREIGN KEY (`AccountId`) REFERENCES `Accounts` (`Id`) ON DELETE RESTRICT;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200429105510_rename_device_to_hardwarevault') THEN
    ALTER TABLE `WorkstationEvents` ADD CONSTRAINT `FK_WorkstationEvents_HardwareVaults_HardwareVaultId` FOREIGN KEY (`HardwareVaultId`) REFERENCES `HardwareVaults` (`Id`) ON DELETE RESTRICT;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200429105510_rename_device_to_hardwarevault') THEN
    ALTER TABLE `WorkstationSessions` ADD CONSTRAINT `FK_WorkstationSessions_Accounts_AccountId` FOREIGN KEY (`AccountId`) REFERENCES `Accounts` (`Id`) ON DELETE RESTRICT;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200429105510_rename_device_to_hardwarevault') THEN
    ALTER TABLE `WorkstationSessions` ADD CONSTRAINT `FK_WorkstationSessions_HardwareVaults_HardwareVaultId` FOREIGN KEY (`HardwareVaultId`) REFERENCES `HardwareVaults` (`Id`) ON DELETE RESTRICT;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200429105510_rename_device_to_hardwarevault') THEN
    ALTER TABLE `HardwareVaults` ADD CONSTRAINT `FK_HardwareVaults_Employees_EmployeeId` FOREIGN KEY (`EmployeeId`) REFERENCES `Employees` (`Id`) ON DELETE RESTRICT;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200429105510_rename_device_to_hardwarevault') THEN
    ALTER TABLE `HardwareVaults` ADD CONSTRAINT `FK_HardwareVaults_HardwareVaultProfiles_AcceessProfileId` FOREIGN KEY (`HardwareVaultProfileId`) REFERENCES `HardwareVaultProfiles` (`Id`) ON DELETE RESTRICT;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200429105510_rename_device_to_hardwarevault') THEN
    ALTER TABLE `HardwareVaultTasks` ADD CONSTRAINT `FK_HardwareVaultTasks_Accounts_AccountId` FOREIGN KEY (`AccountId`) REFERENCES `Accounts` (`Id`) ON DELETE RESTRICT;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200429105510_rename_device_to_hardwarevault') THEN
    ALTER TABLE `WorkstationProximityVaults` ADD CONSTRAINT `FK_WorkstationProximityVaults_HardwareVaults_HardwareVaultId` FOREIGN KEY (`HardwareVaultId`) REFERENCES `HardwareVaults` (`Id`) ON DELETE RESTRICT;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200429105510_rename_device_to_hardwarevault') THEN
    ALTER TABLE `WorkstationProximityVaults` ADD CONSTRAINT `FK_WorkstationProximityVaults_Workstations_WorkstationId` FOREIGN KEY (`WorkstationId`) REFERENCES `Workstations` (`Id`) ON DELETE RESTRICT;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200429105510_rename_device_to_hardwarevault') THEN
                    UPDATE HardwareVaults
                    SET Status = IF(MasterPassword IS NOT NULL, 2, 0);
                
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200429105510_rename_device_to_hardwarevault') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20200429105510_rename_device_to_hardwarevault', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200505124514_added_new_account_fields_storageId_timestamp') THEN
    ALTER TABLE `Accounts` ADD `StorageId` int unsigned NOT NULL DEFAULT 0;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200505124514_added_new_account_fields_storageId_timestamp') THEN
    ALTER TABLE `Accounts` ADD `Timestamp` int unsigned NOT NULL DEFAULT 0;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200505124514_added_new_account_fields_storageId_timestamp') THEN
                    UPDATE Accounts
                    SET StorageId = IdFromDevice;
                
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200505124514_added_new_account_fields_storageId_timestamp') THEN
    ALTER TABLE `Accounts` DROP COLUMN `IdFromDevice`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200505124514_added_new_account_fields_storageId_timestamp') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20200505124514_added_new_account_fields_storageId_timestamp', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200505132035_added_new_account_fields_password_otpsecret') THEN
    ALTER TABLE `Accounts` ADD `OtpSecret` longtext CHARACTER SET utf8mb4 NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200505132035_added_new_account_fields_password_otpsecret') THEN
    ALTER TABLE `Accounts` ADD `Password` longtext CHARACTER SET utf8mb4 NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200505132035_added_new_account_fields_password_otpsecret') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20200505132035_added_new_account_fields_password_otpsecret', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200505133146_added_new_employee_field_ActiveDirectoryGuid') THEN
    ALTER TABLE `Employees` ADD `ActiveDirectoryGuid` longtext CHARACTER SET utf8mb4 NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200505133146_added_new_employee_field_ActiveDirectoryGuid') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20200505133146_added_new_employee_field_ActiveDirectoryGuid', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200506085724_added_hwtask_field_timestamp') THEN
    ALTER TABLE `HardwareVaultTasks` ADD `Timestamp` int unsigned NOT NULL DEFAULT 0;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200506085724_added_hwtask_field_timestamp') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20200506085724_added_hwtask_field_timestamp', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200526100351_added_employee_cascade_delete') THEN
    ALTER TABLE `Accounts` DROP FOREIGN KEY `FK_Accounts_Employees_EmployeeId`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200526100351_added_employee_cascade_delete') THEN
    ALTER TABLE `WorkstationEvents` DROP FOREIGN KEY `FK_WorkstationEvents_Employees_EmployeeId`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200526100351_added_employee_cascade_delete') THEN
    ALTER TABLE `WorkstationSessions` DROP FOREIGN KEY `FK_WorkstationSessions_Employees_EmployeeId`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200526100351_added_employee_cascade_delete') THEN
    ALTER TABLE `Accounts` ADD CONSTRAINT `FK_Accounts_Employees_EmployeeId` FOREIGN KEY (`EmployeeId`) REFERENCES `Employees` (`Id`) ON DELETE CASCADE;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200526100351_added_employee_cascade_delete') THEN
    ALTER TABLE `WorkstationEvents` ADD CONSTRAINT `FK_WorkstationEvents_Employees_EmployeeId` FOREIGN KEY (`EmployeeId`) REFERENCES `Employees` (`Id`) ON DELETE CASCADE;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200526100351_added_employee_cascade_delete') THEN
    ALTER TABLE `WorkstationSessions` ADD CONSTRAINT `FK_WorkstationSessions_Employees_EmployeeId` FOREIGN KEY (`EmployeeId`) REFERENCES `Employees` (`Id`) ON DELETE CASCADE;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200526100351_added_employee_cascade_delete') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20200526100351_added_employee_cascade_delete', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200602070547_added_fk_hardwarevaultlicense') THEN
    ALTER TABLE `HardwareVaultLicenses` MODIFY COLUMN `LicenseOrderId` varchar(255) NOT NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200602070547_added_fk_hardwarevaultlicense') THEN
    ALTER TABLE `HardwareVaultLicenses` MODIFY COLUMN `HardwareVaultId` varchar(255) NOT NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200602070547_added_fk_hardwarevaultlicense') THEN
    CREATE INDEX `IX_HardwareVaultLicenses_HardwareVaultId` ON `HardwareVaultLicenses` (`HardwareVaultId`);
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200602070547_added_fk_hardwarevaultlicense') THEN
    CREATE INDEX `IX_HardwareVaultLicenses_LicenseOrderId` ON `HardwareVaultLicenses` (`LicenseOrderId`);
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200602070547_added_fk_hardwarevaultlicense') THEN
    ALTER TABLE `HardwareVaultLicenses` ADD CONSTRAINT `FK_HardwareVaultLicenses_HardwareVaults_HardwareVaultId` FOREIGN KEY (`HardwareVaultId`) REFERENCES `HardwareVaults` (`Id`) ON DELETE CASCADE;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200602070547_added_fk_hardwarevaultlicense') THEN
    ALTER TABLE `HardwareVaultLicenses` ADD CONSTRAINT `FK_HardwareVaultLicenses_LicenseOrders_LicenseOrderId` FOREIGN KEY (`LicenseOrderId`) REFERENCES `LicenseOrders` (`Id`) ON DELETE CASCADE;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200602070547_added_fk_hardwarevaultlicense') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20200602070547_added_fk_hardwarevaultlicense', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200604132155_added_fk_hardwarevaulttask') THEN
    ALTER TABLE `HardwareVaultTasks` MODIFY COLUMN `HardwareVaultId` varchar(255) NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200604132155_added_fk_hardwarevaulttask') THEN
    CREATE INDEX `IX_HardwareVaultTasks_HardwareVaultId` ON `HardwareVaultTasks` (`HardwareVaultId`);
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200604132155_added_fk_hardwarevaulttask') THEN
    ALTER TABLE `HardwareVaultTasks` ADD CONSTRAINT `FK_HardwareVaultTasks_HardwareVaults_HardwareVaultId` FOREIGN KEY (`HardwareVaultId`) REFERENCES `HardwareVaults` (`Id`) ON DELETE RESTRICT;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200604132155_added_fk_hardwarevaulttask') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20200604132155_added_fk_hardwarevaulttask', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200605095215_updated_account_storageId') THEN
    ALTER TABLE `Accounts` MODIFY COLUMN `StorageId` longblob NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200605095215_updated_account_storageId') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20200605095215_updated_account_storageId', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200610145625_added_timestamp_field_hardwarevault') THEN
    ALTER TABLE `HardwareVaults` ADD `Timestamp` int unsigned NOT NULL DEFAULT 0;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200610145625_added_timestamp_field_hardwarevault') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20200610145625_added_timestamp_field_hardwarevault', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200616131913_updated_account_on_delete') THEN
    ALTER TABLE `WorkstationEvents` DROP FOREIGN KEY `FK_WorkstationEvents_Accounts_AccountId`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200616131913_updated_account_on_delete') THEN
    ALTER TABLE `WorkstationSessions` DROP FOREIGN KEY `FK_WorkstationSessions_Accounts_AccountId`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200616131913_updated_account_on_delete') THEN
    ALTER TABLE `WorkstationEvents` ADD CONSTRAINT `FK_WorkstationEvents_Accounts_AccountId` FOREIGN KEY (`AccountId`) REFERENCES `Accounts` (`Id`) ON DELETE SET NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200616131913_updated_account_on_delete') THEN
    ALTER TABLE `WorkstationSessions` ADD CONSTRAINT `FK_WorkstationSessions_Accounts_AccountId` FOREIGN KEY (`AccountId`) REFERENCES `Accounts` (`Id`) ON DELETE SET NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200616131913_updated_account_on_delete') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20200616131913_updated_account_on_delete', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200616134454_updated_workstation_on_delete') THEN
    ALTER TABLE `WorkstationEvents` DROP FOREIGN KEY `FK_WorkstationEvents_Workstations_WorkstationId`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200616134454_updated_workstation_on_delete') THEN
    ALTER TABLE `WorkstationProximityVaults` DROP FOREIGN KEY `FK_WorkstationProximityVaults_Workstations_WorkstationId`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200616134454_updated_workstation_on_delete') THEN
    ALTER TABLE `WorkstationSessions` DROP FOREIGN KEY `FK_WorkstationSessions_Workstations_WorkstationId`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200616134454_updated_workstation_on_delete') THEN
    ALTER TABLE `WorkstationEvents` ADD CONSTRAINT `FK_WorkstationEvents_Workstations_WorkstationId` FOREIGN KEY (`WorkstationId`) REFERENCES `Workstations` (`Id`) ON DELETE CASCADE;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200616134454_updated_workstation_on_delete') THEN
    ALTER TABLE `WorkstationProximityVaults` ADD CONSTRAINT `FK_WorkstationProximityVaults_Workstations_WorkstationId` FOREIGN KEY (`WorkstationId`) REFERENCES `Workstations` (`Id`) ON DELETE CASCADE;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200616134454_updated_workstation_on_delete') THEN
    ALTER TABLE `WorkstationSessions` ADD CONSTRAINT `FK_WorkstationSessions_Workstations_WorkstationId` FOREIGN KEY (`WorkstationId`) REFERENCES `Workstations` (`Id`) ON DELETE CASCADE;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200616134454_updated_workstation_on_delete') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20200616134454_updated_workstation_on_delete', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200616140433_updated_department_on_delete') THEN
    ALTER TABLE `Employees` DROP FOREIGN KEY `FK_Employees_Departments_DepartmentId`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200616140433_updated_department_on_delete') THEN
    ALTER TABLE `WorkstationEvents` DROP FOREIGN KEY `FK_WorkstationEvents_Departments_DepartmentId`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200616140433_updated_department_on_delete') THEN
    ALTER TABLE `Workstations` DROP FOREIGN KEY `FK_Workstations_Departments_DepartmentId`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200616140433_updated_department_on_delete') THEN
    ALTER TABLE `WorkstationSessions` DROP FOREIGN KEY `FK_WorkstationSessions_Departments_DepartmentId`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200616140433_updated_department_on_delete') THEN
    ALTER TABLE `Employees` ADD CONSTRAINT `FK_Employees_Departments_DepartmentId` FOREIGN KEY (`DepartmentId`) REFERENCES `Departments` (`Id`) ON DELETE SET NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200616140433_updated_department_on_delete') THEN
    ALTER TABLE `WorkstationEvents` ADD CONSTRAINT `FK_WorkstationEvents_Departments_DepartmentId` FOREIGN KEY (`DepartmentId`) REFERENCES `Departments` (`Id`) ON DELETE SET NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200616140433_updated_department_on_delete') THEN
    ALTER TABLE `Workstations` ADD CONSTRAINT `FK_Workstations_Departments_DepartmentId` FOREIGN KEY (`DepartmentId`) REFERENCES `Departments` (`Id`) ON DELETE SET NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200616140433_updated_department_on_delete') THEN
    ALTER TABLE `WorkstationSessions` ADD CONSTRAINT `FK_WorkstationSessions_Departments_DepartmentId` FOREIGN KEY (`DepartmentId`) REFERENCES `Departments` (`Id`) ON DELETE SET NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200616140433_updated_department_on_delete') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20200616140433_updated_department_on_delete', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200701085926_added_is_status_applied_field') THEN
    ALTER TABLE `SharedAccounts` MODIFY COLUMN `Password` longtext CHARACTER SET utf8mb4 NOT NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200701085926_added_is_status_applied_field') THEN
    ALTER TABLE `HardwareVaults` ADD `IsStatusApplied` tinyint(1) NOT NULL DEFAULT FALSE;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200701085926_added_is_status_applied_field') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20200701085926_added_is_status_applied_field', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200709092958_account_storageid_set_required') THEN
    ALTER TABLE `Accounts` MODIFY COLUMN `StorageId` longblob NOT NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200709092958_account_storageid_set_required') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20200709092958_account_storageid_set_required', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200713125529_updated_position_delete_behavior') THEN
    ALTER TABLE `Employees` DROP FOREIGN KEY `FK_Employees_Positions_PositionId`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200713125529_updated_position_delete_behavior') THEN
    ALTER TABLE `Employees` ADD CONSTRAINT `FK_Employees_Positions_PositionId` FOREIGN KEY (`PositionId`) REFERENCES `Positions` (`Id`) ON DELETE SET NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200713125529_updated_position_delete_behavior') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20200713125529_updated_position_delete_behavior', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200722090758_fixed_summary_dbquery') THEN
    DROP TABLE `SummaryByDayAndEmployee`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200722090758_fixed_summary_dbquery') THEN
    DROP TABLE `SummaryByDepartments`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200722090758_fixed_summary_dbquery') THEN
    DROP TABLE `SummaryByEmployees`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200722090758_fixed_summary_dbquery') THEN
    DROP TABLE `SummaryByWorkstations`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200722090758_fixed_summary_dbquery') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20200722090758_fixed_summary_dbquery', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200811110023_added_update_in_active_directory_field') THEN
    ALTER TABLE `Accounts` ADD `UpdateInActiveDirectory` tinyint(1) NOT NULL DEFAULT FALSE;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200811110023_added_update_in_active_directory_field') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20200811110023_added_update_in_active_directory_field', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200911144338_added_change_password_when_expired_field') THEN
    ALTER TABLE `Groups` ADD `ChangePasswordWhenExpired` tinyint(1) NOT NULL DEFAULT FALSE;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200911144338_added_change_password_when_expired_field') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20200911144338_added_change_password_when_expired_field', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200921130546_added_when_changed_field') THEN
    ALTER TABLE `Employees` ADD `WhenChanged` datetime(6) NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20200921130546_added_when_changed_field') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20200921130546_added_when_changed_field', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20201008090805_updated_hw_profile_prop_names') THEN
    ALTER TABLE `HardwareVaultProfiles` RENAME COLUMN `ButtonBonding` TO `ButtonPairing`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20201008090805_updated_hw_profile_prop_names') THEN
    ALTER TABLE `HardwareVaultProfiles` RENAME COLUMN `ButtonNewChannel` TO `ButtonStorageAccess`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20201008090805_updated_hw_profile_prop_names') THEN
    ALTER TABLE `HardwareVaultProfiles` RENAME COLUMN `MasterKeyBonding` TO `MasterKeyPairing`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20201008090805_updated_hw_profile_prop_names') THEN
    ALTER TABLE `HardwareVaultProfiles` RENAME COLUMN `MasterKeyNewChannel` TO `MasterKeyStorageAccess`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20201008090805_updated_hw_profile_prop_names') THEN
    ALTER TABLE `HardwareVaultProfiles` RENAME COLUMN `PinBonding` TO `PinPairing`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20201008090805_updated_hw_profile_prop_names') THEN
    ALTER TABLE `HardwareVaultProfiles` RENAME COLUMN `PinNewChannel` TO `PinStorageAccess`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20201008090805_updated_hw_profile_prop_names') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20201008090805_updated_hw_profile_prop_names', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20201201104118_added_login_type') THEN
    ALTER TABLE `SharedAccounts` RENAME COLUMN `Kind` TO `LoginType`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20201201104118_added_login_type') THEN
    ALTER TABLE `Accounts` RENAME COLUMN `Kind` TO `LoginType`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20201201104118_added_login_type') THEN
                     UPDATE SharedAccounts
                            SET LoginType = (CASE
                                WHEN Login Like '.\\\%' THEN '1'
                                WHEN Login LIKE '@\\\%' THEN '4'
                                WHEN Login LIKE 'AzureAD\\\%' THEN '3'
                                WHEN Login LIKE '_%\\\%_' THEN '2'
                                ELSE '0' END);
                    
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20201201104118_added_login_type') THEN
                     UPDATE Accounts
                            SET LoginType = (CASE
                                WHEN Login Like '.\\\%' THEN '1'
                                WHEN Login LIKE '@\\\%' THEN '4'
                                WHEN Login LIKE 'AzureAD\\\%' THEN '3'
                                WHEN Login LIKE '_%\\\%_' THEN '2'
                                ELSE '0' END);
                    
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20201201104118_added_login_type') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20201201104118_added_login_type', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20201201144615_renamed_account_type') THEN
    ALTER TABLE `Accounts` RENAME COLUMN `Type` TO `AccountType`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20201201144615_renamed_account_type') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20201201144615_renamed_account_type', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210210150258_added_FidoStoredCredential_entity') THEN
    CREATE TABLE `FidoStoredCredential` (
        `Id` varchar(255) NOT NULL,
        `SecurityKeyName` longtext CHARACTER SET utf8mb4 NULL,
        `Username` longtext CHARACTER SET utf8mb4 NULL,
        `UserId` longblob NULL,
        `PublicKey` longblob NULL,
        `UserHandle` longblob NULL,
        `SignatureCounter` int unsigned NOT NULL,
        `CredType` longtext CHARACTER SET utf8mb4 NULL,
        `RegDate` datetime(6) NOT NULL,
        `AaGuid` char(36) NOT NULL,
        `DescriptorJson` longtext CHARACTER SET utf8mb4 NULL,
        CONSTRAINT `PK_FidoStoredCredential` PRIMARY KEY (`Id`)
    );
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210210150258_added_FidoStoredCredential_entity') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20210210150258_added_FidoStoredCredential_entity', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210224144232_updated_app_user_fields') THEN
    ALTER TABLE `AspNetUsers` DROP COLUMN `LastName`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210224144232_updated_app_user_fields') THEN
    ALTER TABLE `AspNetUsers` RENAME COLUMN `FirstName` TO `FullName`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210224144232_updated_app_user_fields') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20210224144232_updated_app_user_fields', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210407142956_rename_proximity_vault_table') THEN
    ALTER TABLE `WorkstationProximityVaults` RENAME `WorkstationHardwareVaultPairs`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210407142956_rename_proximity_vault_table') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20210407142956_rename_proximity_vault_table', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210520065650_added_firstname_and_lastname') THEN
    CREATE TABLE `SummaryByDayAndEmployee` (
        `AvgSessionsDuration` time(6) NOT NULL,
        `Company` longtext CHARACTER SET utf8mb4 NULL,
        `Date` datetime(6) NOT NULL,
        `Department` longtext CHARACTER SET utf8mb4 NULL,
        `Employee` longtext CHARACTER SET utf8mb4 NULL,
        `SessionsCount` int NOT NULL,
        `TotalSessionsDuration` time(6) NOT NULL,
        `WorkstationsCount` int NOT NULL
    ) CHARACTER SET=utf8mb4;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210520065650_added_firstname_and_lastname') THEN
    CREATE TABLE `SummaryByDepartments` (
        `AvgSessionsDuration` time(6) NOT NULL,
        `AvgTotalDuartionByEmployee` time(6) NOT NULL,
        `AvgTotalSessionsCountByEmployee` decimal(65,30) NOT NULL,
        `Company` longtext CHARACTER SET utf8mb4 NULL,
        `Department` longtext CHARACTER SET utf8mb4 NULL,
        `EmployeesCount` int NOT NULL,
        `TotalSessionsCount` int NOT NULL,
        `TotalSessionsDuration` time(6) NOT NULL,
        `WorkstationsCount` int NOT NULL
    ) CHARACTER SET=utf8mb4;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210520065650_added_firstname_and_lastname') THEN
    CREATE TABLE `SummaryByEmployees` (
        `AvgSessionsCountPerDay` decimal(65,30) NOT NULL,
        `AvgSessionsDuration` time(6) NOT NULL,
        `AvgWorkingHoursPerDay` time(6) NOT NULL,
        `Company` longtext CHARACTER SET utf8mb4 NULL,
        `Department` longtext CHARACTER SET utf8mb4 NULL,
        `Employee` longtext CHARACTER SET utf8mb4 NULL,
        `TotalSessionsCount` int NOT NULL,
        `TotalSessionsDuration` time(6) NOT NULL,
        `WorkingDaysCount` int NOT NULL,
        `WorkstationsCount` int NOT NULL
    ) CHARACTER SET=utf8mb4;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210520065650_added_firstname_and_lastname') THEN
    CREATE TABLE `SummaryByWorkstations` (
        `AvgSessionsDuration` time(6) NOT NULL,
        `AvgTotalDuartionByEmployee` time(6) NOT NULL,
        `AvgTotalSessionsCountByEmployee` decimal(65,30) NOT NULL,
        `EmployeesCount` int NOT NULL,
        `TotalSessionsCount` int NOT NULL,
        `TotalSessionsDuration` time(6) NOT NULL,
        `Workstation` longtext CHARACTER SET utf8mb4 NULL
    ) CHARACTER SET=utf8mb4;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210520065650_added_firstname_and_lastname') THEN
    DROP TABLE `SummaryByDayAndEmployee`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210520065650_added_firstname_and_lastname') THEN
    DROP TABLE `SummaryByDepartments`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210520065650_added_firstname_and_lastname') THEN
    DROP TABLE `SummaryByEmployees`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210520065650_added_firstname_and_lastname') THEN
    DROP TABLE `SummaryByWorkstations`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210520065650_added_firstname_and_lastname') THEN
    ALTER TABLE `AspNetUsers` DROP COLUMN `FullName`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210520065650_added_firstname_and_lastname') THEN
    ALTER TABLE `WorkstationSessions` MODIFY COLUMN `UserSession` longtext CHARACTER SET utf8mb4 NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210520065650_added_firstname_and_lastname') THEN
    ALTER TABLE `Workstations` MODIFY COLUMN `OS` longtext CHARACTER SET utf8mb4 NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210520065650_added_firstname_and_lastname') THEN
    ALTER TABLE `Workstations` MODIFY COLUMN `Name` longtext CHARACTER SET utf8mb4 NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210520065650_added_firstname_and_lastname') THEN
    ALTER TABLE `Workstations` MODIFY COLUMN `IP` longtext CHARACTER SET utf8mb4 NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210520065650_added_firstname_and_lastname') THEN
    ALTER TABLE `Workstations` MODIFY COLUMN `Domain` longtext CHARACTER SET utf8mb4 NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210520065650_added_firstname_and_lastname') THEN
    ALTER TABLE `Workstations` MODIFY COLUMN `ClientVersion` longtext CHARACTER SET utf8mb4 NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210520065650_added_firstname_and_lastname') THEN
    ALTER TABLE `WorkstationEvents` MODIFY COLUMN `UserSession` longtext CHARACTER SET utf8mb4 NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210520065650_added_firstname_and_lastname') THEN
    ALTER TABLE `WorkstationEvents` MODIFY COLUMN `Note` longtext CHARACTER SET utf8mb4 NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210520065650_added_firstname_and_lastname') THEN
    ALTER TABLE `Templates` MODIFY COLUMN `Urls` longtext CHARACTER SET utf8mb4 NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210520065650_added_firstname_and_lastname') THEN
    ALTER TABLE `Templates` MODIFY COLUMN `Name` longtext CHARACTER SET utf8mb4 NOT NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210520065650_added_firstname_and_lastname') THEN
    ALTER TABLE `Templates` MODIFY COLUMN `Apps` longtext CHARACTER SET utf8mb4 NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210520065650_added_firstname_and_lastname') THEN
    ALTER TABLE `SoftwareVaults` MODIFY COLUMN `OS` longtext CHARACTER SET utf8mb4 NOT NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210520065650_added_firstname_and_lastname') THEN
    ALTER TABLE `SoftwareVaults` MODIFY COLUMN `Model` longtext CHARACTER SET utf8mb4 NOT NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210520065650_added_firstname_and_lastname') THEN
    ALTER TABLE `SoftwareVaults` MODIFY COLUMN `ClientAppVersion` longtext CHARACTER SET utf8mb4 NOT NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210520065650_added_firstname_and_lastname') THEN
    ALTER TABLE `SharedAccounts` MODIFY COLUMN `Urls` longtext CHARACTER SET utf8mb4 NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210520065650_added_firstname_and_lastname') THEN
    ALTER TABLE `SharedAccounts` MODIFY COLUMN `Password` longtext CHARACTER SET utf8mb4 NOT NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210520065650_added_firstname_and_lastname') THEN
    ALTER TABLE `SharedAccounts` MODIFY COLUMN `OtpSecret` longtext CHARACTER SET utf8mb4 NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210520065650_added_firstname_and_lastname') THEN
    ALTER TABLE `SharedAccounts` MODIFY COLUMN `Name` longtext CHARACTER SET utf8mb4 NOT NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210520065650_added_firstname_and_lastname') THEN
    ALTER TABLE `SharedAccounts` MODIFY COLUMN `Login` longtext CHARACTER SET utf8mb4 NOT NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210520065650_added_firstname_and_lastname') THEN
    ALTER TABLE `SharedAccounts` MODIFY COLUMN `Apps` longtext CHARACTER SET utf8mb4 NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210520065650_added_firstname_and_lastname') THEN
    ALTER TABLE `Positions` MODIFY COLUMN `Name` longtext CHARACTER SET utf8mb4 NOT NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210520065650_added_firstname_and_lastname') THEN
    ALTER TABLE `LicenseOrders` MODIFY COLUMN `Note` longtext CHARACTER SET utf8mb4 NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210520065650_added_firstname_and_lastname') THEN
    ALTER TABLE `LicenseOrders` MODIFY COLUMN `ContactEmail` longtext CHARACTER SET utf8mb4 NOT NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210520065650_added_firstname_and_lastname') THEN
    ALTER TABLE `HardwareVaultTasks` MODIFY COLUMN `Password` longtext CHARACTER SET utf8mb4 NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210520065650_added_firstname_and_lastname') THEN
    ALTER TABLE `HardwareVaultTasks` MODIFY COLUMN `OtpSecret` longtext CHARACTER SET utf8mb4 NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210520065650_added_firstname_and_lastname') THEN
    ALTER TABLE `HardwareVaults` MODIFY COLUMN `StatusDescription` longtext CHARACTER SET utf8mb4 NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210520065650_added_firstname_and_lastname') THEN
    ALTER TABLE `HardwareVaults` MODIFY COLUMN `Model` longtext CHARACTER SET utf8mb4 NOT NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210520065650_added_firstname_and_lastname') THEN
    ALTER TABLE `HardwareVaults` MODIFY COLUMN `MasterPassword` longtext CHARACTER SET utf8mb4 NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210520065650_added_firstname_and_lastname') THEN
    ALTER TABLE `HardwareVaults` MODIFY COLUMN `Firmware` longtext CHARACTER SET utf8mb4 NOT NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210520065650_added_firstname_and_lastname') THEN
    ALTER TABLE `HardwareVaultProfiles` MODIFY COLUMN `Name` longtext CHARACTER SET utf8mb4 NOT NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210520065650_added_firstname_and_lastname') THEN
    ALTER TABLE `HardwareVaultActivations` MODIFY COLUMN `VaultId` longtext CHARACTER SET utf8mb4 NOT NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210520065650_added_firstname_and_lastname') THEN
    ALTER TABLE `HardwareVaultActivations` MODIFY COLUMN `AcivationCode` longtext CHARACTER SET utf8mb4 NOT NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210520065650_added_firstname_and_lastname') THEN
    ALTER TABLE `Groups` MODIFY COLUMN `Email` longtext CHARACTER SET utf8mb4 NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210520065650_added_firstname_and_lastname') THEN
    ALTER TABLE `Groups` MODIFY COLUMN `Description` longtext CHARACTER SET utf8mb4 NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210520065650_added_firstname_and_lastname') THEN
    ALTER TABLE `FidoStoredCredential` MODIFY COLUMN `Username` longtext CHARACTER SET utf8mb4 NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210520065650_added_firstname_and_lastname') THEN
    ALTER TABLE `FidoStoredCredential` MODIFY COLUMN `SecurityKeyName` longtext CHARACTER SET utf8mb4 NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210520065650_added_firstname_and_lastname') THEN
    ALTER TABLE `FidoStoredCredential` MODIFY COLUMN `DescriptorJson` longtext CHARACTER SET utf8mb4 NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210520065650_added_firstname_and_lastname') THEN
    ALTER TABLE `FidoStoredCredential` MODIFY COLUMN `CredType` longtext CHARACTER SET utf8mb4 NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210520065650_added_firstname_and_lastname') THEN
    ALTER TABLE `Employees` MODIFY COLUMN `PrimaryAccountId` longtext CHARACTER SET utf8mb4 NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210520065650_added_firstname_and_lastname') THEN
    ALTER TABLE `Employees` MODIFY COLUMN `PhoneNumber` longtext CHARACTER SET utf8mb4 NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210520065650_added_firstname_and_lastname') THEN
    ALTER TABLE `Employees` MODIFY COLUMN `Email` longtext CHARACTER SET utf8mb4 NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210520065650_added_firstname_and_lastname') THEN
    ALTER TABLE `Employees` MODIFY COLUMN `ActiveDirectoryGuid` longtext CHARACTER SET utf8mb4 NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210520065650_added_firstname_and_lastname') THEN
    ALTER TABLE `Departments` MODIFY COLUMN `Name` longtext CHARACTER SET utf8mb4 NOT NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210520065650_added_firstname_and_lastname') THEN
    ALTER TABLE `DataProtection` MODIFY COLUMN `Value` longtext CHARACTER SET utf8mb4 NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210520065650_added_firstname_and_lastname') THEN
    ALTER TABLE `Companies` MODIFY COLUMN `Name` longtext CHARACTER SET utf8mb4 NOT NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210520065650_added_firstname_and_lastname') THEN
    ALTER TABLE `AspNetUserTokens` MODIFY COLUMN `Value` longtext CHARACTER SET utf8mb4 NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210520065650_added_firstname_and_lastname') THEN
    ALTER TABLE `AspNetUsers` MODIFY COLUMN `SecurityStamp` longtext CHARACTER SET utf8mb4 NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210520065650_added_firstname_and_lastname') THEN
    ALTER TABLE `AspNetUsers` MODIFY COLUMN `PhoneNumber` longtext CHARACTER SET utf8mb4 NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210520065650_added_firstname_and_lastname') THEN
    ALTER TABLE `AspNetUsers` MODIFY COLUMN `PasswordHash` longtext CHARACTER SET utf8mb4 NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210520065650_added_firstname_and_lastname') THEN
    ALTER TABLE `AspNetUsers` MODIFY COLUMN `ConcurrencyStamp` longtext CHARACTER SET utf8mb4 NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210520065650_added_firstname_and_lastname') THEN
    ALTER TABLE `AspNetUsers` ADD `FirstName` longtext CHARACTER SET utf8mb4 NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210520065650_added_firstname_and_lastname') THEN
    ALTER TABLE `AspNetUsers` ADD `LastName` longtext CHARACTER SET utf8mb4 NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210520065650_added_firstname_and_lastname') THEN
    ALTER TABLE `AspNetUserLogins` MODIFY COLUMN `ProviderDisplayName` longtext CHARACTER SET utf8mb4 NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210520065650_added_firstname_and_lastname') THEN
    ALTER TABLE `AspNetUserClaims` MODIFY COLUMN `ClaimValue` longtext CHARACTER SET utf8mb4 NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210520065650_added_firstname_and_lastname') THEN
    ALTER TABLE `AspNetUserClaims` MODIFY COLUMN `ClaimType` longtext CHARACTER SET utf8mb4 NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210520065650_added_firstname_and_lastname') THEN
    ALTER TABLE `AspNetRoles` MODIFY COLUMN `ConcurrencyStamp` longtext CHARACTER SET utf8mb4 NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210520065650_added_firstname_and_lastname') THEN
    ALTER TABLE `AspNetRoleClaims` MODIFY COLUMN `ClaimValue` longtext CHARACTER SET utf8mb4 NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210520065650_added_firstname_and_lastname') THEN
    ALTER TABLE `AspNetRoleClaims` MODIFY COLUMN `ClaimType` longtext CHARACTER SET utf8mb4 NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210520065650_added_firstname_and_lastname') THEN
    ALTER TABLE `AppSettings` MODIFY COLUMN `Value` longtext CHARACTER SET utf8mb4 NOT NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210520065650_added_firstname_and_lastname') THEN
    ALTER TABLE `Accounts` MODIFY COLUMN `Urls` longtext CHARACTER SET utf8mb4 NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210520065650_added_firstname_and_lastname') THEN
    ALTER TABLE `Accounts` MODIFY COLUMN `Password` longtext CHARACTER SET utf8mb4 NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210520065650_added_firstname_and_lastname') THEN
    ALTER TABLE `Accounts` MODIFY COLUMN `OtpSecret` longtext CHARACTER SET utf8mb4 NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210520065650_added_firstname_and_lastname') THEN
    ALTER TABLE `Accounts` MODIFY COLUMN `Name` longtext CHARACTER SET utf8mb4 NOT NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210520065650_added_firstname_and_lastname') THEN
    ALTER TABLE `Accounts` MODIFY COLUMN `Login` longtext CHARACTER SET utf8mb4 NOT NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210520065650_added_firstname_and_lastname') THEN
    ALTER TABLE `Accounts` MODIFY COLUMN `Apps` longtext CHARACTER SET utf8mb4 NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210520065650_added_firstname_and_lastname') THEN
    UPDATE AspNetUsers SET FirstName = 'admin', LastName = 'hideez';
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210520065650_added_firstname_and_lastname') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20210520065650_added_firstname_and_lastname', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210521123337_added_culture_field') THEN
    ALTER TABLE `AspNetUsers` ADD `Culture` longtext CHARACTER SET utf8mb4 NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210521123337_added_culture_field') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20210521123337_added_culture_field', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210608090355_added_saml_relying_party') THEN
    CREATE TABLE `SamlRelyingParties` (
        `Id` varchar(255) CHARACTER SET utf8mb4 NOT NULL,
        `Issuer` varchar(255) CHARACTER SET utf8mb4 NOT NULL,
        `SingleSignOnDestination` longtext CHARACTER SET utf8mb4 NULL,
        `SingleLogoutResponseDestination` longtext CHARACTER SET utf8mb4 NULL,
        `SignatureValidationCertificateBase64` longtext CHARACTER SET utf8mb4 NULL,
        CONSTRAINT `PK_SamlRelyingParties` PRIMARY KEY (`Id`)
    ) CHARACTER SET=utf8mb4;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210608090355_added_saml_relying_party') THEN
    CREATE UNIQUE INDEX `IX_SamlRelyingParties_Issuer` ON `SamlRelyingParties` (`Issuer`);
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210608090355_added_saml_relying_party') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20210608090355_added_saml_relying_party', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210608143101_updated_saml_relying_party_field') THEN
    ALTER TABLE `SamlRelyingParties` MODIFY COLUMN `SingleSignOnDestination` longtext CHARACTER SET utf8mb4 NOT NULL DEFAULT ('');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210608143101_updated_saml_relying_party_field') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20210608143101_updated_saml_relying_party_field', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210630114557_added_name_identifier_format_field') THEN
    ALTER TABLE `SamlRelyingParties` ADD `NameIdentifierFormat` longtext CHARACTER SET utf8mb4 NOT NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210630114557_added_name_identifier_format_field') THEN
    UPDATE SamlRelyingParties SET NameIdentifierFormat = 'urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress';
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210630114557_added_name_identifier_format_field') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20210630114557_added_name_identifier_format_field', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210701083146_added_external_id_and_name_identifier_field') THEN
    ALTER TABLE `SamlRelyingParties` ADD `NameIdentifierField` int NOT NULL DEFAULT 0;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210701083146_added_external_id_and_name_identifier_field') THEN
    ALTER TABLE `AspNetUsers` ADD `ExternalId` longtext CHARACTER SET utf8mb4 NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210701083146_added_external_id_and_name_identifier_field') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20210701083146_added_external_id_and_name_identifier_field', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210701101222_added_employee_external_id') THEN
    ALTER TABLE `Employees` ADD `ExternalId` longtext CHARACTER SET utf8mb4 NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210701101222_added_employee_external_id') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20210701101222_added_employee_external_id', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210707084209_added_allow_passwordless_by_u2f_field') THEN
    ALTER TABLE `AspNetUsers` ADD `AllowPasswordlessByU2F` tinyint(1) NOT NULL DEFAULT FALSE;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210707084209_added_allow_passwordless_by_u2f_field') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20210707084209_added_allow_passwordless_by_u2f_field', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210719083023_added_user_auth_type') THEN
    ALTER TABLE `AspNetUsers` ADD `UserAuthType` int NOT NULL DEFAULT 0;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210719083023_added_user_auth_type') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20210719083023_added_user_auth_type', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210723095934_added_last_password_change_field') THEN
    ALTER TABLE `AspNetUsers` ADD `LastPasswordChange` datetime(6) NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210723095934_added_last_password_change_field') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20210723095934_added_last_password_change_field', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210803064053_added_required_two_factor_field') THEN
    ALTER TABLE `AspNetUsers` DROP COLUMN `UserAuthType`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210803064053_added_required_two_factor_field') THEN
    ALTER TABLE `AspNetUsers` ADD `RequiredTwoFactor` tinyint(1) NOT NULL DEFAULT FALSE;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210803064053_added_required_two_factor_field') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20210803064053_added_required_two_factor_field', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210810120711_added_different_security_key_stamp_field') THEN
    ALTER TABLE `AspNetUsers` ADD `DifferentSecurityKeyStamp` longtext CHARACTER SET utf8mb4 NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210810120711_added_different_security_key_stamp_field') THEN
    UPDATE AspNetUsers SET DifferentSecurityKeyStamp = UUID();
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210810120711_added_different_security_key_stamp_field') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20210810120711_added_different_security_key_stamp_field', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210920084047_rename_field_different_security_key_stamp') THEN
    ALTER TABLE `AspNetUsers` RENAME COLUMN `DifferentSecurityKeyStamp` TO `AdditionalSecurityStamp`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210920084047_rename_field_different_security_key_stamp') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20210920084047_rename_field_different_security_key_stamp', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210921135717_updated_software_vaults_table') THEN
    ALTER TABLE `SoftwareVaults` DROP COLUMN `HasNewLicense`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210921135717_updated_software_vaults_table') THEN
    ALTER TABLE `SoftwareVaults` DROP COLUMN `LicenseEndDate`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210921135717_updated_software_vaults_table') THEN
    ALTER TABLE `SoftwareVaults` DROP COLUMN `LicenseStatus`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210921135717_updated_software_vaults_table') THEN
    ALTER TABLE `SoftwareVaults` DROP COLUMN `Status`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210921135717_updated_software_vaults_table') THEN
    ALTER TABLE `SoftwareVaults` RENAME COLUMN `NeedSync` TO `RegistrationCompleted`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210921135717_updated_software_vaults_table') THEN
    ALTER TABLE `SoftwareVaults` RENAME COLUMN `CreatedAt` TO `RegistrationDate`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210921135717_updated_software_vaults_table') THEN
    ALTER TABLE `SoftwareVaults` MODIFY COLUMN `OS` longtext CHARACTER SET utf8mb4 NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210921135717_updated_software_vaults_table') THEN
    ALTER TABLE `SoftwareVaults` MODIFY COLUMN `Model` longtext CHARACTER SET utf8mb4 NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210921135717_updated_software_vaults_table') THEN
    ALTER TABLE `SoftwareVaults` MODIFY COLUMN `ClientAppVersion` longtext CHARACTER SET utf8mb4 NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210921135717_updated_software_vaults_table') THEN
    ALTER TABLE `SoftwareVaults` ADD `AuthenticatorKey` longtext CHARACTER SET utf8mb4 NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210921135717_updated_software_vaults_table') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20210921135717_updated_software_vaults_table', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210923094925_added_authn_stamp_field') THEN
    ALTER TABLE `AspNetUsers` ADD `AuthNStamp` longtext CHARACTER SET utf8mb4 NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210923094925_added_authn_stamp_field') THEN
    UPDATE AspNetUsers SET AuthNStamp = UUID();
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210923094925_added_authn_stamp_field') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20210923094925_added_authn_stamp_field', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210924122849_renamed_additional_stamp') THEN
    ALTER TABLE `AspNetUsers` RENAME COLUMN `AdditionalSecurityStamp` TO `RegStamp`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210924122849_renamed_additional_stamp') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20210924122849_renamed_additional_stamp', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210927084039_switched_software_vault_to_user') THEN
    ALTER TABLE `SoftwareVaults` DROP FOREIGN KEY `FK_SoftwareVaults_Employees_EmployeeId`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210927084039_switched_software_vault_to_user') THEN
    ALTER TABLE `SoftwareVaults` RENAME COLUMN `EmployeeId` TO `UserId`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210927084039_switched_software_vault_to_user') THEN
    ALTER TABLE `SoftwareVaults` RENAME INDEX `IX_SoftwareVaults_EmployeeId` TO `IX_SoftwareVaults_UserId`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210927084039_switched_software_vault_to_user') THEN
    ALTER TABLE `SoftwareVaults` MODIFY COLUMN `AuthenticatorKey` longtext CHARACTER SET utf8mb4 NOT NULL DEFAULT ('');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210927084039_switched_software_vault_to_user') THEN
    ALTER TABLE `SoftwareVaults` ADD CONSTRAINT `FK_SoftwareVaults_AspNetUsers_UserId` FOREIGN KEY (`UserId`) REFERENCES `AspNetUsers` (`Id`) ON DELETE CASCADE;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210927084039_switched_software_vault_to_user') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20210927084039_switched_software_vault_to_user', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210927131359_added_has_authenticator_field') THEN
    ALTER TABLE `AspNetUsers` ADD `HasAuthenticator` tinyint(1) NOT NULL DEFAULT FALSE;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210927131359_added_has_authenticator_field') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20210927131359_added_has_authenticator_field', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210929064843_renamed_fields_authn_reg') THEN
    ALTER TABLE `AspNetUsers` RENAME COLUMN `RegStamp` TO `RegistrationStamp`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210929064843_renamed_fields_authn_reg') THEN
    ALTER TABLE `AspNetUsers` RENAME COLUMN `AuthNStamp` TO `AuthenticationStamp`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210929064843_renamed_fields_authn_reg') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20210929064843_renamed_fields_authn_reg', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210929070447_removed_software_vaults_invitation_table') THEN
    DROP TABLE `SoftwareVaultInvitations`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20210929070447_removed_software_vaults_invitation_table') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20210929070447_removed_software_vaults_invitation_table', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20211007075936_added_fk_employee_to_aspnetuser') THEN
    ALTER TABLE `Employees` DROP INDEX `IX_Employees_FirstName_LastName`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20211007075936_added_fk_employee_to_aspnetuser') THEN
    UPDATE AspNetUsers SET FirstName = Email, LastName = '' WHERE FirstName IS NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20211007075936_added_fk_employee_to_aspnetuser') THEN
    INSERT INTO Employees (Id, FirstName, LastName, Email, PhoneNumber) SELECT Id, FirstName, LastName, Email, PhoneNumber FROM AspNetUsers WHERE Id NOT IN(SELECT Id FROM Employees);
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20211007075936_added_fk_employee_to_aspnetuser') THEN
    UPDATE Employees SET Email = UUID() WHERE Email IS NULL OR Email = '';
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20211007075936_added_fk_employee_to_aspnetuser') THEN
    INSERT INTO AspNetUsers (Id, UserName, NormalizedUserName, Email, NormalizedEmail, EmailConfirmed, PhoneNumberConfirmed, TwoFactorEnabled, LockoutEnabled, AllowPasswordlessByU2F, RequiredTwoFactor, AccessFailedCount, HasAuthenticator, SecurityStamp, ConcurrencyStamp, Culture, RegistrationStamp, AuthenticationStamp) SELECT Id, IFNULL(Email, FirstName), UPPER(IFNULL(Email, FirstName)), IFNULL(Email, FirstName), UPPER(IFNULL(Email, FirstName)), 0, 0, 0, 1, 0, 0, 0, 0, UUID(), UUID(), 'en-US', UUID(), UUID() FROM Employees WHERE Id NOT IN(SELECT Id FROM AspNetUsers);
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20211007075936_added_fk_employee_to_aspnetuser') THEN
    INSERT INTO AspNetUserRoles (UserId, RoleId) SELECT Id, (SELECT Id FROM AspNetRoles WHERE NAME = 'User') FROM AspNetUsers WHERE Id NOT IN(SELECT UserId FROM AspNetUserRoles);
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20211007075936_added_fk_employee_to_aspnetuser') THEN
    ALTER TABLE `Employees` DROP COLUMN `ExternalId`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20211007075936_added_fk_employee_to_aspnetuser') THEN
    ALTER TABLE `Employees` DROP COLUMN `WhenChanged`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20211007075936_added_fk_employee_to_aspnetuser') THEN
    ALTER TABLE `AspNetUsers` DROP COLUMN `FirstName`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20211007075936_added_fk_employee_to_aspnetuser') THEN
    ALTER TABLE `AspNetUsers` DROP COLUMN `LastName`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20211007075936_added_fk_employee_to_aspnetuser') THEN
    ALTER TABLE `Employees` MODIFY COLUMN `LastName` longtext CHARACTER SET utf8mb4 NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20211007075936_added_fk_employee_to_aspnetuser') THEN
    ALTER TABLE `Employees` MODIFY COLUMN `FirstName` longtext CHARACTER SET utf8mb4 NOT NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20211007075936_added_fk_employee_to_aspnetuser') THEN
    ALTER TABLE `Employees` MODIFY COLUMN `Email` longtext CHARACTER SET utf8mb4 NOT NULL DEFAULT ('');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20211007075936_added_fk_employee_to_aspnetuser') THEN
    ALTER TABLE `AspNetUsers` ADD `SingleSignOnEnabled` tinyint(1) NOT NULL DEFAULT FALSE;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20211007075936_added_fk_employee_to_aspnetuser') THEN
    ALTER TABLE `AspNetUsers` ADD CONSTRAINT `FK_AspNetUsers_Employees_Id` FOREIGN KEY (`Id`) REFERENCES `Employees` (`Id`) ON DELETE CASCADE;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20211007075936_added_fk_employee_to_aspnetuser') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20211007075936_added_fk_employee_to_aspnetuser', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20211011122146_removed_primary_account_id') THEN
    ALTER TABLE `Employees` DROP COLUMN `PrimaryAccountId`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20211011122146_removed_primary_account_id') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20211011122146_removed_primary_account_id', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20211020115206_added_multiple_domains') THEN
    DELETE FROM AppSettings WHERE Id = 'domain';
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20211020115206_added_multiple_domains') THEN
    ALTER TABLE `Employees` DROP COLUMN `ActiveDirectoryGuid`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20211020115206_added_multiple_domains') THEN
    ALTER TABLE `Accounts` DROP COLUMN `UpdateInActiveDirectory`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20211020115206_added_multiple_domains') THEN
    CREATE TABLE `DomainsSettings` (
        `Id` varchar(255) CHARACTER SET utf8mb4 NOT NULL,
        `Type` int NOT NULL,
        `Name` longtext CHARACTER SET utf8mb4 NULL,
        `Credentials` longtext CHARACTER SET utf8mb4 NULL,
        `EnableSso` tinyint(1) NOT NULL,
        `RequiredTwoFactor` tinyint(1) NOT NULL,
        `AllowPasswordlessByU2F` tinyint(1) NOT NULL,
        `ExternalIdField` longtext CHARACTER SET utf8mb4 NULL,
        CONSTRAINT `PK_DomainsSettings` PRIMARY KEY (`Id`)
    ) CHARACTER SET=utf8mb4;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20211020115206_added_multiple_domains') THEN
    CREATE TABLE `EmployeeDomains` (
        `Id` varchar(255) CHARACTER SET utf8mb4 NOT NULL,
        `EmployeeId` varchar(255) CHARACTER SET utf8mb4 NULL,
        `DomainSettingsId` varchar(255) CHARACTER SET utf8mb4 NULL,
        `ActiveDirectoryGuid` longtext CHARACTER SET utf8mb4 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
    ) CHARACTER SET=utf8mb4;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20211020115206_added_multiple_domains') THEN
    CREATE INDEX `IX_EmployeeDomains_DomainSettingsId` ON `EmployeeDomains` (`DomainSettingsId`);
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20211020115206_added_multiple_domains') THEN
    CREATE INDEX `IX_EmployeeDomains_EmployeeId` ON `EmployeeDomains` (`EmployeeId`);
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20211020115206_added_multiple_domains') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20211020115206_added_multiple_domains', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20211027092103_added_hardware_vault_backups') THEN
    ALTER TABLE `HardwareVaultTasks` ADD `HardwareVaultBackupId` varchar(255) CHARACTER SET utf8mb4 NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20211027092103_added_hardware_vault_backups') THEN
    CREATE TABLE `HardwareVaultBackups` (
        `Id` varchar(255) CHARACTER SET utf8mb4 NOT NULL,
        `EmployeeId` varchar(255) CHARACTER SET utf8mb4 NOT NULL,
        `HardwareVaultId` longtext CHARACTER SET utf8mb4 NOT NULL,
        `Password` longtext CHARACTER SET utf8mb4 NOT NULL,
        `Accounts` longtext CHARACTER SET utf8mb4 NOT NULL,
        `Data` longblob NOT NULL,
        `StorageUpdateCounter` tinyint unsigned NOT NULL,
        `CreatedAt` datetime(6) NOT NULL,
        `DurationOfCreation` time(6) NOT NULL,
        CONSTRAINT `PK_HardwareVaultBackups` PRIMARY KEY (`Id`),
        CONSTRAINT `FK_HardwareVaultBackups_Employees_EmployeeId` FOREIGN KEY (`EmployeeId`) REFERENCES `Employees` (`Id`) ON DELETE CASCADE
    ) CHARACTER SET=utf8mb4;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20211027092103_added_hardware_vault_backups') THEN
    CREATE INDEX `IX_HardwareVaultTasks_HardwareVaultBackupId` ON `HardwareVaultTasks` (`HardwareVaultBackupId`);
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20211027092103_added_hardware_vault_backups') THEN
    CREATE INDEX `IX_HardwareVaultBackups_EmployeeId` ON `HardwareVaultBackups` (`EmployeeId`);
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20211027092103_added_hardware_vault_backups') THEN
    ALTER TABLE `HardwareVaultTasks` ADD CONSTRAINT `FK_HardwareVaultTasks_HardwareVaultBackups_HardwareVaultBackupId` FOREIGN KEY (`HardwareVaultBackupId`) REFERENCES `HardwareVaultBackups` (`Id`) ON DELETE CASCADE;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20211027092103_added_hardware_vault_backups') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20211027092103_added_hardware_vault_backups', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20211028134834_removed_need_sync') THEN
    ALTER TABLE `HardwareVaults` DROP COLUMN `NeedSync`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20211028134834_removed_need_sync') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20211028134834_removed_need_sync', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20211101123140_remove_accounts_soft_delete') THEN
    ALTER TABLE `WorkstationEvents` ADD `AccountName` longtext CHARACTER SET utf8mb4 NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20211101123140_remove_accounts_soft_delete') THEN
    ALTER TABLE `WorkstationEvents` ADD `AccountType` int NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20211101123140_remove_accounts_soft_delete') THEN
    ALTER TABLE `WorkstationEvents` ADD `LoginType` int NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20211101123140_remove_accounts_soft_delete') THEN
    UPDATE WorkstationEvents AS we, Accounts AS a SET we.AccountName = a.Name, we.AccountType = a.AccountType, we.LoginType = a.LoginType WHERE  we.AccountId = a.Id;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20211101123140_remove_accounts_soft_delete') THEN
    ALTER TABLE `WorkstationEvents` DROP FOREIGN KEY `FK_WorkstationEvents_Accounts_AccountId`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20211101123140_remove_accounts_soft_delete') THEN
    ALTER TABLE `WorkstationEvents` DROP INDEX `IX_WorkstationEvents_AccountId`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20211101123140_remove_accounts_soft_delete') THEN
    ALTER TABLE `WorkstationEvents` DROP COLUMN `AccountId`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20211101123140_remove_accounts_soft_delete') THEN
    ALTER TABLE `WorkstationSessions` ADD `AccountName` longtext CHARACTER SET utf8mb4 NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20211101123140_remove_accounts_soft_delete') THEN
    ALTER TABLE `WorkstationSessions` ADD `AccountType` int NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20211101123140_remove_accounts_soft_delete') THEN
    ALTER TABLE `WorkstationSessions` ADD `LoginType` int NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20211101123140_remove_accounts_soft_delete') THEN
    UPDATE WorkstationSessions AS we, Accounts AS a SET we.AccountName = a.Name, we.AccountType = a.AccountType, we.LoginType = a.LoginType WHERE  we.AccountId = a.Id;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20211101123140_remove_accounts_soft_delete') THEN
    ALTER TABLE `WorkstationSessions` DROP FOREIGN KEY `FK_WorkstationSessions_Accounts_AccountId`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20211101123140_remove_accounts_soft_delete') THEN
    ALTER TABLE `WorkstationSessions` DROP INDEX `IX_WorkstationSessions_AccountId`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20211101123140_remove_accounts_soft_delete') THEN
    ALTER TABLE `WorkstationSessions` DROP COLUMN `AccountId`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20211101123140_remove_accounts_soft_delete') THEN
    ALTER TABLE `HardwareVaultTasks` ADD `AccountForUnlock` tinyint(1) NOT NULL DEFAULT FALSE;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20211101123140_remove_accounts_soft_delete') THEN
    ALTER TABLE `HardwareVaultTasks` ADD `AccountStorageId` longblob NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20211101123140_remove_accounts_soft_delete') THEN
    ALTER TABLE `HardwareVaultTasks` DROP FOREIGN KEY `FK_HardwareVaultTasks_Accounts_AccountId`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20211101123140_remove_accounts_soft_delete') THEN
    ALTER TABLE `HardwareVaultTasks` ADD CONSTRAINT `FK_HardwareVaultTasks_Accounts_AccountId` FOREIGN KEY (`AccountId`) REFERENCES `Accounts` (`Id`) ON DELETE CASCADE;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20211101123140_remove_accounts_soft_delete') THEN
    UPDATE HardwareVaultTasks AS t, Accounts AS a SET t.AccountForUnlock = IF(a.LoginType = 0, 0, 1), t.AccountStorageId = a.StorageId WHERE t.AccountId = a.Id;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20211101123140_remove_accounts_soft_delete') THEN
    UPDATE HardwareVaultTasks SET AccountId = NULL WHERE Operation = 3;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20211101123140_remove_accounts_soft_delete') THEN
    DELETE FROM Accounts WHERE Deleted = 1;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20211101123140_remove_accounts_soft_delete') THEN
    ALTER TABLE `Accounts` DROP COLUMN `Deleted`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20211101123140_remove_accounts_soft_delete') THEN
    DELETE FROM SharedAccounts WHERE Deleted = 1;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20211101123140_remove_accounts_soft_delete') THEN
    ALTER TABLE `SharedAccounts` DROP COLUMN `Deleted`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20211101123140_remove_accounts_soft_delete') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20211101123140_remove_accounts_soft_delete', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20211122124953_added_employees_license') THEN
    CREATE TABLE `EmployeesLicenseOrders` (
        `Id` varchar(255) CHARACTER SET utf8mb4 NOT NULL,
        `ContactEmail` longtext CHARACTER SET utf8mb4 NOT NULL,
        `Note` longtext CHARACTER SET utf8mb4 NULL,
        `EmployeesCount` int NOT NULL,
        `EndDate` datetime(6) NOT NULL,
        `CreatedAt` datetime(6) NOT NULL,
        `OrderStatus` int NOT NULL,
        CONSTRAINT `PK_EmployeesLicenseOrders` PRIMARY KEY (`Id`)
    ) CHARACTER SET=utf8mb4;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20211122124953_added_employees_license') THEN
    CREATE TABLE `EmployeesLicenses` (
        `Id` varchar(255) CHARACTER SET utf8mb4 NOT NULL,
        `EmployeesLicenseOrderId` varchar(255) CHARACTER SET utf8mb4 NULL,
        `ImportedAt` datetime(6) NOT NULL,
        `Data` longtext CHARACTER SET utf8mb4 NULL,
        `SignedData` longtext CHARACTER SET utf8mb4 NULL,
        CONSTRAINT `PK_EmployeesLicenses` PRIMARY KEY (`Id`),
        CONSTRAINT `FK_EmployeesLicenses_EmployeesLicenseOrders_EmployeesLicenseOrd~` FOREIGN KEY (`EmployeesLicenseOrderId`) REFERENCES `EmployeesLicenseOrders` (`Id`) ON DELETE CASCADE
    ) CHARACTER SET=utf8mb4;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20211122124953_added_employees_license') THEN
    CREATE UNIQUE INDEX `IX_EmployeesLicenses_EmployeesLicenseOrderId` ON `EmployeesLicenses` (`EmployeesLicenseOrderId`);
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20211122124953_added_employees_license') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20211122124953_added_employees_license', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20211124084123_renamed_license_orders') THEN
    ALTER TABLE `HardwareVaultLicenses` DROP FOREIGN KEY `FK_HardwareVaultLicenses_LicenseOrders_LicenseOrderId`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20211124084123_renamed_license_orders') THEN
    ALTER TABLE `LicenseOrders` RENAME `HardwareVaultLicenseOrders`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20211124084123_renamed_license_orders') THEN
    ALTER TABLE `HardwareVaultLicenses` ADD CONSTRAINT `FK_HardwareVaultLicenses_HardwareVaultLicenseOrders_LicenseOrde~` FOREIGN KEY (`LicenseOrderId`) REFERENCES `HardwareVaultLicenseOrders` (`Id`) ON DELETE CASCADE;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20211124084123_renamed_license_orders') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20211124084123_renamed_license_orders', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20211208155003_renamed_signeddata_field') THEN
    ALTER TABLE `EmployeesLicenses` RENAME COLUMN `SignedData` TO `Signature`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20211208155003_renamed_signeddata_field') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20211208155003_renamed_signeddata_field', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20220111140355_added_registered_created_fields') THEN
    ALTER TABLE `Workstations` ADD `RegisteredAt` datetime(6) NOT NULL DEFAULT '0001-01-01 00:00:00';
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20220111140355_added_registered_created_fields') THEN
    UPDATE Workstations AS w SET w.RegisteredAt = UTC_TIMESTAMP();
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20220111140355_added_registered_created_fields') THEN
    ALTER TABLE `WorkstationHardwareVaultPairs` ADD `CreatedAt` datetime(6) NOT NULL DEFAULT '0001-01-01 00:00:00';
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20220111140355_added_registered_created_fields') THEN
    UPDATE WorkstationHardwareVaultPairs AS w SET w.CreatedAt = UTC_TIMESTAMP();
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20220111140355_added_registered_created_fields') THEN
    ALTER TABLE `Templates` ADD `CreatedAt` datetime(6) NOT NULL DEFAULT '0001-01-01 00:00:00';
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20220111140355_added_registered_created_fields') THEN
    UPDATE Templates AS t SET t.CreatedAt = UTC_TIMESTAMP();
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20220111140355_added_registered_created_fields') THEN
    ALTER TABLE `SharedAccounts` ADD `CreatedAt` datetime(6) NOT NULL DEFAULT '0001-01-01 00:00:00';
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20220111140355_added_registered_created_fields') THEN
    UPDATE SharedAccounts AS t SET t.CreatedAt = UTC_TIMESTAMP();
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20220111140355_added_registered_created_fields') THEN
    ALTER TABLE `Employees` ADD `RegisteredAt` datetime(6) NOT NULL DEFAULT '0001-01-01 00:00:00';
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20220111140355_added_registered_created_fields') THEN
    UPDATE Employees AS e SET e.RegisteredAt = UTC_TIMESTAMP();
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20220111140355_added_registered_created_fields') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20220111140355_added_registered_created_fields', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20220201110814_added_openiddict') THEN
    CREATE TABLE `OpenIddictApplications` (
        `Id` varchar(255) CHARACTER SET utf8mb4 NOT NULL,
        `ClientId` varchar(100) CHARACTER SET utf8mb4 NULL,
        `ClientSecret` longtext CHARACTER SET utf8mb4 NULL,
        `ConcurrencyToken` varchar(50) CHARACTER SET utf8mb4 NULL,
        `ConsentType` varchar(50) CHARACTER SET utf8mb4 NULL,
        `DisplayName` longtext CHARACTER SET utf8mb4 NULL,
        `DisplayNames` longtext CHARACTER SET utf8mb4 NULL,
        `Permissions` longtext CHARACTER SET utf8mb4 NULL,
        `PostLogoutRedirectUris` longtext CHARACTER SET utf8mb4 NULL,
        `Properties` longtext CHARACTER SET utf8mb4 NULL,
        `RedirectUris` longtext CHARACTER SET utf8mb4 NULL,
        `Requirements` longtext CHARACTER SET utf8mb4 NULL,
        `Type` varchar(50) CHARACTER SET utf8mb4 NULL,
        CONSTRAINT `PK_OpenIddictApplications` PRIMARY KEY (`Id`)
    ) CHARACTER SET=utf8mb4;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20220201110814_added_openiddict') THEN
    CREATE TABLE `OpenIddictScopes` (
        `Id` varchar(255) CHARACTER SET utf8mb4 NOT NULL,
        `ConcurrencyToken` varchar(50) CHARACTER SET utf8mb4 NULL,
        `Description` longtext CHARACTER SET utf8mb4 NULL,
        `Descriptions` longtext CHARACTER SET utf8mb4 NULL,
        `DisplayName` longtext CHARACTER SET utf8mb4 NULL,
        `DisplayNames` longtext CHARACTER SET utf8mb4 NULL,
        `Name` varchar(200) CHARACTER SET utf8mb4 NULL,
        `Properties` longtext CHARACTER SET utf8mb4 NULL,
        `Resources` longtext CHARACTER SET utf8mb4 NULL,
        CONSTRAINT `PK_OpenIddictScopes` PRIMARY KEY (`Id`)
    ) CHARACTER SET=utf8mb4;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20220201110814_added_openiddict') THEN
    CREATE TABLE `OpenIddictAuthorizations` (
        `Id` varchar(255) CHARACTER SET utf8mb4 NOT NULL,
        `ApplicationId` varchar(255) CHARACTER SET utf8mb4 NULL,
        `ConcurrencyToken` varchar(50) CHARACTER SET utf8mb4 NULL,
        `CreationDate` datetime(6) NULL,
        `Properties` longtext CHARACTER SET utf8mb4 NULL,
        `Scopes` longtext CHARACTER SET utf8mb4 NULL,
        `Status` varchar(50) CHARACTER SET utf8mb4 NULL,
        `Subject` varchar(400) CHARACTER SET utf8mb4 NULL,
        `Type` varchar(50) CHARACTER SET utf8mb4 NULL,
        CONSTRAINT `PK_OpenIddictAuthorizations` PRIMARY KEY (`Id`),
        CONSTRAINT `FK_OpenIddictAuthorizations_OpenIddictApplications_ApplicationId` FOREIGN KEY (`ApplicationId`) REFERENCES `OpenIddictApplications` (`Id`) ON DELETE RESTRICT
    ) CHARACTER SET=utf8mb4;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20220201110814_added_openiddict') THEN
    CREATE TABLE `OpenIddictTokens` (
        `Id` varchar(255) CHARACTER SET utf8mb4 NOT NULL,
        `ApplicationId` varchar(255) CHARACTER SET utf8mb4 NULL,
        `AuthorizationId` varchar(255) CHARACTER SET utf8mb4 NULL,
        `ConcurrencyToken` varchar(50) CHARACTER SET utf8mb4 NULL,
        `CreationDate` datetime(6) NULL,
        `ExpirationDate` datetime(6) NULL,
        `Payload` longtext CHARACTER SET utf8mb4 NULL,
        `Properties` longtext CHARACTER SET utf8mb4 NULL,
        `RedemptionDate` datetime(6) NULL,
        `ReferenceId` varchar(100) CHARACTER SET utf8mb4 NULL,
        `Status` varchar(50) CHARACTER SET utf8mb4 NULL,
        `Subject` varchar(400) CHARACTER SET utf8mb4 NULL,
        `Type` varchar(50) CHARACTER SET utf8mb4 NULL,
        CONSTRAINT `PK_OpenIddictTokens` PRIMARY KEY (`Id`),
        CONSTRAINT `FK_OpenIddictTokens_OpenIddictApplications_ApplicationId` FOREIGN KEY (`ApplicationId`) REFERENCES `OpenIddictApplications` (`Id`) ON DELETE RESTRICT,
        CONSTRAINT `FK_OpenIddictTokens_OpenIddictAuthorizations_AuthorizationId` FOREIGN KEY (`AuthorizationId`) REFERENCES `OpenIddictAuthorizations` (`Id`) ON DELETE RESTRICT
    ) CHARACTER SET=utf8mb4;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20220201110814_added_openiddict') THEN
    CREATE UNIQUE INDEX `IX_OpenIddictApplications_ClientId` ON `OpenIddictApplications` (`ClientId`);
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20220201110814_added_openiddict') THEN
    CREATE INDEX `IX_OpenIddictAuthorizations_ApplicationId_Status_Subject_Type` ON `OpenIddictAuthorizations` (`ApplicationId`, `Status`, `Subject`, `Type`);
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20220201110814_added_openiddict') THEN
    CREATE UNIQUE INDEX `IX_OpenIddictScopes_Name` ON `OpenIddictScopes` (`Name`);
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20220201110814_added_openiddict') THEN
    CREATE INDEX `IX_OpenIddictTokens_ApplicationId_Status_Subject_Type` ON `OpenIddictTokens` (`ApplicationId`, `Status`, `Subject`, `Type`);
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20220201110814_added_openiddict') THEN
    CREATE INDEX `IX_OpenIddictTokens_AuthorizationId` ON `OpenIddictTokens` (`AuthorizationId`);
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20220201110814_added_openiddict') THEN
    CREATE UNIQUE INDEX `IX_OpenIddictTokens_ReferenceId` ON `OpenIddictTokens` (`ReferenceId`);
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20220201110814_added_openiddict') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20220201110814_added_openiddict', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20220214135903_added_workstation_passwordless_logon_fields') THEN
    ALTER TABLE `DomainsSettings` ADD `CertificateConfig` longtext CHARACTER SET utf8mb4 NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20220214135903_added_workstation_passwordless_logon_fields') THEN
    ALTER TABLE `DomainsSettings` ADD `CertificateTemplateId` longtext CHARACTER SET utf8mb4 NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20220214135903_added_workstation_passwordless_logon_fields') THEN
    ALTER TABLE `DomainsSettings` ADD `CspName` longtext CHARACTER SET utf8mb4 NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20220214135903_added_workstation_passwordless_logon_fields') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20220214135903_added_workstation_passwordless_logon_fields', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20220222141750_persist_keys_to_db_context') THEN
    CREATE TABLE `DataProtectionKeys` (
        `Id` int NOT NULL AUTO_INCREMENT,
        `FriendlyName` longtext CHARACTER SET utf8mb4 NULL,
        `Xml` longtext CHARACTER SET utf8mb4 NULL,
        CONSTRAINT `PK_DataProtectionKeys` PRIMARY KEY (`Id`)
    ) CHARACTER SET=utf8mb4;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20220222141750_persist_keys_to_db_context') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20220222141750_persist_keys_to_db_context', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20220323093643_removed_hardware_vault_backup_cascade_delete') THEN
    ALTER TABLE `HardwareVaultTasks` DROP FOREIGN KEY `FK_HardwareVaultTasks_HardwareVaultBackups_HardwareVaultBackupId`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20220323093643_removed_hardware_vault_backup_cascade_delete') THEN
    ALTER TABLE `HardwareVaultTasks` ADD CONSTRAINT `FK_HardwareVaultTasks_HardwareVaultBackups_HardwareVaultBackupId` FOREIGN KEY (`HardwareVaultBackupId`) REFERENCES `HardwareVaultBackups` (`Id`) ON DELETE RESTRICT;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20220323093643_removed_hardware_vault_backup_cascade_delete') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20220323093643_removed_hardware_vault_backup_cascade_delete', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20220428140050_added_fido2_key_enrollments') THEN
    CREATE TABLE `Fido2KeyEnrollments` (
        `Id` varchar(255) CHARACTER SET utf8mb4 NOT NULL,
        `SerialNumber` longtext CHARACTER SET utf8mb4 NOT NULL,
        `KeyHandle` longtext CHARACTER SET utf8mb4 NOT NULL,
        `PublicKey` longtext CHARACTER SET utf8mb4 NOT NULL,
        `PIN` longtext CHARACTER SET utf8mb4 NOT NULL,
        `IsEnrolled` tinyint(1) NOT NULL,
        `AllowRequestPIN` tinyint(1) NOT NULL,
        `EmployeeId` varchar(255) CHARACTER SET utf8mb4 NULL,
        CONSTRAINT `PK_Fido2KeyEnrollments` PRIMARY KEY (`Id`),
        CONSTRAINT `FK_Fido2KeyEnrollments_Employees_EmployeeId` FOREIGN KEY (`EmployeeId`) REFERENCES `Employees` (`Id`) ON DELETE SET NULL
    ) CHARACTER SET=utf8mb4;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20220428140050_added_fido2_key_enrollments') THEN
    CREATE TABLE `Fido2Metadatas` (
        `Id` varchar(255) CHARACTER SET utf8mb4 NOT NULL,
        `Metadata` longtext CHARACTER SET utf8mb4 NOT NULL,
        CONSTRAINT `PK_Fido2Metadatas` PRIMARY KEY (`Id`)
    ) CHARACTER SET=utf8mb4;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20220428140050_added_fido2_key_enrollments') THEN
    CREATE INDEX `IX_Fido2KeyEnrollments_EmployeeId` ON `Fido2KeyEnrollments` (`EmployeeId`);
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20220428140050_added_fido2_key_enrollments') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20220428140050_added_fido2_key_enrollments', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20220429131832_added_fido2_authenticator_attachment') THEN
    ALTER TABLE `FidoStoredCredential` RENAME COLUMN `RegDate` TO `RegistrationDate`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20220429131832_added_fido2_authenticator_attachment') THEN
    ALTER TABLE `FidoStoredCredential` ADD `AuthenticatorAttachment` int NOT NULL DEFAULT 1;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20220429131832_added_fido2_authenticator_attachment') THEN
    ALTER TABLE `FidoStoredCredential` RENAME `Fido2StoredCredentials`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20220429131832_added_fido2_authenticator_attachment') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20220429131832_added_fido2_authenticator_attachment', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20220602201036_updated_data_protection') THEN
    DROP TABLE `DataProtection`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20220602201036_updated_data_protection') THEN
    CREATE TABLE `DataProtection` (
        `Id` varchar(255) CHARACTER SET utf8mb4 NOT NULL,
        `Value` longtext CHARACTER SET utf8mb4 NULL,
        `CreatedAt` datetime(6) NOT NULL,
        CONSTRAINT `PK_DataProtection` PRIMARY KEY (`Id`)
    ) CHARACTER SET=utf8mb4;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20220602201036_updated_data_protection') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20220602201036_updated_data_protection', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20220606114614_added_allowed_workstations_field') THEN
    ALTER TABLE `SharedAccounts` ADD `AllowedWorkstations` longtext CHARACTER SET utf8mb4 NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20220606114614_added_allowed_workstations_field') THEN
    ALTER TABLE `Accounts` ADD `AllowedWorkstations` longtext CHARACTER SET utf8mb4 NULL;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20220606114614_added_allowed_workstations_field') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20220606114614_added_allowed_workstations_field', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20220629092459_removed_mds2_metadata_table') THEN
    DROP TABLE `Fido2Metadatas`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript;
 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20220629092459_removed_mds2_metadata_table') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20220629092459_removed_mds2_metadata_table', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;
DROP PROCEDURE `POMELO_BEFORE_DROP_PRIMARY_KEY`;
DROP PROCEDURE `POMELO_AFTER_ADD_PRIMARY_KEY`;

START TRANSACTION;
DROP PROCEDURE IF EXISTS MigrationsScript; 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20190805123858_init') THEN
    DELETE FROM `__EFMigrationsHistory`;
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
DROP PROCEDURE IF EXISTS MigrationsScript; 
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20220706120010_Initial') THEN
    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20220706120010_Initial', '6.0.4');
    END IF;
END 
 ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;
COMMIT;