github.com/mattermost/mattermost-server/v5@v5.39.3/migrations/migrations.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  	"net/http"
     8  
     9  	"github.com/mattermost/mattermost-server/v5/app"
    10  	tjobs "github.com/mattermost/mattermost-server/v5/jobs/interfaces"
    11  	"github.com/mattermost/mattermost-server/v5/model"
    12  	"github.com/mattermost/mattermost-server/v5/store"
    13  )
    14  
    15  const (
    16  	MigrationStateUnscheduled = "unscheduled"
    17  	MigrationStateInProgress  = "in_progress"
    18  	MigrationStateCompleted   = "completed"
    19  
    20  	JobDataKeyMigration           = "migration_key"
    21  	JobDataKeyMigration_LAST_DONE = "last_done"
    22  )
    23  
    24  type MigrationsJobInterfaceImpl struct {
    25  	srv *app.Server
    26  }
    27  
    28  func init() {
    29  	app.RegisterJobsMigrationsJobInterface(func(s *app.Server) tjobs.MigrationsJobInterface {
    30  		return &MigrationsJobInterfaceImpl{s}
    31  	})
    32  }
    33  
    34  func MakeMigrationsList() []string {
    35  	return []string{
    36  		model.MIGRATION_KEY_ADVANCED_PERMISSIONS_PHASE_2,
    37  	}
    38  }
    39  
    40  func GetMigrationState(migration string, store store.Store) (string, *model.Job, *model.AppError) {
    41  	if _, err := store.System().GetByName(migration); err == nil {
    42  		return MigrationStateCompleted, nil, nil
    43  	}
    44  
    45  	jobs, err := store.Job().GetAllByType(model.JOB_TYPE_MIGRATIONS)
    46  	if err != nil {
    47  		return "", nil, model.NewAppError("GetMigrationState", "app.job.get_all.app_error", nil, err.Error(), http.StatusInternalServerError)
    48  	}
    49  
    50  	for _, job := range jobs {
    51  		if key, ok := job.Data[JobDataKeyMigration]; ok {
    52  			if key != migration {
    53  				continue
    54  			}
    55  
    56  			switch job.Status {
    57  			case model.JOB_STATUS_IN_PROGRESS, model.JOB_STATUS_PENDING:
    58  				return MigrationStateInProgress, job, nil
    59  			default:
    60  				return MigrationStateUnscheduled, job, nil
    61  			}
    62  		}
    63  	}
    64  
    65  	return MigrationStateUnscheduled, nil, nil
    66  }