diff --git a/app/controllers/api/projects.php b/app/controllers/api/projects.php index 43918ee98d..ffb60afca4 100644 --- a/app/controllers/api/projects.php +++ b/app/controllers/api/projects.php @@ -17,7 +17,6 @@ use Appwrite\SDK\Method; use Appwrite\SDK\Response as SDKResponse; use Appwrite\Template\Template; use Appwrite\Utopia\Database\Validator\ProjectId; -use Appwrite\Utopia\Database\Validator\Queries\Projects; use Appwrite\Utopia\Request; use Appwrite\Utopia\Response; use PHPMailer\PHPMailer\PHPMailer; @@ -52,8 +51,26 @@ use Utopia\Validator\Text; use Utopia\Validator\URL; use Utopia\Validator\WhiteList; -function getDatabaseDSN(string $databasetype, $region): string +// for returning dsn of multitype dbs with type optionally based on input dsn +function getDatabaseDSN(string $databasetype, $region, ?string $dsn = null): string { + $isSharedTablesV1 = false; + $isSharedTablesV2 = false; + if (!empty($dsn)) { + try { + $parsedDsn = new DSN($dsn); + $dsnHost = $parsedDsn->getHost(); + } catch (\InvalidArgumentException) { + $dsnHost = $dsn; + } + + $sharedTables = \explode(',', System::getEnv('_APP_DATABASE_SHARED_TABLES', '')); + $sharedTablesV1 = \explode(',', System::getEnv('_APP_DATABASE_SHARED_TABLES_V1', '')); + $sharedTablesV2 = \array_diff($sharedTables, $sharedTablesV1); + $isSharedTablesV1 = \in_array($dsnHost, $sharedTablesV1); + $isSharedTablesV2 = \in_array($dsnHost, $sharedTablesV2); + } + $databases = []; $databaseKeys = []; /** @@ -61,12 +78,18 @@ function getDatabaseDSN(string $databasetype, $region): string */ $databaseOverride = ''; $dbScheme = ''; + $sharedTables = []; + $sharedTablesV1 = []; + $sharedTablesV2 = []; + switch ($databasetype) { case 'documentsDatabase': $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'); + $sharedTables = \explode(',', System::getEnv('_APP_DATABASE_DOCUMENTSDB_SHARED_TABLES', '')); + $sharedTablesV1 = \explode(',', System::getEnv('_APP_DATABASE_DOCUMENTSDB_SHARED_TABLES_V1', '')); break; default: // legacy/tablesdb @@ -74,6 +97,8 @@ function getDatabaseDSN(string $databasetype, $region): string $databaseKeys = System::getEnv('_APP_DATABASE_KEYS', ''); $databaseOverride = System::getEnv('_APP_DATABASE_OVERRIDE'); $dbScheme = System::getEnv('_APP_DB_HOST', 'mysql'); + $sharedTables = \explode(',', System::getEnv('_APP_DATABASE_SHARED_TABLES', '')); + $sharedTablesV1 = \explode(',', System::getEnv('_APP_DATABASE_SHARED_TABLES_V1', '')); break; } @@ -83,32 +108,40 @@ function getDatabaseDSN(string $databasetype, $region): string return str_contains($value, $region); }); } + $sharedTablesV2 = \array_diff($sharedTables, $sharedTablesV1); $index = \array_search($databaseOverride, $databases); if ($index !== false) { - $dsn = $databases[$index]; + $selectedDsn = $databases[$index]; } else { - $dsn = $databases[array_rand($databases)]; + if (!empty($dsn)) { + if ($isSharedTablesV1) { + $databases = array_filter($databases, fn ($value) => \in_array($value, $sharedTablesV1)); + } elseif ($isSharedTablesV2) { + $databases = array_filter($databases, fn ($value) => \in_array($value, $sharedTablesV2)); + } else { + $databases = array_filter($databases, fn ($value) => !\in_array($value, $sharedTables)); + } + } + $selectedDsn = !empty($databases) ? $databases[array_rand($databases)] : ''; } - $sharedTables = \explode(',', System::getEnv('_APP_DATABASE_SHARED_TABLES', '')); - if (\in_array($dsn, $sharedTables)) { + if (\in_array($selectedDsn, $sharedTables)) { $schema = 'appwrite'; $database = 'appwrite'; $namespace = System::getEnv('_APP_DATABASE_SHARED_NAMESPACE', ''); - $dsn = $schema . '://' . $dsn . '?database=' . $database; + $selectedDsn = $schema . '://' . $selectedDsn . '?database=' . $database; if (!empty($namespace)) { - $dsn .= '&namespace=' . $namespace; + $selectedDsn .= '&namespace=' . $namespace; } } try { - // for validation - new DSN($dsn); + new DSN($selectedDsn); } catch (\InvalidArgumentException) { - $dsn = $dbScheme.'://' . $dsn; + $selectedDsn = $dbScheme.'://' . $selectedDsn; } - return $dsn; + return $selectedDsn; } App::init() @@ -233,7 +266,7 @@ App::post('/v1/projects') 'accessedAt' => DateTime::now(), 'search' => implode(' ', [$projectId, $name]), 'database' => $dsn, - 'documentsDatabase' => getDatabaseDSN('documentsDatabase', $region) + 'documentsDatabase' => getDatabaseDSN('documentsDatabase', $region, $dsn) ])); } catch (Duplicate) { throw new Exception(Exception::PROJECT_ALREADY_EXISTS); diff --git a/composer.json b/composer.json index cf91d42890..5b24ec2758 100644 --- a/composer.json +++ b/composer.json @@ -65,7 +65,7 @@ "utopia-php/locale": "0.8.*", "utopia-php/logger": "0.6.*", "utopia-php/messaging": "0.20.*", - "utopia-php/migration": "dev-feat-documents-db as 1.1.1", + "utopia-php/migration": "1.*", "utopia-php/orchestration": "0.9.*", "utopia-php/platform": "0.7.*", "utopia-php/pools": "0.8.*", diff --git a/composer.lock b/composer.lock index ca89343ceb..69de044b10 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "fd23f533058976f75c6da390654d0eff", + "content-hash": "3999dacf9f2fc437123cca148964cb7e", "packages": [ { "name": "adhocore/jwt", @@ -4451,16 +4451,16 @@ }, { "name": "utopia-php/migration", - "version": "dev-feat-documents-db", + "version": "1.4.0", "source": { "type": "git", "url": "https://github.com/utopia-php/migration.git", - "reference": "bcd7bcb634dea60485c16648e68ed7a255315560" + "reference": "18bd7d39dcee09280f40edb12879c727ecec98d3" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/migration/zipball/bcd7bcb634dea60485c16648e68ed7a255315560", - "reference": "bcd7bcb634dea60485c16648e68ed7a255315560", + "url": "https://api.github.com/repos/utopia-php/migration/zipball/18bd7d39dcee09280f40edb12879c727ecec98d3", + "reference": "18bd7d39dcee09280f40edb12879c727ecec98d3", "shasum": "" }, "require": { @@ -4500,9 +4500,9 @@ ], "support": { "issues": "https://github.com/utopia-php/migration/issues", - "source": "https://github.com/utopia-php/migration/tree/feat-documents-db" + "source": "https://github.com/utopia-php/migration/tree/1.4.0" }, - "time": "2025-11-20T13:04:52+00:00" + "time": "2025-11-21T06:08:59+00:00" }, { "name": "utopia-php/mongo", @@ -8886,18 +8886,9 @@ "time": "2024-03-07T20:33:40+00:00" } ], - "aliases": [ - { - "package": "utopia-php/migration", - "version": "dev-feat-documents-db", - "alias": "1.1.1", - "alias_normalized": "1.1.1.0" - } - ], + "aliases": [], "minimum-stability": "dev", - "stability-flags": { - "utopia-php/migration": 20 - }, + "stability-flags": {}, "prefer-stable": true, "prefer-lowest": false, "platform": {