47 lines
1.1 KiB
Go
47 lines
1.1 KiB
Go
package database
|
|
|
|
import (
|
|
"orbits-server/internal/shared/utility"
|
|
"path/filepath"
|
|
|
|
"gorm.io/driver/sqlite"
|
|
"gorm.io/gorm"
|
|
"gorm.io/gorm/logger"
|
|
)
|
|
|
|
//var watchdogStop = make(chan struct{})
|
|
|
|
func Kickoff(workDir string) (*gorm.DB, error) {
|
|
dbLoc := filepath.Join(workDir, "station.db")
|
|
db, err := gorm.Open(sqlite.Open(dbLoc), &gorm.Config{
|
|
Logger: logger.Discard, // disable gorm logging since its not slog (yet)
|
|
TranslateError: true,
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// try to use GORM automigrate if the schema changes
|
|
if err := db.AutoMigrate(
|
|
&AccessKey{}, // api keys for authentication
|
|
&Command{}, // app state and command status
|
|
&File{}, // files database for keeping track
|
|
&Tenant{}, // table for tenants and its data
|
|
&Group{}, // group table for privileges
|
|
&Device{}, // devices table
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// create the first row if it does not exist yet
|
|
if err := db.FirstOrCreate(&Command{}, Command{
|
|
ID: 0,
|
|
State: "idle",
|
|
MediaType: utility.Unspecified,
|
|
}).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return db, nil
|
|
}
|