Skip to content

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

chore: support SyncSchema to only sync the given list of databases (#1728)

Danny Xu3d6db1b61a18
diff --git a/plugin/db/clickhouse/sync.go b/plugin/db/clickhouse/sync.go
index 757703af02..87fef01049 100644
--- a/plugin/db/clickhouse/sync.go
+++ b/plugin/db/clickhouse/sync.go
@@ -60,19 +60,25 @@ func (driver *Driver) SyncInstance(ctx context.Context) (*db.InstanceMeta, error
}
// SyncSchema syncs the schema.
-func (driver *Driver) SyncSchema(ctx context.Context) ([]*db.Schema, error) {
+func (driver *Driver) SyncSchema(ctx context.Context, databaseList ...string) ([]*db.Schema, error) {
excludedDatabaseList := []string{
// Skip our internal "bytebase" database
"'bytebase'",
}
-
+ var includeDatabaseList []string
// Skip all system databases
for k := range systemDatabases {
- excludedDatabaseList = append(excludedDatabaseList, fmt.Sprintf("'%s'", k))
+ excludedDatabaseList = append(excludedDatabaseList, fmt.Sprintf("'%s'", strings.ToLower(k)))
+ }
+ for _, k := range databaseList {
+ includeDatabaseList = append(includeDatabaseList, fmt.Sprintf("'%s'", strings.ToLower(k)))
}
// Query column info
columnWhere := fmt.Sprintf("LOWER(database) NOT IN (%s)", strings.Join(excludedDatabaseList, ", "))
+ if len(databaseList) > 0 {
+ columnWhere = fmt.Sprintf("%s AND LOWER(database) IN (%s)", columnWhere, strings.Join(includeDatabaseList, ", "))
+ }
query := `
SELECT
database,
@@ -118,6 +124,9 @@ func (driver *Driver) SyncSchema(ctx context.Context) ([]*db.Schema, error) {
// Query table info
tableWhere := fmt.Sprintf("LOWER(database) NOT IN (%s)", strings.Join(excludedDatabaseList, ", "))
+ if len(databaseList) > 0 {
+ tableWhere = fmt.Sprintf("%s AND LOWER(database) IN (%s)", tableWhere, strings.Join(includeDatabaseList, ", "))
+ }
query = `
SELECT
database,
@@ -183,6 +192,9 @@ func (driver *Driver) SyncSchema(ctx context.Context) ([]*db.Schema, error) {
var schemaList []*db.Schema
// Query db info
where := fmt.Sprintf("name NOT IN (%s)", strings.Join(excludedDatabaseList, ", "))
+ if len(databaseList) > 0 {
+ where = fmt.Sprintf("%s AND name IN (%s)", where, strings.Join(includeDatabaseList, ", "))
+ }
query = `
SELECT
name
diff --git a/plugin/db/driver.go b/plugin/db/driver.go
index 924d714ff2..471216a3a5 100644
--- a/plugin/db/driver.go
+++ b/plugin/db/driver.go
@@ -424,7 +424,8 @@ type Driver interface {
Ping(ctx context.Context) error
GetDbConnection(ctx context.Context, database string) (*sql.DB, error)
GetVersion(ctx context.Context) (string, error)
- SyncSchema(ctx context.Context) ([]*Schema, error)
+ // SyncSchema syncs the database schema. If databaseList is empty, we will sync all databases for the instance.
+ SyncSchema(ctx context.Context, databaseList ...string) ([]*Schema, error)
SyncInstance(ctx context.Context) (*InstanceMeta, error)
// Execute will execute the statement. For CREATE DATABASE statement, some types of databases such as Postgres
// will not use transactions to execute the statement but will still use transactions to execute the rest of statements.
diff --git a/plugin/db/mysql/sync.go b/plugin/db/mysql/sync.go
index 4c1e4c29bb..f2a3622403 100644
--- a/plugin/db/mysql/sync.go
+++ b/plugin/db/mysql/sync.go
@@ -76,7 +76,7 @@ func (driver *Driver) SyncInstance(ctx context.Context) (*db.InstanceMeta, error
}
// SyncSchema syncs the schema.
-func (driver *Driver) SyncSchema(ctx context.Context) ([]*db.Schema, error) {
+func (driver *Driver) SyncSchema(ctx context.Context, databaseList ...string) ([]*db.Schema, error) {
// Query MySQL version
version, err := driver.GetVersion(ctx)
if err != nil {
@@ -88,14 +88,20 @@ func (driver *Driver) SyncSchema(ctx context.Context) ([]*db.Schema, error) {
// Skip our internal "bytebase" database
"'bytebase'",
}
-
+ var includeDatabaseList []string
// Skip all system databases
for k := range systemDatabases {
- excludedDatabaseList = append(excludedDatabaseList, fmt.Sprintf("'%s'", k))
+ excludedDatabaseList = append(excludedDatabaseList, fmt.Sprintf("'%s'", strings.ToLower(k)))
+ }
+ for _, k := range databaseList {
+ includeDatabaseList = append(includeDatabaseList, fmt.Sprintf("'%s'", strings.ToLower(k)))
}
// Query index info
indexWhere := fmt.Sprintf("LOWER(TABLE_SCHEMA) NOT IN (%s)", strings.Join(excludedDatabaseList, ", "))
+ if len(databaseList) > 0 {
+ indexWhere = fmt.Sprintf("%s AND LOWER(TABLE_SCHEMA) IN (%s)", indexWhere, strings.Join(includeDatabaseList, ", "))
+ }
query := `
SELECT
TABLE_SCHEMA,
@@ -171,6 +177,9 @@ func (driver *Driver) SyncSchema(ctx context.Context) ([]*db.Schema, error) {
// Query column info
columnWhere := fmt.Sprintf("LOWER(TABLE_SCHEMA) NOT IN (%s)", strings.Join(excludedDatabaseList, ", "))
+ if len(databaseList) > 0 {
+ columnWhere = fmt.Sprintf("%s AND LOWER(TABLE_SCHEMA) IN (%s)", columnWhere, strings.Join(includeDatabaseList, ", "))
+ }
query = `
SELECT
TABLE_SCHEMA,
@@ -228,6 +237,9 @@ func (driver *Driver) SyncSchema(ctx context.Context) ([]*db.Schema, error) {
// Query table info
tableWhere := fmt.Sprintf("LOWER(TABLE_SCHEMA) NOT IN (%s)", strings.Join(excludedDatabaseList, ", "))
+ if len(databaseList) > 0 {
+ tableWhere = fmt.Sprintf("%s AND LOWER(TABLE_SCHEMA) IN (%s)", tableWhere, strings.Join(includeDatabaseList, ", "))
+ }
query = `
SELECT
TABLE_SCHEMA,
@@ -309,6 +321,9 @@ func (driver *Driver) SyncSchema(ctx context.Context) ([]*db.Schema, error) {
// Query view info
viewWhere := fmt.Sprintf("LOWER(TABLE_SCHEMA) NOT IN (%s)", strings.Join(excludedDatabaseList, ", "))
+ if len(databaseList) > 0 {
+ viewWhere = fmt.Sprintf("%s AND LOWER(TABLE_SCHEMA) IN (%s)", viewWhere, strings.Join(includeDatabaseList, ", "))
+ }
query = `
SELECT
TABLE_SCHEMA,
@@ -349,6 +364,9 @@ func (driver *Driver) SyncSchema(ctx context.Context) ([]*db.Schema, error) {
// Query db info
where := fmt.Sprintf("LOWER(SCHEMA_NAME) NOT IN (%s)", strings.Join(excludedDatabaseList, ", "))
+ if len(databaseList) > 0 {
+ where = fmt.Sprintf("%s AND LOWER(TABLE_SCHEMA) IN (%s)", where, strings.Join(includeDatabaseList, ", "))
+ }
query = `
SELECT
SCHEMA_NAME,
diff --git a/plugin/db/pg/sync.go b/plugin/db/pg/sync.go
index cba18c604f..54d0c0d370 100644
--- a/plugin/db/pg/sync.go
+++ b/plugin/db/pg/sync.go
@@ -128,7 +128,7 @@ func (driver *Driver) SyncInstance(ctx context.Context) (*db.InstanceMeta, error
}
// SyncSchema syncs the schema.
-func (driver *Driver) SyncSchema(ctx context.Context) ([]*db.Schema, error) {
+func (driver *Driver) SyncSchema(ctx context.Context, databaseList ...string) ([]*db.Schema, error) {
// Skip all system databases
for k := range systemDatabases {
excludedDatabaseList[k] = true
@@ -146,6 +146,16 @@ func (driver *Driver) SyncSchema(ctx context.Context) ([]*db.Schema, error) {
if _, ok := excludedDatabaseList[dbName]; ok {
continue
}
+ exists := false
+ for _, k := range databaseList {
+ if dbName == k {
+ exists = true
+ break
+ }
+ }
+ if !exists {
+ continue
+ }
var schema db.Schema
schema.Name = dbName
diff --git a/plugin/db/snowflake/sync.go b/plugin/db/snowflake/sync.go
index 308ef9db49..7f4373b2d0 100644
--- a/plugin/db/snowflake/sync.go
+++ b/plugin/db/snowflake/sync.go
@@ -55,7 +55,7 @@ func (driver *Driver) SyncInstance(ctx context.Context) (*db.InstanceMeta, error
}
// SyncSchema synces the schema.
-func (driver *Driver) SyncSchema(ctx context.Context) ([]*db.Schema, error) {
+func (driver *Driver) SyncSchema(ctx context.Context, databaseList ...string) ([]*db.Schema, error) {
// Query user info
if err := driver.useRole(ctx, accountAdminRole); err != nil {
return nil, err
@@ -72,6 +72,16 @@ func (driver *Driver) SyncSchema(ctx context.Context) ([]*db.Schema, error) {
if database == bytebaseDatabase {
continue
}
+ exists := false
+ for _, k := range databaseList {
+ if database == k {
+ exists = true
+ break
+ }
+ }
+ if !exists {
+ continue
+ }
var schema db.Schema
schema.Name = database
diff --git a/plugin/db/sqlite/sync.go b/plugin/db/sqlite/sync.go
index 2fa9e73b47..b99ff155bb 100644
--- a/plugin/db/sqlite/sync.go
+++ b/plugin/db/sqlite/sync.go
@@ -52,7 +52,7 @@ func (driver *Driver) SyncInstance(ctx context.Context) (*db.InstanceMeta, error
}
// SyncSchema syncs the schema.
-func (driver *Driver) SyncSchema(ctx context.Context) ([]*db.Schema, error) {
+func (driver *Driver) SyncSchema(ctx context.Context, databaseList ...string) ([]*db.Schema, error) {
databases, err := driver.getDatabases()
if err != nil {
return nil, err
@@ -63,6 +63,16 @@ func (driver *Driver) SyncSchema(ctx context.Context) ([]*db.Schema, error) {
if _, ok := excludedDatabaseList[dbName]; ok {
continue
}
+ exists := false
+ for _, k := range databaseList {
+ if dbName == k {
+ exists = true
+ break
+ }
+ }
+ if !exists {
+ continue
+ }
var schema db.Schema
schema.Name = dbName