flipped-aurora/gin-vue-admin · error
mssql config invalid
Error message
mssql config invalid
What it means
MssqlInitHandler.WriteConfig persists the chosen MSSQL config back to config.yaml. It first type-asserts the context value "config" to config.Mssql; if the assertion fails (the key is missing or holds another DB's config struct), it returns "mssql config invalid". This indicates the handler was invoked on a context not prepared by its own EnsureDB.
Source
Thrown at server/service/system/sys_initdb_mssql.go:27
"github.com/flipped-aurora/gin-vue-admin/server/utils"
"github.com/google/uuid"
"github.com/gookit/color"
"gorm.io/driver/sqlserver"
"gorm.io/gorm"
"path/filepath"
)
type MssqlInitHandler struct{}
func NewMssqlInitHandler() *MssqlInitHandler {
return &MssqlInitHandler{}
}
// WriteConfig mssql回写配置
func (h MssqlInitHandler) WriteConfig(ctx context.Context) error {
c, ok := ctx.Value("config").(config.Mssql)
if !ok {
return errors.New("mssql config invalid")
}
global.GVA_CONFIG.System.DbType = "mssql"
global.GVA_CONFIG.Mssql = c
global.GVA_CONFIG.JWT.SigningKey = uuid.New().String()
cs := utils.StructToMap(global.GVA_CONFIG)
for k, v := range cs {
global.GVA_VP.Set(k, v)
}
global.GVA_ACTIVE_DBNAME = &c.Dbname
return global.GVA_VP.WriteConfig()
}
// EnsureDB 创建数据库并初始化 mssql
func (h MssqlInitHandler) EnsureDB(ctx context.Context, conf *request.InitDB) (next context.Context, err error) {
if s, ok := ctx.Value("dbtype").(string); !ok || s != "mssql" {
return ctx, ErrDBTypeMismatch
}
View on GitHub (pinned to 3136500ef3)
Solutions
- Only call WriteConfig with the context returned from the same MssqlInitHandler.EnsureDB call, which stores config.Mssql under key "config".
- Go through InitDBService.InitDB(conf) with conf.DBType="mssql" so handler pairing is automatic.
- Check for handler mix-ups: each DB's EnsureDB must feed its own WriteConfig.
- If writing custom code, set ctx = context.WithValue(ctx, "config", conf.ToMssqlConfig()) before calling.
Example fix
// before ctx, _ = mysqlHandler.EnsureDB(ctx, &conf) // stores config.Mysql err = mssqlHandler.WriteConfig(ctx) // -> mssql config invalid // after ctx, _ = mssqlHandler.EnsureDB(ctx, &conf) // stores config.Mssql err = mssqlHandler.WriteConfig(ctx)
Defensive patterns
Strategy: type-guard
Validate before calling
if c, ok := ctx.Value("config").(config.Mssql); !ok {
return errors.New("mssql config missing in context")
} Type guard
func hasMssqlConfig(ctx context.Context) bool {
_, ok := ctx.Value("config").(config.Mssql)
return ok
} Try / catch
err := handler.WriteConfig(ctx)
if err != nil && err.Error() == "mssql config invalid" {
// ctx not produced by MssqlInitHandler.EnsureDB; rerun EnsureDB first
} Prevention
- Always pair each handler's EnsureDB with its own WriteConfig.
- Pass EnsureDB's returned context directly to WriteConfig; don't rebuild it.
- Prefer InitDBService.InitDB which guarantees handler pairing.
- Add a unit test asserting WriteConfig succeeds after the same handler's EnsureDB.
When it happens
Trigger: Calling MssqlInitHandler.WriteConfig(ctx) where ctx lacks a "config" value of concrete type config.Mssql — e.g. the value was never set, was set by a different handler (config.Mysql/Pgsql/Sqlite), or was set under a different key.
Common situations: Mixing handlers (mysql EnsureDB + mssql WriteConfig); custom code calling WriteConfig with a hand-built context; refactors renaming or retyping the ctx "config" value.
Related errors
AI-assisted analysis of flipped-aurora/gin-vue-admin@3136500ef3 (2026-08-31).
Data as JSON: /api/errors/6cdcdb8b0ee38dd4.
Report an issue: GitHub.