Skip to content

Instantly share code, notes, and snippets.

@jirevwe
Created September 13, 2022 16:00
Show Gist options
  • Save jirevwe/74f449ac0a5847a52f9edb63ad7ff9b2 to your computer and use it in GitHub Desktop.
Save jirevwe/74f449ac0a5847a52f9edb63ad7ff9b2 to your computer and use it in GitHub Desktop.
migrate from Convoy v0.6.0-rc.5 to 0.6.7
module github.com/frain-dev/migrate
go 1.19
require go.mongodb.org/mongo-driver v1.10.2
require (
github.com/golang/snappy v0.0.1 // indirect
github.com/klauspost/compress v1.13.6 // indirect
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
github.com/xdg-go/scram v1.1.1 // indirect
github.com/xdg-go/stringprep v1.0.3 // indirect
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d // indirect
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d // indirect
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect
golang.org/x/text v0.3.7 // indirect
)
package main
import (
"context"
"log"
"time"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
func main() {
dbDsn := "mongodb://localhost:27017"
dbName := "convoy"
type RTConfig struct {
Duration string `json:"duration"`
}
type Config struct {
RateLimit RTConfig `json:"ratelimit"`
}
type Group struct {
UID string `json:"uid" bson:"uid"`
Config Config `json:"config" bson:"config"`
DocumentStatus string `json:"-" bson:"document_status"`
}
opts := options.Client()
opts.ApplyURI(dbDsn)
client, err := mongo.Connect(context.Background(), opts)
if err != nil {
log.Fatal(err)
}
conn := client.Database(dbName, nil)
cursor, err := conn.Collection("groups").Find(context.Background(), bson.M{})
if err != nil {
log.Fatalf("find: %v", err)
}
groups := make([]Group, 0)
err = cursor.All(context.Background(), &groups)
if err != nil {
log.Fatal(err)
}
var newDuration uint64
for _, group := range groups {
duration, err := time.ParseDuration(group.Config.RateLimit.Duration)
if err != nil {
// Set default when an error occurs.
newDuration = 60
} else {
newDuration = uint64(duration.Seconds())
}
update := bson.M{"config.ratelimit.duration": newDuration}
_, err = conn.Collection("groups").UpdateOne(context.Background(), bson.M{"uid": group.UID}, bson.M{"$set": update}, nil)
if err != nil {
log.Fatal(err)
}
if err != nil {
log.Fatalf("Failed migration %v", err)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment