Skip to content

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

feat: sql-editor setting environment (#12281)

Liu Ji923313f6ef9f
diff --git a/frontend/src/components/EnvironmentForm/Form.vue b/frontend/src/components/EnvironmentForm/Form.vue
index 6c1645f663..478dac3ecd 100644
--- a/frontend/src/components/EnvironmentForm/Form.vue
+++ b/frontend/src/components/EnvironmentForm/Form.vue
@@ -55,83 +55,90 @@
</NCheckbox>
</div>
- <div class="flex flex-col gap-y-2">
- <label class="textlabel">
- {{ $t("policy.rollout.name") }}
- </label>
- <span
- v-show="!create && valueChanged('rolloutPolicy')"
- class="textlabeltip !ml-0"
- >{{ $t("policy.rollout.tip") }}</span
- >
- <div class="textinfolabel">
- {{ $t("policy.rollout.info") }}
- <a
- class="inline-flex items-center text-blue-600 ml-1 hover:underline"
- href="https://www.bytebase.com/docs/administration/environment-policy/rollout-policy"
- target="_blank"
- >{{ $t("common.learn-more")
- }}<heroicons-outline:external-link class="w-4 h-4"
- /></a>
+ <template v-if="!simple">
+ <div class="flex flex-col gap-y-2">
+ <label class="textlabel">
+ {{ $t("policy.rollout.name") }}
+ </label>
+ <span
+ v-show="!create && valueChanged('rolloutPolicy')"
+ class="textlabeltip !ml-0"
+ >{{ $t("policy.rollout.tip") }}</span
+ >
+ <div class="textinfolabel">
+ {{ $t("policy.rollout.info") }}
+ <a
+ class="inline-flex items-center text-blue-600 ml-1 hover:underline"
+ href="https://www.bytebase.com/docs/administration/environment-policy/rollout-policy"
+ target="_blank"
+ >{{ $t("common.learn-more")
+ }}<heroicons-outline:external-link class="w-4 h-4"
+ /></a>
+ </div>
+ <RolloutPolicyConfig
+ v-model:policy="state.rolloutPolicy"
+ :disabled="!allowEdit"
+ />
</div>
- <RolloutPolicyConfig
- v-model:policy="state.rolloutPolicy"
- :disabled="!allowEdit"
- />
- </div>
- <div v-if="!create" class="flex flex-col gap-y-2">
- <label class="textlabel">
- {{ $t("sql-review.title") }}
- </label>
- <div>
- <div v-if="sqlReviewPolicy" class="inline-flex items-center gap-x-2">
- <Switch
- v-if="allowEditSQLReviewPolicy"
- :value="sqlReviewPolicy.enforce"
- :text="true"
- @update:value="toggleSQLReviewPolicy"
- />
- <span
- class="textlabel normal-link !text-accent"
- @click="onSQLReviewPolicyClick"
- >{{ sqlReviewPolicy.name }}</span
+ <div v-if="!create" class="flex flex-col gap-y-2">
+ <label class="textlabel">
+ {{ $t("sql-review.title") }}
+ </label>
+ <div>
+ <div
+ v-if="sqlReviewPolicy"
+ class="inline-flex items-center gap-x-2"
+ >
+ <Switch
+ v-if="allowEditSQLReviewPolicy"
+ :value="sqlReviewPolicy.enforce"
+ :text="true"
+ @update:value="toggleSQLReviewPolicy"
+ />
+ <span
+ class="textlabel normal-link !text-accent"
+ @click="onSQLReviewPolicyClick"
+ >{{ sqlReviewPolicy.name }}</span
+ >
+ </div>
+ <NButton
+ v-else-if="hasPermission('bb.policies.update')"
+ @click.prevent="onSQLReviewPolicyClick"
>
+ {{ $t("sql-review.configure-policy") }}
+ </NButton>
+ <span v-else class="textinfolabel">
+ {{ $t("sql-review.no-policy-set") }}
+ </span>
</div>
- <NButton
- v-else-if="hasPermission('bb.policies.update')"
- @click.prevent="onSQLReviewPolicyClick"
- >
- {{ $t("sql-review.configure-policy") }}
- </NButton>
- <span v-else class="textinfolabel">
- {{ $t("sql-review.no-policy-set") }}
- </span>
</div>
- </div>
- <div v-if="!create" class="flex flex-col gap-y-2">
- <label class="textlabel flex items-center">
- {{ $t("environment.access-control.title") }}
- <FeatureBadge feature="bb.feature.access-control" />
- </label>
- <div>
- <div class="inline-flex items-center gap-x-2">
- <Switch
- :value="disableCopyDataPolicy"
- :text="true"
- :disabled="!allowEditDisableCopyData"
- @update:value="upsertPolicy"
- />
- <span class="textlabel">{{
- $t("environment.access-control.disable-copy-data-from-sql-editor")
- }}</span>
+ <div v-if="!create" class="flex flex-col gap-y-2">
+ <label class="textlabel flex items-center">
+ {{ $t("environment.access-control.title") }}
+ <FeatureBadge feature="bb.feature.access-control" />
+ </label>
+ <div>
+ <div class="inline-flex items-center gap-x-2">
+ <Switch
+ :value="disableCopyDataPolicy"
+ :text="true"
+ :disabled="!allowEditDisableCopyData"
+ @update:value="upsertPolicy"
+ />
+ <span class="textlabel">{{
+ $t(
+ "environment.access-control.disable-copy-data-from-sql-editor"
+ )
+ }}</span>
+ </div>
</div>
</div>
- </div>
+ </template>
</div>
- <div v-if="!create" class="mt-6 flex justify-between items-center pt-5">
+ <div v-if="!create && !hideArchiveRestore" class="mt-6 flex justify-between items-center pt-5">
<template v-if="state.environment.state === State.ACTIVE">
<BBButtonConfirm
v-if="allowArchive"
@@ -166,6 +173,7 @@
</template>
<script lang="ts" setup>
+import { NCheckbox } from "naive-ui";
import { Status } from "nice-grpc-common";
import { computed, onMounted } from "vue";
import { useI18n } from "vue-i18n";
@@ -194,7 +202,11 @@ import {
import { extractEnvironmentResourceName, sqlReviewPolicySlug } from "@/utils";
import { getErrorCode } from "@/utils/grpcweb";
import { useEnvironmentFormContext } from "./context";
-import { NCheckbox } from "naive-ui";
+
+defineProps<{
+ simple?: boolean;
+ hideArchiveRestore?: boolean;
+}>();
const { t } = useI18n();
const router = useRouter();
diff --git a/frontend/src/components/EnvironmentForm/context.ts b/frontend/src/components/EnvironmentForm/context.ts
index f8a2ddca2d..5b73c904bd 100644
--- a/frontend/src/components/EnvironmentForm/context.ts
+++ b/frontend/src/components/EnvironmentForm/context.ts
@@ -12,7 +12,7 @@ import type {
} from "@/types/proto/v1/environment_service";
import { PolicyType, type Policy } from "@/types/proto/v1/org_policy_service";
import { hasWorkspacePermissionV2 } from "@/utils";
-import type { ResourceIdField } from "../v2";
+import type { ResourceIdField } from "@/components/v2";
export type LocalState = {
environment: Environment;
diff --git a/frontend/src/views/EnvironmentDashboard.vue b/frontend/src/views/EnvironmentDashboard.vue
index a0cafd0e83..d4f3c14dd1 100644
--- a/frontend/src/views/EnvironmentDashboard.vue
+++ b/frontend/src/views/EnvironmentDashboard.vue
@@ -1,5 +1,5 @@
<template>
- <div class="h-full flex flex-col">
+ <div class="w-full flex flex-col gap-4 py-4 px-2 overflow-y-auto">
<NTabs
type="line"
:bar-width="200"
@@ -72,7 +72,7 @@ import {
Form as EnvironmentFormBody,
Buttons as EnvironmentFormButtons,
} from "@/components/EnvironmentForm";
-import { Drawer } from "@/components/v2";
+import { Drawer, DrawerContent } from "@/components/v2";
import { EnvironmentV1Name, MiniActionButton } from "@/components/v2";
import { useBodyLayoutContext } from "@/layouts/common";
import { ENVIRONMENT_V1_ROUTE_DASHBOARD } from "@/router/dashboard/workspaceRoutes";
diff --git a/frontend/src/views/EnvironmentDetail.vue b/frontend/src/views/EnvironmentDetail.vue
index f9080a4d83..0b846cd5d3 100644
--- a/frontend/src/views/EnvironmentDetail.vue
+++ b/frontend/src/views/EnvironmentDetail.vue
@@ -13,9 +13,15 @@
@restore="doRestore"
@update-policy="updatePolicy"
>
- <EnvironmentFormBody class="w-full px-4 pb-2" />
+ <EnvironmentFormBody
+ :simple="simple"
+ :hide-archive-restore="hideArchiveRestore"
+ class="w-full px-4 pb-2"
+ :class="bodyClass"
+ />
<EnvironmentFormButtons
class="sticky bottom-0 bg-white py-2 px-4 border-t border-block-border"
+ :class="buttonsClass"
/>
</EnvironmentForm>
@@ -32,6 +38,11 @@ import { reactive, watch, watchEffect } from "vue";
import { useI18n } from "vue-i18n";
import { useRouter } from "vue-router";
import ArchiveBanner from "@/components/ArchiveBanner.vue";
+import {
+ EnvironmentForm,
+ Form as EnvironmentFormBody,
+ Buttons as EnvironmentFormButtons,
+} from "@/components/EnvironmentForm";
import { ENVIRONMENT_V1_ROUTE_DETAIL } from "@/router/dashboard/environmentV1";
import { ENVIRONMENT_V1_ROUTE_DASHBOARD } from "@/router/dashboard/workspaceRoutes";
import { hasFeature, pushNotification } from "@/store";
@@ -55,12 +66,7 @@ import {
PolicyType,
PolicyResourceType,
} from "@/types/proto/v1/org_policy_service";
-import { extractEnvironmentResourceName } from "@/utils";
-import {
- EnvironmentForm,
- Form as EnvironmentFormBody,
- Buttons as EnvironmentFormButtons,
-} from "@/components/EnvironmentForm";
+import { extractEnvironmentResourceName, type VueClass } from "@/utils";
interface LocalState {
environment: Environment;
@@ -73,12 +79,13 @@ interface LocalState {
| "bb.feature.environment-tier-policy";
}
-const props = defineProps({
- environmentId: {
- required: true,
- type: String,
- },
-});
+const props = defineProps<{
+ environmentId: string;
+ simple?: boolean;
+ hideArchiveRestore?: boolean;
+ bodyClass?: VueClass;
+ buttonsClass?: VueClass;
+}>();
const emit = defineEmits(["archive"]);
diff --git a/frontend/src/views/sql-editor/Setting/Environment/Environment.vue b/frontend/src/views/sql-editor/Setting/Environment/Environment.vue
index 9c0e4a753d..3948519b76 100644
--- a/frontend/src/views/sql-editor/Setting/Environment/Environment.vue
+++ b/frontend/src/views/sql-editor/Setting/Environment/Environment.vue
@@ -1,3 +1,251 @@
<template>
- <h1>Environment</h1>
-</template>
\ No newline at end of file
+ <div class="w-full flex flex-col gap-4 pt-4 px-2 overflow-hidden relative">
+ <div class="grid grid-cols-3 gap-x-2 gap-y-4 md:inline-flex items-stretch">
+ <NButton @click="handleClickCreateEnvironment">
+ <template #icon>
+ <PlusIcon class="h-4 w-4" />
+ </template>
+ <NEllipsis>
+ {{ $t("environment.create") }}
+ </NEllipsis>
+ </NButton>
+ </div>
+
+ <div class="flex-1 overflow-y-auto">
+ <NTabs
+ type="line"
+ :bar-width="200"
+ :value="state.selectedId"
+ size="large"
+ justify-content="start"
+ tab-style="margin: 0; padding: 0 2.5rem 0.5rem 2rem;"
+ @update:value="onTabChange"
+ >
+ <NTabPane
+ v-for="(item, index) in tabItemList"
+ :key="item.id"
+ :name="item.id"
+ :tab="() => renderTab(item.data, index)"
+ >
+ <EnvironmentDetail
+ :environment-id="item.id"
+ :simple="true"
+ :hide-archive-restore="true"
+ body-class="!px-0"
+ buttons-class="!absolute left-2 right-2"
+ @archive="doArchive"
+ />
+ </NTabPane>
+ </NTabs>
+ </div>
+ </div>
+
+ <Drawer v-model:show="state.showCreateModal">
+ <EnvironmentForm
+ :create="true"
+ :environment="getEnvironmentCreate()"
+ :rollout-policy="DEFAULT_NEW_ROLLOUT_POLICY"
+ :environment-tier="defaultEnvironmentTier"
+ @create="doCreate"
+ @cancel="state.showCreateModal = false"
+ >
+ <DrawerContent :title="$t('environment.create')">
+ <EnvironmentFormBody
+ :simple="true"
+ :hide-archive-restore="true"
+ class="w-[36rem]"
+ />
+ <template #footer>
+ <EnvironmentFormButtons />
+ </template>
+ </DrawerContent>
+ </EnvironmentForm>
+ </Drawer>
+
+ <FeatureModal
+ :open="state.missingRequiredFeature != undefined"
+ :feature="state.missingRequiredFeature"
+ @cancel="state.missingRequiredFeature = undefined"
+ />
+</template>
+
+<script lang="ts" setup>
+import { PlusIcon } from "lucide-vue-next";
+import { NTabs, NTabPane, NButton, NEllipsis } from "naive-ui";
+import { onMounted, computed, reactive, watch, h } from "vue";
+import { useRoute, useRouter } from "vue-router";
+import type { BBTabItem } from "@/bbkit/types";
+import {
+ EnvironmentForm,
+ Form as EnvironmentFormBody,
+ Buttons as EnvironmentFormButtons,
+} from "@/components/EnvironmentForm";
+import { Drawer, DrawerContent } from "@/components/v2";
+import { EnvironmentV1Name } from "@/components/v2";
+import { SQL_EDITOR_SETTING_ENVIRONMENT_MODULE } from "@/router/sqlEditor";
+import {
+ hasFeature,
+ useEnvironmentV1Store,
+ defaultEnvironmentTier,
+ useEnvironmentV1List,
+} from "@/store";
+import {
+ usePolicyV1Store,
+ getEmptyRolloutPolicy,
+} from "@/store/modules/v1/policy";
+import { VirtualRoleType, emptyEnvironment } from "@/types";
+import type {
+ Environment,
+ EnvironmentTier,
+} from "@/types/proto/v1/environment_service";
+import type { Policy } from "@/types/proto/v1/org_policy_service";
+import { PolicyResourceType } from "@/types/proto/v1/org_policy_service";
+import { extractEnvironmentResourceName } from "@/utils";
+import EnvironmentDetail from "@/views/EnvironmentDetail.vue";
+
+const DEFAULT_NEW_ROLLOUT_POLICY: Policy = getEmptyRolloutPolicy(
+ "",
+ PolicyResourceType.ENVIRONMENT
+);
+
+interface LocalState {
+ selectedId: string;
+ showCreateModal: boolean;
+ missingRequiredFeature?:
+ | "bb.feature.approval-policy"
+ | "bb.feature.custom-approval"
+ | "bb.feature.environment-tier-policy";
+}
+
+const environmentV1Store = useEnvironmentV1Store();
+const policyV1Store = usePolicyV1Store();
+const route = useRoute();
+const router = useRouter();
+
+const state = reactive<LocalState>({
+ selectedId: "",
+ showCreateModal: false,
+});
+
+const selectEnvironmentOnHash = () => {
+ if (environmentList.value.length > 0) {
+ if (route.hash) {
+ for (let i = 0; i < environmentList.value.length; i++) {
+ const id = extractEnvironmentResourceName(
+ environmentList.value[i].name
+ );
+ if (id === route.hash.slice(1)) {
+ selectEnvironment(i);
+ break;
+ }
+ }
+ } else {
+ selectEnvironment(0);
+ }
+ }
+};
+
+onMounted(() => {
+ selectEnvironmentOnHash();
+});
+
+watch(
+ () => route.hash,
+ () => {
+ if (route.name == SQL_EDITOR_SETTING_ENVIRONMENT_MODULE) {
+ selectEnvironmentOnHash();
+ }
+ }
+);
+
+const environmentList = useEnvironmentV1List();
+
+const tabItemList = computed((): BBTabItem[] => {
+ if (environmentList.value) {
+ const list = environmentList.value;
+ return list.map((item, index: number): BBTabItem => {
+ const title = `${index + 1}. ${item.title}`;
+ const id = extractEnvironmentResourceName(item.name);
+ return { title, id, data: item };
+ });
+ }
+ return [];
+});
+
+const getEnvironmentCreate = () => {
+ return emptyEnvironment();
+};
+
+const handleClickCreateEnvironment = () => {
+ state.showCreateModal = true;
+};
+
+const doCreate = async (params: {
+ environment: Partial<Environment>;
+ rolloutPolicy: Policy;
+ environmentTier: EnvironmentTier;
+}) => {
+ const { environment, rolloutPolicy, environmentTier } = params;
+ const rp = rolloutPolicy.rolloutPolicy;
+ if (rp?.automatic === false) {
+ if (rp.issueRoles.includes(VirtualRoleType.LAST_APPROVER)) {
+ if (!hasFeature("bb.feature.custom-approval")) {
+ state.missingRequiredFeature = "bb.feature.custom-approval";
+ return;
+ }
+ }
+ if (!hasFeature("bb.feature.approval-policy")) {
+ state.missingRequiredFeature = "bb.feature.approval-policy";
+ return;
+ }
+ }
+
+ const createdEnvironment = await environmentV1Store.createEnvironment({
+ name: environment.name,
+ title: environment.title,
+ order: environmentList.value.length,
+ tier: environmentTier,
+ });
+ await environmentV1Store.fetchEnvironments();
+
+ const requests = [
+ policyV1Store.upsertPolicy({
+ parentPath: createdEnvironment.name,
+ updateMask: ["payload"],
+ policy: rolloutPolicy,
+ }),
+ ];
+ await Promise.all(requests);
+ state.showCreateModal = false;
+ selectEnvironment(createdEnvironment.order);
+};
+
+const doArchive = (/* environment: Environment */) => {
+ if (environmentList.value.length > 0) {
+ selectEnvironment(0);
+ }
+};
+
+const selectEnvironment = (index: number) => {
+ const id = extractEnvironmentResourceName(environmentList.value[index].name);
+ onTabChange(id);
+};
+
+const onTabChange = (id: string) => {
+ state.selectedId = id;
+ router.replace({
+ hash: "#" + id,
+ });
+};
+
+const renderTab = (env: Environment, index: number) => {
+ const child = [
+ h(EnvironmentV1Name, {
+ environment: env,
+ link: false,
+ prefix: `${index + 1}.`,
+ }),
+ ];
+ return h("div", { class: "flex items-center space-x-2" }, child);
+};
+</script>
diff --git a/frontend/src/views/sql-editor/Setting/Sidebar/common.ts b/frontend/src/views/sql-editor/Setting/Sidebar/common.ts
index 752f74c9ee..300317142b 100644
--- a/frontend/src/views/sql-editor/Setting/Sidebar/common.ts
+++ b/frontend/src/views/sql-editor/Setting/Sidebar/common.ts
@@ -1,9 +1,10 @@
-import { LayersIcon } from "lucide-vue-next";
+import { LayersIcon, SquareStackIcon } from "lucide-vue-next";
import { computed, h } from "vue";
import { useI18n } from "vue-i18n";
import { useRoute, type RouteRecordRaw } from "vue-router";
import type { SidebarItem } from "@/components/CommonSidebar.vue";
import sqlEditorRoutes, {
+ SQL_EDITOR_SETTING_ENVIRONMENT_MODULE,
SQL_EDITOR_SETTING_INSTANCE_MODULE,
} from "@/router/sqlEditor";
import { useCurrentUserV1 } from "@/store";
@@ -99,12 +100,12 @@ export const useSidebarItems = () => {
// name: SQL_EDITOR_SETTING_PROJECT_MODULE,
// type: "route",
// },
- // {
- // title: t("common.environments"),
- // icon: h(SquareStackIcon),
- // name: SQL_EDITOR_SETTING_ENVIRONMENT_MODULE,
- // type: "route",
- // },
+ {
+ title: t("common.environments"),
+ icon: h(SquareStackIcon),
+ name: SQL_EDITOR_SETTING_ENVIRONMENT_MODULE,
+ type: "route",
+ },
];
return filterSidebarByPermissions(sidebarList);