diff --git a/OutOfSchool/OutOfSchool.IdentityServer/Data/Migrations/OutOfSchoolMigrations/20230127121701_AddCodeficatorParentsTable.cs b/OutOfSchool/OutOfSchool.IdentityServer/Data/Migrations/OutOfSchoolMigrations/20230127121701_AddCodeficatorParentsTable.cs index a84ec761bf..9d35c942ed 100644 --- a/OutOfSchool/OutOfSchool.IdentityServer/Data/Migrations/OutOfSchoolMigrations/20230127121701_AddCodeficatorParentsTable.cs +++ b/OutOfSchool/OutOfSchool.IdentityServer/Data/Migrations/OutOfSchoolMigrations/20230127121701_AddCodeficatorParentsTable.cs @@ -43,97 +43,99 @@ protected override void Up(MigrationBuilder migrationBuilder) table: "CodeficatorParents", column: "ParentId"); - migrationBuilder.Sql(@" - DROP TRIGGER IF EXISTS catottgs_AFTER_DELETE; - - CREATE DEFINER = CURRENT_USER TRIGGER catottgs_AFTER_DELETE AFTER DELETE ON CATOTTGs FOR EACH ROW - BEGIN - DELETE FROM CodeficatorParents WHERE CatottgsId = old.Id; - DELETE FROM CodeficatorParents WHERE ParentId = old.Id; - END;"); - - migrationBuilder.Sql(@" - DROP TRIGGER IF EXISTS catottgs_AFTER_INSERT; - - CREATE DEFINER = CURRENT_USER TRIGGER catottgs_AFTER_INSERT AFTER INSERT ON CATOTTGs FOR EACH ROW - BEGIN - INSERT INTO CodeficatorParents (CatottgsId, ParentId, Level)( - WITH recursive parent_users (Id, Parentid, Level) AS ( - SELECT new.Id, new.ParentId, 1 Level - UNION ALL - SELECT t.Id, t.ParentId, Level + 1 - FROM CATOTTGs t INNER JOIN parent_users pu - ON t.Id = pu.ParentId - ) - SELECT new.Id, ParentId, Level FROM parent_users WHERE ParentId IS NOT NULL); - END;"); - - migrationBuilder.Sql(@" - DROP TRIGGER IF EXISTS catottgs_AFTER_UPDATE; - - CREATE DEFINER = CURRENT_USER TRIGGER catottgs_AFTER_UPDATE AFTER UPDATE ON CATOTTGs FOR EACH ROW - BEGIN - IF (new.ParentId <> old.ParentId OR new.Id <> old.Id) THEN - DELETE - c1 - FROM - CodeficatorParents c1 - INNER JOIN (SELECT CatottgsId FROM CodeficatorParents WHERE ParentId = old.Id - - UNION - - SELECT new.Id - - UNION - - SELECT old.Id) c2 - ON c2.CatottgsId = c1.CatottgsId; - - INSERT INTO CodeficatorParents (CatottgsId, ParentId, Level)( - WITH recursive parent_users (Id, ParentId, Level) AS ( - SELECT Id, ParentId, 1 Level - FROM CATOTTGs t - WHERE Id IN (SELECT CatottgsId From CodeficatorParents WHERE ParentId = old.Id - - UNION - - SELECT new.Id - - UNION - - SELECT old.Id) - UNION ALL - SELECT pu.Id, t.ParentId, Level + 1 - FROM CATOTTGs t INNER JOIN parent_users pu - ON t.Id = pu.ParentId - ) - SELECT Id, ParentId, Level FROM parent_users WHERE ParentId IS NOT NULL); - END IF; - END;"); - - migrationBuilder.Sql(@" - DELETE FROM CodeficatorParents; - - INSERT INTO CodeficatorParents (CatottgsId, ParentId, Level)( - WITH RECURSIVE parent_users (Id, ParentId, Level) AS ( - SELECT Id, ParentId, 1 Level - FROM CATOTTGs t - UNION ALL - SELECT pu.id, t.parentid, level + 1 - FROM CATOTTGs t INNER JOIN parent_users pu - ON t.Id = pu.ParentId - ) - SELECT Id, ParentId, Level FROM parent_users WHERE ParentId IS NOT NULL);"); + // TODO: Commenting creation of triggers. We try to make another mechanism of working with CATOTTG's parents. + + // migrationBuilder.Sql(@" + // DROP TRIGGER IF EXISTS catottgs_AFTER_DELETE; + + // CREATE DEFINER = CURRENT_USER TRIGGER catottgs_AFTER_DELETE AFTER DELETE ON CATOTTGs FOR EACH ROW + // BEGIN + // DELETE FROM CodeficatorParents WHERE CatottgsId = old.Id; + // DELETE FROM CodeficatorParents WHERE ParentId = old.Id; + // END;"); + + // migrationBuilder.Sql(@" + //DROP TRIGGER IF EXISTS catottgs_AFTER_INSERT; + + // CREATE DEFINER = CURRENT_USER TRIGGER catottgs_AFTER_INSERT AFTER INSERT ON CATOTTGs FOR EACH ROW + // BEGIN + // INSERT INTO CodeficatorParents (CatottgsId, ParentId, Level)( + // WITH recursive parent_users (Id, Parentid, Level) AS ( + // SELECT new.Id, new.ParentId, 1 Level + // UNION ALL + // SELECT t.Id, t.ParentId, Level + 1 + // FROM CATOTTGs t INNER JOIN parent_users pu + // ON t.Id = pu.ParentId + // ) + // SELECT new.Id, ParentId, Level FROM parent_users WHERE ParentId IS NOT NULL); + // END;"); + + // migrationBuilder.Sql(@" + //DROP TRIGGER IF EXISTS catottgs_AFTER_UPDATE; + + // CREATE DEFINER = CURRENT_USER TRIGGER catottgs_AFTER_UPDATE AFTER UPDATE ON CATOTTGs FOR EACH ROW + // BEGIN + // IF (new.ParentId <> old.ParentId OR new.Id <> old.Id) THEN + // DELETE + // c1 + // FROM + // CodeficatorParents c1 + // INNER JOIN (SELECT CatottgsId FROM CodeficatorParents WHERE ParentId = old.Id + + // UNION + + // SELECT new.Id + + // UNION + + // SELECT old.Id) c2 + // ON c2.CatottgsId = c1.CatottgsId; + + // INSERT INTO CodeficatorParents (CatottgsId, ParentId, Level)( + // WITH recursive parent_users (Id, ParentId, Level) AS ( + // SELECT Id, ParentId, 1 Level + // FROM CATOTTGs t + // WHERE Id IN (SELECT CatottgsId From CodeficatorParents WHERE ParentId = old.Id + + // UNION + + // SELECT new.Id + + // UNION + + // SELECT old.Id) + // UNION ALL + // SELECT pu.Id, t.ParentId, Level + 1 + // FROM CATOTTGs t INNER JOIN parent_users pu + // ON t.Id = pu.ParentId + // ) + // SELECT Id, ParentId, Level FROM parent_users WHERE ParentId IS NOT NULL); + // END IF; + // END;"); + + // migrationBuilder.Sql(@" + //DELETE FROM CodeficatorParents; + + // INSERT INTO CodeficatorParents (CatottgsId, ParentId, Level)( + // WITH RECURSIVE parent_users (Id, ParentId, Level) AS ( + // SELECT Id, ParentId, 1 Level + // FROM CATOTTGs t + // UNION ALL + // SELECT pu.id, t.parentid, level + 1 + // FROM CATOTTGs t INNER JOIN parent_users pu + // ON t.Id = pu.ParentId + // ) + // SELECT Id, ParentId, Level FROM parent_users WHERE ParentId IS NOT NULL);"); } protected override void Down(MigrationBuilder migrationBuilder) { - migrationBuilder.Sql(@" - DROP TRIGGER IF EXISTS catottgs_AFTER_UPDATE; + // migrationBuilder.Sql(@" + //DROP TRIGGER IF EXISTS catottgs_AFTER_UPDATE; - DROP TRIGGER IF EXISTS catottgs_AFTER_INSERT; + // DROP TRIGGER IF EXISTS catottgs_AFTER_INSERT; - DROP TRIGGER IF EXISTS catottgs_AFTER_DELETE;"); + // DROP TRIGGER IF EXISTS catottgs_AFTER_DELETE;"); migrationBuilder.DropTable( name: "CodeficatorParents");