Skip to content

Commit

Permalink
fix: replace deployment query broken into 2 queries (#2110)
Browse files Browse the repository at this point in the history
  • Loading branch information
matt2e authored Jul 18, 2024
1 parent b5bab9a commit 0ec4d16
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 37 deletions.
10 changes: 6 additions & 4 deletions backend/controller/dal/dal.go
Original file line number Diff line number Diff line change
Expand Up @@ -752,19 +752,21 @@ func (d *DAL) ReplaceDeployment(ctx context.Context, newDeploymentKey model.Depl
var replacedDeploymentKey optional.Option[model.DeploymentKey]
oldDeployment, err := tx.GetExistingDeploymentForModule(ctx, newDeployment.ModuleName)
if err == nil {
count, err := tx.ReplaceDeployment(ctx, oldDeployment.Key, newDeploymentKey, int32(minReplicas))
err = tx.SetDeploymentDesiredReplicas(ctx, oldDeployment.Key, 0)
if err != nil {
return fmt.Errorf("replace deployment failed to replace min replicas from %v to %v: %w", oldDeployment.Key, newDeploymentKey, dalerrs.TranslatePGError(err))
return fmt.Errorf("replace deployment failed to set old deployment replicas from %v to %v: %w", oldDeployment.Key, newDeploymentKey, dalerrs.TranslatePGError(err))
}
if count == 1 {
return fmt.Errorf("replace deployment failed: deployment already exists from %v to %v: %w", oldDeployment.Key, newDeploymentKey, ErrReplaceDeploymentAlreadyActive)
err = tx.SetDeploymentDesiredReplicas(ctx, newDeploymentKey, int32(minReplicas))
if err != nil {
return fmt.Errorf("replace deployment failed to set new deployment replicas from %v to %v: %w", oldDeployment.Key, newDeploymentKey, dalerrs.TranslatePGError(err))
}
err = d.deploymentWillDeactivate(ctx, tx, oldDeployment.Key)
if err != nil {
return fmt.Errorf("replace deployment failed willDeactivate trigger from %v to %v: %w", oldDeployment.Key, newDeploymentKey, dalerrs.TranslatePGError(err))
}
replacedDeploymentKey = optional.Some(oldDeployment.Key)
} else if !dalerrs.IsNotFound(err) {
// any error other than not found
return fmt.Errorf("replace deployment failed to get existing deployment for %v: %w", newDeploymentKey, dalerrs.TranslatePGError(err))
} else {
// Set the desired replicas for the new deployment
Expand Down
1 change: 0 additions & 1 deletion backend/controller/sql/querier.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 0 additions & 12 deletions backend/controller/sql/queries.sql
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,6 @@ RETURNING id;
INSERT INTO deployment_artefacts (deployment_id, artefact_id, executable, path)
VALUES ((SELECT id FROM deployments WHERE key = @key::deployment_key), $2, $3, $4);

-- name: ReplaceDeployment :one
WITH update_container AS (
UPDATE deployments AS d
SET min_replicas = update_deployments.min_replicas
FROM (VALUES (sqlc.arg('old_deployment')::deployment_key, 0),
(sqlc.arg('new_deployment')::deployment_key, sqlc.arg('min_replicas')::INT))
AS update_deployments(key, min_replicas)
WHERE d.key = update_deployments.key
RETURNING 1)
SELECT COUNT(*)
FROM update_container;

-- name: GetDeployment :one
SELECT sqlc.embed(d), m.language, m.name AS module_name, d.min_replicas
FROM deployments d
Expand Down
20 changes: 0 additions & 20 deletions backend/controller/sql/queries.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0ec4d16

Please sign in to comment.