Skip to content

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

Issue/Pipeline/Stage/Task model implementation

天舟9371a8ed5038
diff --git a/backend/api/issue.go b/backend/api/issue.go
new file mode 100644
index 0000000000..0af4eab816
--- /dev/null
+++ b/backend/api/issue.go
@@ -0,0 +1,135 @@
+package api
+
+import "context"
+
+// Issue status
+type IssueStatus string
+
+const (
+ Issue_Open IssueStatus = "OPEN"
+ Issue_Done IssueStatus = "DONE"
+ Issue_Canceled IssueStatus = "CANCELED"
+)
+
+func (e IssueStatus) String() string {
+ switch e {
+ case Issue_Open:
+ return "OPEN"
+ case Issue_Done:
+ return "DONE"
+ case Issue_Canceled:
+ return "CANCELED"
+ }
+ return ""
+}
+
+// Issue type
+type IssueType string
+
+const (
+ IssueGeneral IssueType = "bb.general"
+ IssueDatabaseCreate IssueType = "bb.database.create"
+ IssueDatabaseGrant IssueType = "bb.database.grant"
+ IssueDatabaseSchemaUpdate IssueType = "bb.database.schema.update"
+ IssueDataSourceRequest IssueType = "bb.data-source.request"
+)
+
+func (e IssueType) String() string {
+ switch e {
+ case IssueGeneral:
+ return "bb.general"
+ case IssueDatabaseCreate:
+ return "bb.database.create"
+ case IssueDatabaseGrant:
+ return "bb.database.grant"
+ case IssueDatabaseSchemaUpdate:
+ return "bb.database.schema.update"
+ case IssueDataSourceRequest:
+ return "bb.data-source.request"
+ }
+ return "bb.unknown"
+}
+
+type Issue struct {
+ ID int `jsonapi:"primary,issue"`
+
+ // Standard fields
+ CreatorId int `jsonapi:"attr,creatorId"`
+ CreatedTs int64 `jsonapi:"attr,createdTs"`
+ UpdaterId int `jsonapi:"attr,updaterId"`
+ UpdatedTs int64 `jsonapi:"attr,updatedTs"`
+ WorkspaceId int
+
+ // Related fields
+ ProjectId int
+ Project *Project `jsonapi:"relation,project"`
+ PipelineId int
+ // Pipeline *Pipeline `jsonapi:"relation,Pipeline"`
+
+ // Domain specific fields
+ Name string `jsonapi:"attr,name"`
+ Status IssueStatus `jsonapi:"attr,status"`
+ Type IssueType `jsonapi:"attr,type"`
+ Description string `jsonapi:"attr,description"`
+ AssigneeId int `jsonapi:"attr,assignee"`
+ SubscriberIdList []int `jsonapi:"attr,subscriberList"`
+ Sql string `jsonapi:"attr,sql"`
+ RollbackSql string `jsonapi:"attr,rollbackSql"`
+ Payload string `jsonapi:"attr,payload"`
+}
+
+type IssueCreate struct {
+ // Standard fields
+ // Value is assigned from the jwt subject field passed by the client.
+ CreatorId int
+ WorkspaceId int
+
+ // Related fields
+ ProjectId int `jsonapi:"attr,projectId"`
+ PipelineId int `jsonapi:"attr,pipelineId"`
+
+ // Domain specific fields
+ Name string `jsonapi:"attr,name"`
+ Type IssueType `jsonapi:"attr,type"`
+ Description string `jsonapi:"attr,description"`
+ AssigneeId int `jsonapi:"attr,assignee"`
+ SubscriberLIdist []int `jsonapi:"attr,subscriberList"`
+ Sql string `jsonapi:"attr,sql"`
+ RollbackSql string `jsonapi:"attr,rollbackSql"`
+ Payload string `jsonapi:"attr,payload"`
+}
+
+type IssueFind struct {
+ ID *int
+
+ // Standard fields
+ WorkspaceId *int
+}
+
+type IssuePatch struct {
+ ID int `jsonapi:"primary,issuePatch"`
+
+ // Standard fields
+ // Value is assigned from the jwt subject field passed by the client.
+ UpdaterId int
+ WorkspaceId int
+
+ // Related fields
+ ProjectId *int `jsonapi:"attr,projectId"`
+
+ // Domain specific fields
+ Name *string `jsonapi:"attr,name"`
+ Description *string `jsonapi:"attr,description"`
+ AssigneeId *int `jsonapi:"attr,assignee"`
+ SubscriberIdList *[]int `jsonapi:"attr,subscriberList"`
+ Sql *string `jsonapi:"attr,sql"`
+ RollbackSql *string `jsonapi:"attr,rollbackSql"`
+ Payload *string `jsonapi:"attr,payload"`
+}
+
+type IssueService interface {
+ CreateIssue(ctx context.Context, create *IssueCreate) (*Issue, error)
+ FindIssueList(ctx context.Context, find *IssueFind) ([]*Issue, error)
+ FindIssue(ctx context.Context, find *IssueFind) (*Issue, error)
+ PatchIssueByID(ctx context.Context, patch *IssuePatch) (*Issue, error)
+}
diff --git a/backend/api/pipeline.go b/backend/api/pipeline.go
new file mode 100644
index 0000000000..a2cd300a11
--- /dev/null
+++ b/backend/api/pipeline.go
@@ -0,0 +1,77 @@
+package api
+
+import "context"
+
+// Pipeline status
+type PipelineStatus string
+
+const (
+ Pipeline_Open PipelineStatus = "OPEN"
+ Pipeline_Done PipelineStatus = "DONE"
+ Pipeline_Canceled PipelineStatus = "CANCELED"
+)
+
+func (e PipelineStatus) String() string {
+ switch e {
+ case Pipeline_Open:
+ return "OPEN"
+ case Pipeline_Done:
+ return "DONE"
+ case Pipeline_Canceled:
+ return "CANCELED"
+ }
+ return ""
+}
+
+type Pipeline struct {
+ ID int `jsonapi:"primary,pipeline"`
+
+ // Standard fields
+ CreatorId int `jsonapi:"attr,creatorId"`
+ CreatedTs int64 `jsonapi:"attr,createdTs"`
+ UpdaterId int `jsonapi:"attr,updaterId"`
+ UpdatedTs int64 `jsonapi:"attr,updatedTs"`
+ WorkspaceId int
+
+ // Related fields
+ // StageList []*Stage `jsonapi:"relation,stage"`
+
+ // Domain specific fields
+ Name string `jsonapi:"attr,name"`
+ Status PipelineStatus `jsonapi:"attr,status"`
+}
+
+type PipelineCreate struct {
+ // Standard fields
+ // Value is assigned from the jwt subject field passed by the client.
+ CreatorId int
+ WorkspaceId int
+
+ // Domain specific fields
+ Name string `jsonapi:"attr,name"`
+}
+
+type PipelineFind struct {
+ ID *int
+
+ // Standard fields
+ WorkspaceId *int
+}
+
+type PipelinePatch struct {
+ ID int `jsonapi:"primary,pipelinePatch"`
+
+ // Standard fields
+ // Value is assigned from the jwt subject field passed by the client.
+ UpdaterId int
+ WorkspaceId int
+
+ // Domain specific fields
+ Status *PipelineStatus `jsonapi:"attr,status"`
+}
+
+type PipelineService interface {
+ CreatePipeline(ctx context.Context, create *PipelineCreate) (*Pipeline, error)
+ FindPipeline(ctx context.Context, find *PipelineFind) (*Pipeline, error)
+ PatchPipelineByID(ctx context.Context, patch *PipelinePatch) (*Pipeline, error)
+}
diff --git a/backend/api/stage.go b/backend/api/stage.go
new file mode 100644
index 0000000000..32a55e676f
--- /dev/null
+++ b/backend/api/stage.go
@@ -0,0 +1,79 @@
+package api
+
+import "context"
+
+type StageType string
+
+const (
+ StageGeneral StageType = "bb.stage.general"
+ StageDatabaseCreate StageType = "bb.stage.database.create"
+ StageDatabaseGrant StageType = "bb.stage.database.grant"
+ StageDatabaseSchemaUpdate StageType = "bb.stage.database.schema.update"
+)
+
+func (e StageType) String() string {
+ switch e {
+ case StageGeneral:
+ return "bb.stage.general"
+ case StageDatabaseCreate:
+ return "bb.stage.database.create"
+ case StageDatabaseGrant:
+ return "bb.stage.database.grant"
+ case StageDatabaseSchemaUpdate:
+ return "bb.stage.database.schema.update"
+ }
+ return "bb.stage.unknown"
+}
+
+type Stage struct {
+ ID int `jsonapi:"primary,stage"`
+
+ // Standard fields
+ CreatorId int `jsonapi:"attr,creatorId"`
+ CreatedTs int64 `jsonapi:"attr,createdTs"`
+ UpdaterId int `jsonapi:"attr,updaterId"`
+ UpdatedTs int64 `jsonapi:"attr,updatedTs"`
+ WorkspaceId int
+
+ // Related fields
+ Environment *Environment `jsonapi:"relation,environment"`
+ EnvironmentId int
+ Pipeline *Pipeline `jsonapi:"relation,pipeline"`
+ PipelineId int
+ // TaskList []*Stage `jsonapi:"relation,task"`
+
+ // Domain specific fields
+ Name string `jsonapi:"attr,name"`
+ Type StageType `jsonapi:"attr,type"`
+}
+
+type StageCreate struct {
+ // Standard fields
+ // Value is assigned from the jwt subject field passed by the client.
+ CreatorId int
+ WorkspaceId int
+
+ // Related fields
+ EnvironmentId int `jsonapi:"relation,environmentId"`
+ PipelineId int `jsonapi:"relation,pipelineId"`
+
+ // Domain specific fields
+ Name string `jsonapi:"attr,name"`
+ Type StageType `jsonapi:"attr,type"`
+}
+
+type StageFind struct {
+ ID *int
+
+ // Standard fields
+ WorkspaceId *int
+
+ // Related fields
+ PipelineId *int
+}
+
+type StageService interface {
+ CreateStage(ctx context.Context, create *StageCreate) (*Stage, error)
+ FindStageList(ctx context.Context, find *StageFind) ([]*Stage, error)
+ FindStage(ctx context.Context, find *StageFind) (*Stage, error)
+}
diff --git a/backend/api/task.go b/backend/api/task.go
new file mode 100644
index 0000000000..865384fbc0
--- /dev/null
+++ b/backend/api/task.go
@@ -0,0 +1,121 @@
+package api
+
+import "context"
+
+type TaskStatus string
+
+const (
+ TaskPending TaskStatus = "PENDING"
+ TaskRunning TaskStatus = "RUNNING"
+ TaskDone TaskStatus = "DONE"
+ TaskFailed TaskStatus = "FAILED"
+ TaskSkipped TaskStatus = "SKIPPED"
+)
+
+func (e TaskStatus) String() string {
+ switch e {
+ case TaskPending:
+ return "PENDING"
+ case TaskRunning:
+ return "RUNNING"
+ case TaskDone:
+ return "DONE"
+ case TaskFailed:
+ return "FAILED"
+ case TaskSkipped:
+ return "SKIPPED"
+ }
+ return "UNKNOWN"
+}
+
+type TaskType string
+
+const (
+ TaskGeneral TaskType = "bb.task.general"
+ TaskApprove TaskType = "bb.task.approve"
+ TaskDatabaseSchemaUpdate TaskType = "bb.task.database.schema.update"
+)
+
+type TaskWhen string
+
+const (
+ TaskOnSuccess TaskWhen = "ON_SUCCESS"
+ TaskManual TaskWhen = "MANUAL"
+)
+
+type Task struct {
+ ID int `jsonapi:"primary,task"`
+
+ // Standard fields
+ CreatorId int `jsonapi:"attr,creatorId"`
+ CreatedTs int64 `jsonapi:"attr,createdTs"`
+ UpdaterId int `jsonapi:"attr,updaterId"`
+ UpdatedTs int64 `jsonapi:"attr,updatedTs"`
+ WorkspaceId int
+
+ // Related fields
+ Pipeline *Pipeline `jsonapi:"relation,pipeline"`
+ PipelineId int
+ Stage *Stage `jsonapi:"relation,stage"`
+ StageId int
+ Database *Database `jsonapi:"relation,database"`
+ DatabaseId int
+
+ // Domain specific fields
+ Name string `jsonapi:"attr,name"`
+ Status TaskStatus `jsonapi:"attr,status"`
+ Type TaskType `jsonapi:"attr,type"`
+ When TaskWhen `jsonapi:"attr,when"`
+ Payload string `jsonapi:"attr,payload"`
+}
+
+type TaskCreate struct {
+ // Standard fields
+ // Value is assigned from the jwt subject field passed by the client.
+ CreatorId int
+ WorkspaceId int
+
+ // Related fields
+ PipelineId int `jsonapi:"relation,pipelineId"`
+ StageId int `jsonapi:"relation,stageId"`
+ DatabaseId int `jsonapi:"relation,databaseId"`
+
+ // Domain specific fields
+ Name string `jsonapi:"attr,name"`
+ Type TaskType `jsonapi:"attr,type"`
+ When TaskWhen `jsonapi:"attr,when"`
+ Payload string `jsonapi:"attr,payload"`
+}
+
+type TaskFind struct {
+ ID *int
+
+ // Standard fields
+ WorkspaceId *int
+
+ // Related fields
+ PipelineId *int
+ StageId *int
+}
+
+type TaskPatch struct {
+ ID int `jsonapi:"primary,environmentPatch"`
+
+ // Standard fields
+ // Value is assigned from the jwt subject field passed by the client.
+ UpdaterId int
+ WorkspaceId int
+
+ // Domain specific fields
+ // This is the container containing the pipeline this task belongs.
+ ContainerId *int
+ Status *TaskStatus `jsonapi:"attr,status"`
+ Comment *string `jsonapi:"attr,comment"`
+}
+
+type TaskService interface {
+ CreateTask(ctx context.Context, create *TaskCreate) (*Task, error)
+ FindTaskList(ctx context.Context, find *TaskFind) ([]*Task, error)
+ FindTask(ctx context.Context, find *TaskFind) (*Task, error)
+ PatchTaskByID(ctx context.Context, patch *TaskPatch) (*Task, error)
+}
diff --git a/backend/store/issue.go b/backend/store/issue.go
new file mode 100644
index 0000000000..e121502d76
--- /dev/null
+++ b/backend/store/issue.go
@@ -0,0 +1,259 @@
+package store
+
+import (
+ "context"
+ "fmt"
+ "strings"
+
+ "github.com/bytebase/bytebase"
+ "github.com/bytebase/bytebase/api"
+)
+
+var (
+ _ api.IssueService = (*IssueService)(nil)
+)
+
+// IssueService represents a service for managing issue.
+type IssueService struct {
+ l *bytebase.Logger
+ db *DB
+}
+
+// NewIssueService returns a new instance of IssueService.
+func NewIssueService(logger *bytebase.Logger, db *DB) *IssueService {
+ return &IssueService{l: logger, db: db}
+}
+
+// CreateIssue creates a new issue.
+func (s *IssueService) CreateIssue(ctx context.Context, create *api.IssueCreate) (*api.Issue, error) {
+ tx, err := s.db.BeginTx(ctx, nil)
+ if err != nil {
+ return nil, FormatError(err)
+ }
+ defer tx.Rollback()
+
+ issue, err := s.createIssue(ctx, tx, create)
+ if err != nil {
+ return nil, err
+ }
+
+ if err := tx.Commit(); err != nil {
+ return nil, FormatError(err)
+ }
+
+ return issue, nil
+}
+
+// FindIssueList retrieves a list of issues based on find.
+func (s *IssueService) FindIssueList(ctx context.Context, find *api.IssueFind) ([]*api.Issue, error) {
+ tx, err := s.db.BeginTx(ctx, nil)
+ if err != nil {
+ return nil, FormatError(err)
+ }
+ defer tx.Rollback()
+
+ list, err := s.findIssueList(ctx, tx, find)
+ if err != nil {
+ return []*api.Issue{}, err
+ }
+
+ return list, nil
+}
+
+// FindIssue retrieves a single issue based on find.
+// Returns ENOTFOUND if no matching record.
+// Returns the first matching one and prints a warning if finding more than 1 matching records.
+func (s *IssueService) FindIssue(ctx context.Context, find *api.IssueFind) (*api.Issue, error) {
+ tx, err := s.db.BeginTx(ctx, nil)
+ if err != nil {
+ return nil, FormatError(err)
+ }
+ defer tx.Rollback()
+
+ list, err := s.findIssueList(ctx, tx, find)
+ if err != nil {
+ return nil, err
+ } else if len(list) == 0 {
+ return nil, &bytebase.Error{Code: bytebase.ENOTFOUND, Message: fmt.Sprintf("issue not found: %v", find)}
+ } else if len(list) > 1 {
+ s.l.Warnf("found mulitple issues: %d, expect 1", len(list))
+ }
+ return list[0], nil
+}
+
+// PatchIssueByID updates an existing issue by ID.
+// Returns ENOTFOUND if issue does not exist.
+func (s *IssueService) PatchIssueByID(ctx context.Context, patch *api.IssuePatch) (*api.Issue, error) {
+ tx, err := s.db.BeginTx(ctx, nil)
+ if err != nil {
+ return nil, FormatError(err)
+ }
+ defer tx.Rollback()
+
+ issue, err := s.patchIssue(ctx, tx, patch)
+ if err != nil {
+ return nil, FormatError(err)
+ }
+
+ if err := tx.Commit(); err != nil {
+ return nil, FormatError(err)
+ }
+
+ return issue, nil
+}
+
+// createIssue creates a new issue.
+func (s *IssueService) createIssue(ctx context.Context, tx *Tx, create *api.IssueCreate) (*api.Issue, error) {
+ row, err := tx.QueryContext(ctx, `
+ INSERT INTO issue (
+ creator_id,
+ updater_id,
+ workspace_id,
+ project_id,
+ pipeline_id,
+ name
+ )
+ VALUES (?, ?, ?, ?, ?, 'OPEN')
+ RETURNING id, creator_id, created_ts, updater_id, updated_ts, workspace_id, project_id, pipeline_id, name, `+"`status`, "+`payload
+ `,
+ create.CreatorId,
+ create.CreatorId,
+ create.WorkspaceId,
+ create.ProjectId,
+ create.PipelineId,
+ create.Name,
+ )
+
+ if err != nil {
+ return nil, FormatError(err)
+ }
+ defer row.Close()
+
+ row.Next()
+ var issue api.Issue
+ if err := row.Scan(
+ &issue.ID,
+ &issue.CreatorId,
+ &issue.CreatedTs,
+ &issue.UpdaterId,
+ &issue.UpdatedTs,
+ &issue.WorkspaceId,
+ &issue.ProjectId,
+ &issue.PipelineId,
+ &issue.Name,
+ &issue.Status,
+ &issue.Payload,
+ ); err != nil {
+ return nil, FormatError(err)
+ }
+
+ return &issue, nil
+}
+
+func (s *IssueService) findIssueList(ctx context.Context, tx *Tx, find *api.IssueFind) (_ []*api.Issue, err error) {
+ // Build WHERE clause.
+ where, args := []string{"1 = 1"}, []interface{}{}
+ if v := find.ID; v != nil {
+ where, args = append(where, "id = ?"), append(args, *v)
+ }
+ if v := find.WorkspaceId; v != nil {
+ where, args = append(where, "workspace_id = ?"), append(args, *v)
+ }
+
+ rows, err := tx.QueryContext(ctx, `
+ SELECT
+ id,
+ creator_id,
+ created_ts,
+ updater_id,
+ updated_ts,
+ workspace_id,
+ project_id,
+ pipeline_id,
+ name,
+ `+"`status`,"+`
+ payload
+ FROM issue
+ WHERE `+strings.Join(where, " AND "),
+ args...,
+ )
+ if err != nil {
+ return nil, FormatError(err)
+ }
+ defer rows.Close()
+
+ // Iterate over result set and deserialize rows into list.
+ list := make([]*api.Issue, 0)
+ for rows.Next() {
+ var issue api.Issue
+ if err := rows.Scan(
+ &issue.ID,
+ &issue.CreatorId,
+ &issue.CreatedTs,
+ &issue.UpdaterId,
+ &issue.UpdatedTs,
+ &issue.WorkspaceId,
+ &issue.ProjectId,
+ &issue.PipelineId,
+ &issue.Name,
+ &issue.Status,
+ &issue.Payload,
+ ); err != nil {
+ return nil, FormatError(err)
+ }
+
+ list = append(list, &issue)
+ }
+ if err := rows.Err(); err != nil {
+ return nil, FormatError(err)
+ }
+
+ return list, nil
+}
+
+// patchIssue updates a issue by ID. Returns the new state of the issue after update.
+func (s *IssueService) patchIssue(ctx context.Context, tx *Tx, patch *api.IssuePatch) (*api.Issue, error) {
+ // Build UPDATE clause.
+ set, args := []string{"updater_id = ?"}, []interface{}{patch.UpdaterId}
+ if v := patch.Name; v != nil {
+ set, args = append(set, "name = ?"), append(args, *v)
+ }
+
+ args = append(args, patch.ID)
+
+ // Execute update query with RETURNING.
+ row, err := tx.QueryContext(ctx, `
+ UPDATE issue
+ SET `+strings.Join(set, ", ")+`
+ WHERE id = ?
+ RETURNING id, creator_id, created_ts, updater_id, updated_ts, workspace_id, project_id, pipeline_id, name, `+"`status`, "+`payload
+ `,
+ args...,
+ )
+ if err != nil {
+ return nil, FormatError(err)
+ }
+ defer row.Close()
+
+ if row.Next() {
+ var issue api.Issue
+ if err := row.Scan(
+ &issue.ID,
+ &issue.CreatorId,
+ &issue.CreatedTs,
+ &issue.UpdaterId,
+ &issue.UpdatedTs,
+ &issue.WorkspaceId,
+ &issue.ProjectId,
+ &issue.PipelineId,
+ &issue.Name,
+ &issue.Status,
+ &issue.Payload,
+ ); err != nil {
+ return nil, FormatError(err)
+ }
+ return &issue, nil
+ }
+
+ return nil, &bytebase.Error{Code: bytebase.ENOTFOUND, Message: fmt.Sprintf("issue ID not found: %d", patch.ID)}
+}
diff --git a/backend/store/migration/0001_init_schema.down.sql b/backend/store/migration/0001_init_schema.down.sql
index 1c8948d1a5..0b6c42e72c 100644
--- a/backend/store/migration/0001_init_schema.down.sql
+++ b/backend/store/migration/0001_init_schema.down.sql
@@ -1,4 +1,12 @@
-DROP TABLE IF EXISTS data_source;
+DROP TABLE task;
+
+DROP TABLE stage;
+
+DROP TABLE pipeline;
+
+DROP TABLE issue;
+
+DROP TABLE data_source;
DROP TABLE db;
diff --git a/backend/store/migration/0001_init_schema.up.sql b/backend/store/migration/0001_init_schema.up.sql
index f5e01d4b3c..64c5211a33 100644
--- a/backend/store/migration/0001_init_schema.up.sql
+++ b/backend/store/migration/0001_init_schema.up.sql
@@ -378,4 +378,149 @@ SET
WHERE
rowid = old.rowid;
-END;
\ No newline at end of file
+END;
+
+-----------------------
+-- Pipeline related BEGIN
+-- pipeline table stores the workspace pipeline
+CREATE TABLE pipeline (
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
+ row_status TEXT NOT NULL CHECK (
+ row_status IN ('NORMAL', 'ARCHIVED', 'PENDING_DELETE')
+ ) DEFAULT 'NORMAL',
+ creator_id INTEGER NOT NULL REFERENCES principal (id),
+ created_ts BIGINT NOT NULL DEFAULT (strftime('%s', 'now')),
+ updater_id INTEGER NOT NULL REFERENCES principal (id),
+ updated_ts BIGINT NOT NULL DEFAULT (strftime('%s', 'now')),
+ workspace_id INTEGER NOT NULL REFERENCES workspace (id),
+ name TEXT NOT NULL,
+ `status` TEXT NOT NULL CHECK (`status` IN ('OPEN', 'DONE', 'CANCELED'))
+);
+
+INSERT INTO
+ sqlite_sequence (name, seq)
+VALUES
+ ('pipeline', 1000);
+
+CREATE TRIGGER IF NOT EXISTS `trigger_update_pipeline_modification_time`
+AFTER
+UPDATE
+ ON `pipeline` FOR EACH ROW BEGIN
+UPDATE
+ `pipeline`
+SET
+ updated_ts = (strftime('%s', 'now'))
+WHERE
+ rowid = old.rowid;
+
+END;
+
+-- stage table stores the stage for the pipeline
+CREATE TABLE stage (
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
+ row_status TEXT NOT NULL CHECK (
+ row_status IN ('NORMAL', 'ARCHIVED', 'PENDING_DELETE')
+ ) DEFAULT 'NORMAL',
+ creator_id INTEGER NOT NULL REFERENCES principal (id),
+ created_ts BIGINT NOT NULL DEFAULT (strftime('%s', 'now')),
+ updater_id INTEGER NOT NULL REFERENCES principal (id),
+ updated_ts BIGINT NOT NULL DEFAULT (strftime('%s', 'now')),
+ workspace_id INTEGER NOT NULL REFERENCES workspace (id),
+ pipeline_id INTEGER NOT NULL REFERENCES pipeline (id),
+ environment_id INTEGER NOT NULL REFERENCES environment (id),
+ name TEXT NOT NULL,
+ `type` TEXT NOT NULL CHECK (`type` LIKE 'bb.stage.%')
+);
+
+INSERT INTO
+ sqlite_sequence (name, seq)
+VALUES
+ ('stage', 1000);
+
+CREATE TRIGGER IF NOT EXISTS `trigger_update_stage_modification_time`
+AFTER
+UPDATE
+ ON `stage` FOR EACH ROW BEGIN
+UPDATE
+ `stage`
+SET
+ updated_ts = (strftime('%s', 'now'))
+WHERE
+ rowid = old.rowid;
+
+END;
+
+-- task table stores the task for the stage
+CREATE TABLE task (
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
+ row_status TEXT NOT NULL CHECK (
+ row_status IN ('NORMAL', 'ARCHIVED', 'PENDING_DELETE')
+ ) DEFAULT 'NORMAL',
+ creator_id INTEGER NOT NULL REFERENCES principal (id),
+ created_ts BIGINT NOT NULL DEFAULT (strftime('%s', 'now')),
+ updater_id INTEGER NOT NULL REFERENCES principal (id),
+ updated_ts BIGINT NOT NULL DEFAULT (strftime('%s', 'now')),
+ workspace_id INTEGER NOT NULL REFERENCES workspace (id),
+ pipeline_id INTEGER NOT NULL REFERENCES pipeline (id),
+ stage_id INTEGER NOT NULL REFERENCES stage (id),
+ -- Could be empty for tasks like creating database
+ database_id INTEGER REFERENCES db (id),
+ name TEXT NOT NULL,
+ `status` TEXT NOT NULL CHECK (
+ `status` IN (
+ 'PENDING',
+ 'RUNNING',
+ 'DONE',
+ 'FAILED',
+ "SKIPPED"
+ )
+ ),
+ `type` TEXT NOT NULL CHECK (`type` LIKE 'bb.task.%'),
+ `when` TEXT NOT NULL CHECK (`when` IN ('ON_SUCCESS', 'MANUAL')),
+ payload TEXT NOT NULL DEFAULT ''
+);
+
+INSERT INTO
+ sqlite_sequence (name, seq)
+VALUES
+ ('task', 1000);
+
+CREATE TRIGGER IF NOT EXISTS `trigger_update_task_modification_time`
+AFTER
+UPDATE
+ ON `task` FOR EACH ROW BEGIN
+UPDATE
+ `task`
+SET
+ updated_ts = (strftime('%s', 'now'))
+WHERE
+ rowid = old.rowid;
+
+END;
+
+-- Pipeline related END
+-----------------------
+-- issue table stores the workspace issue
+-- Each issue links a pipeline driving the resolution.
+CREATE TABLE issue (
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
+ row_status TEXT NOT NULL CHECK (
+ row_status IN ('NORMAL', 'ARCHIVED', 'PENDING_DELETE')
+ ) DEFAULT 'NORMAL',
+ creator_id INTEGER NOT NULL REFERENCES principal (id),
+ created_ts BIGINT NOT NULL DEFAULT (strftime('%s', 'now')),
+ updater_id INTEGER NOT NULL REFERENCES principal (id),
+ updated_ts BIGINT NOT NULL DEFAULT (strftime('%s', 'now')),
+ workspace_id INTEGER NOT NULL REFERENCES workspace (id),
+ project_id INTEGER NOT NULL REFERENCES project (id),
+ pipeline_id INTEGER NOT NULL REFERENCES pipeline (id),
+ name TEXT NOT NULL,
+ `status` TEXT NOT NULL CHECK (`status` IN ('OPEN', 'DONE', 'CANCELED')),
+ `type` TEXT NOT NULL CHECK (`type` LIKE 'bb.%'),
+ description TEXT NOT NULL,
+ assignee_id INTEGER REFERENCES principal (id),
+ subscriberList TEXT NOT NULL,
+ `sql` TEXT NOT NULL,
+ rollback_sql TEXT NOT NULL,
+ payload TEXT NOT NULL DEFAULT ''
+);
\ No newline at end of file
diff --git a/backend/store/pipeline.go b/backend/store/pipeline.go
new file mode 100644
index 0000000000..f97b7f2d3e
--- /dev/null
+++ b/backend/store/pipeline.go
@@ -0,0 +1,228 @@
+package store
+
+import (
+ "context"
+ "fmt"
+ "strings"
+
+ "github.com/bytebase/bytebase"
+ "github.com/bytebase/bytebase/api"
+)
+
+var (
+ _ api.PipelineService = (*PipelineService)(nil)
+)
+
+// PipelineService represents a service for managing pipeline.
+type PipelineService struct {
+ l *bytebase.Logger
+ db *DB
+}
+
+// NewPipelineService returns a new instance of PipelineService.
+func NewPipelineService(logger *bytebase.Logger, db *DB) *PipelineService {
+ return &PipelineService{l: logger, db: db}
+}
+
+// CreatePipeline creates a new pipeline.
+func (s *PipelineService) CreatePipeline(ctx context.Context, create *api.PipelineCreate) (*api.Pipeline, error) {
+ tx, err := s.db.BeginTx(ctx, nil)
+ if err != nil {
+ return nil, FormatError(err)
+ }
+ defer tx.Rollback()
+
+ pipeline, err := s.createPipeline(ctx, tx, create)
+ if err != nil {
+ return nil, err
+ }
+
+ if err := tx.Commit(); err != nil {
+ return nil, FormatError(err)
+ }
+
+ return pipeline, nil
+}
+
+// FindPipeline retrieves a single pipeline based on find.
+// Returns ENOTFOUND if no matching record.
+// Returns the first matching one and prints a warning if finding more than 1 matching records.
+func (s *PipelineService) FindPipeline(ctx context.Context, find *api.PipelineFind) (*api.Pipeline, error) {
+ tx, err := s.db.BeginTx(ctx, nil)
+ if err != nil {
+ return nil, FormatError(err)
+ }
+ defer tx.Rollback()
+
+ list, err := s.findPipelineList(ctx, tx, find)
+ if err != nil {
+ return nil, err
+ } else if len(list) == 0 {
+ return nil, &bytebase.Error{Code: bytebase.ENOTFOUND, Message: fmt.Sprintf("pipeline not found: %v", find)}
+ } else if len(list) > 1 {
+ s.l.Warnf("found mulitple pipelines: %d, expect 1", len(list))
+ }
+ return list[0], nil
+}
+
+// PatchPipelineByID updates an existing pipeline by ID.
+// Returns ENOTFOUND if pipeline does not exist.
+func (s *PipelineService) PatchPipelineByID(ctx context.Context, patch *api.PipelinePatch) (*api.Pipeline, error) {
+ tx, err := s.db.BeginTx(ctx, nil)
+ if err != nil {
+ return nil, FormatError(err)
+ }
+ defer tx.Rollback()
+
+ pipeline, err := s.patchPipeline(ctx, tx, patch)
+ if err != nil {
+ return nil, FormatError(err)
+ }
+
+ if err := tx.Commit(); err != nil {
+ return nil, FormatError(err)
+ }
+
+ return pipeline, nil
+}
+
+// createPipeline creates a new pipeline.
+func (s *PipelineService) createPipeline(ctx context.Context, tx *Tx, create *api.PipelineCreate) (*api.Pipeline, error) {
+ row, err := tx.QueryContext(ctx, `
+ INSERT INTO pipeline (
+ creator_id,
+ updater_id,
+ workspace_id,
+ name,
+ `+"`status`"+`
+ )
+ VALUES (?, ?, ?, 'OPEN')
+ RETURNING id, creator_id, created_ts, updater_id, updated_ts, workspace_id, name, `+"`status`"+`
+ `,
+ create.CreatorId,
+ create.CreatorId,
+ create.WorkspaceId,
+ create.Name,
+ )
+
+ if err != nil {
+ return nil, FormatError(err)
+ }
+ defer row.Close()
+
+ row.Next()
+ var pipeline api.Pipeline
+ if err := row.Scan(
+ &pipeline.ID,
+ &pipeline.CreatorId,
+ &pipeline.CreatedTs,
+ &pipeline.UpdaterId,
+ &pipeline.UpdatedTs,
+ &pipeline.WorkspaceId,
+ &pipeline.Name,
+ &pipeline.Status,
+ ); err != nil {
+ return nil, FormatError(err)
+ }
+
+ return &pipeline, nil
+}
+
+func (s *PipelineService) findPipelineList(ctx context.Context, tx *Tx, find *api.PipelineFind) (_ []*api.Pipeline, err error) {
+ // Build WHERE clause.
+ where, args := []string{"1 = 1"}, []interface{}{}
+ if v := find.ID; v != nil {
+ where, args = append(where, "id = ?"), append(args, *v)
+ }
+ if v := find.WorkspaceId; v != nil {
+ where, args = append(where, "workspace_id = ?"), append(args, *v)
+ }
+
+ rows, err := tx.QueryContext(ctx, `
+ SELECT
+ id,
+ creator_id,
+ created_ts,
+ updater_id,
+ updated_ts,
+ workspace_id,
+ name,
+ `+"`status`"+`
+ FROM pipeline
+ WHERE `+strings.Join(where, " AND "),
+ args...,
+ )
+ if err != nil {
+ return nil, FormatError(err)
+ }
+ defer rows.Close()
+
+ // Iterate over result set and deserialize rows into list.
+ list := make([]*api.Pipeline, 0)
+ for rows.Next() {
+ var pipeline api.Pipeline
+ if err := rows.Scan(
+ &pipeline.ID,
+ &pipeline.CreatorId,
+ &pipeline.CreatedTs,
+ &pipeline.UpdaterId,
+ &pipeline.UpdatedTs,
+ &pipeline.WorkspaceId,
+ &pipeline.Name,
+ &pipeline.Status,
+ ); err != nil {
+ return nil, FormatError(err)
+ }
+
+ list = append(list, &pipeline)
+ }
+ if err := rows.Err(); err != nil {
+ return nil, FormatError(err)
+ }
+
+ return list, nil
+}
+
+// patchPipeline updates a pipeline by ID. Returns the new state of the pipeline after update.
+func (s *PipelineService) patchPipeline(ctx context.Context, tx *Tx, patch *api.PipelinePatch) (*api.Pipeline, error) {
+ // Build UPDATE clause.
+ set, args := []string{"updater_id = ?"}, []interface{}{patch.UpdaterId}
+ if v := patch.Status; v != nil {
+ set, args = append(set, "status = ?"), append(args, api.PipelineStatus(*v))
+ }
+
+ args = append(args, patch.ID)
+
+ // Execute update query with RETURNING.
+ row, err := tx.QueryContext(ctx, `
+ UPDATE pipeline
+ SET `+strings.Join(set, ", ")+`
+ WHERE id = ?
+ RETURNING id, creator_id, created_ts, updater_id, updated_ts, workspace_id, name, `+"`status`"+`
+ `,
+ args...,
+ )
+ if err != nil {
+ return nil, FormatError(err)
+ }
+ defer row.Close()
+
+ if row.Next() {
+ var pipeline api.Pipeline
+ if err := row.Scan(
+ &pipeline.ID,
+ &pipeline.CreatorId,
+ &pipeline.CreatedTs,
+ &pipeline.UpdaterId,
+ &pipeline.UpdatedTs,
+ &pipeline.WorkspaceId,
+ &pipeline.Name,
+ &pipeline.Status,
+ ); err != nil {
+ return nil, FormatError(err)
+ }
+ return &pipeline, nil
+ }
+
+ return nil, &bytebase.Error{Code: bytebase.ENOTFOUND, Message: fmt.Sprintf("pipeline ID not found: %d", patch.ID)}
+}
diff --git a/backend/store/seed/0090_pipeline.sql b/backend/store/seed/0090_pipeline.sql
new file mode 100644
index 0000000000..0cf1311de1
--- /dev/null
+++ b/backend/store/seed/0090_pipeline.sql
@@ -0,0 +1,18 @@
+INSERT INTO
+ pipeline (
+ id,
+ creator_id,
+ updater_id,
+ workspace_id,
+ name,
+ `status`
+ )
+VALUES
+ (
+ 9001,
+ 1001,
+ 1001,
+ 1,
+ 'Hello world',
+ 'OPEN'
+ );
\ No newline at end of file
diff --git a/backend/store/seed/0100_stage.sql b/backend/store/seed/0100_stage.sql
new file mode 100644
index 0000000000..9b12676522
--- /dev/null
+++ b/backend/store/seed/0100_stage.sql
@@ -0,0 +1,23 @@
+-- Stage for Pipeline 9001 "Hello world"
+INSERT INTO
+ stage (
+ id,
+ creator_id,
+ updater_id,
+ workspace_id,
+ pipeline_id,
+ environment_id,
+ name,
+ `type`
+ )
+VALUES
+ (
+ 10001,
+ 1001,
+ 1001,
+ 1,
+ 9001,
+ 5004,
+ 'Prod',
+ 'bb.stage.schema.update'
+ );
\ No newline at end of file
diff --git a/backend/store/seed/0110_task.sql b/backend/store/seed/0110_task.sql
new file mode 100644
index 0000000000..19e6c32551
--- /dev/null
+++ b/backend/store/seed/0110_task.sql
@@ -0,0 +1,29 @@
+-- Task for Pipeline 9001 "Hello world", Stage 10001 "Prod" to update database 7008 "fakedb4"
+INSERT INTO
+ task (
+ id,
+ creator_id,
+ updater_id,
+ workspace_id,
+ pipeline_id,
+ stage_id,
+ database_id,
+ name,
+ `type`,
+ `status`,
+ `when`
+ )
+VALUES
+ (
+ 11001,
+ 1001,
+ 1001,
+ 1,
+ 9001,
+ 10001,
+ 7008,
+ 'Waiting approval',
+ 'bb.task.approve',
+ 'PENDING',
+ 'MANUAL'
+ );
\ No newline at end of file
diff --git a/backend/store/seed/0120_issue.sql b/backend/store/seed/0120_issue.sql
new file mode 100644
index 0000000000..e69de29bb2
diff --git a/backend/store/seed/0130_activity.sql b/backend/store/seed/0130_activity.sql
new file mode 100644
index 0000000000..e69de29bb2
diff --git a/backend/store/stage.go b/backend/store/stage.go
new file mode 100644
index 0000000000..f350234fec
--- /dev/null
+++ b/backend/store/stage.go
@@ -0,0 +1,193 @@
+package store
+
+import (
+ "context"
+ "fmt"
+ "strings"
+
+ "github.com/bytebase/bytebase"
+ "github.com/bytebase/bytebase/api"
+)
+
+var (
+ _ api.StageService = (*StageService)(nil)
+)
+
+// StageService represents a service for managing stage.
+type StageService struct {
+ l *bytebase.Logger
+ db *DB
+}
+
+// NewStageService returns a new instance of StageService.
+func NewStageService(logger *bytebase.Logger, db *DB) *StageService {
+ return &StageService{l: logger, db: db}
+}
+
+// CreateStage creates a new stage.
+func (s *StageService) CreateStage(ctx context.Context, create *api.StageCreate) (*api.Stage, error) {
+ tx, err := s.db.BeginTx(ctx, nil)
+ if err != nil {
+ return nil, FormatError(err)
+ }
+ defer tx.Rollback()
+
+ stage, err := s.createStage(ctx, tx, create)
+ if err != nil {
+ return nil, err
+ }
+
+ if err := tx.Commit(); err != nil {
+ return nil, FormatError(err)
+ }
+
+ return stage, nil
+}
+
+// FindStageList retrieves a list of stages based on find.
+func (s *StageService) FindStageList(ctx context.Context, find *api.StageFind) ([]*api.Stage, error) {
+ tx, err := s.db.BeginTx(ctx, nil)
+ if err != nil {
+ return nil, FormatError(err)
+ }
+ defer tx.Rollback()
+
+ list, err := s.findStageList(ctx, tx, find)
+ if err != nil {
+ return []*api.Stage{}, err
+ }
+
+ return list, nil
+}
+
+// FindStage retrieves a single stage based on find.
+// Returns ENOTFOUND if no matching record.
+// Returns the first matching one and prints a warning if finding more than 1 matching records.
+func (s *StageService) FindStage(ctx context.Context, find *api.StageFind) (*api.Stage, error) {
+ tx, err := s.db.BeginTx(ctx, nil)
+ if err != nil {
+ return nil, FormatError(err)
+ }
+ defer tx.Rollback()
+
+ list, err := s.findStageList(ctx, tx, find)
+ if err != nil {
+ return nil, err
+ } else if len(list) == 0 {
+ return nil, &bytebase.Error{Code: bytebase.ENOTFOUND, Message: fmt.Sprintf("stage not found: %v", find)}
+ } else if len(list) > 1 {
+ s.l.Warnf("found mulitple stages: %d, expect 1", len(list))
+ }
+ return list[0], nil
+}
+
+// createStage creates a new stage.
+func (s *StageService) createStage(ctx context.Context, tx *Tx, create *api.StageCreate) (*api.Stage, error) {
+ row, err := tx.QueryContext(ctx, `
+ INSERT INTO stage (
+ creator_id,
+ updater_id,
+ workspace_id,
+ pipeline_id,
+ environment_id,
+ name,
+ `+"`type`"+`
+ )
+ VALUES (?, ?, ?, ?, ?, ?, ?)
+ RETURNING id, creator_id, created_ts, updater_id, updated_ts, workspace_id, pipeline_id, environment_id, name, `+"`type`"+`
+ `,
+ create.CreatorId,
+ create.CreatorId,
+ create.WorkspaceId,
+ create.PipelineId,
+ create.EnvironmentId,
+ create.Name,
+ create.Type,
+ )
+
+ if err != nil {
+ return nil, FormatError(err)
+ }
+ defer row.Close()
+
+ row.Next()
+ var stage api.Stage
+ if err := row.Scan(
+ &stage.ID,
+ &stage.CreatorId,
+ &stage.CreatedTs,
+ &stage.UpdaterId,
+ &stage.UpdatedTs,
+ &stage.WorkspaceId,
+ &stage.PipelineId,
+ &stage.EnvironmentId,
+ &stage.Name,
+ &stage.Type,
+ ); err != nil {
+ return nil, FormatError(err)
+ }
+
+ return &stage, nil
+}
+
+func (s *StageService) findStageList(ctx context.Context, tx *Tx, find *api.StageFind) (_ []*api.Stage, err error) {
+ // Build WHERE clause.
+ where, args := []string{"1 = 1"}, []interface{}{}
+ if v := find.ID; v != nil {
+ where, args = append(where, "id = ?"), append(args, *v)
+ }
+ if v := find.WorkspaceId; v != nil {
+ where, args = append(where, "workspace_id = ?"), append(args, *v)
+ }
+ if v := find.PipelineId; v != nil {
+ where, args = append(where, "pipeline_id = ?"), append(args, *v)
+ }
+
+ rows, err := tx.QueryContext(ctx, `
+ SELECT
+ id,
+ creator_id,
+ created_ts,
+ updater_id,
+ updated_ts,
+ workspace_id,
+ pipeline_id,
+ environment_id,
+ name,
+ `+"`type`"+`
+ FROM stage
+ WHERE `+strings.Join(where, " AND "),
+ args...,
+ )
+ if err != nil {
+ return nil, FormatError(err)
+ }
+ defer rows.Close()
+
+ // Iterate over result set and deserialize rows into list.
+ list := make([]*api.Stage, 0)
+ for rows.Next() {
+ var stage api.Stage
+ if err := rows.Scan(
+ &stage.ID,
+ &stage.CreatorId,
+ &stage.CreatedTs,
+ &stage.UpdaterId,
+ &stage.UpdatedTs,
+ &stage.WorkspaceId,
+ &stage.PipelineId,
+ &stage.EnvironmentId,
+ &stage.Name,
+ &stage.Type,
+ ); err != nil {
+ return nil, FormatError(err)
+ }
+
+ list = append(list, &stage)
+ }
+ if err := rows.Err(); err != nil {
+ return nil, FormatError(err)
+ }
+
+ return list, nil
+}
diff --git a/backend/store/task.go b/backend/store/task.go
new file mode 100644
index 0000000000..e8daf23d7e
--- /dev/null
+++ b/backend/store/task.go
@@ -0,0 +1,286 @@
+package store
+
+import (
+ "context"
+ "fmt"
+ "strings"
+
+ "github.com/bytebase/bytebase"
+ "github.com/bytebase/bytebase/api"
+)
+
+var (
+ _ api.TaskService = (*TaskService)(nil)
+)
+
+// TaskService represents a service for managing task.
+type TaskService struct {
+ l *bytebase.Logger
+ db *DB
+}
+
+// NewTaskService returns a new instance of TaskService.
+func NewTaskService(logger *bytebase.Logger, db *DB) *TaskService {
+ return &TaskService{l: logger, db: db}
+}
+
+// CreateTask creates a new task.
+func (s *TaskService) CreateTask(ctx context.Context, create *api.TaskCreate) (*api.Task, error) {
+ tx, err := s.db.BeginTx(ctx, nil)
+ if err != nil {
+ return nil, FormatError(err)
+ }
+ defer tx.Rollback()
+
+ task, err := s.createTask(ctx, tx, create)
+ if err != nil {
+ return nil, err
+ }
+
+ if err := tx.Commit(); err != nil {
+ return nil, FormatError(err)
+ }
+
+ return task, nil
+}
+
+// FindTaskList retrieves a list of tasks based on find.
+func (s *TaskService) FindTaskList(ctx context.Context, find *api.TaskFind) ([]*api.Task, error) {
+ tx, err := s.db.BeginTx(ctx, nil)
+ if err != nil {
+ return nil, FormatError(err)
+ }
+ defer tx.Rollback()
+
+ list, err := s.findTaskList(ctx, tx, find)
+ if err != nil {
+ return []*api.Task{}, err
+ }
+
+ return list, nil
+}
+
+// FindTask retrieves a single task based on find.
+// Returns ENOTFOUND if no matching record.
+// Returns the first matching one and prints a warning if finding more than 1 matching records.
+func (s *TaskService) FindTask(ctx context.Context, find *api.TaskFind) (*api.Task, error) {
+ tx, err := s.db.BeginTx(ctx, nil)
+ if err != nil {
+ return nil, FormatError(err)
+ }
+ defer tx.Rollback()
+
+ list, err := s.findTaskList(ctx, tx, find)
+ if err != nil {
+ return nil, err
+ } else if len(list) == 0 {
+ return nil, &bytebase.Error{Code: bytebase.ENOTFOUND, Message: fmt.Sprintf("task not found: %v", find)}
+ } else if len(list) > 1 {
+ s.l.Warnf("found mulitple tasks: %d, expect 1", len(list))
+ }
+ return list[0], nil
+}
+
+// PatchTaskByID updates an existing task by ID.
+// Returns ENOTFOUND if task does not exist.
+func (s *TaskService) PatchTaskByID(ctx context.Context, patch *api.TaskPatch) (*api.Task, error) {
+ tx, err := s.db.BeginTx(ctx, nil)
+ if err != nil {
+ return nil, FormatError(err)
+ }
+ defer tx.Rollback()
+
+ task, err := s.patchTask(ctx, tx, patch)
+ if err != nil {
+ return nil, FormatError(err)
+ }
+
+ if err := tx.Commit(); err != nil {
+ return nil, FormatError(err)
+ }
+
+ return task, nil
+}
+
+// createTask creates a new task.
+func (s *TaskService) createTask(ctx context.Context, tx *Tx, create *api.TaskCreate) (*api.Task, error) {
+ row, err := tx.QueryContext(ctx, `
+ INSERT INTO task (
+ creator_id,
+ updater_id,
+ workspace_id,
+ pipeline_id,
+ stage_id,
+ database_id,
+ name,
+ `+"`status,`"+`
+ `+"`type,`"+`
+ `+"`when,`"+`
+ payload
+ )
+ VALUES (?, ?, ?, ?, ?, ?, ?, 'PENDING', ?, ?, ?)
+ RETURNING id, creator_id, created_ts, updater_id, updated_ts, workspace_id, pipeline_id, stage_id, database_id, name, `+"`status`, `type`, `when`, payload"+`
+ `,
+ create.CreatorId,
+ create.CreatorId,
+ create.WorkspaceId,
+ create.PipelineId,
+ create.StageId,
+ create.DatabaseId,
+ create.Name,
+ create.Type,
+ create.When,
+ create.Payload,
+ )
+
+ if err != nil {
+ return nil, FormatError(err)
+ }
+ defer row.Close()
+
+ row.Next()
+ var task api.Task
+ if err := row.Scan(
+ &task.ID,
+ &task.CreatorId,
+ &task.CreatedTs,
+ &task.UpdaterId,
+ &task.UpdatedTs,
+ &task.WorkspaceId,
+ &task.PipelineId,
+ &task.StageId,
+ &task.DatabaseId,
+ &task.Name,
+ &task.Status,
+ &task.Type,
+ &task.When,
+ &task.Payload,
+ ); err != nil {
+ return nil, FormatError(err)
+ }
+
+ return &task, nil
+}
+
+func (s *TaskService) findTaskList(ctx context.Context, tx *Tx, find *api.TaskFind) (_ []*api.Task, err error) {
+ // Build WHERE clause.
+ where, args := []string{"1 = 1"}, []interface{}{}
+ if v := find.ID; v != nil {
+ where, args = append(where, "id = ?"), append(args, *v)
+ }
+ if v := find.WorkspaceId; v != nil {
+ where, args = append(where, "workspace_id = ?"), append(args, *v)
+ }
+ if v := find.PipelineId; v != nil {
+ where, args = append(where, "pipeline_id = ?"), append(args, *v)
+ }
+ if v := find.StageId; v != nil {
+ where, args = append(where, "stage_id = ?"), append(args, *v)
+ }
+
+ rows, err := tx.QueryContext(ctx, `
+ SELECT
+ id,
+ creator_id,
+ created_ts,
+ updater_id,
+ updated_ts,
+ workspace_id,
+ pipeline_id,
+ stage_id,
+ database_id,
+ name,
+ `+"`status,`"+`
+ `+"`type,`"+`
+ `+"`when,`"+`
+ payload
+ FROM task
+ WHERE `+strings.Join(where, " AND "),
+ args...,
+ )
+ if err != nil {
+ return nil, FormatError(err)
+ }
+ defer rows.Close()
+
+ // Iterate over result set and deserialize rows into list.
+ list := make([]*api.Task, 0)
+ for rows.Next() {
+ var task api.Task
+ if err := rows.Scan(
+ &task.ID,
+ &task.CreatorId,
+ &task.CreatedTs,
+ &task.UpdaterId,
+ &task.UpdatedTs,
+ &task.WorkspaceId,
+ &task.PipelineId,
+ &task.StageId,
+ &task.DatabaseId,
+ &task.Name,
+ &task.Status,
+ &task.Type,
+ &task.When,
+ &task.Payload,
+ ); err != nil {
+ return nil, FormatError(err)
+ }
+
+ list = append(list, &task)
+ }
+ if err := rows.Err(); err != nil {
+ return nil, FormatError(err)
+ }
+
+ return list, nil
+}
+
+// patchTask updates a task by ID. Returns the new state of the task after update.
+func (s *TaskService) patchTask(ctx context.Context, tx *Tx, patch *api.TaskPatch) (*api.Task, error) {
+ // Build UPDATE clause.
+ set, args := []string{"updater_id = ?"}, []interface{}{patch.UpdaterId}
+ if v := patch.Status; v != nil {
+ set, args = append(set, "`status = ?"), append(args, api.TaskStatus(*v))
+ }
+
+ args = append(args, patch.ID)
+
+ // Execute update query with RETURNING.
+ row, err := tx.QueryContext(ctx, `
+ UPDATE task
+ SET `+strings.Join(set, ", ")+`
+ WHERE id = ?
+ RETURNING id, creator_id, created_ts, updater_id, updated_ts, workspace_id, pipeline_id, stage_id, database_id, name, `+"`status`, `type`, `when`, payload"+`
+ `,
+ args...,
+ )
+ if err != nil {
+ return nil, FormatError(err)
+ }
+ defer row.Close()
+
+ if row.Next() {
+ var task api.Task
+ if err := row.Scan(
+ &task.ID,
+ &task.CreatorId,
+ &task.CreatedTs,
+ &task.UpdaterId,
+ &task.UpdatedTs,
+ &task.WorkspaceId,
+ &task.PipelineId,
+ &task.StageId,
+ &task.DatabaseId,
+ &task.Name,
+ &task.Status,
+ &task.Type,
+ &task.When,
+ &task.Payload,
+ ); err != nil {
+ return nil, FormatError(err)
+ }
+ return &task, nil
+ }
+
+ return nil, &bytebase.Error{Code: bytebase.ENOTFOUND, Message: fmt.Sprintf("task ID not found: %d", patch.ID)}
+}
diff --git a/frontend/src/miragejs/seeds/index.ts b/frontend/src/miragejs/seeds/index.ts
index 8dc6579da8..d497742957 100644
--- a/frontend/src/miragejs/seeds/index.ts
+++ b/frontend/src/miragejs/seeds/index.ts
@@ -195,7 +195,7 @@ const workspacesSeeder = (server: any) => {
name: `Pipeline hello world`,
creatorId: ws1Dev1.id,
updaterId: ws1Dev1.id,
- status: "PENDING",
+ status: "OPEN",
workspace: workspace1,
});
@@ -218,7 +218,7 @@ const workspacesSeeder = (server: any) => {
creatorId: ws1Dev1.id,
updaterId: ws1Dev1.id,
name: environmentList1[3].name,
- type: "bb.stage.database.create",
+ type: "bb.stage.schema.update",
status: "PENDING",
environmentId: environmentList1[3].id,
pipeline,
diff --git a/frontend/src/types/issue.ts b/frontend/src/types/issue.ts
index c1f4907402..d05dfbac7e 100644
--- a/frontend/src/types/issue.ts
+++ b/frontend/src/types/issue.ts
@@ -74,10 +74,10 @@ export type IssuePatch = {
// Domain specific fields
name?: string;
description?: string;
+ assigneeId?: PrincipalId;
subscriberIdList?: PrincipalId[];
sql?: string;
rollbackSql?: string;
- assigneeId?: PrincipalId;
payload?: IssuePayload;
};
diff --git a/frontend/src/types/pipeline.ts b/frontend/src/types/pipeline.ts
index e7f4e8f053..05b3077c15 100644
--- a/frontend/src/types/pipeline.ts
+++ b/frontend/src/types/pipeline.ts
@@ -216,8 +216,8 @@ export type Task = {
// Domain specific fields
name: string;
- type: TaskType;
status: TaskStatus;
+ type: TaskType;
when: TaskWhenType;
// We may get an empty database for tasks like creating database.
database: Database;
@@ -232,14 +232,6 @@ export type TaskCreate = {
databaseId?: DatabaseId;
};
-export type TaskPatch = {
- // Standard fields
- updaterId: PrincipalId;
-
- // Domain specific fields
- status: TaskStatus;
-};
-
export type TaskStatusPatch = {
// Standard fields
updaterId: PrincipalId;