github.com/haalcala/mattermost-server-change-repo@v0.0.0-20210713015153-16753fbeee5f/migrations/migrations_test.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  	"testing"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  	"github.com/stretchr/testify/require"
    11  
    12  	"github.com/mattermost/mattermost-server/v5/model"
    13  )
    14  
    15  func TestGetMigrationState(t *testing.T) {
    16  	if testing.Short() {
    17  		t.SkipNow()
    18  	}
    19  	th := Setup()
    20  	defer th.TearDown()
    21  
    22  	migrationKey := model.NewId()
    23  
    24  	th.DeleteAllJobsByTypeAndMigrationKey(model.JOB_TYPE_MIGRATIONS, migrationKey)
    25  
    26  	// Test with no job yet.
    27  	state, job, err := GetMigrationState(migrationKey, th.App.Srv().Store)
    28  	assert.Nil(t, err)
    29  	assert.Nil(t, job)
    30  	assert.Equal(t, "unscheduled", state)
    31  
    32  	// Test with the system table showing the migration as done.
    33  	system := model.System{
    34  		Name:  migrationKey,
    35  		Value: "true",
    36  	}
    37  	nErr := th.App.Srv().Store.System().Save(&system)
    38  	assert.Nil(t, nErr)
    39  
    40  	state, job, err = GetMigrationState(migrationKey, th.App.Srv().Store)
    41  	assert.Nil(t, err)
    42  	assert.Nil(t, job)
    43  	assert.Equal(t, "completed", state)
    44  
    45  	_, nErr = th.App.Srv().Store.System().PermanentDeleteByName(migrationKey)
    46  	assert.Nil(t, nErr)
    47  
    48  	// Test with a job scheduled in "pending" state.
    49  	j1 := &model.Job{
    50  		Id:       model.NewId(),
    51  		CreateAt: model.GetMillis(),
    52  		Data: map[string]string{
    53  			JobDataKeyMigration: migrationKey,
    54  		},
    55  		Status: model.JOB_STATUS_PENDING,
    56  		Type:   model.JOB_TYPE_MIGRATIONS,
    57  	}
    58  
    59  	j1, nErr = th.App.Srv().Store.Job().Save(j1)
    60  	require.Nil(t, nErr)
    61  
    62  	state, job, err = GetMigrationState(migrationKey, th.App.Srv().Store)
    63  	assert.Nil(t, err)
    64  	assert.Equal(t, j1.Id, job.Id)
    65  	assert.Equal(t, "in_progress", state)
    66  
    67  	// Test with a job scheduled in "in progress" state.
    68  	j2 := &model.Job{
    69  		Id:       model.NewId(),
    70  		CreateAt: j1.CreateAt + 1,
    71  		Data: map[string]string{
    72  			JobDataKeyMigration: migrationKey,
    73  		},
    74  		Status: model.JOB_STATUS_IN_PROGRESS,
    75  		Type:   model.JOB_TYPE_MIGRATIONS,
    76  	}
    77  
    78  	j2, nErr = th.App.Srv().Store.Job().Save(j2)
    79  	require.Nil(t, nErr)
    80  
    81  	state, job, err = GetMigrationState(migrationKey, th.App.Srv().Store)
    82  	assert.Nil(t, err)
    83  	assert.Equal(t, j2.Id, job.Id)
    84  	assert.Equal(t, "in_progress", state)
    85  
    86  	// Test with a job scheduled in "error" state.
    87  	j3 := &model.Job{
    88  		Id:       model.NewId(),
    89  		CreateAt: j2.CreateAt + 1,
    90  		Data: map[string]string{
    91  			JobDataKeyMigration: migrationKey,
    92  		},
    93  		Status: model.JOB_STATUS_ERROR,
    94  		Type:   model.JOB_TYPE_MIGRATIONS,
    95  	}
    96  
    97  	j3, nErr = th.App.Srv().Store.Job().Save(j3)
    98  	require.Nil(t, nErr)
    99  
   100  	state, job, err = GetMigrationState(migrationKey, th.App.Srv().Store)
   101  	assert.Nil(t, err)
   102  	assert.Equal(t, j3.Id, job.Id)
   103  	assert.Equal(t, "unscheduled", state)
   104  }