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