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

Related to vikunja/api#1199
This commit is contained in:
kolaente 2022-07-13 23:43:53 +02:00
parent 596d2bf676
commit 54348c5891
Signed by untrusted user: konrad
GPG Key ID: F40E70337AB24C9B
1 changed files with 12 additions and 0 deletions

View File

@ -18,6 +18,9 @@ package db
import (
"encoding/json"
"strconv"
"code.vikunja.io/api/pkg/log"
"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
}