github.com/ashishbhate/mattermost-server@v5.11.1+incompatible/migrations/advanced_permissions_phase_2.go (about)

     1  // Copyright (c) 2018-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/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 len(p.LastChannelId) != 26 {
    35  		return false
    36  	}
    37  
    38  	if len(p.LastTeamId) != 26 {
    39  		return false
    40  	}
    41  
    42  	if len(p.LastUserId) != 26 {
    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 len(lastDone) == 0 {
    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  		if result := <-worker.app.Srv.Store.Team().MigrateTeamMembers(progress.LastTeamId, progress.LastUserId); result.Err != nil {
    75  			return false, progress.ToJson(), result.Err
    76  		} else {
    77  			if result.Data == nil {
    78  				// 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.
    79  				progress.LastUserId = strings.Repeat("0", 26)
    80  				progress.CurrentTable = "ChannelMembers"
    81  				return false, progress.ToJson(), nil
    82  			}
    83  
    84  			data := result.Data.(map[string]string)
    85  			progress.LastTeamId = data["TeamId"]
    86  			progress.LastUserId = data["UserId"]
    87  		}
    88  	} else if progress.CurrentTable == "ChannelMembers" {
    89  		// Run a ChannelMembers migration batch.
    90  		if result := <-worker.app.Srv.Store.Channel().MigrateChannelMembers(progress.LastChannelId, progress.LastUserId); result.Err != nil {
    91  			return false, progress.ToJson(), result.Err
    92  		} else {
    93  			if result.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  			data := result.Data.(map[string]string)
   100  			progress.LastChannelId = data["ChannelId"]
   101  			progress.LastUserId = data["UserId"]
   102  		}
   103  	}
   104  
   105  	return false, progress.ToJson(), nil
   106  }