From a3b7cf84d58377ac0a6dfde39e1eb372d63dc205 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Mon, 14 Oct 2024 14:50:56 +1300 Subject: [PATCH] Allow shared tables V1 and V2 --- app/controllers/api/projects.php | 50 +++++++++++++++----------------- 1 file changed, 23 insertions(+), 27 deletions(-) diff --git a/app/controllers/api/projects.php b/app/controllers/api/projects.php index f20b79a00a..e80e2888b2 100644 --- a/app/controllers/api/projects.php +++ b/app/controllers/api/projects.php @@ -118,6 +118,10 @@ App::post('/v1/projects') $projectId = ($projectId == 'unique()') ? ID::unique() : $projectId; + if ($projectId === 'console') { + throw new Exception(Exception::PROJECT_RESERVED_PROJECT, "'console' is a reserved project."); + } + $databases = Config::getParam('pools-database', []); $databaseOverride = System::getEnv('_APP_DATABASE_OVERRIDE'); @@ -128,10 +132,6 @@ App::post('/v1/projects') $dsn = $databases[array_rand($databases)]; } - if ($projectId === 'console') { - throw new Exception(Exception::PROJECT_RESERVED_PROJECT, "'console' is a reserved project."); - } - // TODO: Temporary until all projects are using shared tables. $sharedTables = \explode(',', System::getEnv('_APP_DATABASE_SHARED_TABLES', '')); @@ -194,11 +194,13 @@ App::post('/v1/projects') $adapter = $pools->get($dsn->getHost())->pop()->getResource(); $dbForProject = new Database($adapter, $cache); $sharedTables = \explode(',', System::getEnv('_APP_DATABASE_SHARED_TABLES', '')); + $sharedTablesV1 = \explode(',', System::getEnv('_APP_DATABASE_SHARED_TABLES_V1', '')); + $globalCollections = !\in_array($dsn->getHost(), $sharedTablesV1); if (\in_array($dsn->getHost(), $sharedTables)) { $dbForProject ->setSharedTables(true) - ->setTenant(null) + ->setTenant($globalCollections ? null : $project->getInternalId()) ->setNamespace($dsn->getParam('namespace')); } else { $dbForProject @@ -207,38 +209,32 @@ App::post('/v1/projects') ->setNamespace('_' . $project->getInternalId()); } + $create = true; + try { $dbForProject->create(); } catch (Duplicate) { - // Database already exists + $create = false; } - $audit = new Audit($dbForProject); - $audit->setup(); + if ($create || !$globalCollections) { + $audit = new Audit($dbForProject); + $audit->setup(); - $abuse = new TimeLimit('', 0, 1, $dbForProject); - $abuse->setup(); + $abuse = new TimeLimit('', 0, 1, $dbForProject); + $abuse->setup(); - /** @var array $collections */ - $collections = Config::getParam('collections', [])['projects'] ?? []; + /** @var array $collections */ + $collections = Config::getParam('collections', [])['projects'] ?? []; - foreach ($collections as $key => $collection) { - if (($collection['$collection'] ?? '') !== Database::METADATA) { - continue; - } + foreach ($collections as $key => $collection) { + if (($collection['$collection'] ?? '') !== Database::METADATA) { + continue; + } - $attributes = \array_map(function (array $attribute) { - return new Document($attribute); - }, $collection['attributes']); - - $indexes = \array_map(function (array $index) { - return new Document($index); - }, $collection['indexes']); - - try { + $attributes = \array_map(fn ($attribute) => new Document($attribute), $collection['attributes']); + $indexes = \array_map(fn (array $index) => new Document($index), $collection['indexes']); $dbForProject->createCollection($key, $attributes, $indexes); - } catch (Duplicate) { - // Collection already exists } }