Skip to content

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

chore: extract database resource form component from requesting exporter (#7456)

boojack685d1a1d0fe6
diff --git a/frontend/src/components/Issue/panel/RequestExportPanel/SelectTableForm/index.vue b/frontend/src/components/Issue/panel/RequestExportPanel/ExportResourceForm/DatabaseResourceSelector.vue
similarity index 90%
rename from frontend/src/components/Issue/panel/RequestExportPanel/SelectTableForm/index.vue
rename to frontend/src/components/Issue/panel/RequestExportPanel/ExportResourceForm/DatabaseResourceSelector.vue
index e4aba91603..f3ac716ac9 100644
--- a/frontend/src/components/Issue/panel/RequestExportPanel/SelectTableForm/index.vue
+++ b/frontend/src/components/Issue/panel/RequestExportPanel/ExportResourceForm/DatabaseResourceSelector.vue
@@ -38,6 +38,7 @@ import Label from "./Label.vue";
const props = defineProps<{
projectId: string;
databaseId: string;
+ databaseResources: DatabaseResource[];
}>();
const emit = defineEmits<{
@@ -46,7 +47,20 @@ const emit = defineEmits<{
const databaseStore = useDatabaseV1Store();
const dbSchemaStore = useDBSchemaV1Store();
-const selectedValueList = ref<string[]>([]);
+const selectedValueList = ref<string[]>(
+ props.databaseResources.map((databaseResource) => {
+ const database = databaseStore.getDatabaseByName(
+ databaseResource.databaseName
+ );
+ if (databaseResource.table !== undefined) {
+ return `t-${database.uid}-${databaseResource.schema}-${databaseResource.table}`;
+ } else if (databaseResource.schema !== undefined) {
+ return `s-${database.uid}-${databaseResource.schema}`;
+ } else {
+ return `d-${database.uid}`;
+ }
+ })
+);
const databaseResourceMap = ref<Map<string, DatabaseResource>>(new Map());
const loading = ref(false);
diff --git a/frontend/src/components/Issue/panel/RequestExportPanel/SelectTableForm/Label.vue b/frontend/src/components/Issue/panel/RequestExportPanel/ExportResourceForm/Label.vue
similarity index 100%
rename from frontend/src/components/Issue/panel/RequestExportPanel/SelectTableForm/Label.vue
rename to frontend/src/components/Issue/panel/RequestExportPanel/ExportResourceForm/Label.vue
diff --git a/frontend/src/components/Issue/panel/RequestExportPanel/SelectTableForm/common.ts b/frontend/src/components/Issue/panel/RequestExportPanel/ExportResourceForm/common.ts
similarity index 100%
rename from frontend/src/components/Issue/panel/RequestExportPanel/SelectTableForm/common.ts
rename to frontend/src/components/Issue/panel/RequestExportPanel/ExportResourceForm/common.ts
diff --git a/frontend/src/components/Issue/panel/RequestExportPanel/ExportResourceForm/index.vue b/frontend/src/components/Issue/panel/RequestExportPanel/ExportResourceForm/index.vue
new file mode 100644
index 0000000000..10550b4b02
--- /dev/null
+++ b/frontend/src/components/Issue/panel/RequestExportPanel/ExportResourceForm/index.vue
@@ -0,0 +1,159 @@
+<template>
+ <div class="w-full mb-2">
+ <NRadioGroup
+ v-model:value="state.exportMethod"
+ class="w-full !flex flex-row justify-start items-center gap-4"
+ >
+ <NRadio :value="'SQL'" label="SQL" />
+ <NTooltip :disabled="allowSelectTableResource">
+ <template #trigger>
+ <NRadio
+ :disabled="!allowSelectTableResource"
+ :value="'DATABASE'"
+ :label="$t('common.database')"
+ />
+ </template>
+ {{ $t("issue.grant-request.please-select-database-first") }}
+ </NTooltip>
+ </NRadioGroup>
+ </div>
+ <div
+ v-show="state.exportMethod === 'SQL'"
+ class="w-full h-[300px] border rounded"
+ >
+ <MonacoEditor
+ class="w-full h-full py-2"
+ :value="state.statement"
+ :auto-focus="false"
+ :language="'sql'"
+ :dialect="dialect"
+ @change="handleStatementChange"
+ />
+ </div>
+ <div
+ v-if="state.exportMethod === 'DATABASE'"
+ class="w-full flex flex-row justify-start items-center"
+ >
+ <DatabaseResourceSelector
+ :project-id="project!.uid"
+ :database-id="(props.databaseId as string)"
+ :database-resources="state.databaseResources"
+ @update="handleTableResourceUpdate"
+ />
+ </div>
+</template>
+
+<script lang="ts" setup>
+import { computed, reactive, watch, watchEffect } from "vue";
+import { useDatabaseV1Store, useProjectV1Store } from "@/store";
+import {
+ DatabaseResource,
+ SQLDialect,
+ UNKNOWN_ID,
+ dialectOfEngineV1,
+} from "@/types";
+import { Engine } from "@/types/proto/v1/common";
+import { stringifyDatabaseResources } from "@/utils/issue/cel";
+import DatabaseResourceSelector from "./DatabaseResourceSelector.vue";
+
+const props = defineProps<{
+ projectId?: string;
+ databaseId?: string;
+ statement?: string;
+ databaseResources?: DatabaseResource[];
+}>();
+
+const emit = defineEmits<{
+ (event: "update:condition", value?: string): void;
+ (
+ event: "update:database-resources",
+ databaseResources: DatabaseResource[]
+ ): void;
+}>();
+
+interface LocalState {
+ exportMethod: "SQL" | "DATABASE";
+ statement: string;
+ databaseResources: DatabaseResource[];
+}
+
+const projectStore = useProjectV1Store();
+const databaseStore = useDatabaseV1Store();
+const state = reactive<LocalState>({
+ exportMethod: "SQL",
+ statement: props.statement || "",
+ databaseResources: props.databaseResources || [],
+});
+
+const project = computed(() => {
+ return props.projectId
+ ? projectStore.getProjectByUID(props.projectId)
+ : undefined;
+});
+
+const selectedDatabase = computed(() => {
+ if (!props.databaseId || props.databaseId === String(UNKNOWN_ID)) {
+ return undefined;
+ }
+ return databaseStore.getDatabaseByUID(props.databaseId);
+});
+
+const allowSelectTableResource = computed(() => {
+ return props.databaseId !== undefined;
+});
+
+const dialect = computed((): SQLDialect => {
+ const db = selectedDatabase.value;
+ return dialectOfEngineV1(db?.instanceEntity.engine ?? Engine.MYSQL);
+});
+
+// Prepare project entity.
+watchEffect(async () => {
+ if (!props.projectId) {
+ return;
+ }
+ await projectStore.getOrFetchProjectByUID(props.projectId);
+});
+
+watch(
+ () => [state.exportMethod, state.statement, state.databaseResources],
+ () => {
+ if (state.exportMethod === "SQL") {
+ if (state.statement === "") {
+ emit("update:condition", undefined);
+ } else {
+ emit(
+ "update:condition",
+ `request.statement == "${btoa(
+ unescape(encodeURIComponent(state.statement))
+ )}"`
+ );
+ }
+ } else {
+ if (state.databaseResources.length === 0) {
+ emit("update:condition", undefined);
+ } else {
+ const condition = stringifyDatabaseResources(state.databaseResources);
+ emit("update:condition", condition);
+ }
+ emit("update:database-resources", state.databaseResources);
+ }
+ }
+);
+
+const handleStatementChange = (value: string) => {
+ state.statement = value;
+};
+
+const handleTableResourceUpdate = (
+ databaseResourceList: DatabaseResource[]
+) => {
+ if (databaseResourceList.length > 1) {
+ throw new Error("Only one table can be selected");
+ } else if (databaseResourceList.length === 0) {
+ state.databaseResources = [];
+ } else {
+ state.databaseResources = databaseResourceList;
+ }
+};
+</script>
diff --git a/frontend/src/components/Issue/panel/RequestExportPanel/index.vue b/frontend/src/components/Issue/panel/RequestExportPanel/index.vue
index f2ccacb27f..5060de2b3f 100644
--- a/frontend/src/components/Issue/panel/RequestExportPanel/index.vue
+++ b/frontend/src/components/Issue/panel/RequestExportPanel/index.vue
@@ -63,51 +63,13 @@
{{ $t("issue.grant-request.export-method") }}
<RequiredStar />
</span>
- <div class="w-full mb-2">
- <NRadioGroup
- v-model:value="state.exportMethod"
- class="w-full !flex flex-row justify-start items-center gap-4"
- name="export-method"
- >
- <NRadio :value="'SQL'" label="SQL" />
- <NTooltip :disabled="allowSelectTableResource">
- <template #trigger>
- <NRadio
- :disabled="!allowSelectTableResource"
- :value="'DATABASE'"
- :label="$t('common.database')"
- />
- </template>
- {{ $t("issue.grant-request.please-select-database-first") }}
- </NTooltip>
- </NRadioGroup>
- </div>
- <div
- v-show="state.exportMethod === 'SQL'"
- class="w-full h-[300px] border rounded"
- >
- <MonacoEditor
- class="w-full h-full py-2"
- :value="state.statement"
- :auto-focus="false"
- :language="'sql'"
- :dialect="dialect"
- @change="handleStatementChange"
- />
- </div>
- <div
- v-if="state.exportMethod === 'DATABASE'"
- class="w-full flex flex-row justify-start items-center"
- >
- <SelectTableForm
- :project-id="(state.projectId as string)"
- :database-id="state.databaseId as string"
- :selected-database-resource-list="
- selectedTableResource ? [selectedTableResource] : []
- "
- @update="handleTableResourceUpdate"
- />
- </div>
+ <ExportResourceForm
+ :project-id="state.projectId"
+ :database-id="state.databaseId"
+ :statement="statement"
+ @update:condition="state.databaseResourceCondition = $event"
+ @update:database-resources="state.databaseResources = $event"
+ />
</div>
<div class="w-full flex flex-col justify-start items-start">
<span class="flex items-center textlabel mb-2">
@@ -180,24 +142,16 @@
</template>
<script lang="ts" setup>
-import {
- NDrawer,
- NDrawerContent,
- NRadioGroup,
- NRadio,
- NInput,
- NTooltip,
-} from "naive-ui";
-import { computed, onMounted, reactive, ref } from "vue";
+import dayjs from "dayjs";
+import { NDrawer, NDrawerContent, NRadioGroup, NRadio, NInput } from "naive-ui";
+import { computed, onMounted, reactive } from "vue";
import { useI18n } from "vue-i18n";
import {
DatabaseResource,
IssueCreate,
PresetRoleType,
- SQLDialect,
SYSTEM_BOT_ID,
UNKNOWN_ID,
- dialectOfEngineV1,
} from "@/types";
import {
extractUserUID,
@@ -211,15 +165,12 @@ import {
useIssueStore,
useProjectV1Store,
} from "@/store";
-import MonacoEditor from "@/components/MonacoEditor";
import RequiredStar from "@/components/RequiredStar.vue";
import { InstanceV1EngineIcon } from "@/components/v2";
import DatabaseSelect from "@/components/DatabaseSelect.vue";
-import { Engine } from "@/types/proto/v1/common";
-import { head } from "lodash-es";
+import { head, isUndefined } from "lodash-es";
import { useRouter } from "vue-router";
-import SelectTableForm from "./SelectTableForm/index.vue";
-import dayjs from "dayjs";
+import ExportResourceForm from "./ExportResourceForm/index.vue";
import { stringifyDatabaseResources } from "@/utils/issue/cel";
import ExpirationSelector from "@/components/ExpirationSelector.vue";
@@ -227,9 +178,10 @@ interface LocalState {
projectId?: string;
environmentId?: string;
databaseId?: string;
+ databaseResourceCondition?: string;
+ databaseResources: DatabaseResource[];
expireDays: number;
maxRowCount: number;
- exportMethod: "SQL" | "DATABASE";
exportFormat: "CSV" | "JSON" | "SQL" | "XLSX";
statement: string;
description: string;
@@ -249,14 +201,13 @@ const router = useRouter();
const currentUser = useCurrentUserV1();
const databaseStore = useDatabaseV1Store();
const state = reactive<LocalState>({
+ databaseResources: [],
expireDays: 1,
maxRowCount: 1000,
- exportMethod: "SQL",
exportFormat: "CSV",
statement: "",
description: "",
});
-const selectedTableResource = ref<DatabaseResource>();
const selectedDatabase = computed(() => {
if (!state.databaseId || state.databaseId === String(UNKNOWN_ID)) {
@@ -288,21 +239,10 @@ const allowCreate = computed(() => {
if (!state.databaseId) {
return false;
}
-
- if (state.exportMethod === "SQL") {
- return state.statement && state.statement !== "";
- } else {
- return selectedTableResource.value !== undefined;
+ if (isUndefined(state.databaseResourceCondition)) {
+ return false;
}
-});
-
-const allowSelectTableResource = computed(() => {
- return state.databaseId !== undefined;
-});
-
-const dialect = computed((): SQLDialect => {
- const db = selectedDatabase.value;
- return dialectOfEngineV1(db?.instanceEntity.engine ?? Engine.MYSQL);
+ return true;
});
onMounted(async () => {
@@ -344,22 +284,6 @@ const handleDatabaseSelect = (databaseId: string) => {
}
};
-const handleTableResourceUpdate = (
- databaseResourceList: DatabaseResource[]
-) => {
- if (databaseResourceList.length > 1) {
- throw new Error("Only one table can be selected");
- } else if (databaseResourceList.length === 0) {
- selectedTableResource.value = undefined;
- } else {
- selectedTableResource.value = databaseResourceList[0];
- }
-};
-
-const handleStatementChange = (value: string) => {
- state.statement = value;
-};
-
const doCreateIssue = async () => {
if (!allowCreate.value) {
return;
@@ -398,24 +322,17 @@ const doCreateIssue = async () => {
);
expression.push(`request.export_format == "${state.exportFormat}"`);
expression.push(`request.row_limit == ${state.maxRowCount}`);
- if (state.exportMethod === "SQL") {
- expression.push(
- `request.statement == "${btoa(
- unescape(encodeURIComponent(state.statement))
- )}"`
- );
- const cel = stringifyDatabaseResources([
+ if (state.databaseResourceCondition) {
+ expression.push(state.databaseResourceCondition);
+ }
+ // If the export statement is not empty, add the selected database to the condition.
+ if (state.databaseResources.length === 0) {
+ const condition = stringifyDatabaseResources([
{
databaseName: selectedDatabase.value!.name,
},
]);
- expression.push(cel);
- } else {
- if (!selectedTableResource.value) {
- throw new Error("No table selected");
- }
- const cel = stringifyDatabaseResources([selectedTableResource.value]);
- expression.push(cel);
+ expression.push(condition);
}
const celExpressionString = expression.join(" && ");
@@ -442,10 +359,10 @@ const generateIssueName = () => {
throw new Error("No database selected");
}
- if (state.exportMethod === "SQL") {
+ if (state.databaseResources.length === 0) {
return `Request data export for "${database.databaseName} (${database.instanceEntity.title})"`;
} else {
- const tableResource = selectedTableResource.value as DatabaseResource;
+ const tableResource = state.databaseResources[0] as DatabaseResource;
const nameList = [database.databaseName];
if (tableResource.schema) {
nameList.push(tableResource.schema);
diff --git a/frontend/src/components/Issue/panel/RequestQueryPanel/DatabaseResourceForm/DatabaseResourceSelector.vue b/frontend/src/components/Issue/panel/RequestQueryPanel/DatabaseResourceForm/DatabaseResourceSelector.vue
index b4172b7379..2f86569ca1 100644
--- a/frontend/src/components/Issue/panel/RequestQueryPanel/DatabaseResourceForm/DatabaseResourceSelector.vue
+++ b/frontend/src/components/Issue/panel/RequestQueryPanel/DatabaseResourceForm/DatabaseResourceSelector.vue
@@ -2,7 +2,6 @@
<div class="w-[60rem] space-y-2">
<NTransfer
v-if="!loading"
- ref="transfer"
v-model:value="selectedValueList"
style="height: calc(100vh - 380px)"
:options="sourceTransferOptions"
@@ -39,7 +38,7 @@ import Label from "./Label.vue";
const props = defineProps<{
projectId: string;
databaseId?: string;
- selectedDatabaseResourceList: DatabaseResource[];
+ databaseResources: DatabaseResource[];
}>();
const emit = defineEmits<{
@@ -50,7 +49,7 @@ const databaseStore = useDatabaseV1Store();
const dbSchemaStore = useDBSchemaV1Store();
const selectedValueList = ref<string[]>(
- props.selectedDatabaseResourceList.map((databaseResource) => {
+ props.databaseResources.map((databaseResource) => {
const database = databaseStore.getDatabaseByName(
databaseResource.databaseName
);
diff --git a/frontend/src/components/Issue/panel/RequestQueryPanel/DatabaseResourceForm/index.vue b/frontend/src/components/Issue/panel/RequestQueryPanel/DatabaseResourceForm/index.vue
index f4c1cca0f8..8323516de7 100644
--- a/frontend/src/components/Issue/panel/RequestQueryPanel/DatabaseResourceForm/index.vue
+++ b/frontend/src/components/Issue/panel/RequestQueryPanel/DatabaseResourceForm/index.vue
@@ -28,7 +28,7 @@
<DatabaseResourceSelector
v-if="project"
:project-id="project.uid"
- :selected-database-resource-list="state.selectedDatabaseResourceList"
+ :database-resources="state.databaseResources"
@update="handleSelectedDatabaseResourceChanged"
/>
</div>
@@ -43,7 +43,7 @@ import DatabaseResourceSelector from "./DatabaseResourceSelector.vue";
interface LocalState {
allDatabases: boolean;
- selectedDatabaseResourceList: DatabaseResource[];
+ databaseResources: DatabaseResource[];
}
const props = defineProps<{
@@ -62,7 +62,7 @@ const emit = defineEmits<{
const projectStore = useProjectV1Store();
const state = reactive<LocalState>({
allDatabases: (props.databaseResources || []).length === 0,
- selectedDatabaseResourceList: props.databaseResources || [],
+ databaseResources: props.databaseResources || [],
});
const project = computed(() => {
@@ -82,24 +82,22 @@ watchEffect(async () => {
const handleSelectedDatabaseResourceChanged = (
databaseResourceList: DatabaseResource[]
) => {
- state.selectedDatabaseResourceList = databaseResourceList;
+ state.databaseResources = databaseResourceList;
};
watch(
- () => [state.allDatabases, state.selectedDatabaseResourceList],
+ () => [state.allDatabases, state.databaseResources],
() => {
if (state.allDatabases) {
emit("update:condition", "");
} else {
- if (state.selectedDatabaseResourceList.length === 0) {
+ if (state.databaseResources.length === 0) {
emit("update:condition", undefined);
} else {
- const condition = stringifyDatabaseResources(
- state.selectedDatabaseResourceList
- );
+ const condition = stringifyDatabaseResources(state.databaseResources);
emit("update:condition", condition);
}
- emit("update:database-resources", state.selectedDatabaseResourceList);
+ emit("update:database-resources", state.databaseResources);
}
}
);