Skip to content

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

chore: extract readonlyDataSourceList (#7382)

Liu Ji9a5ec949ba5b
diff --git a/frontend/src/components/InstanceForm/DataSourceSection/DataSourceTabs.vue b/frontend/src/components/InstanceForm/DataSourceSection/DataSourceTabs.vue
index 0cba60855a..a446349004 100644
--- a/frontend/src/components/InstanceForm/DataSourceSection/DataSourceTabs.vue
+++ b/frontend/src/components/InstanceForm/DataSourceSection/DataSourceTabs.vue
@@ -10,7 +10,7 @@
<NTab :name="adminDataSource.id">
{{ $t("common.admin") }}
</NTab>
- <NTab v-for="ds in readOnlyDataSourceList" :key="ds.id" :name="ds.id">
+ <NTab v-for="ds in readonlyDataSourceList" :key="ds.id" :name="ds.id">
<span>{{ $t("common.read-only") }}</span>
<BBButtonConfirm
v-if="hasReadOnlyDataSource"
@@ -46,10 +46,9 @@
</template>
<script setup lang="ts">
-import { computed, nextTick, ref, watch } from "vue";
+import { nextTick, ref, watch } from "vue";
import { NTabs, NTab, TabsInst, NButton } from "naive-ui";
-import { DataSourceType } from "@/types/proto/v1/instance_service";
import { BBButtonConfirm } from "@/bbkit";
import { useInstanceFormContext } from "../context";
import { EditDataSource } from "../common";
@@ -67,15 +66,10 @@ const {
allowEdit,
dataSourceEditState,
adminDataSource,
+ readonlyDataSourceList,
hasReadOnlyDataSource,
} = useInstanceFormContext();
-const readOnlyDataSourceList = computed(() => {
- return dataSourceEditState.value.dataSources.filter(
- (ds) => ds.type === DataSourceType.READ_ONLY
- );
-});
-
const handleDeleteDataSource = async (ds: EditDataSource) => {
const removeLocalEditDataSource = (ds: EditDataSource) => {
const { dataSources, editingDataSourceId } = dataSourceEditState.value;
@@ -97,22 +91,6 @@ const handleDeleteDataSource = async (ds: EditDataSource) => {
}
removeLocalEditDataSource(ds);
-
- // if (!readonlyDataSource.value) {
- // return;
- // }
- // if (readonlyDataSource.value.pendingCreate) {
- // // TODO: state.currentDataSourceType = DataSourceType.ADMIN;
- // readonlyDataSource.value = undefined;
- // } else {
- // const { instance } = props;
- // if (!instance) return;
- // const ds = getDataSourceByType(instance, DataSourceType.READ_ONLY);
- // if (!ds) return;
- // const updated = await instanceV1Store.deleteDataSource(instance, ds);
- // // TODO: state.currentDataSourceType = DataSourceType.ADMIN;
- // await updateEditState(updated);
- // }
};
watch(
diff --git a/frontend/src/components/InstanceForm/InstanceForm.vue b/frontend/src/components/InstanceForm/InstanceForm.vue
index a666f39159..30e7797182 100644
--- a/frontend/src/components/InstanceForm/InstanceForm.vue
+++ b/frontend/src/components/InstanceForm/InstanceForm.vue
@@ -478,6 +478,7 @@ const {
dataSourceEditState,
adminDataSource,
editingDataSource,
+ readonlyDataSourceList,
hasReadonlyReplicaFeature,
showReadOnlyDataSourceFeatureModal,
} = context;
@@ -593,16 +594,6 @@ const valueChanged = computed(() => {
return !isEqual(editing, original);
});
-// const currentDataSource = computed((): EditDataSource => {
-// if (state.currentDataSourceType === DataSourceType.ADMIN) {
-// return adminDataSource.value;
-// } else if (state.currentDataSourceType === DataSourceType.READ_ONLY) {
-// return readonlyDataSource.value!;
-// } else {
-// throw new Error("Unknown data source type");
-// }
-// });
-
const allowUpdate = computed((): boolean => {
if (!valueChanged.value) {
return false;
@@ -611,12 +602,13 @@ const allowUpdate = computed((): boolean => {
if (!isValidSpannerHost(adminDataSource.value.host)) {
return false;
}
- // if (
- // readonlyDataSource.value &&
- // !isValidSpannerHost(readonlyDataSource.value.host)
- // ) {
- // return false;
- // }
+ if (readonlyDataSourceList.value.length > 0) {
+ if (
+ readonlyDataSourceList.value.some((ds) => !isValidSpannerHost(ds.host))
+ ) {
+ return false;
+ }
+ }
}
return true;
});
@@ -843,16 +835,13 @@ const doUpdate = async () => {
return await updateDataSource(editing, original, adminDataSource.value);
};
const maybeUpsertReadonlyDataSources = async () => {
- const readonlyDataSourceList = dataSourceEditState.value.dataSources.filter(
- (ds) => ds.type === DataSourceType.READ_ONLY
- );
- if (readonlyDataSourceList.length === 0) {
+ if (readonlyDataSourceList.value.length === 0) {
// Nothing to do
return;
}
// Upsert readonly data sources one by one
- for (let i = 0; i < readonlyDataSourceList.length; i++) {
- const editing = readonlyDataSourceList[i];
+ for (let i = 0; i < readonlyDataSourceList.value.length; i++) {
+ const editing = readonlyDataSourceList.value[i];
const patch = extractDataSourceFromEdit(instance, editing);
if (editing.pendingCreate) {
await instanceV1Store.createDataSource(instance, patch);
@@ -1105,10 +1094,7 @@ const checkRODataSourceFeature = (instance: Instance) => {
return true;
}
- const readonlyDataSourceList = dataSourceEditState.value.dataSources.filter(
- (ds) => ds.type === DataSourceType.READ_ONLY
- );
- if (readonlyDataSourceList.length === 0) {
+ if (readonlyDataSourceList.value.length === 0) {
// Not creating or editing any RO data source
return true;
}
@@ -1131,7 +1117,7 @@ const checkRODataSourceFeature = (instance: Instance) => {
return true;
};
// Need to check all RO data sources
- return readonlyDataSourceList.every(checkOne);
+ return readonlyDataSourceList.value.every(checkOne);
};
const changeInstanceActivation = async (on: boolean) => {
diff --git a/frontend/src/components/InstanceForm/context.ts b/frontend/src/components/InstanceForm/context.ts
index c85007767f..78411636b1 100644
--- a/frontend/src/components/InstanceForm/context.ts
+++ b/frontend/src/components/InstanceForm/context.ts
@@ -31,6 +31,7 @@ export type InstanceFormContext = {
// derived states
adminDataSource: ComputedRef<EditDataSource>;
editingDataSource: ComputedRef<EditDataSource | undefined>;
+ readonlyDataSourceList: ComputedRef<EditDataSource[]>;
hasReadOnlyDataSource: ComputedRef<boolean>;
};
@@ -68,13 +69,14 @@ export const provideInstanceFormContext = (
if (editingDataSourceId === undefined) return undefined;
return dataSources.find((ds) => ds.id === editingDataSourceId);
});
- const hasReadOnlyDataSource = computed(() => {
- return (
- dataSourceEditState.value.dataSources.filter(
- (ds) => ds.type === DataSourceType.READ_ONLY
- ).length > 0
+ const readonlyDataSourceList = computed(() => {
+ return dataSourceEditState.value.dataSources.filter(
+ (ds) => ds.type === DataSourceType.READ_ONLY
);
});
+ const hasReadOnlyDataSource = computed(() => {
+ return readonlyDataSourceList.value.length > 0;
+ });
const hasReadonlyReplicaFeature = computed(() => {
return useSubscriptionV1Store().hasInstanceFeature(
@@ -93,6 +95,7 @@ export const provideInstanceFormContext = (
dataSourceEditState,
adminDataSource,
editingDataSource,
+ readonlyDataSourceList,
hasReadOnlyDataSource,
hasReadonlyReplicaFeature,
showReadOnlyDataSourceFeatureModal,