{"record":{"id":"d83d8db257caffb4","repo":"flipped-aurora/gin-vue-admin","slug":"mysql-config-invalid","errorCode":null,"errorMessage":"mysql config invalid","messagePattern":"mysql config invalid","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"server/service/system/sys_initdb_mysql.go","lineNumber":31,"sourceCode":"\n\t\"github.com/flipped-aurora/gin-vue-admin/server/global\"\n\t\"github.com/flipped-aurora/gin-vue-admin/server/model/system/request\"\n\t\"github.com/google/uuid\"\n\t\"gorm.io/driver/mysql\"\n\t\"gorm.io/gorm\"\n)\n\ntype MysqlInitHandler struct{}\n\nfunc NewMysqlInitHandler() *MysqlInitHandler {\n\treturn &MysqlInitHandler{}\n}\n\n// WriteConfig mysql回写配置\nfunc (h MysqlInitHandler) WriteConfig(ctx context.Context) error {\n\tc, ok := ctx.Value(\"config\").(config.Mysql)\n\tif !ok {\n\t\treturn errors.New(\"mysql config invalid\")\n\t}\n\tglobal.GVA_CONFIG.System.DbType = \"mysql\"\n\tglobal.GVA_CONFIG.Mysql = 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 创建数据库并初始化 mysql\nfunc (h MysqlInitHandler) EnsureDB(ctx context.Context, conf *request.InitDB) (next context.Context, err error) {\n\tif s, ok := ctx.Value(\"dbtype\").(string); !ok || s != \"mysql\" {\n\t\treturn ctx, ErrDBTypeMismatch\n\t}\n","sourceCodeStart":13,"sourceCodeEnd":49,"githubUrl":"https://github.com/flipped-aurora/gin-vue-admin/blob/3136500ef380842b0eb6c4daa87c3f8a47fcf9e0/server/service/system/sys_initdb_mysql.go#L13-L49","documentation":"MysqlInitHandler.WriteConfig writes the MySQL settings chosen during init back into config.yaml (setting System.DbType=\"mysql\" and rotating the JWT signing key). It type-asserts the context value \"config\" to config.Mysql and returns \"mysql config invalid\" when the assertion fails, meaning the context was not produced by this handler's EnsureDB.","triggerScenarios":"Calling MysqlInitHandler.WriteConfig(ctx) where ctx has no \"config\" value, or the value is config.Pgsql/Mssql/Sqlite (set by another handler) rather than config.Mysql.","commonSituations":"Calling WriteConfig from a different handler's pipeline; custom initialization code building its own context; tests invoking WriteConfig without running EnsureDB first.","solutions":["Obtain the context from MysqlInitHandler.EnsureDB (which stores conf.ToMysqlConfig() under key \"config\") before calling WriteConfig.","Prefer the standard InitDBService.InitDB flow with DBType=\"mysql\", which guarantees handler pairing.","Verify no other handler overwrote the \"config\" key with a different DB struct.","In custom code, set ctx = context.WithValue(ctx, \"config\", conf.ToMysqlConfig()) first."],"exampleFix":"// before\nctx, _ = pgsqlHandler.EnsureDB(ctx, &conf)\nerr = mysqlHandler.WriteConfig(ctx) // -> mysql config invalid\n// after\nctx, _ = mysqlHandler.EnsureDB(ctx, &conf)\nerr = mysqlHandler.WriteConfig(ctx)","handlingStrategy":"type-guard","validationCode":"if _, ok := ctx.Value(\"config\").(config.Mysql); !ok {\n\treturn errors.New(\"mysql config missing in context\")\n}","typeGuard":"func hasMysqlConfig(ctx context.Context) bool {\n\t_, ok := ctx.Value(\"config\").(config.Mysql)\n\treturn ok\n}","tryCatchPattern":"err := handler.WriteConfig(ctx)\nif err != nil && err.Error() == \"mysql config invalid\" {\n\t// ctx not produced by MysqlInitHandler.EnsureDB; rerun EnsureDB first\n}","preventionTips":["Only feed WriteConfig the context from MysqlInitHandler.EnsureDB.","Don't let another DB handler overwrite the \"config\" context key.","Prefer the standard InitDB flow over manual handler wiring.","Test the EnsureDB -> WriteConfig sequence per DB type."],"tags":["go","mysql","configuration"],"backgroundTag":"config-type-assertion-failed","analyzedSha":"3136500ef380842b0eb6c4daa87c3f8a47fcf9e0","analyzedAt":"2026-08-31T13:50:02.721Z","schemaVersion":2},"datasetVersion":"2026-08-31T19:17:28.585Z"}