diff --git a/cif/update.go b/cif/update.go index e44aed1..b306afd 100644 --- a/cif/update.go +++ b/cif/update.go @@ -33,8 +33,14 @@ func runCifFullDownload(cfg *helpers.Configuration) error { return err } - // Drop timetable collection - dbAccess.DropCollection(dbAccess.TimetableCollection) // I should edit this to prevent removal of VSTP entries in the database. + // Try to remove all non-vstp entries, else give up and delete collection + count, err := dbAccess.PurgeNonVstp() + if err != nil { + log.Warn("Error purging non-vstp schedules, dropping collection", zap.Error(err)) + dbAccess.DropCollection(dbAccess.TimetableCollection) // I should edit this to prevent removal of VSTP entries in the database. + } else { + log.Info("Removed non-vstp services", zap.Int64("deletion count", count)) + } // Process CIF file err = ProcessParsedCif(parsed) diff --git a/dbAccess/cif.go b/dbAccess/cif.go index a206786..2dbeff3 100644 --- a/dbAccess/cif.go +++ b/dbAccess/cif.go @@ -108,6 +108,19 @@ func DeleteCifEntries(deletions []database.DeleteQuery) error { return nil } +// Clears all non-vstp services from the database. Used when a CIF full download is required. +func PurgeNonVstp() (int64, error) { + coll := MongoClient.Database(DatabaseName).Collection(TimetableCollection) + filter := bson.M{"serviceDetail.vstp": false} + + result, err := coll.DeleteMany(context.Background(), filter) + if err != nil { + return result.DeletedCount, err + } + + return result.DeletedCount, nil +} + // Handles 'Create' tasks for CIF Schedule updates, accepts Service structs and batches their creation. func CreateCifEntries(schedules []database.Service) error { // Skip if deletions is empty