Skip to content

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

feat: support creating branch from parent (#8801)

boojack5cebf931ed1a
diff --git a/frontend/src/components/Branch/BaselineSchemaSelector.vue b/frontend/src/components/Branch/BaselineSchemaSelector.vue
index 2e01bac117..978e8f6f53 100644
--- a/frontend/src/components/Branch/BaselineSchemaSelector.vue
+++ b/frontend/src/components/Branch/BaselineSchemaSelector.vue
@@ -91,7 +91,7 @@
<script lang="ts" setup>
import { head, isNull, isUndefined } from "lodash-es";
import { NEllipsis } from "naive-ui";
-import { computed, onMounted, reactive, watch } from "vue";
+import { computed, reactive, watch } from "vue";
import { InstanceV1EngineIcon } from "@/components/v2";
import {
useChangeHistoryStore,
@@ -152,20 +152,27 @@ const prepareChangeHistoryList = async () => {
);
};
-onMounted(async () => {
- if (props.baselineSchema?.databaseId) {
- try {
- const database = await databaseStore.getOrFetchDatabaseByUID(
- props.baselineSchema.databaseId || ""
- );
- state.databaseId = database.uid;
- state.environmentId = database.effectiveEnvironmentEntity.uid;
- state.changeHistory = props.baselineSchema.changeHistory;
- } catch (error) {
- // do nothing.
+watch(
+ () => props,
+ async () => {
+ if (props.baselineSchema?.databaseId) {
+ try {
+ const database = await databaseStore.getOrFetchDatabaseByUID(
+ props.baselineSchema.databaseId || ""
+ );
+ state.databaseId = database.uid;
+ state.environmentId = database.effectiveEnvironmentEntity.uid;
+ state.changeHistory = props.baselineSchema.changeHistory;
+ } catch (error) {
+ // do nothing.
+ }
}
+ },
+ {
+ immediate: true,
+ deep: true,
}
-});
+);
watch(
() => props.projectId,
diff --git a/frontend/src/components/Branch/BranchCreateView.vue b/frontend/src/components/Branch/BranchCreateView.vue
index 6cb4516463..9a6fb69a76 100644
--- a/frontend/src/components/Branch/BranchCreateView.vue
+++ b/frontend/src/components/Branch/BranchCreateView.vue
@@ -22,16 +22,27 @@
state.schemaDesignTitle = ($event.target as HTMLInputElement).value
"
/>
+ <span class="ml-8 mr-4 flex items-center text-sm">{{
+ $t("schema-designer.parent-branch")
+ }}</span>
+ <BranchSelector
+ class="!w-60"
+ :branch="state.parentBranchName"
+ @update:branch="(branch) => (state.parentBranchName = branch)"
+ />
</div>
<NDivider />
<div class="w-full flex flex-row justify-start items-center mt-1">
- <span class="flex w-40 items-center text-sm font-medium">{{
- $t("schema-designer.baseline-version")
+ <span class="flex w-full items-center text-sm font-medium">{{
+ state.parentBranchName
+ ? $t("schema-designer.baseline-version-from-parent")
+ : $t("schema-designer.baseline-version")
}}</span>
</div>
<BaselineSchemaSelector
:project-id="state.projectId"
:baseline-schema="state.baselineSchema"
+ :readonly="!allowToChangeBaseline"
@update="handleBaselineSchemaChange"
/>
<div class="!mt-6 w-full h-[32rem]">
@@ -111,6 +122,7 @@ interface LocalState {
schemaDesignTitle: string;
baselineSchema: BaselineSchema;
schemaDesign: SchemaDesign;
+ parentBranchName?: string;
}
const props = defineProps({
@@ -126,6 +138,7 @@ const route = useRoute();
const projectStore = useProjectV1Store();
const databaseStore = useDatabaseV1Store();
const schemaDesignStore = useSchemaDesignStore();
+const changeHistoryStore = useChangeHistoryStore();
const sheetStore = useSheetV1Store();
const state = reactive<LocalState>({
schemaDesignTitle: "",
@@ -137,6 +150,15 @@ const state = reactive<LocalState>({
});
const refreshId = ref<string>("");
+const project = computed(() => {
+ const project = projectStore.getProjectByUID(state.projectId || "");
+ return project;
+});
+
+const allowToChangeBaseline = computed(() => {
+ return !state.parentBranchName;
+});
+
onMounted(async () => {
const projectName = route.params.projectName;
if (projectName !== "-") {
@@ -157,6 +179,38 @@ watch(
}
);
+watch(
+ () => [state.parentBranchName],
+ async () => {
+ if (!state.parentBranchName) {
+ return;
+ }
+
+ const branch = await schemaDesignStore.getOrFetchSchemaDesignByName(
+ state.parentBranchName
+ );
+ const database = await databaseStore.getOrFetchDatabaseByName(
+ branch.baselineDatabase
+ );
+ state.projectId = database.projectEntity.uid;
+ state.baselineSchema.databaseId = database.uid;
+ if (
+ branch.baselineChangeHistoryId &&
+ branch.baselineChangeHistoryId !== String(UNKNOWN_ID)
+ ) {
+ const changeHistoryName = `${database.name}/changeHistories/${branch.baselineChangeHistoryId}`;
+ state.baselineSchema.changeHistory =
+ await changeHistoryStore.getOrFetchChangeHistoryByName(
+ changeHistoryName
+ );
+ } else {
+ state.baselineSchema.changeHistory = undefined;
+ }
+ state.schemaDesign = branch;
+ refreshId.value = uniqueId();
+ }
+);
+
const prepareFullChangeHistorySchema = async (changeHistory: ChangeHistory) => {
// While a database has no change histories, the state.baselineSchema.changeHistory
// is a mock ChangeHistory entity with uid = -1
@@ -192,9 +246,12 @@ const prepareSchemaDesign = async () => {
baselineSchemaMetadata: baselineMetadata,
schema: fullSchema,
schemaMetadata: baselineMetadata,
+ type: SchemaDesign_Type.MAIN_BRANCH,
});
}
- return SchemaDesign.fromPartial({});
+ return SchemaDesign.fromPartial({
+ type: SchemaDesign_Type.MAIN_BRANCH,
+ });
};
const allowConfirm = computed(() => {
@@ -214,6 +271,10 @@ const handleProjectSelect = async (projectId: string) => {
};
const handleBaselineSchemaChange = async (baselineSchema: BaselineSchema) => {
+ if (state.parentBranchName) {
+ return;
+ }
+
state.baselineSchema = baselineSchema;
if (
baselineSchema.databaseId &&
@@ -225,11 +286,6 @@ const handleBaselineSchemaChange = async (baselineSchema: BaselineSchema) => {
state.schemaDesign = await prepareSchemaDesign();
};
-const project = computed(() => {
- const project = projectStore.getProjectByUID(state.projectId || "");
- return project;
-});
-
const handleConfirm = async () => {
if (!state.schemaDesign) {
return;
@@ -273,26 +329,37 @@ const handleConfirm = async () => {
type: Sheet_Type.TYPE_SQL,
});
- const createdSchemaDesign = await schemaDesignStore.createSchemaDesign(
- project.value.name,
- SchemaDesign.fromPartial({
+ let createdSchemaDesign;
+ if (!state.parentBranchName) {
+ createdSchemaDesign = await schemaDesignStore.createSchemaDesign(
+ project.value.name,
+ SchemaDesign.fromPartial({
+ title: state.schemaDesignTitle,
+ // Keep schema empty in frontend. Backend will generate the design schema.
+ schema: "",
+ schemaMetadata: metadata,
+ baselineSchema: state.schemaDesign.baselineSchema,
+ baselineSchemaMetadata: state.schemaDesign.baselineSchemaMetadata,
+ engine: state.schemaDesign.engine,
+ type: SchemaDesign_Type.MAIN_BRANCH,
+ baselineDatabase: baselineDatabase,
+ baselineSheetName: baselineSheet.name,
+ baselineChangeHistoryId: state.baselineSchema.changeHistory?.uid,
+ protection: {
+ // For main branches, we don't allow force pushes by default.
+ allowForcePushes: false,
+ },
+ })
+ );
+ } else {
+ const parentBranch = await schemaDesignStore.getOrFetchSchemaDesignByName(
+ state.parentBranchName
+ );
+ createdSchemaDesign = await schemaDesignStore.createSchemaDesignDraft({
+ ...parentBranch,
title: state.schemaDesignTitle,
- // Keep schema empty in frontend. Backend will generate the design schema.
- schema: "",
- schemaMetadata: metadata,
- baselineSchema: state.schemaDesign.baselineSchema,
- baselineSchemaMetadata: state.schemaDesign.baselineSchemaMetadata,
- engine: state.schemaDesign.engine,
- type: SchemaDesign_Type.MAIN_BRANCH,
- baselineDatabase: baselineDatabase,
- baselineSheetName: baselineSheet.name,
- baselineChangeHistoryId: state.baselineSchema.changeHistory?.uid,
- protection: {
- // For main branches, we don't allow force pushes by default.
- allowForcePushes: false,
- },
- })
- );
+ });
+ }
pushNotification({
module: "bytebase",
style: "SUCCESS",
diff --git a/frontend/src/components/Branch/BranchDataTable.vue b/frontend/src/components/Branch/BranchDataTable.vue
index 009be0b81e..bc12640c24 100644
--- a/frontend/src/components/Branch/BranchDataTable.vue
+++ b/frontend/src/components/Branch/BranchDataTable.vue
@@ -170,7 +170,7 @@ const rowKey = (row: BranchRowData) => {
const rowProps = (row: BranchRowData) => {
return {
- class: "cursor-pointer hover:bg-gray-100",
+ class: "cursor-pointer hover:bg-gray-100",
onClick: (event: MouseEvent) => {
const targetElement = event.target as HTMLElement;
const triggerElement = targetElement.closest(
@@ -198,7 +198,7 @@ const getUpdatedTimeStr = (branch: SchemaDesign) => {
<style lang="postcss">
.n-data-table-expand-trigger {
- @apply !w-5 !h-5 inline-flex justify-center items-center translate-y-0.5 rounded hover:bg-gray-100 hover:shadow;
+ @apply !w-5 !h-5 inline-flex justify-center items-center translate-y-0.5 rounded hover:bg-white hover:shadow;
}
.n-data-table-expand-trigger > .n-base-icon {
@apply !w-5 !h-5 flex flex-row justify-center items-center;
diff --git a/frontend/src/components/Branch/BranchDetailView.vue b/frontend/src/components/Branch/BranchDetailView.vue
index ea9e60afcf..1301160ae8 100644
--- a/frontend/src/components/Branch/BranchDetailView.vue
+++ b/frontend/src/components/Branch/BranchDetailView.vue
@@ -476,7 +476,7 @@ const handleApplySchemaDesignClick = () => {
const deleteSchemaDesign = async () => {
await schemaDesignStore.deleteSchemaDesign(schemaDesign.value.name);
router.replace({
- name: "workspace.branch",
+ name: "workspace.branch.dashboard",
});
};
</script>
diff --git a/frontend/src/components/Branch/BranchSelector.vue b/frontend/src/components/Branch/BranchSelector.vue
new file mode 100644
index 0000000000..fc010e42dc
--- /dev/null
+++ b/frontend/src/components/Branch/BranchSelector.vue
@@ -0,0 +1,75 @@
+<template>
+ <NSelect
+ v-bind="$attrs"
+ :value="branch"
+ :options="options"
+ :placeholder="$t('database.select-branch')"
+ :clearable="true"
+ :filterable="true"
+ :filter="filterByName"
+ class="bb-branch-select"
+ @update:value="$emit('update:branch', $event)"
+ />
+</template>
+
+<script lang="ts" setup>
+import { NSelect, SelectOption } from "naive-ui";
+import { computed } from "vue";
+import { useSchemaDesignList } from "@/store";
+import { SchemaDesign } from "@/types/proto/v1/schema_design_service";
+
+interface BranchSelectOption extends SelectOption {
+ value: string;
+ branch: SchemaDesign;
+}
+
+const props = withDefaults(
+ defineProps<{
+ branch?: string | undefined;
+ filter?: (branch: SchemaDesign, index: number) => boolean;
+ }>(),
+ {
+ branch: undefined,
+ filter: () => true,
+ }
+);
+
+defineEmits<{
+ (event: "update:branch", name: string | undefined): void;
+}>();
+
+const { schemaDesignList: branchList } = useSchemaDesignList();
+
+const combinedBranchList = computed(() => {
+ let list = branchList.value;
+ if (props.filter) {
+ list = list.filter(props.filter);
+ }
+ return list;
+});
+
+const options = computed(() => {
+ return combinedBranchList.value.map<BranchSelectOption>((branch) => {
+ return {
+ branch,
+ value: branch.name,
+ label: branch.title,
+ };
+ });
+});
+
+const filterByName = (pattern: string, option: SelectOption) => {
+ const { branch } = option as BranchSelectOption;
+ pattern = pattern.toLowerCase();
+ return (
+ branch.name.toLowerCase().includes(pattern) ||
+ branch.title.toLowerCase().includes(pattern)
+ );
+};
+</script>
+
+<style lang="postcss" scoped>
+.bb-branch-select :deep(.n-base-selection-input:focus) {
+ @apply !ring-0;
+}
+</style>
diff --git a/frontend/src/locales/en-US.json b/frontend/src/locales/en-US.json
index 149504b58d..932b3df16e 100644
--- a/frontend/src/locales/en-US.json
+++ b/frontend/src/locales/en-US.json
@@ -3054,6 +3054,7 @@
"schema-version": "Schema version",
"personal-draft": "Personal draft",
"baseline-version": "Baseline",
+ "baseline-version-from-parent": "Baseline from parent branch",
"new-branch": "New branch",
"parent-branch": "Parent",
"save-draft": "Save draft",
diff --git a/frontend/src/locales/es-ES.json b/frontend/src/locales/es-ES.json
index c18a6901ce..61f4ded78c 100644
--- a/frontend/src/locales/es-ES.json
+++ b/frontend/src/locales/es-ES.json
@@ -3052,6 +3052,7 @@
"schema-version": "Versión del esquema",
"personal-draft": "Borrador personal",
"baseline-version": "Versión de referencia",
+ "baseline-version-from-parent": "Versión de referencia de la rama principal",
"new-branch": "Nueva rama",
"parent-branch": "Rama principal",
"save-draft": "Guardar borrador",
diff --git a/frontend/src/locales/zh-CN.json b/frontend/src/locales/zh-CN.json
index 257f396cc1..a3d1cefde0 100644
--- a/frontend/src/locales/zh-CN.json
+++ b/frontend/src/locales/zh-CN.json
@@ -3047,6 +3047,7 @@
"schema-version": "Schema 版本",
"personal-draft": "个人草稿",
"baseline-version": "基线版本",
+ "baseline-version-from-parent": "父分支基线版本",
"new-branch": "新建分支",
"parent-branch": "父分支",
"save-draft": "保存草稿",