Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
chore: refactor migrator (#15493)
Danny Xuc3b2bdf4f5d0
diff --git a/backend/migrator/migrator.go b/backend/migrator/migrator.goindex 4886a62274..0e7fb156ad 100644--- a/backend/migrator/migrator.go+++ b/backend/migrator/migrator.go@@ -16,19 +16,14 @@ import ("github.com/blang/semver/v4""github.com/pkg/errors"-- "github.com/bytebase/bytebase/backend/common"- "github.com/bytebase/bytebase/backend/common/log"- dbdriver "github.com/bytebase/bytebase/backend/plugin/db"- "github.com/bytebase/bytebase/backend/store")//go:embed migrationvar migrationFS embed.FS// MigrateSchema migrates the schema for metadata database.-func MigrateSchema(ctx context.Context, stores *store.Store) (*semver.Version, error) {- conn, err := stores.GetDB().Conn(ctx)+func MigrateSchema(ctx context.Context, db *sql.DB) (*semver.Version, error) {+ conn, err := db.Conn(ctx)if err != nil {return nil, err}@@ -44,7 +39,7 @@ func MigrateSchema(ctx context.Context, stores *store.Store) (*semver.Version, ereturn nil, errors.Wrapf(err, "failed to get cutoff version")}slog.Info(fmt.Sprintf("The prod cutoff schema version: %s", cutoffSchemaVersion))- if err := initializeSchema(ctx, stores, conn, cutoffSchemaVersion); err != nil {+ if err := initializeSchema(ctx, conn, cutoffSchemaVersion); err != nil {return nil, err}var c int@@ -91,25 +86,25 @@ func MigrateSchema(ctx context.Context, stores *store.Store) (*semver.Version, ereturn nil, err}- verBefore, err := getLatestVersion(ctx, stores)+ verBefore, err := getLatestMigrationVersion(ctx, conn)if err != nil {return nil, errors.Wrap(err, "failed to get current schema version")}- if _, err := migrate(ctx, stores, conn, verBefore); err != nil {+ if _, err := migrate(ctx, conn, *verBefore); err != nil {return nil, errors.Wrap(err, "failed to migrate")}- verAfter, err := getLatestVersion(ctx, stores)+ verAfter, err := getLatestMigrationVersion(ctx, conn)if err != nil {return nil, errors.Wrap(err, "failed to get current schema version")}slog.Info(fmt.Sprintf("Current schema version after migration: %s", verAfter))- return &verAfter, nil+ return verAfter, nil}-func initializeSchema(ctx context.Context, storeInstance *store.Store, conn *sql.Conn, cutoffSchemaVersion semver.Version) error {+func initializeSchema(ctx context.Context, conn *sql.Conn, cutoffSchemaVersion semver.Version) error {// We use environment table to determine whether we've initialized the schema.var exists boolif err := conn.QueryRowContext(ctx,@@ -127,7 +122,6 @@ func initializeSchema(ctx context.Context, storeInstance *store.Store, conn *sqlif err != nil {return errors.Wrapf(err, "failed to read latest schema %q", latestSchemaPath)}- stmt := string(buf)version := cutoffSchemaVersion.String()// Set role to database owner so that the schema owner and database owner are consistent.@@ -138,14 +132,8 @@ func initializeSchema(ctx context.Context, storeInstance *store.Store, conn *sqlif _, err := conn.ExecContext(ctx, fmt.Sprintf("SET ROLE '%s'", owner)); err != nil {return err}- if _, err := conn.ExecContext(ctx, stmt); err != nil {- return err- }- if err := storeInstance.CreateInstanceChangeHistoryForMigrator(ctx, &store.InstanceChangeHistoryMessage{- Status: dbdriver.Done,- Version: version,- ExecutionDurationNs: 0,- }); err != nil {++ if err := executeMigration(ctx, conn, string(buf), version); err != nil {return err}slog.Info(fmt.Sprintf("Completed database initial migration with version %s.", cutoffSchemaVersion))@@ -196,54 +184,11 @@ func backfillSchemaObjectOwner(ctx context.Context, conn *sql.Conn) error {return nil}-// 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, storeInstance *store.Store) (semver.Version, error) {- // We look back the past migration history records and return the latest successful (DONE) migration version.- histories, err := storeInstance.ListInstanceChangeHistoryForMigrator(ctx, &store.FindInstanceChangeHistoryMessage{})- if err != nil {- return semver.Version{}, errors.Wrap(err, "failed to get migration history")- }- if len(histories) == 0 {- return semver.Version{}, errors.Errorf("migration history should exist for metadata database")- }-- for _, h := range histories {- if h.Status != dbdriver.Done {- // Non-success migration history record is an anomaly, in the case where the actual- // migration has been applied, the followup migration will likely fail because the- // schema has already been applied. Thus emitting a warning here will assist debugging.- slog.Warn("Found stale migration history",- slog.String("status", string(h.Status)),- slog.String("version", h.Version),- )- continue- }- v, err := semver.Make(h.Version)- if err != nil {- return semver.Version{}, errors.Wrapf(err, "invalid version %q", h.Version)- }- return v, nil- }-- return semver.Version{}, errors.Errorf("failed to find a successful migration history to determine the schema version")-}-const (latestSchemaFile = "LATEST.sql")-// migrate sets up migration tracking and executes pending migration files.-//-// Migration files are embedded in the migration folder and are executed-// in lexicographical order.-//-// We prepend each migration file with version = xxx; Each migration-// file run in a transaction to prevent partial migrations.-//-// The procedure follows https://github.com/bytebase/bytebase/blob/main/docs/schema-update-guide.md.-func migrate(ctx context.Context, stores *store.Store, conn *sql.Conn, curVer semver.Version) (bool, error) {+func migrate(ctx context.Context, conn *sql.Conn, curVer semver.Version) (bool, error) {slog.Info("Apply database migration if needed...")slog.Info(fmt.Sprintf("Current schema version before migration: %s", curVer))@@ -277,7 +222,7 @@ func migrate(ctx context.Context, stores *store.Store, conn *sql.Conn, curVer se}slog.Info(fmt.Sprintf("Migrating %s...", pv.version))version := pv.version.String()- if _, _, err := executeMigrationDefault(ctx, stores, conn, string(buf), version); err != nil {+ if err := executeMigration(ctx, conn, string(buf), version); err != nil {return false, err}retVersion = pv.version@@ -410,84 +355,44 @@ func getMinorVersions(names []string) ([]semver.Version, error) {return versions, nil}-// executeMigrationDefault executes migration.-func executeMigrationDefault(ctx context.Context, stores *store.Store, conn *sql.Conn, statement string, version string) (migrationHistoryID string, updatedSchema string, resErr error) {- insertedID, err := beginMigration(ctx, stores, version)- if err != nil {- return "", "", errors.Wrapf(err, "failed to begin migration")- }-+func executeMigration(ctx context.Context, conn *sql.Conn, statement string, version string) error {startedNs := time.Now().UnixNano()- defer func() {- if err := endMigration(ctx, stores, startedNs, insertedID, resErr == nil /* isDone */); err != nil {- slog.Error("Failed to update migration history record",- log.BBError(err),- slog.String("migration_id", migrationHistoryID),- )- }- }()+ txn, err := conn.BeginTx(ctx, nil)+ if err != nil {+ return err+ }+ defer txn.Rollback()- if _, err := conn.ExecContext(ctx, statement); err != nil {- return "", "", err+ if _, err := txn.ExecContext(ctx, statement); err != nil {+ return err+ }+ durationNano := time.Now().UnixNano() - startedNs+ if _, err := txn.ExecContext(ctx,+ `INSERT INTO instance_change_history (status, version, execution_duration_ns) VALUES ($1, $2, $3)`, "DONE",+ version,+ durationNano,+ ); err != nil {+ return err}- return insertedID, "", nil+ return txn.Commit()}-// beginMigration checks before executing migration and inserts a migration history record with pending status.-func beginMigration(ctx context.Context, stores *store.Store, version string) (string, error) {- // Phase 1 - Pre-check before executing migration- // Check if the same migration version has already been applied.- if list, err := stores.ListInstanceChangeHistoryForMigrator(ctx, &store.FindInstanceChangeHistoryMessage{- Version: &version,- }); err != nil {- return "", errors.Wrap(err, "failed to check duplicate version")- } else if len(list) > 0 {- migrationHistory := list[0]- switch migrationHistory.Status {- case dbdriver.Done:- err := common.Errorf(common.MigrationAlreadyApplied, "already applied version %s, hint: the version might be duplicate, please check the version", version)- slog.Debug(err.Error())- // Force migration- return migrationHistory.UID, nil- case dbdriver.Pending:- err := errors.Errorf("version %s migration is already in progress", version)- slog.Debug(err.Error())- // For force migration, we will ignore the existing migration history and continue to migration.- return migrationHistory.UID, nil- case dbdriver.Failed:- err := errors.Errorf("version %s migration has failed, please check your database to make sure things are fine and then start a new migration using a new version", version)- slog.Debug(err.Error())- // For force migration, we will ignore the existing migration history and continue to migration.- return migrationHistory.UID, nil+func getLatestMigrationVersion(ctx context.Context, conn *sql.Conn) (*semver.Version, error) {+ query := `SELECT version FROM instance_change_history WHERE status = 'DONE' ORDER BY id DESC`++ var v string+ if err := conn.QueryRowContext(ctx, query).Scan(&v); err != nil {+ if err == sql.ErrNoRows {+ return nil, nil}+ return nil, err}- // Phase 2 - Record migration history as PENDING.- insertedID, err := stores.CreatePendingInstanceChangeHistoryForMigrator(ctx, version)+ version, err := semver.Make(v)if err != nil {- return "", err+ return nil, errors.Wrapf(err, "invalid version %q", v)}-- return insertedID, nil-}--// endMigration updates the migration history record to DONE or FAILED depending on migration is done or not.-func endMigration(ctx context.Context, storeInstance *store.Store, startedNs int64, insertedID string, isDone bool) error {- migrationDurationNs := time.Now().UnixNano() - startedNs- update := &store.UpdateInstanceChangeHistoryMessage{- ID: insertedID,- ExecutionDurationNs: &migrationDurationNs,- }- if isDone {- // Upon success, update the migration history as 'DONE', execution_duration_ns, updated schema.- status := dbdriver.Done- update.Status = &status- } else {- // Otherwise, update the migration history as 'FAILED', execution_duration.- status := dbdriver.Failed- update.Status = &status- }- return storeInstance.UpdateInstanceChangeHistory(ctx, update)+ return &version, nil}diff --git a/backend/migrator/migrator_test.go b/backend/migrator/migrator_test.goindex 198019c522..042059a397 100644--- a/backend/migrator/migrator_test.go+++ b/backend/migrator/migrator_test.go@@ -1,19 +1,13 @@package migratorimport (- "context"- "database/sql""fmt"- "path""testing""github.com/blang/semver/v4""github.com/stretchr/testify/require"- "github.com/bytebase/bytebase/backend/common"_ "github.com/bytebase/bytebase/backend/plugin/db/pg"- "github.com/bytebase/bytebase/backend/resources/postgres"- "github.com/bytebase/bytebase/backend/store")func TestGetMinorMigrationVersions(t *testing.T) {@@ -136,52 +130,6 @@ func TestGetPatchVersions(t *testing.T) {}}-var (- pgPort = 6000-)--func TestMigrationCompatibility(t *testing.T) {- pgDir := t.TempDir()-- pgBinDir, err := postgres.Install(path.Join(pgDir, "resource"))- require.NoError(t, err)-- stopInstance := postgres.SetupTestInstance(pgBinDir, t.TempDir(), pgPort)- defer stopInstance()-- ctx := context.Background()- pgURL := fmt.Sprintf("host=%s port=%d user=%s database=postgres", common.GetPostgresSocketDir(), pgPort, postgres.TestPgUser)- db, err := sql.Open("pgx", pgURL)- require.NoError(t, err)- defer db.Close()- conn, err := db.Conn(ctx)- require.NoError(t, err)- defer conn.Close()-- stores, err := store.New(ctx, pgURL)- require.NoError(t, err)-- releaseVersion, err := getProdCutoffVersion()- require.NoError(t, err)-- // Create initial schema.- err = initializeSchema(ctx, stores, conn, releaseVersion)- require.NoError(t, err)- // Check migration history.- histories, err := stores.ListInstanceChangeHistoryForMigrator(ctx, &store.FindInstanceChangeHistoryMessage{})- require.NoError(t, err)- require.Len(t, histories, 1)- require.Equal(t, histories[0].Version, releaseVersion.String())-- // Check no migration after passing current version as the release cutoff version.- _, err = migrate(ctx, stores, conn, releaseVersion)- require.NoError(t, err)- // Check migration history.- histories, err = stores.ListInstanceChangeHistoryForMigrator(ctx, &store.FindInstanceChangeHistoryMessage{})- require.NoError(t, err)- require.Len(t, histories, 1)-}-func TestGetCutoffVersion(t *testing.T) {releaseVersion, err := getProdCutoffVersion()require.NoError(t, err)diff --git a/backend/server/server.go b/backend/server/server.goindex deaba2fc4f..f9ddadcd40 100644--- a/backend/server/server.go+++ b/backend/server/server.go@@ -169,7 +169,7 @@ func NewServer(ctx context.Context, profile *config.Profile) (*Server, error) {if err := demo.LoadDemoDataIfNeeded(ctx, stores, profile.DemoName); err != nil {return nil, errors.Wrapf(err, "failed to load demo data")}- if _, err := migrator.MigrateSchema(ctx, stores); err != nil {+ if _, err := migrator.MigrateSchema(ctx, stores.GetDB()); err != nil {return nil, err}}diff --git a/backend/store/instance_change_history.go b/backend/store/instance_change_history.godeleted file mode 100644index da3c3027b7..0000000000--- a/backend/store/instance_change_history.go+++ /dev/null@@ -1,181 +0,0 @@-package store--import (- "context"- "database/sql"- "fmt"- "strings"-- "github.com/bytebase/bytebase/backend/plugin/db"-)--// InstanceChangeHistoryMessage records the change history of an instance.-// it deprecates the old MigrationHistory.-type InstanceChangeHistoryMessage struct {- Status db.MigrationStatus- Version string- ExecutionDurationNs int64-- // Output only- UID string-}--// FindInstanceChangeHistoryMessage is for listing a list of instance change history.-type FindInstanceChangeHistoryMessage struct {- Version *string-}--// UpdateInstanceChangeHistoryMessage is for updating an instance change history.-type UpdateInstanceChangeHistoryMessage struct {- ID string-- Status *db.MigrationStatus- ExecutionDurationNs *int64-}--// CreateInstanceChangeHistoryForMigrator creates an instance change history for migrator.-func (s *Store) CreateInstanceChangeHistoryForMigrator(ctx context.Context, create *InstanceChangeHistoryMessage) error {- tx, err := s.db.BeginTx(ctx, nil)- if err != nil {- return err- }- defer tx.Rollback()-- if _, err := s.createInstanceChangeHistoryImplForMigrator(ctx, tx, create); err != nil {- return err- }- return tx.Commit()-}--// CreatePendingInstanceChangeHistory creates an instance change history.-func (s *Store) CreatePendingInstanceChangeHistoryForMigrator(ctx context.Context, version string) (string, error) {- tx, err := s.db.BeginTx(ctx, nil)- if err != nil {- return "", err- }- defer tx.Rollback()-- instanceChange := &InstanceChangeHistoryMessage{- Status: db.Pending,- Version: version,- ExecutionDurationNs: 0,- }- var uid string- id, err := s.createInstanceChangeHistoryImplForMigrator(ctx, tx, instanceChange)- if err != nil {- return "", err- }- uid = id-- if err := tx.Commit(); err != nil {- return "", err- }-- return uid, nil-}--func (*Store) createInstanceChangeHistoryImplForMigrator(ctx context.Context, txn *sql.Tx, create *InstanceChangeHistoryMessage) (string, error) {- query := `- INSERT INTO instance_change_history (- status,- version,- execution_duration_ns- ) VALUES ($1, $2, $3)- RETURNING id`-- var uid string- if err := txn.QueryRowContext(ctx, query,- create.Status,- create.Version,- create.ExecutionDurationNs,- ).Scan(&uid); err != nil {- return "", err- }-- return uid, nil-}--// UpdateInstanceChangeHistory updates an instance change history.-// it deprecates the old UpdateHistoryAsDone and UpdateHistoryAsFailed.-func (s *Store) UpdateInstanceChangeHistory(ctx context.Context, update *UpdateInstanceChangeHistoryMessage) error {- set, args := []string{}, []any{}- if v := update.Status; v != nil {- set, args = append(set, fmt.Sprintf("status = $%d", len(args)+1)), append(args, *v)- }- if v := update.ExecutionDurationNs; v != nil {- set, args = append(set, fmt.Sprintf("execution_duration_ns = $%d", len(args)+1)), append(args, *v)- }- if len(set) == 0 {- return nil- }- query := `- UPDATE instance_change_history- SET ` + strings.Join(set, ", ") + `- WHERE ` + fmt.Sprintf("id = $%d", len(args)+1)- args = append(args, update.ID)-- tx, err := s.db.BeginTx(ctx, nil)- if err != nil {- return err- }- defer tx.Rollback()-- if _, err := tx.ExecContext(ctx, query, args...); err != nil {- return err- }-- return tx.Commit()-}--// ListInstanceChangeHistoryForMigrator finds the instance change history for the migrator.-func (s *Store) ListInstanceChangeHistoryForMigrator(ctx context.Context, find *FindInstanceChangeHistoryMessage) ([]*InstanceChangeHistoryMessage, error) {- where, args := []string{"TRUE"}, []any{}- if v := find.Version; v != nil {- where, args = append(where, fmt.Sprintf("version = $%d", len(args)+1)), append(args, *v)- }-- // Ordering by ID is enough to ensure the correct version order.- query := `- SELECT- id,- status,- version,- execution_duration_ns- FROM instance_change_history- WHERE ` + strings.Join(where, " AND ") + ` ORDER BY id DESC`-- tx, err := s.db.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})- if err != nil {- return nil, err- }- defer tx.Rollback()-- rows, err := tx.QueryContext(ctx, query, args...)- if err != nil {- return nil, err- }- defer rows.Close()-- var list []*InstanceChangeHistoryMessage- for rows.Next() {- var changeHistory InstanceChangeHistoryMessage- if err := rows.Scan(- &changeHistory.UID,- &changeHistory.Status,- &changeHistory.Version,- &changeHistory.ExecutionDurationNs,- ); err != nil {- return nil, err- }- list = append(list, &changeHistory)- }- if err := rows.Err(); err != nil {- return nil, err- }-- if err := tx.Commit(); err != nil {- return nil, err- }-- return list, nil-}