From bae6e465fa564e1be53b4adfca59cc13b2a4e3ba Mon Sep 17 00:00:00 2001 From: fogelito Date: Mon, 11 Nov 2024 18:46:47 +0200 Subject: [PATCH] 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)