Skip to content

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

feat(tenancy): generate pipeline for tenant schema update (#269)

spinningbot21753ab5f961
diff --git a/server/issue.go b/server/issue.go
index 60e3d1d716..655a8cb36d 100644
--- a/server/issue.go
+++ b/server/issue.go
@@ -695,6 +695,45 @@ func (s *Server) getPipelineFromIssue(ctx context.Context, issueCreate *api.Issu
if err := json.Unmarshal([]byte(issueCreate.CreateContext), &m); err != nil {
return nil, err
}
+ project, err := s.composeProjectByID(ctx, issueCreate.ProjectID)
+ if err != nil {
+ return nil, echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch project ID: %v", issueCreate.ProjectID)).SetInternal(err)
+ }
+ // Tenant mode project pipeline has its own generation.
+ if project.TenantMode == api.TenantModeTenant {
+ if m.MigrationType != db.Migrate {
+ return nil, echo.NewHTTPError(http.StatusBadRequest, "Only Migrate type migration can be performed on tenant mode project")
+ }
+ if len(m.UpdateSchemaDetailList) != 1 {
+ return nil, echo.NewHTTPError(http.StatusBadRequest, "Tenant mode project should have exactly one update schema detail")
+ }
+ d := m.UpdateSchemaDetailList[0]
+ if d.Statement == "" {
+ return nil, echo.NewHTTPError(http.StatusBadRequest, "Failed to create issue, sql statement missing")
+ }
+ deployConfig, err := s.DeploymentConfigService.FindDeploymentConfig(ctx, &api.DeploymentConfigFind{
+ ProjectID: &issueCreate.ProjectID,
+ })
+ if err != nil {
+ return nil, echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch deplopment config for project ID: %v", issueCreate.ProjectID)).SetInternal(err)
+ }
+ if deployConfig == nil {
+ return nil, echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Deployment config missing for project ID: %v", issueCreate.ProjectID)).SetInternal(err)
+ }
+ deploySchedule, err := api.ValidateAndGetDeploymentSchedule(deployConfig.Payload)
+ if err != nil {
+ return nil, echo.NewHTTPError(http.StatusInternalServerError, "Failed to get deplopment schedule").SetInternal(err)
+ }
+ // Find all databases in the project.
+ databaseList, err := s.DatabaseService.FindDatabaseList(ctx, &api.DatabaseFind{
+ ProjectID: &issueCreate.ProjectID,
+ })
+ if err != nil {
+ return nil, echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch databases in project ID: %v", issueCreate.ProjectID)).SetInternal(err)
+ }
+ return generatePipelineCreateFromDeploymentSchedule(deploySchedule, databaseList), nil
+ }
+
pc := &api.PipelineCreate{
CreatorID: creatorID,
}
diff --git a/server/project.go b/server/project.go
index 1560e00f08..a33c607edc 100644
--- a/server/project.go
+++ b/server/project.go
@@ -531,6 +531,11 @@ func (s *Server) registerProjectRoutes(g *echo.Group) {
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to get deployment configuration for project id: %d", id)).SetInternal(err)
}
+ // We should return empty deployment config when it doesn't exist.
+ if deploymentConfig == nil {
+ deploymentConfig = &api.DeploymentConfig{}
+ }
+
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
if err := jsonapi.MarshalPayload(c.Response().Writer, deploymentConfig); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to marshal get deployment configuration response: %v", id)).SetInternal(err)
diff --git a/store/deploy.go b/store/deploy.go
index 5412f5ec05..19374c491b 100644
--- a/store/deploy.go
+++ b/store/deploy.go
@@ -86,7 +86,7 @@ func (s *DeploymentConfigService) FindDeploymentConfig(ctx context.Context, find
switch len(ret) {
case 0:
- return &api.DeploymentConfig{}, nil
+ return nil, nil
case 1:
return ret[0], nil
default: