Skip to content

Commit

Permalink
watcher repository: don't load history notifications for DeleteWatche…
Browse files Browse the repository at this point in the history
…rsWithStaleData
  • Loading branch information
OlehParyshkura2 committed Apr 17, 2024
1 parent 401fa1d commit 6220c1d
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 27 deletions.
22 changes: 13 additions & 9 deletions services/migration/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package migration
import (
"airdao-mobile-api/services/watcher"
"errors"
"time"

"go.mongodb.org/mongo-driver/bson/primitive"
"go.uber.org/zap"
Expand Down Expand Up @@ -79,19 +80,19 @@ func historicalNotificationMigration(db *mongo.Client, dbName string, logger *za
defer cursor.Close(context.Background())

for cursor.Next(context.Background()) {
var watcher watcher.Watcher
if err := cursor.Decode(&watcher); err != nil {
var w watcher.Watcher
if err := cursor.Decode(&w); err != nil {
return err
}

// Check if historical_notifications field is nil
if watcher.HistoricalNotifications != nil {
logger.Info("Migrating historical notifications...", watcher.ID)
if w.HistoricalNotifications != nil {
logger.Info("Migrating historical notifications...", w.ID)

// Iterate over historical notifications only if it's not nil
for _, notification := range *watcher.HistoricalNotifications {
for _, notification := range *w.HistoricalNotifications {
notification.ID = primitive.NewObjectID()
notification.WatcherID = watcher.ID
notification.WatcherID = w.ID
_, err := historyCollection.InsertOne(context.Background(), notification)
if err != nil {
return err
Expand All @@ -101,17 +102,16 @@ func historicalNotificationMigration(db *mongo.Client, dbName string, logger *za
// Update watcher to set historical_notifications to nil
_, err := watcherCollection.UpdateOne(
context.Background(),
bson.M{"_id": watcher.ID},
bson.M{"_id": w.ID},
bson.M{"$set": bson.M{"historical_notifications": nil}},
)
if err != nil {
return err
}
} else {
// Log a message or handle the case when historical_notifications is nil
logger.Info("No historical notifications found for watcher:", watcher.ID)
logger.Info("No historical notifications found for watcher:", w.ID)
}

}

if err := cursor.Err(); err != nil {
Expand All @@ -128,5 +128,9 @@ func historicalNotificationMigration(db *mongo.Client, dbName string, logger *za
if err != nil {
return err
}

// Pause execution for index to be fully formed
time.Sleep(5 * time.Second)

return nil
}
36 changes: 20 additions & 16 deletions services/watcher/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ const CollectionName = "watcher"
//go:generate mockgen -source=repository.go -destination=mocks/repository_mock.go
type Repository interface {
GetWatcher(ctx context.Context, filters bson.M) (*Watcher, error)
GetAllWatchers(ctx context.Context) ([]*Watcher, error)
GetWatcherList(ctx context.Context, filters bson.M, page int) ([]*Watcher, error)

CreateWatcher(ctx context.Context, watcher *Watcher) error
Expand Down Expand Up @@ -72,29 +71,34 @@ func (r *repository) GetWatcher(ctx context.Context, filters bson.M) (*Watcher,
return &watcher, nil
}

func (r *repository) GetAllWatchers(ctx context.Context) ([]*Watcher, error) {
page := 1
watchers := make([]*Watcher, 0)
for {
watchersPage, err := r.GetWatcherList(ctx, bson.M{}, page)
if err != nil {
r.logger.Errorf("unable to get GetAllWatchers watchers: %v page %v", err, page)
return nil, err
}
if len(watchersPage) == 0 {
break
}
func (r *repository) getAllWatchersWithoutHistory(ctx context.Context) ([]*Watcher, error) {
r.logger.Info("GetAllWatchersWithoutHistory is called")

collection := r.db.Database(r.dbName).Collection(r.watchersCollectionName)

cur, err := collection.Find(ctx, bson.M{})
if err != nil {
r.logger.Errorf("unable to find watchers due to internal error: %v", err)
return nil, err
}

defer cur.Close(ctx)

var watchers []*Watcher

watchers = append(watchers, watchersPage...)
page++
if err := cur.All(ctx, &watchers); err != nil {
r.logger.Errorf("unable to decode watchers: %v", err)
return nil, err
}

r.logger.Info("GetAllWatchersWithoutHistory got watchers")

return watchers, nil
}

func (r *repository) DeleteWatchersWithStaleData(ctx context.Context) error {
r.logger.Info("DeleteWatchersWithStaleData is called")
watchers, err := r.GetAllWatchers(ctx)
watchers, err := r.getAllWatchersWithoutHistory(ctx)
if err != nil {
r.logger.Errorf("unable to get all watchers: %v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions services/watcher/repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func (suite *WatcherTestSuite) TestDeleteWatcher() {
suite.Require().NoError(err)

// Read all watchers
watchers, err := suite.watcherRepository.GetAllWatchers(ctx)
watchers, err := suite.watcherRepository.GetWatcherList(ctx, bson.M{}, 1)
suite.Require().NoError(err)
suite.Require().NotNil(watchers)
// Verify that the watcher has been deleted find the watcher by ID
Expand Down Expand Up @@ -279,7 +279,7 @@ func (suite *WatcherTestSuite) TestWatcherWithAddressAndNotifications() {
}

func (suite *WatcherTestSuite) TestWatcherGetList() {
all, err := suite.watcherRepository.GetAllWatchers(context.Background())
all, err := suite.watcherRepository.GetWatcherList(context.Background(), bson.M{}, 1)
suite.Require().NoError(err)
suite.Require().NotNil(all)

Expand Down

0 comments on commit 6220c1d

Please sign in to comment.