{"record":{"id":"a0f566e0b80b8817","repo":"apache/answer","slug":"get-first-version-failed-v","errorCode":null,"errorMessage":"get first version failed: %v","messagePattern":"get first version failed: (.+?)","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/migrations/migrations.go","lineNumber":128,"sourceCode":"\tNewMigration(\"v2.0.1\", \"change avatar type to text\", updateAvatarType, false),\n\tNewMigration(\"v2.0.2\", \"add reasoning content to ai conversation record\", addAIConversationReasoningContent, false),\n\tNewMigration(\"v2.0.3\", \"add require email verification login setting\", addRequireEmailVerification, true),\n}\n\nfunc GetMigrations() []Migration {\n\treturn migrations\n}\n\n// GetCurrentDBVersion returns the current db version\nfunc GetCurrentDBVersion(engine *xorm.Engine) (int64, error) {\n\tif err := engine.Sync(new(entity.Version)); err != nil {\n\t\treturn -1, fmt.Errorf(\"sync version failed: %v\", err)\n\t}\n\n\tcurrentVersion := &entity.Version{ID: 1}\n\thas, err := engine.Get(currentVersion)\n\tif err != nil {\n\t\treturn -1, fmt.Errorf(\"get first version failed: %v\", err)\n\t}\n\tif !has {\n\t\t_, err := engine.InsertOne(&entity.Version{ID: 1, VersionNumber: 0})\n\t\tif err != nil {\n\t\t\treturn -1, fmt.Errorf(\"insert first version failed: %v\", err)\n\t\t}\n\t\treturn 0, nil\n\t}\n\treturn currentVersion.VersionNumber, nil\n}\n\n// ExpectedVersion returns the expected db version\nfunc ExpectedVersion() int64 {\n\treturn int64(minDBVersion + len(migrations))\n}\n\n// Migrate database to current version\nfunc Migrate(debug bool, dbConf *data.Database, cacheConf *data.CacheConf, upgradeToSpecificVersion string) error {","sourceCodeStart":110,"sourceCodeEnd":146,"githubUrl":"https://github.com/apache/answer/blob/3b9f1370612e690a0b7f230f05e688930db4c6d3/internal/migrations/migrations.go#L110-L146","documentation":"After syncing, GetCurrentDBVersion fetches the row with ID=1 from the version table. If that SELECT fails, the error is wrapped as 'get first version failed: %v', meaning the version table exists but could not be read.","triggerScenarios":"engine.Get(currentVersion) on &entity.Version{ID:1} fails: table corrupted, connection dropped between Sync and Get, permissions deny SELECT on the version table, or driver/dialect error.","commonSituations":"Version table created by a different xorm version with incompatible column types; DB connection pool exhausted mid-migration; read-replica lag or read-only endpoint used for migrations.","solutions":["Inspect the wrapped DB error for the root cause","Verify SELECT privileges on the `version` table","Point migrations at the primary DB, not a read replica","Drop and recreate the empty version table if its schema is corrupt (only when safe)"],"exampleFix":"// before\nreturn -1, fmt.Errorf(\"get first version failed: %v\", err)\n// after\nreturn -1, fmt.Errorf(\"get first version failed: %w\", err)","handlingStrategy":"retry","validationCode":"// verify the version table is readable before migrating\nif has, err := engine.IsTableExist(&entity.Version{}); err == nil && has {\n    if _, err := engine.SQL(\"SELECT COUNT(*) FROM version\").Query(); err != nil {\n        return fmt.Errorf(\"version table unreadable: %w\", err)\n    }\n}","typeGuard":null,"tryCatchPattern":"version, err := migrations.GetCurrentDBVersion(engine)\nif err != nil && strings.Contains(err.Error(), \"get first version failed\") {\n    // transient DB issue: retry once after backoff\n    time.Sleep(2 * time.Second)\n    version, err = migrations.GetCurrentDBVersion(engine)\n}","preventionTips":["Use the primary DB endpoint for migrations","Check connection pool health and limits before long migrations","Ensure SELECT privileges on the version table","Avoid running Migrate during failovers or backups"],"tags":["database","migration","query-failed"],"backgroundTag":"migration-version-read-failed","analyzedSha":"3b9f1370612e690a0b7f230f05e688930db4c6d3","analyzedAt":"2026-09-05T18:18:39.533Z","contentChangedAt":"2026-09-05T18:18:39.533Z","schemaVersion":2},"datasetVersion":"2026-09-12T22:17:10.623Z"}