Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Romanchuk / Fix trigger's migration #999

Merged
merged 1 commit into from
Feb 10, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down