Skip to content

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

Support limit in fetching migration history and always return the migration history in creation DESC order

天舟b0b0abd09ad5
diff --git a/db/driver.go b/db/driver.go
index efbea5d5de..32a2bf9628 100644
--- a/db/driver.go
+++ b/db/driver.go
@@ -209,6 +209,8 @@ type MigrationHistory struct {
type MigrationHistoryFind struct {
Database *string
+ // If specified, then it will only fetch "Limit" most recent migration histories
+ Limit *int
}
type Driver interface {
diff --git a/db/mysql.go b/db/mysql.go
index ed2347dae8..c6d27373d3 100644
--- a/db/mysql.go
+++ b/db/mysql.go
@@ -450,8 +450,8 @@ func (driver *MySQLDriver) FindMigrationHistoryList(ctx context.Context, find *M
where, args = append(where, "namespace = ?"), append(args, *v)
}
- rows, err := tx.QueryContext(ctx, `
- SELECT
+ var query = `
+ SELECT
id,
created_by,
created_ts,
@@ -459,8 +459,8 @@ func (driver *MySQLDriver) FindMigrationHistoryList(ctx context.Context, find *M
updated_ts,
namespace,
sequence,
- `+"`engine`,"+`
- `+"`type`,"+`
+ ` + "`engine`," + `
+ ` + "`type`," + `
version,
description,
statement,
@@ -468,9 +468,13 @@ func (driver *MySQLDriver) FindMigrationHistoryList(ctx context.Context, find *M
issue_id,
payload
FROM bytebase.migration_history
- WHERE `+strings.Join(where, " AND "),
- args...,
- )
+ 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, err
}
diff --git a/frontend/src/store/modules/instance.ts b/frontend/src/store/modules/instance.ts
index 5516fbaa80..930c8aa4ef 100644
--- a/frontend/src/store/modules/instance.ts
+++ b/frontend/src/store/modules/instance.ts
@@ -239,16 +239,18 @@ const actions = {
{
instanceId,
databaseName,
+ limit,
}: {
instanceId: InstanceId;
databaseName: string;
+ limit?: number;
}
): Promise<MigrationHistory> {
- const data = (
- await axios.get(
- `/api/instance/${instanceId}/migration/history?database=${databaseName}`
- )
- ).data.data;
+ var url = `/api/instance/${instanceId}/migration/history?database=${databaseName}`;
+ if (limit) {
+ url += `&limit=${limit}`;
+ }
+ const data = (await axios.get(url)).data.data;
const historyList = data.map((history: ResourceObject) => {
return convertMigrationHistory(history);
});
diff --git a/server/instance.go b/server/instance.go
index 2f61c50621..d2aa0505b4 100644
--- a/server/instance.go
+++ b/server/instance.go
@@ -250,6 +250,19 @@ func (s *Server) registerInstanceRoutes(g *echo.Group) {
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch instance ID: %v", id)).SetInternal(err)
}
+ find := &db.MigrationHistoryFind{}
+ databaseStr := c.QueryParams().Get("database")
+ if databaseStr != "" {
+ find.Database = &databaseStr
+ }
+ if limitStr := c.QueryParam("limit"); limitStr != "" {
+ limit, err := strconv.Atoi(limitStr)
+ if err != nil {
+ return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("limit query parameter is not a number: %s", limitStr)).SetInternal(err)
+ }
+ find.Limit = &limit
+ }
+
historyList := []*api.MigrationHistory{}
driver, err := db.Open(instance.Engine, db.DriverConfig{Logger: s.l}, db.ConnectionConfig{
Username: instance.Username,
@@ -258,11 +271,6 @@ func (s *Server) registerInstanceRoutes(g *echo.Group) {
Port: instance.Port,
})
if err == nil {
- find := &db.MigrationHistoryFind{}
- databaseStr := c.QueryParams().Get("database")
- if databaseStr != "" {
- find.Database = &databaseStr
- }
list, err := driver.FindMigrationHistoryList(context.Background(), find)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to fetch migration history list").SetInternal(err)