mirror of
https://github.com/Syngnat/GoNavi.git
synced 2026-06-02 20:49:48 +08:00
✨ feat(postgres): 新增数据库节点新建模式功能
- 后端新增 CreateSchema 接口,支持在选中 PostgreSQL 数据库下创建 schema - 侧边栏旧版菜单和新版菜单均增加新建模式入口 - 创建成功后刷新对象树,并支持空模式显示 - 补充 Wails 绑定与创建模式相关测试 Refs #480
This commit is contained in:
@@ -149,6 +149,56 @@ func (a *App) CreateDatabase(config connection.ConnectionConfig, dbName string)
|
||||
return connection.QueryResult{Success: true, Message: "数据库创建成功"}
|
||||
}
|
||||
|
||||
func isPostgresSchemaDDLDBType(dbType string) bool {
|
||||
switch resolveDDLDBType(connection.ConnectionConfig{Type: dbType}) {
|
||||
case "postgres", "kingbase", "highgo", "vastbase", "opengauss":
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func buildCreateSchemaSQL(dbType string, schemaName string) (string, error) {
|
||||
schemaName = strings.TrimSpace(schemaName)
|
||||
if schemaName == "" {
|
||||
return "", fmt.Errorf("模式名称不能为空")
|
||||
}
|
||||
|
||||
if !isPostgresSchemaDDLDBType(dbType) {
|
||||
return "", fmt.Errorf("当前数据源(%s)暂不支持通过此入口新建模式", dbType)
|
||||
}
|
||||
|
||||
return fmt.Sprintf("CREATE SCHEMA %s", quoteIdentByType(dbType, schemaName)), nil
|
||||
}
|
||||
|
||||
func (a *App) CreateSchema(config connection.ConnectionConfig, dbName string, schemaName string) connection.QueryResult {
|
||||
dbType := resolveDDLDBType(config)
|
||||
targetDbName := strings.TrimSpace(dbName)
|
||||
if targetDbName == "" {
|
||||
targetDbName = strings.TrimSpace(config.Database)
|
||||
}
|
||||
if targetDbName == "" {
|
||||
return connection.QueryResult{Success: false, Message: "目标数据库不能为空"}
|
||||
}
|
||||
|
||||
query, err := buildCreateSchemaSQL(dbType, schemaName)
|
||||
if err != nil {
|
||||
return connection.QueryResult{Success: false, Message: err.Error()}
|
||||
}
|
||||
|
||||
runConfig := buildRunConfigForDDL(config, dbType, targetDbName)
|
||||
dbInst, err := a.getDatabase(runConfig)
|
||||
if err != nil {
|
||||
return connection.QueryResult{Success: false, Message: err.Error()}
|
||||
}
|
||||
|
||||
if _, err := dbInst.Exec(query); err != nil {
|
||||
return connection.QueryResult{Success: false, Message: err.Error()}
|
||||
}
|
||||
|
||||
return connection.QueryResult{Success: true, Message: "模式创建成功"}
|
||||
}
|
||||
|
||||
func resolveDDLDBType(config connection.ConnectionConfig) string {
|
||||
dbType := strings.ToLower(strings.TrimSpace(config.Type))
|
||||
if dbType == "doris" {
|
||||
|
||||
@@ -115,3 +115,58 @@ func TestCreateDatabase_SQLServerUsesBracketIdentifiers(t *testing.T) {
|
||||
t.Fatalf("unexpected SQL Server create database SQL, want %q got %q", want, fakeDB.execQueries[0])
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildCreateSchemaSQL_PostgresQuotesSchemaName(t *testing.T) {
|
||||
got, err := buildCreateSchemaSQL("postgresql", `sales"Ops`)
|
||||
if err != nil {
|
||||
t.Fatalf("expected postgres create schema SQL, got error: %v", err)
|
||||
}
|
||||
const want = `CREATE SCHEMA "sales""Ops"`
|
||||
if got != want {
|
||||
t.Fatalf("unexpected create schema SQL, want %q got %q", want, got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildCreateSchemaSQL_RejectsUnsupportedDatabaseType(t *testing.T) {
|
||||
if _, err := buildCreateSchemaSQL("mysql", "sales"); err == nil {
|
||||
t.Fatalf("expected unsupported database type error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateSchema_CustomPostgresUsesSelectedDatabase(t *testing.T) {
|
||||
originalNewDatabaseFunc := newDatabaseFunc
|
||||
originalResolveDialConfigWithProxyFunc := resolveDialConfigWithProxyFunc
|
||||
t.Cleanup(func() {
|
||||
newDatabaseFunc = originalNewDatabaseFunc
|
||||
resolveDialConfigWithProxyFunc = originalResolveDialConfigWithProxyFunc
|
||||
})
|
||||
|
||||
fakeDB := &fakeCreateDatabaseDB{}
|
||||
newDatabaseFunc = func(dbType string) (db.Database, error) {
|
||||
return fakeDB, nil
|
||||
}
|
||||
resolveDialConfigWithProxyFunc = func(raw connection.ConnectionConfig) (connection.ConnectionConfig, error) {
|
||||
return raw, nil
|
||||
}
|
||||
|
||||
app := NewAppWithSecretStore(secretstore.NewUnavailableStore("test"))
|
||||
result := app.CreateSchema(connection.ConnectionConfig{
|
||||
Type: "custom",
|
||||
Driver: "pgx",
|
||||
Database: "postgres",
|
||||
}, "tenant_db", `tenant"schema`)
|
||||
|
||||
if !result.Success {
|
||||
t.Fatalf("expected create schema success, got failure: %s", result.Message)
|
||||
}
|
||||
if fakeDB.connectConfig.Database != "tenant_db" {
|
||||
t.Fatalf("expected create schema connection to use selected database tenant_db, got %q", fakeDB.connectConfig.Database)
|
||||
}
|
||||
if len(fakeDB.execQueries) != 1 {
|
||||
t.Fatalf("expected one create schema statement, got %d: %#v", len(fakeDB.execQueries), fakeDB.execQueries)
|
||||
}
|
||||
const want = `CREATE SCHEMA "tenant""schema"`
|
||||
if fakeDB.execQueries[0] != want {
|
||||
t.Fatalf("unexpected create schema SQL, want %q got %q", want, fakeDB.execQueries[0])
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user