Skip to content

Commit

Permalink
Merge pull request #2103 from carolynvs/improve-migration-error
Browse files Browse the repository at this point in the history
Include the schema versions in migration errors
  • Loading branch information
carolynvs authored May 26, 2022
2 parents 084f212 + ef7cc53 commit df3b28c
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 16 deletions.
15 changes: 12 additions & 3 deletions pkg/storage/migrations/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"path/filepath"
"time"

"get.porter.sh/porter/pkg"
"get.porter.sh/porter/pkg/config"
"get.porter.sh/porter/pkg/storage"
"get.porter.sh/porter/pkg/tracing"
Expand Down Expand Up @@ -73,14 +74,22 @@ func (m *Manager) Connect(ctx context.Context) error {

if !m.allowOutOfDateSchema && m.MigrationRequired() {
m.Close()
return span.Error(errors.New(`The schema of Porter's data is in an older format than supported by this version of Porter.
return span.Error(errors.Errorf(`The schema of Porter's data is in an older format than supported by this version of Porter.
Porter %s uses the following database schema:
%#v
Your database schema is:
%#v
Refer to https://porter.sh/storage-migrate for more information and instructions to back up your data.
Once your data has been backed up, run the following command to perform the migration:
porter storage migrate
`))
`, pkg.Version, storage.NewSchema(), m.schema))
}

m.initialized = true

cs := storage.NewInstallationStore(m.store)
Expand Down
62 changes: 49 additions & 13 deletions pkg/storage/migrations/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func TestManager_NoMigrationEmptyHome(t *testing.T) {
require.NoError(t, err, "List credentials failed")
}

func TestClaimStorage_HaltOnMigrationRequired(t *testing.T) {
func TestInstallationStorage_HaltOnMigrationRequired(t *testing.T) {
t.Parallel()

tc := config.NewTestConfig(t)
Expand All @@ -86,16 +86,28 @@ func TestClaimStorage_HaltOnMigrationRequired(t *testing.T) {
err := mgr.store.Update(context.Background(), CollectionConfig, storage.UpdateOptions{Document: schema, Upsert: true})
require.NoError(t, err, "Save schema failed")

checkMigrationError := func(t *testing.T, err error) {
require.Error(t, err, "Operation should halt because a migration is required")
assert.Contains(t, err.Error(), "The schema of Porter's data is in an older format than supported by this version of Porter", "The error should be a migration error")

wantVersionComp := `Porter uses the following database schema:
storage.Schema{ID:"schema", Installations:"1.0.1", Credentials:"1.0.1", Parameters:"1.0.1"}
Your database schema is:
storage.Schema{ID:"schema", Installations:"needs-migration", Credentials:"1.0.1", Parameters:"1.0.1"}`
assert.Contains(t, err.Error(), wantVersionComp, "the migration error should contain the current and expected db schema")
}

t.Run("list", func(t *testing.T) {
_, err = claimStore.ListInstallations(context.Background(), "", "", nil)
require.Error(t, err, "Operation should halt because a migration is required")
assert.Contains(t, err.Error(), "The schema of Porter's data is in an older format than supported by this version of Porter")
checkMigrationError(t, err)
})

t.Run("read", func(t *testing.T) {
_, err = claimStore.GetInstallation(context.Background(), "", "mybun")
require.Error(t, err, "Operation should halt because a migration is required")
assert.Contains(t, err.Error(), "The schema of Porter's data is in an older format than supported by this version of Porter")
checkMigrationError(t, err)
})

}
Expand Down Expand Up @@ -129,16 +141,28 @@ func TestCredentialStorage_HaltOnMigrationRequired(t *testing.T) {
err := mgr.store.Update(context.Background(), CollectionConfig, storage.UpdateOptions{Document: schema, Upsert: true})
require.NoError(t, err, "Save schema failed")

checkMigrationError := func(t *testing.T, err error) {
require.Error(t, err, "Operation should halt because a migration is required")
assert.Contains(t, err.Error(), "The schema of Porter's data is in an older format than supported by this version of Porter", "The error should be a migration error")

wantVersionComp := `Porter uses the following database schema:
storage.Schema{ID:"schema", Installations:"1.0.1", Credentials:"1.0.1", Parameters:"1.0.1"}
Your database schema is:
storage.Schema{ID:"schema", Installations:"1.0.1", Credentials:"needs-migration", Parameters:"1.0.1"}`
assert.Contains(t, err.Error(), wantVersionComp, "the migration error should contain the current and expected db schema")
}

t.Run("list", func(t *testing.T) {
_, err = credStore.ListCredentialSets(context.Background(), "", "", nil)
require.Error(t, err, "Operation should halt because a migration is required")
assert.Contains(t, err.Error(), "The schema of Porter's data is in an older format than supported by this version of Porter")
checkMigrationError(t, err)
})

t.Run("read", func(t *testing.T) {
_, err = credStore.GetCredentialSet(context.Background(), "", "mybun")
require.Error(t, err, "Operation should halt because a migration is required")
assert.Contains(t, err.Error(), "The schema of Porter's data is in an older format than supported by this version of Porter")
checkMigrationError(t, err)
})
}

Expand Down Expand Up @@ -170,16 +194,28 @@ func TestParameterStorage_HaltOnMigrationRequired(t *testing.T) {
err := mgr.store.Update(context.Background(), CollectionConfig, storage.UpdateOptions{Document: schema, Upsert: true})
require.NoError(t, err, "Save schema failed")

checkMigrationError := func(t *testing.T, err error) {
require.Error(t, err, "Operation should halt because a migration is required")
assert.Contains(t, err.Error(), "The schema of Porter's data is in an older format than supported by this version of Porter", "The error should be a migration error")

wantVersionComp := `Porter uses the following database schema:
storage.Schema{ID:"schema", Installations:"1.0.1", Credentials:"1.0.1", Parameters:"1.0.1"}
Your database schema is:
storage.Schema{ID:"schema", Installations:"1.0.1", Credentials:"1.0.1", Parameters:"needs-migration"}`
assert.Contains(t, err.Error(), wantVersionComp, "the migration error should contain the current and expected db schema")
}

t.Run("list", func(t *testing.T) {
_, err = paramStore.ListParameterSets(context.Background(), "", "", nil)
require.Error(t, err, "Operation should halt because a migration is required")
assert.Contains(t, err.Error(), "The schema of Porter's data is in an older format than supported by this version of Porter")
checkMigrationError(t, err)
})

t.Run("read", func(t *testing.T) {
_, err = paramStore.GetParameterSet(context.Background(), "", "mybun")
require.Error(t, err, "Operation should halt because a migration is required")
assert.Contains(t, err.Error(), "The schema of Porter's data is in an older format than supported by this version of Porter")
checkMigrationError(t, err)
})
}

Expand Down

0 comments on commit df3b28c

Please sign in to comment.