-
-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
api: add migration to remove devices with no namespace related
- Loading branch information
1 parent
b67618e
commit a4d6113
Showing
2 changed files
with
55 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}, | ||
} |