Skip to content

Commit

Permalink
Fix #441 migrate thirdPartySharing string values to boolean
Browse files Browse the repository at this point in the history
  • Loading branch information
albinpa authored and georgepadayatti committed Nov 2, 2023
1 parent ccdc133 commit 12b848b
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
5 changes: 5 additions & 0 deletions internal/cmd/start_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
v2HttpPaths "github.com/bb-consent/api/internal/http_path/v2"
"github.com/bb-consent/api/internal/iam"
"github.com/bb-consent/api/internal/middleware"
"github.com/bb-consent/api/internal/migrate"
privacyDashboard "github.com/bb-consent/api/internal/privacy_dashboard"
"github.com/bb-consent/api/internal/rbac"
"github.com/bb-consent/api/internal/sms"
Expand Down Expand Up @@ -95,6 +96,10 @@ func StartApiCmdHandler(cmd *cobra.Command, args []string) {
tenant.SingleTenantConfiguration(loadedConfig)
}

// Applying migration
log.Println("Applying migrate")
migrate.Migrate()

// Router
router := mux.NewRouter()
v2HttpPaths.SetRoutes(router, authEnforcer)
Expand Down
68 changes: 68 additions & 0 deletions internal/migrate/migrate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package migrate

import (
"context"
"fmt"

"github.com/bb-consent/api/internal/dataagreement"
"github.com/bb-consent/api/internal/policy"
"go.mongodb.org/mongo-driver/bson"
)

func Migrate() {
migrateThirdPartyDataSharingToTrueInPolicyCollection()
migrateThirdPartyDataSharingToTrueInDataAgreementsCollection()
}

func migrateThirdPartyDataSharingToTrueInPolicyCollection() {
policyCollection := policy.Collection()

filter := bson.M{"thirdpartydatasharing": ""}
update := bson.M{"$set": bson.M{"thirdpartydatasharing": true}}
_, err := policyCollection.UpdateMany(context.TODO(), filter, update)
if err != nil {
fmt.Println(err)
}
filter = bson.M{"thirdpartydatasharing": "true"}
update = bson.M{"$set": bson.M{"thirdpartydatasharing": true}}
_, err = policyCollection.UpdateMany(context.TODO(), filter, update)
if err != nil {
fmt.Println(err)
}
filter = bson.M{"thirdpartydatasharing": "false"}
update = bson.M{"$set": bson.M{"thirdpartydatasharing": true}}
_, err = policyCollection.UpdateMany(context.TODO(), filter, update)
if err != nil {
fmt.Println(err)
}
}

func migrateThirdPartyDataSharingToTrueInDataAgreementsCollection() {
dataAgreementCollection := dataagreement.Collection()

filter := bson.M{"policy.thirdpartydatasharing": ""}
update := bson.M{"$set": bson.M{"policy.thirdpartydatasharing": true}}

_, err := dataAgreementCollection.UpdateMany(context.TODO(), filter, update)
if err != nil {
fmt.Println(err)
}

filter = bson.M{"policy.thirdpartydatasharing": "true"}
update = bson.M{"$set": bson.M{"policy.thirdpartydatasharing": true}}

_, err = dataAgreementCollection.UpdateMany(context.TODO(), filter, update)
if err != nil {
fmt.Println(err)
}

filter = bson.M{"policy.thirdpartydatasharing": "false"}
update = bson.M{"$set": bson.M{"policy.thirdpartydatasharing": true}}

_, err = dataAgreementCollection.UpdateMany(context.TODO(), filter, update)
if err != nil {
fmt.Println(err)
}
// TODO: Handle impact towards revisions

}

0 comments on commit 12b848b

Please sign in to comment.