Example #1
0
func UpdateUsernameInBothDbs(currentUsername, newUsername string) error {
	err := modelhelper.UpdateUser(
		bson.M{"username": currentUsername},
		bson.M{"username": newUsername},
	)

	if err != nil {
		return err
	}

	err = modelhelper.UpdateAccount(
		modelhelper.Selector{"profile.nickname": currentUsername},
		modelhelper.Selector{"$set": modelhelper.Selector{"profile.nickname": newUsername}},
	)

	if err != nil {
		return err
	}

	acc := NewAccount()
	if err := acc.ByNick(currentUsername); err != nil {
		return err
	}

	acc.Nick = newUsername

	return acc.Update()
}
Example #2
0
func (mwc *Controller) handleAccountError(oldAccount *mongomodels.Account, err error) {
	errAccountCount++
	mwc.log.Error("Error occurred for account %s: %s", oldAccount.Id.Hex(), err)
	s := modelhelper.Selector{"_id": oldAccount.Id}
	o := modelhelper.Selector{"$set": modelhelper.Selector{"migration": MigrationFailed, "error": err.Error()}}
	if err := modelhelper.UpdateAccount(s, o); err != nil {
		mwc.log.Warning("Could not update account document: %s", err)
	}
}
Example #3
0
// makeSureAccount checks if incoming account is in postgres, if not creates it
// lazily and sets socialapi id of it in mongo
func makeSureAccount(groupName string, username string) (*models.Account, error) {
	// try to fetch account from postgres
	acc := models.NewAccount()
	err := acc.ByNick(username)
	if err == nil {
		return acc, nil
	}

	if err != bongo.RecordNotFound {
		return acc, err
	}

	// if account is not in postgres, try to create it

	// we need account first
	macc, err := modelhelper.GetAccount(username)
	if err != nil {
		return nil, err
	}

	acc = models.NewAccount() // account is nil, set it
	acc.OldId = macc.Id.Hex()
	acc.Nick = username
	if err := acc.Create(); err != nil {
		return nil, err
	}

	// set it to cache, in case we may need it
	if err := models.Cache.Account.SetToCache(acc); err != nil {
		return nil, err
	}

	// we just created the account in postgres, so update mongo with
	// socialApiId
	s := modelhelper.Selector{
		"profile.nickname": username,
	}
	o := modelhelper.Selector{"$set": modelhelper.Selector{
		"socialApiId": strconv.FormatInt(acc.Id, 10),
	}}
	if err := modelhelper.UpdateAccount(s, o); err != nil {
		return nil, err
	}

	return acc, nil
}
Example #4
0
func CreateAccountInBothDbsWithNick(nick string) (*Account, error) {
	accId := bson.NewObjectId()
	accHex := nick

	oldAcc, err := modelhelper.GetAccount(nick)
	if err == mgo.ErrNotFound {

		oldAcc = &kodingmodels.Account{
			Id: accId,
			Profile: struct {
				Nickname  string `bson:"nickname" json:"nickname"`
				FirstName string `bson:"firstName" json:"firstName"`
				LastName  string `bson:"lastName" json:"lastName"`
				Hash      string `bson:"hash" json:"hash"`
			}{
				Nickname: nick,
			},
		}

		err := modelhelper.CreateAccount(oldAcc)
		if err != nil {
			return nil, err
		}
	}

	oldUser, err := modelhelper.GetUser(nick)
	if err == mgo.ErrNotFound {
		oldUser = &kodingmodels.User{
			ObjectId:       bson.NewObjectId(),
			Password:       accHex,
			Salt:           accHex,
			Name:           nick,
			Email:          accHex + "@koding.com",
			Status:         "confirmed",
			EmailFrequency: &kodingmodels.EmailFrequency{},
		}

		err = modelhelper.CreateUser(oldUser)
		if err != nil {
			return nil, err
		}
	}

	a := NewAccount()
	a.Nick = nick
	a.OldId = accId.Hex()

	if err := a.ByNick(nick); err == bongo.RecordNotFound {
		if err := a.Create(); err != nil {
			return nil, err
		}
	}

	if oldAcc.SocialApiId != strconv.FormatInt(a.Id, 10) {
		s := modelhelper.Selector{"_id": oldAcc.Id}
		o := modelhelper.Selector{"$set": modelhelper.Selector{
			"socialApiId": strconv.FormatInt(a.Id, 10),
		}}

		if err := modelhelper.UpdateAccount(s, o); err != nil {
			return nil, err
		}
	}

	return a, nil
}
Example #5
0
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)
}