Repository: appwrite/appwrite
File: src/Appwrite/Platform/Modules/Functions/Workers/Builds.php (PHP)

Code before:
            $logs = $deployment->getAttribute('buildLogs', '');
            $date = \date('H:i:s');
            $logs .= "[90m[$date] [90m[[0mappwrite[90m][32m Deployment finished. [0m\n";
            $deployment->setAttribute('buildLogs', $logs);

            /** Update the status */
            $deployment->setAttribute('status', 'ready');
            $deployment = $dbForProject->updateDocument('deployments', $deploymentId, $deployment);

            if ($deployment->getSequence() === $resource->getAttribute('latestDeploymentInternalId', '')) {
                $dbForProject->updateDocument($resource->getCollection(), $resource->getId(), new Document(['latestDeploymentStatus' => $deployment->getAttribute('status', '')]));
            }

            $queueForRealtime
                ->setPayload($deployment->getArrayCopy())
                ->trigger();

            if ($isVcsEnabled) {
                $this->runGitAction('ready', $github, $providerCommitHash, $owner, $repositoryName, $project, $resource, $deployment->getId(), $dbForProject, $dbForPlatform);
            }

            Console::success("Build id: $deploymentId created");

            /** Set auto deploy */
            $activateBuild = true;
            if ($deployment->getAttribute('activate') === true) {
                // Check if current active deployment started later than this deployment
                $resource = $dbForProject->getDocument($resource->getCollection(), $resource->getId());
                $currentActiveDeploymentId = $resource->getAttribute('deploymentId', '');
                if (!empty($currentActiveDeploymentId)) {
                    $currentActiveDeployment = $dbForProject->getDocument('deployments', $currentActiveDeploymentId);
                    if (!$currentActiveDeployment->isEmpty()) {
                        $currentActiveStartTime = $currentActiveDeployment->getAttribute('buildStartedAt', '');
                        $deploymentStartTime = $deployment->getAttribute('buildStartedAt', '');

                        // Skip auto-activation if current active deployment started later than deployment that is being activated
                        if (!empty($currentActiveStartTime) && !empty($deploymentStartTime) && $currentActiveStartTime > $deploymentStartTime) {
                            Console::info('Skipping auto-activation as current deployment is more recent');
                            $activateBuild = false;
                        }
                    }
                }

                if ($activateBuild) {
                    switch ($resource->getCollection()) {
                        case 'functions':
                            $resource = $dbForProject->updateDocument('functions', $resource->getId(), new Document([
                                'live' => true,
                                'deploymentId' => $deployment->getId(),
                                'deploymentInternalId' => $deployment->getSequence(),
                                'deploymentCreatedAt' => $deployment->getCreatedAt(),

Review comment (bug):
I dont like `activateBuild` being `true` by default.
I am worried due to bug, we might be activating preview deployment, which is terrible experience.

To be safe, let's update this to be `false` by default, and only set to `true` if deployment has activate true, and it's not having race condition issue

Code after:
            $logs = $deployment->getAttribute('buildLogs', '');
            $date = \date('H:i:s');
            $logs .= "[90m[$date] [90m[[0mappwrite[90m][32m Deployment finished. [0m\n";
            $deployment->setAttribute('buildLogs', $logs);

            /** Update the status */
            $deployment->setAttribute('status', 'ready');
            $deployment = $dbForProject->updateDocument('deployments', $deploymentId, $deployment);

            if ($deployment->getSequence() === $resource->getAttribute('latestDeploymentInternalId', '')) {
                $resource = $dbForProject->updateDocument($resource->getCollection(), $resource->getId(), new Document(['latestDeploymentStatus' => $deployment->getAttribute('status', '')]));
            }

            $queueForRealtime
                ->setPayload($deployment->getArrayCopy())
                ->trigger();

            if ($isVcsEnabled) {
                $this->runGitAction('ready', $github, $providerCommitHash, $owner, $repositoryName, $project, $resource, $deployment->getId(), $dbForProject, $dbForPlatform);
            }

            Console::success("Build id: $deploymentId created");

            /** Set auto deploy */
            $activateBuild = false;
            if ($deployment->getAttribute('activate') === true) {
                // Check if current active deployment started later than this deployment
                $resource = $dbForProject->getDocument($resource->getCollection(), $resource->getId());
                $currentActiveDeploymentId = $resource->getAttribute('deploymentId', '');
                if (!empty($currentActiveDeploymentId)) {
                    $currentActiveDeployment = $dbForProject->getDocument('deployments', $currentActiveDeploymentId);
                    if (!$currentActiveDeployment->isEmpty()) {
                        $currentActiveStartTime = $currentActiveDeployment->getCreatedAt();
                        $deploymentStartTime = $deployment->getCreatedAt();

                        // Skip auto-activation if current active deployment started later than deployment that is being activated
                        if ($currentActiveStartTime < $deploymentStartTime) {
                            $activateBuild = true;
                        } else {
                            Console::info('Skipping auto-activation as current deployment is more recent');
                        }
                    }
                } else {
                    $activateBuild = true;
                }
            }

            if ($activateBuild) {
                switch ($resource->getCollection()) {
                    case 'functions':
                        $resource = $dbForProject->updateDocument('functions', $resource->getId(), new Document([