diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Create.php b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Create.php index 51bef56c47..27ee70ef3a 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Create.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Create.php @@ -4,6 +4,7 @@ namespace Appwrite\Platform\Modules\Databases\Http\Databases; use Appwrite\Event\Event; use Appwrite\Extend\Exception; +use Appwrite\Platform\Modules\Databases\Pool; use Appwrite\SDK\AuthType; use Appwrite\SDK\ContentType; use Appwrite\SDK\Deprecated; @@ -19,9 +20,7 @@ use Utopia\Database\Exception\Index as IndexException; use Utopia\Database\Exception\Limit as LimitException; use Utopia\Database\Exception\Structure as StructureException; use Utopia\Database\Helpers\ID; -use Utopia\DSN\DSN; use Utopia\Http\Adapter\Swoole\Response as SwooleResponse; -use Utopia\System\System; use Utopia\Validator\Boolean; use Utopia\Validator\Text; @@ -36,96 +35,7 @@ class Create extends Action { // TODO: use database worker for for creating the v2 schema if not present // it is considered that the v2 metadata schema is already created during server start in the http.php - return $this->constructDatabaseDSNFromProjectDatabase($this->getDatabaseType(), $project->getAttribute('region'), $project->getAttribute('database')); - } - - private function constructDatabaseDSNFromProjectDatabase(string $databasetype, $region, ?string $dsn = null): string - { - $databases = []; - $databaseKeys = []; - /** - * @var string|null $databaseOverride - */ - $databaseOverride = ''; - $dbScheme = ''; - $databaseSharedTables = []; - - switch ($databasetype) { - case DOCUMENTSDB: - $databases = Config::getParam('pools-documentsdb', []); - $databaseKeys = System::getEnv('_APP_DATABASE_DOCUMENTSDB_KEYS', ''); - $databaseOverride = System::getEnv('_APP_DATABASE_DOCUMENTSDB_OVERRIDE'); - $dbScheme = System::getEnv('_APP_DB_HOST_DOCUMENTSDB', 'mongodb'); - $databaseSharedTables = \array_filter(\explode(',', System::getEnv('_APP_DATABASE_DOCUMENTSDB_SHARED_TABLES', ''))); - break; - case VECTORSDB: - $databases = Config::getParam('pools-vectorsdb', []); - $databaseKeys = System::getEnv('_APP_DATABASE_VECTORSDB_KEYS', ''); - $databaseOverride = System::getEnv('_APP_DATABASE_VECTORSDB_OVERRIDE'); - $dbScheme = System::getEnv('_APP_DB_HOST_VECTORSDB', 'postgresql'); - $databaseSharedTables = \array_filter(\explode(',', System::getEnv('_APP_DATABASE_VECTORSDB_SHARED_TABLES', ''))); - break; - default: - // legacy/tablesdb - // it is already created during create project - return $dsn; - } - - $isSharedTables = false; - - if (!empty($dsn)) { - try { - $parsedDsn = new DSN($dsn); - $dsnHost = $parsedDsn->getHost(); - } catch (\InvalidArgumentException) { - $dsnHost = $dsn; - } - - $projectSharedTables = \explode(',', System::getEnv('_APP_DATABASE_SHARED_TABLES', '')); - $isSharedTables = \in_array($dsnHost, $projectSharedTables); - } - - if ($region !== 'default') { - $keys = explode(',', $databaseKeys); - $databases = array_filter($keys, function ($value) use ($region) { - return str_contains($value, $region); - }); - } - - $index = \array_search($databaseOverride, $databases); - if ($index !== false) { - $selectedDsn = $databases[$index]; - } else { - if (!empty($dsn) && !empty($databaseSharedTables)) { - if ($isSharedTables) { - $databases = array_filter($databases, fn ($value) => \in_array($value, $databaseSharedTables)); - } else { - $databases = array_filter($databases, fn ($value) => !\in_array($value, $databaseSharedTables)); - } - } - if (empty($databases)) { - throw new Exception(Exception::GENERAL_SERVER_ERROR, "No {$databasetype} database pool available for the current shared-tables mode"); - } - $selectedDsn = $databases[array_rand($databases)]; - } - - if (\in_array($selectedDsn, $databaseSharedTables)) { - $schema = 'appwrite'; - $database = 'appwrite'; - $namespace = System::getEnv('_APP_DATABASE_SHARED_NAMESPACE', ''); - $selectedDsn = $schema . '://' . $selectedDsn . '?database=' . $database; - - if (!empty($namespace)) { - $selectedDsn .= '&namespace=' . $namespace; - } - } - try { - new DSN($selectedDsn); - } catch (\InvalidArgumentException) { - $selectedDsn = $dbScheme.'://' . $selectedDsn; - } - - return $selectedDsn; + return Pool::dsn($this->getDatabaseType(), (string) $project->getAttribute('region', 'default'), $project->getAttribute('database')); } protected function getDatabaseCollection() diff --git a/src/Appwrite/Platform/Modules/Databases/Pool.php b/src/Appwrite/Platform/Modules/Databases/Pool.php new file mode 100644 index 0000000000..960dbd13ff --- /dev/null +++ b/src/Appwrite/Platform/Modules/Databases/Pool.php @@ -0,0 +1,100 @@ +getHost(); + } catch (\InvalidArgumentException) { + $projectHost = $projectDsn; + } + + $projectSharedTables = \explode(',', System::getEnv('_APP_DATABASE_SHARED_TABLES', '')); + $projectUsesSharedTables = \in_array($projectHost, $projectSharedTables); + } + + if ($region !== 'default') { + $databases = \array_filter( + \explode(',', $keys), + fn (string $value): bool => \str_contains($value, $region) + ); + } + + $index = \array_search($override, $databases); + if ($index !== false) { + $selectedDsn = $databases[$index]; + } else { + if (!empty($projectDsn) && !empty($sharedTables)) { + if ($projectUsesSharedTables) { + $databases = \array_filter($databases, fn (string $value): bool => \in_array($value, $sharedTables)); + } else { + $databases = \array_filter($databases, fn (string $value): bool => !\in_array($value, $sharedTables)); + } + } + + if (empty($databases)) { + throw new Exception(Exception::GENERAL_SERVER_ERROR, "No {$type} database pool available for the current shared-tables mode"); + } + + $selectedDsn = $databases[\array_rand($databases)]; + } + + if (\in_array($selectedDsn, $sharedTables)) { + $namespace = System::getEnv('_APP_DATABASE_SHARED_NAMESPACE', ''); + $selectedDsn = 'appwrite://' . $selectedDsn . '?database=appwrite'; + + if (!empty($namespace)) { + $selectedDsn .= '&namespace=' . $namespace; + } + } + + try { + new DSN($selectedDsn); + } catch (\InvalidArgumentException) { + $selectedDsn = $scheme . '://' . $selectedDsn; + } + + return $selectedDsn; + } +} diff --git a/tests/unit/Platform/Modules/Databases/PoolTest.php b/tests/unit/Platform/Modules/Databases/PoolTest.php new file mode 100644 index 0000000000..0ae7e2ebb7 --- /dev/null +++ b/tests/unit/Platform/Modules/Databases/PoolTest.php @@ -0,0 +1,127 @@ + */ + private array $params; + + /** @var array */ + private array $variables = []; + + protected function setUp(): void + { + $this->params = Config::$params; + + foreach (self::VARIABLES as $variable) { + $this->variables[$variable] = \getenv($variable); + \putenv($variable); + } + } + + protected function tearDown(): void + { + Config::$params = $this->params; + + foreach ($this->variables as $variable => $value) { + \putenv($value === false ? $variable : $variable . '=' . $value); + } + } + + public function testReturnsProjectDatabaseForTablesDB(): void + { + $dsn = 'mysql://project?database=appwrite'; + + $this->assertSame($dsn, Pool::dsn('tablesdb', 'default', $dsn)); + } + + public function testRejectsEmptyProjectDatabaseForTablesDB(): void + { + $this->expectException(Exception::class); + $this->expectExceptionMessage('Project database DSN is required for tablesdb databases'); + + Pool::dsn('tablesdb', 'default', null); + } + + public function testSelectsDocumentsDatabasePool(): void + { + Config::setParam('pools-documentsdb', ['documents']); + + $this->assertSame('mongodb://documents', Pool::dsn('documentsdb', 'default', null)); + } + + public function testSelectsVectorsDatabasePool(): void + { + Config::setParam('pools-vectorsdb', ['vectors']); + + $this->assertSame('postgresql://vectors', Pool::dsn('vectorsdb', 'default', null)); + } + + public function testSelectsSharedPoolForSharedProject(): void + { + Config::setParam('pools-documentsdb', ['documents-dedicated', 'documents-shared']); + \putenv('_APP_DATABASE_SHARED_TABLES=projects-shared'); + \putenv('_APP_DATABASE_DOCUMENTSDB_SHARED_TABLES=documents-shared'); + \putenv('_APP_DATABASE_SHARED_NAMESPACE=tenant'); + + $this->assertSame( + 'appwrite://documents-shared?database=appwrite&namespace=tenant', + Pool::dsn('documentsdb', 'default', 'mysql://projects-shared') + ); + } + + public function testSelectsDedicatedPoolForDedicatedProject(): void + { + Config::setParam('pools-documentsdb', ['documents-dedicated', 'documents-shared']); + \putenv('_APP_DATABASE_SHARED_TABLES=projects-shared'); + \putenv('_APP_DATABASE_DOCUMENTSDB_SHARED_TABLES=documents-shared'); + + $this->assertSame( + 'mongodb://documents-dedicated', + Pool::dsn('documentsdb', 'default', 'mysql://projects-dedicated') + ); + } + + public function testSelectsPoolForRegion(): void + { + Config::setParam('pools-documentsdb', ['default-documents']); + \putenv('_APP_DATABASE_DOCUMENTSDB_KEYS=fra-documents,nyc-documents'); + + $this->assertSame( + 'mongodb://fra-documents', + Pool::dsn('documentsdb', 'fra', null) + ); + } + + public function testOverrideTakesPrecedence(): void + { + Config::setParam('pools-documentsdb', ['documents-default', 'documents-override']); + \putenv('_APP_DATABASE_DOCUMENTSDB_OVERRIDE=documents-override'); + + $this->assertSame( + 'mongodb://documents-override', + Pool::dsn('documentsdb', 'default', null) + ); + } +}