Skip to content

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

fix: save change history id as baseline schema instead of sheet id (#7154)

boojack378b0d92054e
diff --git a/backend/api/v1/database_service.go b/backend/api/v1/database_service.go
index 88c82059cf..c0a83a1ce3 100644
--- a/backend/api/v1/database_service.go
+++ b/backend/api/v1/database_service.go
@@ -684,10 +684,6 @@ func (s *DatabaseService) GetChangeHistory(ctx context.Context, request *v1pb.Ge
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, err.Error())
}
- changeHistoryID, err := strconv.ParseInt(changeHistoryIDStr, 10, 64)
- if err != nil {
- return nil, status.Errorf(codes.InvalidArgument, "cannot parse change history id %q", changeHistoryIDStr)
- }
instance, err := s.store.GetInstanceV2(ctx, &store.FindInstanceMessage{
ResourceID: &instanceID,
})
@@ -710,7 +706,7 @@ func (s *DatabaseService) GetChangeHistory(ctx context.Context, request *v1pb.Ge
find := &store.FindInstanceChangeHistoryMessage{
InstanceID: &instance.UID,
DatabaseID: &database.UID,
- ID: &changeHistoryID,
+ ID: &changeHistoryIDStr,
}
if request.View == v1pb.ChangeHistoryView_CHANGE_HISTORY_VIEW_FULL {
find.ShowFull = true
diff --git a/backend/api/v1/schema_design_service.go b/backend/api/v1/schema_design_service.go
index de79122db7..ff8786eadb 100644
--- a/backend/api/v1/schema_design_service.go
+++ b/backend/api/v1/schema_design_service.go
@@ -160,29 +160,23 @@ func (s *SchemaDesignService) CreateSchemaDesign(ctx context.Context, request *v
if err != nil {
return nil, status.Errorf(codes.Internal, err.Error())
}
- schemaVersionID, err := strconv.ParseInt(changeHistoryIDStr, 10, 64)
- if err != nil || schemaVersionID <= 0 {
- return nil, status.Errorf(codes.InvalidArgument, fmt.Sprintf("invalid schema version %s, must be positive integer", schemaDesign.SchemaVersion))
- }
changeHistory, err := s.store.GetInstanceChangeHistory(ctx, &store.FindInstanceChangeHistoryMessage{
- ID: &schemaVersionID,
+ ID: &changeHistoryIDStr,
InstanceID: &instance.UID,
})
if err != nil {
return nil, status.Errorf(codes.Internal, err.Error())
}
if changeHistory == nil {
- return nil, status.Errorf(codes.NotFound, "schema version %d not found", schemaVersionID)
+ return nil, status.Errorf(codes.NotFound, "schema version %s not found", changeHistoryIDStr)
}
schemaDesignSheetPayload := &storepb.SheetPayload{
Type: storepb.SheetPayload_SCHEMA_DESIGN,
SchemaDesign: &storepb.SheetPayload_SchemaDesign{
- Engine: storepb.Engine(schemaDesign.Engine),
+ BaselineChangeHistoryId: changeHistory.UID,
+ Engine: storepb.Engine(schemaDesign.Engine),
},
}
- if changeHistory.SheetID != nil {
- schemaDesignSheetPayload.SchemaDesign.BaselineSheetId = int64(*changeHistory.SheetID)
- }
payloadBytes, err := protojson.Marshal(schemaDesignSheetPayload)
if err != nil {
return nil, status.Errorf(codes.Internal, fmt.Sprintf("failed to marshal schema design sheet payload: %v", err))
@@ -364,24 +358,17 @@ func (s *SchemaDesignService) convertSheetToSchemaDesign(ctx context.Context, sh
return nil, status.Errorf(codes.Internal, fmt.Sprintf("failed to transform schema string to database metadata: %v", err))
}
- baselineSheetID := int(sheetPayload.SchemaDesign.BaselineSheetId)
- baselineSheet, err := s.getSheet(ctx, &store.FindSheetMessage{
- UID: &baselineSheetID,
- })
- if err != nil {
- return nil, status.Errorf(codes.Internal, fmt.Sprintf("failed to find sheet: %v", err))
- }
baselineSchema := ""
schemaVersion := ""
- if baselineSheet != nil {
- baselineSchema = baselineSheet.Statement
+ if sheetPayload.SchemaDesign.BaselineChangeHistoryId != "" {
changeHistory, err := s.store.GetInstanceChangeHistory(ctx, &store.FindInstanceChangeHistoryMessage{
- SheetID: &baselineSheet.UID,
+ ID: &sheetPayload.SchemaDesign.BaselineChangeHistoryId,
})
if err != nil {
return nil, status.Errorf(codes.Internal, fmt.Sprintf("failed to find change history: %v", err))
}
if changeHistory != nil {
+ baselineSchema = changeHistory.Schema
schemaVersion = changeHistory.UID
}
}
diff --git a/backend/store/instance_change_history.go b/backend/store/instance_change_history.go
index 9b0f54be99..1273284ce7 100644
--- a/backend/store/instance_change_history.go
+++ b/backend/store/instance_change_history.go
@@ -55,7 +55,7 @@ const instanceChangeHistoryTruncateLength = 1024 * 1024
// FindInstanceChangeHistoryMessage is for listing a list of instance change history.
type FindInstanceChangeHistoryMessage struct {
- ID *int64
+ ID *string
InstanceID *int
DatabaseID *int
SheetID *int
@@ -253,6 +253,7 @@ func convertInstanceChangeHistoryToMigrationHistory(change *InstanceChangeHistor
// FindInstanceChangeHistoryList finds a list of instance change history and returns as a list of migration history.
func (s *Store) FindInstanceChangeHistoryList(ctx context.Context, find *db.MigrationHistoryFind) ([]*db.MigrationHistory, error) {
findMessage := &FindInstanceChangeHistoryMessage{
+ ID: find.ID,
InstanceID: find.InstanceID,
DatabaseID: find.DatabaseID,
Source: find.Source,
@@ -260,13 +261,6 @@ func (s *Store) FindInstanceChangeHistoryList(ctx context.Context, find *db.Migr
Limit: find.Limit,
ShowFull: true,
}
- if v := find.ID; v != nil {
- id, err := strconv.ParseInt(*v, 10, 64)
- if err != nil {
- return nil, err
- }
- findMessage.ID = &id
- }
list, err := s.ListInstanceChangeHistory(ctx, findMessage)
if err != nil {
@@ -305,7 +299,11 @@ func (s *Store) FindInstanceChangeHistoryList(ctx context.Context, find *db.Migr
func (s *Store) ListInstanceChangeHistory(ctx context.Context, find *FindInstanceChangeHistoryMessage) ([]*InstanceChangeHistoryMessage, error) {
where, args := []string{"TRUE"}, []any{}
if v := find.ID; v != nil {
- where, args = append(where, fmt.Sprintf("instance_change_history.id = $%d", len(args)+1)), append(args, *v)
+ id, err := strconv.Atoi(*v)
+ if err != nil {
+ return nil, errors.Wrapf(err, "failed to convert id %q to int", *v)
+ }
+ where, args = append(where, fmt.Sprintf("instance_change_history.id = $%d", len(args)+1)), append(args, id)
}
sheetField := "instance_change_history.sheet_id"
if v := find.InstanceID; v != nil {
diff --git a/frontend/src/types/proto/store/sheet.ts b/frontend/src/types/proto/store/sheet.ts
index 0dd30a621c..a22893bdde 100644
--- a/frontend/src/types/proto/store/sheet.ts
+++ b/frontend/src/types/proto/store/sheet.ts
@@ -64,7 +64,8 @@ export interface SheetPayload_UsedByIssue {
}
export interface SheetPayload_SchemaDesign {
- baselineSheetId: number;
+ /** The baseline instance change history id of the schema design. */
+ baselineChangeHistoryId: string;
engine: Engine;
}
@@ -372,13 +373,13 @@ export const SheetPayload_UsedByIssue = {
};
function createBaseSheetPayload_SchemaDesign(): SheetPayload_SchemaDesign {
- return { baselineSheetId: 0, engine: 0 };
+ return { baselineChangeHistoryId: "", engine: 0 };
}
export const SheetPayload_SchemaDesign = {
encode(message: SheetPayload_SchemaDesign, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
- if (message.baselineSheetId !== 0) {
- writer.uint32(8).int64(message.baselineSheetId);
+ if (message.baselineChangeHistoryId !== "") {
+ writer.uint32(10).string(message.baselineChangeHistoryId);
}
if (message.engine !== 0) {
writer.uint32(16).int32(message.engine);
@@ -394,11 +395,11 @@ export const SheetPayload_SchemaDesign = {
const tag = reader.uint32();
switch (tag >>> 3) {
case 1:
- if (tag !== 8) {
+ if (tag !== 10) {
break;
}
- message.baselineSheetId = longToNumber(reader.int64() as Long);
+ message.baselineChangeHistoryId = reader.string();
continue;
case 2:
if (tag !== 16) {
@@ -418,14 +419,14 @@ export const SheetPayload_SchemaDesign = {
fromJSON(object: any): SheetPayload_SchemaDesign {
return {
- baselineSheetId: isSet(object.baselineSheetId) ? Number(object.baselineSheetId) : 0,
+ baselineChangeHistoryId: isSet(object.baselineChangeHistoryId) ? String(object.baselineChangeHistoryId) : "",
engine: isSet(object.engine) ? engineFromJSON(object.engine) : 0,
};
},
toJSON(message: SheetPayload_SchemaDesign): unknown {
const obj: any = {};
- message.baselineSheetId !== undefined && (obj.baselineSheetId = Math.round(message.baselineSheetId));
+ message.baselineChangeHistoryId !== undefined && (obj.baselineChangeHistoryId = message.baselineChangeHistoryId);
message.engine !== undefined && (obj.engine = engineToJSON(message.engine));
return obj;
},
@@ -436,7 +437,7 @@ export const SheetPayload_SchemaDesign = {
fromPartial(object: DeepPartial<SheetPayload_SchemaDesign>): SheetPayload_SchemaDesign {
const message = createBaseSheetPayload_SchemaDesign();
- message.baselineSheetId = object.baselineSheetId ?? 0;
+ message.baselineChangeHistoryId = object.baselineChangeHistoryId ?? "";
message.engine = object.engine ?? 0;
return message;
},
diff --git a/proto/gen/grpc-doc/store/README.md b/proto/gen/grpc-doc/store/README.md
index c33db5dbc4..32af129100 100644
--- a/proto/gen/grpc-doc/store/README.md
+++ b/proto/gen/grpc-doc/store/README.md
@@ -1743,7 +1743,7 @@ We support three types of SMTP encryption: NONE, STARTTLS, and SSL/TLS.
| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
-| baseline_sheet_id | [int64](#int64) | | |
+| baseline_change_history_id | [string](#string) | | The baseline instance change history id of the schema design. |
| engine | [Engine](#bytebase-store-Engine) | | |
diff --git a/proto/generated-go/store/sheet.pb.go b/proto/generated-go/store/sheet.pb.go
index c1febaf3a1..c9b4fcd847 100644
--- a/proto/generated-go/store/sheet.pb.go
+++ b/proto/generated-go/store/sheet.pb.go
@@ -286,8 +286,9 @@ type SheetPayload_SchemaDesign struct {
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
- BaselineSheetId int64 `protobuf:"varint,1,opt,name=baseline_sheet_id,json=baselineSheetId,proto3" json:"baseline_sheet_id,omitempty"`
- Engine Engine `protobuf:"varint,2,opt,name=engine,proto3,enum=bytebase.store.Engine" json:"engine,omitempty"`
+ // The baseline instance change history id of the schema design.
+ BaselineChangeHistoryId string `protobuf:"bytes,1,opt,name=baseline_change_history_id,json=baselineChangeHistoryId,proto3" json:"baseline_change_history_id,omitempty"`
+ Engine Engine `protobuf:"varint,2,opt,name=engine,proto3,enum=bytebase.store.Engine" json:"engine,omitempty"`
}
func (x *SheetPayload_SchemaDesign) Reset() {
@@ -322,11 +323,11 @@ func (*SheetPayload_SchemaDesign) Descriptor() ([]byte, []int) {
return file_store_sheet_proto_rawDescGZIP(), []int{0, 2}
}
-func (x *SheetPayload_SchemaDesign) GetBaselineSheetId() int64 {
+func (x *SheetPayload_SchemaDesign) GetBaselineChangeHistoryId() string {
if x != nil {
- return x.BaselineSheetId
+ return x.BaselineChangeHistoryId
}
- return 0
+ return ""
}
func (x *SheetPayload_SchemaDesign) GetEngine() Engine {
@@ -342,7 +343,7 @@ var file_store_sheet_proto_rawDesc = []byte{
0x0a, 0x11, 0x73, 0x74, 0x6f, 0x72, 0x65, 0x2f, 0x73, 0x68, 0x65, 0x65, 0x74, 0x2e, 0x70, 0x72,
0x6f, 0x74, 0x6f, 0x12, 0x0e, 0x62, 0x79, 0x74, 0x65, 0x62, 0x61, 0x73, 0x65, 0x2e, 0x73, 0x74,
0x6f, 0x72, 0x65, 0x1a, 0x12, 0x73, 0x74, 0x6f, 0x72, 0x65, 0x2f, 0x63, 0x6f, 0x6d, 0x6d, 0x6f,
- 0x6e, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, 0xd4, 0x05, 0x0a, 0x0c, 0x53, 0x68, 0x65, 0x65,
+ 0x6e, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, 0xe5, 0x05, 0x0a, 0x0c, 0x53, 0x68, 0x65, 0x65,
0x74, 0x50, 0x61, 0x79, 0x6c, 0x6f, 0x61, 0x64, 0x12, 0x35, 0x0a, 0x04, 0x74, 0x79, 0x70, 0x65,
0x18, 0x01, 0x20, 0x01, 0x28, 0x0e, 0x32, 0x21, 0x2e, 0x62, 0x79, 0x74, 0x65, 0x62, 0x61, 0x73,
0x65, 0x2e, 0x73, 0x74, 0x6f, 0x72, 0x65, 0x2e, 0x53, 0x68, 0x65, 0x65, 0x74, 0x50, 0x61, 0x79,
@@ -378,18 +379,19 @@ var file_store_sheet_proto_rawDesc = []byte{
0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x03, 0x52, 0x07, 0x69, 0x73, 0x73, 0x75, 0x65, 0x49, 0x64,
0x12, 0x1f, 0x0a, 0x0b, 0x69, 0x73, 0x73, 0x75, 0x65, 0x5f, 0x74, 0x69, 0x74, 0x6c, 0x65, 0x18,
0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0a, 0x69, 0x73, 0x73, 0x75, 0x65, 0x54, 0x69, 0x74, 0x6c,
- 0x65, 0x1a, 0x6a, 0x0a, 0x0c, 0x53, 0x63, 0x68, 0x65, 0x6d, 0x61, 0x44, 0x65, 0x73, 0x69, 0x67,
- 0x6e, 0x12, 0x2a, 0x0a, 0x11, 0x62, 0x61, 0x73, 0x65, 0x6c, 0x69, 0x6e, 0x65, 0x5f, 0x73, 0x68,
- 0x65, 0x65, 0x74, 0x5f, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x03, 0x52, 0x0f, 0x62, 0x61,
- 0x73, 0x65, 0x6c, 0x69, 0x6e, 0x65, 0x53, 0x68, 0x65, 0x65, 0x74, 0x49, 0x64, 0x12, 0x2e, 0x0a,
- 0x06, 0x65, 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0e, 0x32, 0x16, 0x2e,
- 0x62, 0x79, 0x74, 0x65, 0x62, 0x61, 0x73, 0x65, 0x2e, 0x73, 0x74, 0x6f, 0x72, 0x65, 0x2e, 0x45,
- 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x52, 0x06, 0x65, 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x22, 0x2f, 0x0a,
- 0x04, 0x54, 0x79, 0x70, 0x65, 0x12, 0x14, 0x0a, 0x10, 0x54, 0x59, 0x50, 0x45, 0x5f, 0x55, 0x4e,
- 0x53, 0x50, 0x45, 0x43, 0x49, 0x46, 0x49, 0x45, 0x44, 0x10, 0x00, 0x12, 0x11, 0x0a, 0x0d, 0x53,
- 0x43, 0x48, 0x45, 0x4d, 0x41, 0x5f, 0x44, 0x45, 0x53, 0x49, 0x47, 0x4e, 0x10, 0x01, 0x42, 0x14,
- 0x5a, 0x12, 0x67, 0x65, 0x6e, 0x65, 0x72, 0x61, 0x74, 0x65, 0x64, 0x2d, 0x67, 0x6f, 0x2f, 0x73,
- 0x74, 0x6f, 0x72, 0x65, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
+ 0x65, 0x1a, 0x7b, 0x0a, 0x0c, 0x53, 0x63, 0x68, 0x65, 0x6d, 0x61, 0x44, 0x65, 0x73, 0x69, 0x67,
+ 0x6e, 0x12, 0x3b, 0x0a, 0x1a, 0x62, 0x61, 0x73, 0x65, 0x6c, 0x69, 0x6e, 0x65, 0x5f, 0x63, 0x68,
+ 0x61, 0x6e, 0x67, 0x65, 0x5f, 0x68, 0x69, 0x73, 0x74, 0x6f, 0x72, 0x79, 0x5f, 0x69, 0x64, 0x18,
+ 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x17, 0x62, 0x61, 0x73, 0x65, 0x6c, 0x69, 0x6e, 0x65, 0x43,
+ 0x68, 0x61, 0x6e, 0x67, 0x65, 0x48, 0x69, 0x73, 0x74, 0x6f, 0x72, 0x79, 0x49, 0x64, 0x12, 0x2e,
+ 0x0a, 0x06, 0x65, 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0e, 0x32, 0x16,
+ 0x2e, 0x62, 0x79, 0x74, 0x65, 0x62, 0x61, 0x73, 0x65, 0x2e, 0x73, 0x74, 0x6f, 0x72, 0x65, 0x2e,
+ 0x45, 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x52, 0x06, 0x65, 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x22, 0x2f,
+ 0x0a, 0x04, 0x54, 0x79, 0x70, 0x65, 0x12, 0x14, 0x0a, 0x10, 0x54, 0x59, 0x50, 0x45, 0x5f, 0x55,
+ 0x4e, 0x53, 0x50, 0x45, 0x43, 0x49, 0x46, 0x49, 0x45, 0x44, 0x10, 0x00, 0x12, 0x11, 0x0a, 0x0d,
+ 0x53, 0x43, 0x48, 0x45, 0x4d, 0x41, 0x5f, 0x44, 0x45, 0x53, 0x49, 0x47, 0x4e, 0x10, 0x01, 0x42,
+ 0x14, 0x5a, 0x12, 0x67, 0x65, 0x6e, 0x65, 0x72, 0x61, 0x74, 0x65, 0x64, 0x2d, 0x67, 0x6f, 0x2f,
+ 0x73, 0x74, 0x6f, 0x72, 0x65, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
}
var (
diff --git a/proto/store/sheet.proto b/proto/store/sheet.proto
index 89241465be..dc21a2f93b 100644
--- a/proto/store/sheet.proto
+++ b/proto/store/sheet.proto
@@ -32,7 +32,8 @@ message SheetPayload {
repeated UsedByIssue used_by_issues = 3;
message SchemaDesign {
- int64 baseline_sheet_id = 1;
+ // The baseline instance change history id of the schema design.
+ string baseline_change_history_id = 1;
Engine engine = 2;
}
SchemaDesign schema_design = 4;