Skip to content

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

Add issue_id and payload column to the migration_history table and implement the corresponding logic

天舟c21fcc15152f
diff --git a/api/instance.go b/api/instance.go
index a00fbf4ac9..8874e1d30d 100644
--- a/api/instance.go
+++ b/api/instance.go
@@ -128,6 +128,9 @@ type MigrationHistory struct {
Description string `jsonapi:"attr,description"`
Statement string `jsonapi:"attr,statement"`
ExecutionDuration int `jsonapi:"attr,executionDuration"`
+ // This is a string instead of int as the issue id may come from other issue tracking system in the future
+ IssueId string `jsonapi:"attr,issueId"`
+ Payload string `jsonapi:"attr,payload"`
}
type InstanceService interface {
diff --git a/api/task.go b/api/task.go
index 599b76eac3..ec4301d7e7 100644
--- a/api/task.go
+++ b/api/task.go
@@ -3,6 +3,8 @@ package api
import (
"context"
"encoding/json"
+
+ "github.com/bytebase/bytebase/common"
)
const ONBOARDING_TASK_ID1 = 101
@@ -56,9 +58,9 @@ type TaskDatabaseCreatePayload struct {
}
type TaskDatabaseSchemaUpdatePayload struct {
- Statement string `json:"statement,omitempty"`
- RollbackStatement string `json:"rollbackStatement,omitempty"`
- VCSPushEvent *VCSPushEvent `json:"pushEvent,omitempty"`
+ Statement string `json:"statement,omitempty"`
+ RollbackStatement string `json:"rollbackStatement,omitempty"`
+ VCSPushEvent *common.VCSPushEvent `json:"pushEvent,omitempty"`
}
type Task struct {
@@ -113,7 +115,7 @@ type TaskCreate struct {
DatabaseName string `jsonapi:"attr,databaseName"`
CharacterSet string `jsonapi:"attr,characterSet"`
Collation string `jsonapi:"attr,collation"`
- VCSPushEvent *VCSPushEvent
+ VCSPushEvent *common.VCSPushEvent
}
type TaskFind struct {
diff --git a/api/vcs.go b/api/vcs.go
index 858dc71b2e..ca63a6edfb 100644
--- a/api/vcs.go
+++ b/api/vcs.go
@@ -3,22 +3,10 @@ package api
import (
"context"
"encoding/json"
-)
-
-type VCSType string
-const (
- GITLAB_SELF_HOST VCSType = "GITLAB_SELF_HOST"
+ "github.com/bytebase/bytebase/common"
)
-func (e VCSType) String() string {
- switch e {
- case GITLAB_SELF_HOST:
- return "GITLAB_SELF_HOST"
- }
- return "UNKNOWN"
-}
-
type VCS struct {
ID int `jsonapi:"primary,vcs"`
@@ -31,12 +19,12 @@ type VCS struct {
UpdatedTs int64 `jsonapi:"attr,updatedTs"`
// Domain specific fields
- Name string `jsonapi:"attr,name"`
- Type VCSType `jsonapi:"attr,type"`
- InstanceURL string `jsonapi:"attr,instanceURL"`
- ApiURL string `jsonapi:"attr,apiURL"`
- ApplicationId string `jsonapi:"attr,applicationId"`
- Secret string `jsonapi:"attr,secret"`
+ Name string `jsonapi:"attr,name"`
+ Type common.VCSType `jsonapi:"attr,type"`
+ InstanceURL string `jsonapi:"attr,instanceURL"`
+ ApiURL string `jsonapi:"attr,apiURL"`
+ ApplicationId string `jsonapi:"attr,applicationId"`
+ Secret string `jsonapi:"attr,secret"`
}
type VCSCreate struct {
@@ -45,9 +33,9 @@ type VCSCreate struct {
CreatorId int
// Domain specific fields
- Name string `jsonapi:"attr,name"`
- Type VCSType `jsonapi:"attr,type"`
- InstanceURL string `jsonapi:"attr,instanceURL"`
+ Name string `jsonapi:"attr,name"`
+ Type common.VCSType `jsonapi:"attr,type"`
+ InstanceURL string `jsonapi:"attr,instanceURL"`
// ApiURL derives from InstanceURL
ApiURL string
ApplicationId string `jsonapi:"attr,applicationId"`
@@ -87,30 +75,6 @@ type VCSDelete struct {
DeleterId int
}
-// These payload types are only used when marshalling to the json format for saving into the database.
-// So we annotate with json tag using camelCase naming which is consistent with normal
-// json naming convention
-type VCSFileCommit struct {
- ID string `json:"id"`
- Title string `json:"title"`
- Message string `json:"message"`
- CreatedTs int64 `json:"createdTs"`
- URL string `json:"url"`
- AuthorName string `json:"authorName"`
- Added string `json:"added"`
-}
-
-type VCSPushEvent struct {
- VCSType VCSType `json:"vcsType"`
- BaseDirectory string `json:"baseDir"`
- Ref string `json:"ref"`
- RepositoryID string `json:"repoId"`
- RepositoryURL string `json:"repoUrl"`
- RepositoryFullPath string `json:"repoFullPath"`
- AuthorName string `json:"authorName"`
- FileCommit VCSFileCommit `json:"fileCommit"`
-}
-
type VCSService interface {
CreateVCS(ctx context.Context, create *VCSCreate) (*VCS, error)
FindVCSList(ctx context.Context, find *VCSFind) ([]*VCS, error)
diff --git a/common/vcs.go b/common/vcs.go
new file mode 100644
index 0000000000..dc0f77cb9a
--- /dev/null
+++ b/common/vcs.go
@@ -0,0 +1,39 @@
+package common
+
+type VCSType string
+
+const (
+ GITLAB_SELF_HOST VCSType = "GITLAB_SELF_HOST"
+)
+
+func (e VCSType) String() string {
+ switch e {
+ case GITLAB_SELF_HOST:
+ return "GITLAB_SELF_HOST"
+ }
+ return "UNKNOWN"
+}
+
+// These payload types are only used when marshalling to the json format for saving into the database.
+// So we annotate with json tag using camelCase naming which is consistent with normal
+// json naming convention
+type VCSFileCommit struct {
+ ID string `json:"id"`
+ Title string `json:"title"`
+ Message string `json:"message"`
+ CreatedTs int64 `json:"createdTs"`
+ URL string `json:"url"`
+ AuthorName string `json:"authorName"`
+ Added string `json:"added"`
+}
+
+type VCSPushEvent struct {
+ VCSType VCSType `json:"vcsType"`
+ BaseDirectory string `json:"baseDir"`
+ Ref string `json:"ref"`
+ RepositoryID string `json:"repoId"`
+ RepositoryURL string `json:"repoUrl"`
+ RepositoryFullPath string `json:"repoFullPath"`
+ AuthorName string `json:"authorName"`
+ FileCommit VCSFileCommit `json:"fileCommit"`
+}
diff --git a/db/driver.go b/db/driver.go
index dccc044285..3f6519df32 100644
--- a/db/driver.go
+++ b/db/driver.go
@@ -7,6 +7,7 @@ import (
"strings"
"sync"
+ "github.com/bytebase/bytebase/common"
"go.uber.org/zap"
)
@@ -74,6 +75,10 @@ func (e MigrationType) String() string {
return "UNKNOWN"
}
+type MigrationInfoPayload struct {
+ VCSPushEvent *common.VCSPushEvent `json:"pushEvent,omitempty"`
+}
+
type MigrationInfo struct {
Version string
Namespace string
@@ -82,6 +87,8 @@ type MigrationInfo struct {
Type MigrationType
Description string
Creator string
+ IssueId string
+ Payload string
}
// ParseMigrationInfo derives MigrationInfo from fullPath and baseDir
@@ -152,6 +159,8 @@ type MigrationHistory struct {
Description string
Statement string
ExecutionDuration int
+ IssueId string
+ Payload string
}
type MigrationHistoryFind struct {
diff --git a/db/mysql.go b/db/mysql.go
index aef89389c1..1ed0c399a1 100644
--- a/db/mysql.go
+++ b/db/mysql.go
@@ -280,9 +280,11 @@ func (driver *MySQLDriver) ExecuteMigration(ctx context.Context, m *MigrationInf
version,
description,
statement,
- execution_duration
+ execution_duration,
+ issue_id,
+ payload
)
- VALUES (?, unix_timestamp(), ?, unix_timestamp(), ?, ?, ?, ?, ?, ?, ?)
+ VALUES (?, unix_timestamp(), ?, unix_timestamp(), ?, ?, ?, ?, ?, ?, ?, ?, ?)
`,
m.Creator,
m.Creator,
@@ -293,6 +295,8 @@ func (driver *MySQLDriver) ExecuteMigration(ctx context.Context, m *MigrationInf
m.Description,
statement,
time.Now().Unix()-startedTs,
+ m.IssueId,
+ m.Payload,
)
if err != nil {
@@ -329,7 +333,9 @@ func (driver *MySQLDriver) FindMigrationHistoryList(ctx context.Context, find *M
version,
description,
statement,
- execution_duration
+ execution_duration,
+ issue_id,
+ payload
FROM bytebase.migration_history
WHERE `+strings.Join(where, " AND "),
args...,
@@ -356,6 +362,8 @@ func (driver *MySQLDriver) FindMigrationHistoryList(ctx context.Context, find *M
&history.Description,
&history.Statement,
&history.ExecutionDuration,
+ &history.IssueId,
+ &history.Payload,
); err != nil {
return nil, err
}
diff --git a/db/mysql_migration_schema.sql b/db/mysql_migration_schema.sql
index 29f15bb168..a33840c1d2 100644
--- a/db/mysql_migration_schema.sql
+++ b/db/mysql_migration_schema.sql
@@ -56,7 +56,9 @@ CREATE TABLE bytebase.migration_history (
description TEXT NOT NULL,
-- Recorded the migration statement
statement TEXT NOT NULL,
- execution_duration INTEGER NOT NULL
+ execution_duration INTEGER NOT NULL,
+ issue_id TEXT NOT NULL,
+ payload TEXT NOT NULL
);
CREATE UNIQUE INDEX bytebase_idx_unique_migration_history_namespace_sequence ON bytebase.migration_history (namespace(256), sequence);
diff --git a/frontend/src/components/MigrationHistoryTable.vue b/frontend/src/components/MigrationHistoryTable.vue
index 49259a9f32..67ead2a36e 100644
--- a/frontend/src/components/MigrationHistoryTable.vue
+++ b/frontend/src/components/MigrationHistoryTable.vue
@@ -9,23 +9,30 @@
>
<template v-slot:body="{ rowData: history }">
<BBTableCell :leftPadding="4" class="w-16 table-cell">
- {{ history.creator }}
+ {{ history.version }}
</BBTableCell>
<BBTableCell class="w-16 table-cell">
- {{ humanizeTs(history.createdTs) }}
+ {{ history.type }}
</BBTableCell>
- <BBTableCell class="w-32 table-cell">
- {{ history.version }}
- </BBTableCell>
- <BBTableCell class="w-8 table-cell truncate">
- {{ history.statement }}
+ <BBTableCell class="w-16 table-cell">
+ <template v-if="history.issueId">
+ <router-link :to="`/issue/${history.issueId}`" class="normal-link"
+ >{{ history.issueId }}
+ </router-link>
+ </template>
</BBTableCell>
- <BBTableCell class="w-8 table-cell truncate">
+ <BBTableCell class="w-32 table-cell truncate">
{{ history.description }}
</BBTableCell>
<BBTableCell class="w-16 table-cell">
{{ secondsToString(history.executionDuration) }}
</BBTableCell>
+ <BBTableCell class="w-16 table-cell">
+ {{ humanizeTs(history.createdTs) }}
+ </BBTableCell>
+ <BBTableCell class="w-16 table-cell">
+ {{ history.creator }}
+ </BBTableCell>
</template>
</BBTable>
</template>
@@ -37,16 +44,13 @@ import { secondsToString } from "../utils";
const COLUMN_LIST = [
{
- title: "Creator",
- },
- {
- title: "Created",
+ title: "Version",
},
{
- title: "Version",
+ title: "Type",
},
{
- title: "SQL",
+ title: "Issue",
},
{
title: "Description",
@@ -54,6 +58,12 @@ const COLUMN_LIST = [
{
title: "Duration",
},
+ {
+ title: "Created",
+ },
+ {
+ title: "Creator",
+ },
];
export default {
diff --git a/frontend/src/store/modules/instance.ts b/frontend/src/store/modules/instance.ts
index 1c8ebc69c8..c111f68fa1 100644
--- a/frontend/src/store/modules/instance.ts
+++ b/frontend/src/store/modules/instance.ts
@@ -50,8 +50,11 @@ function convert(
function convertMigrationHistory(history: ResourceObject): MigrationHistory {
return {
- ...(history.attributes as Omit<MigrationHistory, "id">),
+ ...(history.attributes as Omit<MigrationHistory, "id" | "issueId">),
id: parseInt(history.id),
+ // This issueId is special since it's stored in the migration history table
+ // and may refer to the issueId from the external system in the future.
+ issueId: parseInt(history.attributes.issueId as string),
};
}
diff --git a/frontend/src/types/instance.ts b/frontend/src/types/instance.ts
index ae44cfd8c2..4a99220d03 100644
--- a/frontend/src/types/instance.ts
+++ b/frontend/src/types/instance.ts
@@ -2,6 +2,7 @@ import { RowStatus } from "./common";
import { Environment } from "./environment";
import { EnvironmentId, InstanceId, MigrationHistoryId } from "./id";
import { Principal } from "./principal";
+import { VCSPushEvent } from "./vcs";
export type EngineType = "MYSQL";
@@ -66,6 +67,10 @@ export type InstanceMigration = {
export type MigrationType = "BASELINE" | "SQL";
+export type MigrationHistoryPayload = {
+ pushEvent?: VCSPushEvent;
+};
+
export type MigrationHistory = {
id: MigrationHistoryId;
creator: string;
@@ -78,4 +83,6 @@ export type MigrationHistory = {
description: string;
statement: string;
executionDuration: number;
+ issueId: number;
+ payload?: MigrationHistoryPayload;
};
diff --git a/server/instance.go b/server/instance.go
index 7bf6f6bdfc..a95e27bfa5 100644
--- a/server/instance.go
+++ b/server/instance.go
@@ -281,6 +281,8 @@ func (s *Server) registerInstanceRoutes(g *echo.Group) {
Description: entry.Description,
Statement: entry.Statement,
ExecutionDuration: entry.ExecutionDuration,
+ IssueId: entry.IssueId,
+ Payload: entry.Payload,
})
}
}
diff --git a/server/task_executor_schema_update.go b/server/task_executor_schema_update.go
index 4cce348d29..f1bc9a3ed4 100644
--- a/server/task_executor_schema_update.go
+++ b/server/task_executor_schema_update.go
@@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
+ "strconv"
"strings"
"github.com/bytebase/bytebase/api"
@@ -49,6 +50,30 @@ func (exec *SchemaUpdateTaskExecutor) RunOnce(ctx context.Context, server *Serve
return true, "", fmt.Errorf("failed to start schema migration, error: %w", err)
}
mi.Creator = payload.VCSPushEvent.FileCommit.AuthorName
+
+ miPayload := &db.MigrationInfoPayload{
+ VCSPushEvent: payload.VCSPushEvent,
+ }
+ bytes, err := json.Marshal(miPayload)
+ if err != nil {
+ return true, "", fmt.Errorf("failed to start schema migration, unable to marshal vcs push event payload %w", err)
+ }
+ mi.Payload = string(bytes)
+ }
+
+ issueFind := &api.IssueFind{
+ PipelineId: &task.PipelineId,
+ }
+ issue, err := server.IssueService.FindIssue(ctx, issueFind)
+ if err != nil {
+ // If somehow we unable about to find the issue, we just emit the error since it's not
+ // critical enough to fail the entire operation.
+ exec.l.Error("Failed to fetch containing issue for composing the migration info",
+ zap.Int("task_id", task.ID),
+ zap.Error(err),
+ )
+ } else {
+ mi.IssueId = strconv.Itoa(issue.ID)
}
sql := strings.TrimSpace(payload.Statement)
diff --git a/server/webhook.go b/server/webhook.go
index 232ce48b5a..64d04ea018 100644
--- a/server/webhook.go
+++ b/server/webhook.go
@@ -14,6 +14,7 @@ import (
"github.com/bytebase/bytebase"
"github.com/bytebase/bytebase/api"
+ "github.com/bytebase/bytebase/common"
"github.com/bytebase/bytebase/db"
"github.com/bytebase/bytebase/external/gitlab"
"github.com/labstack/echo/v4"
@@ -180,7 +181,7 @@ func (s *Server) registerWebhookRoutes(g *echo.Group) {
if err != nil {
s.l.Warn("Failed to parse timestamp. Skip", zap.String("file", added), zap.Error(err))
}
- vcsPushEvent := api.VCSPushEvent{
+ vcsPushEvent := common.VCSPushEvent{
VCSType: repository.VCS.Type,
BaseDirectory: repository.BaseDirectory,
Ref: pushEvent.Ref,
@@ -188,7 +189,7 @@ func (s *Server) registerWebhookRoutes(g *echo.Group) {
RepositoryURL: pushEvent.Project.WebURL,
RepositoryFullPath: pushEvent.Project.FullPath,
AuthorName: pushEvent.AuthorName,
- FileCommit: api.VCSFileCommit{
+ FileCommit: common.VCSFileCommit{
ID: commit.ID,
Title: commit.Title,
Message: commit.Message,