Skip to content

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

feat: allow to select target branch when merging (#8804)

boojack8365dc0a0b83
diff --git a/backend/api/v1/schema_design_service.go b/backend/api/v1/schema_design_service.go
index 09815782e0..c0d23a24a8 100644
--- a/backend/api/v1/schema_design_service.go
+++ b/backend/api/v1/schema_design_service.go
@@ -296,7 +296,7 @@ func (s *SchemaDesignService) UpdateSchemaDesign(ctx context.Context, request *v
currentPrincipalID := ctx.Value(common.PrincipalIDContextKey).(int)
_, sheetID, err := common.GetProjectResourceIDAndSchemaDesignSheetID(request.SchemaDesign.Name)
if err != nil {
- return nil, status.Errorf(codes.InvalidArgument, err.Error())
+ return nil, status.Errorf(codes.InvalidArgument, fmt.Sprintf("invalid schema design name: %v", err))
}
sheetUID, err := strconv.Atoi(sheetID)
if err != nil {
@@ -318,10 +318,13 @@ func (s *SchemaDesignService) UpdateSchemaDesign(ctx context.Context, request *v
UID: sheetUID,
UpdaterID: currentPrincipalID,
}
- schemaDesign := request.SchemaDesign
+ schemaDesign, err := s.convertSheetToSchemaDesign(ctx, sheet, v1pb.SchemaDesignView_SCHEMA_DESIGN_VIEW_FULL)
+ if err != nil {
+ return nil, status.Errorf(codes.Internal, fmt.Sprintf("failed to convert sheet to schema design: %v", err))
+ }
_, databaseName, err := common.GetInstanceDatabaseID(schemaDesign.BaselineDatabase)
if err != nil {
- return nil, status.Errorf(codes.InvalidArgument, err.Error())
+ return nil, status.Errorf(codes.InvalidArgument, fmt.Sprintf("invalid baseline database: %v", err))
}
if slices.Contains(request.UpdateMask.Paths, "title") {
@@ -344,7 +347,7 @@ func (s *SchemaDesignService) UpdateSchemaDesign(ctx context.Context, request *v
if slices.Contains(request.UpdateMask.Paths, "baseline_sheet_name") {
_, sheetUID, err := common.GetProjectResourceIDSheetUID(schemaDesign.BaselineSheetName)
if err != nil {
- return nil, status.Errorf(codes.InvalidArgument, err.Error())
+ return nil, status.Errorf(codes.InvalidArgument, fmt.Sprintf("invalid baseline sheet name: %v", err))
}
sheet.Payload.SchemaDesign.BaselineSheetId = fmt.Sprintf("%d", sheetUID)
sheetUpdate.Payload = sheet.Payload
@@ -396,16 +399,13 @@ func (s *SchemaDesignService) MergeSchemaDesign(ctx context.Context, request *v1
if err != nil {
return nil, err
}
- if schemaDesign.Type != v1pb.SchemaDesign_PERSONAL_DRAFT {
- return nil, status.Errorf(codes.InvalidArgument, "only personal draft schema design can be merged")
- }
_, targetSheetID, err := common.GetProjectResourceIDAndSchemaDesignSheetID(request.TargetName)
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, err.Error())
}
targetSheetUID, err := strconv.Atoi(targetSheetID)
- if err != nil || targetSheetUID <= 0 {
+ if err != nil {
return nil, status.Errorf(codes.InvalidArgument, fmt.Sprintf("invalid sheet id %s, must be positive integer", targetSheetID))
}
targetSheet, err := s.getSheet(ctx, &store.FindSheetMessage{
@@ -420,11 +420,6 @@ func (s *SchemaDesignService) MergeSchemaDesign(ctx context.Context, request *v1
if err != nil {
return nil, err
}
- // Only allow merging to main branch schema design.
- // Maybe we can support merging to other personal draft schema design in the future.
- if targetSchemaDesign.Type != v1pb.SchemaDesign_MAIN_BRANCH {
- return nil, status.Errorf(codes.InvalidArgument, "only main branch schema design can be merged to")
- }
baselineEtag := generateEtag([]byte(schemaDesign.BaselineSchema))
// Restrict merging only when the target schema design is not updated.
diff --git a/frontend/src/components/Branch/BranchDetailView.vue b/frontend/src/components/Branch/BranchDetailView.vue
index 1301160ae8..881a823e38 100644
--- a/frontend/src/components/Branch/BranchDetailView.vue
+++ b/frontend/src/components/Branch/BranchDetailView.vue
@@ -25,7 +25,7 @@
>
<template v-if="!state.isEditing">
<NButton @click="handleEdit">{{ $t("common.edit") }}</NButton>
- <NButton v-if="parentBranch" @click="handleMergeBranch">{{
+ <NButton @click="handleMergeBranch">{{
$t("schema-designer.merge-branch")
}}</NButton>
<NButton type="primary" @click="handleApplySchemaDesignClick">{{
@@ -107,7 +107,7 @@
</template>
<script lang="ts" setup>
-import { cloneDeep, isEqual, uniqueId } from "lodash-es";
+import { cloneDeep, head, isEqual, uniqueId } from "lodash-es";
import { NButton, NDivider, NInput, NTooltip, useDialog, NTag } from "naive-ui";
import { Status } from "nice-grpc-common";
import { CSSProperties, computed, reactive, ref, watch } from "vue";
@@ -124,8 +124,14 @@ import {
useDatabaseV1Store,
useSchemaEditorV1Store,
} from "@/store";
-import { useSchemaDesignStore } from "@/store/modules/schemaDesign";
-import { getProjectAndSchemaDesignSheetId } from "@/store/modules/v1/common";
+import {
+ useSchemaDesignList,
+ useSchemaDesignStore,
+} from "@/store/modules/schemaDesign";
+import {
+ getProjectAndSchemaDesignSheetId,
+ projectNamePrefix,
+} from "@/store/modules/v1/common";
import { UNKNOWN_ID } from "@/types";
import {
SchemaDesign,
@@ -155,6 +161,7 @@ const router = useRouter();
const databaseStore = useDatabaseV1Store();
const changeHistoryStore = useChangeHistoryStore();
const schemaDesignStore = useSchemaDesignStore();
+const { schemaDesignList } = useSchemaDesignList();
const { runSQLCheck } = provideSQLCheckContext();
const dialog = useDialog();
const state = reactive<LocalState>({
@@ -292,13 +299,29 @@ const handleBranchTitleInputBlur = async () => {
};
const handleMergeBranch = () => {
- if (!parentBranch.value) {
+ const tempList = schemaDesignList.value.filter((item) => {
+ const [projectName] = getProjectAndSchemaDesignSheetId(item.name);
+ return (
+ `${projectNamePrefix}${projectName}` === project.value.name &&
+ item.engine === schemaDesign.value.engine &&
+ item.name !== schemaDesign.value.name
+ );
+ });
+ const targetBranchName = parentBranch.value
+ ? parentBranch.value.name
+ : head(tempList)?.name;
+ if (!targetBranchName) {
+ pushNotification({
+ module: "bytebase",
+ style: "CRITICAL",
+ title: "No branch to merge.",
+ });
return;
}
mergeBranchPanelContext.value = {
sourceBranchName: schemaDesign.value.name,
- targetBranchName: parentBranch.value.name,
+ targetBranchName: targetBranchName,
};
state.showDiffEditor = true;
};
diff --git a/frontend/src/components/Branch/BranchSelector.vue b/frontend/src/components/Branch/BranchSelector.vue
index fc010e42dc..9bb7507275 100644
--- a/frontend/src/components/Branch/BranchSelector.vue
+++ b/frontend/src/components/Branch/BranchSelector.vue
@@ -4,8 +4,8 @@
:value="branch"
:options="options"
:placeholder="$t('database.select-branch')"
- :clearable="true"
:filterable="true"
+ :clearable="clearable"
:filter="filterByName"
class="bb-branch-select"
@update:value="$emit('update:branch', $event)"
@@ -27,9 +27,11 @@ const props = withDefaults(
defineProps<{
branch?: string | undefined;
filter?: (branch: SchemaDesign, index: number) => boolean;
+ clearable?: boolean;
}>(),
{
branch: undefined,
+ clearable: true,
filter: () => true,
}
);
diff --git a/frontend/src/components/Branch/MergeBranchPanel.vue b/frontend/src/components/Branch/MergeBranchPanel.vue
index 48e4d0006f..99e03cd10b 100644
--- a/frontend/src/components/Branch/MergeBranchPanel.vue
+++ b/frontend/src/components/Branch/MergeBranchPanel.vue
@@ -28,11 +28,12 @@
</div>
<div class="w-full pr-12 pt-4 pb-6 grid grid-cols-3">
<div class="flex flex-row justify-end">
- <NInput
+ <BranchSelector
class="!w-4/5 text-center"
- readonly
- :value="targetBranch.title"
- size="large"
+ :clearable="false"
+ :branch="targetBranchName"
+ :filter="targetBranchFilter"
+ @update:branch="(branch) => (targetBranchName = branch || '')"
/>
</div>
<div class="flex flex-row justify-center">
@@ -58,6 +59,7 @@
<div class="w-full h-[calc(100%-14rem)] border">
<DiffEditor
v-if="state.initialized"
+ :key="targetBranchName"
class="h-full"
:original="targetBranch.schema"
:value="state.editingSchema"
@@ -80,7 +82,8 @@ import {
useDialog,
} from "naive-ui";
import { Status } from "nice-grpc-common";
-import { computed, onMounted, reactive } from "vue";
+import { computed, reactive, ref } from "vue";
+import { watch } from "vue";
import { useI18n } from "vue-i18n";
import DiffEditor from "@/components/MonacoEditor/DiffEditor.vue";
import { pushNotification, useSheetV1Store } from "@/store";
@@ -118,21 +121,35 @@ const { t } = useI18n();
const dialog = useDialog();
const sheetStore = useSheetV1Store();
const schemaDesignStore = useSchemaDesignStore();
+const targetBranchName = ref<string>(props.targetBranchName || "");
const sourceBranch = computed(() => {
return schemaDesignStore.getSchemaDesignByName(props.sourceBranchName || "");
});
const targetBranch = computed(() => {
- return schemaDesignStore.getSchemaDesignByName(props.targetBranchName || "");
+ return schemaDesignStore.getSchemaDesignByName(targetBranchName.value);
});
-onMounted(async () => {
- // Fetching the latest source branch.
- await schemaDesignStore.fetchSchemaDesignByName(props.targetBranchName);
- state.editingSchema = sourceBranch.value.schema;
- state.initialized = true;
-});
+const targetBranchFilter = (branch: SchemaDesign) => {
+ return (
+ branch.name !== props.sourceBranchName &&
+ branch.engine === sourceBranch.value.engine
+ );
+};
+
+watch(
+ () => targetBranchName.value,
+ async () => {
+ // Fetching the latest source branch.
+ await schemaDesignStore.fetchSchemaDesignByName(targetBranchName.value);
+ state.editingSchema = sourceBranch.value.schema;
+ state.initialized = true;
+ },
+ {
+ immediate: true,
+ }
+);
const handleSaveDraft = async (ignoreNotify?: boolean) => {
const updateMask = ["schema", "baseline_sheet_name"];
@@ -199,7 +216,7 @@ const handleMergeBranch = async () => {
onPositiveClick: async () => {
// Fetching the latest target branch.
await schemaDesignStore.fetchSchemaDesignByName(
- props.targetBranchName
+ targetBranchName.value
);
},
});
@@ -220,7 +237,7 @@ const handleMergeBranch = async () => {
title: t("schema-designer.message.merge-to-main-successfully"),
});
- emit("merged", props.targetBranchName);
+ emit("merged", targetBranchName.value);
if (state.deleteBranchAfterMerged) {
await schemaDesignStore.deleteSchemaDesign(props.sourceBranchName);
}
diff --git a/frontend/src/components/SchemaEditorV1/utils/metadata.ts b/frontend/src/components/SchemaEditorV1/utils/metadata.ts
index db1e834107..98665a08ae 100644
--- a/frontend/src/components/SchemaEditorV1/utils/metadata.ts
+++ b/frontend/src/components/SchemaEditorV1/utils/metadata.ts
@@ -302,7 +302,7 @@ export const transformTableEditToMetadata = (
if (tableEdit.primaryKey.columnIdList.length > 0) {
const primaryIndex = IndexMetadata.fromPartial({
- name: tableEdit.primaryKey.name,
+ name: tableEdit.primaryKey.name || `${tableEdit.name}_pkey`,
primary: true,
expressions: [],
});