/
helpers.go
43 lines (37 loc) · 1.21 KB
/
helpers.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
package db
import (
"context"
"fmt"
"log"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
)
// RecordPreImages enables pre/post images for a collection for MongoDB < 6
func RecordPreImages(mongoInstance *mongo.Database, colName string) error {
cmd := bson.D{
{Key: "collMod", Value: colName},
{Key: "recordPreImages", Value: true},
}
result := bson.M{}
err := mongoInstance.RunCommand(context.Background(), cmd).Decode(&result)
if err != nil {
return fmt.Errorf("failed to enable recording of pre and post images: %w", err)
}
log.Printf("recording of pre and post images enabled: %+v", result)
return nil
}
// EnablePrePostImages enables pre/post images for a collection for MongoDB >= 6
func EnablePrePostImages(mongoInstance *mongo.Database, colName string) error {
cmd := bson.D{
{Key: "collMod", Value: colName},
{Key: "changeStreamPreAndPostImages",
Value: bson.D{{Key: "enabled", Value: true}}},
}
result := bson.M{}
err := mongoInstance.RunCommand(context.Background(), cmd).Decode(&result)
if err != nil {
return fmt.Errorf("failed to enable change stream pre and post images: %w", err)
}
log.Printf("change stream pre and post images enabled: %+v", result)
return nil
}