{"record":{"id":"6cdcdb8b0ee38dd4","repo":"flipped-aurora/gin-vue-admin","slug":"mssql-config-invalid","errorCode":null,"errorMessage":"mssql config invalid","messagePattern":"mssql config invalid","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"server/service/system/sys_initdb_mssql.go","lineNumber":27,"sourceCode":"\t\"github.com/flipped-aurora/gin-vue-admin/server/utils\"\n\t\"github.com/google/uuid\"\n\t\"github.com/gookit/color\"\n\t\"gorm.io/driver/sqlserver\"\n\t\"gorm.io/gorm\"\n\t\"path/filepath\"\n)\n\ntype MssqlInitHandler struct{}\n\nfunc NewMssqlInitHandler() *MssqlInitHandler {\n\treturn &MssqlInitHandler{}\n}\n\n// WriteConfig mssql回写配置\nfunc (h MssqlInitHandler) WriteConfig(ctx context.Context) error {\n\tc, ok := ctx.Value(\"config\").(config.Mssql)\n\tif !ok {\n\t\treturn errors.New(\"mssql config invalid\")\n\t}\n\tglobal.GVA_CONFIG.System.DbType = \"mssql\"\n\tglobal.GVA_CONFIG.Mssql = c\n\tglobal.GVA_CONFIG.JWT.SigningKey = uuid.New().String()\n\tcs := utils.StructToMap(global.GVA_CONFIG)\n\tfor k, v := range cs {\n\t\tglobal.GVA_VP.Set(k, v)\n\t}\n\tglobal.GVA_ACTIVE_DBNAME = &c.Dbname\n\treturn global.GVA_VP.WriteConfig()\n}\n\n// EnsureDB 创建数据库并初始化 mssql\nfunc (h MssqlInitHandler) EnsureDB(ctx context.Context, conf *request.InitDB) (next context.Context, err error) {\n\tif s, ok := ctx.Value(\"dbtype\").(string); !ok || s != \"mssql\" {\n\t\treturn ctx, ErrDBTypeMismatch\n\t}\n","sourceCodeStart":9,"sourceCodeEnd":45,"githubUrl":"https://github.com/flipped-aurora/gin-vue-admin/blob/3136500ef380842b0eb6c4daa87c3f8a47fcf9e0/server/service/system/sys_initdb_mssql.go#L9-L45","documentation":"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.","triggerScenarios":"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.","commonSituations":"Mixing handlers (mysql EnsureDB + mssql WriteConfig); custom code calling WriteConfig with a hand-built context; refactors renaming or retyping the ctx \"config\" value.","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."],"exampleFix":"// before\nctx, _ = mysqlHandler.EnsureDB(ctx, &conf) // stores config.Mysql\nerr = mssqlHandler.WriteConfig(ctx)        // -> mssql config invalid\n// after\nctx, _ = mssqlHandler.EnsureDB(ctx, &conf) // stores config.Mssql\nerr = mssqlHandler.WriteConfig(ctx)","handlingStrategy":"type-guard","validationCode":"if c, ok := ctx.Value(\"config\").(config.Mssql); !ok {\n\treturn errors.New(\"mssql config missing in context\")\n}","typeGuard":"func hasMssqlConfig(ctx context.Context) bool {\n\t_, ok := ctx.Value(\"config\").(config.Mssql)\n\treturn ok\n}","tryCatchPattern":"err := handler.WriteConfig(ctx)\nif err != nil && err.Error() == \"mssql config invalid\" {\n\t// ctx not produced by MssqlInitHandler.EnsureDB; rerun EnsureDB first\n}","preventionTips":["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."],"tags":["go","mssql","configuration"],"backgroundTag":"config-type-assertion-failed","analyzedSha":"3136500ef380842b0eb6c4daa87c3f8a47fcf9e0","analyzedAt":"2026-08-31T13:50:02.721Z","schemaVersion":2},"datasetVersion":"2026-08-31T19:17:28.585Z"}