[go: nahoru, domu]

Skip to content

Commit

Permalink
api: add migration to remove devices with no namespace related
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardoveiga authored and gustavosbarreto committed Jun 11, 2021
1 parent b67618e commit a4d6113
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
1 change: 1 addition & 0 deletions api/store/mongo/migrations/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func GenerateMigrations() []migrate.Migration {
migration_22,
migration_23,
migration_24,
migration_25,
}
}

Expand Down
54 changes: 54 additions & 0 deletions api/store/mongo/migrations/migration_25.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package migrations

import (
"context"

"github.com/sirupsen/logrus"
migrate "github.com/xakep666/mongo-migrate"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
)

var migration_25 = migrate.Migration{
Version: 25,
Description: "remove devices with no namespaces related",
Up: func(db *mongo.Database) error {
logrus.Info("Applying migration 25 - Up")
query := []bson.M{
{
"$lookup": bson.M{
"from": "namespaces",
"localField": "tenant_id",
"foreignField": "tenant_id",
"as": "namespace",
},
},
{
"$addFields": bson.M{
"namespace": bson.M{"$anyElementTrue": []interface{}{"$namespace"}},
},
},

{
"$match": bson.M{
"namespace": bson.M{"$eq": true},
},
},

{
"$unset": "namespace",
},

{
"$out": "devices",
},
}

_, err := db.Collection("devices").Aggregate(context.TODO(), query)
return err
},
Down: func(db *mongo.Database) error {
logrus.Info("Applying migration 25 - Down")
return nil
},
}

0 comments on commit a4d6113

Please sign in to comment.