github.com/lologarithm/mattermost-server@v5.3.2-0.20181002060438-c82a84ed765b+incompatible/migrations/migrations.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 "github.com/mattermost/mattermost-server/app" 8 tjobs "github.com/mattermost/mattermost-server/jobs/interfaces" 9 "github.com/mattermost/mattermost-server/model" 10 "github.com/mattermost/mattermost-server/store" 11 ) 12 13 const ( 14 MIGRATION_STATE_UNSCHEDULED = "unscheduled" 15 MIGRATION_STATE_IN_PROGRESS = "in_progress" 16 MIGRATION_STATE_COMPLETED = "completed" 17 18 JOB_DATA_KEY_MIGRATION = "migration_key" 19 JOB_DATA_KEY_MIGRATION_LAST_DONE = "last_done" 20 ) 21 22 type MigrationsJobInterfaceImpl struct { 23 App *app.App 24 } 25 26 func init() { 27 app.RegisterJobsMigrationsJobInterface(func(a *app.App) tjobs.MigrationsJobInterface { 28 return &MigrationsJobInterfaceImpl{a} 29 }) 30 } 31 32 func MakeMigrationsList() []string { 33 return []string{ 34 model.MIGRATION_KEY_ADVANCED_PERMISSIONS_PHASE_2, 35 } 36 } 37 38 func GetMigrationState(migration string, store store.Store) (string, *model.Job, *model.AppError) { 39 if result := <-store.System().GetByName(migration); result.Err == nil { 40 return MIGRATION_STATE_COMPLETED, nil, nil 41 } 42 43 if result := <-store.Job().GetAllByType(model.JOB_TYPE_MIGRATIONS); result.Err != nil { 44 return "", nil, result.Err 45 } else { 46 for _, job := range result.Data.([]*model.Job) { 47 if key, ok := job.Data[JOB_DATA_KEY_MIGRATION]; ok { 48 if key != migration { 49 continue 50 } 51 52 switch job.Status { 53 case model.JOB_STATUS_IN_PROGRESS, model.JOB_STATUS_PENDING: 54 return MIGRATION_STATE_IN_PROGRESS, job, nil 55 default: 56 return MIGRATION_STATE_UNSCHEDULED, job, nil 57 } 58 } 59 } 60 } 61 62 return MIGRATION_STATE_UNSCHEDULED, nil, nil 63 }