Skip to content

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

refactor: getDatabaseMatrixFromDeploymentSchedule should include empty stage (#3546)

Danny Xu2805778744ca
diff --git a/server/deploy.go b/server/deploy.go
index 96f0ee78ef..ec896a28b6 100644
--- a/server/deploy.go
+++ b/server/deploy.go
@@ -45,10 +45,9 @@ 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) {
+// The matrix will include the stage even if the stage has no database.
+func getDatabaseMatrixFromDeploymentSchedule(schedule *api.DeploymentSchedule, baseDatabaseName, dbNameTemplate string, databaseList []*api.Database) ([][]*api.Database, error) {
var matrix [][]*api.Database
- var deployments []*api.Deployment
// idToLabels maps databaseID -> label.Key -> label.Value
idToLabels := make(map[int]map[string]string)
@@ -60,7 +59,7 @@ func getDatabaseMatrixFromDeploymentSchedule(schedule *api.DeploymentSchedule, b
}
var labelList []*api.DatabaseLabel
if err := json.Unmarshal([]byte(database.Labels), &labelList); err != nil {
- return nil, nil, err
+ return nil, err
}
for _, label := range labelList {
idToLabels[database.ID][label.Key] = label.Value
@@ -104,18 +103,17 @@ func getDatabaseMatrixFromDeploymentSchedule(schedule *api.DeploymentSchedule, b
for _, id := range matchedDatabaseList {
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
- })
-
+ // sort databases in stage based on IDs.
if len(databaseList) > 0 {
- matrix = append(matrix, databaseList)
- deployments = append(deployments, deployment)
+ sort.Slice(databaseList, func(i, j int) bool {
+ return databaseList[i].ID < databaseList[j].ID
+ })
}
+
+ matrix = append(matrix, databaseList)
}
- return deployments, matrix, nil
+ return 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 eeb0495ef9..4847dfff0f 100644
--- a/server/deploy_test.go
+++ b/server/deploy_test.go
@@ -181,6 +181,7 @@ func TestGetDatabaseMatrixFromDeploymentSchedule(t *testing.T) {
},
[][]*api.Database{
{dbs[0], dbs[2]},
+ nil,
},
},
{
@@ -244,7 +245,7 @@ func TestGetDatabaseMatrixFromDeploymentSchedule(t *testing.T) {
}
for _, test := range tests {
- _, matrix, _ := getDatabaseMatrixFromDeploymentSchedule(test.schedule, test.baseDatabaseName, test.databaseNameTemplate, test.databaseList)
- assert.Equal(t, matrix, test.want)
+ matrix, _ := getDatabaseMatrixFromDeploymentSchedule(test.schedule, test.baseDatabaseName, test.databaseNameTemplate, test.databaseList)
+ assert.Equal(t, matrix, test.want, test.name)
}
}
diff --git a/server/issue.go b/server/issue.go
index a6fb89dd22..02721662cf 100644
--- a/server/issue.go
+++ b/server/issue.go
@@ -807,13 +807,17 @@ func (s *Server) getPipelineCreateForDatabaseSchemaAndDataUpdate(ctx context.Con
migrationDetail := c.DetailList[0]
baseDatabaseName := migrationDetail.DatabaseName
- deployments, matrix, err := getDatabaseMatrixFromDeploymentSchedule(deploySchedule, baseDatabaseName, project.DBNameTemplate, dbList)
+ matrix, err := getDatabaseMatrixFromDeploymentSchedule(deploySchedule, baseDatabaseName, project.DBNameTemplate, dbList)
if err != nil {
return nil, echo.NewHTTPError(http.StatusInternalServerError, "Failed to create deployment pipeline").SetInternal(err)
}
// Convert to pipelineCreate
for i, databaseList := range matrix {
+ // Skip the stage if the stage includes no database.
+ if len(databaseList) == 0 {
+ continue
+ }
// Since environment is required for stage, we use an internal bb system environment for tenant deployments.
environmentSet := make(map[string]bool)
var environmentID int
@@ -837,7 +841,7 @@ func (s *Server) getPipelineCreateForDatabaseSchemaAndDataUpdate(ctx context.Con
}
create.StageList = append(create.StageList, api.StageCreate{
- Name: deployments[i].Name,
+ Name: deploySchedule.Deployments[i].Name,
EnvironmentID: environmentID,
TaskList: taskCreateList,
})
@@ -1455,7 +1459,7 @@ func (s *Server) getSchemaFromPeerTenantDatabase(ctx context.Context, instance *
if err != nil {
return "", "", echo.NewHTTPError(http.StatusInternalServerError, "Failed to get deployment schedule").SetInternal(err)
}
- _, matrix, err := getDatabaseMatrixFromDeploymentSchedule(deploySchedule, baseDatabaseName, project.DBNameTemplate, dbList)
+ matrix, err := getDatabaseMatrixFromDeploymentSchedule(deploySchedule, baseDatabaseName, project.DBNameTemplate, dbList)
if err != nil {
return "", "", echo.NewHTTPError(http.StatusInternalServerError, "Failed to create deployment pipeline").SetInternal(err)
}