Skip to content

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

feat: make sync schema optional after creating/patching the instance connection info (#1274)

tianzhou4fc1817835e7
diff --git a/api/data_source.go b/api/data_source.go
index 1c634af1f7..7e13958167 100644
--- a/api/data_source.go
+++ b/api/data_source.go
@@ -92,6 +92,10 @@ type DataSourceCreate struct {
Type DataSourceType `jsonapi:"attr,type"`
Username string `jsonapi:"attr,username"`
Password string `jsonapi:"attr,password"`
+ // If true, syncs the schema after creating the data source. The client
+ // may set to false if the target data source's instance contains too many databases
+ // to avoid the request timeout.
+ SyncSchema bool `jsonapi:"attr,syncSchema"`
}
// DataSourceFind is the API message for finding data sources.
@@ -126,4 +130,8 @@ type DataSourcePatch struct {
Username *string `jsonapi:"attr,username"`
Password *string `jsonapi:"attr,password"`
UseEmptyPassword *bool `jsonapi:"attr,useEmptyPassword"`
+ // If true, syncs the schema after patching the data source. The client
+ // may set to false if the target data source's instance contains too many databases
+ // to avoid the request timeout.
+ SyncSchema bool `jsonapi:"attr,syncSchema"`
}
diff --git a/api/instance.go b/api/instance.go
index f8b3610780..ac8e754c74 100644
--- a/api/instance.go
+++ b/api/instance.go
@@ -55,6 +55,10 @@ type InstanceCreate struct {
Port string `jsonapi:"attr,port"`
Username string `jsonapi:"attr,username"`
Password string `jsonapi:"attr,password"`
+ // If true, syncs the schema after adding the instance. The client
+ // may set to false if the target instance contains too many databases
+ // to avoid the request timeout.
+ SyncSchema bool `jsonapi:"attr,syncSchema"`
}
// InstanceFind is the API message for finding instances.
@@ -83,14 +87,15 @@ type InstancePatch struct {
UpdaterID int
// Domain specific fields
- Name *string `jsonapi:"attr,name"`
- EngineVersion *string
- ExternalLink *string `jsonapi:"attr,externalLink"`
- Host *string `jsonapi:"attr,host"`
- Port *string `jsonapi:"attr,port"`
- Username *string `jsonapi:"attr,username"`
- Password *string `jsonapi:"attr,password"`
- UseEmptyPassword bool `jsonapi:"attr,useEmptyPassword"`
+ Name *string `jsonapi:"attr,name"`
+ EngineVersion *string
+ ExternalLink *string `jsonapi:"attr,externalLink"`
+ Host *string `jsonapi:"attr,host"`
+ Port *string `jsonapi:"attr,port"`
+ // If true, syncs the schema after patching the instance. The client
+ // may set to false if the target instance contains too many databases
+ // to avoid the request timeout.
+ SyncSchema bool `jsonapi:"attr,syncSchema"`
}
// DataSourceFromInstanceWithType gets a typed data source from a instance.
diff --git a/frontend/src/components/CreateInstanceForm.vue b/frontend/src/components/CreateInstanceForm.vue
index 681e3220e4..66d8463bad 100644
--- a/frontend/src/components/CreateInstanceForm.vue
+++ b/frontend/src/components/CreateInstanceForm.vue
@@ -181,32 +181,37 @@
</div>
<!-- Action Button Group -->
- <div class="pt-4">
+ <div class="pt-4 px-2">
<!-- Create button group -->
- <div class="flex justify-end items-center">
- <div>
+ <div class="flex justify-between items-center">
+ <BBCheckbox
+ :title="$t('instance.sync-schema-afterward')"
+ :value="state.instance.syncSchema"
+ @toggle="state.instance.syncSchema = !state.instance.syncSchema"
+ />
+ <div class="flex justify-end items-center">
<BBSpin
v-if="state.isCreatingInstance"
:title="$t('common.creating')"
/>
- </div>
- <div class="ml-2">
- <button
- type="button"
- class="btn-normal py-2 px-4"
- :disabled="state.isCreatingInstance"
- @click.prevent="cancel"
- >
- {{ $t("common.cancel") }}
- </button>
- <button
- type="button"
- class="btn-primary ml-3 inline-flex justify-center py-2 px-4"
- :disabled="!allowCreate || state.isCreatingInstance"
- @click.prevent="tryCreate"
- >
- {{ $t("common.create") }}
- </button>
+ <div class="ml-2">
+ <button
+ type="button"
+ class="btn-normal py-2 px-4"
+ :disabled="state.isCreatingInstance"
+ @click.prevent="cancel"
+ >
+ {{ $t("common.cancel") }}
+ </button>
+ <button
+ type="button"
+ class="btn-primary ml-3 inline-flex justify-center py-2 px-4"
+ :disabled="!allowCreate || state.isCreatingInstance"
+ @click.prevent="tryCreate"
+ >
+ {{ $t("common.create") }}
+ </button>
+ </div>
</div>
</div>
</div>
@@ -282,6 +287,7 @@ const state = reactive<LocalState>({
// In release mode, Bytebase is likely run inside docker and access the local network via host.docker.internal.
host: isDev() ? "127.0.0.1" : "host.docker.internal",
username: "",
+ syncSchema: true,
},
showCreateInstanceWarningModal: false,
createInstanceWarning: "",
diff --git a/frontend/src/components/InstanceForm.vue b/frontend/src/components/InstanceForm.vue
index e03aa432dd..5c11b6cc6d 100644
--- a/frontend/src/components/InstanceForm.vue
+++ b/frontend/src/components/InstanceForm.vue
@@ -252,22 +252,32 @@
</div>
<!-- Action Button Group -->
- <div class="pt-4">
- <div class="flex justify-end items-center">
+ <div class="pt-4 px-2">
+ <div class="flex justify-between items-center">
<div>
- <BBSpin v-if="state.isUpdating" :title="$t('common.updating')" />
+ <BBCheckbox
+ v-if="connectionInfoChanged"
+ :title="$t('instance.sync-schema-afterward')"
+ :value="state.syncSchema"
+ @toggle="state.syncSchema = !state.syncSchema"
+ />
+ </div>
+ <div class="flex justify-end items-center">
+ <div>
+ <BBSpin v-if="state.isUpdating" :title="$t('common.updating')" />
+ </div>
+ <button
+ v-if="allowEdit"
+ type="button"
+ :disabled="!valueChanged || state.isUpdating"
+ :class="
+ !valueChanged || state.isUpdating ? 'btn-normal' : 'btn-primary'
+ "
+ @click.prevent="doUpdate"
+ >
+ {{ $t("common.update") }}
+ </button>
</div>
- <button
- v-if="allowEdit"
- type="button"
- :disabled="!valueChanged || state.isUpdating"
- :class="
- !valueChanged || state.isUpdating ? 'btn-normal' : 'btn-primary'
- "
- @click.prevent="doUpdate"
- >
- {{ $t("common.update") }}
- </button>
</div>
</div>
</form>
@@ -309,6 +319,7 @@ interface State {
originalInstance: Instance;
instance: Instance;
isUpdating: boolean;
+ syncSchema: boolean;
dataSourceList: EditDataSource[];
currentDataSourceType: DataSourceType;
}
@@ -340,6 +351,7 @@ const state = reactive<State>({
// Make hard copy since we are going to make equal comparison to determine the update button enable state.
instance: cloneDeep(props.instance),
isUpdating: false,
+ syncSchema: true,
dataSourceList: dataSourceList,
currentDataSourceType: "ADMIN",
});
@@ -354,6 +366,21 @@ const valueChanged = computed(() => {
return !isEqual(state.instance, state.originalInstance);
});
+const connectionInfoChanged = computed(() => {
+ if (!valueChanged.value) {
+ return false;
+ }
+
+ return (
+ state.instance.host != state.originalInstance.host ||
+ state.instance.port != state.originalInstance.port ||
+ !isEqual(
+ state.originalInstance.dataSourceList,
+ state.instance.dataSourceList
+ )
+ );
+});
+
const defaultPort = computed(() => {
if (state.instance.engine == "CLICKHOUSE") {
return "9000";
@@ -502,10 +529,10 @@ const updateInstance = (field: string, value: string) => {
};
const doUpdate = () => {
- const patchedInstance: InstancePatch = {};
+ const patchedInstance: InstancePatch = { syncSchema: state.syncSchema };
let instanceInfoChanged = false;
let dataSourceListChanged = false;
- let connectionInfoChanged = false;
+ let reloadDatabaseAndUser = connectionInfoChanged.value;
if (state.instance.name != state.originalInstance.name) {
patchedInstance.name = state.instance.name;
@@ -517,13 +544,11 @@ const doUpdate = () => {
}
if (state.instance.host != state.originalInstance.host) {
patchedInstance.host = state.instance.host;
- connectionInfoChanged = true;
instanceInfoChanged = true;
}
if (state.instance.port != state.originalInstance.port) {
patchedInstance.port = state.instance.port;
instanceInfoChanged = true;
- connectionInfoChanged = true;
}
if (
@@ -553,6 +578,7 @@ const doUpdate = () => {
type: dataSource.type,
username: dataSource.username,
password: dataSource.password,
+ syncSchema: state.syncSchema,
})
);
}
@@ -563,7 +589,7 @@ const doUpdate = () => {
dataSourceStore.patchDataSource({
databaseId: dataSource.databaseId,
dataSourceId: dataSource.id,
- dataSource: dataSource,
+ dataSource: { ...dataSource, syncSchema: state.syncSchema },
})
);
}
@@ -583,18 +609,15 @@ const doUpdate = () => {
instanceStore
.fetchInstanceById(state.instance.id)
.then((instance) => {
- state.isUpdating = false;
state.originalInstance = instance;
state.instance = cloneDeep(state.originalInstance);
- state.dataSourceList = state.instance.dataSourceList.map(
- (dataSource) => {
- return {
- ...cloneDeep(dataSource),
- updatedPassword: "",
- useEmptyPassword: false,
- } as EditDataSource;
- }
- );
+ state.dataSourceList = instance.dataSourceList.map((dataSource) => {
+ return {
+ ...cloneDeep(dataSource),
+ updatedPassword: "",
+ useEmptyPassword: false,
+ } as EditDataSource;
+ });
pushNotification({
module: "bytebase",
style: "SUCCESS",
@@ -604,12 +627,14 @@ const doUpdate = () => {
});
// Backend will sync the schema upon connection info change, so here we try to fetch the synced schema.
- if (connectionInfoChanged) {
- useDatabaseStore().fetchDatabaseListByInstanceId(state.instance.id);
+ if (reloadDatabaseAndUser) {
+ useDatabaseStore().fetchDatabaseListByInstanceId(instance.id);
+ useInstanceStore().fetchInstanceUserListById(instance.id);
}
})
.finally(() => {
state.isUpdating = false;
+ state.syncSchema = true;
});
});
}
diff --git a/frontend/src/locales/en-US.json b/frontend/src/locales/en-US.json
index d6f0ed5d9b..76db6ec18a 100644
--- a/frontend/src/locales/en-US.json
+++ b/frontend/src/locales/en-US.json
@@ -529,6 +529,7 @@
"new-database": "New Database",
"syncing": "Syncing ...",
"sync-now": "Sync Now",
+ "sync-schema-afterward": "Sync schema afterward",
"create-migration-schema": "Create migration schema",
"missing-migration-schema": "Missing migration schema",
"unable-to-connect-instance-to-check-migration-schema": "Unable to connect instance to check migration schema",
diff --git a/frontend/src/locales/zh-CN.json b/frontend/src/locales/zh-CN.json
index bf14d18995..86945e34a1 100644
--- a/frontend/src/locales/zh-CN.json
+++ b/frontend/src/locales/zh-CN.json
@@ -529,6 +529,7 @@
"new-database": "@:common.new@:common.database",
"syncing": "同步中 ...",
"sync-now": "现在同步",
+ "sync-schema-afterward": "随即同步 schema",
"create-migration-schema": "@:common.create@:common.migration @:{'common.schema'}",
"missing-migration-schema": "缺少@:common.migration @:common.schema",
"unable-to-connect-instance-to-check-migration-schema": "无法连接到@:{'common.instance'}以检查@:{'common.migration'} @:{'common.schema'}",
diff --git a/frontend/src/types/dataSource.ts b/frontend/src/types/dataSource.ts
index 368733c33a..b58ee6a43c 100644
--- a/frontend/src/types/dataSource.ts
+++ b/frontend/src/types/dataSource.ts
@@ -44,6 +44,7 @@ export type DataSourceCreate = {
type: DataSourceType;
username?: string;
password?: string;
+ syncSchema: boolean;
};
export type DataSourcePatch = {
@@ -52,6 +53,7 @@ export type DataSourcePatch = {
username?: string;
password?: string;
useEmptyPassword?: boolean;
+ syncSchema: boolean;
};
export type DataSourceMember = {
diff --git a/frontend/src/types/instance.ts b/frontend/src/types/instance.ts
index bfe25e66ab..f2b1d407f6 100644
--- a/frontend/src/types/instance.ts
+++ b/frontend/src/types/instance.ts
@@ -79,6 +79,7 @@ export type InstanceCreate = {
// In mysql, username can be empty which means anonymous user
username?: string;
password?: string;
+ syncSchema: boolean;
};
export type InstancePatch = {
@@ -90,6 +91,7 @@ export type InstancePatch = {
externalLink?: string;
host?: string;
port?: string;
+ syncSchema: boolean;
};
export type MigrationSchemaStatus = "UNKNOWN" | "OK" | "NOT_EXIST";
diff --git a/server/database.go b/server/database.go
index 332a0b447c..1864c2f785 100644
--- a/server/database.go
+++ b/server/database.go
@@ -718,6 +718,15 @@ func (s *Server) registerDatabaseRoutes(g *echo.Group) {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create data source").SetInternal(err)
}
+ if dataSourceCreate.SyncSchema {
+ // Refetches the instance to get the updated data source.
+ updatedInstance, err := s.store.GetInstanceByID(ctx, database.InstanceID)
+ if err != nil {
+ return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch updated instance with ID %d", database.InstanceID)).SetInternal(err)
+ }
+ s.syncEngineVersionAndSchema(ctx, updatedInstance)
+ }
+
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
if err := jsonapi.MarshalPayload(c.Response().Writer, dataSource); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to marshal create data source response").SetInternal(err)
@@ -777,6 +786,15 @@ func (s *Server) registerDatabaseRoutes(g *echo.Group) {
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to update data source with ID %d", dataSourceID)).SetInternal(err)
}
+ if dataSourcePatch.SyncSchema {
+ // Refetches the instance to get the updated data source.
+ updatedInstance, err := s.store.GetInstanceByID(ctx, database.InstanceID)
+ if err != nil {
+ return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch updated instance with ID %d", database.InstanceID)).SetInternal(err)
+ }
+ s.syncEngineVersionAndSchema(ctx, updatedInstance)
+ }
+
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
if err := jsonapi.MarshalPayload(c.Response().Writer, dataSourceNew); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to marshal patch data source response").SetInternal(err)
diff --git a/server/instance.go b/server/instance.go
index a9634464da..326c0f0831 100644
--- a/server/instance.go
+++ b/server/instance.go
@@ -51,8 +51,9 @@ func (s *Server) registerInstanceRoutes(g *echo.Group) {
zap.String("engine", string(instance.Engine)),
zap.Error(err))
}
- // Try immediately sync the engine version and schema after instance creation.
- s.syncEngineVersionAndSchema(ctx, instance)
+ if instanceCreate.SyncSchema {
+ s.syncEngineVersionAndSchema(ctx, instance)
+ }
}
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
@@ -166,7 +167,9 @@ func (s *Server) registerInstanceRoutes(g *echo.Group) {
zap.String("engine", string(instancePatched.Engine)),
zap.Error(err))
}
- s.syncEngineVersionAndSchema(ctx, instancePatched)
+ if instancePatch.SyncSchema {
+ s.syncEngineVersionAndSchema(ctx, instancePatched)
+ }
}
}