From 2de195e88ee0f2c4d44360459778a1f2dc20866d Mon Sep 17 00:00:00 2001
From: Quazgar <quazgar@posteo.de>
Date: Tue, 18 Aug 2020 07:31:10 +0000
Subject: [PATCH] FIX: Deleting child of 3 generations works now.

For issue #21
---
 CHANGELOG.md                  |  1 +
 procedures/deleteIsaCache.sql |  5 +--
 tests/test_issues.sql         | 64 +++++++++++++++++++++++++++++++++++
 3 files changed, 68 insertions(+), 2 deletions(-)
 create mode 100644 tests/test_issues.sql

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 4dedd48..bd3c231 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -58,5 +58,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
 * Fixed several bugs when an Entity inherits from itself (#18, caosdb-server #85).
 * Bug in `updateEntity.sql` (when updating the primary name without a prior call
   to `deleteEntityProperties`). Same thing for `deleteEntity`.
+* #21 Bug which prevented deletion of deeply inheriting entities, if versioning was enabled.
 
 ### Security ###
diff --git a/procedures/deleteIsaCache.sql b/procedures/deleteIsaCache.sql
index f37c3fc..c0fc831 100644
--- a/procedures/deleteIsaCache.sql
+++ b/procedures/deleteIsaCache.sql
@@ -29,7 +29,8 @@ DELIMITER //
 
 /* Delete "is a" relations from the given entity towards ancestors.
 
-Note that relations towards descendants are not deleted (they probably should have been deleted before).
+Note that relations towards descendants are not deleted (they probably should have been deleted
+before).
 
 After this procedure, there are no more entries in `isa_cache`, where the
 parameter entity is a child or inside the rpath.
@@ -54,7 +55,7 @@ BEGIN
             SELECT e.child, IVersion AS child_iversion, e.parent
             FROM isa_cache AS e
             WHERE e.child = EntityID
-            AND e.rpath = EntityID;
+            AND e.rpath = CAST(EntityID AS CHAR);
     END IF;
 
     DELETE FROM isa_cache
diff --git a/tests/test_issues.sql b/tests/test_issues.sql
new file mode 100644
index 0000000..ab64de8
--- /dev/null
+++ b/tests/test_issues.sql
@@ -0,0 +1,64 @@
+/**
+ * ** header v3.0
+ * This file is a part of the CaosDB Project.
+ *
+ * Copyright (C) 2020 IndiScale GmbH <info@indiscale.com>
+ * Copyright (C) 2020 Daniel Hornung <d.hornung@indiscale.com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ *
+ * ** end header
+ */
+
+USE _caosdb_schema_unit_tests;
+BEGIN;
+CALL tap.no_plan();
+
+
+-- ########################################################################
+-- TEST Issues from https://gitlab.com/caosdb/caosdb-mysqlbackend/-/issues
+-- ########################################################################
+
+-------------------------------------------------------------------------------
+--                                  Issue 21                                 --
+-- Deleting a child with 3 levels of ancestors.                              --
+-------------------------------------------------------------------------------
+
+-- Setup
+SET @SRID = "SRID_issue_21";
+INSERT INTO transactions (srid,seconds,nanos,username,realm) VALUES
+(@SRID, 1234, 2345, "me", "home");
+CALL entityACL(@ACLID1, "{acl1}");
+
+-- Insert entities and obtain IDs
+CALL insertEntity("A", "Desc A", "RECORDTYPE", "{acl1}");
+CALL insertEntity("B", "Desc B", "RECORDTYPE", "{acl1}");
+CALL insertEntity("C", "Desc C", "RECORDTYPE", "{acl1}");
+CALL insertEntity("rec", "Desc rec", "RECORD", "{acl1}");
+
+SELECT entity_id INTO @ID_A FROM name_data WHERE value="A";
+SELECT entity_id INTO @ID_B FROM name_data WHERE value="B";
+SELECT entity_id INTO @ID_C FROM name_data WHERE value="C";
+SELECT entity_id INTO @ID_rec FROM name_data WHERE value="rec";
+
+-- Insert is-a relationships
+CALL insertIsA(@ID_A, @ID_B);
+CALL insertIsA(@ID_B, @ID_C);
+CALL insertIsA(@ID_rec, @ID_A);
+
+-- Try to delete last child
+-- leads to failure in issue #21
+CALL deleteIsa(@ID_rec);
+
+ROLLBACK;
-- 
GitLab