Allow shared tables V1 and V2

This commit is contained in:
Jake Barnby 2024-10-14 14:50:56 +13:00
parent add6a60af4
commit a3b7cf84d5
No known key found for this signature in database
GPG key ID: C437A8CC85B96E9C

View file

@ -118,6 +118,10 @@ App::post('/v1/projects')
$projectId = ($projectId == 'unique()') ? ID::unique() : $projectId; $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', []); $databases = Config::getParam('pools-database', []);
$databaseOverride = System::getEnv('_APP_DATABASE_OVERRIDE'); $databaseOverride = System::getEnv('_APP_DATABASE_OVERRIDE');
@ -128,10 +132,6 @@ App::post('/v1/projects')
$dsn = $databases[array_rand($databases)]; $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. // TODO: Temporary until all projects are using shared tables.
$sharedTables = \explode(',', System::getEnv('_APP_DATABASE_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(); $adapter = $pools->get($dsn->getHost())->pop()->getResource();
$dbForProject = new Database($adapter, $cache); $dbForProject = new Database($adapter, $cache);
$sharedTables = \explode(',', System::getEnv('_APP_DATABASE_SHARED_TABLES', '')); $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)) { if (\in_array($dsn->getHost(), $sharedTables)) {
$dbForProject $dbForProject
->setSharedTables(true) ->setSharedTables(true)
->setTenant(null) ->setTenant($globalCollections ? null : $project->getInternalId())
->setNamespace($dsn->getParam('namespace')); ->setNamespace($dsn->getParam('namespace'));
} else { } else {
$dbForProject $dbForProject
@ -207,38 +209,32 @@ App::post('/v1/projects')
->setNamespace('_' . $project->getInternalId()); ->setNamespace('_' . $project->getInternalId());
} }
$create = true;
try { try {
$dbForProject->create(); $dbForProject->create();
} catch (Duplicate) { } catch (Duplicate) {
// Database already exists $create = false;
} }
$audit = new Audit($dbForProject); if ($create || !$globalCollections) {
$audit->setup(); $audit = new Audit($dbForProject);
$audit->setup();
$abuse = new TimeLimit('', 0, 1, $dbForProject); $abuse = new TimeLimit('', 0, 1, $dbForProject);
$abuse->setup(); $abuse->setup();
/** @var array $collections */ /** @var array $collections */
$collections = Config::getParam('collections', [])['projects'] ?? []; $collections = Config::getParam('collections', [])['projects'] ?? [];
foreach ($collections as $key => $collection) { foreach ($collections as $key => $collection) {
if (($collection['$collection'] ?? '') !== Database::METADATA) { if (($collection['$collection'] ?? '') !== Database::METADATA) {
continue; continue;
} }
$attributes = \array_map(function (array $attribute) { $attributes = \array_map(fn ($attribute) => new Document($attribute), $collection['attributes']);
return new Document($attribute); $indexes = \array_map(fn (array $index) => new Document($index), $collection['indexes']);
}, $collection['attributes']);
$indexes = \array_map(function (array $index) {
return new Document($index);
}, $collection['indexes']);
try {
$dbForProject->createCollection($key, $attributes, $indexes); $dbForProject->createCollection($key, $attributes, $indexes);
} catch (Duplicate) {
// Collection already exists
} }
} }