func (mwc *Controller) createChannelParticipants(s modelhelper.Selector, channelId int64) error { migrateRelationship := func(relationship interface{}) error { r := relationship.(*mongomodels.Relationship) if r.MigrationStatus == "Completed" { return nil } // fetch follower id, err := mwc.AccountIdByOldId(r.TargetId.Hex()) if err != nil { mwc.log.Error("Participant account %s cannot be fetched: %s", r.TargetId.Hex(), err) return nil } cp := models.NewChannelParticipant() cp.ChannelId = channelId cp.AccountId = id cp.StatusConstant = models.ChannelParticipant_STATUS_ACTIVE cp.LastSeenAt = r.TimeStamp cp.UpdatedAt = r.TimeStamp cp.CreatedAt = r.TimeStamp if err := cp.CreateRaw(); err != nil { mwc.log.Error("Participant cannot be created: %s", err) return nil } r.MigrationStatus = "Completed" if err := modelhelper.UpdateRelationship(r); err != nil { mwc.log.Error("Participant relationship cannot be flagged as migrated: %s", err) } return nil } iterOptions := helpers.NewIterOptions() iterOptions.CollectionName = "relationships" iterOptions.F = migrateRelationship iterOptions.Filter = s iterOptions.Result = &mongomodels.Relationship{} iterOptions.Limit = 1000000000 iterOptions.Skip = 0 return helpers.Iter(modelhelper.Mongo, iterOptions) }
func (mwc *Controller) migrateAllAccountsToAlgolia() { mwc.log.Notice("Account migration to Algolia started") successCount := 0 s := modelhelper.Selector{ "type": "registered", } c := config.MustGet() algolia := algoliasearch.NewClient(c.Algolia.AppId, c.Algolia.ApiSecretKey) // create message handler handler := algoliaconnector.New(mwc.log, algolia, c.Algolia.IndexSuffix) migrateAccount := func(account interface{}) error { oldAccount := account.(*mongomodels.Account) return handler.AccountCreated(&models.Account{ OldId: oldAccount.Id.Hex(), Nick: oldAccount.Profile.Nickname, }) } iterOptions := helpers.NewIterOptions() iterOptions.CollectionName = "jAccounts" iterOptions.F = migrateAccount iterOptions.Filter = s iterOptions.Result = &mongomodels.Account{} iterOptions.Limit = 10000000 iterOptions.Skip = 0 iterOptions.Sort = []string{"-_id"} helpers.Iter(modelhelper.Mongo, iterOptions) mwc.log.Notice("Account migration completed for %d account with %d errors", successCount, errAccountCount) }
func main() { // init the package initialize() fmt.Println("Non existent deleter started") resources := []struct { CollName string Func func(interface{}) error Filter helper.Selector Res interface{} }{ { // Delete JUsers that does not have JAccount CollName: "jUsers", Func: deleteUser, Filter: filter(), Res: &models.User{}, }, { // Delete JAccounts that does not have JUser CollName: "jAccounts", Func: deleteAccount, Filter: filter(), Res: &models.Account{}, }, { // delete all groups that does not have any members JAccount CollName: "jGroups", Func: deleteGroups, Filter: filter(), Res: &models.Group{}, }, { // delete koding provided jMachines CollName: "jMachines", Func: deleteMachines, Filter: filter(), Res: &models.Machine{}, }, { // delete JWorkspaces that does not have JMachine. CollName: "jWorkspaces", Func: deleteWorkspaces, Filter: filter(), Res: &models.Workspace{}, }, { // delete jNames that have invalid collectionName. CollName: "jNames", Func: deleteNames, Filter: filter(), Res: &models.Name{}, }, { // delete jComputeStacks that does not have the account. CollName: "jComputeStacks", Func: deleteComputeStacks, Filter: filter(), Res: &models.ComputeStack{}, }, { // delete all jCombinedAppStorages which "accountId" does not exist in JAccount CollName: "jCombinedAppStorages", Func: deleteCombinedAppStorages, Filter: filter(), Res: &models.CombinedAppStorage{}, }, { // delete all rels that does not have source or target CollName: "relationships", Func: deleteRels, Filter: filter(), Res: &models.Relationship{}, }, } collsToClean := strings.Split(*flagColls, ",") fmt.Printf("will clean up following %q\n", collsToClean) for _, res := range resources { if !isIn(res.CollName, collsToClean...) { fmt.Printf("skipping %q it is not in op list\n", res.CollName) continue } iterOptions := helpers.NewIterOptions() iterOptions.CollectionName = res.CollName iterOptions.F = res.Func iterOptions.Filter = res.Filter iterOptions.Result = res.Res iterOptions.Limit = *flagLimit iterOptions.Skip = *flagSkip fmt.Printf("starting operating on %q\n", res.CollName) err := helpers.Iter(helper.Mongo, iterOptions) if err != nil { fmt.Printf("Error while iter %s\n", err) os.Exit(1) } fmt.Printf("finished with %s\n", res.CollName) } fmt.Println("worker done!") }
func (mwc *Controller) migrateAllGroups() { mwc.log.Notice("Group migration started") s := modelhelper.Selector{ "migration": modelhelper.Selector{"$exists": false}, } errCount := 0 successCount := 0 handleError := func(g *mongomodels.Group, err error) { mwc.log.Error("an error occurred for group %s: %s", g.Id.Hex(), err) errCount++ s := modelhelper.Selector{"slug": g.Slug} o := modelhelper.Selector{"$set": modelhelper.Selector{"migration": MigrationFailed, "error": err.Error()}} if err := modelhelper.UpdateGroupPartial(s, o); err != nil { mwc.log.Warning("Could not update group document: %s", err) } } migrateGroup := func(group interface{}) error { oldGroup := group.(*mongomodels.Group) if oldGroup.SocialApiChannelId != "" { s := modelhelper.Selector{"slug": oldGroup.Slug} o := modelhelper.Selector{"$set": modelhelper.Selector{"migration": MigrationCompleted}} modelhelper.UpdateGroupPartial(s, o) return nil } c, err := mwc.createGroupChannel(oldGroup.Slug) if err != nil { handleError(oldGroup, err) return nil } // if err := mwc.createGroupMembers(&group, c.Id); err != nil { // handleError(&group, err) // continue // return nil // } if err := completeGroupMigration(oldGroup, c.Id); err != nil { handleError(oldGroup, err) return nil } successCount++ return nil } iterOptions := helpers.NewIterOptions() iterOptions.CollectionName = "jGroups" iterOptions.F = migrateGroup iterOptions.Filter = s iterOptions.Result = &mongomodels.Group{} iterOptions.Limit = 10000000 iterOptions.Skip = 0 helpers.Iter(modelhelper.Mongo, iterOptions) helpers.Iter(modelhelper.Mongo, iterOptions) mwc.log.Notice("Group migration completed for %d groups with %d errors", successCount, errCount) }
func (mwc *Controller) migrateAllAccounts() { mwc.log.Notice("Account migration started") successCount := 0 s := modelhelper.Selector{ "type": "registered", } migrateAccount := func(account interface{}) error { oldAccount := account.(*mongomodels.Account) socialApiId, err := oldAccount.GetSocialApiId() if err != nil { mwc.handleAccountError(oldAccount, err) return nil } if socialApiId != 0 { return nil } s := modelhelper.Selector{"_id": oldAccount.Id} if socialApiId > 0 { o := modelhelper.Selector{"$set": modelhelper.Selector{ "migration": MigrationCompleted, }} modelhelper.UpdateAccount(s, o) successCount++ return nil } id, err := mwc.AccountIdByOldId( oldAccount.Id.Hex(), ) if err != nil { mwc.handleAccountError(oldAccount, err) return nil } o := modelhelper.Selector{"$set": modelhelper.Selector{ "socialApiId": strconv.FormatInt(id, 10), "migration": MigrationCompleted, }} if err := modelhelper.UpdateAccount(s, o); err != nil { mwc.handleAccountError(oldAccount, err) return nil } successCount++ return nil } iterOptions := helpers.NewIterOptions() iterOptions.CollectionName = "jAccounts" iterOptions.F = migrateAccount iterOptions.Filter = s iterOptions.Result = &mongomodels.Account{} iterOptions.Limit = 10000000 iterOptions.Skip = 0 helpers.Iter(modelhelper.Mongo, iterOptions) mwc.log.Notice("Account migration completed for %d account with %d errors", successCount, errAccountCount) }