Skip to content

Commit

Permalink
MM-59466 remove unused migrations (#711)
Browse files Browse the repository at this point in the history
* rm unused migrations

* archive synthetic users

* Revert "archive synthetic users"

This reverts commit 4f18c98.
  • Loading branch information
lieut-data authored Jul 18, 2024
1 parent 4585e70 commit 838e99d
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 307 deletions.
174 changes: 4 additions & 170 deletions server/store/sqlstore/data_migrations.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package sqlstore

import (
"database/sql"
"fmt"
"strconv"
"time"
Expand All @@ -10,177 +9,12 @@ import (
)

const (
RemoteIDMigrationKey = "RemoteIDMigrationComplete"
SetEmailVerifiedToTrueForRemoteUsersMigrationKey = "SetEmailVerifiedToTrueForRemoteUsersMigrationComplete"
MSTeamUserIDDedupMigrationKey = "MSTeamUserIDDedupMigrationComplete"
WhitelistedUsersMigrationKey = "WhitelistedUsersMigrationComplete"

DedupScoreDefault byte = 0
DedupScoreNotSynthetic byte = 1
legacyRemoteIDMigrationKey = "RemoteIDMigrationComplete"
legacySetEmailVerifiedToTrueForRemoteUsersMigrationKey = "SetEmailVerifiedToTrueForRemoteUsersMigrationComplete"
legacyMSTeamUserIDDedupMigrationKey = "MSTeamUserIDDedupMigrationComplete"
WhitelistedUsersMigrationKey = "WhitelistedUsersMigrationComplete"
)

func (s *SQLStore) runMigrationRemoteID(remoteID string) error {
setting, err := s.getSystemSetting(s.db, RemoteIDMigrationKey)
if err != nil {
return fmt.Errorf("cannot get Remote ID migration state: %w", err)
}

if hasAlreadyRun, _ := strconv.ParseBool(setting); hasAlreadyRun {
return nil
}

s.api.LogDebug("Running Remote ID migration")
start := time.Now()

_, qErr := s.getQueryBuilder(s.db).Update("Users").Set("RemoteID", remoteID).Where(sq.And{
sq.NotEq{"RemoteID": nil},
sq.NotEq{"RemoteID": ""},
sq.Expr("RemoteID NOT IN (SELECT remoteid FROM remoteclusters)"),
sq.Like{"Username": "msteams_%"},
}).Exec()

if qErr != nil {
return qErr
}

if err := s.setSystemSetting(s.db, RemoteIDMigrationKey, strconv.FormatBool(true)); err != nil {
return fmt.Errorf("cannot mark Remote ID migration as completed: %w", err)
}

s.api.LogDebug("Remote ID migration run successfully", "elapsed", time.Since(start))

return nil
}

func (s *SQLStore) runSetEmailVerifiedToTrueForRemoteUsers(remoteID string) error {
setting, err := s.getSystemSetting(s.db, SetEmailVerifiedToTrueForRemoteUsersMigrationKey)
if err != nil {
return fmt.Errorf("cannot get Set Email Verified to True for Remote Users migration state: %w", err)
}

if hasAlreadyRun, _ := strconv.ParseBool(setting); hasAlreadyRun {
return nil
}

s.api.LogDebug("Running Set Email Verified to True for Remote Users migration")
start := time.Now()

_, qErr := s.getQueryBuilder(s.db).
Update("Users").
Set("EmailVerified", true).
Where(sq.And{
sq.Eq{"RemoteID": remoteID},
sq.Eq{"EmailVerified": false},
}).Exec()

if qErr != nil {
return qErr
}

if err := s.setSystemSetting(s.db, SetEmailVerifiedToTrueForRemoteUsersMigrationKey, strconv.FormatBool(true)); err != nil {
return fmt.Errorf("cannot mark Set Email Verified to True for Remote Users migration as completed: %w", err)
}

s.api.LogDebug("Set Email Verified to True for Remote Users migration run successfully", "elapsed", time.Since(start))

return nil
}

func (s *SQLStore) runMSTeamUserIDDedup() error {
setting, err := s.getSystemSetting(s.db, MSTeamUserIDDedupMigrationKey)
if err != nil {
return fmt.Errorf("cannot get MSTeam User ID Dedup migration state: %w", err)
}

if hasAlreadyRun, _ := strconv.ParseBool(setting); hasAlreadyRun {
return nil
}

s.api.LogDebug("Running MSTeam User ID Dedup migration")

// get all users with duplicate msteamsuserid
rows, err := s.getQueryBuilder(s.replica).Select(
"mmuserid",
"msteamsuserid",
"remoteid",
).
From(usersTableName).
Where(sq.Expr("msteamsuserid IN ( SELECT msteamsuserid FROM " + usersTableName + " GROUP BY msteamsuserid HAVING COUNT(*) > 1)")).
LeftJoin("users ON " + usersTableName + ".mmuserid = users.id").
OrderBy("users.createat ASC").
Query()
if err != nil {
return err
}

// find the best candidate to keep based on:
// 1. real user > synthetic user
// 2. keep the oldest user
bestCandidate := map[string]string{}
bestCandidateScore := map[string]byte{}
for rows.Next() {
var mmUserID, teamsUserID, remoteID string
var nRemoteID sql.NullString

err = rows.Scan(&mmUserID, &teamsUserID, &nRemoteID)
if err != nil {
return err
}

remoteID = ""
if nRemoteID.Valid {
remoteID = nRemoteID.String
}

currentUserScore := DedupScoreDefault
if remoteID == "" {
currentUserScore = DedupScoreNotSynthetic
}

_, ok := bestCandidate[teamsUserID]
if !ok {
bestCandidate[teamsUserID] = mmUserID
bestCandidateScore[teamsUserID] = currentUserScore
continue
}

if ok && currentUserScore > bestCandidateScore[teamsUserID] {
bestCandidate[teamsUserID] = mmUserID
bestCandidateScore[teamsUserID] = currentUserScore
continue
}
}

if len(bestCandidate) == 0 {
return nil
}

// for each msteams id, we're deleting all the duplicates except the best candidate
orCond := sq.Or{}
for teamsUserID, mmUserID := range bestCandidate {
orCond = append(orCond, sq.And{
sq.Eq{"msteamsuserid": teamsUserID},
sq.NotEq{"mmuserid": mmUserID},
})
}

s.api.LogInfo("Deleting duplicates")
_, err = s.getQueryBuilder(s.db).Delete(usersTableName).
Where(orCond).
Exec()
if err != nil {
return err
}

if err := s.setSystemSetting(s.db, MSTeamUserIDDedupMigrationKey, strconv.FormatBool(true)); err != nil {
return fmt.Errorf("cannot mark MSTeam User ID Dedup migration as completed: %w", err)
}

s.api.LogDebug("MSTeam User ID Dedup migration run successfully")

return nil
}

func (s *SQLStore) runWhitelistedUsersMigration() error {
setting, err := s.getSystemSetting(s.db, WhitelistedUsersMigrationKey)
if err != nil {
Expand Down
14 changes: 0 additions & 14 deletions server/store/sqlstore/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,20 +73,6 @@ func (s *SQLStore) runMigrationSequence(engine *morph.Morph, driver drivers.Driv
return mErr
}

if remoteID != "" {
if mErr := s.runMigrationRemoteID(remoteID); mErr != nil {
return fmt.Errorf("error running remote ID migration: %w", mErr)
}

if mErr := s.runSetEmailVerifiedToTrueForRemoteUsers(remoteID); mErr != nil {
return fmt.Errorf("error running set email verified to true for remote users migration: %w", mErr)
}
}

if err := s.runMSTeamUserIDDedup(); err != nil {
return err
}

if mErr := s.ensureMigrationsAppliedUpToVersion(engine, driver, migrationWhitelistedUsersRequiredVersion); mErr != nil {
return mErr
}
Expand Down
111 changes: 0 additions & 111 deletions server/store/sqlstore/migrations_test.go

This file was deleted.

12 changes: 0 additions & 12 deletions server/store/sqlstore/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,3 @@ func tableExist(store *SQLStore, tableName string) (bool, error) {
defer rows.Close()
return rows.Next(), nil
}

func createUniqueIndex(store *SQLStore, tableName, indexName, columnList string) error {
if _, err := store.db.Exec(fmt.Sprintf("CREATE UNIQUE INDEX IF NOT EXISTS %s ON %s (%s)", indexName, tableName, columnList)); err != nil {
return err
}

return nil
}

func createMSTeamsUserIDUniqueIndex(store *SQLStore) error {
return createUniqueIndex(store, usersTableName, "idx_msteamssync_users_msteamsuserid_unq", "msteamsuserid")
}

0 comments on commit 838e99d

Please sign in to comment.