Simplify schema reading

This commit is contained in:
binwiederhier
2026-03-03 14:52:29 -05:00
parent 33b19814c7
commit bff2b47eb6

View File

@@ -216,24 +216,13 @@ func setupSQLite(db *sql.DB, startupQueries string, cacheDuration time.Duration)
return err return err
} }
// If 'messages' table does not exist, this must be a new database // If 'messages' table does not exist, this must be a new database
rowsMC, err := db.Query(sqliteSelectMessagesCountQuery) var messagesCount int
if err != nil { if err := db.QueryRow(sqliteSelectMessagesCountQuery).Scan(&messagesCount); err != nil {
return setupNewSQLite(db) return setupNewSQLite(db)
} }
rowsMC.Close() // If 'messages' table exists (schema >= 0), check 'schemaVersion' table
// If 'messages' table exists, check 'schemaVersion' table var schemaVersion int
schemaVersion := 0 db.QueryRow(sqliteSelectSchemaVersionQuery).Scan(&schemaVersion) // Error means schema version is zero!
rowsSV, err := db.Query(sqliteSelectSchemaVersionQuery)
if err == nil {
defer rowsSV.Close()
if !rowsSV.Next() {
return fmt.Errorf("cannot determine schema version: cache file may be corrupt")
}
if err := rowsSV.Scan(&schemaVersion); err != nil {
return err
}
rowsSV.Close()
}
// Do migrations // Do migrations
if schemaVersion == sqliteCurrentSchemaVersion { if schemaVersion == sqliteCurrentSchemaVersion {
return nil return nil