Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
Return detail string on task run success so that it can be included in the task run interface
天舟1866c89a8145
diff --git a/server/task_executor.go b/server/task_executor.goindex 8f3efe6dd5..76a4d78366 100644--- a/server/task_executor.go+++ b/server/task_executor.go@@ -10,5 +10,5 @@ type TaskExecutor interface {// RunOnce will be called periodically by the scheduler until terminated is true.// Note, it's possible that err could be non-nil while terminated is false, which// usually indicates a transient error and will make scheduler retry later.- RunOnce(ctx context.Context, server *Server, task *api.Task) (terminated bool, err error)+ RunOnce(ctx context.Context, server *Server, task *api.Task) (terminated bool, detail string, err error)}diff --git a/server/task_executor_database_create.go b/server/task_executor_database_create.goindex 701664bff8..5a2baa53ad 100644--- a/server/task_executor_database_create.go+++ b/server/task_executor_database_create.go@@ -20,7 +20,7 @@ type DatabaseCreateTaskExecutor struct {l *zap.Logger}-func (exec *DatabaseCreateTaskExecutor) RunOnce(ctx context.Context, server *Server, task *api.Task) (terminated bool, err error) {+func (exec *DatabaseCreateTaskExecutor) RunOnce(ctx context.Context, server *Server, task *api.Task) (terminated bool, detail string, err error) {defer func() {if r := recover(); r != nil {panicErr, ok := r.(error)@@ -35,11 +35,11 @@ func (exec *DatabaseCreateTaskExecutor) RunOnce(ctx context.Context, server *Serpayload := &api.TaskDatabaseCreatePayload{}if err := json.Unmarshal([]byte(task.Payload), payload); err != nil {- return true, fmt.Errorf("invalid create database payload: %w", err)+ return true, "", fmt.Errorf("invalid create database payload: %w", err)}if err := server.ComposeTaskRelationship(ctx, task); err != nil {- return true, err+ return true, "", err}instance := task.Instance@@ -50,7 +50,7 @@ func (exec *DatabaseCreateTaskExecutor) RunOnce(ctx context.Context, server *SerPort: instance.Port,})if err != nil {- return true, fmt.Errorf("failed to connect instance: %v with user: %v. %w", instance.Name, instance.Username, err)+ return true, "", fmt.Errorf("failed to connect instance: %v with user: %v. %w", instance.Name, instance.Username, err)}exec.l.Debug("Start creating database...",@@ -60,8 +60,8 @@ func (exec *DatabaseCreateTaskExecutor) RunOnce(ctx context.Context, server *Ser)if err := driver.Execute(ctx, payload.Statement); err != nil {- return true, err+ return true, "", err}- return true, nil+ return true, fmt.Sprintf("Created database '%s'", payload.DatabaseName), nil}diff --git a/server/task_executor_default.go b/server/task_executor_default.goindex 28c0e2207f..e0314c8e7b 100644--- a/server/task_executor_default.go+++ b/server/task_executor_default.go@@ -2,6 +2,7 @@ package serverimport ("context"+ "fmt""github.com/bytebase/bytebase/api""go.uber.org/zap"@@ -17,8 +18,8 @@ type DefaultTaskExecutor struct {l *zap.Logger}-func (exec *DefaultTaskExecutor) RunOnce(ctx context.Context, server *Server, task *api.Task) (terminated bool, err error) {+func (exec *DefaultTaskExecutor) RunOnce(ctx context.Context, server *Server, task *api.Task) (terminated bool, detail string, err error) {exec.l.Info("Run default task type", zap.String("task", task.Name))- return true, nil+ return true, fmt.Sprintf("No-op task %s", task.Name), nil}diff --git a/server/task_executor_schema_update.go b/server/task_executor_schema_update.goindex 747438e37f..4cce348d29 100644--- a/server/task_executor_schema_update.go+++ b/server/task_executor_schema_update.go@@ -21,7 +21,7 @@ type SchemaUpdateTaskExecutor struct {l *zap.Logger}-func (exec *SchemaUpdateTaskExecutor) RunOnce(ctx context.Context, server *Server, task *api.Task) (terminated bool, err error) {+func (exec *SchemaUpdateTaskExecutor) RunOnce(ctx context.Context, server *Server, task *api.Task) (terminated bool, detail string, err error) {defer func() {if r := recover(); r != nil {panicErr, ok := r.(error)@@ -36,7 +36,7 @@ func (exec *SchemaUpdateTaskExecutor) RunOnce(ctx context.Context, server *Servepayload := &api.TaskDatabaseSchemaUpdatePayload{}if err := json.Unmarshal([]byte(task.Payload), payload); err != nil {- return true, fmt.Errorf("invalid database schema update payload: %w", err)+ return true, "", fmt.Errorf("invalid database schema update payload: %w", err)}mi := &db.MigrationInfo{@@ -46,7 +46,7 @@ func (exec *SchemaUpdateTaskExecutor) RunOnce(ctx context.Context, server *Servemi, err = db.ParseMigrationInfo(payload.VCSPushEvent.FileCommit.Added, payload.VCSPushEvent.BaseDirectory)// This should not happen normally as we already check this when creating the issue. Just in case.if err != nil {- return true, fmt.Errorf("failed to start schema migration, error: %w", err)+ return true, "", fmt.Errorf("failed to start schema migration, error: %w", err)}mi.Creator = payload.VCSPushEvent.FileCommit.AuthorName}@@ -54,11 +54,11 @@ func (exec *SchemaUpdateTaskExecutor) RunOnce(ctx context.Context, server *Servesql := strings.TrimSpace(payload.Statement)// Only baseline can have empty sql statement, which indicates empty database.if mi.Type != db.Baseline && sql == "" {- return true, fmt.Errorf("empty sql statement")+ return true, "", fmt.Errorf("empty sql statement")}if err := server.ComposeTaskRelationship(ctx, task); err != nil {- return true, err+ return true, "", err}instance := task.Instance@@ -74,7 +74,7 @@ func (exec *SchemaUpdateTaskExecutor) RunOnce(ctx context.Context, server *ServeDatabase: databaseName,})if err != nil {- return true, fmt.Errorf("failed to connect instance: %v with user: %v. %w", instance.Name, instance.Username, err)+ return true, "", fmt.Errorf("failed to connect instance: %v with user: %v. %w", instance.Name, instance.Username, err)}if payload.VCSPushEvent == nil {@@ -85,7 +85,7 @@ func (exec *SchemaUpdateTaskExecutor) RunOnce(ctx context.Context, server *Serve)if err := driver.Execute(ctx, sql); err != nil {- return true, err+ return true, "", err}} else {exec.l.Debug("Start sql migration...",@@ -97,16 +97,21 @@ func (exec *SchemaUpdateTaskExecutor) RunOnce(ctx context.Context, server *Servesetup, err := driver.NeedsSetupMigration(ctx)if err != nil {- return true, fmt.Errorf("failed to check migration setup for instance: %v, %w", instance.Name, err)+ return true, "", fmt.Errorf("failed to check migration setup for instance: %v, %w", instance.Name, err)}if setup {- return true, fmt.Errorf("missing migration schema for instance: %v", instance.Name)+ return true, "", fmt.Errorf("missing migration schema for instance: %v", instance.Name)}if err := driver.ExecuteMigration(ctx, mi, sql); err != nil {- return true, err+ return true, "", err}}- return true, nil+ detail = fmt.Sprintf("Applied migration version %s to database '%s'", mi.Version, databaseName)+ if mi.Type == db.Baseline {+ detail = fmt.Sprintf("Established baseline version %s for database '%s'", mi.Version, databaseName)+ }++ return true, detail, nil}diff --git a/server/task_scheduler.go b/server/task_scheduler.goindex 2152b69e4f..da94f927b0 100644--- a/server/task_scheduler.go+++ b/server/task_scheduler.go@@ -109,7 +109,7 @@ func (s *TaskScheduler) Run() error {continue}- done, err := executor.RunOnce(context.Background(), s.server, task)+ done, detail, err := executor.RunOnce(context.Background(), s.server, task)if done {if err != nil {taskStatusPatch := &api.TaskStatusPatch{@@ -122,7 +122,13 @@ func (s *TaskScheduler) Run() error {continue}- _, err = s.server.ChangeTaskStatus(context.Background(), task, api.TaskDone, api.SYSTEM_BOT_ID)+ taskStatusPatch := &api.TaskStatusPatch{+ ID: task.ID,+ UpdaterId: api.SYSTEM_BOT_ID,+ Status: api.TaskDone,+ Comment: detail,+ }+ _, err = s.server.ChangeTaskStatusWithPatch(context.Background(), task, taskStatusPatch)if err != nil {s.l.Error("Failed to mark task as DONE",zap.Int("id", task.ID),diff --git a/store/seed/test/10120__task_run.sql b/store/seed/test/10120__task_run.sqlindex 09ae757f29..4d45a2941a 100644--- a/store/seed/test/10120__task_run.sql+++ b/store/seed/test/10120__task_run.sql@@ -22,7 +22,7 @@ VALUES'Add initial schema 1624873710','DONE','bb.task.database.schema.update',- '',+ 'Established baseline version %s for database ''shop''','{"statement":"CREATE TABLE product (\n\tid INTEGER PRIMARY KEY AUTO_INCREMENT,\n\tname TEXT NOT NULL,\n\tdescription TEXT NOT NULL\n);\n","pushEvent":{"vcsType":"GITLAB_SELF_HOST","baseDir":"bytebase","ref":"refs/heads/master","repoId":"14","repoUrl":"http://gitlab.bytebase.com/bytebase-demo/shop","repoFullPath":"bytebase-demo/shop","authorName":"tianzhou","fileCommit":{"id":"da90a2510eccd051ad14e4b89ca904d733169a39","title":"Create product table","message":"Create product table","createdTs":1624873354,"url":"http://gitlab.bytebase.com/bytebase-demo/shop/-/commit/da90a2510eccd051ad14e4b89ca904d733169a39","authorName":"tianzhou","added":"bytebase/v1__shop__baseline__add_initial_schema.sql"}}}');@@ -50,7 +50,7 @@ VALUES'Add initial schema 1624883710','DONE','bb.task.database.schema.update',- '',+ 'Established baseline version %s for database ''shop''','{"statement":"CREATE TABLE product (\n\tid INTEGER PRIMARY KEY AUTO_INCREMENT,\n\tname TEXT NOT NULL,\n\tdescription TEXT NOT NULL\n);\n","pushEvent":{"vcsType":"GITLAB_SELF_HOST","baseDir":"bytebase","ref":"refs/heads/master","repoId":"14","repoUrl":"http://gitlab.bytebase.com/bytebase-demo/shop","repoFullPath":"bytebase-demo/shop","authorName":"tianzhou","fileCommit":{"id":"da90a2510eccd051ad14e4b89ca904d733169a39","title":"Create product table","message":"Create product table","createdTs":1624873354,"url":"http://gitlab.bytebase.com/bytebase-demo/shop/-/commit/da90a2510eccd051ad14e4b89ca904d733169a39","authorName":"tianzhou","added":"bytebase/v1__shop__baseline__add_initial_schema.sql"}}}');@@ -102,7 +102,7 @@ VALUES'Add initial schema 1624865387','DONE','bb.task.database.schema.update',- '',+ 'Established baseline version 202106280000 for database ''blog''','{"statement":"CREATE TABLE `user` (\n\tid INTEGER PRIMARY KEY AUTO_INCREMENT,\n\tname TEXT NOT NULL,\n\temail TEXT NOT NULL\n);\n\nCREATE TABLE post (\n\tid INTEGER PRIMARY KEY AUTO_INCREMENT,\n\tname TEXT NOT NULL,\n\tcontent TEXT NOT NULL,\n\tauthor_id INTEGER NOT NULL,\n\tFOREIGN KEY (author_id) REFERENCES `user` (id) ON DELETE RESTRICT\n);\n\nCREATE TABLE comment (\n\tid INTEGER PRIMARY KEY AUTO_INCREMENT,\n\tcontent TEXT NOT NULL,\n\tpost_id INTEGER NOT NULL,\n\tauthor_id INTEGER NOT NULL,\n\tFOREIGN KEY (post_id) REFERENCES post (id) ON DELETE CASCADE,\n\tFOREIGN KEY (author_id) REFERENCES `user` (id) ON DELETE RESTRICT\n);\n","pushEvent":{"vcsType":"GITLAB_SELF_HOST","baseDir":"bytebase","ref":"refs/heads/master","repoId":"13","repoUrl":"http://gitlab.bytebase.com/bytebase-demo/blog","repoFullPath":"bytebase-demo/blog","authorName":"tianzhou","fileCommit":{"id":"d7f3b88b93c4d7f57b710980cdf92f72dcc4cd1e","title":"Create user, post, comment table for dev environment","message":"Create user, post, comment table for dev environment","createdTs":1624865383,"url":"http://gitlab.bytebase.com/bytebase-demo/blog/-/commit/d7f3b88b93c4d7f57b710980cdf92f72dcc4cd1e","authorName":"tianzhou","added":"bytebase/dev/202106280000__blog__baseline__add_initial_schema.sql"}}}');@@ -130,7 +130,7 @@ VALUES'Add initial schema 1624866790','DONE','bb.task.database.schema.update',- '',+ 'Established baseline version 202106280000 for database ''blog''','{"statement":"CREATE TABLE `user` (\n\tid INTEGER PRIMARY KEY AUTO_INCREMENT,\n\tname TEXT NOT NULL,\n\temail TEXT NOT NULL\n);\n\nCREATE TABLE post (\n\tid INTEGER PRIMARY KEY AUTO_INCREMENT,\n\tname TEXT NOT NULL,\n\tcontent TEXT NOT NULL,\n\tauthor_id INTEGER NOT NULL,\n\tFOREIGN KEY (author_id) REFERENCES `user` (id) ON DELETE RESTRICT\n);\n\nCREATE TABLE comment (\n\tid INTEGER PRIMARY KEY AUTO_INCREMENT,\n\tcontent TEXT NOT NULL,\n\tpost_id INTEGER NOT NULL,\n\tauthor_id INTEGER NOT NULL,\n\tFOREIGN KEY (post_id) REFERENCES post (id) ON DELETE CASCADE,\n\tFOREIGN KEY (author_id) REFERENCES `user` (id) ON DELETE RESTRICT\n);\n","pushEvent":{"vcsType":"GITLAB_SELF_HOST","baseDir":"bytebase","ref":"refs/heads/master","repoId":"13","repoUrl":"http://gitlab.bytebase.com/bytebase-demo/blog","repoFullPath":"bytebase-demo/blog","authorName":"tianzhou","fileCommit":{"id":"d7f3b88b93c4d7f57b710980cdf92f72dcc4cd1e","title":"Create user, post, comment table for dev environment","message":"Create user, post, comment table for dev environment","createdTs":1624865383,"url":"http://gitlab.bytebase.com/bytebase-demo/blog/-/commit/d7f3b88b93c4d7f57b710980cdf92f72dcc4cd1e","authorName":"tianzhou","added":"bytebase/dev/202106280000__blog__baseline__add_initial_schema.sql"}}}');@@ -158,7 +158,7 @@ VALUES'Add initial schema 1624868407','DONE','bb.task.database.schema.update',- '',+ 'Established baseline version 202106280000 for database ''blog''','{"statement":"CREATE TABLE `user` (\n\tid INTEGER PRIMARY KEY AUTO_INCREMENT,\n\tname TEXT NOT NULL,\n\temail TEXT NOT NULL\n);\n\nCREATE TABLE post (\n\tid INTEGER PRIMARY KEY AUTO_INCREMENT,\n\tname TEXT NOT NULL,\n\tcontent TEXT NOT NULL,\n\tauthor_id INTEGER NOT NULL,\n\tFOREIGN KEY (author_id) REFERENCES `user` (id) ON DELETE RESTRICT\n);\n\nCREATE TABLE comment (\n\tid INTEGER PRIMARY KEY AUTO_INCREMENT,\n\tcontent TEXT NOT NULL,\n\tpost_id INTEGER NOT NULL,\n\tauthor_id INTEGER NOT NULL,\n\tFOREIGN KEY (post_id) REFERENCES post (id) ON DELETE CASCADE,\n\tFOREIGN KEY (author_id) REFERENCES `user` (id) ON DELETE RESTRICT\n);\n","pushEvent":{"vcsType":"GITLAB_SELF_HOST","baseDir":"bytebase","ref":"refs/heads/master","repoId":"13","repoUrl":"http://gitlab.bytebase.com/bytebase-demo/blog","repoFullPath":"bytebase-demo/blog","authorName":"tianzhou","fileCommit":{"id":"d7f3b88b93c4d7f57b710980cdf92f72dcc4cd1e","title":"Create user, post, comment table for dev environment","message":"Create user, post, comment table for dev environment","createdTs":1624865383,"url":"http://gitlab.bytebase.com/bytebase-demo/blog/-/commit/d7f3b88b93c4d7f57b710980cdf92f72dcc4cd1e","authorName":"tianzhou","added":"bytebase/dev/202106280000__blog__baseline__add_initial_schema.sql"}}}');@@ -186,7 +186,7 @@ VALUES'Add initial schema 1624868680','DONE','bb.task.database.schema.update',- '',+ 'Established baseline version 202106280000 for database ''blog''','{"statement":"CREATE TABLE `user` (\n\tid INTEGER PRIMARY KEY AUTO_INCREMENT,\n\tname TEXT NOT NULL,\n\temail TEXT NOT NULL\n);\n\nCREATE TABLE post (\n\tid INTEGER PRIMARY KEY AUTO_INCREMENT,\n\tname TEXT NOT NULL,\n\tcontent TEXT NOT NULL,\n\tauthor_id INTEGER NOT NULL,\n\tFOREIGN KEY (author_id) REFERENCES `user` (id) ON DELETE RESTRICT\n);\n\nCREATE TABLE comment (\n\tid INTEGER PRIMARY KEY AUTO_INCREMENT,\n\tcontent TEXT NOT NULL,\n\tpost_id INTEGER NOT NULL,\n\tauthor_id INTEGER NOT NULL,\n\tFOREIGN KEY (post_id) REFERENCES post (id) ON DELETE CASCADE,\n\tFOREIGN KEY (author_id) REFERENCES `user` (id) ON DELETE RESTRICT\n);\n","pushEvent":{"vcsType":"GITLAB_SELF_HOST","baseDir":"bytebase","ref":"refs/heads/master","repoId":"13","repoUrl":"http://gitlab.bytebase.com/bytebase-demo/blog","repoFullPath":"bytebase-demo/blog","authorName":"tianzhou","fileCommit":{"id":"d7f3b88b93c4d7f57b710980cdf92f72dcc4cd1e","title":"Create user, post, comment table for dev environment","message":"Create user, post, comment table for dev environment","createdTs":1624865383,"url":"http://gitlab.bytebase.com/bytebase-demo/blog/-/commit/d7f3b88b93c4d7f57b710980cdf92f72dcc4cd1e","authorName":"tianzhou","added":"bytebase/dev/202106280000__blog__baseline__add_initial_schema.sql"}}}');@@ -214,6 +214,6 @@ VALUES'Add created at column 1624869944','FAILED','bb.task.database.schema.update',- 'failed to connect instance: On-premises Dev MySQL with user: admin. dial tcp: lookup mysql.dev.example.com: no such host',+ 'database ''blog'' has already applied version 202106280100','{"statement":"ALTER TABLE `user` ADD COLUMN `created_at` DATETIME NOT NULL;\n\nALTER TABLE post ADD COLUMN `created_at` DATETIME NOT NULL;\n\nALTER TABLE comment ADD COLUMN `created_at` DATETIME NOT NULL;\n","pushEvent":{"vcsType":"GITLAB_SELF_HOST","baseDir":"bytebase","ref":"refs/heads/master","repoId":"13","repoUrl":"http://gitlab.bytebase.com/bytebase-demo/blog","repoFullPath":"bytebase-demo/blog","authorName":"tianzhou","fileCommit":{"id":"171ceaf7659ceb8e495aa3ef356ec686656f9dc0","title":"Add created_at column to user,post,comment table for dev environment","message":"Add created_at column to user,post,comment table for dev environment","createdTs":1624869938,"url":"http://gitlab.bytebase.com/bytebase-demo/blog/-/commit/171ceaf7659ceb8e495aa3ef356ec686656f9dc0","authorName":"tianzhou","added":"bytebase/dev/202106280100__blog__add_created_at_column.sql"}}}');\ No newline at end of filediff --git a/store/seed/test/10140__activity.sql b/store/seed/test/10140__activity.sqlindex 895485c9f5..3c17c88e0d 100644--- a/store/seed/test/10140__activity.sql+++ b/store/seed/test/10140__activity.sql@@ -129,7 +129,7 @@ VALUES1624873710,13002,'bb.pipeline.task.status.update',- '',+ 'Established baseline version %s for database ''shop''','{"taskId":11002,"oldStatus":"RUNNING","newStatus":"DONE"}');@@ -175,7 +175,7 @@ VALUES1624873710,13002,'bb.pipeline.task.status.update',- '',+ 'Established baseline version %s for database ''shop''','{"taskId":11003,"oldStatus":"RUNNING","newStatus":"DONE"}');@@ -281,7 +281,7 @@ VALUES1624865388,13004,'bb.pipeline.task.status.update',- '',+ 'Established baseline version 202106280000 for database ''blog''','{"taskId":11008,"oldStatus":"RUNNING","newStatus":"DONE"}');@@ -374,7 +374,7 @@ VALUES1624866791,13005,'bb.pipeline.task.status.update',- '',+ 'Established baseline version 202106280000 for database ''blog''','{"taskId":11009,"oldStatus":"RUNNING","newStatus":"DONE"}');@@ -467,7 +467,7 @@ VALUES1624868408,13006,'bb.pipeline.task.status.update',- '',+ 'Established baseline version 202106280000 for database ''blog''','{"taskId":11010,"oldStatus":"RUNNING","newStatus":"DONE"}');@@ -560,7 +560,7 @@ VALUES1624868681,13007,'bb.pipeline.task.status.update',- '',+ 'Established baseline version 202106280000 for database ''blog''','{"taskId":11011,"oldStatus":"RUNNING","newStatus":"DONE"}');@@ -653,6 +653,6 @@ VALUES1624869945,13008,'bb.pipeline.task.status.update',- 'failed to connect instance: On-premises Dev MySQL with user: admin. dial tcp: lookup mysql.dev.example.com: no such host',+ 'database ''blog'' has already applied version 202106280100','{"taskId":11012,"oldStatus":"RUNNING","newStatus":"FAILED"}');\ No newline at end of filediff --git a/store/task.go b/store/task.goindex 31d36f8739..85991043ad 100644--- a/store/task.go+++ b/store/task.go@@ -333,13 +333,13 @@ func (s *TaskService) patchTaskStatus(ctx context.Context, tx *Tx, patch *api.TataskRunStatusPatch := &api.TaskRunStatusPatch{ID: &taskRun.ID,TaskId: &patch.ID,+ Detail: patch.Comment,}switch patch.Status {case api.TaskDone:taskRunStatusPatch.Status = api.TaskRunDonecase api.TaskFailed:taskRunStatusPatch.Status = api.TaskRunFailed- taskRunStatusPatch.Detail = patch.Commentcase api.TaskPending:case api.TaskPendingApproval:case api.TaskCanceled: