From a4b158c42d973c5a9813c8a09efbf6a10a0b886b Mon Sep 17 00:00:00 2001
From: Timm Fitschen <t.fitschen@indiscale.com>
Date: Mon, 2 Oct 2023 20:55:58 +0200
Subject: [PATCH] WIP: string ids

---
 .../create_entity_ids_table.sql               |  4 +-
 procedures/getDependentEntities.sql           |  7 +-
 procedures/insertFile.sql                     |  5 ++
 procedures/retrieveChildren.sql               | 35 ----------
 tests/test_autotap.sql                        |  7 --
 tests/test_entity_versioning.sql              | 64 ++++++++++---------
 6 files changed, 42 insertions(+), 80 deletions(-)
 delete mode 100644 procedures/retrieveChildren.sql

diff --git a/patches/patch20221122-6.0-SNAPSHOT/create_entity_ids_table.sql b/patches/patch20221122-6.0-SNAPSHOT/create_entity_ids_table.sql
index cbc6ded..ddd1ad1 100644
--- a/patches/patch20221122-6.0-SNAPSHOT/create_entity_ids_table.sql
+++ b/patches/patch20221122-6.0-SNAPSHOT/create_entity_ids_table.sql
@@ -10,7 +10,7 @@ DROP PROCEDURE IF EXISTS setPassword;
 -- new entity_ids table
 DROP TABLE IF EXISTS `entity_ids`;
 CREATE TABLE `entity_ids` (
-    `id` int(10) unsigned NOT NULL,
+    `id` VARCHAR(255) NOT NULL,
     `internal_id` int(10) unsigned NOT NULL COMMENT 'Internal ID of an entity. This id is used internally in the *_data tables and elsewhere. This ID is never exposed via the CaosDB API.',
     PRIMARY KEY `entity_ids_pk` (`id`),
     CONSTRAINT `entity_ids_internal_id` FOREIGN KEY (`internal_id`) REFERENCES `entities` (`id`)
@@ -18,5 +18,3 @@ CREATE TABLE `entity_ids` (
 
 -- fill all existing entities into the new entity_ids table.
 INSERT INTO entity_ids (id, internal_id) SELECT id, id FROM entities WHERE id>0 AND role!="DOMAIN";
-UPDATE entity_ids SET id = internal_id;
--- ALTER TABLE entity_ids CHANGE id id int(10) unsigned NOT NULL AUTO_INCREMENT  COMMENT 'External ID of an entity. This is the id of an entity which is exposed via the CaosDB API.';
diff --git a/procedures/getDependentEntities.sql b/procedures/getDependentEntities.sql
index 980a0d9..17ee476 100644
--- a/procedures/getDependentEntities.sql
+++ b/procedures/getDependentEntities.sql
@@ -38,8 +38,8 @@ delimiter //
  * EntityID : VARCHAR(255)
  *    The entity id.
  *
- * Selects
- * -------
+ * ResultSet
+ * ---------
  * EntityID : VARCHAR(255)
  *
  */
@@ -85,8 +85,7 @@ BEGIN
     INSERT IGNORE INTO refering (id) SELECT entity_id from data_type WHERE datatype=InternalEntityID AND domain_id=0 AND entity_id!=InternalEntityID;
     INSERT IGNORE INTO refering (id) SELECT domain_id from data_type WHERE datatype=InternalEntityID;
 
-    -- TODO activate after `retrieveChildren` has been removed.
-    -- INSERT IGNORE INTO refering (id) SELECT child FROM isa_cache WHERE parent = InternalParentID AND rpath=child;
+    INSERT IGNORE INTO refering (id) SELECT child FROM isa_cache WHERE parent = InternalEntityID AND rpath=child;
 
     SELECT e.id FROM refering AS r LEFT JOIN entity_ids AS e ON r.id = e.internal_id WHERE r.id!=0 AND e.internal_id!=InternalEntityID;
 
diff --git a/procedures/insertFile.sql b/procedures/insertFile.sql
index c7e66cd..385f972 100644
--- a/procedures/insertFile.sql
+++ b/procedures/insertFile.sql
@@ -19,7 +19,9 @@
  */
 
 DROP PROCEDURE IF EXISTS db_5_0.insertFile;
+/*
 DELIMITER //
+ */
 
 /*
  * Insert the file properties of a File entity.
@@ -36,6 +38,8 @@ DELIMITER //
  * FileSize : BIGINT UNSIGNED
  *   Size of the file in bytes.
  */
+
+/*
 CREATE PROCEDURE db_5_0.insertFile(in EntityID VARCHAR(255), in Hash VARCHAR(255), in FileSize BIGINT UNSIGNED, in FilePath TEXT)
 BEGIN
     DECLARE InternalEntityID INT UNSIGNED DEFAULT NULL;
@@ -46,3 +50,4 @@ BEGIN
 END;
 //
 DELIMITER ;
+*/
diff --git a/procedures/retrieveChildren.sql b/procedures/retrieveChildren.sql
deleted file mode 100644
index 20615fa..0000000
--- a/procedures/retrieveChildren.sql
+++ /dev/null
@@ -1,35 +0,0 @@
-/*
- * This file is a part of the CaosDB Project.
- *
- * Copyright (C) 2023 IndiScale GmbH <info@indiscale.com>
- * Copyright (C) 2023 Timm Fitschen <t.fitschen@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/>.
- */
-
--- TODO: REMOVE
-DROP PROCEDURE IF EXISTS db_5_0.retrieveChildren;
-delimiter //
-
-CREATE PROCEDURE db_5_0.retrieveChildren(in ParentID VARCHAR(255))
-BEGIN
-    DECLARE InternalParentID INT UNSIGNED DEFAULT NULL;
-
-    SELECT internal_id INTO InternalParentID from entity_ids WHERE id = ParentID;
-
-    SELECT eids.id FROM isa_cache AS i JOIN entity_ids AS eids ON i.child = eids.internal_id WHERE i.parent=InternalParentID and i.rpath=i.child;
-
-END;
-//
-delimiter ;
diff --git a/tests/test_autotap.sql b/tests/test_autotap.sql
index 9f77a59..8afd5cd 100644
--- a/tests/test_autotap.sql
+++ b/tests/test_autotap.sql
@@ -2733,12 +2733,5 @@ SELECT tap.procedure_is_deterministic('_caosdb_schema_unit_tests','insert_single
 SELECT tap.procedure_security_type_is('_caosdb_schema_unit_tests','insert_single_child_version','DEFINER','');
 SELECT tap.procedure_sql_data_access_is('_caosdb_schema_unit_tests','insert_single_child_version','CONTAINS SQL','');
 
--- PROCEDURES _caosdb_schema_unit_tests.delete_all_entity_versions
-
-SELECT tap.has_procedure('_caosdb_schema_unit_tests', 'delete_all_entity_versions', '');
-SELECT tap.procedure_is_deterministic('_caosdb_schema_unit_tests','delete_all_entity_versions','NO','');
-SELECT tap.procedure_security_type_is('_caosdb_schema_unit_tests','delete_all_entity_versions','DEFINER','');
-SELECT tap.procedure_sql_data_access_is('_caosdb_schema_unit_tests','delete_all_entity_versions','CONTAINS SQL','');
-
 CALL tap.finish();
 ROLLBACK;
diff --git a/tests/test_entity_versioning.sql b/tests/test_entity_versioning.sql
index ad321c2..4f8e8fa 100644
--- a/tests/test_entity_versioning.sql
+++ b/tests/test_entity_versioning.sql
@@ -54,21 +54,21 @@ INSERT INTO transactions (srid,seconds,nanos,username,realm) VALUES
 DELETE FROM entities WHERE id > 99;
 CALL entityACL(@ACLID1, "{acl1}");
 CALL insertEntity(@EntityID1, "EntityName", "EntityDesc", "RECORDTYPE", "{acl1}");
-SELECT entity_id INTO @InternalEntityID FROM name_data WHERE value="EntityName";
+SELECT entity_id INTO @InternalEntityID1 FROM name_data WHERE value="EntityName";
 
 
 -- TEST insert_single_child_version
 SELECT count(*) INTO @x FROM entity_version;
 SELECT tap.eq(@x, 0, "no versions there yet");
 
-CALL insert_single_child_version(@InternalEntityID, "hashbla", "versionbla", NULL, "SRIDbla");
+CALL insert_single_child_version(@InternalEntityID1, "hashbla", "versionbla", NULL, "SRIDbla");
 SELECT _ipparent INTO @x from entity_version WHERE version="versionbla";
 SELECT tap.eq(@x, NULL, "no parent for the first version");
 
 -- add a second version
 SELECT count(*) INTO @x FROM entity_version;
 SELECT tap.eq(@x, 1, "one version there already");
-CALL insert_single_child_version(@InternalEntityID, "hashblub", "versionblub", "versionbla", "SRIDblub");
+CALL insert_single_child_version(@InternalEntityID1, "hashblub", "versionblub", "versionbla", "SRIDblub");
 
 SELECT _ipparent INTO @x from entity_version WHERE version="versionblub";
 SELECT tap.eq(@x, 1, "the original entity is the parent");
@@ -79,7 +79,7 @@ SELECT tap.eq(@x, 2, "two versions there already");
 
 CALL tap._assert_throws(
      concat("CALL insert_single_child_version(",
-            @InternalEntityID, ', "hashblieb", "versionblieb", "non-existing-parent", "SRIDBlieb")'),
+            @InternalEntityID1, ', "hashblieb", "versionblieb", "non-existing-parent", "SRIDBlieb")'),
      "non existing parent throws");
 
 SELECT count(*) INTO @x FROM entity_version;
@@ -91,11 +91,13 @@ SELECT tap.eq(get_primary_parent_version(@EntityID1, "versionblub"), "versionbla
 SELECT tap.eq(get_primary_parent_version(@EntityID1, "versionbla"), NULL, "versionbla has no parent");
 
 
+-- Reactivate when versioning's FORGET is being implemented
 -- TEST delete_all_entity_versions
 SELECT count(*) INTO @x FROM entity_version;
 SELECT tap.ok(@x > 0, "several versions in the table");
 
-CALL delete_all_entity_versions(@EntityID1);
+-- CALL delete_all_entity_versions(@EntityID1);
+DELETE FROM entity_version WHERE entity_id = @InternalEntityID1;
 SELECT count(*) INTO @x FROM entity_version;
 SELECT tap.eq(@x, 0, "no versions there any more");
 
@@ -117,19 +119,19 @@ SELECT tap.eq(@x, 0, "before insertEntity, no versions there");
 -- TEST insertEntity - should produce a version w/o parent
 SET @SRID = "SRIDbla";
 CALL insertEntity(@EntityID2, "EntityName", "EntityDesc", "RECORDTYPE", "{acl1}");
-SELECT entity_id INTO @InternalEntityID FROM name_data WHERE value="EntityName";
+SELECT entity_id INTO @InternalEntityID2 FROM name_data WHERE value="EntityName";
 CALL insertEntity(@EntityID3, "ParentName", "ParentDesc", "RECORDTYPE", "{acl1}");
 SELECT entity_id INTO @ParentID FROM name_data WHERE value="ParentName";
 CALL insertIsa(@EntityID2, @EntityID3);
 CALL insertEntityProperty(0, @EntityID2, 17, "null_data", NULL, NULL,
     "RECOMMENDED", NULL, "DescOverride", NULL, NULL, 0);
 
-SELECT count(*) INTO @x FROM entity_version WHERE entity_id = @InternalEntityID;
+SELECT count(*) INTO @x FROM entity_version WHERE entity_id = @InternalEntityID2;
 SELECT tap.eq(@x, 1, "after insertEntity, a version is there.");
-SELECT _iversion INTO @x FROM entity_version WHERE entity_id = @InternalEntityID and _ipparent is NULL;
+SELECT _iversion INTO @x FROM entity_version WHERE entity_id = @InternalEntityID2 and _ipparent is NULL;
 SELECT tap.eq(@x, 1, "after insertEntity, the _iversion number is 1.");
 
-SELECT _ipparent INTO @x from entity_version WHERE entity_id = @InternalEntityID;
+SELECT _ipparent INTO @x from entity_version WHERE entity_id = @InternalEntityID2;
 SELECT tap.eq(@x, NULL, "no parent for the freshly inserted entity");
 SELECT tap.eq(count(*), 0, "no entity in archive_entities before first update")
     FROM archive_entities;
@@ -139,12 +141,12 @@ SELECT tap.eq(count(*), 0, "no entity in archive_entities before first update")
 SET @SRID = "SRIDblub";
 CALL deleteEntityProperties(@EntityID2);
 CALL updateEntity(@EntityID2, "NewEntityName", "NewEntityDesc", "RECORD", NULL, NULL, "{acl1}");
-SELECT count(*) INTO @x FROM entity_version WHERE entity_id = @InternalEntityID;
+SELECT count(*) INTO @x FROM entity_version WHERE entity_id = @InternalEntityID2;
 SELECT tap.eq(@x, 2, "after updateEntity, a second version is there.");
 
-SELECT _iversion INTO @x FROM entity_version WHERE entity_id = @InternalEntityID and _ipparent = 1;
+SELECT _iversion INTO @x FROM entity_version WHERE entity_id = @InternalEntityID2 and _ipparent = 1;
 SELECT tap.eq(@x, 2, "after updateEntity, the _iversion number incremented.");
-SELECT _ipparent INTO @x FROM entity_version WHERE entity_id = @InternalEntityID and _ipparent = 1;
+SELECT _ipparent INTO @x FROM entity_version WHERE entity_id = @InternalEntityID2 and _ipparent = 1;
 SELECT tap.eq(@x, 1, "after updateEntity, the _pparent points to the first version");
 
 SELECT tap.eq(count(*), 1, "after updateEntity, one entity in archive_entities")
@@ -157,7 +159,7 @@ CALL get_version_history(@EntityID2);
 -- TEST retrieveEntity
 
 SELECT version INTO @x FROM entity_version
-    WHERE entity_id = @InternalEntityID
+    WHERE entity_id = @InternalEntityID2
     AND _iversion = 2;
 CALL retrieveEntity(@EntityID2, NULL);
 CALL retrieveEntity(@EntityID2, "non-existing-version");
@@ -189,20 +191,20 @@ CALL retrieveOverrides(0, @EntityID2, @x);
 -- and 2nd updateEntity
 SET @SRID = "SRIDblieb";
 CALL updateEntity(@EntityID2, "EntityName", "EntityDesc", "RECORDTYPE", NULL, NULL, "{acl1}");
-SELECT count(*) INTO @x FROM entity_version WHERE entity_id = @InternalEntityID;
+SELECT count(*) INTO @x FROM entity_version WHERE entity_id = @InternalEntityID2;
 SELECT tap.eq(@x, 3, "after 2nd updateEntity, a 3rd version is there.");
 
-SELECT _iversion INTO @x FROM entity_version WHERE entity_id = @InternalEntityID and _ipparent = 2;
+SELECT _iversion INTO @x FROM entity_version WHERE entity_id = @InternalEntityID2 and _ipparent = 2;
 SELECT tap.eq(@x, 3, "after 2nd updateEntity, the _iversion number incremented again.");
-SELECT _ipparent INTO @x FROM entity_version WHERE entity_id = @InternalEntityID and _iversion = 3;
+SELECT _ipparent INTO @x FROM entity_version WHERE entity_id = @InternalEntityID2 and _iversion = 3;
 SELECT tap.eq(@x, 2, "after 2nd updateEntity, the _pparent points to the 2nd version");
-SELECT tap.eq("SRIDblieb", srid, "correct transaction was stored") FROM entity_version WHERE entity_id = @InternalEntityID AND _ipparent = 2;
+SELECT tap.eq("SRIDblieb", srid, "correct transaction was stored") FROM entity_version WHERE entity_id = @InternalEntityID2 AND _ipparent = 2;
 
 
 -- TEST deleteEntity - should remove all versions
 CALL deleteIsa(@EntityID3);
 CALL deleteEntity(@EntityID2);
-SELECT count(*) INTO @x FROM entity_version WHERE entity_id = @InternalEntityID;
+SELECT count(*) INTO @x FROM entity_version WHERE entity_id = @InternalEntityID2;
 SELECT tap.eq(@x, 0, "no versions there any more");
 
 CALL deleteEntity(@EntityID3);
@@ -227,19 +229,19 @@ DELETE FROM feature_config WHERE _key = "ENTITY_VERSIONING";
 CALL entityACL(@ACLID1, "{acl1}");
 CALL insertEntity(@EntityID4, "EntityName", "EntityDesc", "RECORDTYPE", "{acl1}");
 SELECT count(*) INTO @NumOfEntities FROM entities;
-SELECT entity_id INTO @InternalEntityID FROM name_data WHERE value="EntityName";
+SELECT entity_id INTO @InternalEntityID4 FROM name_data WHERE value="EntityName";
 
 SET @TheUser = "TheUser"; -- used to identify the matching entry in transaction_log
 -- fill transaction_log: one entity with two updates (and one insert) and another entity with insert and delete.
 INSERT INTO transaction_log (transaction, entity_id, username, realm, seconds, nanos)
     -- the first entry is the one which is be found by _fix_unversioned
     VALUES
-        ("Update", @InternalEntityID, @TheUser,     "CaosDB",  23458, 254),
+        ("Update", @InternalEntityID4, @TheUser,     "CaosDB",  23458, 254),
         -- the rest is dummy data
-        ("Update", @InternalEntityID, "OtherUser",  "CaosDB",   2345, 633), -- not the latest transaction
-        ("Insert", @InternalEntityID, "OtherUser",  "CaosDB",    245, 633), -- not the latest transaction
-        ("Insert", @InternalEntityID + 1, @TheUser, "CaosDB",   2325, 653), -- not the right entity, inserted before our target
-        ("Delete", @InternalEntityID + 1, @TheUser, "CaosDB", 232526, 653); -- not the right entity, deleted after our target
+        ("Update", @InternalEntityID4, "OtherUser",  "CaosDB",   2345, 633), -- not the latest transaction
+        ("Insert", @InternalEntityID4, "OtherUser",  "CaosDB",    245, 633), -- not the latest transaction
+        ("Insert", @InternalEntityID4 + 1, @TheUser, "CaosDB",   2325, 653), -- not the right entity, inserted before our target
+        ("Delete", @InternalEntityID4 + 1, @TheUser, "CaosDB", 232526, 653); -- not the right entity, deleted after our target
 
 
 SELECT tap.eq(COUNT(*), 5, "five entries in transaction_log") FROM transaction_log;
@@ -259,11 +261,11 @@ SELECT tap.eq(COUNT(*), 2,
     "after _fix_unversioned, one entry for our test entity in transactions, one for the standard entities.")
     FROM transactions;
 
-SELECT tap.eq(entity_id, @InternalEntityID, "versioned entity has correct id") FROM entity_version WHERE entity_id > 99;
+SELECT tap.eq(entity_id, @InternalEntityID4, "versioned entity has correct id") FROM entity_version WHERE entity_id > 99;
 SELECT tap.ok(srid IS NOT NULL, "srid was generated and user/time matches entries from transaction_log")
     FROM transactions AS t JOIN transaction_log AS l
     ON (l.seconds = t.seconds AND l.nanos = t.nanos AND l.username = t.username AND l.realm = t.realm)
-    WHERE l.entity_id = @InternalEntityID AND l.username = @TheUser;
+    WHERE l.entity_id = @InternalEntityID4444 AND l.username = @TheUser;
 
 CALL tap.finish();
 ROLLBACK;
@@ -286,15 +288,15 @@ CALL entityACL(@ACLID1, "{acl1}");
 CALL insertEntity(@EntityID5, "EntityName1", "EntityDesc1", "RECORDTYPE", "{acl1}");
 CALL insertEntity(@EntityID6, "EntityName2", "EntityDesc2", "RECORDTYPE", "{acl1}");
 SELECT count(*) INTO @NumOfEntities FROM entities;
-SELECT entity_id INTO @InternalEntityID1 FROM name_data WHERE value="EntityName1";
-SELECT entity_id INTO @InternalEntityID2 FROM name_data WHERE value="EntityName2";
+SELECT entity_id INTO @InternalEntityID5 FROM name_data WHERE value="EntityName1";
+SELECT entity_id INTO @InternalEntityID6 FROM name_data WHERE value="EntityName2";
 
 INSERT INTO transaction_log (transaction, entity_id, username, realm, seconds,
         nanos)
     -- the first entry is the one which will be found by _fix_unversioned
-    VALUES ("INSERT", @InternalEntityID1, "User", "CaosDB", 10000, 250),
-        ("INSERT", @InternalEntityID2, "User", "CaosDB", 10000, 250),
-        ("UPDATE", @InternalEntityID2, "User", "CaosDB", 20000, 250);
+    VALUES ("INSERT", @InternalEntityID5, "User", "CaosDB", 10000, 250),
+        ("INSERT", @InternalEntityID6, "User", "CaosDB", 10000, 250),
+        ("UPDATE", @InternalEntityID6, "User", "CaosDB", 20000, 250);
 
 
 SELECT tap.eq(COUNT(*), 3, "three entries in transaction_log") FROM transaction_log;
-- 
GitLab