github.com/vnforks/kid/v5@v5.22.1-0.20200408055009-b89d99c65676/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/vnforks/kid/v5/model"
    13  )
    14  
    15  type AdvancedPermissionsPhase2Progress struct {
    16  	CurrentTable  string `json:"current_table"`
    17  	LastBranchId    string `json:"last_branch_id"`
    18  	LastClassId string `json:"last_class_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.LastClassId) != 26 {
    35  		return false
    36  	}
    37  
    38  	if len(p.LastBranchId) != 26 {
    39  		return false
    40  	}
    41  
    42  	if len(p.LastUserId) != 26 {
    43  		return false
    44  	}
    45  
    46  	switch p.CurrentTable {
    47  	case "BranchMembers":
    48  	case "ClassMembers":
    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 = "BranchMembers"
    62  		progress.LastClassId = strings.Repeat("0", 26)
    63  		progress.LastBranchId = 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 == "BranchMembers" {
    73  		// Run a BranchMembers migration batch.
    74  		if result, err := worker.app.Srv().Store.Branch().MigrateBranchMembers(progress.LastBranchId, progress.LastUserId); err != nil {
    75  			return false, progress.ToJson(), err
    76  		} else {
    77  			if result == 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 = "ClassMembers"
    81  				return false, progress.ToJson(), nil
    82  			}
    83  
    84  			progress.LastBranchId = result["BranchId"]
    85  			progress.LastUserId = result["UserId"]
    86  		}
    87  	} else if progress.CurrentTable == "ClassMembers" {
    88  		// Run a ClassMembers migration batch.
    89  		if data, err := worker.app.Srv().Store.Class().MigrateClassMembers(progress.LastClassId, progress.LastUserId); err != nil {
    90  			return false, progress.ToJson(), err
    91  		} else {
    92  			if data == nil {
    93  				// We haven't progressed. That means we've reached the end of this final stage of the migration.
    94  
    95  				return true, progress.ToJson(), nil
    96  			}
    97  
    98  			progress.LastClassId = data["ClassId"]
    99  			progress.LastUserId = data["UserId"]
   100  		}
   101  	}
   102  
   103  	return false, progress.ToJson(), nil
   104  }