Skip to content

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

feat(action): merge files for declarative rollout (#17203)

p0ny806dee31816c
diff --git a/action/command/file.go b/action/command/file.go
index 8447fda70e..e742c2008c 100644
--- a/action/command/file.go
+++ b/action/command/file.go
@@ -15,6 +15,8 @@ import (
v1pb "github.com/bytebase/bytebase/backend/generated-go/v1"
)
+const versionFormat = "20060102.150405"
+
// getReleaseFiles returns the release files and the digest of the release.
func getReleaseFiles(w *world.World) ([]*v1pb.Release_File, string, error) {
matches, err := filepath.Glob(w.FilePattern)
@@ -30,8 +32,35 @@ func getReleaseFiles(w *world.World) ([]*v1pb.Release_File, string, error) {
h := sha256.New()
if w.Declarative {
- // For declarative files, we need to concat all the file contents.
- var contents []byte
+ // For declarative files, we need to concat all the file contents if it is rollout.
+ if w.IsRollout {
+ var contents []byte
+ for _, m := range matches {
+ content, err := os.ReadFile(m)
+ if err != nil {
+ return nil, "", err
+ }
+ if _, err := h.Write([]byte(m)); err != nil {
+ return nil, "", errors.Wrapf(err, "failed to write file path")
+ }
+ if _, err := h.Write(content); err != nil {
+ return nil, "", errors.Wrapf(err, "failed to write file content")
+ }
+ contents = append(contents, content...)
+ }
+ return []*v1pb.Release_File{
+ {
+ // use file pattern as the path
+ Path: w.FilePattern,
+ Type: v1pb.Release_File_DECLARATIVE,
+ Version: w.CurrentTime.Format(versionFormat),
+ ChangeType: v1pb.Release_File_DDL,
+ Statement: contents,
+ },
+ }, hex.EncodeToString(h.Sum(nil)), nil
+ }
+
+ var files []*v1pb.Release_File
for _, m := range matches {
content, err := os.ReadFile(m)
if err != nil {
@@ -43,18 +72,15 @@ func getReleaseFiles(w *world.World) ([]*v1pb.Release_File, string, error) {
if _, err := h.Write(content); err != nil {
return nil, "", errors.Wrapf(err, "failed to write file content")
}
- contents = append(contents, content...)
- }
- return []*v1pb.Release_File{
- {
- // use file pattern as the path
- Path: w.FilePattern,
+ files = append(files, &v1pb.Release_File{
+ Path: m,
Type: v1pb.Release_File_DECLARATIVE,
- Version: w.CurrentTime.Format("20060102.150405"),
+ Version: w.CurrentTime.Format(versionFormat),
ChangeType: v1pb.Release_File_DDL,
- Statement: contents,
- },
- }, hex.EncodeToString(h.Sum(nil)), nil
+ Statement: content,
+ })
+ }
+ return files, hex.EncodeToString(h.Sum(nil)), nil
}
var files []*v1pb.Release_File
diff --git a/action/command/rollout.go b/action/command/rollout.go
index 0ff75d6a86..c7ec4afa12 100644
--- a/action/command/rollout.go
+++ b/action/command/rollout.go
@@ -58,6 +58,7 @@ func runRollout(w *world.World) func(command *cobra.Command, _ []string) error {
defer func() {
writeOutputJSON(w)
}()
+ w.IsRollout = true
ctx := command.Context()
client, err := NewClient(w.URL, w.ServiceAccount, w.ServiceAccountSecret)
if err != nil {
diff --git a/action/world/world.go b/action/world/world.go
index f6f38c1f13..7c25070965 100644
--- a/action/world/world.go
+++ b/action/world/world.go
@@ -11,6 +11,9 @@ type World struct {
Platform JobPlatform
CurrentTime time.Time
+ // Whether it is the rollout subcommand.
+ IsRollout bool
+
// bytebase-action flags
Output string
URL string
diff --git a/backend/api/v1/release_service.go b/backend/api/v1/release_service.go
index d9ff32ebb3..0b6b909fe3 100644
--- a/backend/api/v1/release_service.go
+++ b/backend/api/v1/release_service.go
@@ -68,9 +68,7 @@ func (s *ReleaseService) CreateRelease(ctx context.Context, req *connect.Request
return nil, connect.NewError(connect.CodeNotFound, errors.Errorf("project %v not found", projectID))
}
- // Use the sanitized files from now on.
- // For declarative files, the files are merged into one file.
- sanitizedFiles, err := validateAndSanitizeReleaseFiles(ctx, s.store, req.Msg.Release.Files)
+ sanitizedFiles, err := validateAndSanitizeReleaseFiles(ctx, s.store, req.Msg.Release.Files, true)
if err != nil {
return nil, connect.NewError(connect.CodeInvalidArgument, errors.Wrapf(err, "invalid release files"))
}
@@ -481,15 +479,12 @@ func convertReleaseVcsSource(vs *v1pb.Release_VCSSource) *storepb.ReleasePayload
// validateAndSanitizeReleaseFiles validates and sanitizes the release files inputs.
// It ensures that each file has either a sheet or a statement, and that the sheet is valid.
// It also checks for duplicate versions and sorts the files by version.
+// For creating release with declarative files, it checks if there is only one file.
//
// The input files are modified in place.
// If a sheet is provided, it populates the statement from the sheet.
// If a statement is provided, it computes the sheetSha256 from the statement.
-//
-// The returned files are sorted by version in ascending order.
-// For declarative files, all files contents are merged into one file and returned.
-// It is safe to use the input files for CheckRelease with declarative files.
-func validateAndSanitizeReleaseFiles(ctx context.Context, s *store.Store, files []*v1pb.Release_File) ([]*v1pb.Release_File, error) {
+func validateAndSanitizeReleaseFiles(ctx context.Context, s *store.Store, files []*v1pb.Release_File, createRelease bool) ([]*v1pb.Release_File, error) {
if len(files) == 0 {
return nil, errors.Errorf("release files cannot be empty")
}
@@ -553,6 +548,9 @@ func validateAndSanitizeReleaseFiles(ctx context.Context, s *store.Store, files
if fileTypeCount[v1pb.Release_File_VERSIONED] > 0 && fileTypeCount[v1pb.Release_File_DECLARATIVE] > 0 {
return nil, errors.Errorf("cannot have both versioned and declarative files")
}
+ if createRelease && fileTypeCount[v1pb.Release_File_DECLARATIVE] > 1 {
+ return nil, errors.Errorf("cannot have multiple declarative files when creating release")
+ }
// Create files with additional parsed version data for sorting.
type fileWithVersion struct {
diff --git a/backend/api/v1/release_service_check.go b/backend/api/v1/release_service_check.go
index 6ab5312081..fe6bbdbd6d 100644
--- a/backend/api/v1/release_service_check.go
+++ b/backend/api/v1/release_service_check.go
@@ -98,7 +98,7 @@ func (s *ReleaseService) CheckRelease(ctx context.Context, req *connect.Request[
}
// Validate and sanitize release files.
- sanitizedFiles, err := validateAndSanitizeReleaseFiles(ctx, s.store, request.Release.Files)
+ sanitizedFiles, err := validateAndSanitizeReleaseFiles(ctx, s.store, request.Release.Files, false)
if err != nil {
return nil, connect.NewError(connect.CodeInvalidArgument, errors.Wrapf(err, "invalid release files"))
}
@@ -111,10 +111,7 @@ func (s *ReleaseService) CheckRelease(ctx context.Context, req *connect.Request[
var response *v1pb.CheckReleaseResponse
switch releaseFileType {
case v1pb.Release_File_DECLARATIVE:
- // To check declarative files, we use the original files.
- // Because the sanitized files are merged into one file for the declarative case and
- // we lose file path information.
- resp, err := s.checkReleaseDeclarative(ctx, request.Release.Files, targetDatabases)
+ resp, err := s.checkReleaseDeclarative(ctx, sanitizedFiles, targetDatabases)
if err != nil {
return nil, connect.NewError(connect.CodeInternal, errors.Wrapf(err, "failed to check release declarative"))
}
@@ -190,7 +187,7 @@ loop:
// Add a warning advice if SHA256 mismatch
checkResult := &v1pb.CheckReleaseResponse_CheckResult{
File: file.Path,
- Target: fmt.Sprintf("instances/%s/databases/%s", instance.ResourceID, database.DatabaseName),
+ Target: common.FormatDatabase(instance.ResourceID, database.DatabaseName),
Advices: []*v1pb.Advice{
{
Status: v1pb.Advice_WARNING,
@@ -210,7 +207,7 @@ loop:
checkResult, err := func() (*v1pb.CheckReleaseResponse_CheckResult, error) {
checkResult := &v1pb.CheckReleaseResponse_CheckResult{
File: file.Path,
- Target: fmt.Sprintf("instances/%s/databases/%s", instance.ResourceID, database.DatabaseName),
+ Target: common.FormatDatabase(instance.ResourceID, database.DatabaseName),
}
// statement is guaranteed to be populated by validateAndSanitizeReleaseFiles
statement := string(file.Statement)