Add update time check function

This commit is contained in:
Fred Boniface 2024-03-26 16:32:00 +00:00
parent 74813e2fb0
commit c144a98fd1
3 changed files with 63 additions and 1 deletions

View File

@ -8,10 +8,42 @@ import (
"git.fjla.uk/owlboard/timetable-mgr/log"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo/options"
"go.uber.org/zap"
)
const timetableCollection string = "timetable"
const databaseName string = "owlboard"
func DropCollection(collectionName string) error {
database := MongoClient.Database(databaseName)
collection := database.Collection(collectionName)
err := collection.Drop(context.Background())
if err != nil {
log.Msg.Error("Error dropping collection", zap.String("Collection Name", collectionName), zap.Error(err))
return err
}
return nil
}
func CheckUpdateTime(collectionName string) (int64, error) {
database := MongoClient.Database(databaseName)
collection := database.Collection(metaCollection)
filter := bson.D{
{Key: "target", Value: collectionName},
{Key: "type", Value: "collection"},
}
var result struct {
Updated int64 `bson:"updated"`
}
err := collection.FindOne(context.Background(), filter).Decode(&result)
if err != nil {
return 0, err
}
return result.Updated, nil
}
func PushVersionToDb() {
version := database.Version{

View File

@ -12,6 +12,11 @@ import (
"go.mongodb.org/mongo-driver/mongo/options"
)
const databaseName string = "owlboard"
const CorpusCollection string = "corpus"
const StationsCollection string = "stations"
const metaCollection string = "meta"
// Provide the DB Connection to other functions
var MongoClient (*mongo.Client)

25
src/dbAccess/corpus.go Normal file
View File

@ -0,0 +1,25 @@
package dbAccess
import (
"context"
)
const corpusCollection = "corpus"
const stationsCollection = "stations"
func dropExistingCorpus() error {
database := MongoClient.Database(databaseName)
collection := database.Collection(corpusCollection)
err := collection.Drop(context.Background())
if err != nil {
return err
}
collection = database.Collection(stationsCollection)
err = collection.Drop(context.Background())
if err != nil {
return err
}
return nil
}