Skip to content

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

refactor(frontend): ship several usages to environment v1 (#6073)

Liu Jicbe602cfb488
diff --git a/frontend/src/components/AnomalyTable.vue b/frontend/src/components/AnomalyTable.vue
index 4cc3c386d9..f9f54c4105 100644
--- a/frontend/src/components/AnomalyTable.vue
+++ b/frontend/src/components/AnomalyTable.vue
@@ -88,11 +88,8 @@ import {
AnomalyType,
} from "../types";
import { databaseSlug, humanizeTs, instanceSlug } from "../utils";
-import {
- useDatabaseStore,
- useEnvironmentStore,
- useInstanceStore,
-} from "@/store";
+import { useDatabaseStore, useInstanceStore } from "@/store";
+import { useEnvironmentV1Store } from "@/store";
type Action = {
onClick: () => void;
@@ -171,10 +168,10 @@ export default defineComponent({
case "bb.anomaly.database.backup.policy-violation": {
const payload =
anomaly.payload as AnomalyDatabaseBackupPolicyViolationPayload;
- const environment = useEnvironmentStore().getEnvironmentById(
+ const environment = useEnvironmentV1Store().getEnvironmentByUID(
payload.environmentId
);
- return `'${environment.name}' environment requires ${payload.expectedSchedule} auto-backup.`;
+ return `'${environment.title}' environment requires ${payload.expectedSchedule} auto-backup.`;
}
case "bb.anomaly.database.backup.missing": {
const payload =
diff --git a/frontend/src/components/CustomApproval/Settings/components/common/ExprEditor/components/common.ts b/frontend/src/components/CustomApproval/Settings/components/common/ExprEditor/components/common.ts
index 4c7d15a05d..c8069b0b53 100644
--- a/frontend/src/components/CustomApproval/Settings/components/common/ExprEditor/components/common.ts
+++ b/frontend/src/components/CustomApproval/Settings/components/common/ExprEditor/components/common.ts
@@ -3,22 +3,29 @@ import type { SelectOption } from "naive-ui";
import { ConditionExpr, Factor, SQLTypeList } from "@/plugins/cel";
import { useExprEditorContext } from "../context";
-import { useCurrentUser, useEnvironmentStore, useProjectStore } from "@/store";
+import {
+ useCurrentUser,
+ useEnvironmentV1Store,
+ useProjectStore,
+} from "@/store";
import { engineName, PresetRiskLevelList, SupportedSourceList } from "@/types";
import { Risk_Source, risk_SourceToJSON } from "@/types/proto/v1/risk_service";
import { levelText } from "../../utils";
-import { supportedEngineList } from "@/utils";
+import { extractEnvironmentResourceName, supportedEngineList } from "@/utils";
export const useSelectOptions = (expr: Ref<ConditionExpr>) => {
const context = useExprEditorContext();
const { riskSource } = context;
const getEnvironmentIdOptions = () => {
- const environmentList = useEnvironmentStore().getEnvironmentList();
- return environmentList.map<SelectOption>((env) => ({
- label: env.resourceId,
- value: env.resourceId,
- }));
+ const environmentList = useEnvironmentV1Store().getEnvironmentList();
+ return environmentList.map<SelectOption>((env) => {
+ const environmentId = extractEnvironmentResourceName(env.name);
+ return {
+ label: environmentId,
+ value: environmentId,
+ };
+ });
};
const getProjectIdOptions = () => {
diff --git a/frontend/src/components/DatabaseListSidePanel.vue b/frontend/src/components/DatabaseListSidePanel.vue
index 0c0582b25e..d37c574d15 100644
--- a/frontend/src/components/DatabaseListSidePanel.vue
+++ b/frontend/src/components/DatabaseListSidePanel.vue
@@ -14,9 +14,13 @@ import { useRouter } from "vue-router";
import { useI18n } from "vue-i18n";
import { Action, defineAction, useRegisterActions } from "@bytebase/vue-kbar";
import type { BBOutlineItem } from "@/bbkit/types";
-import { Database, Environment, EnvironmentId, UNKNOWN_ID } from "@/types";
-import { databaseSlug, environmentName, projectSlug } from "@/utils";
-import { useEnvironmentList, useCurrentUser, useDatabaseStore } from "@/store";
+import { Database, EnvironmentId, UNKNOWN_ID } from "@/types";
+import { databaseSlug, environmentV1Name, projectSlug } from "@/utils";
+import {
+ useEnvironmentV1List,
+ useCurrentUser,
+ useDatabaseStore,
+} from "@/store";
export default defineComponent({
name: "DatabaseListSidePanel",
@@ -27,14 +31,16 @@ export default defineComponent({
const currentUser = useCurrentUser();
- const rawEnvironmentList = useEnvironmentList();
+ const rawEnvironmentList = useEnvironmentV1List();
+
+ // Reserve the environment list, put "Prod" to the top.
const environmentList = computed(() =>
cloneDeep(rawEnvironmentList.value).reverse()
);
const prepareList = () => {
// It will also be called when user logout
- if (currentUser.value.id != UNKNOWN_ID) {
+ if (currentUser.value.id !== UNKNOWN_ID) {
databaseStore.fetchDatabaseList();
}
};
@@ -51,7 +57,7 @@ export default defineComponent({
const databaseListByEnvironment = computed(() => {
const envToDbMap: Map<EnvironmentId, BBOutlineItem[]> = new Map();
for (const environment of environmentList.value) {
- envToDbMap.set(environment.id, []);
+ envToDbMap.set(environment.uid, []);
}
const list = [...databaseList.value].filter(
(db) => db.project.tenantMode !== "TENANT"
@@ -60,7 +66,9 @@ export default defineComponent({
return a.name.localeCompare(b.name);
});
for (const database of list) {
- const dbList = envToDbMap.get(database.instance.environment.id)!;
+ const dbList = envToDbMap.get(
+ String(database.instance.environment.id)
+ )!;
// dbList may be undefined if the environment is archived
if (dbList) {
dbList.push({
@@ -72,14 +80,15 @@ export default defineComponent({
}
return environmentList.value
- .filter((environment: Environment) => {
- return envToDbMap.get(environment.id)!.length > 0;
+ .filter((environment) => {
+ const items = envToDbMap.get(environment.uid) ?? [];
+ return items.length > 0;
})
- .map((environment: Environment): BBOutlineItem => {
+ .map((environment): BBOutlineItem => {
return {
- id: `bb.env.${environment.id}`,
- name: environmentName(environment),
- childList: envToDbMap.get(environment.id),
+ id: `bb.env.${environment.uid}`,
+ name: environmentV1Name(environment),
+ childList: envToDbMap.get(environment.uid),
childCollapse: true,
};
});
diff --git a/frontend/src/components/EnvironmentForm.vue b/frontend/src/components/EnvironmentForm.vue
index 6a8f24097c..ad703ee710 100644
--- a/frontend/src/components/EnvironmentForm.vue
+++ b/frontend/src/components/EnvironmentForm.vue
@@ -334,7 +334,7 @@ import { hasWorkspacePermission, sqlReviewPolicySlug } from "@/utils";
import {
pushNotification,
useCurrentUser,
- useEnvironmentList,
+ useEnvironmentV1List,
useSQLReviewStore,
} from "@/store";
import AssigneeGroupEditor from "./EnvironmentForm/AssigneeGroupEditor.vue";
@@ -481,7 +481,7 @@ watch(
const currentUser = useCurrentUser();
-const environmentList = useEnvironmentList();
+const environmentList = useEnvironmentV1List();
const hasPermission = computed(() => {
return hasWorkspacePermission(
diff --git a/frontend/src/components/InstanceTable.vue b/frontend/src/components/InstanceTable.vue
index 770791fde9..491ba05296 100644
--- a/frontend/src/components/InstanceTable.vue
+++ b/frontend/src/components/InstanceTable.vue
@@ -40,9 +40,9 @@
import { PropType, defineComponent, computed } from "vue";
import { useI18n } from "vue-i18n";
import { useRouter } from "vue-router";
-import { urlfy, instanceSlug, environmentName } from "@/utils";
+import { urlfy, instanceSlug, environmentV1Name } from "@/utils";
import { EnvironmentId, Instance } from "@/types";
-import { useEnvironmentStore } from "@/store";
+import { useEnvironmentV1Store } from "@/store";
import InstanceEngineIcon from "./InstanceEngineIcon.vue";
import ProductionEnvironmentIcon from "./Environment/ProductionEnvironmentIcon.vue";
import { BBGridColumn } from "@/bbkit";
@@ -102,8 +102,8 @@ export default defineComponent({
};
const environmentNameFromId = (id: EnvironmentId) => {
- const env = useEnvironmentStore().getEnvironmentById(id);
- return environmentName(env);
+ const env = useEnvironmentV1Store().getEnvironmentByUID(id);
+ return environmentV1Name(env);
};
return {
diff --git a/frontend/src/components/ProvideDashboardContext.vue b/frontend/src/components/ProvideDashboardContext.vue
index cf6d28c1a9..aa3fca8187 100644
--- a/frontend/src/components/ProvideDashboardContext.vue
+++ b/frontend/src/components/ProvideDashboardContext.vue
@@ -5,6 +5,7 @@
<script lang="ts">
import {
useEnvironmentStore,
+ useEnvironmentV1Store,
useMemberStore,
useRoleStore,
usePrincipalStore,
@@ -16,7 +17,6 @@ import {
} from "@/store";
import { defineComponent } from "vue";
import { DEFAULT_PROJECT_ID } from "../types";
-import { useEnvironmentV1Store } from "@/store/modules/v1/environment";
export default defineComponent({
name: "ProvideDashboardContext",
diff --git a/frontend/src/components/SQLReview/SQLReviewCreation.vue b/frontend/src/components/SQLReview/SQLReviewCreation.vue
index 9d80c6d7a9..b8ca307400 100644
--- a/frontend/src/components/SQLReview/SQLReviewCreation.vue
+++ b/frontend/src/components/SQLReview/SQLReviewCreation.vue
@@ -68,11 +68,11 @@ import {
pushNotification,
useSQLReviewStore,
useSubscriptionStore,
+ useEnvironmentV1List,
} from "@/store";
import { hasWorkspacePermission } from "@/utils";
import { rulesToTemplate } from "./components";
import { Environment } from "@/types/proto/v1/environment_service";
-import { useEnvironmentList } from "@/store/modules/v1/environment";
interface LocalState {
currentStep: number;
@@ -157,7 +157,7 @@ const onTemplateApply = (template: SQLReviewPolicyTemplate | undefined) => {
};
const availableEnvironmentList = computed((): Environment[] => {
- const environmentList = useEnvironmentList();
+ const environmentList = useEnvironmentV1List();
const filteredList = store.availableEnvironments(
environmentList.value,
props.policy?.id
diff --git a/frontend/src/components/SQLReview/SQLReviewPolicyTable.vue b/frontend/src/components/SQLReview/SQLReviewPolicyTable.vue
index c5c44e99a7..55f86e995e 100644
--- a/frontend/src/components/SQLReview/SQLReviewPolicyTable.vue
+++ b/frontend/src/components/SQLReview/SQLReviewPolicyTable.vue
@@ -75,7 +75,12 @@ import { useI18n } from "vue-i18n";
import { useRouter } from "vue-router";
import { BBButtonConfirm, BBCheckbox, BBGrid, BBGridColumn } from "@/bbkit";
-import { pushNotification, useCurrentUser, useSQLReviewStore } from "@/store";
+import {
+ pushNotification,
+ useCurrentUser,
+ useSQLReviewStore,
+ useEnvironmentV1List,
+} from "@/store";
import { hasWorkspacePermission, sqlReviewPolicySlug } from "@/utils";
import { SQLReviewPolicy } from "@/types";
import ProductionEnvironmentIcon from "../Environment/ProductionEnvironmentIcon.vue";
@@ -83,7 +88,6 @@ import {
Environment,
environmentTierToJSON,
} from "@/types/proto/v1/environment_service";
-import { useEnvironmentList } from "@/store/modules/v1/environment";
import { environmentTitleV1 } from "@/utils";
type EnvironmentReviewPolicy = {
@@ -132,7 +136,7 @@ const hasPermission = computed(() => {
);
});
-const environmentList = useEnvironmentList();
+const environmentList = useEnvironmentV1List();
const reviewPolicyList = computed(() => sqlReviewStore.reviewPolicyList);
const combinedList = computed(() => {
diff --git a/frontend/src/components/TransferDatabaseForm/TransferMultipleDatabaseForm.vue b/frontend/src/components/TransferDatabaseForm/TransferMultipleDatabaseForm.vue
index c7f13a5e41..e28b312800 100644
--- a/frontend/src/components/TransferDatabaseForm/TransferMultipleDatabaseForm.vue
+++ b/frontend/src/components/TransferDatabaseForm/TransferMultipleDatabaseForm.vue
@@ -6,15 +6,15 @@
class="overflow-y-auto"
style="max-height: calc(100vh - 380px)"
arrow-placement="left"
- :default-expanded-names="environmentList.map((env) => env.id)"
+ :default-expanded-names="environmentList.map((env) => env.uid)"
>
<NCollapseItem
v-for="{
environment,
databaseList: databaseListInEnvironment,
} in databaseListGroupByEnvironment"
- :key="environment.id"
- :name="environment.id"
+ :key="environment.uid"
+ :name="environment.uid"
>
<template #header>
<label class="flex items-center gap-x-2" @click.stop="">
@@ -36,11 +36,8 @@
)
"
/>
- <div>{{ environment.name }}</div>
- <ProductionEnvironmentIcon
- class="w-4 h-4 -ml-1"
- :environment="environment"
- />
+ <div>{{ environment.title }}</div>
+ <ProductionEnvironmentV1Icon :environment="environment" />
</label>
</template>
@@ -72,9 +69,9 @@
type="checkbox"
class="h-4 w-4 text-accent rounded disabled:cursor-not-allowed focus:ring-accent"
:checked="
- isDatabaseSelectedForEnvironment(database.id, environment.id)
+ isDatabaseSelectedForEnvironment(database.id, environment.uid)
"
- @input="(e: any) => toggleDatabaseIdForEnvironment(database.id, environment.id, e.target.checked)"
+ @input="(e: any) => toggleDatabaseIdForEnvironment(database.id, environment.uid, e.target.checked)"
@click.stop=""
/>
<span
@@ -137,9 +134,11 @@ import { computed, PropType, reactive, watch } from "vue";
import { NCollapse, NCollapseItem } from "naive-ui";
import { type BBGridColumn, BBGrid } from "@/bbkit";
-import { Database, DatabaseId, Environment, EnvironmentId } from "@/types";
+import { Database, DatabaseId, EnvironmentId } from "@/types";
import { TransferSource } from "./utils";
-import { useDatabaseStore, useEnvironmentList } from "@/store";
+import { useDatabaseStore, useEnvironmentV1List } from "@/store";
+import { Environment } from "@/types/proto/v1/environment_service";
+import { ProductionEnvironmentV1Icon } from "../v2";
type LocalState = {
selectedDatabaseIdListForEnvironment: Map<EnvironmentId, Set<DatabaseId>>;
@@ -162,7 +161,7 @@ const emit = defineEmits<{
}>();
const databaseStore = useDatabaseStore();
-const environmentList = useEnvironmentList();
+const environmentList = useEnvironmentV1List();
const state = reactive<LocalState>({
selectedDatabaseIdListForEnvironment: new Map(),
@@ -190,7 +189,7 @@ const gridColumnList = computed((): BBGridColumn[] => {
const databaseListGroupByEnvironment = computed(() => {
const listByEnv = environmentList.value.map((environment) => {
const databaseList = props.databaseList.filter(
- (db) => db.instance.environment.id === environment.id
+ (db) => String(db.instance.environment.id) === environment.uid
);
return {
environment,
@@ -246,7 +245,8 @@ const getAllSelectionStateForEnvironment = (
databaseList: Database[]
): { checked: boolean; indeterminate: boolean } => {
const set =
- state.selectedDatabaseIdListForEnvironment.get(environment.id) ?? new Set();
+ state.selectedDatabaseIdListForEnvironment.get(environment.uid) ??
+ new Set();
const checked = databaseList.every((db) => set.has(db.id));
const indeterminate = !checked && databaseList.some((db) => set.has(db.id));
@@ -262,7 +262,7 @@ const toggleAllDatabasesSelectionForEnvironment = (
on: boolean
) => {
databaseList.forEach((db) =>
- toggleDatabaseIdForEnvironment(db.id, environment.id, on)
+ toggleDatabaseIdForEnvironment(db.id, environment.uid, on)
);
};
@@ -271,7 +271,8 @@ const getSelectionStateSummaryForEnvironment = (
databaseList: Database[]
) => {
const set =
- state.selectedDatabaseIdListForEnvironment.get(environment.id) || new Set();
+ state.selectedDatabaseIdListForEnvironment.get(environment.uid) ||
+ new Set();
const selected = databaseList.filter((db) => set.has(db.id)).length;
const total = databaseList.length;
diff --git a/frontend/src/components/v2/Model/EnvironmentV1Name.vue b/frontend/src/components/v2/Model/EnvironmentV1Name.vue
new file mode 100644
index 0000000000..1685833d26
--- /dev/null
+++ b/frontend/src/components/v2/Model/EnvironmentV1Name.vue
@@ -0,0 +1,48 @@
+<template>
+ <component
+ :is="link ? 'router-link' : tag"
+ v-bind="bindings"
+ class="inline-flex items-center gap-x-1"
+ :class="link && 'normal-link'"
+ >
+ <span>{{ environmentV1Name(environment) }}</span>
+ <ProductionEnvironmentV1Icon
+ :environment="environment"
+ class="!text-current"
+ />
+ </component>
+</template>
+
+<script lang="ts" setup>
+import { computed } from "vue";
+
+import { Environment } from "@/types/proto/v1/environment_service";
+import { environmentV1Name, environmentV1Slug } from "@/utils";
+import ProductionEnvironmentV1Icon from "./ProductionEnvironmentV1Icon.vue";
+
+const props = withDefaults(
+ defineProps<{
+ environment: Environment;
+ tag?: string;
+ link?: boolean;
+ }>(),
+ {
+ tag: "span",
+ link: true,
+ }
+);
+
+const bindings = computed(() => {
+ if (props.link) {
+ return {
+ to: `/environment/${environmentV1Slug(props.environment)}`,
+ activeClass: "",
+ exactActiveClass: "",
+ onClick: (e: MouseEvent) => {
+ e.stopPropagation();
+ },
+ };
+ }
+ return {};
+});
+</script>
diff --git a/frontend/src/components/v2/Model/ProductionEnvironmentV1Icon.vue b/frontend/src/components/v2/Model/ProductionEnvironmentV1Icon.vue
new file mode 100644
index 0000000000..48a8d3ec41
--- /dev/null
+++ b/frontend/src/components/v2/Model/ProductionEnvironmentV1Icon.vue
@@ -0,0 +1,64 @@
+<template>
+ <NTooltip
+ v-if="enabled"
+ :disabled="!tooltip"
+ :delay="0"
+ :show-arrow="false"
+ :animated="false"
+ placement="top-start"
+ >
+ <template #trigger>
+ <heroicons-solid:shield-exclamation
+ class="text-control inline-block"
+ :class="tooltip ? 'pointer-events-auto' : 'pointer-events-none'"
+ v-bind="$attrs"
+ />
+ </template>
+
+ <span>{{ $t("environment.production-environment") }}</span>
+ </NTooltip>
+</template>
+
+<script lang="ts">
+import { computed, defineComponent, PropType } from "vue";
+import { featureToRef } from "@/store";
+import {
+ Environment,
+ EnvironmentTier,
+} from "@/types/proto/v1/environment_service";
+
+export default defineComponent({
+ name: "ProductionEnvironmentIcon",
+ inheritAttrs: false,
+ props: {
+ environment: {
+ type: Object as PropType<Environment>,
+ default: undefined,
+ },
+ tier: {
+ type: Number as PropType<EnvironmentTier>,
+ default: EnvironmentTier.UNPROTECTED,
+ },
+ tooltip: {
+ type: Boolean,
+ default: false,
+ },
+ },
+ setup(props) {
+ const hasEnvironmentTierPolicyFeature = featureToRef(
+ "bb.feature.environment-tier-policy"
+ );
+
+ const enabled = computed((): boolean => {
+ if (!hasEnvironmentTierPolicyFeature.value) {
+ return false;
+ }
+ return (
+ (props.environment?.tier ?? props.tier) === EnvironmentTier.PROTECTED
+ );
+ });
+
+ return { enabled };
+ },
+});
+</script>
diff --git a/frontend/src/components/v2/Model/index.ts b/frontend/src/components/v2/Model/index.ts
index a05fa14e09..c0b0357b64 100644
--- a/frontend/src/components/v2/Model/index.ts
+++ b/frontend/src/components/v2/Model/index.ts
@@ -1,6 +1,8 @@
import DatabaseName from "./DatabaseName.vue";
import InstanceName from "./InstanceName.vue";
import EnvironmentName from "./EnvironmentName.vue";
+import EnvironmentV1Name from "./EnvironmentV1Name.vue";
+import ProductionEnvironmentV1Icon from "./ProductionEnvironmentV1Icon.vue";
import ProjectName from "./ProjectName.vue";
import ProjectV1Table from "./ProjectV1Table.vue";
@@ -8,6 +10,8 @@ export {
DatabaseName,
InstanceName,
EnvironmentName,
+ EnvironmentV1Name,
+ ProductionEnvironmentV1Icon,
ProjectName,
ProjectV1Table,
};
diff --git a/frontend/src/store/modules/v1/environment.ts b/frontend/src/store/modules/v1/environment.ts
index da7924b21a..7b3a889ca9 100644
--- a/frontend/src/store/modules/v1/environment.ts
+++ b/frontend/src/store/modules/v1/environment.ts
@@ -106,9 +106,12 @@ export const useEnvironmentV1Store = defineStore("environment_v1", {
return this.environmentMapByName.get(name);
},
getEnvironmentByUID(uid: EnvironmentId) {
+ if (typeof uid === "string") {
+ uid = parseInt(uid, 10);
+ }
return (
- this.environmentList.find((env) => env.uid == uid) ??
- ({} as Environment)
+ this.environmentList.find((env) => parseInt(env.uid, 10) == uid) ??
+ Environment.fromJSON({})
);
},
},
@@ -131,7 +134,7 @@ const getUpdateMaskFromEnvironments = (
return updateMask;
};
-export const useEnvironmentList = (showDeleted = false) => {
+export const useEnvironmentV1List = (showDeleted = false) => {
const store = useEnvironmentV1Store();
return computed(() => store.getEnvironmentList(showDeleted));
};
diff --git a/frontend/src/store/modules/v1/index.ts b/frontend/src/store/modules/v1/index.ts
index 122ea63357..833758251e 100644
--- a/frontend/src/store/modules/v1/index.ts
+++ b/frontend/src/store/modules/v1/index.ts
@@ -1,3 +1,4 @@
+export * from "./environment";
export * from "./project";
export * from "./projectIamPolicy";
export * from "./projectWebhook";
diff --git a/frontend/src/utils/v1/environment.ts b/frontend/src/utils/v1/environment.ts
new file mode 100644
index 0000000000..c5e6ec3888
--- /dev/null
+++ b/frontend/src/utils/v1/environment.ts
@@ -0,0 +1,22 @@
+import slug from "slug";
+
+import { State } from "@/types/proto/v1/common";
+import { Environment } from "@/types/proto/v1/environment_service";
+
+export const extractEnvironmentResourceName = (name: string) => {
+ const pattern = /(?:^|\/)environments\/([^/]+)(?:$|\/)/;
+ const matches = name.match(pattern);
+ return matches?.[1] ?? "";
+};
+
+export function environmentV1Slug(environment: Environment): string {
+ return [slug(environment.title), environment.uid].join("-");
+}
+
+export function environmentV1Name(environment: Environment) {
+ const parts = [environment.title];
+ if (environment.state === State.DELETED) {
+ parts.push("(Archived)");
+ }
+ return parts.join(" ");
+}
diff --git a/frontend/src/utils/v1/index.ts b/frontend/src/utils/v1/index.ts
index 295d3e5bcd..5872e0e72d 100644
--- a/frontend/src/utils/v1/index.ts
+++ b/frontend/src/utils/v1/index.ts
@@ -1,3 +1,4 @@
+export * from "./environment";
export * from "./project";
export * from "./projectWebhook";
export * from "./deploymentConfig";