{"record":{"id":"8d5c0930e46916b0","repo":"flipped-aurora/gin-vue-admin","slug":"postgresql-config-invalid","errorCode":null,"errorMessage":"postgresql config invalid","messagePattern":"postgresql config invalid","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"server/service/system/sys_initdb_pgsql.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/postgres\"\n\t\"gorm.io/gorm\"\n)\n\ntype PgsqlInitHandler struct{}\n\nfunc NewPgsqlInitHandler() *PgsqlInitHandler {\n\treturn &PgsqlInitHandler{}\n}\n\n// WriteConfig pgsql 回写配置\nfunc (h PgsqlInitHandler) WriteConfig(ctx context.Context) error {\n\tc, ok := ctx.Value(\"config\").(config.Pgsql)\n\tif !ok {\n\t\treturn errors.New(\"postgresql config invalid\")\n\t}\n\tglobal.GVA_CONFIG.System.DbType = \"pgsql\"\n\tglobal.GVA_CONFIG.Pgsql = 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 创建数据库并初始化 pg\nfunc (h PgsqlInitHandler) EnsureDB(ctx context.Context, conf *request.InitDB) (next context.Context, err error) {\n\tif s, ok := ctx.Value(\"dbtype\").(string); !ok || s != \"pgsql\" {\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_pgsql.go#L13-L49","documentation":"PgsqlInitHandler.WriteConfig persists PostgreSQL init settings to config.yaml (DbType=\"pgsql\", new JWT signing key). It type-asserts the context value \"config\" to config.Pgsql; on failure it returns \"postgresql config invalid\". The error signals the context was not prepared by the pgsql EnsureDB.","triggerScenarios":"Calling PgsqlInitHandler.WriteConfig(ctx) when ctx's \"config\" value is missing or holds config.Mysql/Mssql/Sqlite instead of config.Pgsql.","commonSituations":"Cross-handler misuse (e.g. mysql EnsureDB feeding pgsql WriteConfig); hand-rolled init pipelines; tests calling WriteConfig in isolation.","solutions":["Call WriteConfig with the context returned by PgsqlInitHandler.EnsureDB, which stores conf.ToPgsqlConfig() under key \"config\".","Use InitDBService.InitDB with DBType=\"pgsql\" so pairing is handled for you.","Audit custom pipelines to ensure each DB handler's EnsureDB output flows to its own WriteConfig.","If needed manually: ctx = context.WithValue(ctx, \"config\", conf.ToPgsqlConfig())."],"exampleFix":"// before\nctx, _ = mssqlHandler.EnsureDB(ctx, &conf)\nerr = pgsqlHandler.WriteConfig(ctx) // -> postgresql config invalid\n// after\nctx, _ = pgsqlHandler.EnsureDB(ctx, &conf)\nerr = pgsqlHandler.WriteConfig(ctx)","handlingStrategy":"type-guard","validationCode":"if _, ok := ctx.Value(\"config\").(config.Pgsql); !ok {\n\treturn errors.New(\"postgresql config missing in context\")\n}","typeGuard":"func hasPgsqlConfig(ctx context.Context) bool {\n\t_, ok := ctx.Value(\"config\").(config.Pgsql)\n\treturn ok\n}","tryCatchPattern":"err := handler.WriteConfig(ctx)\nif err != nil && err.Error() == \"postgresql config invalid\" {\n\t// ctx not produced by PgsqlInitHandler.EnsureDB; rerun EnsureDB first\n}","preventionTips":["Only feed WriteConfig the context from PgsqlInitHandler.EnsureDB.","Keep one DB handler per init run; never mix pipelines.","Prefer InitDBService.InitDB with DBType=\"pgsql\".","Test the EnsureDB -> WriteConfig sequence for pgsql."],"tags":["go","postgresql","configuration"],"backgroundTag":"config-type-assertion-failed","analyzedSha":"3136500ef380842b0eb6c4daa87c3f8a47fcf9e0","analyzedAt":"2026-08-31T13:50:02.721Z","schemaVersion":2},"datasetVersion":"2026-08-31T19:17:28.585Z"}