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

75 lines
2.5 KiB
PHP
Raw Normal View History

2023-03-22 18:52:43 +00:00
<?php
namespace Appwrite\Platform\Tasks;
use Utopia\Platform\Action;
use Utopia\CLI\Console;
use Utopia\Database\Query;
use Utopia\Database\Database;
use Utopia\Database\Validator\Authorization;
2023-03-22 19:35:04 +00:00
use Utopia\Pools\Group;
2023-03-22 18:52:43 +00:00
class PatchDeleteScheduleUpdatedAtAttribute extends Action
{
public static function getName(): string
{
return 'patch-delete-schedule-updated-at-attribute';
}
public function __construct()
{
$this
->desc('Ensure function collections do not have scheduleUpdatedAt attribute')
2023-03-22 19:35:04 +00:00
->inject('pools')
2023-03-22 18:52:43 +00:00
->inject('dbForConsole')
->inject('getProjectDB')
2023-03-22 19:35:04 +00:00
->callback(fn (Group $pools, Database $dbForConsole, callable $getProjectDB) => $this->action($pools, $dbForConsole, $getProjectDB));
2023-03-22 18:52:43 +00:00
}
/**
* Iterate over every function on every project to make sure there is a schedule. If not, recreate the schedule.
*/
2023-03-22 19:35:04 +00:00
public function action(Group $pools, Database $dbForConsole, callable $getProjectDB): void
2023-03-22 18:52:43 +00:00
{
Authorization::disable();
Authorization::setDefaultStatus(false);
Console::title('PatchDeleteScheduleUpdatedAtAttribute V1');
Console::success(APP_NAME . ' PatchDeleteScheduleUpdatedAtAttribute v1 has started');
$limit = 100;
$projectCursor = null;
while (true) {
$projectsQueries = [Query::limit($limit)];
if ($projectCursor !== null) {
$projectsQueries[] = Query::cursorAfter($projectCursor);
}
$projects = $dbForConsole->find('projects', $projectsQueries);
if (count($projects) === 0) {
break;
}
foreach ($projects as $project) {
Console::log("Checking Project " . $project->getAttribute('name') . " (" . $project->getId() . ")");
$dbForProject = $getProjectDB($project);
try {
/**
* Delete 'scheduleUpdatedAt' attribute
*/
$dbForProject->deleteAttribute('functions', 'scheduleUpdatedAt');
$dbForProject->deleteCachedCollection('functions');
Console::success("'scheduleUpdatedAt' deleted.");
} catch (\Throwable $th) {
Console::warning("'scheduleUpdatedAt' errored: {$th->getMessage()}");
}
2023-03-22 19:35:04 +00:00
$pools->reclaim();
2023-03-22 18:52:43 +00:00
}
$projectCursor = $projects[array_key_last($projects)];
}
}
}