appwrite/src/Appwrite/Platform/Tasks/DeleteOrphanedProjects.php

141 lines
4.9 KiB
PHP
Raw Normal View History

2023-10-26 14:45:23 +00:00
<?php
namespace Appwrite\Platform\Tasks;
use Utopia\App;
use Utopia\Config\Config;
use Utopia\Database\Query;
use Utopia\Platform\Action;
use Utopia\Cache\Cache;
use Utopia\CLI\Console;
use Utopia\Database\Database;
use Utopia\Pools\Group;
use Utopia\Registry\Registry;
2023-11-08 10:05:51 +00:00
use Utopia\Validator\Boolean;
2023-10-26 14:45:23 +00:00
class DeleteOrphanedProjects extends Action
{
public static function getName(): string
{
return 'delete-orphaned-projects';
}
public function __construct()
{
$this
2023-11-13 17:24:55 +00:00
->desc('Delete orphaned projects')
2023-11-13 17:28:48 +00:00
->param('commit', false, new Boolean(true), 'Commit project deletion', true)
2023-10-26 14:45:23 +00:00
->inject('pools')
->inject('cache')
->inject('dbForConsole')
->inject('register')
2023-11-08 10:05:51 +00:00
->callback(function (bool $commit, Group $pools, Cache $cache, Database $dbForConsole, Registry $register) {
$this->action($commit, $pools, $cache, $dbForConsole, $register);
2023-10-26 14:45:23 +00:00
});
}
2023-11-08 10:05:51 +00:00
public function action(bool $commit, Group $pools, Cache $cache, Database $dbForConsole, Registry $register): void
2023-10-26 14:45:23 +00:00
{
Console::title('Delete orphaned projects V1');
Console::success(APP_NAME . ' Delete orphaned projects started');
2023-11-13 17:24:55 +00:00
/** @var array $collections */
$collectionsConfig = Config::getParam('collections', [])['projects'] ?? [];
2023-10-26 14:45:23 +00:00
/* Initialise new Utopia app */
$app = new App('UTC');
$console = $app->getResource('console');
$projects = [$console];
/** Database connections */
$totalProjects = $dbForConsole->count('projects');
Console::success("Found a total of: {$totalProjects} projects");
2023-11-08 10:05:51 +00:00
$orphans = 1;
2023-11-13 17:24:55 +00:00
$cnt = 0;
2023-10-26 14:45:23 +00:00
$count = 0;
$limit = 30;
$sum = 30;
$offset = 0;
while (!empty($projects)) {
foreach ($projects as $project) {
/**
* Skip user projects with id 'console'
*/
if ($project->getId() === 'console') {
continue;
}
try {
$db = $project->getAttribute('database');
$adapter = $pools
->get($db)
->pop()
->getResource();
$dbForProject = new Database($adapter, $cache);
$dbForProject->setDefaultDatabase('appwrite');
$dbForProject->setNamespace('_' . $project->getInternalId());
2023-11-13 17:24:55 +00:00
$collectionsCreated = 0;
$cnt++;
if ($dbForProject->exists($dbForProject->getDefaultDatabase(), Database::METADATA)) {
$collectionsCreated = $dbForProject->count(Database::METADATA);
}
2023-11-13 17:27:20 +00:00
$msg = '(' . $cnt . ') found (' . $collectionsCreated . ') collections on project (' . $project->getInternalId() . ') , database (' . $project['database'] . ')';
2023-11-13 17:24:55 +00:00
/**
2023-11-13 17:27:20 +00:00
* +2 = audit+abuse
2023-11-13 17:24:55 +00:00
*/
if ($collectionsCreated === (count($collectionsConfig) + 2)) {
Console::log($msg . ' ignoring....');
continue;
}
Console::log($msg);
if ($collectionsCreated > 0) {
$collections = $dbForProject->find(Database::METADATA, []);
foreach ($collections as $collection) {
if ($commit) {
$dbForProject->deleteCollection($collection->getId());
$dbForConsole->deleteCachedCollection($collection->getId());
}
Console::info('--Deleting collection (' . $collection->getId() . ') project no (' . $project->getInternalId() . ')');
2023-11-08 10:05:51 +00:00
}
2023-10-26 17:53:36 +00:00
}
2023-11-13 17:24:55 +00:00
if ($commit) {
$dbForConsole->deleteDocument('projects', $project->getId());
$dbForConsole->deleteCachedDocument('projects', $project->getId());
2023-11-08 10:05:51 +00:00
}
2023-11-13 17:24:55 +00:00
Console::info('--Deleting project no (' . $project->getInternalId() . ')');
$orphans++;
} catch (\Throwable $th) {
Console::error('Error: ' . $th->getMessage());
2023-10-26 14:45:23 +00:00
} finally {
$pools
->get($db)
->reclaim();
}
}
$sum = \count($projects);
$projects = $dbForConsole->find('projects', [
Query::limit($limit),
Query::offset($offset),
]);
$offset = $offset + $limit;
$count = $count + $sum;
}
2023-11-08 10:05:51 +00:00
Console::log('Iterated through ' . $count - 1 . '/' . $totalProjects . ' projects found ' . $orphans - 1 . ' orphans');
}
2023-10-26 14:45:23 +00:00
}