diff --git a/app/config/collections/common.php b/app/config/collections/common.php index c5dc187ba4..2e3df819b7 100644 --- a/app/config/collections/common.php +++ b/app/config/collections/common.php @@ -2662,6 +2662,17 @@ return [ 'array' => false, 'filters' => [], ], + [ + '$id' => ID::custom('folder'), + 'type' => Database::VAR_STRING, + 'format' => '', + 'size' => 2048, + 'signed' => true, + 'required' => false, + 'default' => '', + 'array' => false, + 'filters' => [], + ], [ '$id' => ID::custom('signature'), 'type' => Database::VAR_STRING, @@ -2850,6 +2861,13 @@ return [ 'lengths' => [256], 'orders' => [Database::ORDER_ASC], ], + [ + '$id' => ID::custom('_key_folder'), + 'type' => Database::INDEX_KEY, + 'attributes' => ['folder'], + 'lengths' => [256], + 'orders' => [Database::ORDER_ASC], + ], [ '$id' => ID::custom('_key_signature'), 'type' => Database::INDEX_KEY, diff --git a/src/Appwrite/Migration/Version/V25.php b/src/Appwrite/Migration/Version/V25.php index 800b9b451f..3ca42d2af3 100644 --- a/src/Appwrite/Migration/Version/V25.php +++ b/src/Appwrite/Migration/Version/V25.php @@ -7,6 +7,8 @@ use Exception; use Throwable; use Utopia\Console; use Utopia\Database\Database; +use Utopia\Database\Document; +use Utopia\Database\Exception\Duplicate; class V25 extends Migration { @@ -17,6 +19,9 @@ class V25 extends Migration { Console::info('Migrating collections'); $this->migrateCollections(); + + Console::info('Migrating buckets'); + $this->migrateBuckets(); } /** @@ -66,4 +71,37 @@ class V25 extends Migration } } } + + /** + * Add the virtual folder `folder` attribute and index to every + * per-bucket files collection. + * + * @return void + * @throws Throwable + */ + private function migrateBuckets(): void + { + $this->dbForProject->foreach('buckets', function (Document $bucket) { + $bucketTable = "bucket_{$bucket->getSequence()}"; + Console::log("Migrating Bucket {$bucket->getId()} ({$bucket->getAttribute('name')})"); + + try { + $this->createAttributeFromCollection($this->dbForProject, $bucketTable, 'folder', 'files'); + } catch (Duplicate) { + Console::warning("'folder' from {$bucketTable}: Column already exists"); + } catch (Throwable $th) { + Console::warning("'folder' from {$bucketTable}: {$th->getMessage()}"); + } + + try { + $this->createIndexFromCollection($this->dbForProject, $bucketTable, '_key_folder', 'files'); + } catch (Duplicate) { + Console::warning("'_key_folder' from {$bucketTable}: Index already exists"); + } catch (Throwable $th) { + Console::warning("'_key_folder' from {$bucketTable}: {$th->getMessage()}"); + } + + $this->dbForProject->purgeCachedCollection($bucketTable); + }); + } }