Skip to content

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

fix: backfill migration history by chunks (#5127)

p0ny848ff2e26469
diff --git a/backend/plugin/db/clickhouse/migrate.go b/backend/plugin/db/clickhouse/migrate.go
index 9e516e6f5a..69653b67ad 100644
--- a/backend/plugin/db/clickhouse/migrate.go
+++ b/backend/plugin/db/clickhouse/migrate.go
@@ -269,5 +269,8 @@ func (driver *Driver) FindMigrationHistoryList(ctx context.Context, find *db.Mig
if v := find.Limit; v != nil {
query += fmt.Sprintf(" LIMIT %d", *v)
}
+ if v := find.Offset; v != nil {
+ query += fmt.Sprintf(" OFFSET %d", *v)
+ }
return util.FindMigrationHistoryList(ctx, query, params, driver, db.BytebaseDatabase)
}
diff --git a/backend/plugin/db/driver.go b/backend/plugin/db/driver.go
index 3229d5854d..d21409cbb8 100644
--- a/backend/plugin/db/driver.go
+++ b/backend/plugin/db/driver.go
@@ -356,7 +356,8 @@ type MigrationHistoryFind struct {
Source *MigrationSource
Version *string
// If specified, then it will only fetch "Limit" most recent migration histories
- Limit *int
+ Limit *int
+ Offset *int
// Fields below should be set if fetching from metaDB instance_change_history table.
DatabaseID *int
diff --git a/backend/plugin/db/mysql/migrate.go b/backend/plugin/db/mysql/migrate.go
index 3875275019..0353044154 100644
--- a/backend/plugin/db/mysql/migrate.go
+++ b/backend/plugin/db/mysql/migrate.go
@@ -251,5 +251,8 @@ func (driver *Driver) FindMigrationHistoryList(ctx context.Context, find *db.Mig
if v := find.Limit; v != nil {
query += fmt.Sprintf(" LIMIT %d", *v)
}
+ if v := find.Offset; v != nil {
+ query += fmt.Sprintf(" OFFSET %d", *v)
+ }
return util.FindMigrationHistoryList(ctx, query, params, driver, db.BytebaseDatabase)
}
diff --git a/backend/plugin/db/pg/migrate.go b/backend/plugin/db/pg/migrate.go
index d13c231b08..98ae2dd3ce 100644
--- a/backend/plugin/db/pg/migrate.go
+++ b/backend/plugin/db/pg/migrate.go
@@ -303,6 +303,9 @@ func (driver *Driver) FindMigrationHistoryList(ctx context.Context, find *db.Mig
if v := find.Limit; v != nil {
query += fmt.Sprintf(" LIMIT %d", *v)
}
+ if v := find.Offset; v != nil {
+ query += fmt.Sprintf(" OFFSET %d", *v)
+ }
database := db.BytebaseDatabase
if driver.strictUseDb() {
diff --git a/backend/plugin/db/snowflake/migrate.go b/backend/plugin/db/snowflake/migrate.go
index 18d668717c..089093831b 100644
--- a/backend/plugin/db/snowflake/migrate.go
+++ b/backend/plugin/db/snowflake/migrate.go
@@ -279,6 +279,9 @@ func (driver *Driver) FindMigrationHistoryList(ctx context.Context, find *db.Mig
if v := find.Limit; v != nil {
query += fmt.Sprintf(" LIMIT %d", *v)
}
+ if v := find.Offset; v != nil {
+ query += fmt.Sprintf(" OFFSET %d", *v)
+ }
return util.FindMigrationHistoryList(ctx, query, params, driver, bytebaseDatabase)
}
diff --git a/backend/server/server.go b/backend/server/server.go
index c4e89d19f4..57422e8e24 100644
--- a/backend/server/server.go
+++ b/backend/server/server.go
@@ -568,7 +568,7 @@ func NewServer(ctx context.Context, profile config.Profile) (*Server, error) {
p.Use(e)
// TODO: remove this on May 2023
- s.backfillInstanceChangeHistory(ctx)
+ go s.backfillInstanceChangeHistory(ctx)
serverStarted = true
return s, nil
@@ -1096,7 +1096,10 @@ func (s *Server) backfillInstanceChangeHistory(ctx context.Context) {
var errList error
for _, instance := range instanceList {
err := func(instance *store.InstanceMessage) error {
- if instance.Engine == db.Redis || instance.Engine == db.Oracle || instance.Engine == db.Spanner || instance.Engine == db.MongoDB {
+ limit := 10
+ offset := 0
+
+ if instance.Engine == db.Redis || instance.Engine == db.Oracle || instance.Engine == db.Spanner || instance.Engine == db.MongoDB || instance.Engine == db.SQLite {
return nil
}
if instanceMigrated[instance.UID] {
@@ -1118,73 +1121,78 @@ func (s *Server) backfillInstanceChangeHistory(ctx context.Context) {
}
defer driver.Close(ctx)
- history, err := driver.FindMigrationHistoryList(ctx, &db.MigrationHistoryFind{
- InstanceID: &instance.UID,
- })
- if err != nil {
- return err
- }
- if len(history) == 0 {
- return nil
- }
-
- var creates []*store.InstanceChangeHistoryMessage
- for _, h := range history {
- var databaseID *int
- if database, ok := nameToDatabase[h.Namespace]; ok {
- databaseID = &database.UID
- }
-
- var issueID *int
- if id, err := strconv.Atoi(h.IssueID); err != nil {
- errList = multierr.Append(errList, err)
- } else if hasIssueID[id] {
- // Has FK constraint on issue_id.
- // Set to id if issue exists.
- issueID = &id
+ for {
+ history, err := driver.FindMigrationHistoryList(ctx, &db.MigrationHistoryFind{
+ InstanceID: &instance.UID,
+ Limit: &limit,
+ Offset: &offset,
+ })
+ if err != nil {
+ return err
}
-
- creatorID := api.SystemBotID
- updaterID := api.SystemBotID
- if principal, ok := nameToPrincipal[h.Creator]; ok {
- creatorID = principal.ID
+ if len(history) == 0 {
+ break
}
- if principal, ok := nameToPrincipal[h.Updater]; ok {
- updaterID = principal.ID
+ offset += limit
+
+ var creates []*store.InstanceChangeHistoryMessage
+ for _, h := range history {
+ var databaseID *int
+ if database, ok := nameToDatabase[h.Namespace]; ok {
+ databaseID = &database.UID
+ }
+
+ var issueID *int
+ if id, err := strconv.Atoi(h.IssueID); err != nil {
+ errList = multierr.Append(errList, err)
+ } else if hasIssueID[id] {
+ // Has FK constraint on issue_id.
+ // Set to id if issue exists.
+ issueID = &id
+ }
+
+ creatorID := api.SystemBotID
+ updaterID := api.SystemBotID
+ if principal, ok := nameToPrincipal[h.Creator]; ok {
+ creatorID = principal.ID
+ }
+ if principal, ok := nameToPrincipal[h.Updater]; ok {
+ updaterID = principal.ID
+ }
+
+ storedVersion, err := util.ToStoredVersion(h.UseSemanticVersion, h.Version, h.SemanticVersionSuffix)
+ if err != nil {
+ return err
+ }
+
+ changeHistory := store.InstanceChangeHistoryMessage{
+ CreatorID: creatorID,
+ CreatedTs: h.CreatedTs,
+ UpdaterID: updaterID,
+ UpdatedTs: h.UpdatedTs,
+ InstanceID: &instance.UID,
+ DatabaseID: databaseID,
+ IssueID: issueID,
+ ReleaseVersion: h.ReleaseVersion,
+ Sequence: int64(h.Sequence),
+ Source: h.Source,
+ Type: h.Type,
+ Status: h.Status,
+ Version: storedVersion,
+ Description: h.Description,
+ Statement: h.Statement,
+ Schema: h.Schema,
+ SchemaPrev: h.SchemaPrev,
+ ExecutionDurationNs: h.ExecutionDurationNs,
+ Payload: h.Payload,
+ }
+
+ creates = append(creates, &changeHistory)
}
- storedVersion, err := util.ToStoredVersion(h.UseSemanticVersion, h.Version, h.SemanticVersionSuffix)
- if err != nil {
+ if _, err := s.store.CreateInstanceChangeHistory(ctx, creates...); err != nil {
return err
}
-
- changeHistory := store.InstanceChangeHistoryMessage{
- CreatorID: creatorID,
- CreatedTs: h.CreatedTs,
- UpdaterID: updaterID,
- UpdatedTs: h.UpdatedTs,
- InstanceID: &instance.UID,
- DatabaseID: databaseID,
- IssueID: issueID,
- ReleaseVersion: h.ReleaseVersion,
- Sequence: int64(h.Sequence),
- Source: h.Source,
- Type: h.Type,
- Status: h.Status,
- Version: storedVersion,
- Description: h.Description,
- Statement: h.Statement,
- Schema: h.Schema,
- SchemaPrev: h.SchemaPrev,
- ExecutionDurationNs: h.ExecutionDurationNs,
- Payload: h.Payload,
- }
-
- creates = append(creates, &changeHistory)
- }
-
- if _, err := s.store.CreateInstanceChangeHistory(ctx, creates...); err != nil {
- return err
}
return nil
}(instance)