Skip to content

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

pg: implement FindMigrationHistoryList().

spinningbot9fc1c5aff562
diff --git a/plugin/db/pg/pg.go b/plugin/db/pg/pg.go
index 9fda8d977d..8efee4ebc0 100644
--- a/plugin/db/pg/pg.go
+++ b/plugin/db/pg/pg.go
@@ -412,7 +412,92 @@ func (driver *Driver) ExecuteMigration(ctx context.Context, m *db.MigrationInfo,
}
func (driver *Driver) FindMigrationHistoryList(ctx context.Context, find *db.MigrationHistoryFind) ([]*db.MigrationHistory, error) {
- return nil, fmt.Errorf("not implemented")
+ 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
}
// Dump and restore