diff --git a/backend/controller/dal/dal.go b/backend/controller/dal/dal.go index a0fd3b0c3c..816e16da48 100644 --- a/backend/controller/dal/dal.go +++ b/backend/controller/dal/dal.go @@ -752,12 +752,13 @@ 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 { @@ -765,6 +766,7 @@ func (d *DAL) ReplaceDeployment(ctx context.Context, newDeploymentKey model.Depl } 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 diff --git a/backend/controller/sql/querier.go b/backend/controller/sql/querier.go index b8a825dc83..44b526f84c 100644 --- a/backend/controller/sql/querier.go +++ b/backend/controller/sql/querier.go @@ -90,7 +90,6 @@ type Querier interface { PublishEventForTopic(ctx context.Context, arg PublishEventForTopicParams) error ReleaseLease(ctx context.Context, idempotencyKey uuid.UUID, key leases.Key) (bool, error) RenewLease(ctx context.Context, ttl time.Duration, idempotencyKey uuid.UUID, key leases.Key) (bool, error) - ReplaceDeployment(ctx context.Context, oldDeployment model.DeploymentKey, newDeployment model.DeploymentKey, minReplicas int32) (int64, error) // Find an idle runner and reserve it for the given deployment. ReserveRunner(ctx context.Context, reservationTimeout time.Time, deploymentKey model.DeploymentKey, labels []byte) (Runner, error) SetDeploymentDesiredReplicas(ctx context.Context, key model.DeploymentKey, minReplicas int32) error diff --git a/backend/controller/sql/queries.sql b/backend/controller/sql/queries.sql index fe860a3165..1a9e618cf6 100644 --- a/backend/controller/sql/queries.sql +++ b/backend/controller/sql/queries.sql @@ -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 diff --git a/backend/controller/sql/queries.sql.go b/backend/controller/sql/queries.sql.go index 00c8bb9646..720df76aa0 100644 --- a/backend/controller/sql/queries.sql.go +++ b/backend/controller/sql/queries.sql.go @@ -1856,26 +1856,6 @@ func (q *Queries) RenewLease(ctx context.Context, ttl time.Duration, idempotency return column_1, err } -const replaceDeployment = `-- name: ReplaceDeployment :one -WITH update_container AS ( - UPDATE deployments AS d - SET min_replicas = update_deployments.min_replicas - FROM (VALUES ($1::deployment_key, 0), - ($2::deployment_key, $3::INT)) - AS update_deployments(key, min_replicas) - WHERE d.key = update_deployments.key - RETURNING 1) -SELECT COUNT(*) -FROM update_container -` - -func (q *Queries) ReplaceDeployment(ctx context.Context, oldDeployment model.DeploymentKey, newDeployment model.DeploymentKey, minReplicas int32) (int64, error) { - row := q.db.QueryRow(ctx, replaceDeployment, oldDeployment, newDeployment, minReplicas) - var count int64 - err := row.Scan(&count) - return count, err -} - const reserveRunner = `-- name: ReserveRunner :one UPDATE runners SET state = 'reserved',