pomme/internal/db/db.go
Sam Therapy f0fdd15473
use new import URL
Signed-off-by: Sam Therapy <sam@samtherapy.net>
2023-02-17 19:42:48 +01:00

27 lines
585 B
Go

package db
import (
"fmt"
"dns.froth.zone/pomme/internal"
"github.com/glebarez/sqlite"
"gorm.io/gorm"
)
// InitDb is the init function for the database.
func InitDb(path string) (db *gorm.DB, err error, ok bool) {
ok = true
db, err = gorm.Open(sqlite.Open(path), &gorm.Config{})
if err != nil {
return db, fmt.Errorf("failed to connect database: %w", err), !ok
}
// Migrate the schema
err = db.AutoMigrate(&internal.User{}, &internal.ZoneRequest{})
if err != nil {
return &gorm.DB{}, fmt.Errorf("failed to run DB migration: %w", err), !ok
}
return db, nil, ok
}