-
Notifications
You must be signed in to change notification settings - Fork 1
/
init.go
63 lines (57 loc) · 1.95 KB
/
init.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package dbCollections
import (
"context"
"log"
"github.com/pixlise/core/v4/core/logger"
"github.com/pixlise/core/v4/core/utils"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
)
func InitCollections(db *mongo.Database, iLog logger.ILogger, environment string) {
// Ensure collections exist, required because some collections are "first" written to in a transaction which fails
// if the collection doesn't already exist
collectionsRequired := []string{
QuantificationsName,
OwnershipName,
ElementSetsName,
ExpressionGroupsName,
ExpressionsName,
ModulesName,
ModuleVersionsName,
RegionsOfInterestName,
ScreenConfigurationName,
UserROIDisplaySettings,
UserExpressionDisplaySettings,
WidgetDataName,
}
ctx := context.TODO()
existingCollections, err := db.ListCollectionNames(ctx, bson.D{})
if err != nil {
log.Fatal(err)
}
for _, collName := range collectionsRequired {
if !utils.ItemInSlice(collName, existingCollections) {
// Doesn't exist, create it
iLog.Infof("Mongo collection %v doesn't exist, pre-creating it...", collName)
err = db.CreateCollection(ctx, collName)
if err != nil {
log.Fatal(err)
}
}
}
// We want to be able to watch change streams on some of our collections... DocumentDB seems to require this to be enabled
// separately, so do that here
// if environment != "unittest" && environment != "prodMigrated" {
// Tried with an env, got AWS DocumentDB error: The modifyChangeStreams command can only be run against the admin database
// Had to run this manually on DB with mongosh (connected to the DocumentDB cluster): db.adminCommand({modifyChangeStreams: 1, database: "pixlise-feature-v4", collection: "jobStatuses", enable: true})
// result := db.RunCommand(ctx, bson.D{
// {"modifyChangeStreams", 1},
// {"database", db.Name()},
// {"collection", JobStatusName},
// {"enable", true},
// })
// if result != nil {
// log.Fatal(result.Err())
// }
// }
}