Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
pg: merge FindMigrationHistoryList to util package.
spinningbot8e66c059a6bf
diff --git a/plugin/db/mysql/mysql.go b/plugin/db/mysql/mysql.goindex 2bb5ec167f..c1639be6e9 100644--- a/plugin/db/mysql/mysql.go+++ b/plugin/db/mysql/mysql.go@@ -508,110 +508,17 @@ func (driver *Driver) ExecuteMigration(ctx context.Context, m *db.MigrationInfo,)VALUES (?, unix_timestamp(), ?, unix_timestamp(), ?, ?, ?, ?, 'PENDING', ?, ?, ?, ?, 0, ?, ?)`- updateHistoryQuery := `- UPDATE bytebase.migration_history SET- ` + "`status` = 'DONE'," + `- ` + "execution_duration = ?," + `- ` + "`schema` = ?" + `- WHERE id = ?- `args := util.MigrationExecutionArgs{DumpTxn: dumpTxn,InsertHistoryQuery: insertHistoryQuery,- UpdateHistoryQuery: updateHistoryQuery,TablePrefix: "bytebase.",}return util.ExecuteMigration(ctx, driver.db, m, statement, args)}func (driver *Driver) FindMigrationHistoryList(ctx context.Context, find *db.MigrationHistoryFind) ([]*db.MigrationHistory, error) {- tx, err := driver.db.BeginTx(ctx, nil)- if err != nil {- return nil, err- }- defer tx.Rollback()-- where, args := []string{"1 = 1"}, []interface{}{}- if v := find.ID; v != nil {- where, args = append(where, "id = ?"), append(args, *v)- }- if v := find.Database; v != nil {- where, args = append(where, "namespace = ?"), append(args, *v)- }- if v := find.Version; v != nil {- where, args = append(where, "version = ?"), append(args, *v)- }-- var query = `- SELECT- id,- created_by,- created_ts,- updated_by,- updated_ts,- namespace,- sequence,- ` + "`engine`," + `- ` + "`type`," + `- ` + "`status`," + `- version,- description,- statement,- ` + "`schema`," + `- execution_duration,- issue_id,- payload- FROM bytebase.migration_history- WHERE ` + strings.Join(where, " AND ") + `- ORDER BY created_ts DESC`- if v := find.Limit; v != nil {- query += fmt.Sprintf(" LIMIT %d", *v)- }-- rows, err := tx.QueryContext(ctx, query, args...)- if err != nil {- return nil, util.FormatErrorWithQuery(err, query)- }- defer rows.Close()-- // Iterate over result set and deserialize rows into list.- list := make([]*db.MigrationHistory, 0)- for rows.Next() {- var history db.MigrationHistory- if err := rows.Scan(- &history.ID,- &history.Creator,- &history.CreatedTs,- &history.Updater,- &history.UpdatedTs,- &history.Namespace,- &history.Sequence,- &history.Engine,- &history.Type,- &history.Status,- &history.Version,- &history.Description,- &history.Statement,- &history.Schema,- &history.ExecutionDuration,- &history.IssueId,- &history.Payload,- ); err != nil {- return nil, err- }-- list = append(list, &history)- }- if err := rows.Err(); err != nil {- return nil, err- }-- if err := tx.Commit(); err != nil {- return nil, err- }-- return list, nil+ return util.FindMigrationHistoryList(ctx, driver.db, find, "bytebase.")}// Dump and restorediff --git a/plugin/db/pg/pg.go b/plugin/db/pg/pg.goindex 3260ac1ee0..b9afa0c16b 100644--- a/plugin/db/pg/pg.go+++ b/plugin/db/pg/pg.go@@ -429,111 +429,18 @@ func (driver *Driver) ExecuteMigration(ctx context.Context, m *db.MigrationInfo,)VALUES (?, EXTRACT(epoch from NOW()), ?, EXTRACT(epoch from NOW()), ?, ?, ?, ?, 'PENDING', ?, ?, ?, ?, 0, ?, ?)`- updateHistoryQuery := `- UPDATE migration_history SET- ` + "`status` = 'DONE'," + `- ` + "execution_duration = ?," + `- ` + "`schema` = ?" + `- WHERE id = ?- `args := util.MigrationExecutionArgs{// TODO(spinningbot): implement dumpTxn.// DumpTxn: dumpTxn,InsertHistoryQuery: insertHistoryQuery,- UpdateHistoryQuery: updateHistoryQuery,TablePrefix: "",}return util.ExecuteMigration(ctx, driver.db, m, statement, args)}func (driver *Driver) FindMigrationHistoryList(ctx context.Context, find *db.MigrationHistoryFind) ([]*db.MigrationHistory, error) {- tx, err := driver.db.BeginTx(ctx, nil)- if err != nil {- return nil, err- }- defer tx.Rollback()-- where, args := []string{"1 = 1"}, []interface{}{}- if v := find.ID; v != nil {- where, args = append(where, "id = ?"), append(args, *v)- }- if v := find.Database; v != nil {- where, args = append(where, "namespace = ?"), append(args, *v)- }- if v := find.Version; v != nil {- where, args = append(where, "version = ?"), append(args, *v)- }-- var query = `- SELECT- id,- created_by,- created_ts,- updated_by,- updated_ts,- namespace,- sequence,- ` + "`engine`," + `- ` + "`type`," + `- ` + "`status`," + `- version,- description,- statement,- ` + "`schema`," + `- execution_duration,- issue_id,- payload- FROM migration_history- WHERE ` + strings.Join(where, " AND ") + `- ORDER BY created_ts DESC`- if v := find.Limit; v != nil {- query += fmt.Sprintf(" LIMIT %d", *v)- }-- rows, err := tx.QueryContext(ctx, query, args...)- if err != nil {- return nil, util.FormatErrorWithQuery(err, query)- }- defer rows.Close()-- // Iterate over result set and deserialize rows into list.- list := make([]*db.MigrationHistory, 0)- for rows.Next() {- var history db.MigrationHistory- if err := rows.Scan(- &history.ID,- &history.Creator,- &history.CreatedTs,- &history.Updater,- &history.UpdatedTs,- &history.Namespace,- &history.Sequence,- &history.Engine,- &history.Type,- &history.Status,- &history.Version,- &history.Description,- &history.Statement,- &history.Schema,- &history.ExecutionDuration,- &history.IssueId,- &history.Payload,- ); err != nil {- return nil, err- }-- list = append(list, &history)- }- if err := rows.Err(); err != nil {- return nil, err- }-- if err := tx.Commit(); err != nil {- return nil, err- }-- return list, nil+ return util.FindMigrationHistoryList(ctx, driver.db, find, "")}// Dump and restorediff --git a/plugin/db/util/driverutil.go b/plugin/db/util/driverutil.goindex 9aa4ce313e..30fcccbd3e 100644--- a/plugin/db/util/driverutil.go+++ b/plugin/db/util/driverutil.go@@ -37,7 +37,6 @@ func NeedsSetupMigrationSchema(ctx context.Context, sqldb *sql.DB, query string)type MigrationExecutionArgs struct {DumpTxn func(ctx context.Context, txn *sql.Tx, database string, out io.Writer, schemaOnly bool) errorInsertHistoryQuery string- UpdateHistoryQuery stringTablePrefix string}@@ -153,14 +152,23 @@ func ExecuteMigration(ctx context.Context, sqldb *sql.DB, m *db.MigrationInfo, s// Phase 5 - Update the migration history with 'DONE', execution_duration, updated schema.- _, err = afterTx.ExecContext(ctx, args.UpdateHistoryQuery,+ updateHistoryQuery := `+ UPDATE ` ++ args.TablePrefix + `migration_history ` ++ `SET+ ` + "`status` = 'DONE'," + `+ ` + "execution_duration = ?," + `+ ` + "`schema` = ?" + `+ WHERE id = ?+`+ _, err = afterTx.ExecContext(ctx, updateHistoryQuery,duration,afterSchemaBuf.String(),insertedId,)if err != nil {- return "", FormatErrorWithQuery(err, args.UpdateHistoryQuery)+ return "", FormatErrorWithQuery(err, updateHistoryQuery)}if err := afterTx.Commit(); err != nil {@@ -279,6 +287,97 @@ func findNextSequence(ctx context.Context, tx *sql.Tx, namespace string, requirereturn int(sequence.Int32), nil}+// FindMigrationHistoryList will find the list of migration history.+func FindMigrationHistoryList(ctx context.Context, sqldb *sql.DB, find *db.MigrationHistoryFind, tablePrefix string) ([]*db.MigrationHistory, error) {+ tx, err := sqldb.BeginTx(ctx, nil)+ if err != nil {+ return nil, err+ }+ defer tx.Rollback()++ where, args := []string{"1 = 1"}, []interface{}{}+ if v := find.ID; v != nil {+ where, args = append(where, "id = ?"), append(args, *v)+ }+ if v := find.Database; v != nil {+ where, args = append(where, "namespace = ?"), append(args, *v)+ }+ if v := find.Version; v != nil {+ where, args = append(where, "version = ?"), append(args, *v)+ }++ var query = `+ SELECT+ id,+ created_by,+ created_ts,+ updated_by,+ updated_ts,+ namespace,+ sequence,+ ` + "`engine`," + `+ ` + "`type`," + `+ ` + "`status`," + `+ version,+ description,+ statement,+ ` + "`schema`," + `+ execution_duration,+ issue_id,+ payload+ FROM ` ++ tablePrefix + `migration_history ` ++ `WHERE ` + strings.Join(where, " AND ") + `+ ORDER BY created_ts DESC`+ if v := find.Limit; v != nil {+ query += fmt.Sprintf(" LIMIT %d", *v)+ }++ rows, err := tx.QueryContext(ctx, query, args...)+ if err != nil {+ return nil, FormatErrorWithQuery(err, query)+ }+ defer rows.Close()++ // Iterate over result set and deserialize rows into list.+ list := make([]*db.MigrationHistory, 0)+ for rows.Next() {+ var history db.MigrationHistory+ if err := rows.Scan(+ &history.ID,+ &history.Creator,+ &history.CreatedTs,+ &history.Updater,+ &history.UpdatedTs,+ &history.Namespace,+ &history.Sequence,+ &history.Engine,+ &history.Type,+ &history.Status,+ &history.Version,+ &history.Description,+ &history.Statement,+ &history.Schema,+ &history.ExecutionDuration,+ &history.IssueId,+ &history.Payload,+ ); err != nil {+ return nil, err+ }++ list = append(list, &history)+ }+ if err := rows.Err(); err != nil {+ return nil, err+ }++ if err := tx.Commit(); err != nil {+ return nil, err+ }++ return list, nil+}+func formatError(err error) error {if err == nil {return nil