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;
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
}
}