Skip to content

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

chore: fix create branch frontend (#9671)

Danny Xudda5c595e635
diff --git a/backend/api/v1/branch_service.go b/backend/api/v1/branch_service.go
index 95547e8c58..20cfa2b8e8 100644
--- a/backend/api/v1/branch_service.go
+++ b/backend/api/v1/branch_service.go
@@ -456,11 +456,24 @@ func (*BranchService) DiffMetadata(_ context.Context, request *v1pb.DiffMetadata
}
func (s *BranchService) getProject(ctx context.Context, projectID string) (*store.ProjectMessage, error) {
- project, err := s.store.GetProjectV2(ctx, &store.FindProjectMessage{
- ResourceID: &projectID,
- })
- if err != nil {
- return nil, status.Errorf(codes.Internal, err.Error())
+ var project *store.ProjectMessage
+ projectUID, isNumber := isNumber(projectID)
+ if isNumber {
+ v, err := s.store.GetProjectV2(ctx, &store.FindProjectMessage{
+ UID: &projectUID,
+ })
+ if err != nil {
+ return nil, status.Errorf(codes.Internal, err.Error())
+ }
+ project = v
+ } else {
+ v, err := s.store.GetProjectV2(ctx, &store.FindProjectMessage{
+ ResourceID: &projectID,
+ })
+ if err != nil {
+ return nil, status.Errorf(codes.Internal, err.Error())
+ }
+ project = v
}
if project == nil {
return nil, status.Errorf(codes.NotFound, "project %q not found", projectID)
diff --git a/backend/migrator/migration/dev/LATEST.sql b/backend/migrator/migration/dev/LATEST.sql
index d96dbd8c26..ae725180fb 100644
--- a/backend/migrator/migration/dev/LATEST.sql
+++ b/backend/migrator/migration/dev/LATEST.sql
@@ -1253,7 +1253,9 @@ EXECUTE FUNCTION trigger_update_updated_ts();
CREATE TABLE branch (
id SERIAL PRIMARY KEY,
row_status row_status NOT NULL DEFAULT 'NORMAL',
+ creator_id INTEGER NOT NULL REFERENCES principal (id),
created_ts BIGINT NOT NULL DEFAULT extract(epoch from now()),
+ updater_id INTEGER NOT NULL REFERENCES principal (id),
updated_ts BIGINT NOT NULL DEFAULT extract(epoch from now()),
project_id INTEGER NOT NULL REFERENCES project (id),
name TEXT NOT NULL,
diff --git a/backend/migrator/migration/prod/2.12/0000##branch.sql b/backend/migrator/migration/prod/2.12/0000##branch.sql
index ce52a4498a..a481b3c118 100644
--- a/backend/migrator/migration/prod/2.12/0000##branch.sql
+++ b/backend/migrator/migration/prod/2.12/0000##branch.sql
@@ -1,7 +1,9 @@
CREATE TABLE branch (
id SERIAL PRIMARY KEY,
row_status row_status NOT NULL DEFAULT 'NORMAL',
+ creator_id INTEGER NOT NULL REFERENCES principal (id),
created_ts BIGINT NOT NULL DEFAULT extract(epoch from now()),
+ updater_id INTEGER NOT NULL REFERENCES principal (id),
updated_ts BIGINT NOT NULL DEFAULT extract(epoch from now()),
project_id INTEGER NOT NULL REFERENCES project (id),
name TEXT NOT NULL,
diff --git a/backend/migrator/migration/prod/LATEST.sql b/backend/migrator/migration/prod/LATEST.sql
index 5657926428..59ce07f4a5 100644
--- a/backend/migrator/migration/prod/LATEST.sql
+++ b/backend/migrator/migration/prod/LATEST.sql
@@ -1252,7 +1252,9 @@ EXECUTE FUNCTION trigger_update_updated_ts();
CREATE TABLE branch (
id SERIAL PRIMARY KEY,
row_status row_status NOT NULL DEFAULT 'NORMAL',
+ creator_id INTEGER NOT NULL REFERENCES principal (id),
created_ts BIGINT NOT NULL DEFAULT extract(epoch from now()),
+ updater_id INTEGER NOT NULL REFERENCES principal (id),
updated_ts BIGINT NOT NULL DEFAULT extract(epoch from now()),
project_id INTEGER NOT NULL REFERENCES project (id),
name TEXT NOT NULL,
diff --git a/frontend/src/components/Branch/BranchCreateView.vue b/frontend/src/components/Branch/BranchCreateView.vue
index 8357ff7015..9b7460ac88 100644
--- a/frontend/src/components/Branch/BranchCreateView.vue
+++ b/frontend/src/components/Branch/BranchCreateView.vue
@@ -76,20 +76,18 @@
</template>
<script lang="ts" setup>
-import { cloneDeep, uniqueId } from "lodash-es";
+import { uniqueId } from "lodash-es";
import { NButton, NDivider, NInput } from "naive-ui";
import { computed, onMounted, reactive, ref, watch } from "vue";
import { useI18n } from "vue-i18n";
import { useRouter } from "vue-router";
import SchemaEditorV1 from "@/components/SchemaEditorV1/index.vue";
-import { mergeSchemaEditToMetadata } from "@/components/SchemaEditorV1/utils";
import { ProjectSelect } from "@/components/v2";
import {
pushNotification,
useDatabaseV1Store,
useProjectV1Store,
useSchemaEditorV1Store,
- useSheetV1Store,
} from "@/store";
import { useBranchStore } from "@/store/modules/branch";
import {
@@ -99,11 +97,6 @@ import {
import { UNKNOWN_ID } from "@/types";
import { Branch } from "@/types/proto/v1/branch_service";
import { DatabaseMetadata } from "@/types/proto/v1/database_service";
-import {
- Sheet_Source,
- Sheet_Type,
- Sheet_Visibility,
-} from "@/types/proto/v1/sheet_service";
import { projectV1Slug } from "@/utils";
import BaselineSchemaSelector from "./BaselineSchemaSelector.vue";
import { validateBranchName } from "./utils";
@@ -136,7 +129,6 @@ const router = useRouter();
const projectStore = useProjectV1Store();
const databaseStore = useDatabaseV1Store();
const branchStore = useBranchStore();
-const sheetStore = useSheetV1Store();
const state = reactive<LocalState>({
projectId: props.projectId,
loading: false,
@@ -284,37 +276,15 @@ const handleConfirm = async () => {
}
state.isCreating = true;
- const metadata = mergeSchemaEditToMetadata(
- branchSchema.schemaList,
- cloneDeep(
- state.branch.baselineSchemaMetadata || DatabaseMetadata.fromPartial({})
- )
- );
const baselineDatabase = `${database.instanceEntity.name}/${databaseNamePrefix}${state.baselineSchema.databaseId}`;
- // Create a baseline sheet for the schema design.
- const baselineSheet = await sheetStore.createSheet(project.value.name, {
- title: `baseline schema of ${branchTitle.value}`,
- database: baselineDatabase,
- content: new TextEncoder().encode(state.branch.baselineSchema),
- visibility: Sheet_Visibility.VISIBILITY_PROJECT,
- source: Sheet_Source.SOURCE_BYTEBASE_ARTIFACT,
- type: Sheet_Type.TYPE_SQL,
- });
let createdSchemaDesign;
if (!state.parentBranchName) {
createdSchemaDesign = await branchStore.createBranch(
project.value.name,
+ branchTitle.value,
Branch.fromPartial({
- title: branchTitle.value,
- // Keep schema empty in frontend. Backend will generate the design schema.
- schema: "",
- schemaMetadata: metadata,
- baselineSchema: state.branch.baselineSchema,
- baselineSchemaMetadata: state.branch.baselineSchemaMetadata,
- engine: state.branch.engine,
baselineDatabase: baselineDatabase,
- parentBranch: baselineSheet.name,
})
);
} else {
@@ -322,10 +292,11 @@ const handleConfirm = async () => {
state.parentBranchName,
false /* useCache */
);
- createdSchemaDesign = await branchStore.createBranchDraft({
- ...parentBranch,
- title: branchTitle.value,
- });
+ createdSchemaDesign = await branchStore.createBranchDraft(
+ project.value.name,
+ branchTitle.value,
+ parentBranch.name
+ );
}
state.isCreating = false;
pushNotification({
diff --git a/frontend/src/components/Branch/BranchDetailView.vue b/frontend/src/components/Branch/BranchDetailView.vue
index 038aa3f48c..f9a75f70cd 100644
--- a/frontend/src/components/Branch/BranchDetailView.vue
+++ b/frontend/src/components/Branch/BranchDetailView.vue
@@ -129,6 +129,7 @@ import {
} from "@/store";
import { useBranchList, useBranchStore } from "@/store/modules/branch";
import {
+ getProjectName,
getProjectAndBranchId,
projectNamePrefix,
} from "@/store/modules/v1/common";
@@ -158,7 +159,9 @@ const { t } = useI18n();
const router = useRouter();
const databaseStore = useDatabaseV1Store();
const branchStore = useBranchStore();
-const { branchList, ready } = useBranchList();
+const { branchList, ready } = useBranchList(
+ getProjectAndBranchId(props.branch.name)[0]
+);
const { runSQLCheck } = provideSQLCheckContext();
const dialog = useDialog();
const state = reactive<LocalState>({
@@ -383,13 +386,12 @@ const handleSaveBranch = async () => {
if (updateMask.length !== 0) {
if (schemaDesign.value.parentBranch === "") {
const branchName = generateForkedBranchName(schemaDesign.value);
- const newBranch = await branchStore.createBranchDraft({
- ...schemaDesign.value,
- baselineSchema: schemaDesign.value.schema,
- schemaMetadata: mergedMetadata,
- baselineSchemaMetadata: baselineMetadata,
- title: branchName,
- });
+ const projectId = getProjectName(project.value.name);
+ const newBranch = await branchStore.createBranchDraft(
+ projectId,
+ branchName,
+ "" // TODO(d): use parent branch name.
+ );
try {
await branchStore.mergeBranch({
name: schemaDesign.value.name,
diff --git a/frontend/src/components/Branch/BranchSelector.vue b/frontend/src/components/Branch/BranchSelector.vue
index 74d49f3a8f..85ae934ea2 100644
--- a/frontend/src/components/Branch/BranchSelector.vue
+++ b/frontend/src/components/Branch/BranchSelector.vue
@@ -41,7 +41,7 @@ defineEmits<{
(event: "update:branch", name: string | undefined): void;
}>();
-const { branchList: branchList } = useBranchList();
+const { branchList: branchList } = useBranchList(props.project || "");
const projectStore = useProjectV1Store();
const databaseStore = useDatabaseV1Store();
diff --git a/frontend/src/components/Changelist/ChangelistDetail/AddChangePanel/form/BranchForm.vue b/frontend/src/components/Changelist/ChangelistDetail/AddChangePanel/form/BranchForm.vue
index 0487522a39..e29ec423c5 100644
--- a/frontend/src/components/Changelist/ChangelistDetail/AddChangePanel/form/BranchForm.vue
+++ b/frontend/src/components/Changelist/ChangelistDetail/AddChangePanel/form/BranchForm.vue
@@ -53,6 +53,7 @@ import { computed, reactive, watch } from "vue";
import { DatabaseSelect } from "@/components/v2";
import { useDatabaseV1Store, useLocalSheetStore } from "@/store";
import { useBranchList, useBranchStore } from "@/store/modules/branch";
+import { getProjectName } from "@/store/modules/v1/common";
import { UNKNOWN_ID } from "@/types";
import { Branch } from "@/types/proto/v1/branch_service";
import { Changelist_Change as Change } from "@/types/proto/v1/changelist_service";
@@ -79,7 +80,7 @@ const state = reactive<LocalState>({
const { project } = useChangelistDetailContext();
const { changesFromBranch: changes } = useAddChangeContext();
-const { branchList, ready } = useBranchList();
+const { branchList, ready } = useBranchList(getProjectName(project.value.name));
const localSheetStore = useLocalSheetStore();
const database = computed(() => {
diff --git a/frontend/src/components/Project/CreateProjectBranchProtectionRulePanel.vue b/frontend/src/components/Project/CreateProjectBranchProtectionRulePanel.vue
index 881f01c9b0..989081d59e 100644
--- a/frontend/src/components/Project/CreateProjectBranchProtectionRulePanel.vue
+++ b/frontend/src/components/Project/CreateProjectBranchProtectionRulePanel.vue
@@ -91,6 +91,7 @@ import { computed, reactive, watch } from "vue";
import { Drawer, DrawerContent } from "@/components/v2";
import { useRoleStore, useBranchList } from "@/store";
import {
+ getProjectName,
getProjectAndBranchId,
projectNamePrefix,
protectionRulesSuffix,
@@ -122,7 +123,7 @@ const emit = defineEmits<{
(event: "close"): void;
}>();
-const { branchList } = useBranchList();
+const { branchList } = useBranchList(getProjectName(props.project.name));
const protectionRuleStore = useProjectProtectionRulesStore();
const branchProtectionRules = useProjectBranchProtectionRules(
props.project.name
diff --git a/frontend/src/components/ProjectBranchesPanel.vue b/frontend/src/components/ProjectBranchesPanel.vue
index 4a6aa3bbe1..6164e72670 100644
--- a/frontend/src/components/ProjectBranchesPanel.vue
+++ b/frontend/src/components/ProjectBranchesPanel.vue
@@ -44,7 +44,7 @@ interface LocalState {
const router = useRouter();
const projectV1Store = useProjectV1Store();
-const { branchList, ready } = useBranchList();
+const { branchList, ready } = useBranchList(props.projectId);
const state = reactive<LocalState>({
searchKeyword: "",
});
diff --git a/frontend/src/store/modules/branch.ts b/frontend/src/store/modules/branch.ts
index 26f32c8c1e..4bc2a7e1ed 100644
--- a/frontend/src/store/modules/branch.ts
+++ b/frontend/src/store/modules/branch.ts
@@ -6,45 +6,49 @@ import {
Branch,
BranchView,
} from "@/types/proto/v1/branch_service";
-import {
- getProjectAndBranchId,
- projectNamePrefix,
- branchNamePrefix,
-} from "./v1/common";
+import { projectNamePrefix } from "./v1/common";
export const useBranchStore = defineStore("schema_design", () => {
const branchMapByName = reactive(new Map<string, Branch>());
const getBranchRequestCacheByName = new Map<string, Promise<Branch>>();
// Actions
- const fetchBranchList = async (projectName: string = "projects/-") => {
+ const fetchBranchList = async (projectName: string) => {
const { branches } = await branchServiceClient.listBranches({
- parent: projectName,
+ parent: projectNamePrefix + projectName,
view: BranchView.BRANCH_VIEW_BASIC,
});
return branches;
};
- const createBranch = async (projectResourceId: string, branch: Branch) => {
+ const createBranch = async (
+ projectResourceId: string,
+ branchId: string,
+ branch: Branch
+ ) => {
+ console.log("Barny1: ", parent, branchId, branch);
const createdBranch = await branchServiceClient.createBranch({
parent: projectResourceId,
+ branchId: branchId,
branch,
});
- console.debug("baseline schema", branch.baselineSchema);
- console.debug("target metadata", branch.schemaMetadata);
console.debug("got schema", createdBranch.schema);
branchMapByName.set(createdBranch.name, createdBranch);
return createdBranch;
};
- const createBranchDraft = async (branch: Branch) => {
- const [projectName, branchId] = getProjectAndBranchId(branch.name);
- const projectResourceId = `${projectNamePrefix}${projectName}`;
- const parentBranch = `${projectResourceId}/${branchNamePrefix}${branchId}`;
- return createBranch(projectResourceId, {
- ...branch,
- parentBranch: parentBranch,
- });
+ const createBranchDraft = async (
+ projectResourceId: string,
+ branchId: string,
+ parentBranch: string
+ ) => {
+ return createBranch(
+ projectResourceId,
+ branchId,
+ Branch.fromPartial({
+ parentBranch: parentBranch,
+ })
+ );
};
const updateBranch = async (branch: Branch, updateMask: string[]) => {
@@ -118,7 +122,7 @@ export const useBranchStore = defineStore("schema_design", () => {
};
});
-export const useBranchList = (projectName: string | undefined = undefined) => {
+export const useBranchList = (projectName: string) => {
const store = useBranchStore();
const ready = ref(false);
const branchList = ref<Branch[]>([]);