working toward storing state in db
This commit is contained in:
37
database/dbmodel.go
Normal file
37
database/dbmodel.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jinzhu/gorm"
|
||||
|
||||
_ "github.com/jinzhu/gorm/dialects/sqlite"
|
||||
)
|
||||
|
||||
// NB that when you add a model below you must add it to this list!
|
||||
|
||||
func (m *Manager) doMigrations() {
|
||||
m.db.AutoMigrate(&instance{})
|
||||
}
|
||||
|
||||
type instance struct {
|
||||
gorm.Model
|
||||
Identifier uuid.UUID
|
||||
ErrorCount uint
|
||||
SuccessCount uint
|
||||
HighestID int
|
||||
Hostname string
|
||||
Identified bool
|
||||
Fetching bool
|
||||
Disabled bool
|
||||
Implementation string
|
||||
NextFetch time.Time
|
||||
NodeInfoURL string
|
||||
ServerVersionString string
|
||||
ServerImplementationString string
|
||||
}
|
||||
|
||||
func (m *Manager) ListInstances() {
|
||||
// FIXME have this produce a list of Instance
|
||||
}
|
||||
62
database/manager.go
Normal file
62
database/manager.go
Normal file
@@ -0,0 +1,62 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/jinzhu/gorm"
|
||||
|
||||
_ "github.com/jinzhu/gorm/dialects/sqlite"
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
type Manager struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
func New() *Manager {
|
||||
m := new(Manager)
|
||||
m.init()
|
||||
return m
|
||||
}
|
||||
|
||||
func (m *Manager) init() {
|
||||
m.open()
|
||||
}
|
||||
|
||||
func mkdirp(p string) error {
|
||||
src, err := os.Stat(p)
|
||||
if os.IsNotExist(err) {
|
||||
errDir := os.MkdirAll(p, 0755)
|
||||
if errDir != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
if src.Mode().IsRegular() {
|
||||
return errors.New("file already exists at path")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Manager) open() {
|
||||
log.Info().Msg("opening database")
|
||||
dirname := filepath.Dir(viper.GetString("DbStorageLocation"))
|
||||
err := mkdirp(dirname)
|
||||
if err != nil {
|
||||
log.Panic().
|
||||
Err(err).
|
||||
Msg("db path erro")
|
||||
}
|
||||
|
||||
db, err := gorm.Open("sqlite3", viper.GetString("DbStorageLocation"))
|
||||
if err != nil {
|
||||
log.Panic().
|
||||
Err(err).
|
||||
Msg("failed to open database")
|
||||
}
|
||||
m.db = db
|
||||
m.doMigrations()
|
||||
}
|
||||
Reference in New Issue
Block a user