Hallucinate a whole lot of shit...
Too much shit...
This commit is contained in:
64
repositories/database.go
Normal file
64
repositories/database.go
Normal file
@@ -0,0 +1,64 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"go-eve-pi/options"
|
||||
"go-eve-pi/types"
|
||||
|
||||
"gorm.io/driver/sqlite"
|
||||
"gorm.io/gorm"
|
||||
|
||||
logger "git.site.quack-lab.dev/dave/cylogger"
|
||||
)
|
||||
|
||||
// Database manages all repositories and provides a unified interface
|
||||
type Database struct {
|
||||
*gorm.DB
|
||||
Character *CharacterRepository
|
||||
Cache *CacheRepository
|
||||
}
|
||||
|
||||
// NewDatabase creates a new database instance with all repositories
|
||||
func NewDatabase() (*Database, error) {
|
||||
// Get database path from options
|
||||
dbPath := options.GlobalOptions.DBPath
|
||||
if dbPath == "" {
|
||||
dbPath = "eve-pi.db"
|
||||
}
|
||||
|
||||
// Ensure directory exists
|
||||
dir := filepath.Dir(dbPath)
|
||||
if err := os.MkdirAll(dir, 0755); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Connect to database
|
||||
db, err := gorm.Open(sqlite.Open(dbPath), &gorm.Config{})
|
||||
if err != nil {
|
||||
logger.Error("Failed to connect to database: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
logger.Info("Connected to database: %s", dbPath)
|
||||
|
||||
// Create repositories
|
||||
characterRepo := NewCharacterRepository(db)
|
||||
cacheRepo := NewCacheRepository(db)
|
||||
|
||||
database := &Database{
|
||||
DB: db,
|
||||
Character: characterRepo,
|
||||
Cache: cacheRepo,
|
||||
}
|
||||
|
||||
// Run migrations
|
||||
if err := database.AutoMigrate(&types.Character{}, &types.CacheEntry{}); err != nil {
|
||||
logger.Error("Failed to run database migrations: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
logger.Info("Database initialized successfully")
|
||||
return database, nil
|
||||
}
|
||||
Reference in New Issue
Block a user