forked from requilence/integram
-
Notifications
You must be signed in to change notification settings - Fork 0
/
migrations.go
44 lines (35 loc) · 1.04 KB
/
migrations.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package integram
import (
"time"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)
func migrations(db *mgo.Database, serviceName string) error {
err := migrateMissingOAuthStores(db, serviceName)
if err != nil {
return err
}
return nil
}
func migrateMissingOAuthStores(db *mgo.Database, serviceName string) error {
name := "MissingOAuthStores"
n, _ := db.C("migrations").FindId(serviceName + "_" + name).Count()
if n > 0 {
return nil
}
info, err := db.C("users").UpdateAll(bson.M{
"protected." + serviceName + ".oauthtoken": bson.M{"$exists": true, "$ne": ""},
"$or": []bson.M{
{"protected." + serviceName + ".oauthstore": bson.M{"$exists": false}},
{"protected." + serviceName + ".oauthstore": ""},
},
},
bson.M{"$set": bson.M{
"protected." + serviceName + ".oauthstore": "default",
"protected." + serviceName + ".oauthvalid": true,
}})
if err != nil && err != mgo.ErrNotFound {
return err
}
return db.C("migrations").Insert(bson.M{"_id": serviceName + "_" + name, "date": time.Now(), "migrated": info.Updated})
}