github.com/haalcala/mattermost-server-change-repo@v0.0.0-20210713015153-16753fbeee5f/migrations/advanced_permissions_phase_2.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See LICENSE.txt for license information.
     3  
     4  package migrations
     5  
     6  import (
     7  	"encoding/json"
     8  	"io"
     9  	"net/http"
    10  	"strings"
    11  
    12  	"github.com/mattermost/mattermost-server/v5/model"
    13  )
    14  
    15  type AdvancedPermissionsPhase2Progress struct {
    16  	CurrentTable  string `json:"current_table"`
    17  	LastTeamId    string `json:"last_team_id"`
    18  	LastChannelId string `json:"last_channel_id"`
    19  	LastUserId    string `json:"last_user"`
    20  }
    21  
    22  func (p *AdvancedPermissionsPhase2Progress) ToJson() string {
    23  	b, _ := json.Marshal(p)
    24  	return string(b)
    25  }
    26  
    27  func AdvancedPermissionsPhase2ProgressFromJson(data io.Reader) *AdvancedPermissionsPhase2Progress {
    28  	var o *AdvancedPermissionsPhase2Progress
    29  	json.NewDecoder(data).Decode(&o)
    30  	return o
    31  }
    32  
    33  func (p *AdvancedPermissionsPhase2Progress) IsValid() bool {
    34  	if !model.IsValidId(p.LastChannelId) {
    35  		return false
    36  	}
    37  
    38  	if !model.IsValidId(p.LastTeamId) {
    39  		return false
    40  	}
    41  
    42  	if !model.IsValidId(p.LastUserId) {
    43  		return false
    44  	}
    45  
    46  	switch p.CurrentTable {
    47  	case "TeamMembers":
    48  	case "ChannelMembers":
    49  	default:
    50  		return false
    51  	}
    52  
    53  	return true
    54  }
    55  
    56  func (worker *Worker) runAdvancedPermissionsPhase2Migration(lastDone string) (bool, string, *model.AppError) {
    57  	var progress *AdvancedPermissionsPhase2Progress
    58  	if lastDone == "" {
    59  		// Haven't started the migration yet.
    60  		progress = new(AdvancedPermissionsPhase2Progress)
    61  		progress.CurrentTable = "TeamMembers"
    62  		progress.LastChannelId = strings.Repeat("0", 26)
    63  		progress.LastTeamId = strings.Repeat("0", 26)
    64  		progress.LastUserId = strings.Repeat("0", 26)
    65  	} else {
    66  		progress = AdvancedPermissionsPhase2ProgressFromJson(strings.NewReader(lastDone))
    67  		if !progress.IsValid() {
    68  			return false, "", model.NewAppError("MigrationsWorker.runAdvancedPermissionsPhase2Migration", "migrations.worker.run_advanced_permissions_phase_2_migration.invalid_progress", map[string]interface{}{"progress": progress.ToJson()}, "", http.StatusInternalServerError)
    69  		}
    70  	}
    71  
    72  	if progress.CurrentTable == "TeamMembers" {
    73  		// Run a TeamMembers migration batch.
    74  		result, err := worker.srv.Store.Team().MigrateTeamMembers(progress.LastTeamId, progress.LastUserId)
    75  		if err != nil {
    76  			return false, progress.ToJson(), model.NewAppError("MigrationsWorker.runAdvancedPermissionsPhase2Migration", "app.team.migrate_team_members.update.app_error", nil, err.Error(), http.StatusInternalServerError)
    77  		}
    78  		if result == nil {
    79  			// We haven't progressed. That means that we've reached the end of this stage of the migration, and should now advance to the next stage.
    80  			progress.LastUserId = strings.Repeat("0", 26)
    81  			progress.CurrentTable = "ChannelMembers"
    82  			return false, progress.ToJson(), nil
    83  		}
    84  
    85  		progress.LastTeamId = result["TeamId"]
    86  		progress.LastUserId = result["UserId"]
    87  	} else if progress.CurrentTable == "ChannelMembers" {
    88  		// Run a ChannelMembers migration batch.
    89  		data, err := worker.srv.Store.Channel().MigrateChannelMembers(progress.LastChannelId, progress.LastUserId)
    90  		if err != nil {
    91  			return false, progress.ToJson(), model.NewAppError("MigrationsWorker.runAdvancedPermissionsPhase2Migration", "app.channel.migrate_channel_members.select.app_error", nil, err.Error(), http.StatusInternalServerError)
    92  		}
    93  		if data == nil {
    94  			// We haven't progressed. That means we've reached the end of this final stage of the migration.
    95  
    96  			return true, progress.ToJson(), nil
    97  		}
    98  
    99  		progress.LastChannelId = data["ChannelId"]
   100  		progress.LastUserId = data["UserId"]
   101  	}
   102  
   103  	return false, progress.ToJson(), nil
   104  }