Skip to content

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

refactor: simplify the sheet store (#10972)

ecmadaoc0b7dd86953a
diff --git a/backend/api/gitops/webhook.go b/backend/api/gitops/webhook.go
index 242fcc96d0..62372ff9f1 100644
--- a/backend/api/gitops/webhook.go
+++ b/backend/api/gitops/webhook.go
@@ -1915,9 +1915,6 @@ func (s *Service) prepareIssueFromSDLFile(ctx context.Context, oauthContext *com
ProjectUID: repoInfo.project.UID,
Title: file,
Statement: sdl,
- Visibility: store.ProjectSheet,
- Source: store.SheetFromBytebaseArtifact,
- Type: store.SheetForSQL,
Payload: sheetPayload,
})
if err != nil {
@@ -1988,9 +1985,6 @@ func (s *Service) prepareIssueFromFile(
ProjectUID: repoInfo.project.UID,
Title: fileInfo.item.FileName,
Statement: content,
- Visibility: store.ProjectSheet,
- Source: store.SheetFromBytebaseArtifact,
- Type: store.SheetForSQL,
Payload: sheetPayload,
})
if err != nil {
@@ -2025,9 +2019,6 @@ func (s *Service) prepareIssueFromFile(
ProjectUID: repoInfo.project.UID,
Title: fileInfo.item.FileName,
Statement: migrationFile.Statement,
- Visibility: store.ProjectSheet,
- Source: store.SheetFromBytebaseArtifact,
- Type: store.SheetForSQL,
Payload: sheetPayload,
})
if err != nil {
@@ -2076,9 +2067,6 @@ func (s *Service) prepareIssueFromFile(
ProjectUID: repoInfo.project.UID,
Title: fileInfo.item.FileName,
Statement: content,
- Visibility: store.ProjectSheet,
- Source: store.SheetFromBytebaseArtifact,
- Type: store.SheetForSQL,
Payload: sheetPayload,
})
if err != nil {
@@ -2154,9 +2142,6 @@ func (s *Service) tryUpdateTasksFromModifiedFile(ctx context.Context, databases
ProjectUID: issue.Project.UID,
Title: fileName,
Statement: statement,
- Visibility: store.ProjectSheet,
- Source: store.SheetFromBytebaseArtifact,
- Type: store.SheetForSQL,
Payload: sheetPayload,
})
if err != nil {
diff --git a/backend/api/v1/rollout_service.go b/backend/api/v1/rollout_service.go
index b2ebb77a30..108cfef289 100644
--- a/backend/api/v1/rollout_service.go
+++ b/backend/api/v1/rollout_service.go
@@ -971,7 +971,7 @@ func (s *RolloutService) UpdatePlan(ctx context.Context, request *v1pb.UpdatePla
sheet, err := s.store.GetSheet(ctx, &store.FindSheetMessage{
UID: &sheetUID,
- }, api.SystemBotID)
+ })
if err != nil {
return status.Errorf(codes.Internal, "failed to get sheet %q: %v", config.ChangeDatabaseConfig.Sheet, err)
}
@@ -1318,9 +1318,6 @@ func (s *RolloutService) createPipeline(ctx context.Context, project *store.Proj
ProjectUID: project.UID,
Title: fmt.Sprintf("Sheet for task %v", c.Name),
Statement: c.Statement,
- Visibility: store.ProjectSheet,
- Source: store.SheetFromBytebaseArtifact,
- Type: store.SheetForSQL,
})
if err != nil {
return nil, errors.Wrapf(err, "failed to create sheet for task %v", c.Name)
diff --git a/backend/api/v1/rollout_service_converter.go b/backend/api/v1/rollout_service_converter.go
index 5e7b309bc3..c8607b96cf 100644
--- a/backend/api/v1/rollout_service_converter.go
+++ b/backend/api/v1/rollout_service_converter.go
@@ -343,7 +343,7 @@ func convertToPlanCheckRun(ctx context.Context, s *store.Store, projectID string
}
if sheetUID := int(run.Config.GetSheetUid()); sheetUID != 0 {
- sheet, err := s.GetSheet(ctx, &store.FindSheetMessage{UID: &sheetUID}, api.SystemBotID)
+ sheet, err := s.GetSheet(ctx, &store.FindSheetMessage{UID: &sheetUID})
if err != nil {
return nil, errors.Wrapf(err, "failed to get sheet")
}
diff --git a/backend/api/v1/rollout_service_task.go b/backend/api/v1/rollout_service_task.go
index ec781c1995..33fa05de7a 100644
--- a/backend/api/v1/rollout_service_task.go
+++ b/backend/api/v1/rollout_service_task.go
@@ -220,9 +220,6 @@ func getTaskCreatesFromCreateDatabaseConfig(ctx context.Context, s *store.Store,
ProjectUID: project.UID,
Title: fmt.Sprintf("Sheet for creating database %v", databaseName),
Statement: statement,
- Visibility: store.ProjectSheet,
- Source: store.SheetFromBytebaseArtifact,
- Type: store.SheetForSQL,
})
if err != nil {
return nil, errors.Wrap(err, "failed to create database creation sheet")
diff --git a/backend/api/v1/sheet_service.go b/backend/api/v1/sheet_service.go
index 8fb060c382..7f5c38df17 100644
--- a/backend/api/v1/sheet_service.go
+++ b/backend/api/v1/sheet_service.go
@@ -229,7 +229,7 @@ func (s *SheetService) UpdateSheet(ctx context.Context, request *v1pb.UpdateShee
sheet, err := s.store.GetSheet(ctx, &store.FindSheetMessage{
UID: &sheetUID,
ProjectUID: &project.UID,
- }, principalID)
+ })
if err != nil {
return nil, status.Errorf(codes.Internal, fmt.Sprintf("failed to get sheet: %v", err))
}
@@ -251,8 +251,6 @@ func (s *SheetService) UpdateSheet(ctx context.Context, request *v1pb.UpdateShee
for _, path := range request.UpdateMask.Paths {
switch path {
- case "title":
- sheetPatch.Title = &request.Sheet.Title
case "content":
statement := string(request.Sheet.Content)
sheetPatch.Statement = &statement
@@ -291,14 +289,10 @@ func (s *SheetService) DeleteSheet(ctx context.Context, request *v1pb.DeleteShee
return nil, status.Errorf(codes.NotFound, fmt.Sprintf("project with resource id %q had deleted", projectResourceID))
}
- principalID, ok := ctx.Value(common.PrincipalIDContextKey).(int)
- if !ok {
- return nil, status.Errorf(codes.Internal, "principal ID not found")
- }
sheet, err := s.store.GetSheet(ctx, &store.FindSheetMessage{
UID: &sheetUID,
ProjectUID: &project.UID,
- }, principalID)
+ })
if err != nil {
return nil, status.Errorf(codes.Internal, fmt.Sprintf("failed to get sheet: %v", err))
}
@@ -314,11 +308,7 @@ func (s *SheetService) DeleteSheet(ctx context.Context, request *v1pb.DeleteShee
}
func (s *SheetService) findSheet(ctx context.Context, find *store.FindSheetMessage) (*store.SheetMessage, error) {
- principalID, ok := ctx.Value(common.PrincipalIDContextKey).(int)
- if !ok {
- return nil, status.Errorf(codes.Internal, "principal ID not found")
- }
- sheet, err := s.store.GetSheet(ctx, find, principalID)
+ sheet, err := s.store.GetSheet(ctx, find)
if err != nil {
return nil, status.Errorf(codes.Internal, fmt.Sprintf("failed to get sheet: %v", err))
}
@@ -343,18 +333,14 @@ func (s *SheetService) canWriteSheet(ctx context.Context, sheet *store.SheetMess
return true, nil
}
- if sheet.Visibility == store.ProjectSheet {
- projectRoles, err := s.findProjectRoles(ctx, sheet.ProjectUID, user)
- if err != nil {
- return false, err
- }
- if len(projectRoles) == 0 {
- return false, nil
- }
- return projectRoles[api.ProjectOwner], nil
+ projectRoles, err := s.findProjectRoles(ctx, sheet.ProjectUID, user)
+ if err != nil {
+ return false, err
}
-
- return false, nil
+ if len(projectRoles) == 0 {
+ return false, nil
+ }
+ return projectRoles[api.ProjectOwner], nil
}
func (s *SheetService) findProjectRoles(ctx context.Context, projectUID int, user *store.UserMessage) (map[api.Role]bool, error) {
@@ -430,9 +416,6 @@ func convertToStoreSheetMessage(projectUID int, databaseUID *int, creatorID int,
CreatorID: creatorID,
Title: sheet.Title,
Statement: string(sheet.Content),
- Visibility: store.ProjectSheet,
- Source: store.SheetFromBytebaseArtifact,
- Type: store.SheetForSQL,
}
if sheet.Payload != nil {
sheetMessage.Payload = &storepb.SheetPayload{
diff --git a/backend/runner/plancheck/ghost_sync_executor.go b/backend/runner/plancheck/ghost_sync_executor.go
index 687a447979..c1bab4e007 100644
--- a/backend/runner/plancheck/ghost_sync_executor.go
+++ b/backend/runner/plancheck/ghost_sync_executor.go
@@ -81,7 +81,7 @@ func (e *GhostSyncExecutor) Run(ctx context.Context, config *storepb.PlanCheckRu
}
sheetUID := int(config.SheetUid)
- sheet, err := e.store.GetSheet(ctx, &store.FindSheetMessage{UID: &sheetUID}, api.SystemBotID)
+ sheet, err := e.store.GetSheet(ctx, &store.FindSheetMessage{UID: &sheetUID})
if err != nil {
return nil, errors.Wrapf(err, "failed to get sheet %d", sheetUID)
}
diff --git a/backend/runner/plancheck/statement_advise_executor.go b/backend/runner/plancheck/statement_advise_executor.go
index 76a59f4788..e976d2d9b6 100644
--- a/backend/runner/plancheck/statement_advise_executor.go
+++ b/backend/runner/plancheck/statement_advise_executor.go
@@ -113,7 +113,7 @@ func (e *StatementAdviseExecutor) runForDatabaseTarget(ctx context.Context, conf
}
sheetUID := int(config.SheetUid)
- sheet, err := e.store.GetSheet(ctx, &store.FindSheetMessage{UID: &sheetUID}, api.SystemBotID)
+ sheet, err := e.store.GetSheet(ctx, &store.FindSheetMessage{UID: &sheetUID})
if err != nil {
return nil, errors.Wrapf(err, "failed to get sheet %d", sheetUID)
}
@@ -255,7 +255,7 @@ func (e *StatementAdviseExecutor) runForDatabaseGroupTarget(ctx context.Context,
}
sheetUID := int(config.SheetUid)
- sheet, err := e.store.GetSheet(ctx, &store.FindSheetMessage{UID: &sheetUID}, api.SystemBotID)
+ sheet, err := e.store.GetSheet(ctx, &store.FindSheetMessage{UID: &sheetUID})
if err != nil {
return nil, errors.Wrapf(err, "failed to get sheet %d", sheetUID)
}
diff --git a/backend/runner/plancheck/statement_report_executor.go b/backend/runner/plancheck/statement_report_executor.go
index c9eb971000..57523b4a4d 100644
--- a/backend/runner/plancheck/statement_report_executor.go
+++ b/backend/runner/plancheck/statement_report_executor.go
@@ -89,7 +89,7 @@ func (e *StatementReportExecutor) runForDatabaseTarget(ctx context.Context, conf
}
sheetUID := int(config.SheetUid)
- sheet, err := e.store.GetSheet(ctx, &store.FindSheetMessage{UID: &sheetUID}, api.SystemBotID)
+ sheet, err := e.store.GetSheet(ctx, &store.FindSheetMessage{UID: &sheetUID})
if err != nil {
return nil, errors.Wrapf(err, "failed to get sheet %d", sheetUID)
}
@@ -194,7 +194,7 @@ func (e *StatementReportExecutor) runForDatabaseGroupTarget(ctx context.Context,
}
sheetUID := int(config.SheetUid)
- sheet, err := e.store.GetSheet(ctx, &store.FindSheetMessage{UID: &sheetUID}, api.SystemBotID)
+ sheet, err := e.store.GetSheet(ctx, &store.FindSheetMessage{UID: &sheetUID})
if err != nil {
return nil, errors.Wrapf(err, "failed to get sheet %d", sheetUID)
}
diff --git a/backend/runner/plancheck/statement_type_executor.go b/backend/runner/plancheck/statement_type_executor.go
index 1e76c3b089..c4647191ef 100644
--- a/backend/runner/plancheck/statement_type_executor.go
+++ b/backend/runner/plancheck/statement_type_executor.go
@@ -11,7 +11,6 @@ import (
"github.com/bytebase/bytebase/backend/common"
"github.com/bytebase/bytebase/backend/component/dbfactory"
- api "github.com/bytebase/bytebase/backend/legacyapi"
"github.com/bytebase/bytebase/backend/plugin/advisor"
"github.com/bytebase/bytebase/backend/plugin/parser/base"
mysqlparser "github.com/bytebase/bytebase/backend/plugin/parser/mysql"
@@ -92,7 +91,7 @@ func (e *StatementTypeExecutor) runForDatabaseTarget(ctx context.Context, config
}
sheetUID := int(config.SheetUid)
- sheet, err := e.store.GetSheet(ctx, &store.FindSheetMessage{UID: &sheetUID}, api.SystemBotID)
+ sheet, err := e.store.GetSheet(ctx, &store.FindSheetMessage{UID: &sheetUID})
if err != nil {
return nil, errors.Wrapf(err, "failed to get sheet %d", sheetUID)
}
@@ -213,7 +212,7 @@ func (e *StatementTypeExecutor) runForDatabaseGroupTarget(ctx context.Context, c
}
sheetUID := int(config.SheetUid)
- sheet, err := e.store.GetSheet(ctx, &store.FindSheetMessage{UID: &sheetUID}, api.SystemBotID)
+ sheet, err := e.store.GetSheet(ctx, &store.FindSheetMessage{UID: &sheetUID})
if err != nil {
return nil, errors.Wrapf(err, "failed to get sheet %d", sheetUID)
}
diff --git a/backend/runner/rollbackrun/runner.go b/backend/runner/rollbackrun/runner.go
index 8584e31736..1dc804585a 100644
--- a/backend/runner/rollbackrun/runner.go
+++ b/backend/runner/rollbackrun/runner.go
@@ -150,9 +150,6 @@ func (r *Runner) generateOracleRollbackSQL(ctx context.Context, task *store.Task
ProjectUID: project.UID,
Title: fmt.Sprintf("Sheet for rolling back task %v", task.ID),
Statement: rollbackStatement,
- Visibility: store.ProjectSheet,
- Source: store.SheetFromBytebaseArtifact,
- Type: store.SheetForSQL,
})
if err != nil {
slog.Error("failed to create database creation sheet", log.BBError(err))
@@ -237,9 +234,6 @@ func (r *Runner) generateMySQLRollbackSQL(ctx context.Context, task *store.TaskM
ProjectUID: project.UID,
Title: fmt.Sprintf("Sheet for rolling back task %d", task.ID),
Statement: rollbackStatement,
- Visibility: store.ProjectSheet,
- Source: store.SheetFromBytebaseArtifact,
- Type: store.SheetForSQL,
})
if err != nil {
slog.Error("failed to create database creation sheet", log.BBError(err))
@@ -264,7 +258,7 @@ func (r *Runner) generateMySQLRollbackSQLImpl(ctx context.Context, payload *api.
return "", ctx.Err()
}
// We cannot support rollback SQL generation for sheets because it can take lots of resources.
- sheet, err := r.store.GetSheet(ctx, &store.FindSheetMessage{UID: &payload.SheetID}, api.SystemBotID)
+ sheet, err := r.store.GetSheet(ctx, &store.FindSheetMessage{UID: &payload.SheetID})
if err != nil {
return "", errors.Wrapf(err, "failed to get sheet %d", payload.SheetID)
}
diff --git a/backend/runner/taskrun/database_create_executor.go b/backend/runner/taskrun/database_create_executor.go
index 7af96bdc94..b91438d64d 100644
--- a/backend/runner/taskrun/database_create_executor.go
+++ b/backend/runner/taskrun/database_create_executor.go
@@ -69,7 +69,7 @@ func (exec *DatabaseCreateExecutor) RunOnce(ctx context.Context, driverCtx conte
if err != nil {
return true, nil, errors.Wrapf(err, "failed to get sheet statement of sheet: %d", payload.SheetID)
}
- sheet, err := exec.store.GetSheet(ctx, &store.FindSheetMessage{UID: &payload.SheetID}, api.SystemBotID)
+ sheet, err := exec.store.GetSheet(ctx, &store.FindSheetMessage{UID: &payload.SheetID})
if err != nil {
return true, nil, errors.Wrapf(err, "failed to get sheet: %d", payload.SheetID)
}
diff --git a/backend/runner/taskrun/executor.go b/backend/runner/taskrun/executor.go
index b361953bbe..d613c81990 100644
--- a/backend/runner/taskrun/executor.go
+++ b/backend/runner/taskrun/executor.go
@@ -545,7 +545,7 @@ func postMigration(ctx context.Context, stores *store.Store, activityManager *ac
if sheetID != nil && task.DatabaseID != nil {
sheet, err := stores.GetSheet(ctx, &store.FindSheetMessage{
UID: sheetID,
- }, api.SystemBotID)
+ })
if err != nil {
slog.Error("Failed to get sheet from store", slog.Int("sheetID", *sheetID), log.BBError(err))
} else if sheet.Payload != nil && (sheet.Payload.DatabaseConfig != nil || sheet.Payload.BaselineDatabaseConfig != nil) {
diff --git a/backend/server/onboard.go b/backend/server/onboard.go
index ce19a9d19b..d9697c09bd 100644
--- a/backend/server/onboard.go
+++ b/backend/server/onboard.go
@@ -192,11 +192,8 @@ func (s *Server) generateOnboardingData(ctx context.Context, user *store.UserMes
ProjectUID: project.UID,
DatabaseUID: &testDatabase.UID,
- Title: "Alter table to test sample instance for sample issue",
- Statement: "ALTER TABLE employee ADD COLUMN IF NOT EXISTS email TEXT DEFAULT '';",
- Visibility: store.ProjectSheet,
- Source: store.SheetFromBytebaseArtifact,
- Type: store.SheetForSQL,
+ Title: "Alter table to test sample instance for sample issue",
+ Statement: "ALTER TABLE employee ADD COLUMN IF NOT EXISTS email TEXT DEFAULT '';",
})
if err != nil {
return errors.Wrapf(err, "failed to create test sheet for sample project")
@@ -208,11 +205,8 @@ func (s *Server) generateOnboardingData(ctx context.Context, user *store.UserMes
ProjectUID: project.UID,
DatabaseUID: &prodDatabase.UID,
- Title: "Alter table to prod sample instance for sample issue",
- Statement: "ALTER TABLE employee ADD COLUMN IF NOT EXISTS email TEXT DEFAULT '';",
- Visibility: store.ProjectSheet,
- Source: store.SheetFromBytebaseArtifact,
- Type: store.SheetForSQL,
+ Title: "Alter table to prod sample instance for sample issue",
+ Statement: "ALTER TABLE employee ADD COLUMN IF NOT EXISTS email TEXT DEFAULT '';",
})
if err != nil {
return errors.Wrapf(err, "failed to create prod sheet for sample project")
diff --git a/backend/store/sheet.go b/backend/store/sheet.go
index 38a926caee..68ca8d37c3 100644
--- a/backend/store/sheet.go
+++ b/backend/store/sheet.go
@@ -14,18 +14,6 @@ import (
storepb "github.com/bytebase/bytebase/proto/generated-go/store"
)
-// SheetVisibility is the visibility of a sheet.
-type SheetVisibility string
-
-const (
- // PrivateSheet is the sheet visibility for PRIVATE. Only sheet OWNER can read/write.
- PrivateSheet SheetVisibility = "PRIVATE"
- // ProjectSheet is the sheet visibility for PROJECT. Both sheet OWNER and project OWNER can read/write, and project DEVELOPER can read.
- ProjectSheet SheetVisibility = "PROJECT"
- // PublicSheet is the sheet visibility for PUBLIC. Sheet OWNER can read/write, and all others can read.
- PublicSheet SheetVisibility = "PUBLIC"
-)
-
// SheetSource is the type of sheet origin source.
type SheetSource string
@@ -55,23 +43,17 @@ type SheetMessage struct {
CreatorID int
UpdaterID int
- Title string
- Statement string
- Visibility SheetVisibility
- Source SheetSource
- Type SheetType
- Payload *storepb.SheetPayload
+ Title string
+ Statement string
+ Payload *storepb.SheetPayload
// Output only fields
UID int
Size int64
CreatedTime time.Time
UpdatedTime time.Time
- Starred bool
- Pinned bool
// Internal fields
- rowStatus RowStatus
createdTs int64
updatedTs int64
}
@@ -80,51 +62,23 @@ type SheetMessage struct {
type FindSheetMessage struct {
UID *int
- // Standard fields
- RowStatus *RowStatus
-
// Used to find the creator's sheet list.
// When finding shared PROJECT/PUBLIC sheets, this value should be empty.
// It does not make sense to set both `CreatorID` and `ExcludedCreatorID`.
CreatorID *int
- // Used to find the sheets that are not created by the creator.
- ExcludedCreatorID *int
// LoadFull is used if we want to load the full sheet.
LoadFull bool
// Related fields
- ProjectUID *int
- DatabaseUID *int
-
- // Domain fields
- Title *string
- Visibilities []SheetVisibility
- Source *SheetSource
- NotSource *SheetSource
- Type *SheetType
- PayloadType *string
- // Used to find (un)starred/pinned sheet list, could be PRIVATE/PROJECT/PUBLIC sheet.
- // For now, we only need the starred sheets.
- OrganizerPrincipalIDStarred *int
- OrganizerPrincipalIDNotStarred *int
- // Used to find a sheet list from projects containing PrincipalID as an active member.
- // When finding a shared PROJECT/PUBLIC sheets, this value should be present.
- PrincipalID *int
- // Used to filter the sheet list by schema design fields.
- SchemaDesignFilter *string
+ ProjectUID *int
}
// PatchSheetMessage is the message to patch a sheet.
type PatchSheetMessage struct {
- UID int
- UpdaterID int
- Title *string
- Statement *string
- Visibility *string
- ProjectUID *int
- DatabaseUID *int
- Payload *storepb.SheetPayload
+ UID int
+ UpdaterID int
+ Statement *string
}
// GetSheetStatementByID gets the statement of a sheet by ID.
@@ -133,24 +87,22 @@ func (s *Store) GetSheetStatementByID(ctx context.Context, id int) (string, erro
return v, nil
}
- sheets, err := s.ListSheets(ctx, &FindSheetMessage{UID: &id, LoadFull: true}, SystemBotID)
+ sheet, err := s.GetSheet(ctx, &FindSheetMessage{UID: &id, LoadFull: true})
if err != nil {
return "", err
}
- if len(sheets) == 0 {
+ if sheet == nil {
return "", errors.Errorf("sheet not found with id %d", id)
}
- if len(sheets) > 1 {
- return "", errors.Errorf("expected 1 sheet, got %d", len(sheets))
- }
- statement := sheets[0].Statement
+
+ statement := sheet.Statement
s.sheetCache.Add(id, statement)
return statement, nil
}
// GetSheet gets a sheet.
-func (s *Store) GetSheet(ctx context.Context, find *FindSheetMessage, currentPrincipalID int) (*SheetMessage, error) {
- sheets, err := s.ListSheets(ctx, find, currentPrincipalID)
+func (s *Store) GetSheet(ctx context.Context, find *FindSheetMessage) (*SheetMessage, error) {
+ sheets, err := s.listSheets(ctx, find)
if err != nil {
return nil, err
}
@@ -165,69 +117,26 @@ func (s *Store) GetSheet(ctx context.Context, find *FindSheetMessage, currentPri
return sheet, nil
}
-// ListSheets returns a list of sheets.
-func (s *Store) ListSheets(ctx context.Context, find *FindSheetMessage, currentPrincipalID int) ([]*SheetMessage, error) {
+// listSheets returns a list of sheets.
+func (s *Store) listSheets(ctx context.Context, find *FindSheetMessage) ([]*SheetMessage, error) {
where, args := []string{"TRUE"}, []any{}
+ where, args = append(where, fmt.Sprintf("sheet.source = $%d", len(args)+1)), append(args, SheetFromBytebaseArtifact)
+ where, args = append(where, fmt.Sprintf("sheet.type = $%d", len(args)+1)), append(args, SheetForSQL)
+ // Standard fields
if v := find.UID; v != nil {
where, args = append(where, fmt.Sprintf("sheet.id = $%d", len(args)+1)), append(args, *v)
}
-
- // Standard fields
- if v := find.RowStatus; v != nil {
- where, args = append(where, fmt.Sprintf("sheet.row_status = $%d", len(args)+1)), append(args, *v)
- }
if v := find.CreatorID; v != nil {
where, args = append(where, fmt.Sprintf("sheet.creator_id = $%d", len(args)+1)), append(args, *v)
}
- if v := find.ExcludedCreatorID; v != nil {
- where, args = append(where, fmt.Sprintf("sheet.creator_id != $%d", len(args)+1)), append(args, *v)
- }
// Related fields
if v := find.ProjectUID; v != nil {
where, args = append(where, fmt.Sprintf("sheet.project_id = $%d", len(args)+1)), append(args, *v)
}
- if v := find.Title; v != nil {
- where, args = append(where, fmt.Sprintf("sheet.name = $%d", len(args)+1)), append(args, *v)
- }
- if v := find.DatabaseUID; v != nil {
- where, args = append(where, fmt.Sprintf("sheet.database_id = $%d", len(args)+1)), append(args, *v)
- }
// Domain fields
- visibilitiesWhere := []string{}
- for _, v := range find.Visibilities {
- visibilitiesWhere, args = append(visibilitiesWhere, fmt.Sprintf("visibility = $%d", len(args)+1)), append(args, v)
- }
- if len(visibilitiesWhere) > 0 {
- where = append(where, fmt.Sprintf("(%s)", strings.Join(visibilitiesWhere, " OR ")))
- }
- if v := find.PrincipalID; v != nil {
- where, args = append(where, fmt.Sprintf("sheet.project_id IN (SELECT project_id FROM project_member WHERE principal_id = $%d)", len(args)+1)), append(args, *v)
- }
- if v := find.OrganizerPrincipalIDStarred; v != nil {
- where, args = append(where, fmt.Sprintf("sheet.id IN (SELECT sheet_id FROM sheet_organizer WHERE principal_id = $%d AND starred = true)", len(args)+1)), append(args, *v)
- }
- if v := find.OrganizerPrincipalIDNotStarred; v != nil {
- where, args = append(where, fmt.Sprintf("sheet.id IN (SELECT sheet_id FROM sheet_organizer WHERE principal_id = $%d AND starred = false)", len(args)+1)), append(args, *v)
- }
-
- if v := find.Source; v != nil {
- where, args = append(where, fmt.Sprintf("sheet.source = $%d", len(args)+1)), append(args, *v)
- }
- if v := find.NotSource; v != nil {
- where, args = append(where, fmt.Sprintf("sheet.source != $%d", len(args)+1)), append(args, *v)
- }
- if v := find.Type; v != nil {
- where, args = append(where, fmt.Sprintf("sheet.type = $%d", len(args)+1)), append(args, *v)
- }
- if v := find.PayloadType; v != nil {
- where, args = append(where, fmt.Sprintf("sheet.payload->>'type' = $%d", len(args)+1)), append(args, *v)
- }
- if v := find.SchemaDesignFilter; v != nil {
- where = append(where, *v)
- }
statementField := fmt.Sprintf("LEFT(sheet.statement, %d)", common.MaxSheetSize)
if find.LoadFull {
statementField = "sheet.statement"
@@ -242,7 +151,6 @@ func (s *Store) ListSheets(ctx context.Context, find *FindSheetMessage, currentP
rows, err := tx.QueryContext(ctx, fmt.Sprintf(`
SELECT
sheet.id,
- sheet.row_status,
sheet.creator_id,
sheet.created_ts,
sheet.updater_id,
@@ -251,16 +159,10 @@ func (s *Store) ListSheets(ctx context.Context, find *FindSheetMessage, currentP
sheet.database_id,
sheet.name,
%s,
- sheet.visibility,
- sheet.source,
- sheet.type,
sheet.payload,
- OCTET_LENGTH(sheet.statement),
- COALESCE(sheet_organizer.starred, FALSE),
- COALESCE(sheet_organizer.pinned, FALSE)
+ OCTET_LENGTH(sheet.statement)
FROM sheet
- LEFT JOIN sheet_organizer ON sheet_organizer.sheet_id = sheet.id AND sheet_organizer.principal_id = %d
- WHERE %s`, statementField, currentPrincipalID, strings.Join(where, " AND ")),
+ WHERE %s`, statementField, strings.Join(where, " AND ")),
args...,
)
if err != nil {
@@ -274,7 +176,6 @@ func (s *Store) ListSheets(ctx context.Context, find *FindSheetMessage, currentP
var payload []byte
if err := rows.Scan(
&sheet.UID,
- &sheet.rowStatus,
&sheet.CreatorID,
&sheet.createdTs,
&sheet.UpdaterID,
@@ -283,13 +184,8 @@ func (s *Store) ListSheets(ctx context.Context, find *FindSheetMessage, currentP
&sheet.DatabaseUID,
&sheet.Title,
&sheet.Statement,
- &sheet.Visibility,
- &sheet.Source,
- &sheet.Type,
&payload,
&sheet.Size,
- &sheet.Starred,
- &sheet.Pinned,
); err != nil {
return nil, err
}
@@ -340,7 +236,7 @@ func (s *Store) CreateSheet(ctx context.Context, create *SheetMessage) (*SheetMe
payload
)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
- RETURNING id, row_status, created_ts, updated_ts, OCTET_LENGTH(statement)
+ RETURNING id, created_ts, updated_ts, OCTET_LENGTH(statement)
`
tx, err := s.db.BeginTx(ctx, nil)
@@ -356,13 +252,12 @@ func (s *Store) CreateSheet(ctx context.Context, create *SheetMessage) (*SheetMe
create.DatabaseUID,
create.Title,
create.Statement,
- create.Visibility,
- create.Source,
- create.Type,
+ ProjectWorkSheet,
+ SheetFromBytebaseArtifact,
+ SheetForSQL,
payload,
).Scan(
&create.UID,
- &create.rowStatus,
&create.createdTs,
&create.updatedTs,
&create.Size,
@@ -397,7 +292,9 @@ func (s *Store) PatchSheet(ctx context.Context, patch *PatchSheetMessage) (*Shee
if err := tx.Commit(); err != nil {
return nil, errors.Wrapf(err, "failed to commit transaction")
}
- s.sheetCache.Remove(patch.UID)
+ if v := patch.Statement; v != nil {
+ s.sheetCache.Add(patch.UID, *v)
+ }
return sheet, nil
}
@@ -424,28 +321,9 @@ func (s *Store) DeleteSheet(ctx context.Context, sheetUID int) error {
// patchSheetImpl updates a sheet's name/statement/visibility/payload/database_id/project_id.
func patchSheetImpl(ctx context.Context, tx *Tx, patch *PatchSheetMessage) (*SheetMessage, error) {
set, args := []string{"updater_id = $1"}, []any{patch.UpdaterID}
- if v := patch.Title; v != nil {
- set, args = append(set, fmt.Sprintf("name = $%d", len(args)+1)), append(args, *v)
- }
if v := patch.Statement; v != nil {
set, args = append(set, fmt.Sprintf("statement = $%d", len(args)+1)), append(args, *v)
}
- if v := patch.Visibility; v != nil {
- set, args = append(set, fmt.Sprintf("visibility = $%d", len(args)+1)), append(args, *v)
- }
- if v := patch.Payload; v != nil {
- payload, err := protojson.Marshal(patch.Payload)
- if err != nil {
- return nil, err
- }
- set, args = append(set, fmt.Sprintf("payload = $%d", len(args)+1)), append(args, payload)
- }
- if v := patch.DatabaseUID; v != nil {
- set, args = append(set, fmt.Sprintf("database_id = $%d", len(args)+1)), append(args, *v)
- }
- if v := patch.ProjectUID; v != nil {
- set, args = append(set, fmt.Sprintf("project_id = $%d", len(args)+1)), append(args, *v)
- }
args = append(args, patch.UID)
@@ -457,12 +335,11 @@ func patchSheetImpl(ctx context.Context, tx *Tx, patch *PatchSheetMessage) (*She
UPDATE sheet
SET `+strings.Join(set, ", ")+`
WHERE id = $%d
- RETURNING id, row_status, creator_id, created_ts, updater_id, updated_ts, project_id, database_id, name, LEFT(statement, %d), visibility, source, type, payload, OCTET_LENGTH(statement)
+ RETURNING id, creator_id, created_ts, updater_id, updated_ts, project_id, database_id, name, LEFT(statement, %d), payload, OCTET_LENGTH(statement)
`, len(args), common.MaxSheetSize),
args...,
).Scan(
&sheet.UID,
- &sheet.rowStatus,
&sheet.CreatorID,
&sheet.createdTs,
&sheet.UpdaterID,
@@ -471,9 +348,6 @@ func patchSheetImpl(ctx context.Context, tx *Tx, patch *PatchSheetMessage) (*She
&databaseID,
&sheet.Title,
&sheet.Statement,
- &sheet.Visibility,
- &sheet.Source,
- &sheet.Type,
&payload,
&sheet.Size,
); err != nil {
diff --git a/frontend/src/components/Changelist/ChangelistDetail/RawSQLPanel/RawSQLPanel.vue b/frontend/src/components/Changelist/ChangelistDetail/RawSQLPanel/RawSQLPanel.vue
index c3d0d8f616..fd8b4ef2d4 100644
--- a/frontend/src/components/Changelist/ChangelistDetail/RawSQLPanel/RawSQLPanel.vue
+++ b/frontend/src/components/Changelist/ChangelistDetail/RawSQLPanel/RawSQLPanel.vue
@@ -158,7 +158,7 @@ const doSaveChange = async () => {
name: sheet.value.name,
});
setSheetStatement(patch, statement.value);
- await sheetStore.patchSheet(patch, ["content"]);
+ await sheetStore.patchSheetContent(patch);
pushNotification({
module: "bytebase",
diff --git a/frontend/src/components/SyncDatabaseSchema/RawSQLEditor.vue b/frontend/src/components/SyncDatabaseSchema/RawSQLEditor.vue
index 41f6cd63a4..b9950b389d 100644
--- a/frontend/src/components/SyncDatabaseSchema/RawSQLEditor.vue
+++ b/frontend/src/components/SyncDatabaseSchema/RawSQLEditor.vue
@@ -237,13 +237,10 @@ const handleUpdateSheet = useDebounceFn(async (statement: string) => {
return;
}
- await sheetStore.patchSheet(
- {
- name: sheet.value.name,
- content: new TextEncoder().encode(statement),
- },
- ["content"]
- );
+ await sheetStore.patchSheetContent({
+ name: sheet.value.name,
+ content: new TextEncoder().encode(statement),
+ });
}, 1000);
const update = (sheetId?: number) => {
diff --git a/frontend/src/store/modules/v1/sheet.ts b/frontend/src/store/modules/v1/sheet.ts
index 39c44307f8..e8629138a0 100644
--- a/frontend/src/store/modules/v1/sheet.ts
+++ b/frontend/src/store/modules/v1/sheet.ts
@@ -127,11 +127,11 @@ export const useSheetV1Store = defineStore("sheet_v1", () => {
return getOrFetchSheetByName(`projects/-/sheets/${uid}`, view);
};
- const patchSheet = async (sheet: Partial<Sheet>, updateMask: string[]) => {
+ const patchSheetContent = async (sheet: Partial<Sheet>) => {
if (!sheet.name) return;
const updated = await sheetServiceClient.updateSheet({
sheet,
- updateMask,
+ updateMask: ["content"],
});
setCache(updated, "FULL");
return updated;
@@ -144,7 +144,7 @@ export const useSheetV1Store = defineStore("sheet_v1", () => {
getOrFetchSheetByName,
getSheetByUID,
getOrFetchSheetByUID,
- patchSheet,
+ patchSheetContent,
};
});