Skip to content

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

refactor: move some logic in a new function named checkCharacterSetCollationOwner() (#2036)

h3n4lcec6cca1bf4a
diff --git a/server/issue.go b/server/issue.go
index dc9d5c22dc..77af7d4872 100644
--- a/server/issue.go
+++ b/server/issue.go
@@ -459,55 +459,13 @@ func (s *Server) getPipelineCreateForDatabaseCreate(ctx context.Context, issueCr
return nil, echo.NewHTTPError(http.StatusInternalServerError, err.Error()).SetInternal(err)
}
- switch instance.Engine {
- case db.ClickHouse:
- // ClickHouse does not support character set and collation at the database level.
- if c.CharacterSet != "" {
- return nil, echo.NewHTTPError(
- http.StatusBadRequest,
- fmt.Sprintf("Failed to create issue, ClickHouse does not support character set, got %s\n", c.CharacterSet),
- )
- }
- if c.Collation != "" {
- return nil, echo.NewHTTPError(
- http.StatusBadRequest,
- fmt.Sprintf("Failed to create issue, ClickHouse does not support collation, got %s\n", c.Collation),
- )
- }
- case db.Snowflake:
- if c.CharacterSet != "" {
- return nil, echo.NewHTTPError(
- http.StatusBadRequest,
- fmt.Sprintf("Failed to create issue, Snowflake does not support character set, got %s\n", c.CharacterSet),
- )
- }
- if c.Collation != "" {
- return nil, echo.NewHTTPError(
- http.StatusBadRequest,
- fmt.Sprintf("Failed to create issue, Snowflake does not support collation, got %s\n", c.Collation),
- )
- }
+ if err := checkCharacterSetCollationOwner(instance.Engine, c.CharacterSet, c.Collation, c.Owner); err != nil {
+ return nil, err
+ }
+
+ if instance.Engine == db.Snowflake {
// Snowflake needs to use upper case of DatabaseName.
c.DatabaseName = strings.ToUpper(c.DatabaseName)
- case db.Postgres:
- if c.Owner == "" {
- return nil, echo.NewHTTPError(
- http.StatusBadRequest,
- "Failed to create issue, database owner is required for postgres",
- )
- }
- case db.SQLite:
- // no-op.
- default:
- if c.CharacterSet == "" {
- return nil, echo.NewHTTPError(http.StatusBadRequest, "Failed to create issue, character set missing")
- }
- // For postgres, we don't explicitly specify a default since the default might be UNSET (denoted by "C").
- // If that's the case, setting an explicit default such as "en_US.UTF-8" might fail if the instance doesn't
- // install it.
- if instance.Engine != db.Postgres && c.Collation == "" {
- return nil, echo.NewHTTPError(http.StatusBadRequest, "Failed to create issue, collation missing")
- }
}
// Validate the labels. Labels are set upon task completion.
@@ -1035,6 +993,44 @@ func createGhostTaskList(database *api.Database, vcsPushEvent *vcs.PushEvent, de
return taskCreateList, taskIndexDAGList, nil
}
+// checkCharacterSetCollationOwner checks if the charactes set, collation and owner are legal according to the dbType.
+func checkCharacterSetCollationOwner(dbType db.Type, characterSet, collation, owner string) error {
+ switch dbType {
+ case db.ClickHouse:
+ // ClickHouse does not support character set and collation at the database level.
+ if characterSet != "" {
+ return fmt.Errorf("ClickHouse does not support character set, but got %s", characterSet)
+ }
+ if collation != "" {
+ return fmt.Errorf("ClickHouse does not support collation, but got %s", collation)
+ }
+ case db.Snowflake:
+ if characterSet != "" {
+ return fmt.Errorf("Snowflake does not support character set, but got %s", characterSet)
+ }
+ if collation != "" {
+ return fmt.Errorf("Snowflake does not support collation, but got %s", collation)
+ }
+ case db.Postgres:
+ if owner == "" {
+ return fmt.Errorf("database owner is required for PostgreSQL")
+ }
+ case db.SQLite:
+ // no-op.
+ default:
+ if characterSet == "" {
+ return fmt.Errorf("character set missing for %s", string(dbType))
+ }
+ // For postgres, we don't explicitly specify a default since the default might be UNSET (denoted by "C").
+ // If that's the case, setting an explicit default such as "en_US.UTF-8" might fail if the instance doesn't
+ // install it.
+ if collation == "" {
+ return fmt.Errorf("collation missing for %s", string(dbType))
+ }
+ }
+ return nil
+}
+
func getDatabaseNameAndStatement(dbType db.Type, createDatabaseContext api.CreateDatabaseContext, schema string) (string, string) {
databaseName := createDatabaseContext.DatabaseName
// Snowflake needs to use upper case of DatabaseName.
diff --git a/server/issue_test.go b/server/issue_test.go
index 62bb383e04..2cca105136 100644
--- a/server/issue_test.go
+++ b/server/issue_test.go
@@ -4,7 +4,9 @@ import (
"testing"
"github.com/bytebase/bytebase/api"
+ "github.com/bytebase/bytebase/plugin/db"
"github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
)
func TestGetPeerTenantDatabase(t *testing.T) {
@@ -67,3 +69,99 @@ func TestGetPeerTenantDatabase(t *testing.T) {
assert.Equal(t, got, test.want)
}
}
+
+func TestCheckCharacterSetCollationOwner(t *testing.T) {
+ tests := []struct {
+ dbType db.Type
+ characterSet string
+ collation string
+ owner string
+ expectError bool
+ }{
+ /* ClickHouse */
+ // With character set or collation
+ {
+ dbType: db.ClickHouse,
+ characterSet: "utf8mb4",
+ expectError: true,
+ },
+ {
+ dbType: db.ClickHouse,
+ collation: "utf8mb4_0900_ai_ci",
+ expectError: true,
+ },
+ // Normal
+ {
+ dbType: db.ClickHouse,
+ expectError: false,
+ },
+
+ /* Snowflake */
+ // With character set or collation
+ {
+ dbType: db.Snowflake,
+ characterSet: "utf8mb4",
+ expectError: true,
+ },
+ {
+ dbType: db.Snowflake,
+ collation: "utf8mb4_0900_ai_ci",
+ expectError: true,
+ },
+ // Normal
+ {
+ dbType: db.Snowflake,
+ expectError: false,
+ },
+
+ /* PostgreSQL */
+ // Without owner
+ {
+ dbType: db.Postgres,
+ owner: "",
+ expectError: true,
+ },
+ // Without character set
+ {
+ dbType: db.Postgres,
+ owner: "bytebase",
+ collation: "fr_FR",
+ expectError: false,
+ },
+ // Without collation
+ {
+ dbType: db.Postgres,
+ owner: "bytebase",
+ characterSet: "UTF8",
+ expectError: false,
+ },
+
+ /* MySQL */
+ // With character set or collation
+ {
+ dbType: db.MySQL,
+ characterSet: "utf8mb4",
+ expectError: true,
+ },
+ {
+ dbType: db.MySQL,
+ collation: "utf8mb4_0900_ai_ci",
+ expectError: true,
+ },
+ // Normal
+ {
+ dbType: db.MySQL,
+ characterSet: "utf8mb4",
+ collation: "utf8mb4_0900_ai_ci",
+ expectError: false,
+ },
+ }
+ for _, test := range tests {
+ err := checkCharacterSetCollationOwner(test.dbType, test.characterSet, test.collation, test.owner)
+ if test.expectError {
+ require.Error(t, err)
+ } else {
+ require.NoError(t, err)
+ }
+ }
+}