Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

chore(tenant): return tasks in a deterministic ascending order, otherwise the tasks in UI issue panel will change (#774)

Danny Xuf8e0bfbfd008
diff --git a/server/deploy.go b/server/deploy.go
index 67aace7378..6c0fd6108b 100644
--- a/server/deploy.go
+++ b/server/deploy.go
@@ -2,6 +2,7 @@ package server
import (
"encoding/json"
+ "sort"
"github.com/bytebase/bytebase/api"
)
@@ -46,7 +47,7 @@ func isMatchExpressions(labels map[string]string, expressionList []*api.LabelSel
// getDatabaseMatrixFromDeploymentSchedule gets a pipeline based on deployment schedule.
// The returned matrix doesn't include deployment with no matched database.
func getDatabaseMatrixFromDeploymentSchedule(schedule *api.DeploymentSchedule, baseDatabaseName, dbNameTemplate string, databaseList []*api.Database) ([]*api.Deployment, [][]*api.Database, error) {
- var pipeline [][]*api.Database
+ var matrix [][]*api.Database
var deployments []*api.Deployment
// idToLabels maps databaseID -> label.Key -> label.Value
@@ -95,18 +96,22 @@ func getDatabaseMatrixFromDeploymentSchedule(schedule *api.DeploymentSchedule, b
}
}
- var stage []*api.Database
+ var databaseList []*api.Database
for _, id := range matchedDatabaseList {
- stage = append(stage, databaseMap[id])
+ databaseList = append(databaseList, databaseMap[id])
}
+ // sort databases in stage based on names.
+ sort.Slice(databaseList, func(i, j int) bool {
+ return databaseList[i].Name > databaseList[j].Name
+ })
- if len(stage) > 0 {
- pipeline = append(pipeline, stage)
+ if len(databaseList) > 0 {
+ matrix = append(matrix, databaseList)
deployments = append(deployments, deployment)
}
}
- return deployments, pipeline, nil
+ return deployments, matrix, nil
}
// formatDatabaseName will return the full database name given the dbNameTemplate, base database name, and labels.
diff --git a/server/deploy_test.go b/server/deploy_test.go
index 9615966ee3..be4efc7127 100644
--- a/server/deploy_test.go
+++ b/server/deploy_test.go
@@ -243,11 +243,11 @@ func TestGetDatabaseMatrixFromDeploymentSchedule(t *testing.T) {
}
for _, test := range tests {
- _, create, _ := getDatabaseMatrixFromDeploymentSchedule(test.schedule, test.baseDatabaseName, test.databaseNameTemplate, test.databaseList)
+ _, matrix, _ := getDatabaseMatrixFromDeploymentSchedule(test.schedule, test.baseDatabaseName, test.databaseNameTemplate, test.databaseList)
- diff := pretty.Diff(create, test.want)
+ diff := pretty.Diff(matrix, test.want)
if len(diff) > 0 {
- t.Errorf("%q: getPipelineFromDeploymentSchedule() got create %+v, want %+v, diff %+v.", test.name, create, test.want, diff)
+ t.Errorf("%q: getPipelineFromDeploymentSchedule() got matrix %+v, want %+v, diff %+v.", test.name, matrix, test.want, diff)
}
}
}
diff --git a/server/issue.go b/server/issue.go
index 107044422c..63484435fe 100644
--- a/server/issue.go
+++ b/server/issue.go
@@ -714,17 +714,17 @@ func (s *Server) createPipelineFromIssue(ctx context.Context, issueCreate *api.I
if err != nil {
return nil, fmt.Errorf("api.GetBaseDatabaseName(%q, %q) failed, error: %v", d.DatabaseName, project.DBNameTemplate, err)
}
- deployments, p, err := s.getTenantDatabaseMatrix(ctx, issueCreate.ProjectID, project.DBNameTemplate, databaseList, baseDatabaseName)
+ deployments, matrix, err := s.getTenantDatabaseMatrix(ctx, issueCreate.ProjectID, project.DBNameTemplate, databaseList, baseDatabaseName)
if err != nil {
return nil, err
}
// Convert to pipelineCreate
- for i, stage := range p {
+ for i, databaseList := range matrix {
// Since environment is required for stage, we use an internal bb system environment for tenant deployments.
environmentSet := make(map[string]bool)
var environmentID int
var taskCreateList []api.TaskCreate
- for _, database := range stage {
+ for _, database := range databaseList {
environmentSet[database.Instance.Environment.Name] = true
environmentID = database.Instance.EnvironmentID
taskStatus := api.TaskPendingApproval
@@ -1050,11 +1050,11 @@ func (s *Server) getTenantDatabaseMatrix(ctx context.Context, projectID int, dbN
return nil, nil, echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to compose database relationship for database ID %v", database.ID)).SetInternal(err)
}
}
- d, p, err := getDatabaseMatrixFromDeploymentSchedule(deploySchedule, baseDatabaseName, dbNameTemplate, databaseList)
+ d, matrix, err := getDatabaseMatrixFromDeploymentSchedule(deploySchedule, baseDatabaseName, dbNameTemplate, databaseList)
if err != nil {
return nil, nil, echo.NewHTTPError(http.StatusInternalServerError, "Failed to create deployment pipeline").SetInternal(err)
}
- return d, p, nil
+ return d, matrix, nil
}
// getSchemaFromPeerTenantDatabase gets the schema version and schema from a peer tenant database.
@@ -1070,11 +1070,11 @@ func (s *Server) getSchemaFromPeerTenantDatabase(ctx context.Context, instance *
return "", "", echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch databases in project ID: %v", projectID)).SetInternal(err)
}
- _, pipeline, err := s.getTenantDatabaseMatrix(ctx, projectID, project.DBNameTemplate, databaseList, baseDatabaseName)
+ _, matrix, err := s.getTenantDatabaseMatrix(ctx, projectID, project.DBNameTemplate, databaseList, baseDatabaseName)
if err != nil {
return "", "", err
}
- similarDB := getPeerTenantDatabase(pipeline, instance.EnvironmentID)
+ similarDB := getPeerTenantDatabase(matrix, instance.EnvironmentID)
// When there is no existing tenant, we will look at all existing databases in the tenant mode project.
// If there are existing databases with the same name, we will disallow the database creation.
diff --git a/store/stage.go b/store/stage.go
index d3a6d9df97..b101a643a4 100644
--- a/store/stage.go
+++ b/store/stage.go
@@ -148,7 +148,7 @@ func (s *StageService) findStageList(ctx context.Context, tx *sql.Tx, find *api.
environment_id,
name
FROM stage
- WHERE `+strings.Join(where, " AND "),
+ WHERE `+strings.Join(where, " AND ")+` ORDER BY id ASC`,
args...,
)
if err != nil {
diff --git a/store/task.go b/store/task.go
index 5330b10ff2..fac0315b9f 100644
--- a/store/task.go
+++ b/store/task.go
@@ -277,7 +277,7 @@ func (s *TaskService) findTaskList(ctx context.Context, tx *sql.Tx, find *api.Ta
payload,
earliest_allowed_ts
FROM task
- WHERE `+strings.Join(where, " AND "),
+ WHERE `+strings.Join(where, " AND ")+` ORDER BY id ASC`,
args...,
)
if err != nil {