Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement database migration script and repository update #40

Merged
merged 5 commits into from
Apr 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading