github.com/vnforks/kid/v5@v5.22.1-0.20200408055009-b89d99c65676/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  	"github.com/vnforks/kid/v5/app"
     8  	tjobs "github.com/vnforks/kid/v5/jobs/interfaces"
     9  	"github.com/vnforks/kid/v5/model"
    10  	"github.com/vnforks/kid/v5/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 _, err := store.System().GetByName(migration); err == nil {
    40  		return MIGRATION_STATE_COMPLETED, nil, nil
    41  	}
    42  
    43  	jobs, err := store.Job().GetAllByType(model.JOB_TYPE_MIGRATIONS)
    44  	if err != nil {
    45  		return "", nil, err
    46  	}
    47  
    48  	for _, job := range jobs {
    49  		if key, ok := job.Data[JOB_DATA_KEY_MIGRATION]; ok {
    50  			if key != migration {
    51  				continue
    52  			}
    53  
    54  			switch job.Status {
    55  			case model.JOB_STATUS_IN_PROGRESS, model.JOB_STATUS_PENDING:
    56  				return MIGRATION_STATE_IN_PROGRESS, job, nil
    57  			default:
    58  				return MIGRATION_STATE_UNSCHEDULED, job, nil
    59  			}
    60  		}
    61  	}
    62  
    63  	return MIGRATION_STATE_UNSCHEDULED, nil, nil
    64  }