Skip to content

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

Generate the create database statement before creating the task so that the statement can be displayed on the UI

天舟0ab27005b72e
diff --git a/api/task.go b/api/task.go
index 0b54900b1b..0473d848ce 100644
--- a/api/task.go
+++ b/api/task.go
@@ -59,6 +59,7 @@ type TaskDatabaseCreatePayload struct {
// The project owning the database.
ProjectId int `json:"projectId,omitempty"`
DatabaseName string `json:"databaseName,omitempty"`
+ Statement string `json:"statement,omitempty"`
CharacterSet string `json:"character,omitempty"`
Collation string `json:"collation,omitempty"`
}
diff --git a/server/issue.go b/server/issue.go
index 4590bf7a6e..ed3a923f78 100644
--- a/server/issue.go
+++ b/server/issue.go
@@ -10,6 +10,7 @@ import (
"github.com/bytebase/bytebase/api"
"github.com/bytebase/bytebase/common"
+ "github.com/bytebase/bytebase/plugin/db"
"github.com/google/jsonapi"
"github.com/labstack/echo/v4"
)
@@ -383,6 +384,19 @@ func (s *Server) CreateIssue(ctx context.Context, issueCreate *api.IssueCreate,
payload.DatabaseName = taskCreate.DatabaseName
payload.CharacterSet = taskCreate.CharacterSet
payload.Collation = taskCreate.Collation
+ instanceFind := &api.InstanceFind{
+ ID: &taskCreate.InstanceId,
+ }
+ instance, err := s.InstanceService.FindInstance(ctx, instanceFind)
+ if err != nil {
+ return nil, fmt.Errorf("failed to create database creation task, failed to fetch instance %w", err)
+ }
+ switch instance.Engine {
+ case db.MySQL, db.TiDB:
+ payload.Statement = fmt.Sprintf("CREATE DATABASE `%s` CHARACTER SET %s COLLATE %s", taskCreate.DatabaseName, taskCreate.CharacterSet, taskCreate.Collation)
+ case db.Postgres:
+ payload.Statement = fmt.Sprintf("CREATE DATABASE \"%s\" ENCODING %q LC_COLLATE %q", taskCreate.DatabaseName, taskCreate.CharacterSet, taskCreate.Collation)
+ }
bytes, err := json.Marshal(payload)
if err != nil {
return nil, fmt.Errorf("failed to create database creation task, unable to marshal payload %w", err)
diff --git a/server/task_executor_database_create.go b/server/task_executor_database_create.go
index 90d9edb64d..b6e40a7079 100644
--- a/server/task_executor_database_create.go
+++ b/server/task_executor_database_create.go
@@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"strconv"
+ "strings"
"github.com/bytebase/bytebase/api"
"github.com/bytebase/bytebase/plugin/db"
@@ -39,6 +40,11 @@ func (exec *DatabaseCreateTaskExecutor) RunOnce(ctx context.Context, server *Ser
return true, nil, fmt.Errorf("invalid create database payload: %w", err)
}
+ statement := strings.TrimSpace(payload.Statement)
+ if statement == "" {
+ return true, nil, fmt.Errorf("empty create database statement")
+ }
+
if err := server.ComposeTaskRelationship(ctx, task); err != nil {
return true, nil, err
}
@@ -50,19 +56,10 @@ func (exec *DatabaseCreateTaskExecutor) RunOnce(ctx context.Context, server *Ser
}
defer driver.Close(ctx)
- var statement string
- switch instance.Engine {
- case db.MySQL:
- statement = fmt.Sprintf("CREATE DATABASE `%s` CHARACTER SET %s COLLATE %s", payload.DatabaseName, payload.CharacterSet, payload.Collation)
- case db.TiDB:
- statement = fmt.Sprintf("CREATE DATABASE `%s` CHARACTER SET %s COLLATE %s", payload.DatabaseName, payload.CharacterSet, payload.Collation)
- case db.Postgres:
- statement = fmt.Sprintf(`CREATE DATABASE "%s"`, payload.DatabaseName)
- }
exec.l.Debug("Start creating database...",
zap.String("instance", instance.Name),
zap.String("database", payload.DatabaseName),
- zap.String("sql", statement),
+ zap.String("statement", statement),
)
// Create a baseline migration history upon creating the database.
diff --git a/server/task_executor_schema_update.go b/server/task_executor_schema_update.go
index e4d2821117..4f48958f47 100644
--- a/server/task_executor_schema_update.go
+++ b/server/task_executor_schema_update.go
@@ -116,10 +116,10 @@ func (exec *SchemaUpdateTaskExecutor) RunOnce(ctx context.Context, server *Serve
mi.IssueId = strconv.Itoa(issue.ID)
}
- sql := strings.TrimSpace(payload.Statement)
+ statement := strings.TrimSpace(payload.Statement)
// Only baseline can have empty sql statement, which indicates empty database.
- if mi.Type != db.Baseline && sql == "" {
- return true, nil, fmt.Errorf("empty sql statement")
+ if mi.Type != db.Baseline && statement == "" {
+ return true, nil, fmt.Errorf("empty statement")
}
if err := server.ComposeTaskRelationship(ctx, task); err != nil {
@@ -137,7 +137,7 @@ func (exec *SchemaUpdateTaskExecutor) RunOnce(ctx context.Context, server *Serve
zap.String("database", databaseName),
zap.String("engine", mi.Engine.String()),
zap.String("type", mi.Type.String()),
- zap.String("sql", sql),
+ zap.String("statement", statement),
)
setup, err := driver.NeedsSetupMigration(ctx)
@@ -148,7 +148,7 @@ func (exec *SchemaUpdateTaskExecutor) RunOnce(ctx context.Context, server *Serve
return true, nil, common.Errorf(common.MigrationSchemaMissing, fmt.Errorf("missing migration schema for instance %q", task.Instance.Name))
}
- migrationId, schema, err := driver.ExecuteMigration(ctx, mi, sql)
+ migrationId, schema, err := driver.ExecuteMigration(ctx, mi, statement)
if err != nil {
return true, nil, err
}