From bae6e465fa564e1be53b4adfca59cc13b2a4e3ba Mon Sep 17 00:00:00 2001 From: fogelito Date: Mon, 11 Nov 2024 18:46:47 +0200 Subject: [PATCH 1/3] updateAttributes --- app/controllers/api/databases.php | 38 ++++++++++++++++++++++++------- 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/app/controllers/api/databases.php b/app/controllers/api/databases.php index 0114fd343c..6af4f592d6 100644 --- a/app/controllers/api/databases.php +++ b/app/controllers/api/databases.php @@ -396,20 +396,37 @@ function updateAttribute( } } - if (!empty($newKey) && $key !== $newKey) { - // Delete attribute and recreate since we can't modify IDs - $original = clone $attribute; + $purge = false; - $dbForProject->deleteDocument('attributes', $attribute->getId()); + if (!empty($newKey) && $key !== $newKey) { + $originalUid = $attribute->getId(); $attribute ->setAttribute('$id', ID::custom($db->getInternalId() . '_' . $collection->getInternalId() . '_' . $newKey)) ->setAttribute('key', $newKey); - try { - $attribute = $dbForProject->createDocument('attributes', $attribute); - } catch (DatabaseException|PDOException) { - $attribute = $dbForProject->createDocument('attributes', $original); + $dbForProject->updateDocument('attributes', $originalUid, $attribute); + + /** + * @var Document $index + */ + foreach ($collection->getAttribute('indexes') as $index) { + /** + * @var string[] $attributes + */ + $attributes = $index->getAttribute('attributes', []); + $found = \array_search($key, $attributes); + + var_dump($attributes); + var_dump('found==='); + var_dump($found); + + if ($found !== false) { + $attributes[$found] = $newKey; + $index->setAttribute('attributes', $attributes); + $dbForProject->updateDocument('indexes', $index->getId(), $index); + $purge = true; + } } } else { $attribute = $dbForProject->updateDocument('attributes', $db->getInternalId() . '_' . $collection->getInternalId() . '_' . $key, $attribute); @@ -417,6 +434,11 @@ function updateAttribute( $dbForProject->purgeCachedDocument('database_' . $db->getInternalId(), $collection->getId()); + if($purge){ + // I think we are missing this? + $dbForProject->purgeCachedCollection('database_' . $db->getInternalId() . '_collection_' . $collection->getInternalId()); + } + $queueForEvents ->setContext('collection', $collection) ->setContext('database', $db) From 71f2c6c1316eb041269409bc591ff5bc2201eaeb Mon Sep 17 00:00:00 2001 From: fogelito Date: Wed, 13 Nov 2024 14:55:56 +0200 Subject: [PATCH 2/3] Remove purgeCachedCollection --- app/controllers/api/databases.php | 12 ------------ src/Appwrite/Platform/Workers/Databases.php | 2 -- 2 files changed, 14 deletions(-) diff --git a/app/controllers/api/databases.php b/app/controllers/api/databases.php index 6af4f592d6..523529698d 100644 --- a/app/controllers/api/databases.php +++ b/app/controllers/api/databases.php @@ -396,8 +396,6 @@ function updateAttribute( } } - $purge = false; - if (!empty($newKey) && $key !== $newKey) { $originalUid = $attribute->getId(); @@ -417,15 +415,10 @@ function updateAttribute( $attributes = $index->getAttribute('attributes', []); $found = \array_search($key, $attributes); - var_dump($attributes); - var_dump('found==='); - var_dump($found); - if ($found !== false) { $attributes[$found] = $newKey; $index->setAttribute('attributes', $attributes); $dbForProject->updateDocument('indexes', $index->getId(), $index); - $purge = true; } } } else { @@ -434,11 +427,6 @@ function updateAttribute( $dbForProject->purgeCachedDocument('database_' . $db->getInternalId(), $collection->getId()); - if($purge){ - // I think we are missing this? - $dbForProject->purgeCachedCollection('database_' . $db->getInternalId() . '_collection_' . $collection->getInternalId()); - } - $queueForEvents ->setContext('collection', $collection) ->setContext('database', $db) diff --git a/src/Appwrite/Platform/Workers/Databases.php b/src/Appwrite/Platform/Workers/Databases.php index f697e7be13..1cbc3a3f8b 100644 --- a/src/Appwrite/Platform/Workers/Databases.php +++ b/src/Appwrite/Platform/Workers/Databases.php @@ -350,11 +350,9 @@ class Databases extends Action } $dbForProject->purgeCachedDocument('database_' . $database->getInternalId(), $collectionId); - $dbForProject->purgeCachedCollection('database_' . $database->getInternalId() . '_collection_' . $collection->getInternalId()); if (!$relatedCollection->isEmpty() && !$relatedAttribute->isEmpty()) { $dbForProject->purgeCachedDocument('database_' . $database->getInternalId(), $relatedCollection->getId()); - $dbForProject->purgeCachedCollection('database_' . $database->getInternalId() . '_collection_' . $relatedCollection->getInternalId()); } } From 68e96e9851a9500a2b087e3bf48b97be1f898e0d Mon Sep 17 00:00:00 2001 From: fogelito Date: Wed, 13 Nov 2024 18:32:20 +0200 Subject: [PATCH 3/3] comment --- app/controllers/api/databases.php | 1 + 1 file changed, 1 insertion(+) diff --git a/app/controllers/api/databases.php b/app/controllers/api/databases.php index 523529698d..f07a1e039d 100644 --- a/app/controllers/api/databases.php +++ b/app/controllers/api/databases.php @@ -2609,6 +2609,7 @@ App::post('/v1/databases/:databaseId/collections/:collectionId/indexes') throw new Exception(Exception::ATTRIBUTE_NOT_AVAILABLE, 'Attribute not available: ' . $oldAttributes[$attributeIndex]['key']); } + // todo: Think of a better solution $lengths[$i] = null; if ($attributeType === Database::VAR_STRING) {