Compare commits

...
Author SHA1 Message Date
shimon 79f78c61da comparison fix 2023-11-16 23:13:14 +02:00
shimon 7ef0fbe741 Merge branch '1.4.x' of github.com:appwrite/appwrite into feat-delete-orphaned-projects
 Conflicts:
	src/Appwrite/Platform/Tasks/DeleteOrphanedProjects.php
2023-11-16 23:11:48 +02:00
shimon 7553c436df comparison fix 2023-11-16 23:10:27 +02:00
@@ -4,6 +4,7 @@ namespace Appwrite\Platform\Tasks;
use Utopia\App;
use Utopia\Config\Config;
use Utopia\Database\Helpers\ID;
use Utopia\Database\Query;
use Utopia\Platform\Action;
use Utopia\Cache\Cache;
@@ -45,6 +46,17 @@ class DeleteOrphanedProjects extends Action
/** @var array $collections */
$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 */
$app = new App('UTC');
$console = $app->getResource('console');
@@ -54,7 +66,7 @@ class DeleteOrphanedProjects extends Action
$totalProjects = $dbForConsole->count('projects');
Console::success("Found a total of: {$totalProjects} projects");
$orphans = 0;
$orphans = 1;
$cnt = 0;
$count = 0;
$limit = 30;
@@ -80,6 +92,7 @@ class DeleteOrphanedProjects extends Action
$dbForProject = new Database($adapter, $cache);
$dbForProject->setDefaultDatabase('appwrite');
$dbForProject->setNamespace('_' . $project->getInternalId());
$collectionsCreated = 0;
$cnt++;
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'] . ')';
/**
* +2 = audit+abuse
*/
if ($collectionsCreated >= (count($collectionsConfig) + 2)) {
if ($collectionsCreated >= count($collectionsConfig)) {
Console::log($msg . ' ignoring....');
continue;
}
@@ -107,6 +118,7 @@ class DeleteOrphanedProjects extends Action
Console::info('--Deleting collection (' . $collection->getId() . ') project no (' . $project->getInternalId() . ')');
}
}
if ($commit) {
$dbForConsole->deleteDocument('projects', $project->getId());
$dbForConsole->deleteCachedDocument('projects', $project->getId());
@@ -116,7 +128,7 @@ class DeleteOrphanedProjects extends Action
$orphans++;
} catch (\Throwable $th) {
Console::error('Error: ' . $th->getMessage());
Console::error('Error: ' . $th->getMessage() . ' ' . $th->getTraceAsString());
} finally {
$pools
->get($db)
@@ -135,6 +147,6 @@ class DeleteOrphanedProjects extends Action
$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');
}
}