Repository: appwrite/appwrite
File: src/Appwrite/Platform/Modules/Compute/Base.php (PHP)

Code before:
        $queueForBuilds
            ->setType(BUILD_TYPE_DEPLOYMENT)
            ->setResource($site)
            ->setDeployment($deployment)
            ->setTemplate($template);

        return $deployment;
    }

    /**
     * Update manual rule for new site deployment.
     * In case of fresh site, deployment ID will be empty in the rules, so we need to update it here.
     *
     * @param \Utopia\Database\Document $project
     * @param \Utopia\Database\Document $site
     * @param \Utopia\Database\Document $deployment
     * @param \Utopia\Database\Database $dbForPlatform
     * @return void
     */
    public function updateManualRuleForNewSiteDeployment(Document $project, Document $site, Document $deployment, Database $dbForPlatform)
    {
        $queries = [
            Query::equal('projectInternalId', [$project->getSequence()]),
            Query::equal('type', ['deployment']),
            Query::equal('deploymentResourceInternalId', [$site->getSequence()]),
            Query::equal('deploymentResourceType', ['site']),
            Query::equal('trigger', ['manual']),
        ];
        $dbForPlatform->forEach('rules', function (Document $rule) use ($deployment, $dbForPlatform) {
            if (empty($rule->getAttribute('deploymentId', ''))) {
                Authorization::skip(fn () => $dbForPlatform->updateDocument('rules', $rule->getId(), new Document([
                    'deploymentId' => $deployment->getId(),
                    'deploymentInternalId' => $deployment->getSequence(),
                ])));
            }
        }, $queries);
    }
}


Review comment (suggestion):
I would also add query for `deploymentId` being empty.

So in scenario when there is production dpeloyment, and developer makes new one, this behaviour doesnt kick in. If it did, we might be introducing downtime to deployments

Code after:
        $this->updateEmptyManualRule($project, $site, $deployment, $dbForPlatform);

        $queueForBuilds
            ->setType(BUILD_TYPE_DEPLOYMENT)
            ->setResource($site)
            ->setDeployment($deployment)
            ->setTemplate($template);

        return $deployment;
    }

    /**
     * Update empty manual rule for deployment.
     * In case of first deployment, deployment ID will be empty in the rules, so we need to update it here.
     *
     * @param \Utopia\Database\Document $project
     * @param \Utopia\Database\Document $resource
     * @param \Utopia\Database\Document $deployment
     * @param \Utopia\Database\Database $dbForPlatform
     * @return void
     */
    public static function updateEmptyManualRule(Document $project, Document $resource, Document $deployment, Database $dbForPlatform)
    {
        $resourceType = $resource->getCollection() === 'sites' ? 'site' : 'function';

        $queries = [
            Query::equal('projectInternalId', [$project->getSequence()]),
            Query::equal('deploymentResourceInternalId', [$resource->getSequence()]),
            Query::equal('deploymentResourceType', [$resourceType]),
            Query::equal('deploymentId', ['']),
            Query::equal('type', ['deployment']),
            Query::equal('trigger', ['manual']),
        ];
        $dbForPlatform->forEach('rules', function (Document $rule) use ($deployment, $dbForPlatform) {
            Authorization::skip(fn () => $dbForPlatform->updateDocument('rules', $rule->getId(), new Document([
                'deploymentId' => $deployment->getId(),
                'deploymentInternalId' => $deployment->getSequence(),
            ])));
        }, $queries);
    }
}