Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
chore: add role selector for postgres (#14337)
boojack71437203bbee
diff --git a/frontend/src/components/IssueV1/components/StatementSection/EditorView/EditorActionPopover.vue b/frontend/src/components/IssueV1/components/StatementSection/EditorView/EditorActionPopover.vuenew file mode 100644index 0000000000..2fcae5707b--- /dev/null+++ b/frontend/src/components/IssueV1/components/StatementSection/EditorView/EditorActionPopover.vue@@ -0,0 +1,58 @@+<template>+ <NPopover trigger="hover" placement="bottom">+ <template #trigger>+ <NButton size="tiny">+ <template #icon>+ <EllipsisVerticalIcon />+ </template>+ </NButton>+ </template>++ <div class="flex flex-col items-start justify-start">+ <template v-if="shouldShowInstaneRoleSelect">+ <InstanceRoleSelect />+ <NDivider class="!my-2" />+ </template>+ <FormatOnSaveCheckbox v-model:value="formatOnSave" :language="language" />+ </div>+ </NPopover>+</template>++<script setup lang="tsx">+import { EllipsisVerticalIcon } from "lucide-vue-next";+import { NPopover, NButton, NDivider } from "naive-ui";+import { computed } from "vue";+import { databaseForTask, useIssueContext } from "@/components/IssueV1/logic";+import { Engine } from "@/types/proto/v1/common";+import { Task_Type } from "@/types/proto/v1/rollout_service";+import { useInstanceV1EditorLanguage } from "@/utils";+import FormatOnSaveCheckbox from "./FormatOnSaveCheckbox.vue";+import InstanceRoleSelect from "./InstanceRoleSelect.vue";++const { formatOnSave, issue, selectedTask } = useIssueContext();++const database = computed(() => {+ return databaseForTask(issue.value, selectedTask.value);+});++const language = useInstanceV1EditorLanguage(+ computed(() => database.value.instanceResource)+);++const shouldShowInstaneRoleSelect = computed(() => {+ // Only works for postgres.+ if (![Engine.POSTGRES].includes(database.value.instanceResource.engine)) {+ return false;+ }+ // Only works for DDL/DML, exclude creating database and schema baseline.+ if (+ ![+ Task_Type.DATABASE_SCHEMA_UPDATE,+ Task_Type.DATABASE_DATA_UPDATE,+ ].includes(selectedTask.value.type)+ ) {+ return false;+ }+ return true;+});+</script>diff --git a/frontend/src/components/IssueV1/components/StatementSection/EditorView/EditorView.vue b/frontend/src/components/IssueV1/components/StatementSection/EditorView/EditorView.vueindex 45cf8f4fa7..5302ec8a96 100644--- a/frontend/src/components/IssueV1/components/StatementSection/EditorView/EditorView.vue+++ b/frontend/src/components/IssueV1/components/StatementSection/EditorView/EditorView.vue@@ -25,10 +25,7 @@><template v-if="isCreating"><template v-if="allowEditStatementWhenCreating">- <FormatOnSaveCheckbox- v-model:value="formatOnSave"- :language="language"- />+ <EditorActionPopover /><SQLUploadButtonsize="tiny":loading="state.isUploadingFile"@@ -71,10 +68,7 @@</template></template><template v-else>- <FormatOnSaveCheckbox- v-model:value="formatOnSave"- :language="language"- />+ <EditorActionPopover /><SQLUploadButtonsize="tiny":loading="state.isUploadingFile"@@ -258,7 +252,8 @@ import {sheetNameOfTaskV1,} from "@/utils";import { useSQLAdviceMarkers } from "../useSQLAdviceMarkers";-import FormatOnSaveCheckbox from "./FormatOnSaveCheckbox.vue";+import EditorActionPopover from "./EditorActionPopover.vue";+import { provideEditorContext } from "./context";import type { EditState } from "./useTempEditState";import { useTempEditState } from "./useTempEditState";@@ -275,14 +270,8 @@ const props = defineProps<{const { t } = useI18n();const route = useRoute();const context = useIssueContext();-const {- events,- isCreating,- issue,- selectedTask,- formatOnSave,- getPlanCheckRunsForTask,-} = context;+const { events, isCreating, issue, selectedTask, getPlanCheckRunsForTask } =+ context;const project = computed(() => issue.value.projectEntity);const dialog = useDialog();const editorContainerElRef = ref<HTMLElement>();@@ -724,6 +713,11 @@ watch(isCreating, (curr, prev) => {}});+provideEditorContext({+ statement: computed(() => state.statement),+ setStatement: handleStatementChange,+});+defineExpose({get editor() {return monacoEditorRef.value;diff --git a/frontend/src/components/IssueV1/components/StatementSection/EditorView/InstanceRoleSelect.vue b/frontend/src/components/IssueV1/components/StatementSection/EditorView/InstanceRoleSelect.vuenew file mode 100644index 0000000000..44bffd45be--- /dev/null+++ b/frontend/src/components/IssueV1/components/StatementSection/EditorView/InstanceRoleSelect.vue@@ -0,0 +1,97 @@+<template>+ <div class="w-full flex flex-row justify-start items-center gap-2">+ <span class="shrink-0">{{ $t("common.role.self") }}</span>+ <NSelect+ v-model:value="state.selectedRole"+ class="!w-40 grow"+ consistent-menu-width+ size="small"+ :options="options"+ :placeholder="$t('instance.select-database-user')"+ :filterable="true"+ :virtual-scroll="true"+ :fallback-option="false"+ />+ </div>+</template>++<script setup lang="tsx">+import { NSelect, type SelectOption } from "naive-ui";+import { computed, reactive, watch } from "vue";+import { databaseForTask, useIssueContext } from "@/components/IssueV1/logic";+import { useEditorContext } from "./context";++/**+ * Regular expression to match and capture the role name in a specific comment format.+ * The expected format is:+ * /* === Bytebase Role Setter. DO NOT EDIT. === *\/+ * SET ROLE <role_name>;+ *+ * The regex captures the role name (\w+) following the "SET ROLE" statement.+ */+const ROLE_SETTER_REGEX =+ /\/\*\s*=== Bytebase Role Setter\. DO NOT EDIT\. === \*\/\s*SET ROLE (\w+);/;++interface LocalState {+ selectedRole?: string;+}++const editorContext = useEditorContext();++const { issue, selectedTask } = useIssueContext();+const state = reactive<LocalState>({});++const database = computed(() => {+ return databaseForTask(issue.value, selectedTask.value);+});++const instanceRoles = computed(() => database.value.instanceResource.roles);++const options = computed(() => {+ return instanceRoles.value.map<SelectOption>((instanceRole) => {+ return {+ value: instanceRole.roleName,+ label: instanceRole.roleName,+ };+ });+});++watch(+ () => selectedTask.value.name,+ async () => {+ // Initialize selected role from statement using regex.+ const match = editorContext.statement.value.match(ROLE_SETTER_REGEX);+ if (match) {+ state.selectedRole = match[1];+ } else {+ state.selectedRole = undefined;+ }+ },+ {+ immediate: true,+ }+);++watch(+ () => state.selectedRole,+ async () => {+ if (state.selectedRole) {+ setRoleInTaskStatement(state.selectedRole);+ }+ }+);++const setRoleInTaskStatement = (roleName: string) => {+ const roleSetterTemplate = `/* === Bytebase Role Setter. DO NOT EDIT. === */\nSET ROLE ${roleName};`;+ let statement = "";+ if (ROLE_SETTER_REGEX.test(editorContext.statement.value)) {+ statement = editorContext.statement.value.replace(+ ROLE_SETTER_REGEX,+ roleSetterTemplate+ );+ } else {+ statement = roleSetterTemplate + "\n" + editorContext.statement.value;+ }+ editorContext.setStatement(statement);+};+</script>diff --git a/frontend/src/components/IssueV1/components/StatementSection/EditorView/context.ts b/frontend/src/components/IssueV1/components/StatementSection/EditorView/context.tsnew file mode 100644index 0000000000..62b477d529--- /dev/null+++ b/frontend/src/components/IssueV1/components/StatementSection/EditorView/context.ts@@ -0,0 +1,20 @@+import type { InjectionKey, Ref } from "vue";+import { inject, provide } from "vue";++export type EditorContext = {+ statement: Ref<string>;+ setStatement: (statement: string) => void;+};++export const KEY = Symbol(+ "bb.issue.context.editor-view"+) as InjectionKey<EditorContext>;++export const useEditorContext = () => {+ return inject(KEY)!;+};++export const provideEditorContext = (context: EditorContext) => {+ provide(KEY, context);+ return context;+};