Guard against missing Mail-Graveyard DB

This commit is contained in:
DonVoo 2026-07-14 19:24:50 +02:00
parent d4d43fe672
commit 29208b7a41
4 changed files with 127 additions and 3 deletions

View file

@ -64,10 +64,13 @@ type AppUser struct {
// Zweiter Lauf kopiert nur das Delta.
// jobs(account_id, started, finished, -- Lauf-Historie fuer den Fortschritt
// total, done, errors, state) im Browser.
func ConnectDB() error {
func ConnectDB(initDB bool) error {
if Cfg.DBPath == "" {
Cfg.DBPath = "mail-graveyard.db"
}
if err := ensureDBFile(Cfg.DBPath, initDB); err != nil {
return err
}
db, err := sql.Open("sqlite", Cfg.DBPath)
if err != nil {
return err
@ -167,6 +170,27 @@ func ConnectDB() error {
return nil
}
func ensureDBFile(path string, initDB bool) error {
info, err := os.Stat(path)
if err == nil {
if info.IsDir() {
return fmt.Errorf("DB %q ist ein Verzeichnis", path)
}
return nil
}
if !errors.Is(err, os.ErrNotExist) {
return err
}
if !initDB {
return fmt.Errorf("DB %q existiert nicht -- mit --init-db neu anlegen oder db_path pruefen", path)
}
f, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0600)
if err != nil {
return err
}
return f.Close()
}
func ensureAppUserColumns() error {
hasDisplayName := false
rows, err := DB.Query(`PRAGMA table_info(app_users)`)