Skip to content

Commit

Permalink
Support schema version range
Browse files Browse the repository at this point in the history
For some reason the latest JW Library version
moved the schema version back to 13.
In the hope that in a next version schema 14
still doesn't contain changes, we are now
allowing a range of supported schema versions.
This means a backup can be of version 13
AND 14 and still be considered compatible.
  • Loading branch information
AndreasSko committed Oct 22, 2023
1 parent 2c233cc commit 965c26a
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 26 deletions.
17 changes: 9 additions & 8 deletions model/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import (
)

const version = 1
const schemaVersion = 14
const supportedSchemaVersionMin = 13
const supportedSchemaVersionMax = 14

type manifest struct {
CreationDate string `json:"creationDate"`
Expand Down Expand Up @@ -61,13 +62,13 @@ func (mfst *manifest) validateManifest() error {
"You might need to upgrade to a newer version of JW Library first", version, mfst.Version)
}

if mfst.UserDataBackup.SchemaVersion > schemaVersion {
return fmt.Errorf("schema version is too new. Should be %d is %d. "+
"Make sure you use the latest version of the merger", schemaVersion, mfst.UserDataBackup.SchemaVersion)
if mfst.UserDataBackup.SchemaVersion > supportedSchemaVersionMax {
return fmt.Errorf("schema version is too new. Should be up to %d is %d. "+
"Make sure you use the latest version of the merger", supportedSchemaVersionMax, mfst.UserDataBackup.SchemaVersion)
}
if mfst.UserDataBackup.SchemaVersion < schemaVersion {
return fmt.Errorf("schema version is too old. Should be %d is %d. "+
"You might need to upgrade to a newer version of JW Library first", schemaVersion, mfst.UserDataBackup.SchemaVersion)
if mfst.UserDataBackup.SchemaVersion < supportedSchemaVersionMin {
return fmt.Errorf("schema version is too old. Should be at least %d is %d. "+
"You might need to upgrade to a newer version of JW Library first", supportedSchemaVersionMin, mfst.UserDataBackup.SchemaVersion)
}

return nil
Expand All @@ -94,7 +95,7 @@ func generateManifest(backupName string, dbFile string) (*manifest, error) {
LastModifiedDate: time.Now().Format("2006-01-02T15:04:05-07:00"),
Hash: hash,
DatabaseName: filepath.Base(dbFile),
SchemaVersion: schemaVersion,
SchemaVersion: supportedSchemaVersionMax,
DeviceName: "go-jwlm",
},
Name: backupName,
Expand Down
44 changes: 27 additions & 17 deletions model/manifest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func Test_manifest_validateManifest2(t *testing.T) {
wantErr assert.ErrorAssertionFunc
}{
{
name: "All good",
name: "Newest schema version",
mfst: &manifest{
UserDataBackup: userDataBackup{
SchemaVersion: 14,
Expand All @@ -79,51 +79,61 @@ func Test_manifest_validateManifest2(t *testing.T) {
wantErr: assert.NoError,
},
{
name: "Manifest version too old",
name: "Older but supported schema version",
mfst: &manifest{
UserDataBackup: userDataBackup{
SchemaVersion: 14,
SchemaVersion: 13,
},
Version: 0,
Version: 1,
},
wantErr: assert.NoError,
},
{
name: "Schema version too old",
mfst: &manifest{
UserDataBackup: userDataBackup{
SchemaVersion: 12,
},
Version: 1,
},
wantErr: func(tt assert.TestingT, err error, i ...interface{}) bool {
return assert.ErrorContains(tt, err, "manifest version is too old. Should be 1 is 0")
return assert.ErrorContains(tt, err, "schema version is too old. Should be at least 13 is 12")
},
},
{
name: "Manifest version too new",
name: "Schema version too new",
mfst: &manifest{
UserDataBackup: userDataBackup{
SchemaVersion: 14,
SchemaVersion: 15,
},
Version: 2,
Version: 1,
},
wantErr: func(tt assert.TestingT, err error, i ...interface{}) bool {
return assert.ErrorContains(tt, err, "manifest version is too new. Should be 1 is 2")
return assert.ErrorContains(tt, err, "schema version is too new. Should be up to 14 is 15")
},
},
{
name: "Schema version too old",
name: "Manifest version too old",
mfst: &manifest{
UserDataBackup: userDataBackup{
SchemaVersion: 13,
SchemaVersion: 14,
},
Version: 1,
Version: 0,
},
wantErr: func(tt assert.TestingT, err error, i ...interface{}) bool {
return assert.ErrorContains(tt, err, "schema version is too old. Should be 14 is 13")
return assert.ErrorContains(tt, err, "manifest version is too old. Should be 1 is 0")
},
},
{
name: "Schema version too new",
name: "Manifest version too new",
mfst: &manifest{
UserDataBackup: userDataBackup{
SchemaVersion: 15,
SchemaVersion: 14,
},
Version: 1,
Version: 2,
},
wantErr: func(tt assert.TestingT, err error, i ...interface{}) bool {
return assert.ErrorContains(tt, err, "schema version is too new. Should be 14 is 15")
return assert.ErrorContains(tt, err, "manifest version is too new. Should be 1 is 2")
},
},
}
Expand Down
2 changes: 1 addition & 1 deletion model/testdata/manifest_outdated.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"lastModifiedDate": "2020-04-09T05:47:26+02:00",
"hash": "d87a67028133cc4de5536affe1b072841def95899b7f7450a5622112b4b5e63f",
"databaseName": "user_data.db",
"schemaVersion": 13,
"schemaVersion": 12,
"deviceName": "iPhone"
},
"name": "UserDataBackup_2020-04-11_iPhone",
Expand Down

0 comments on commit 965c26a

Please sign in to comment.