1
0
Fork 0
mirror of synced 2024-10-02 18:26:49 +13:00

comparison fix

This commit is contained in:
shimon 2023-11-16 23:14:26 +02:00
parent 8013d79ae0
commit 174449ce49

View file

@ -4,6 +4,7 @@ namespace Appwrite\Platform\Tasks;
use Utopia\App; use Utopia\App;
use Utopia\Config\Config; use Utopia\Config\Config;
use Utopia\Database\Helpers\ID;
use Utopia\Database\Query; use Utopia\Database\Query;
use Utopia\Platform\Action; use Utopia\Platform\Action;
use Utopia\Cache\Cache; use Utopia\Cache\Cache;
@ -45,6 +46,17 @@ class DeleteOrphanedProjects extends Action
/** @var array $collections */ /** @var array $collections */
$collectionsConfig = Config::getParam('collections', [])['projects'] ?? []; $collectionsConfig = Config::getParam('collections', [])['projects'] ?? [];
$collectionsConfig = array_merge([
'audit' => [
'$id' => ID::custom('audit'),
'$collection' => Database::METADATA
],
'abuse' => [
'$id' => ID::custom('abuse'),
'$collection' => Database::METADATA
]
], $collectionsConfig);
/* Initialise new Utopia app */ /* Initialise new Utopia app */
$app = new App('UTC'); $app = new App('UTC');
$console = $app->getResource('console'); $console = $app->getResource('console');
@ -54,7 +66,7 @@ class DeleteOrphanedProjects extends Action
$totalProjects = $dbForConsole->count('projects'); $totalProjects = $dbForConsole->count('projects');
Console::success("Found a total of: {$totalProjects} projects"); Console::success("Found a total of: {$totalProjects} projects");
$orphans = 0; $orphans = 1;
$cnt = 0; $cnt = 0;
$count = 0; $count = 0;
$limit = 30; $limit = 30;
@ -80,6 +92,7 @@ class DeleteOrphanedProjects extends Action
$dbForProject = new Database($adapter, $cache); $dbForProject = new Database($adapter, $cache);
$dbForProject->setDefaultDatabase('appwrite'); $dbForProject->setDefaultDatabase('appwrite');
$dbForProject->setNamespace('_' . $project->getInternalId()); $dbForProject->setNamespace('_' . $project->getInternalId());
$collectionsCreated = 0; $collectionsCreated = 0;
$cnt++; $cnt++;
if ($dbForProject->exists($dbForProject->getDefaultDatabase(), Database::METADATA)) { if ($dbForProject->exists($dbForProject->getDefaultDatabase(), Database::METADATA)) {
@ -87,10 +100,8 @@ class DeleteOrphanedProjects extends Action
} }
$msg = '(' . $cnt . ') found (' . $collectionsCreated . ') collections on project (' . $project->getInternalId() . ') , database (' . $project['database'] . ')'; $msg = '(' . $cnt . ') found (' . $collectionsCreated . ') collections on project (' . $project->getInternalId() . ') , database (' . $project['database'] . ')';
/**
* +2 = audit+abuse if ($collectionsCreated >= count($collectionsConfig)) {
*/
if ($collectionsCreated >= (count($collectionsConfig) + 2)) {
Console::log($msg . ' ignoring....'); Console::log($msg . ' ignoring....');
continue; continue;
} }
@ -107,6 +118,7 @@ class DeleteOrphanedProjects extends Action
Console::info('--Deleting collection (' . $collection->getId() . ') project no (' . $project->getInternalId() . ')'); Console::info('--Deleting collection (' . $collection->getId() . ') project no (' . $project->getInternalId() . ')');
} }
} }
if ($commit) { if ($commit) {
$dbForConsole->deleteDocument('projects', $project->getId()); $dbForConsole->deleteDocument('projects', $project->getId());
$dbForConsole->deleteCachedDocument('projects', $project->getId()); $dbForConsole->deleteCachedDocument('projects', $project->getId());
@ -116,7 +128,7 @@ class DeleteOrphanedProjects extends Action
$orphans++; $orphans++;
} catch (\Throwable $th) { } catch (\Throwable $th) {
Console::error('Error: ' . $th->getMessage()); Console::error('Error: ' . $th->getMessage() . ' ' . $th->getTraceAsString());
} finally { } finally {
$pools $pools
->get($db) ->get($db)
@ -135,6 +147,6 @@ class DeleteOrphanedProjects extends Action
$count = $count + $sum; $count = $count + $sum;
} }
Console::log('Iterated through ' . $count - 1 . '/' . $totalProjects . ' projects found ' . $orphans . ' orphans'); Console::log('Iterated through ' . $count - 1 . '/' . $totalProjects . ' projects found ' . $orphans - 1 . ' orphans');
} }
} }