Skip to content

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

chore: update branch ID (#9676)

Danny Xu88b5e349db20
diff --git a/backend/api/v1/branch_service.go b/backend/api/v1/branch_service.go
index 01ecdde548..b8b4ccb9b5 100644
--- a/backend/api/v1/branch_service.go
+++ b/backend/api/v1/branch_service.go
@@ -251,23 +251,42 @@ func (s *BranchService) UpdateBranch(ctx context.Context, request *v1pb.UpdateBr
if branch == nil {
return nil, status.Errorf(codes.NotFound, "branch %q not found", branchID)
}
- schemaDesign := request.Branch
-
+ principalID, ok := ctx.Value(common.PrincipalIDContextKey).(int)
+ if !ok {
+ return nil, status.Errorf(codes.Internal, "principal ID not found")
+ }
// TODO(d): handle etag.
- // TODO(d): support branch id update.
- // if slices.Contains(request.UpdateMask.Paths, "title") {
- // }
+
+ // Handle branch ID update.
+ if slices.Contains(request.UpdateMask.Paths, "branch_id") {
+ if len(request.UpdateMask.Paths) > 1 {
+ return nil, status.Errorf(codes.InvalidArgument, "cannot update branch_id with other types of updates")
+ }
+ updateBranchMessage := &store.UpdateBranchMessage{ProjectID: projectID, ResourceID: branchID, UpdaterID: principalID, UpdateResourceID: &request.Branch.BranchId}
+ if err := s.store.UpdateBranch(ctx, updateBranchMessage); err != nil {
+ return nil, status.Errorf(codes.Internal, fmt.Sprintf("failed to update branch, error %v", err))
+ }
+ branch, err = s.store.GetBranch(ctx, &store.FindBranchMessage{ProjectID: &projectID, ResourceID: &request.Branch.BranchId, LoadFull: true})
+ if err != nil {
+ return nil, err
+ }
+ v1Branch, err := s.convertBranchToBranch(ctx, project, branch, v1pb.BranchView_BRANCH_VIEW_FULL)
+ if err != nil {
+ return nil, err
+ }
+ return v1Branch, nil
+ }
headUpdate := branch.Head
hasHeadUpdate := false
// TODO(d): this section needs some clarifications for merging branches.
if slices.Contains(request.UpdateMask.Paths, "schema") && slices.Contains(request.UpdateMask.Paths, "metadata") {
- headUpdate.Schema = []byte(schemaDesign.Schema)
- sanitizeBranchSchemaMetadata(schemaDesign)
+ headUpdate.Schema = []byte(request.Branch.Schema)
+ sanitizeBranchSchemaMetadata(request.Branch)
// TODO(d): update database metadata and config.
hasHeadUpdate = true
} else if slices.Contains(request.UpdateMask.Paths, "schema") {
- headUpdate.Schema = []byte(schemaDesign.Schema)
+ headUpdate.Schema = []byte(request.Branch.Schema)
hasHeadUpdate = true
// TODO(d): convert schema to metadata.
// Try to transform the schema string to database metadata to make sure it's valid.
@@ -275,7 +294,7 @@ func (s *BranchService) UpdateBranch(ctx context.Context, request *v1pb.UpdateBr
// return nil, status.Errorf(codes.Internal, fmt.Sprintf("failed to transform schema string to database metadata: %v", err))
// }
} else if slices.Contains(request.UpdateMask.Paths, "metadata") {
- sanitizeBranchSchemaMetadata(schemaDesign)
+ sanitizeBranchSchemaMetadata(request.Branch)
// schema, err := getDesignSchema(schemaDesign.Engine, schemaDesign.BaselineSchema, schemaDesign.SchemaMetadata)
// if err != nil {
// return nil, err
@@ -285,10 +304,6 @@ func (s *BranchService) UpdateBranch(ctx context.Context, request *v1pb.UpdateBr
}
// TODO(d): handle database config as well.
- principalID, ok := ctx.Value(common.PrincipalIDContextKey).(int)
- if !ok {
- return nil, status.Errorf(codes.Internal, "principal ID not found")
- }
updateBranchMessage := &store.UpdateBranchMessage{ProjectID: projectID, ResourceID: branchID, UpdaterID: principalID}
if hasHeadUpdate {
updateBranchMessage.Head = headUpdate
@@ -301,11 +316,11 @@ func (s *BranchService) UpdateBranch(ctx context.Context, request *v1pb.UpdateBr
if err != nil {
return nil, err
}
- schemaDesign, err = s.convertBranchToBranch(ctx, project, branch, v1pb.BranchView_BRANCH_VIEW_FULL)
+ v1Branch, err := s.convertBranchToBranch(ctx, project, branch, v1pb.BranchView_BRANCH_VIEW_FULL)
if err != nil {
return nil, err
}
- return schemaDesign, nil
+ return v1Branch, nil
}
// MergeBranch merges a personal draft branch to the target branch.
diff --git a/backend/store/branch.go b/backend/store/branch.go
index 184d645f34..cec5d171bb 100644
--- a/backend/store/branch.go
+++ b/backend/store/branch.go
@@ -41,12 +41,13 @@ type FindBranchMessage struct {
// UpdateBranchMessage is the message to update a branch.
type UpdateBranchMessage struct {
- ProjectID string
- ResourceID string
- UpdaterID int
- Config *storepb.BranchConfig
- Base *storepb.BranchSnapshot
- Head *storepb.BranchSnapshot
+ ProjectID string
+ ResourceID string
+ UpdaterID int
+ Config *storepb.BranchConfig
+ Base *storepb.BranchSnapshot
+ Head *storepb.BranchSnapshot
+ UpdateResourceID *string
}
// GetBranch gets a branch.
@@ -274,6 +275,9 @@ func (s *Store) UpdateBranch(ctx context.Context, update *UpdateBranchMessage) e
}
set, args = append(set, fmt.Sprintf("config = $%d", len(args)+1)), append(args, config)
}
+ if v := update.UpdateResourceID; v != nil {
+ set, args = append(set, fmt.Sprintf("name = $%d", len(args)+1)), append(args, *v)
+ }
args = append(args, project.UID, update.ResourceID)
if _, err := tx.ExecContext(ctx, fmt.Sprintf(`
diff --git a/frontend/src/components/Branch/BranchDetailView.vue b/frontend/src/components/Branch/BranchDetailView.vue
index 30c0416a79..849b605b0d 100644
--- a/frontend/src/components/Branch/BranchDetailView.vue
+++ b/frontend/src/components/Branch/BranchDetailView.vue
@@ -3,14 +3,14 @@
<div class="w-full flex flex-row justify-between items-center">
<div class="w-full flex flex-row justify-start items-center gap-x-2">
<NInput
- v-model:value="state.schemaDesignTitle"
+ v-model:value="state.branchId"
class="!w-auto"
:passively-activated="true"
- :style="titleInputStyle"
- :readonly="!state.isEditingTitle"
+ :style="branchIdInputStyle"
+ :readonly="!state.isEditingBranchId"
:placeholder="'feature/add-billing'"
- @focus="state.isEditingTitle = true"
- @blur="handleBranchTitleInputBlur"
+ @focus="state.isEditingBranchId = true"
+ @blur="handleBranchIdInputBlur"
/>
<NTag v-if="parentBranch" round>
{{ $t("schema-designer.parent-branch") }}:
@@ -74,7 +74,7 @@
:key="schemaEditorKey"
:project="project"
:readonly="!state.isEditing"
- :branch="schemaDesign"
+ :branch="branch"
/>
</div>
<!-- Don't show delete button in view mode. -->
@@ -91,7 +91,7 @@
<TargetDatabasesSelectPanel
v-if="selectTargetDatabasesContext.show"
:project-id="project.uid"
- :engine="schemaDesign.engine"
+ :engine="branch.engine"
:selected-database-id-list="[]"
@close="selectTargetDatabasesContext.show = false"
@update="handleSelectedDatabaseIdListChanged"
@@ -141,9 +141,9 @@ import SchemaDesignEditor from "./SchemaDesignEditor.vue";
import { generateForkedBranchName, validateBranchName } from "./utils";
interface LocalState {
- schemaDesignTitle: string;
+ branchId: string;
isEditing: boolean;
- isEditingTitle: boolean;
+ isEditingBranchId: boolean;
showDiffEditor: boolean;
isSaving: boolean;
}
@@ -164,9 +164,9 @@ const { branchList, ready } = useBranchList(
const { runSQLCheck } = provideSQLCheckContext();
const dialog = useDialog();
const state = reactive<LocalState>({
- schemaDesignTitle: "",
+ branchId: "",
isEditing: false,
- isEditingTitle: false,
+ isEditingBranchId: false,
showDiffEditor: false,
isSaving: false,
});
@@ -181,15 +181,15 @@ const selectTargetDatabasesContext = ref<{
show: false,
});
-const schemaDesign = computed(() => {
+const branch = computed(() => {
return props.branch;
});
const parentBranch = asyncComputed(async () => {
// Show parent branch when the current branch is a personal draft and it's not the new created one.
- if (schemaDesign.value.parentBranch !== "") {
+ if (branch.value.parentBranch !== "") {
return await branchStore.fetchBranchByName(
- schemaDesign.value.parentBranch,
+ branch.value.parentBranch,
true /* useCache */
);
}
@@ -197,21 +197,21 @@ const parentBranch = asyncComputed(async () => {
}, undefined);
const baselineDatabase = computed(() => {
- return databaseStore.getDatabaseByName(schemaDesign.value.baselineDatabase);
+ return databaseStore.getDatabaseByName(branch.value.baselineDatabase);
});
const project = computed(() => {
return baselineDatabase.value.projectEntity;
});
-const titleInputStyle = computed(() => {
+const branchIdInputStyle = computed(() => {
const style: CSSProperties = {
cursor: "default",
minWidth: "10rem",
"--n-color-disabled": "transparent",
"--n-font-size": "20px",
};
- const border = state.isEditingTitle
+ const border = state.isEditingBranchId
? "1px solid rgb(var(--color-control-border))"
: "none";
style["--n-border"] = border;
@@ -221,20 +221,18 @@ const titleInputStyle = computed(() => {
});
const prepareBaselineDatabase = async () => {
- await databaseStore.getOrFetchDatabaseByName(
- schemaDesign.value.baselineDatabase
- );
+ await databaseStore.getOrFetchDatabaseByName(branch.value.baselineDatabase);
};
watch(
() => [props.branch],
async () => {
- state.schemaDesignTitle = schemaDesign.value.branchId;
+ state.branchId = branch.value.branchId;
await prepareBaselineDatabase();
// Prepare the parent branch for personal draft.
- if (schemaDesign.value.parentBranch !== "") {
+ if (branch.value.parentBranch !== "") {
await branchStore.fetchBranchByName(
- schemaDesign.value.parentBranch,
+ branch.value.parentBranch,
true /* useCache */
);
}
@@ -244,8 +242,8 @@ watch(
}
);
-const handleBranchTitleInputBlur = async () => {
- if (state.schemaDesignTitle === "") {
+const handleBranchIdInputBlur = async () => {
+ if (state.branchId === "") {
pushNotification({
module: "bytebase",
style: "WARN",
@@ -253,7 +251,7 @@ const handleBranchTitleInputBlur = async () => {
});
return;
}
- if (!validateBranchName(state.schemaDesignTitle)) {
+ if (!validateBranchName(state.branchId)) {
pushNotification({
module: "bytebase",
style: "CRITICAL",
@@ -263,15 +261,15 @@ const handleBranchTitleInputBlur = async () => {
}
const updateMask = [];
- if (schemaDesign.value.branchId !== state.schemaDesignTitle) {
- updateMask.push("title");
+ if (branch.value.branchId !== state.branchId) {
+ updateMask.push("branch_id");
}
if (updateMask.length !== 0) {
await branchStore.updateBranch(
Branch.fromPartial({
- name: schemaDesign.value.name,
- branchId: state.schemaDesignTitle,
- baselineDatabase: schemaDesign.value.baselineDatabase,
+ name: branch.value.name,
+ branchId: state.branchId,
+ baselineDatabase: branch.value.baselineDatabase,
}),
updateMask
);
@@ -281,7 +279,7 @@ const handleBranchTitleInputBlur = async () => {
title: t("schema-designer.message.updated-succeed"),
});
}
- state.isEditingTitle = false;
+ state.isEditingBranchId = false;
};
const handleMergeBranch = () => {
@@ -289,8 +287,8 @@ const handleMergeBranch = () => {
const [projectName] = getProjectAndBranchId(item.name);
return (
`${projectNamePrefix}${projectName}` === project.value.name &&
- item.engine === schemaDesign.value.engine &&
- item.name !== schemaDesign.value.name
+ item.engine === branch.value.engine &&
+ item.name !== branch.value.name
);
});
const targetBranchName = parentBranch.value
@@ -306,7 +304,7 @@ const handleMergeBranch = () => {
}
mergeBranchPanelContext.value = {
- sourceBranchName: schemaDesign.value.name,
+ sourceBranchName: branch.value.name,
targetBranchName: targetBranchName,
};
state.showDiffEditor = true;
@@ -319,7 +317,7 @@ const handleEdit = async () => {
const handleCancelEdit = async () => {
const schemaEditorV1Store = useSchemaEditorV1Store();
const branchSchema = schemaEditorV1Store.resourceMap["branch"].get(
- schemaDesign.value.name
+ branch.value.name
);
if (!branchSchema) {
return;
@@ -332,7 +330,7 @@ const handleCancelEdit = async () => {
branchSchema.schemaList,
cloneDeep(baselineMetadata)
);
- if (!isEqual(mergedMetadata, schemaDesign.value.schemaMetadata)) {
+ if (!isEqual(mergedMetadata, branch.value.schemaMetadata)) {
// If the metadata is changed, we need to rebuild the editing state.
schemaEditorKey.value = uniqueId();
}
@@ -354,7 +352,7 @@ const handleSaveBranch = async () => {
const updateMask = [];
const schemaEditorV1Store = useSchemaEditorV1Store();
const branchSchema = schemaEditorV1Store.resourceMap["branch"].get(
- schemaDesign.value.name
+ branch.value.name
);
if (!branchSchema) {
return;
@@ -377,22 +375,22 @@ const handleSaveBranch = async () => {
});
return;
}
- if (!isEqual(mergedMetadata, schemaDesign.value.schemaMetadata)) {
+ if (!isEqual(mergedMetadata, branch.value.schemaMetadata)) {
updateMask.push("metadata");
}
state.isSaving = true;
if (updateMask.length !== 0) {
- if (schemaDesign.value.parentBranch === "") {
- const branchName = generateForkedBranchName(schemaDesign.value);
+ if (branch.value.parentBranch === "") {
+ const branchName = generateForkedBranchName(branch.value);
const newBranch = await branchStore.createBranchDraft(
project.value.name,
branchName,
- schemaDesign.value.name
+ branch.value.name
);
try {
await branchStore.mergeBranch({
- name: schemaDesign.value.name,
+ name: branch.value.name,
headBranch: newBranch.name,
});
} catch (error: any) {
@@ -423,7 +421,7 @@ const handleSaveBranch = async () => {
state.showDiffEditor = true;
mergeBranchPanelContext.value = {
sourceBranchName: newBranch.name,
- targetBranchName: schemaDesign.value.name,
+ targetBranchName: branch.value.name,
};
},
});
@@ -443,17 +441,17 @@ const handleSaveBranch = async () => {
await branchStore.deleteBranch(newBranch.name);
// Fetch the latest schema design after merged.
await branchStore.fetchBranchByName(
- schemaDesign.value.name,
+ branch.value.name,
false /* !useCache */
);
} else {
await branchStore.updateBranch(
Branch.fromPartial({
- name: schemaDesign.value.name,
- branchId: state.schemaDesignTitle,
- engine: schemaDesign.value.engine,
- baselineDatabase: schemaDesign.value.baselineDatabase,
- baselineSchema: schemaDesign.value.baselineSchema,
+ name: branch.value.name,
+ branchId: state.branchId,
+ engine: branch.value.engine,
+ baselineDatabase: branch.value.baselineDatabase,
+ baselineSchema: branch.value.baselineSchema,
schemaMetadata: mergedMetadata,
}),
updateMask
@@ -490,9 +488,9 @@ const handleSelectedDatabaseIdListChanged = async (
try {
const diffResponse = await branchServiceClient.diffMetadata(
{
- sourceMetadata: schemaDesign.value.baselineSchemaMetadata,
- targetMetadata: schemaDesign.value.schemaMetadata,
- engine: schemaDesign.value.engine,
+ sourceMetadata: branch.value.baselineSchemaMetadata,
+ targetMetadata: branch.value.schemaMetadata,
+ engine: branch.value.engine,
},
{
silent: true,
@@ -511,8 +509,8 @@ const handleSelectedDatabaseIdListChanged = async (
if (
statement === "" &&
!isEqual(
- schemaDesign.value.baselineSchemaMetadata?.schemaConfigs,
- schemaDesign.value.schemaMetadata?.schemaConfigs
+ branch.value.baselineSchemaMetadata?.schemaConfigs,
+ branch.value.schemaMetadata?.schemaConfigs
)
) {
pushNotification({
@@ -531,7 +529,7 @@ const handleSelectedDatabaseIdListChanged = async (
project: project.value.uid,
mode: "normal",
ghost: undefined,
- branch: schemaDesign.value.name,
+ branch: branch.value.name,
};
query.databaseList = databaseIdList.join(",");
query.sql = statement;
@@ -563,7 +561,7 @@ const generateIssueName = (databaseNameList: string[]) => {
};
const deleteBranch = async () => {
- await branchStore.deleteBranch(schemaDesign.value.name);
+ await branchStore.deleteBranch(branch.value.name);
router.replace({
name: "workspace.project.detail",
hash: "#branches",