Skip to content

Commit

Permalink
Merge pull request #146 from AndreasSko/schema_14
Browse files Browse the repository at this point in the history
Support Schema version 14
  • Loading branch information
AndreasSko committed Aug 13, 2023
2 parents 3e0a485 + cfb9d5a commit f400981
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 12 deletions.
2 changes: 1 addition & 1 deletion model/Database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -881,7 +881,7 @@ func Test_createEmptySQLiteDB(t *testing.T) {
}
hash := fmt.Sprintf("%x", hasher.Sum(nil))

assert.Equal(t, "774af7240646c49f6a55e40b6cdf681a6b04fbcf4acebdb19a6e0e2bef53d766", hash)
assert.Equal(t, "78edd07c0b04212dcc2dd59be0a5d2edf91088136986378147cd8aa04cf4965c", hash)
}

func TestDatabase_saveToNewSQLite(t *testing.T) {
Expand Down
Binary file modified model/data/userData.db
Binary file not shown.
18 changes: 13 additions & 5 deletions model/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
)

const version = 1
const schemaVersion = 13
const schemaVersion = 14

type manifest struct {
CreationDate string `json:"creationDate"`
Expand Down Expand Up @@ -52,13 +52,21 @@ func (mfst *manifest) importManifest(path string) error {

// validateManifest checks if the backup file is compatible by validating the manifest
func (mfst *manifest) validateManifest() error {
if mfst.Version != version {
return fmt.Errorf("Manifest version is incompatible. Should be %d is %d. "+
if mfst.Version > version {
return fmt.Errorf("manifest version is too new. Should be %d is %d. "+
"Make sure you use the latest version of the merger", version, mfst.Version)
}
if mfst.Version < version {
return fmt.Errorf("manifest version is too old. Should be %d is %d. "+
"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 incompatible. Should be %d is %d. "+
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 < 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)
}

Expand Down
82 changes: 78 additions & 4 deletions model/manifest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package model
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"
"time"
Expand All @@ -16,7 +17,7 @@ var exampleManifest = &manifest{
LastModifiedDate: time.Now().Format("2006-01-02T15:04:05-07:00"),
Hash: "e2e09ceba668bb1ad093b2db317237451a01ae9ff435b38c840b70dc434f184f",
DatabaseName: userDataFilename,
SchemaVersion: 13,
SchemaVersion: 14,
DeviceName: "go-jwlm",
},
Name: "test",
Expand All @@ -36,7 +37,7 @@ func Test_manifest_importManifest(t *testing.T) {
LastModifiedDate: "2020-04-09T05:47:26+02:00",
Hash: "d87a67028133cc4de5536affe1b072841def95899b7f7450a5622112b4b5e63f",
DatabaseName: userDataFilename,
SchemaVersion: 13,
SchemaVersion: 14,
DeviceName: "iPhone",
},
Name: "UserDataBackup_2020-04-11_iPhone",
Expand All @@ -48,7 +49,7 @@ func Test_manifest_importManifest(t *testing.T) {
assert.Error(t, mfst.importManifest("nonexistentpath"))
}

func Test_validateManifest(t *testing.T) {
func Test_validateManifest1(t *testing.T) {
path := filepath.Join("testdata", "manifest_correct.json")

mfst := manifest{}
Expand All @@ -61,6 +62,79 @@ func Test_validateManifest(t *testing.T) {
assert.Error(t, mfst.validateManifest())
}

func Test_manifest_validateManifest2(t *testing.T) {
tests := []struct {
name string
mfst *manifest
wantErr assert.ErrorAssertionFunc
}{
{
name: "All good",
mfst: &manifest{
UserDataBackup: userDataBackup{
SchemaVersion: 14,
},
Version: 1,
},
wantErr: assert.NoError,
},
{
name: "Manifest version too old",
mfst: &manifest{
UserDataBackup: userDataBackup{
SchemaVersion: 14,
},
Version: 0,
},
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")
},
},
{
name: "Manifest version too new",
mfst: &manifest{
UserDataBackup: userDataBackup{
SchemaVersion: 14,
},
Version: 2,
},
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")
},
},
{
name: "Schema version too old",
mfst: &manifest{
UserDataBackup: userDataBackup{
SchemaVersion: 13,
},
Version: 1,
},
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")
},
},
{
name: "Schema version too new",
mfst: &manifest{
UserDataBackup: userDataBackup{
SchemaVersion: 15,
},
Version: 1,
},
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")
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := tt.mfst.validateManifest()
tt.wantErr(t, err)
})
}
}

func Test_generateManifest(t *testing.T) {
dbPath := filepath.Join("testdata", userDataFilename)

Expand All @@ -76,7 +150,7 @@ func Test_generateManifest(t *testing.T) {
func Test_exportManifest(t *testing.T) {
tmp, err := ioutil.TempDir("", "go-jwlm")
assert.NoError(t, err)
//defer os.RemoveAll(tmp)
defer os.RemoveAll(tmp)

path := filepath.Join(tmp, "test_manifest.json")
fmt.Println(path)
Expand Down
Binary file modified model/testdata/backup.jwlibrary
Binary file not shown.
Binary file modified model/testdata/backup_shuffled.jwlibrary
Binary file not shown.
Binary file modified model/testdata/backup_withPlaylist.jwlibrary
Binary file not shown.
2 changes: 1 addition & 1 deletion model/testdata/manifest_correct.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": "userData.db",
"schemaVersion": 13,
"schemaVersion": 14,
"deviceName": "iPhone"
},
"name": "UserDataBackup_2020-04-11_iPhone",
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": 8,
"schemaVersion": 13,
"deviceName": "iPhone"
},
"name": "UserDataBackup_2020-04-11_iPhone",
Expand Down

0 comments on commit f400981

Please sign in to comment.