Skip to content

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

fix: empty sha256 for release file statement (#16467)

p0nye7081211d576
diff --git a/backend/api/v1/release_service.go b/backend/api/v1/release_service.go
index 893b973b9b..4e10c7be23 100644
--- a/backend/api/v1/release_service.go
+++ b/backend/api/v1/release_service.go
@@ -2,6 +2,8 @@ package v1
import (
"context"
+ "crypto/sha256"
+ "encoding/hex"
"fmt"
"slices"
@@ -66,7 +68,7 @@ func (s *ReleaseService) CreateRelease(ctx context.Context, request *v1pb.Create
return nil, status.Errorf(codes.NotFound, "project %v not found", projectID)
}
- request.Release.Files, err = validateAndSanitizeReleaseFiles(request.Release.Files)
+ request.Release.Files, err = validateAndSanitizeReleaseFiles(ctx, s.store, request.Release.Files)
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "invalid release files, err: %v", err)
}
@@ -76,9 +78,7 @@ func (s *ReleaseService) CreateRelease(ctx context.Context, request *v1pb.Create
// Check versions.
for _, file := range request.Release.Files {
if file.Sheet == "" {
- if file.Statement == nil {
- return nil, status.Errorf(codes.InvalidArgument, "either sheet or statement must be set")
- }
+ // statement must be present due to validation in validateAndSanitizeReleaseFiles
sheet := &store.SheetMessage{
Title: fmt.Sprintf("File %s", file.Path),
Statement: string(file.Statement),
@@ -413,12 +413,51 @@ func convertReleaseVcsSource(vs *v1pb.Release_VCSSource) *storepb.ReleasePayload
}
}
-func validateAndSanitizeReleaseFiles(files []*v1pb.Release_File) ([]*v1pb.Release_File, error) {
+// 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.
+// If a sheet is provided, it populates the statement from the sheet.
+// If a statement is provided, it computes the sheetSha256 from the statement.
+// It returns an error if any validation fails.
+// The function also generates a unique ID for each file.
+// The files are sorted by version in ascending order.
+func validateAndSanitizeReleaseFiles(ctx context.Context, s *store.Store, files []*v1pb.Release_File) ([]*v1pb.Release_File, error) {
versionSet := map[string]struct{}{}
for _, f := range files {
f.Id = uuid.NewString()
+ switch {
+ // Validate that either sheet or statement is provided
+ case f.Sheet == "" && len(f.Statement) == 0:
+ return nil, errors.Errorf("either sheet or statement must be set for file %q", f.Path)
+ case f.Sheet != "" && len(f.Statement) > 0:
+ return nil, errors.Errorf("cannot set both sheet and statement for file %q", f.Path)
+
+ // If sheet is provided but statement is not, populate statement from sheet
+ case f.Sheet != "":
+ _, sheetUID, err := common.GetProjectResourceIDSheetUID(f.Sheet)
+ if err != nil {
+ return nil, errors.Wrapf(err, "failed to get sheet UID from %q", f.Sheet)
+ }
+ sheet, err := s.GetSheet(ctx, &store.FindSheetMessage{
+ UID: &sheetUID,
+ LoadFull: true,
+ })
+ if err != nil {
+ return nil, errors.Wrapf(err, "failed to get sheet %q", f.Sheet)
+ }
+ if sheet == nil {
+ return nil, errors.Errorf("sheet %q not found", f.Sheet)
+ }
+ f.Statement = []byte(sheet.Statement)
+ f.SheetSha256 = sheet.GetSha256Hex()
+ case len(f.Statement) > 0:
+ // populate sheetSha256 from statement
+ h := sha256.Sum256(f.Statement)
+ f.SheetSha256 = hex.EncodeToString(h[:])
+ }
+
switch f.Type {
case v1pb.ReleaseFileType_VERSIONED:
default:
diff --git a/backend/api/v1/release_service_check.go b/backend/api/v1/release_service_check.go
index d6a55e63eb..42b7e60f5d 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, request *v1pb.CheckRe
}
// Validate and sanitize release files.
- request.Release.Files, err = validateAndSanitizeReleaseFiles(request.Release.Files)
+ request.Release.Files, err = validateAndSanitizeReleaseFiles(ctx, s.store, request.Release.Files)
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "invalid release files, err: %v", err)
}
@@ -188,6 +188,7 @@ func (s *ReleaseService) CheckRelease(ctx context.Context, request *v1pb.CheckRe
File: file.Path,
Target: fmt.Sprintf("instances/%s/databases/%s", instance.ResourceID, database.DatabaseName),
}
+ // statement is guaranteed to be populated by validateAndSanitizeReleaseFiles
statement := string(file.Statement)
// Check if any syntax error in the statement.
if common.EngineSupportSyntaxCheck(engine) {
diff --git a/frontend/src/types/proto/v1/release_service.ts b/frontend/src/types/proto/v1/release_service.ts
index 9e176917c1..3684261eb9 100644
--- a/frontend/src/types/proto/v1/release_service.ts
+++ b/frontend/src/types/proto/v1/release_service.ts
@@ -258,18 +258,22 @@ export interface Release_File {
id: string;
/** The path of the file. e.g. `2.2/V0001_create_table.sql`. */
path: string;
+ type: ReleaseFileType;
+ version: string;
+ changeType: Release_File_ChangeType;
/**
+ * For inputs, we must either use `sheet` or `statement`.
+ * For outputs, we always use `sheet`. `statement` is the preview of the sheet content.
+ *
* The sheet that holds the content.
* Format: projects/{project}/sheets/{sheet}
*/
sheet: string;
- /** The SHA256 hash value of the sheet. */
- sheetSha256: string;
- type: ReleaseFileType;
- version: string;
- changeType: Release_File_ChangeType;
- /** The statement is used for preview or check purpose. */
+ /** The raw SQL statement content. */
statement: Uint8Array;
+ /** The SHA256 hash value of the sheet content or the statement. */
+ sheetSha256: string;
+ /** The size of the statement in bytes. */
statementSize: Long;
}
@@ -1262,12 +1266,12 @@ function createBaseRelease_File(): Release_File {
return {
id: "",
path: "",
- sheet: "",
- sheetSha256: "",
type: ReleaseFileType.TYPE_UNSPECIFIED,
version: "",
changeType: Release_File_ChangeType.CHANGE_TYPE_UNSPECIFIED,
+ sheet: "",
statement: new Uint8Array(0),
+ sheetSha256: "",
statementSize: Long.ZERO,
};
}
@@ -1280,12 +1284,6 @@ export const Release_File: MessageFns<Release_File> = {
if (message.path !== "") {
writer.uint32(18).string(message.path);
}
- if (message.sheet !== "") {
- writer.uint32(26).string(message.sheet);
- }
- if (message.sheetSha256 !== "") {
- writer.uint32(34).string(message.sheetSha256);
- }
if (message.type !== ReleaseFileType.TYPE_UNSPECIFIED) {
writer.uint32(40).int32(releaseFileTypeToNumber(message.type));
}
@@ -1295,9 +1293,15 @@ export const Release_File: MessageFns<Release_File> = {
if (message.changeType !== Release_File_ChangeType.CHANGE_TYPE_UNSPECIFIED) {
writer.uint32(72).int32(release_File_ChangeTypeToNumber(message.changeType));
}
+ if (message.sheet !== "") {
+ writer.uint32(26).string(message.sheet);
+ }
if (message.statement.length !== 0) {
writer.uint32(58).bytes(message.statement);
}
+ if (message.sheetSha256 !== "") {
+ writer.uint32(34).string(message.sheetSha256);
+ }
if (!message.statementSize.equals(Long.ZERO)) {
writer.uint32(64).int64(message.statementSize.toString());
}
@@ -1327,22 +1331,6 @@ export const Release_File: MessageFns<Release_File> = {
message.path = reader.string();
continue;
}
- case 3: {
- if (tag !== 26) {
- break;
- }
-
- message.sheet = reader.string();
- continue;
- }
- case 4: {
- if (tag !== 34) {
- break;
- }
-
- message.sheetSha256 = reader.string();
- continue;
- }
case 5: {
if (tag !== 40) {
break;
@@ -1367,6 +1355,14 @@ export const Release_File: MessageFns<Release_File> = {
message.changeType = release_File_ChangeTypeFromJSON(reader.int32());
continue;
}
+ case 3: {
+ if (tag !== 26) {
+ break;
+ }
+
+ message.sheet = reader.string();
+ continue;
+ }
case 7: {
if (tag !== 58) {
break;
@@ -1375,6 +1371,14 @@ export const Release_File: MessageFns<Release_File> = {
message.statement = reader.bytes();
continue;
}
+ case 4: {
+ if (tag !== 34) {
+ break;
+ }
+
+ message.sheetSha256 = reader.string();
+ continue;
+ }
case 8: {
if (tag !== 64) {
break;
@@ -1400,12 +1404,6 @@ export const Release_File: MessageFns<Release_File> = {
if (message.path !== "") {
obj.path = message.path;
}
- if (message.sheet !== "") {
- obj.sheet = message.sheet;
- }
- if (message.sheetSha256 !== "") {
- obj.sheetSha256 = message.sheetSha256;
- }
if (message.type !== ReleaseFileType.TYPE_UNSPECIFIED) {
obj.type = releaseFileTypeToJSON(message.type);
}
@@ -1415,9 +1413,15 @@ export const Release_File: MessageFns<Release_File> = {
if (message.changeType !== Release_File_ChangeType.CHANGE_TYPE_UNSPECIFIED) {
obj.changeType = release_File_ChangeTypeToJSON(message.changeType);
}
+ if (message.sheet !== "") {
+ obj.sheet = message.sheet;
+ }
if (message.statement.length !== 0) {
obj.statement = base64FromBytes(message.statement);
}
+ if (message.sheetSha256 !== "") {
+ obj.sheetSha256 = message.sheetSha256;
+ }
if (!message.statementSize.equals(Long.ZERO)) {
obj.statementSize = (message.statementSize || Long.ZERO).toString();
}
@@ -1431,12 +1435,12 @@ export const Release_File: MessageFns<Release_File> = {
const message = createBaseRelease_File();
message.id = object.id ?? "";
message.path = object.path ?? "";
- message.sheet = object.sheet ?? "";
- message.sheetSha256 = object.sheetSha256 ?? "";
message.type = object.type ?? ReleaseFileType.TYPE_UNSPECIFIED;
message.version = object.version ?? "";
message.changeType = object.changeType ?? Release_File_ChangeType.CHANGE_TYPE_UNSPECIFIED;
+ message.sheet = object.sheet ?? "";
message.statement = object.statement ?? new Uint8Array(0);
+ message.sheetSha256 = object.sheetSha256 ?? "";
message.statementSize = (object.statementSize !== undefined && object.statementSize !== null)
? Long.fromValue(object.statementSize)
: Long.ZERO;
diff --git a/proto/gen/grpc-doc/openapi.yaml b/proto/gen/grpc-doc/openapi.yaml
index a444840e97..7ea1fef869 100644
--- a/proto/gen/grpc-doc/openapi.yaml
+++ b/proto/gen/grpc-doc/openapi.yaml
@@ -11956,15 +11956,6 @@ components:
path:
type: string
description: The path of the file. e.g. `2.2/V0001_create_table.sql`.
- sheet:
- type: string
- description: |-
- The sheet that holds the content.
- Format: projects/{project}/sheets/{sheet}
- sheetSha256:
- readOnly: true
- type: string
- description: The SHA256 hash value of the sheet.
type:
enum:
- TYPE_UNSPECIFIED
@@ -11981,13 +11972,26 @@ components:
- DML
type: string
format: enum
+ sheet:
+ type: string
+ description: |-
+ For inputs, we must either use `sheet` or `statement`.
+ For outputs, we always use `sheet`. `statement` is the preview of the sheet content.
+
+ The sheet that holds the content.
+ Format: projects/{project}/sheets/{sheet}
statement:
type: string
- description: The statement is used for preview or check purpose.
+ description: The raw SQL statement content.
format: bytes
+ sheetSha256:
+ readOnly: true
+ type: string
+ description: The SHA256 hash value of the sheet content or the statement.
statementSize:
readOnly: true
type: string
+ description: The size of the statement in bytes.
Release_VCSSource:
type: object
properties:
diff --git a/proto/gen/grpc-doc/v1/README.md b/proto/gen/grpc-doc/v1/README.md
index 9361944701..43682fac44 100644
--- a/proto/gen/grpc-doc/v1/README.md
+++ b/proto/gen/grpc-doc/v1/README.md
@@ -9197,13 +9197,15 @@ When paginating, all other parameters provided to `ListReleases` must match the
| ----- | ---- | ----- | ----------- |
| id | [string](#string) | | The unique identifier for the file. |
| path | [string](#string) | | The path of the file. e.g. `2.2/V0001_create_table.sql`. |
-| sheet | [string](#string) | | The sheet that holds the content. Format: projects/{project}/sheets/{sheet} |
-| sheet_sha256 | [string](#string) | | The SHA256 hash value of the sheet. |
| type | [ReleaseFileType](#bytebase-v1-ReleaseFileType) | | |
| version | [string](#string) | | |
| change_type | [Release.File.ChangeType](#bytebase-v1-Release-File-ChangeType) | | |
-| statement | [bytes](#bytes) | | The statement is used for preview or check purpose. |
-| statement_size | [int64](#int64) | | |
+| sheet | [string](#string) | | For inputs, we must either use `sheet` or `statement`. For outputs, we always use `sheet`. `statement` is the preview of the sheet content.
+
+The sheet that holds the content. Format: projects/{project}/sheets/{sheet} |
+| statement | [bytes](#bytes) | | The raw SQL statement content. |
+| sheet_sha256 | [string](#string) | | The SHA256 hash value of the sheet content or the statement. |
+| statement_size | [int64](#int64) | | The size of the statement in bytes. |
diff --git a/proto/gen/grpc-doc/v1/index.html b/proto/gen/grpc-doc/v1/index.html
index 11ec7a0b7e..da229cad73 100644
--- a/proto/gen/grpc-doc/v1/index.html
+++ b/proto/gen/grpc-doc/v1/index.html
@@ -25752,21 +25752,6 @@ If this field is omitted, there are no subsequent pages. </p></td>
<td><p>The path of the file. e.g. `2.2/V0001_create_table.sql`. </p></td>
</tr>
- <tr>
- <td>sheet</td>
- <td><a href="#string">string</a></td>
- <td></td>
- <td><p>The sheet that holds the content.
-Format: projects/{project}/sheets/{sheet} </p></td>
- </tr>
-
- <tr>
- <td>sheet_sha256</td>
- <td><a href="#string">string</a></td>
- <td></td>
- <td><p>The SHA256 hash value of the sheet. </p></td>
- </tr>
-
<tr>
<td>type</td>
<td><a href="#bytebase.v1.ReleaseFileType">ReleaseFileType</a></td>
@@ -25788,18 +25773,36 @@ Format: projects/{project}/sheets/{sheet} </p></td>
<td><p> </p></td>
</tr>
+ <tr>
+ <td>sheet</td>
+ <td><a href="#string">string</a></td>
+ <td></td>
+ <td><p>For inputs, we must either use `sheet` or `statement`.
+For outputs, we always use `sheet`. `statement` is the preview of the sheet content.
+
+The sheet that holds the content.
+Format: projects/{project}/sheets/{sheet} </p></td>
+ </tr>
+
<tr>
<td>statement</td>
<td><a href="#bytes">bytes</a></td>
<td></td>
- <td><p>The statement is used for preview or check purpose. </p></td>
+ <td><p>The raw SQL statement content. </p></td>
+ </tr>
+
+ <tr>
+ <td>sheet_sha256</td>
+ <td><a href="#string">string</a></td>
+ <td></td>
+ <td><p>The SHA256 hash value of the sheet content or the statement. </p></td>
</tr>
<tr>
<td>statement_size</td>
<td><a href="#int64">int64</a></td>
<td></td>
- <td><p> </p></td>
+ <td><p>The size of the statement in bytes. </p></td>
</tr>
</tbody>
diff --git a/proto/generated-go/v1/release_service.pb.go b/proto/generated-go/v1/release_service.pb.go
index 43e24350aa..b2bd5cb185 100644
--- a/proto/generated-go/v1/release_service.pb.go
+++ b/proto/generated-go/v1/release_service.pb.go
@@ -863,18 +863,22 @@ type Release_File struct {
// The unique identifier for the file.
Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"`
// The path of the file. e.g. `2.2/V0001_create_table.sql`.
- Path string `protobuf:"bytes,2,opt,name=path,proto3" json:"path,omitempty"`
+ Path string `protobuf:"bytes,2,opt,name=path,proto3" json:"path,omitempty"`
+ Type ReleaseFileType `protobuf:"varint,5,opt,name=type,proto3,enum=bytebase.v1.ReleaseFileType" json:"type,omitempty"`
+ Version string `protobuf:"bytes,6,opt,name=version,proto3" json:"version,omitempty"`
+ ChangeType Release_File_ChangeType `protobuf:"varint,9,opt,name=change_type,json=changeType,proto3,enum=bytebase.v1.Release_File_ChangeType" json:"change_type,omitempty"`
+ // For inputs, we must either use `sheet` or `statement`.
+ // For outputs, we always use `sheet`. `statement` is the preview of the sheet content.
+ //
// The sheet that holds the content.
// Format: projects/{project}/sheets/{sheet}
Sheet string `protobuf:"bytes,3,opt,name=sheet,proto3" json:"sheet,omitempty"`
- // The SHA256 hash value of the sheet.
- SheetSha256 string `protobuf:"bytes,4,opt,name=sheet_sha256,json=sheetSha256,proto3" json:"sheet_sha256,omitempty"`
- Type ReleaseFileType `protobuf:"varint,5,opt,name=type,proto3,enum=bytebase.v1.ReleaseFileType" json:"type,omitempty"`
- Version string `protobuf:"bytes,6,opt,name=version,proto3" json:"version,omitempty"`
- ChangeType Release_File_ChangeType `protobuf:"varint,9,opt,name=change_type,json=changeType,proto3,enum=bytebase.v1.Release_File_ChangeType" json:"change_type,omitempty"`
- // The statement is used for preview or check purpose.
- Statement []byte `protobuf:"bytes,7,opt,name=statement,proto3" json:"statement,omitempty"`
- StatementSize int64 `protobuf:"varint,8,opt,name=statement_size,json=statementSize,proto3" json:"statement_size,omitempty"`
+ // The raw SQL statement content.
+ Statement []byte `protobuf:"bytes,7,opt,name=statement,proto3" json:"statement,omitempty"`
+ // The SHA256 hash value of the sheet content or the statement.
+ SheetSha256 string `protobuf:"bytes,4,opt,name=sheet_sha256,json=sheetSha256,proto3" json:"sheet_sha256,omitempty"`
+ // The size of the statement in bytes.
+ StatementSize int64 `protobuf:"varint,8,opt,name=statement_size,json=statementSize,proto3" json:"statement_size,omitempty"`
unknownFields protoimpl.UnknownFields
sizeCache protoimpl.SizeCache
}
@@ -923,20 +927,6 @@ func (x *Release_File) GetPath() string {
return ""
}
-func (x *Release_File) GetSheet() string {
- if x != nil {
- return x.Sheet
- }
- return ""
-}
-
-func (x *Release_File) GetSheetSha256() string {
- if x != nil {
- return x.SheetSha256
- }
- return ""
-}
-
func (x *Release_File) GetType() ReleaseFileType {
if x != nil {
return x.Type
@@ -958,6 +948,13 @@ func (x *Release_File) GetChangeType() Release_File_ChangeType {
return Release_File_CHANGE_TYPE_UNSPECIFIED
}
+func (x *Release_File) GetSheet() string {
+ if x != nil {
+ return x.Sheet
+ }
+ return ""
+}
+
func (x *Release_File) GetStatement() []byte {
if x != nil {
return x.Statement
@@ -965,6 +962,13 @@ func (x *Release_File) GetStatement() []byte {
return nil
}
+func (x *Release_File) GetSheetSha256() string {
+ if x != nil {
+ return x.SheetSha256
+ }
+ return ""
+}
+
func (x *Release_File) GetStatementSize() int64 {
if x != nil {
return x.StatementSize
@@ -1091,15 +1095,15 @@ const file_v1_release_service_proto_rawDesc = "" +
"\x05state\x18\a \x01(\x0e2\x12.bytebase.v1.StateB\x04\xe2A\x01\x03R\x05state\x1a\xac\x03\n" +
"\x04File\x12\x0e\n" +
"\x02id\x18\x01 \x01(\tR\x02id\x12\x12\n" +
- "\x04path\x18\x02 \x01(\tR\x04path\x12-\n" +
- "\x05sheet\x18\x03 \x01(\tB\x17\xfaA\x14\n" +
- "\x12bytebase.com/SheetR\x05sheet\x12'\n" +
- "\fsheet_sha256\x18\x04 \x01(\tB\x04\xe2A\x01\x03R\vsheetSha256\x120\n" +
+ "\x04path\x18\x02 \x01(\tR\x04path\x120\n" +
"\x04type\x18\x05 \x01(\x0e2\x1c.bytebase.v1.ReleaseFileTypeR\x04type\x12\x18\n" +
"\aversion\x18\x06 \x01(\tR\aversion\x12E\n" +
"\vchange_type\x18\t \x01(\x0e2$.bytebase.v1.Release.File.ChangeTypeR\n" +
- "changeType\x12\x1c\n" +
- "\tstatement\x18\a \x01(\fR\tstatement\x12+\n" +
+ "changeType\x12-\n" +
+ "\x05sheet\x18\x03 \x01(\tB\x17\xfaA\x14\n" +
+ "\x12bytebase.com/SheetR\x05sheet\x12\x1c\n" +
+ "\tstatement\x18\a \x01(\fR\tstatement\x12'\n" +
+ "\fsheet_sha256\x18\x04 \x01(\tB\x04\xe2A\x01\x03R\vsheetSha256\x12+\n" +
"\x0estatement_size\x18\b \x01(\x03B\x04\xe2A\x01\x03R\rstatementSize\"J\n" +
"\n" +
"ChangeType\x12\x1b\n" +
diff --git a/proto/v1/v1/release_service.proto b/proto/v1/v1/release_service.proto
index 0c7170e7c1..f71ec9e495 100644
--- a/proto/v1/v1/release_service.proto
+++ b/proto/v1/v1/release_service.proto
@@ -223,17 +223,21 @@ message Release {
string id = 1;
// The path of the file. e.g. `2.2/V0001_create_table.sql`.
string path = 2;
- // The sheet that holds the content.
- // Format: projects/{project}/sheets/{sheet}
- string sheet = 3 [(google.api.resource_reference) = {type: "bytebase.com/Sheet"}];
- // The SHA256 hash value of the sheet.
- string sheet_sha256 = 4 [(google.api.field_behavior) = OUTPUT_ONLY];
ReleaseFileType type = 5;
string version = 6;
ChangeType change_type = 9;
- // The statement is used for preview or check purpose.
+ // For inputs, we must either use `sheet` or `statement`.
+ // For outputs, we always use `sheet`. `statement` is the preview of the sheet content.
+ //
+ // The sheet that holds the content.
+ // Format: projects/{project}/sheets/{sheet}
+ string sheet = 3 [(google.api.resource_reference) = {type: "bytebase.com/Sheet"}];
+ // The raw SQL statement content.
bytes statement = 7;
+ // The SHA256 hash value of the sheet content or the statement.
+ string sheet_sha256 = 4 [(google.api.field_behavior) = OUTPUT_ONLY];
+ // The size of the statement in bytes.
int64 statement_size = 8 [(google.api.field_behavior) = OUTPUT_ONLY];
enum ChangeType {