mirror of
https://github.com/octoleo/syncthing.git
synced 2025-04-03 08:11:50 +00:00
lib/db: Store versions for last successful db migration (#7140)
This commit is contained in:
parent
cc4071d0ba
commit
8ebd893349
@ -17,18 +17,6 @@ import (
|
|||||||
"github.com/syncthing/syncthing/lib/protocol"
|
"github.com/syncthing/syncthing/lib/protocol"
|
||||||
)
|
)
|
||||||
|
|
||||||
// List of all dbVersion to dbMinSyncthingVersion pairs for convenience
|
|
||||||
// 0: v0.14.0
|
|
||||||
// 1: v0.14.46
|
|
||||||
// 2: v0.14.48
|
|
||||||
// 3-5: v0.14.49
|
|
||||||
// 6: v0.14.50
|
|
||||||
// 7: v0.14.53
|
|
||||||
// 8-9: v1.4.0
|
|
||||||
// 10-11: v1.6.0
|
|
||||||
// 12-13: v1.7.0
|
|
||||||
// 14: v1.9.0
|
|
||||||
//
|
|
||||||
// dbMigrationVersion is for migrations that do not change the schema and thus
|
// dbMigrationVersion is for migrations that do not change the schema and thus
|
||||||
// do not put restrictions on downgrades (e.g. for repairs after a bugfix).
|
// do not put restrictions on downgrades (e.g. for repairs after a bugfix).
|
||||||
const (
|
const (
|
||||||
@ -37,6 +25,13 @@ const (
|
|||||||
dbMinSyncthingVersion = "v1.9.0"
|
dbMinSyncthingVersion = "v1.9.0"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type migration struct {
|
||||||
|
schemaVersion int64
|
||||||
|
migrationVersion int64
|
||||||
|
minSyncthingVersion string
|
||||||
|
migration func(prevSchema int) error
|
||||||
|
}
|
||||||
|
|
||||||
var errFolderMissing = errors.New("folder present in global list but missing in keyer index")
|
var errFolderMissing = errors.New("folder present in global list but missing in keyer index")
|
||||||
|
|
||||||
type databaseDowngradeError struct {
|
type databaseDowngradeError struct {
|
||||||
@ -96,24 +91,19 @@ func (db *schemaUpdater) updateSchema() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type migration struct {
|
migrations := []migration{
|
||||||
schemaVersion int64
|
{1, 1, "v0.14.0", db.updateSchema0to1},
|
||||||
migrationVersion int64
|
{2, 2, "v0.14.46", db.updateSchema1to2},
|
||||||
migration func(prevSchema int) error
|
{3, 3, "v0.14.48", db.updateSchema2to3},
|
||||||
}
|
{5, 5, "v0.14.49", db.updateSchemaTo5},
|
||||||
var migrations = []migration{
|
{6, 6, "v0.14.50", db.updateSchema5to6},
|
||||||
{1, 1, db.updateSchema0to1},
|
{7, 7, "v0.14.53", db.updateSchema6to7},
|
||||||
{2, 2, db.updateSchema1to2},
|
{9, 9, "v1.4.0", db.updateSchemaTo9},
|
||||||
{3, 3, db.updateSchema2to3},
|
{10, 10, "v1.6.0", db.updateSchemaTo10},
|
||||||
{5, 5, db.updateSchemaTo5},
|
{11, 11, "v1.6.0", db.updateSchemaTo11},
|
||||||
{6, 6, db.updateSchema5to6},
|
{13, 13, "v1.7.0", db.updateSchemaTo13},
|
||||||
{7, 7, db.updateSchema6to7},
|
{14, 14, "v1.9.0", db.updateSchemaTo14},
|
||||||
{9, 9, db.updateSchemaTo9},
|
{14, 15, "v1.9.0", db.migration15},
|
||||||
{10, 10, db.updateSchemaTo10},
|
|
||||||
{11, 11, db.updateSchemaTo11},
|
|
||||||
{13, 13, db.updateSchemaTo13},
|
|
||||||
{14, 14, db.updateSchemaTo14},
|
|
||||||
{14, 15, db.migration15},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, m := range migrations {
|
for _, m := range migrations {
|
||||||
@ -122,23 +112,37 @@ func (db *schemaUpdater) updateSchema() error {
|
|||||||
if err := m.migration(int(prevVersion)); err != nil {
|
if err := m.migration(int(prevVersion)); err != nil {
|
||||||
return fmt.Errorf("failed to do migration %v: %w", m.migrationVersion, err)
|
return fmt.Errorf("failed to do migration %v: %w", m.migrationVersion, err)
|
||||||
}
|
}
|
||||||
|
if err := db.writeVersions(m, miscDB); err != nil {
|
||||||
|
return fmt.Errorf("failed to write versions after migration %v: %w", m.migrationVersion, err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := miscDB.PutInt64("dbVersion", dbVersion); err != nil {
|
if err := db.writeVersions(migration{
|
||||||
return err
|
schemaVersion: dbVersion,
|
||||||
}
|
migrationVersion: dbMigrationVersion,
|
||||||
if err := miscDB.PutString("dbMinSyncthingVersion", dbMinSyncthingVersion); err != nil {
|
minSyncthingVersion: dbMinSyncthingVersion,
|
||||||
return err
|
}, miscDB); err != nil {
|
||||||
}
|
return fmt.Errorf("failed to write versions after migrations: %w", err)
|
||||||
if err := miscDB.PutInt64("dbMigrationVersion", dbMigrationVersion); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
l.Infoln("Compacting database after migration...")
|
l.Infoln("Compacting database after migration...")
|
||||||
return db.Compact()
|
return db.Compact()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (*schemaUpdater) writeVersions(m migration, miscDB *NamespacedKV) error {
|
||||||
|
if err := miscDB.PutInt64("dbVersion", m.schemaVersion); err != nil && err == nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := miscDB.PutString("dbMinSyncthingVersion", m.minSyncthingVersion); err != nil && err == nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := miscDB.PutInt64("dbMigrationVersion", m.migrationVersion); err != nil && err == nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (db *schemaUpdater) updateSchema0to1(_ int) error {
|
func (db *schemaUpdater) updateSchema0to1(_ int) error {
|
||||||
t, err := db.newReadWriteTransaction()
|
t, err := db.newReadWriteTransaction()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user