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(db-plugin): better error msg on duplicate version (#828)
qsliuedac56824565
diff --git a/common/error.go b/common/error.goindex d96277d61a..e870c79bd4 100644--- a/common/error.go+++ b/common/error.go@@ -29,6 +29,8 @@ const (MigrationAlreadyApplied Code = 202MigrationOutOfOrder Code = 203MigrationBaselineMissing Code = 204+ MigrationPending Code = 205+ MigrationFailed Code = 206// 301 task errorTaskTimingNotAllowed Code = 301diff --git a/plugin/db/clickhouse/clickhouse.go b/plugin/db/clickhouse/clickhouse.goindex 3cc826c14e..c220132ea9 100644--- a/plugin/db/clickhouse/clickhouse.go+++ b/plugin/db/clickhouse/clickhouse.go@@ -407,26 +407,6 @@ func (driver *Driver) SetupMigrationIfNeeded(ctx context.Context) error {return nil}-// CheckDuplicateVersion will check whether the version is already applied.-func (Driver) CheckDuplicateVersion(ctx context.Context, tx *sql.Tx, namespace string, source db.MigrationSource, version string) (bool, error) {- const checkDuplicateVersionQuery = `- SELECT 1 FROM bytebase.migration_history- WHERE namespace = $1 AND source = $2 AND version = $3- `- row, err := tx.QueryContext(ctx, checkDuplicateVersionQuery,- namespace, source.String(), version,- )- if err != nil {- return false, util.FormatErrorWithQuery(err, checkDuplicateVersionQuery)- }- defer row.Close()-- if row.Next() {- return true, nil- }- return false, nil-}-// CheckOutOfOrderVersion will return versions that are higher than the given version.func (Driver) CheckOutOfOrderVersion(ctx context.Context, tx *sql.Tx, namespace string, source db.MigrationSource, version string) (minVersionIfValid *string, err error) {const checkOutofOrderVersionQuery = `@@ -649,6 +629,9 @@ func (driver *Driver) FindMigrationHistoryList(ctx context.Context, find *db.Migif v := find.Version; v != nil {paramNames, params = append(paramNames, "version"), append(params, *v)}+ if v := find.Source; v != nil {+ paramNames, params = append(paramNames, "source"), append(params, *v)+ }var query = baseQuery +db.FormatParamNameInNumberedPosition(paramNames) +`ORDER BY created_ts DESC`diff --git a/plugin/db/driver.go b/plugin/db/driver.goindex 2bd76d5177..db948eeed3 100644--- a/plugin/db/driver.go+++ b/plugin/db/driver.go@@ -347,6 +347,7 @@ type MigrationHistoryFind struct {ID *intDatabase *string+ Source *MigrationSourceVersion *string// If specified, then it will only fetch "Limit" most recent migration historiesLimit *intdiff --git a/plugin/db/mysql/mysql.go b/plugin/db/mysql/mysql.goindex 3032b44b3f..145c50203b 100644--- a/plugin/db/mysql/mysql.go+++ b/plugin/db/mysql/mysql.go@@ -582,26 +582,6 @@ func (driver *Driver) SetupMigrationIfNeeded(ctx context.Context) error {return nil}-// CheckDuplicateVersion will check whether the version is already applied.-func (Driver) CheckDuplicateVersion(ctx context.Context, tx *sql.Tx, namespace string, source db.MigrationSource, version string) (bool, error) {- const checkDuplicateVersionQuery = `- SELECT 1 FROM bytebase.migration_history- WHERE namespace = ? AND source = ? AND version = ?- `- row, err := tx.QueryContext(ctx, checkDuplicateVersionQuery,- namespace, source.String(), version,- )- if err != nil {- return false, util.FormatErrorWithQuery(err, checkDuplicateVersionQuery)- }- defer row.Close()-- if row.Next() {- return true, nil- }- return false, nil-}-// CheckOutOfOrderVersion will return versions that are higher than the given version.func (Driver) CheckOutOfOrderVersion(ctx context.Context, tx *sql.Tx, namespace string, source db.MigrationSource, version string) (minVersionIfValid *string, err error) {const checkOutOfOrderVersionQuery = `@@ -803,6 +783,9 @@ func (driver *Driver) FindMigrationHistoryList(ctx context.Context, find *db.Migif v := find.Version; v != nil {paramNames, params = append(paramNames, "version"), append(params, *v)}+ if v := find.Source; v != nil {+ paramNames, params = append(paramNames, "source"), append(params, *v)+ }var query = baseQuery +db.FormatParamNameInQuestionMark(paramNames) +`ORDER BY created_ts DESC`diff --git a/plugin/db/pg/pg.go b/plugin/db/pg/pg.goindex 3f5d1baeb6..140d964eb2 100644--- a/plugin/db/pg/pg.go+++ b/plugin/db/pg/pg.go@@ -502,26 +502,6 @@ func (driver *Driver) SetupMigrationIfNeeded(ctx context.Context) error {return nil}-// CheckDuplicateVersion will check whether the version is already applied.-func (Driver) CheckDuplicateVersion(ctx context.Context, tx *sql.Tx, namespace string, source db.MigrationSource, version string) (bool, error) {- const checkDuplicateVersionQuery = `- SELECT 1 FROM migration_history- WHERE namespace = $1 AND source = $2 AND version = $3- `- row, err := tx.QueryContext(ctx, checkDuplicateVersionQuery,- namespace, source.String(), version,- )- if err != nil {- return false, util.FormatErrorWithQuery(err, checkDuplicateVersionQuery)- }- defer row.Close()-- if row.Next() {- return true, nil- }- return false, nil-}-// CheckOutOfOrderVersion will return versions that are higher than the given version.func (Driver) CheckOutOfOrderVersion(ctx context.Context, tx *sql.Tx, namespace string, source db.MigrationSource, version string) (minVersionIfValid *string, err error) {const checkOutofOrderVersionQuery = `@@ -719,6 +699,9 @@ func (driver *Driver) FindMigrationHistoryList(ctx context.Context, find *db.Migif v := find.Version; v != nil {paramNames, params = append(paramNames, "version"), append(params, *v)}+ if v := find.Source; v != nil {+ paramNames, params = append(paramNames, "source"), append(params, *v)+ }var query = baseQuery +db.FormatParamNameInNumberedPosition(paramNames) +`ORDER BY created_ts DESC`diff --git a/plugin/db/snowflake/snowflake.go b/plugin/db/snowflake/snowflake.goindex 07e285740a..c2c292a16f 100644--- a/plugin/db/snowflake/snowflake.go+++ b/plugin/db/snowflake/snowflake.go@@ -583,26 +583,6 @@ func (driver *Driver) SetupMigrationIfNeeded(ctx context.Context) error {return nil}-// CheckDuplicateVersion will check whether the version is already applied.-func (Driver) CheckDuplicateVersion(ctx context.Context, tx *sql.Tx, namespace string, source db.MigrationSource, version string) (bool, error) {- const checkDuplicateVersionQuery = `- SELECT 1 FROM bytebase.public.migration_history- WHERE namespace = ? AND source = ? AND version = ?- `- row, err := tx.QueryContext(ctx, checkDuplicateVersionQuery,- namespace, source.String(), version,- )- if err != nil {- return false, util.FormatErrorWithQuery(err, checkDuplicateVersionQuery)- }- defer row.Close()-- if row.Next() {- return true, nil- }- return false, nil-}-// CheckOutOfOrderVersion will return versions that are higher than the given version.func (Driver) CheckOutOfOrderVersion(ctx context.Context, tx *sql.Tx, namespace string, source db.MigrationSource, version string) (minVersionIfValid *string, err error) {const checkOutofOrderVersionQuery = `@@ -826,6 +806,9 @@ func (driver *Driver) FindMigrationHistoryList(ctx context.Context, find *db.Migif v := find.Version; v != nil {paramNames, params = append(paramNames, "version"), append(params, *v)}+ if v := find.Source; v != nil {+ paramNames, params = append(paramNames, "source"), append(params, *v)+ }var query = baseQuery +db.FormatParamNameInQuestionMark(paramNames) +`ORDER BY created_ts DESC`diff --git a/plugin/db/sqlite/sqlite.go b/plugin/db/sqlite/sqlite.goindex 92e0a0ea8d..99ecd112b8 100644--- a/plugin/db/sqlite/sqlite.go+++ b/plugin/db/sqlite/sqlite.go@@ -417,26 +417,6 @@ func (driver *Driver) SetupMigrationIfNeeded(ctx context.Context) error {return nil}-// CheckDuplicateVersion will check whether the version is already applied.-func (Driver) CheckDuplicateVersion(ctx context.Context, tx *sql.Tx, namespace string, source db.MigrationSource, version string) (bool, error) {- const checkDuplicateVersionQuery = `- SELECT 1 FROM bytebase_migration_history- WHERE namespace = ? AND source = ? AND version = ?- `- row, err := tx.QueryContext(ctx, checkDuplicateVersionQuery,- namespace, source.String(), version,- )- if err != nil {- return false, util.FormatErrorWithQuery(err, checkDuplicateVersionQuery)- }- defer row.Close()-- if row.Next() {- return true, nil- }- return false, nil-}-// CheckOutOfOrderVersion will return versions that are higher than the given version.func (Driver) CheckOutOfOrderVersion(ctx context.Context, tx *sql.Tx, namespace string, source db.MigrationSource, version string) (minVersionIfValid *string, err error) {const checkOutofOrderVersionQuery = `@@ -638,6 +618,9 @@ func (driver *Driver) FindMigrationHistoryList(ctx context.Context, find *db.Migif v := find.Version; v != nil {paramNames, params = append(paramNames, "version"), append(params, *v)}+ if v := find.Source; v != nil {+ paramNames, params = append(paramNames, "source"), append(params, *v)+ }var query = baseQuery +db.FormatParamNameInQuestionMark(paramNames) +`ORDER BY created_ts DESC`diff --git a/plugin/db/util/driverutil.go b/plugin/db/util/driverutil.goindex 890eaba98d..386a2ee679 100644--- a/plugin/db/util/driverutil.go+++ b/plugin/db/util/driverutil.go@@ -112,8 +112,6 @@ func NeedsSetupMigrationSchema(ctx context.Context, sqldb *sql.DB, query string)// MigrationExecutor is an adapter for ExecuteMigration().type MigrationExecutor interface {db.Driver- // CheckDuplicateVersion will check whether the version is already applied.- CheckDuplicateVersion(ctx context.Context, tx *sql.Tx, namespace string, source db.MigrationSource, version string) (isDuplicate bool, err error)// CheckOutOfOrderVersion will return versions that are higher than the given version.CheckOutOfOrderVersion(ctx context.Context, tx *sql.Tx, namespace string, source db.MigrationSource, version string) (minVersionIfValid *string, err error)// FindBaseline retruns true if any baseline is found.@@ -201,10 +199,24 @@ func beginMigration(ctx context.Context, executor MigrationExecutor, m *db.Migra// Phase 1 - Precheck before executing migration// Check if the same migration version has alraedy been applied- if duplicate, err := executor.CheckDuplicateVersion(ctx, tx, m.Namespace, m.Source, m.Version); err != nil {- return -1, err- } else if duplicate {- return -1, common.Errorf(common.MigrationAlreadyApplied, fmt.Errorf("database %q has already applied version %s", m.Database, m.Version))+ if list, err := executor.FindMigrationHistoryList(ctx, &db.MigrationHistoryFind{+ Database: &m.Namespace,+ Source: &m.Source,+ Version: &m.Version,+ }); err != nil {+ return -1, fmt.Errorf("Check duplicate version error: %q", err)+ } else if len(list) > 0 {+ switch list[0].Status {+ case db.Done:+ return -1, common.Errorf(common.MigrationAlreadyApplied,+ fmt.Errorf("database %q has already applied version %s", m.Database, m.Version))+ case db.Pending:+ return -1, common.Errorf(common.MigrationPending,+ fmt.Errorf("database %q version %s migration is already in progress", m.Database, m.Version))+ case db.Failed:+ return -1, common.Errorf(common.MigrationFailed,+ fmt.Errorf("database %q 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 ", m.Database, m.Version))+ }}// Check if there is any higher version already been applied