reworking database, init and add commands

This commit is contained in:
2020-07-02 15:00:30 -04:00
parent 6379c73e38
commit 5af55ed62e
15 changed files with 220 additions and 295 deletions

View File

@@ -1,23 +1,54 @@
package database
import (
"fmt"
"os"
"time"
"github.com/asdine/storm"
"github.com/rs/zerolog"
)
type DB struct {
*storm.DB
*zerolog.Logger
}
// OpenOrCreate returns a new database object, either from existing database or creates a new one
func OpenOrCreateDB(dbPath string, log *zerolog.Logger) (*DB, error) {
// OpenDB returns a new database object, from opening existing db
func OpenDB(dbPath string, log *zerolog.Logger, version string) (*DB, error) {
var db DB
var err error
databaseLogger := log.With().Str("module", "database").Logger() // Setting up a sub logger for the database module
db.Logger = &databaseLogger
if err := db.ConfigureDB(dbPath); err != nil {
db.Err(err).Msg("unable to configure database")
db.DB, err = storm.Open(dbPath)
if err != nil {
db.Err(err).Msg("No existing database found. this does not appear to be a repo, please init repo")
return &db, err
}
defer db.Close()
return &db, nil
}
// CreateDB sets up bolt and Storm according to the path of the database //TODO: Save a backup of the config in DB on creation
func CreateDB(dbPath string, version string, repoName string) error {
db, err := storm.Open(dbPath)
if err != nil {
fmt.Println("error initializing database")
return err
}
defer db.Close()
fmt.Println("Initializing new database..")
initTime := time.Now().String()
var gvcInit GVCInfo
gvcInit.InitTime = initTime
gvcInit.GVCVersion = version
gvcInit.RepoName = repoName
if err := db.Save(&gvcInit); err != nil {
fmt.Println("Unable to init db")
return err
}
return nil
}
// CheckDB checks to see if database exists
func CheckDB(dbPath string) error {
if _, err := os.Stat(dbPath); os.IsNotExist(err) {
return err
}
return nil
}

View File

@@ -3,34 +3,9 @@ package database
import (
"fmt"
"github.com/asdine/storm"
"github.com/asdine/storm/q"
)
// ConfigureDB sets up bolt and Storm according to the path of the database
// this is done here so that different databases can be configured in different scenarios
func (db *DB) ConfigureDB(dbPath string) error {
var err error
if db.DB, err = storm.Open(dbPath); err != nil {
return err
}
var file File
if err := db.One("Name", "-- All Files --", &file); err != nil {
if err.Error() != "not found" {
db.Err(err).Msg("Error finding file by path")
return err
}
db.Warn().Msg("No existing databse found. initialising new database")
file.Name = "-- All Files --"
//file.Ignore = true //this is currently not used however could result in this file being ignored when file watching (etc) starts
if err := db.Save(&file); err != nil {
db.Err(err).Msg("Error storing the diff")
return err
}
}
return nil
}
// CheckIfFileCurrentlyMonitored checks if the file is already being monitored. This is a read-only check
// to see whether the file was correctly initialised
// (BUG) The hash causes the same file to be in database multiple times!
@@ -55,10 +30,12 @@ func (db *DB) CheckIfFileCurrentlyMonitored(path string) bool {
// This can be used to trigger the same files to be watched again
func (db *DB) RetrieveTrackedFiles() ([]File, error) {
var files []File
fmt.Println("Starting file extraction")
if err := db.All(&files); err != nil {
db.Err(err).Msg("Error retrieving all watched files")
return []File{}, err
}
fmt.Println("Ending file extraction")
return files, nil
}

View File

@@ -5,11 +5,26 @@ import (
"fmt"
"io/ioutil"
"time"
"github.com/asdine/storm"
"github.com/rs/zerolog"
)
type DB struct {
*storm.DB
*zerolog.Logger
}
// GVCInfo stores the basic information about when the repo was init'd and some basic information
type GVCInfo struct {
GVCVersion string `storm:"id"`
InitTime string
RepoName string
}
// Commit stores all the necessary information for a commit
type Commit struct {
CommitHash []byte // The hash of the commit (generated by hashing commit author name, time, the previous commit, and more? TODO: Not sure what else)
CommitHash []byte `storm:"index,unique"` // The hash of the commit (generated by hashing commit author name, time, the previous commit, and more? TODO: Not sure what else)
TrackedFiles []File // All of the tracked files for this commit
Date string
Version string //User can tag a commit with a version number