Skip to content

Commit

Permalink
Merge pull request #40 from ambrosus/database-migration
Browse files Browse the repository at this point in the history
Implement database migration script and repository update
  • Loading branch information
OlehParyshkura2 authored Apr 1, 2024
2 parents c75bdc7 + 39108ef commit 075b447
Show file tree
Hide file tree
Showing 4 changed files with 266 additions and 15 deletions.
10 changes: 9 additions & 1 deletion .github/workflows/testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ jobs:
- name: Checkout code
uses: actions/checkout@v3

- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.19

- name: Install dependencies
run: make deps

- name: Run linter
run: make lint

Expand All @@ -54,4 +62,4 @@ jobs:
export MONGO_DB_URL=http://127.0.0.1
export FIREBASE_CRED_PATH=./example.json
export ANDROID_CHANNEL_NAME=example
make test
make test
6 changes: 6 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"airdao-mobile-api/pkg/logger"
"airdao-mobile-api/pkg/mongodb"
"airdao-mobile-api/services/health"
"airdao-mobile-api/services/migration"
"airdao-mobile-api/services/watcher"
"context"
"errors"
Expand Down Expand Up @@ -57,6 +58,11 @@ func main() {
}
zapLogger.Info("DB connected successfully")

err = migration.RunMigrations(db, cfg.MongoDb.MongoDbName, zapLogger)
if err != nil {
zapLogger.Fatalf("failed to run migration: %s", err)
}

// Firebase
firebaseClient, err := firebase.NewClient(cfg.Firebase.CredPath)
if err != nil {
Expand Down
111 changes: 111 additions & 0 deletions services/migration/migration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package migration

import (
"airdao-mobile-api/services/watcher"
"errors"

"go.mongodb.org/mongo-driver/bson/primitive"
"go.uber.org/zap"
)

import (
"context"

"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
)

type HistoryNotificationDocument struct {
WatcherID primitive.ObjectID `json:"watcher_id" bson:"watcher_id"`
Notification *watcher.HistoryNotification `json:"notification" bson:"notification"`
}

type Migration struct {
ID primitive.ObjectID `json:"id" bson:"_id"`
Name string `json:"name" bson:"name"`
IsApplied bool `json:"is_applied" bson:"is_applied"`
}

func RunMigrations(db *mongo.Client, dbName string, logger *zap.SugaredLogger) error {
err := historicalNotificationMigration(db, dbName, logger)
// Add more migrations here
return err
}

func historicalNotificationMigration(db *mongo.Client, dbName string, logger *zap.SugaredLogger) error {
if db == nil {
return errors.New("[watcher_repository] invalid user database")
}
name := "historical_notification_migration"

migrationsCollection := db.Database(dbName).Collection("migrations")
watcherCollection := db.Database(dbName).Collection(watcher.CollectionName)
historyCollection := db.Database(dbName).Collection(watcher.HistoricalNotificationsCollectionName)

// Check if migration has already been run
var migration Migration
if err := migrationsCollection.FindOne(context.Background(), bson.M{"name": name}).Decode(&migration); err == nil {
logger.Info("Migration already applied.")
return nil
}

cursor, err := watcherCollection.Find(context.Background(), bson.M{})
if err != nil {
return err
}
logger.Info("Starting migration...", name)
defer cursor.Close(context.Background())

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

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

// Iterate over historical notifications only if it's not nil
for _, notification := range *watcher.HistoricalNotifications {
_, err := historyCollection.InsertOne(context.Background(), &HistoryNotificationDocument{
WatcherID: watcher.ID,
Notification: notification,
})
if err != nil {
return err
}
}

// Update watcher to set historical_notifications to nil
_, err := watcherCollection.UpdateOne(
context.Background(),
bson.M{"_id": watcher.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)
}

}

if err := cursor.Err(); err != nil {
return err
}

logger.Info("Migration completed successfully.")

// Mark migration as applied
_, err = migrationsCollection.InsertOne(context.Background(), &Migration{
Name: name,
IsApplied: true,
})
if err != nil {
return err
}
return nil
}
Loading

0 comments on commit 075b447

Please sign in to comment.