Skip to content

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

chore: cleanup list migration history (#5432)

Danny Xudcd151131f75
diff --git a/backend/plugin/advisor/utils_for_tests.go b/backend/plugin/advisor/utils_for_tests.go
index 384a03a3b9..f52802d349 100644
--- a/backend/plugin/advisor/utils_for_tests.go
+++ b/backend/plugin/advisor/utils_for_tests.go
@@ -302,11 +302,6 @@ func (*MockDriver) SetupMigrationIfNeeded(_ context.Context) error {
return nil
}
-// FindMigrationHistoryList implements the Driver interface.
-func (*MockDriver) FindMigrationHistoryList(_ context.Context, _ *database.MigrationHistoryFind) ([]*database.MigrationHistory, error) {
- return nil, nil
-}
-
// Dump implements the Driver interface.
func (*MockDriver) Dump(_ context.Context, _ string, _ io.Writer, _ bool) (string, error) {
return "", nil
diff --git a/backend/plugin/db/driver.go b/backend/plugin/db/driver.go
index 707a984d40..f4e8810883 100644
--- a/backend/plugin/db/driver.go
+++ b/backend/plugin/db/driver.go
@@ -493,9 +493,6 @@ type Driver interface {
// DeleteRole deletes the role by name.
DeleteRole(ctx context.Context, roleName string) error
- // Find the migration history list and return most recent item first.
- FindMigrationHistoryList(ctx context.Context, find *MigrationHistoryFind) ([]*MigrationHistory, error)
-
// Dump and restore
// Dump the database, if dbName is empty, then dump all databases.
// The returned string is the JSON encoded metadata for the logical dump.
diff --git a/backend/plugin/db/mongodb/migrate.go b/backend/plugin/db/mongodb/migrate.go
deleted file mode 100644
index a301fd6adf..0000000000
--- a/backend/plugin/db/mongodb/migrate.go
+++ /dev/null
@@ -1,127 +0,0 @@
-package mongodb
-
-import (
- "context"
- "fmt"
- "strconv"
-
- "go.mongodb.org/mongo-driver/bson"
- "go.mongodb.org/mongo-driver/bson/primitive"
- "go.mongodb.org/mongo-driver/mongo/options"
-
- "github.com/pkg/errors"
-
- "github.com/bytebase/bytebase/backend/plugin/db"
- "github.com/bytebase/bytebase/backend/plugin/db/util"
-)
-
-const (
- // migrationHistoryDefaultDatabase is the default database name for migration history.
- migrationHistoryDefaultDatabase = "bytebase"
- // migrationHistoryDefaultCollection is the default collection name for migration history.
- migrationHistoryDefaultCollection = "migration_history"
-)
-
-// MigrationHistory is the migration history record in MongoDB.
-type MigrationHistory struct {
- ID int64 `bson:"id"`
- CreatedBy string `bson:"created_by"`
- CreatedTs primitive.Timestamp `bson:"created_ts"`
- UpdatedBy string `bson:"updated_by"`
- UpdatedTs primitive.Timestamp `bson:"updated_ts"`
- ReleaseVersion string `bson:"release_version"`
- Namespace string `bson:"namespace"`
- Sequence int64 `bson:"sequence"`
- Source string `bson:"source"`
- Type string `bson:"type"`
- Status string `bson:"status"`
- Version string `bson:"version"`
- Description string `bson:"description"`
- Statement string `bson:"statement"`
- ExecutionDurationNs int64 `bson:"execution_duration_ns"`
- IssueID string `bson:"issue_id"`
- Payload string `bson:"payload"`
-}
-
-// FindMigrationHistoryList finds the migration history list.
-func (driver *Driver) FindMigrationHistoryList(ctx context.Context, find *db.MigrationHistoryFind) ([]*db.MigrationHistory, error) {
- database := driver.client.Database(migrationHistoryDefaultDatabase)
- collection := database.Collection(migrationHistoryDefaultCollection)
- filter := bson.M{}
- if v := find.ID; v != nil {
- longMigrationHistoryID, err := strconv.ParseInt(*v, 10, 64)
- if err != nil {
- return nil, errors.Wrapf(err, "failed to parse inserted ID %s to int64", *v)
- }
- filter["id"] = longMigrationHistoryID
- }
- if v := find.Database; v != nil {
- filter["namespace"] = *v
- }
- if v := find.Version; v != nil {
- storedVersion, err := util.ToStoredVersion(false, *v, "")
- if err != nil {
- return nil, err
- }
- filter["version"] = storedVersion
- }
- if v := find.Source; v != nil {
- filter["source"] = *v
- }
- findOptions := []*options.FindOptions{
- // Sort by id in descending order.
- options.Find().SetSort(bson.M{"id": -1}),
- }
- if v := find.Limit; v != nil {
- findOptions = append(findOptions, options.Find().SetLimit(int64(*v)))
- }
-
- cursor, err := collection.Find(ctx, filter, findOptions...)
- if err != nil {
- return nil, errors.Wrapf(err, "failed to find migration history list")
- }
- defer cursor.Close(ctx)
-
- var migrationHistoryList []*db.MigrationHistory
- for cursor.Next(ctx) {
- var history MigrationHistory
- if err := cursor.Decode(&history); err != nil {
- return nil, errors.Wrapf(err, "failed to decode migration history")
- }
- migrationHistory, err := convertMigrationHistory(history)
- if err != nil {
- return nil, errors.Wrapf(err, "failed to convert migration history")
- }
- migrationHistoryList = append(migrationHistoryList, &migrationHistory)
- }
-
- return migrationHistoryList, nil
-}
-
-func convertMigrationHistory(history MigrationHistory) (db.MigrationHistory, error) {
- useSemanticVersion, version, semanticVersionSuffix, err := util.FromStoredVersion(history.Version)
- if err != nil {
- return db.MigrationHistory{}, err
- }
- return db.MigrationHistory{
- ID: fmt.Sprintf("%d", history.ID),
- Creator: history.CreatedBy,
- CreatedTs: int64(history.CreatedTs.T),
- Updater: history.UpdatedBy,
- UpdatedTs: int64(history.UpdatedTs.T),
- ReleaseVersion: history.ReleaseVersion,
- Namespace: history.Namespace,
- Sequence: int(history.Sequence),
- Source: db.MigrationSource(history.Source),
- Type: db.MigrationType(history.Type),
- Status: db.MigrationStatus(history.Status),
- Version: version,
- Description: history.Description,
- Statement: history.Statement,
- ExecutionDurationNs: history.ExecutionDurationNs,
- IssueID: history.IssueID,
- Payload: history.Payload,
- UseSemanticVersion: useSemanticVersion,
- SemanticVersionSuffix: semanticVersionSuffix,
- }, nil
-}
diff --git a/backend/plugin/db/mongodb/role.go b/backend/plugin/db/mongodb/role.go
index c51a93f5fa..d782a30bf6 100644
--- a/backend/plugin/db/mongodb/role.go
+++ b/backend/plugin/db/mongodb/role.go
@@ -12,6 +12,11 @@ import (
storepb "github.com/bytebase/bytebase/proto/generated-go/store"
)
+const (
+ // bytebaseDefaultDatabase is the default database name for bytebase.
+ bytebaseDefaultDatabase = "bytebase"
+)
+
// CreateRole creates the role.
func (*Driver) CreateRole(_ context.Context, _ *db.DatabaseRoleUpsertMessage) (*db.DatabaseRoleMessage, error) {
return nil, errors.Errorf("create role for MongoDB is not implemented yet")
@@ -39,7 +44,7 @@ func (*Driver) DeleteRole(_ context.Context, _ string) error {
// getUserList returns the list of users.
func (driver *Driver) getInstanceRoles(ctx context.Context) ([]*storepb.InstanceRoleMetadata, error) {
- database := driver.client.Database(migrationHistoryDefaultDatabase)
+ database := driver.client.Database(bytebaseDefaultDatabase)
command := bson.D{{
Key: "usersInfo",
Value: bson.D{{
diff --git a/backend/plugin/db/mongodb/sync.go b/backend/plugin/db/mongodb/sync.go
index 6419efe49a..2723003077 100644
--- a/backend/plugin/db/mongodb/sync.go
+++ b/backend/plugin/db/mongodb/sync.go
@@ -220,7 +220,7 @@ func getIndexes(ctx context.Context, collection *mongo.Collection) ([]*storepb.I
// getVersion returns the version of mongod or mongos instance.
func (driver *Driver) getVersion(ctx context.Context) (string, error) {
- database := driver.client.Database(migrationHistoryDefaultDatabase)
+ database := driver.client.Database(bytebaseDefaultDatabase)
var commandResult bson.M
command := bson.D{{Key: "buildInfo", Value: 1}}
if err := database.RunCommand(ctx, command).Decode(&commandResult); err != nil {
diff --git a/backend/plugin/db/mssql/migrate.go b/backend/plugin/db/mssql/migrate.go
deleted file mode 100644
index 1fc8d267b8..0000000000
--- a/backend/plugin/db/mssql/migrate.go
+++ /dev/null
@@ -1,13 +0,0 @@
-package mssql
-
-import (
- "context"
- "errors"
-
- "github.com/bytebase/bytebase/backend/plugin/db"
-)
-
-// FindMigrationHistoryList finds the migration history list and return most recent item first.
-func (*Driver) FindMigrationHistoryList(context.Context, *db.MigrationHistoryFind) ([]*db.MigrationHistory, error) {
- return nil, errors.New("unsupported")
-}
diff --git a/backend/plugin/db/oracle/migrate.go b/backend/plugin/db/oracle/migrate.go
index 668f292935..d1976efb23 100644
--- a/backend/plugin/db/oracle/migrate.go
+++ b/backend/plugin/db/oracle/migrate.go
@@ -22,8 +22,3 @@ func (d *Driver) ExecuteMigrationWithBeforeCommitTxFunc(ctx context.Context, m *
}
return "", "", nil
}
-
-// FindMigrationHistoryList finds the migration history list and return most recent item first.
-func (*Driver) FindMigrationHistoryList(context.Context, *db.MigrationHistoryFind) ([]*db.MigrationHistory, error) {
- return nil, errors.New("unsupported")
-}
diff --git a/backend/plugin/db/redis/migrate.go b/backend/plugin/db/redis/migrate.go
deleted file mode 100644
index e245b70dae..0000000000
--- a/backend/plugin/db/redis/migrate.go
+++ /dev/null
@@ -1,14 +0,0 @@
-package redis
-
-import (
- "context"
-
- "github.com/pkg/errors"
-
- "github.com/bytebase/bytebase/backend/plugin/db"
-)
-
-// FindMigrationHistoryList finds the migration history list and return most recent item first.
-func (*Driver) FindMigrationHistoryList(context.Context, *db.MigrationHistoryFind) ([]*db.MigrationHistory, error) {
- return nil, errors.New("redis: not supported")
-}
diff --git a/backend/plugin/db/redshift/migrate.go b/backend/plugin/db/redshift/migrate.go
deleted file mode 100644
index 7c5ffef245..0000000000
--- a/backend/plugin/db/redshift/migrate.go
+++ /dev/null
@@ -1,13 +0,0 @@
-package redshift
-
-import (
- "context"
- "errors"
-
- "github.com/bytebase/bytebase/backend/plugin/db"
-)
-
-// FindMigrationHistoryList finds the migration history list and return most recent item first.
-func (*Driver) FindMigrationHistoryList(context.Context, *db.MigrationHistoryFind) ([]*db.MigrationHistory, error) {
- return nil, errors.New("unsupported")
-}
diff --git a/backend/plugin/db/snowflake/migrate.go b/backend/plugin/db/snowflake/migrate.go
deleted file mode 100644
index 551615554b..0000000000
--- a/backend/plugin/db/snowflake/migrate.go
+++ /dev/null
@@ -1,63 +0,0 @@
-package snowflake
-
-import (
- "context"
- "fmt"
-
- "github.com/bytebase/bytebase/backend/plugin/db"
- "github.com/bytebase/bytebase/backend/plugin/db/util"
-)
-
-// FindMigrationHistoryList finds the migration history.
-func (driver *Driver) FindMigrationHistoryList(ctx context.Context, find *db.MigrationHistoryFind) ([]*db.MigrationHistory, error) {
- baseQuery := `
- SELECT
- id,
- created_by,
- created_ts,
- updated_by,
- updated_ts,
- release_version,
- namespace,
- sequence,
- source,
- type,
- status,
- version,
- description,
- statement,
- schema,
- schema_prev,
- execution_duration_ns,
- issue_id,
- payload
- FROM bytebase.public.migration_history `
- paramNames, params := []string{}, []any{}
- if v := find.ID; v != nil {
- paramNames, params = append(paramNames, "id"), append(params, *v)
- }
- if v := find.Database; v != nil {
- paramNames, params = append(paramNames, "namespace"), append(params, *v)
- }
- if v := find.Version; v != nil {
- // TODO(d): support semantic versioning.
- storedVersion, err := util.ToStoredVersion(false, *v, "")
- if err != nil {
- return nil, err
- }
- paramNames, params = append(paramNames, "version"), append(params, storedVersion)
- }
- if v := find.Source; v != nil {
- paramNames, params = append(paramNames, "source"), append(params, *v)
- }
- var query = baseQuery +
- db.FormatParamNameInQuestionMark(paramNames) +
- `ORDER BY id DESC`
- 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/plugin/db/spanner/migrate.go b/backend/plugin/db/spanner/migrate.go
deleted file mode 100644
index f9cda6fffa..0000000000
--- a/backend/plugin/db/spanner/migrate.go
+++ /dev/null
@@ -1,164 +0,0 @@
-package spanner
-
-import (
- "context"
- "fmt"
- "strings"
-
- "cloud.google.com/go/spanner"
- "cloud.google.com/go/spanner/admin/database/apiv1/databasepb"
- "go.uber.org/zap"
-
- "github.com/bytebase/bytebase/backend/common/log"
- "github.com/bytebase/bytebase/backend/plugin/db"
- "github.com/bytebase/bytebase/backend/plugin/db/util"
-
- "google.golang.org/api/iterator"
- "google.golang.org/grpc/codes"
- "google.golang.org/grpc/status"
-)
-
-func (d *Driver) notFoundDatabase(ctx context.Context, databaseName string) (bool, error) {
- dsn := getDSN(d.config.Host, databaseName)
- _, err := d.dbClient.GetDatabase(ctx, &databasepb.GetDatabaseRequest{Name: dsn})
- if status.Code(err) == codes.NotFound {
- return true, nil
- }
- if err != nil {
- return false, err
- }
- return false, nil
-}
-
-func (d *Driver) creataDatabase(ctx context.Context, createStatement string, extraStatement []string) error {
- op, err := d.dbClient.CreateDatabase(ctx, &databasepb.CreateDatabaseRequest{
- Parent: d.config.Host,
- CreateStatement: createStatement,
- ExtraStatements: extraStatement,
- })
- if err != nil {
- return err
- }
- if _, err := op.Wait(ctx); err != nil {
- return err
- }
- return nil
-}
-
-// FindMigrationHistoryList finds the migration history list.
-func (d *Driver) FindMigrationHistoryList(ctx context.Context, find *db.MigrationHistoryFind) ([]*db.MigrationHistory, error) {
- defer func(db string) {
- if err := d.switchDatabase(ctx, db); err != nil {
- log.Error("failed to switch back database for spanner driver", zap.String("database", db), zap.Error(err))
- }
- }(d.dbName)
- if err := d.switchDatabase(ctx, db.BytebaseDatabase); err != nil {
- return nil, err
- }
- query := `
- SELECT
- id,
- created_by,
- created_ts,
- updated_by,
- updated_ts,
- release_version,
- namespace,
- sequence,
- source,
- type,
- status,
- version,
- description,
- statement,
- schema,
- schema_prev,
- execution_duration_ns,
- issue_id,
- payload
- FROM migration_history
- `
- params := make(map[string]any)
- var where []string
-
- if v := find.ID; v != nil {
- where = append(where, `id = @id`)
- params["id"] = *v
- }
- if v := find.Database; v != nil {
- where = append(where, `namespace = @namespace`)
- params["namespace"] = *v
- }
- if v := find.Source; v != nil {
- where = append(where, `source = @source`)
- params["source"] = *v
- }
- if v := find.Version; v != nil {
- // TODO(d): support semantic versioning.
- storedVersion, err := util.ToStoredVersion(false, *v, "")
- if err != nil {
- return nil, err
- }
- where = append(where, "version = @version")
- params["version"] = storedVersion
- }
- if len(where) != 0 {
- query = fmt.Sprintf("%s WHERE %s", query, strings.Join(where, " AND "))
- }
- query = fmt.Sprintf("%s ORDER BY namespace, sequence DESC", query)
- if v := find.Limit; v != nil {
- query += fmt.Sprintf(" LIMIT %d", *v)
- }
-
- stmt := spanner.Statement{
- SQL: query,
- Params: params,
- }
-
- var migrationHistoryList []*db.MigrationHistory
- iter := d.client.Single().Query(ctx, stmt)
- for {
- row, err := iter.Next()
- if err == iterator.Done {
- break
- }
- if err != nil {
- return nil, err
- }
- var history db.MigrationHistory
- var storedVersion string
- var sequence int64
- if err := row.Columns(
- &history.ID,
- &history.Creator,
- &history.CreatedTs,
- &history.Updater,
- &history.UpdatedTs,
- &history.ReleaseVersion,
- &history.Namespace,
- &sequence,
- &history.Source,
- &history.Type,
- &history.Status,
- &storedVersion,
- &history.Description,
- &history.Statement,
- &history.Schema,
- &history.SchemaPrev,
- &history.ExecutionDurationNs,
- &history.IssueID,
- &history.Payload,
- ); err != nil {
- return nil, err
- }
- history.Sequence = int(sequence)
- useSemanticVersion, version, semanticVersionSuffix, err := util.FromStoredVersion(storedVersion)
- if err != nil {
- return nil, err
- }
- history.UseSemanticVersion, history.Version, history.SemanticVersionSuffix = useSemanticVersion, version, semanticVersionSuffix
- migrationHistoryList = append(migrationHistoryList, &history)
- }
-
- return migrationHistoryList, nil
-}
diff --git a/backend/plugin/db/spanner/spanner.go b/backend/plugin/db/spanner/spanner.go
index 1a9a558304..fd9fbdfa00 100644
--- a/backend/plugin/db/spanner/spanner.go
+++ b/backend/plugin/db/spanner/spanner.go
@@ -94,23 +94,6 @@ func (d *Driver) Open(ctx context.Context, _ db.Type, config db.ConnectionConfig
return d, nil
}
-func (d *Driver) switchDatabase(ctx context.Context, dbName string) error {
- if d.dbName == dbName {
- return nil
- }
- if d.client != nil {
- d.client.Close()
- }
- dsn := getDSN(d.config.Host, dbName)
- client, err := spanner.NewClient(ctx, dsn, option.WithCredentialsJSON([]byte(d.config.Password)))
- if err != nil {
- return err
- }
- d.client = client
- d.dbName = dbName
- return nil
-}
-
// Close closes the driver.
func (d *Driver) Close(_ context.Context) error {
if d.client != nil {
@@ -208,6 +191,21 @@ func (d *Driver) Execute(ctx context.Context, statement string, createDatabase b
return rowCount, nil
}
+func (d *Driver) creataDatabase(ctx context.Context, createStatement string, extraStatement []string) error {
+ op, err := d.dbClient.CreateDatabase(ctx, &databasepb.CreateDatabaseRequest{
+ Parent: d.config.Host,
+ CreateStatement: createStatement,
+ ExtraStatements: extraStatement,
+ })
+ if err != nil {
+ return err
+ }
+ if _, err := op.Wait(ctx); err != nil {
+ return err
+ }
+ return nil
+}
+
// QueryConn querys statements.
func (d *Driver) QueryConn(ctx context.Context, _ *sql.Conn, statement string, queryContext *db.QueryContext) ([]any, error) {
stmts, err := sanitizeSQL(statement)
diff --git a/backend/plugin/db/spanner/sync.go b/backend/plugin/db/spanner/sync.go
index bc305296f5..77155e5e57 100644
--- a/backend/plugin/db/spanner/sync.go
+++ b/backend/plugin/db/spanner/sync.go
@@ -8,6 +8,8 @@ import (
"cloud.google.com/go/spanner"
"cloud.google.com/go/spanner/admin/database/apiv1/databasepb"
"google.golang.org/api/iterator"
+ "google.golang.org/grpc/codes"
+ "google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/wrapperspb"
"github.com/pkg/errors"
@@ -104,6 +106,18 @@ func (d *Driver) SyncDBSchema(ctx context.Context, databaseName string) (*storep
return databaseMetadata, err
}
+func (d *Driver) notFoundDatabase(ctx context.Context, databaseName string) (bool, error) {
+ dsn := getDSN(d.config.Host, databaseName)
+ _, err := d.dbClient.GetDatabase(ctx, &databasepb.GetDatabaseRequest{Name: dsn})
+ if status.Code(err) == codes.NotFound {
+ return true, nil
+ }
+ if err != nil {
+ return false, err
+ }
+ return false, nil
+}
+
func getTable(ctx context.Context, tx *spanner.ReadOnlyTransaction) (map[string][]*storepb.TableMetadata, error) {
columnMap, err := getColumn(ctx, tx)
if err != nil {
diff --git a/backend/plugin/db/sqlite/migrate.go b/backend/plugin/db/sqlite/migrate.go
deleted file mode 100644
index efcad289b8..0000000000
--- a/backend/plugin/db/sqlite/migrate.go
+++ /dev/null
@@ -1,60 +0,0 @@
-package sqlite
-
-import (
- "context"
- "fmt"
-
- "github.com/bytebase/bytebase/backend/plugin/db"
- "github.com/bytebase/bytebase/backend/plugin/db/util"
-)
-
-// FindMigrationHistoryList finds the migration history.
-func (driver *Driver) FindMigrationHistoryList(ctx context.Context, find *db.MigrationHistoryFind) ([]*db.MigrationHistory, error) {
- baseQuery := `
- SELECT
- id,
- created_by,
- created_ts,
- updated_by,
- updated_ts,
- release_version,
- namespace,
- sequence,
- source,
- type,
- status,
- version,
- description,
- statement,
- schema,
- schema_prev,
- execution_duration_ns,
- issue_id,
- payload
- FROM bytebase_migration_history `
- paramNames, params := []string{}, []any{}
- if v := find.ID; v != nil {
- paramNames, params = append(paramNames, "id"), append(params, *v)
- }
- if v := find.Database; v != nil {
- paramNames, params = append(paramNames, "namespace"), append(params, *v)
- }
- if v := find.Version; v != nil {
- // TODO(d): support semantic versioning.
- storedVersion, err := util.ToStoredVersion(false, *v, "")
- if err != nil {
- return nil, err
- }
- paramNames, params = append(paramNames, "version"), append(params, storedVersion)
- }
- if v := find.Source; v != nil {
- paramNames, params = append(paramNames, "source"), append(params, *v)
- }
- var query = baseQuery +
- db.FormatParamNameInQuestionMark(paramNames) +
- `ORDER BY id DESC`
- if v := find.Limit; v != nil {
- query += fmt.Sprintf(" LIMIT %d", *v)
- }
- return util.FindMigrationHistoryList(ctx, query, params, driver, bytebaseDatabase)
-}
diff --git a/backend/server/server.go b/backend/server/server.go
index e4b136f6c4..e74c31fbca 100644
--- a/backend/server/server.go
+++ b/backend/server/server.go
@@ -76,13 +76,13 @@ import (
storepb "github.com/bytebase/bytebase/proto/generated-go/store"
// Register clickhouse driver.
- _ "github.com/bytebase/bytebase/backend/plugin/db/clickhouse"
+ "github.com/bytebase/bytebase/backend/plugin/db/clickhouse"
"github.com/bytebase/bytebase/backend/plugin/db/util"
// Register mysql driver.
- _ "github.com/bytebase/bytebase/backend/plugin/db/mysql"
+ "github.com/bytebase/bytebase/backend/plugin/db/mysql"
// Register postgres driver.
- _ "github.com/bytebase/bytebase/backend/plugin/db/pg"
+ "github.com/bytebase/bytebase/backend/plugin/db/pg"
// Register snowflake driver.
_ "github.com/bytebase/bytebase/backend/plugin/db/snowflake"
// Register sqlite driver.
@@ -1178,7 +1178,7 @@ func (s *Server) backfillInstanceChangeHistory(ctx context.Context) {
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 || instance.Engine == db.MSSQL {
+ if !(instance.Engine == db.MySQL || instance.Engine == db.Postgres || instance.Engine == db.ClickHouse) {
return nil
}
if instanceMigrated[instance.UID] {
@@ -1201,13 +1201,37 @@ func (s *Server) backfillInstanceChangeHistory(ctx context.Context) {
defer driver.Close(ctx)
for {
- history, err := driver.FindMigrationHistoryList(ctx, &db.MigrationHistoryFind{
- InstanceID: &instance.UID,
- Limit: &limit,
- Offset: &offset,
- })
- if err != nil {
- return err
+ var history []*db.MigrationHistory
+ if instance.Engine == db.MySQL {
+ myDriver := driver.(*mysql.Driver)
+ history, err = myDriver.FindMigrationHistoryList(ctx, &db.MigrationHistoryFind{
+ InstanceID: &instance.UID,
+ Limit: &limit,
+ Offset: &offset,
+ })
+ if err != nil {
+ return err
+ }
+ } else if instance.Engine == db.Postgres {
+ pgDriver := driver.(*pg.Driver)
+ history, err = pgDriver.FindMigrationHistoryList(ctx, &db.MigrationHistoryFind{
+ InstanceID: &instance.UID,
+ Limit: &limit,
+ Offset: &offset,
+ })
+ if err != nil {
+ return err
+ }
+ } else if instance.Engine == db.ClickHouse {
+ cDriver := driver.(*clickhouse.Driver)
+ history, err = cDriver.FindMigrationHistoryList(ctx, &db.MigrationHistoryFind{
+ InstanceID: &instance.UID,
+ Limit: &limit,
+ Offset: &offset,
+ })
+ if err != nil {
+ return err
+ }
}
if len(history) == 0 {
break
diff --git a/backend/store/pg_engine.go b/backend/store/pg_engine.go
index a286274749..4e4e5bdce8 100644
--- a/backend/store/pg_engine.go
+++ b/backend/store/pg_engine.go
@@ -116,13 +116,14 @@ func (db *DB) Open(ctx context.Context) (schemaVersion *semver.Version, err erro
// The database storing metadata is the same as user name.
databaseName = db.connCfg.Username
}
+ pgDriver := d.(*pg.Driver)
if db.readonly {
log.Info("Database is opened in readonly mode. Skip migration and demo data setup.")
// This should be called before d.GetDBConnection because getLatestVersion would call
// FindMigrationHistoryList which would invalidate the existing db connection. See
// https://github.com/bytebase/bytebase/blame/03cd4ef4f31cb74114144ed282e06b0a00aa40d8/plugin/db/util/driverutil.go#L591
- ver, err := getLatestVersion(ctx, d, databaseName)
+ ver, err := getLatestVersion(ctx, pgDriver, databaseName)
if err != nil {
return nil, errors.Wrap(err, "failed to get current schema version")
}
@@ -133,7 +134,6 @@ func (db *DB) Open(ctx context.Context) (schemaVersion *semver.Version, err erro
}
return ver, nil
}
- pgDriver := d.(*pg.Driver)
// We are also using our own migration core to manage our own schema's migration history.
// So here we will create a "bytebase" database to store the migration history if the target
@@ -142,7 +142,7 @@ func (db *DB) Open(ctx context.Context) (schemaVersion *semver.Version, err erro
return nil, err
}
- verBefore, err := getLatestVersion(ctx, d, databaseName)
+ verBefore, err := getLatestVersion(ctx, pgDriver, databaseName)
if err != nil {
return nil, errors.Wrap(err, "failed to get current schema version")
}
@@ -151,7 +151,7 @@ func (db *DB) Open(ctx context.Context) (schemaVersion *semver.Version, err erro
return nil, errors.Wrap(err, "failed to migrate")
}
- verAfter, err := getLatestVersion(ctx, d, databaseName)
+ verAfter, err := getLatestVersion(ctx, pgDriver, databaseName)
if err != nil {
return nil, errors.Wrap(err, "failed to get current schema version")
}
@@ -191,7 +191,7 @@ func (db *DB) Open(ctx context.Context) (schemaVersion *semver.Version, err erro
// getLatestVersion returns the latest schema version in semantic versioning format.
// We expect our own migration history to use semantic versions.
// If there's no migration history, version will be nil.
-func getLatestVersion(ctx context.Context, d dbdriver.Driver, database string) (*semver.Version, error) {
+func getLatestVersion(ctx context.Context, d *pg.Driver, database string) (*semver.Version, error) {
// We look back the past migration history records and return the latest successful (DONE) migration version.
history, err := d.FindMigrationHistoryList(ctx, &dbdriver.MigrationHistoryFind{
Database: &database,