{"record":{"id":"bbcbf419a9f80cd7","repo":"flipped-aurora/gin-vue-admin","slug":"sqlite-config-invalid","errorCode":null,"errorMessage":"sqlite config invalid","messagePattern":"sqlite config invalid","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"server/service/system/sys_initdb_sqlite.go","lineNumber":28,"sourceCode":"\t\"path/filepath\"\n\n\t\"github.com/flipped-aurora/gin-vue-admin/server/config\"\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/flipped-aurora/gin-vue-admin/server/utils\"\n)\n\ntype SqliteInitHandler struct{}\n\nfunc NewSqliteInitHandler() *SqliteInitHandler {\n\treturn &SqliteInitHandler{}\n}\n\n// WriteConfig mysql回写配置\nfunc (h SqliteInitHandler) WriteConfig(ctx context.Context) error {\n\tc, ok := ctx.Value(\"config\").(config.Sqlite)\n\tif !ok {\n\t\treturn errors.New(\"sqlite config invalid\")\n\t}\n\tglobal.GVA_CONFIG.System.DbType = \"sqlite\"\n\tglobal.GVA_CONFIG.Sqlite = 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 创建数据库并初始化 sqlite\nfunc (h SqliteInitHandler) EnsureDB(ctx context.Context, conf *request.InitDB) (next context.Context, err error) {\n\tif s, ok := ctx.Value(\"dbtype\").(string); !ok || s != \"sqlite\" {\n\t\treturn ctx, ErrDBTypeMismatch\n\t}\n","sourceCodeStart":10,"sourceCodeEnd":46,"githubUrl":"https://github.com/flipped-aurora/gin-vue-admin/blob/3136500ef380842b0eb6c4daa87c3f8a47fcf9e0/server/service/system/sys_initdb_sqlite.go#L10-L46","documentation":"SqliteInitHandler.WriteConfig writes the SQLite init settings back to config.yaml (DbType=\"sqlite\", rotated JWT signing key). It type-asserts the context value \"config\" to config.Sqlite and returns \"sqlite config invalid\" if the assertion fails, i.e. the context was not produced by this handler's EnsureDB. (The doc comment still says \"mysql回写配置\" — a stale copy-paste; the code is sqlite.)","triggerScenarios":"Calling SqliteInitHandler.WriteConfig(ctx) where ctx's \"config\" value is absent or is another DB's config struct (Mysql/Pgsql/Mssql) rather than config.Sqlite.","commonSituations":"Mixing handlers in a custom init flow; invoking WriteConfig directly in tests without EnsureDB; refactors that change what EnsureDB stores under \"config\".","solutions":["Feed WriteConfig the context returned by SqliteInitHandler.EnsureDB, which stores conf.ToSqliteConfig() under key \"config\".","Run the standard InitDBService.InitDB with DBType=\"sqlite\" instead of hand-wiring handlers.","Ensure no other handler replaced the \"config\" value in the context.","Manually: ctx = context.WithValue(ctx, \"config\", conf.ToSqliteConfig()) before calling."],"exampleFix":"// before\nctx, _ = mysqlHandler.EnsureDB(ctx, &conf)\nerr = sqliteHandler.WriteConfig(ctx) // -> sqlite config invalid\n// after\nctx, _ = sqliteHandler.EnsureDB(ctx, &conf)\nerr = sqliteHandler.WriteConfig(ctx)","handlingStrategy":"type-guard","validationCode":"if _, ok := ctx.Value(\"config\").(config.Sqlite); !ok {\n\treturn errors.New(\"sqlite config missing in context\")\n}","typeGuard":"func hasSqliteConfig(ctx context.Context) bool {\n\t_, ok := ctx.Value(\"config\").(config.Sqlite)\n\treturn ok\n}","tryCatchPattern":"err := handler.WriteConfig(ctx)\nif err != nil && err.Error() == \"sqlite config invalid\" {\n\t// ctx not produced by SqliteInitHandler.EnsureDB; rerun EnsureDB first\n}","preventionTips":["Only feed WriteConfig the context from SqliteInitHandler.EnsureDB.","Don't overwrite the \"config\" key with another DB's struct.","Prefer InitDBService.InitDB with DBType=\"sqlite\".","Test the EnsureDB -> WriteConfig sequence for sqlite."],"tags":["go","sqlite","configuration"],"backgroundTag":"config-type-assertion-failed","analyzedSha":"3136500ef380842b0eb6c4daa87c3f8a47fcf9e0","analyzedAt":"2026-08-31T13:50:02.721Z","schemaVersion":2},"datasetVersion":"2026-08-31T19:17:28.585Z"}