Skip to content

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

Add database transfer activity to both the source and target project.

天舟31f55c22e599
diff --git a/api/activity.go b/api/activity.go
index 9c898d51b5..c2d07cf5a0 100644
--- a/api/activity.go
+++ b/api/activity.go
@@ -25,7 +25,8 @@ const (
ActivityMemberDeactivate ActivityType = "bb.member.deactivate"
// Project related
- ActivityProjectRepositoryPush ActivityType = "bb.project.repository.push"
+ ActivityProjectRepositoryPush ActivityType = "bb.project.repository.push"
+ ActivityProjectDatabaseTransfer ActivityType = "bb.project.database.transfer"
)
func (e ActivityType) String() string {
@@ -50,6 +51,8 @@ func (e ActivityType) String() string {
return "bb.member.deactivate"
case ActivityProjectRepositoryPush:
return "bb.project.repository.push"
+ case ActivityProjectDatabaseTransfer:
+ return "bb.project.database.transfer"
}
return "bb.activity.unknown"
}
@@ -145,6 +148,12 @@ type ActivityProjectRepositoryPushPayload struct {
IssueName string `json:"issueName,omitempty"`
}
+type ActivityProjectDatabaseTransferPayload struct {
+ DatabaseId int `json:"databaseId,omitempty"`
+ // Used by activity table to display info without paying the join cost
+ DatabaseName string `json:"databaseName,omitempty"`
+}
+
type Activity struct {
ID int `jsonapi:"primary,activity"`
diff --git a/frontend/src/components/ActivityTable.vue b/frontend/src/components/ActivityTable.vue
index 0b18288d4e..4e8f5a8235 100644
--- a/frontend/src/components/ActivityTable.vue
+++ b/frontend/src/components/ActivityTable.vue
@@ -126,7 +126,11 @@
<script lang="ts">
import { PropType } from "vue";
import { BBTableColumn } from "../bbkit/types";
-import { ActivityProjectRepositoryPushPayload, Activity } from "../types";
+import {
+ ActivityProjectRepositoryPushPayload,
+ Activity,
+ ActivityProjectDatabaseTransferPayload,
+} from "../types";
import { activityName, issueSlug } from "../utils";
type Link = {
@@ -174,6 +178,15 @@ export default {
external: true,
};
}
+ case "bb.project.database.transfer": {
+ const payload =
+ activity.payload as ActivityProjectDatabaseTransferPayload;
+ return {
+ title: payload.databaseName,
+ path: `/db/${payload.databaseId}`,
+ external: false,
+ };
+ }
}
return undefined;
};
diff --git a/frontend/src/types/activity.ts b/frontend/src/types/activity.ts
index 1d732adec5..6429c926d9 100644
--- a/frontend/src/types/activity.ts
+++ b/frontend/src/types/activity.ts
@@ -19,7 +19,9 @@ export type MemberActivityType =
| "bb.member.activate"
| "bb.member.deactivate";
-export type ProjectActivityType = "bb.project.repository.push";
+export type ProjectActivityType =
+ | "bb.project.repository.push"
+ | "bb.project.database.transfer";
export type ActivityType =
| IssueActivityType
@@ -87,6 +89,11 @@ export type ActivityProjectRepositoryPushPayload = {
issueName?: string;
};
+export type ActivityProjectDatabaseTransferPayload = {
+ databaseId: number;
+ databaseName: string;
+};
+
export type ActionPayloadType =
| ActivityIssueCreatePayload
| ActivityIssueCommentCreatePayload
@@ -96,7 +103,8 @@ export type ActionPayloadType =
| ActivityMemberCreatePayload
| ActivityMemberRoleUpdatePayload
| ActivityMemberActivateDeactivatePayload
- | ActivityProjectRepositoryPushPayload;
+ | ActivityProjectRepositoryPushPayload
+ | ActivityProjectDatabaseTransferPayload;
export type Activity = {
id: ActivityId;
diff --git a/frontend/src/utils/activity.ts b/frontend/src/utils/activity.ts
index 19b44b61ca..62a5d3ea7e 100644
--- a/frontend/src/utils/activity.ts
+++ b/frontend/src/utils/activity.ts
@@ -35,6 +35,8 @@ export function activityName(type: ActivityType): string {
return "Deactivate member";
case "bb.project.repository.push":
return "Repository push event";
+ case "bb.project.database.transfer":
+ return "Database transfer";
}
}
diff --git a/server/database.go b/server/database.go
index b9045be57f..f6b80a36c0 100644
--- a/server/database.go
+++ b/server/database.go
@@ -136,6 +136,21 @@ func (s *Server) registerDatabaseRoutes(g *echo.Group) {
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted patch database request").SetInternal(err)
}
+ // If we are transferring the database to a different project, then we create a project activity in both
+ // the old project and new project.
+ var existingDatabase *api.Database
+ if databasePatch.ProjectId != nil {
+ existingDatabase, err = s.DatabaseService.FindDatabase(context.Background(), &api.DatabaseFind{
+ ID: &databasePatch.ID,
+ })
+ if err != nil {
+ if common.ErrorCode(err) == common.ENOTFOUND {
+ return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Database ID not found: %d", id))
+ }
+ return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to patch database ID: %v", id)).SetInternal(err)
+ }
+ }
+
database, err := s.DatabaseService.PatchDatabase(context.Background(), databasePatch)
if err != nil {
if common.ErrorCode(err) == common.ENOTFOUND {
@@ -148,6 +163,59 @@ func (s *Server) registerDatabaseRoutes(g *echo.Group) {
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch updated database relationship: %v", database.Name)).SetInternal(err)
}
+ // Create transferring database project activity.
+ if databasePatch.ProjectId != nil {
+ bytes, err := json.Marshal(api.ActivityProjectDatabaseTransferPayload{
+ DatabaseId: database.ID,
+ DatabaseName: database.Name,
+ })
+ if err == nil {
+ existingDatabase.Project, err = s.ComposeProjectlById(context.Background(), existingDatabase.ProjectId)
+ if err == nil {
+ activityCreate := &api.ActivityCreate{
+ CreatorId: c.Get(GetPrincipalIdContextKey()).(int),
+ ContainerId: existingDatabase.ProjectId,
+ Type: api.ActivityProjectDatabaseTransfer,
+ Level: api.ACTIVITY_INFO,
+ Comment: fmt.Sprintf("Transferred out database %q to project %q.",
+ database.Name, database.Project.Name),
+ Payload: string(bytes),
+ }
+ _, err = s.ActivityManager.CreateActivity(context.Background(), activityCreate, &ActivityMeta{})
+ }
+
+ if err != nil {
+ s.l.Warn("Failed to create project activity after transferring database",
+ zap.Int("database_id", database.ID),
+ zap.String("database_name", database.Name),
+ zap.Int("old_project_id", existingDatabase.ProjectId),
+ zap.Int("new_project_id", database.ProjectId),
+ zap.Error(err))
+ }
+
+ {
+ activityCreate := &api.ActivityCreate{
+ CreatorId: c.Get(GetPrincipalIdContextKey()).(int),
+ ContainerId: database.ProjectId,
+ Type: api.ActivityProjectDatabaseTransfer,
+ Level: api.ACTIVITY_INFO,
+ Comment: fmt.Sprintf("Transferred in database %q from project %q.",
+ existingDatabase.Name, existingDatabase.Project.Name),
+ Payload: string(bytes),
+ }
+ _, err = s.ActivityManager.CreateActivity(context.Background(), activityCreate, &ActivityMeta{})
+ if err != nil {
+ s.l.Warn("Failed to create project activity after transferring database",
+ zap.Int("database_id", database.ID),
+ zap.String("database_name", database.Name),
+ zap.Int("old_project_id", existingDatabase.ProjectId),
+ zap.Int("new_project_id", database.ProjectId),
+ zap.Error(err))
+ }
+ }
+ }
+ }
+
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
if err := jsonapi.MarshalPayload(c.Response().Writer, database); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to marshal database ID response: %v", id)).SetInternal(err)