{"record":{"id":"045136cf7099903c","repo":"flipped-aurora/gin-vue-admin","slug":"db-not-init","errorCode":null,"errorMessage":"db not init","messagePattern":"db not init","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"server/service/system/sys_user.go","lineNumber":55,"sourceCode":"\tu.MustChangePassword = cfg.ForceNewUserChangePassword\n\tu.Password = utils.BcryptHash(u.Password)\n\tu.UUID = uuid.New()\n\tnow := time.Now()\n\tu.PasswordUpdatedAt = &now\n\terr = global.GVA_DB.WithContext(ctx).Create(&u).Error\n\treturn u, err\n}\n\n//@author: [piexlmax](https://github.com/piexlmax)\n//@author: [SliverHorn](https://github.com/SliverHorn)\n//@function: Login\n//@description: 用户登录\n//@param: u *model.SysUser\n//@return: err error, userInter *model.SysUser\n\nfunc (userService *UserService) Login(ctx context.Context, u *system.SysUser) (userInter *system.SysUser, err error) {\n\tif nil == global.GVA_DB {\n\t\treturn nil, fmt.Errorf(\"db not init\")\n\t}\n\n\tvar user system.SysUser\n\terr = global.GVA_DB.WithContext(ctx).Where(\"username = ?\", u.Username).Preload(\"Authorities\").Preload(\"Authority\").Preload(\"Departments\").Preload(\"Positions\").Preload(\"Dept\").First(&user).Error\n\tif err == nil {\n\t\tif ok := utils.BcryptCheck(u.Password, user.Password); !ok {\n\t\t\treturn nil, errors.New(\"密码错误\")\n\t\t}\n\t\tMenuServiceApp.UserAuthorityDefaultRouter(ctx, &user)\n\t}\n\treturn &user, err\n}\n\n//@author: [piexlmax](https://github.com/piexlmax)\n//@function: ChangePassword\n//@description: 修改用户密码\n//@param: u *model.SysUser, newPassword string\n//@return: err error","sourceCodeStart":37,"sourceCodeEnd":73,"githubUrl":"https://github.com/flipped-aurora/gin-vue-admin/blob/3136500ef380842b0eb6c4daa87c3f8a47fcf9e0/server/service/system/sys_user.go#L37-L73","documentation":"UserService.Login checks global.GVA_DB before querying the user; if the global DB singleton is nil (database never initialized), login is impossible and this sentinel error is returned. It indicates the server booted without a working DB connection rather than a per-request problem.","triggerScenarios":"Calling login on a server where GVA initialization failed or was skipped — missing/misnamed config, DB unreachable at startup, running a stripped test binary without init.GlobalDB.","commonSituations":"Wrong config.yaml (yaml extension vs yml) so viper loads nothing; MySQL down/mis-credentials causing init to skip assignment; unit tests invoking Login without initializing GVA_DB.","solutions":["Fix server config.yaml database settings and restart so initialize initializes global.GVA_DB","Check startup logs for DB init errors (connection refused, auth failed) and resolve them","In tests, use the shared testutil helper (testutil.NewMemoryDB(t, ...)) instead of invoking Login on a nil DB"],"exampleFix":"// before (test)\nuser := &system.SysUser{Username:\"a\"}\n_, err := userService.Login(ctx, user) // db not init\n// after\ndb := testutil.NewMemoryDB(t, &system.SysUser{})\n_ = db\n_, err := userService.Login(ctx, user)","handlingStrategy":"try-catch","validationCode":"if global.GVA_DB == nil {\n    return errors.New(\"database not initialized; check config and startup logs\")\n}","typeGuard":null,"tryCatchPattern":"userInter, err := userService.Login(ctx, u)\nif err != nil {\n    if err.Error() == \"db not init\" {\n        log.Fatal(\"GVA_DB is nil: verify config.yaml DB settings and initialize sequence\")\n    }\n}","preventionTips":["Verify database section of server config.yaml on every deploy","Watch startup logs for DB initialization errors before accepting traffic","In tests, always initialize the DB via testutil.NewMemoryDB before calling services","Add a readiness probe that fails when global.GVA_DB is nil"],"tags":["database","initialization","config","go"],"backgroundTag":"missing-env-var","analyzedSha":"3136500ef380842b0eb6c4daa87c3f8a47fcf9e0","analyzedAt":"2026-08-31T13:50:02.721Z","schemaVersion":2},"datasetVersion":"2026-08-31T19:17:28.585Z"}