fix(restore): make sure to reset sequences after importing a dump when using postgres

Related to https://kolaente.dev/vikunja/api/issues/1199
This commit is contained in:
kolaente 2022-07-13 23:43:53 +02:00
parent 596d2bf676
commit 54348c5891
No known key found for this signature in database
GPG key ID: F40E70337AB24C9B

View file

@ -18,6 +18,9 @@ package db
import ( import (
"encoding/json" "encoding/json"
"strconv"
"code.vikunja.io/api/pkg/log"
"xorm.io/xorm/schemas" "xorm.io/xorm/schemas"
) )
@ -57,6 +60,15 @@ func Restore(table string, contents []map[string]interface{}) (err error) {
} }
} }
if Type() == schemas.POSTGRES {
idSequence := table + "_id_seq"
_, err = x.Query("SELECT setval('" + idSequence + "', " + strconv.Itoa(len(contents)) + ", true);")
if err != nil {
log.Warningf("Could not reset id sequence for %s: %s", idSequence, err)
err = nil
}
}
return return
} }