fix: reset id sequence when importing a dump from postgres

This commit is contained in:
kolaente 2022-07-21 14:54:52 +02:00
parent f93b68819d
commit 0f555b7ec7
Signed by untrusted user: konrad
GPG Key ID: F40E70337AB24C9B
1 changed files with 2 additions and 4 deletions

View File

@ -17,10 +17,8 @@
package db
import (
"encoding/json"
"strconv"
"code.vikunja.io/api/pkg/log"
"encoding/json"
"xorm.io/xorm/schemas"
)
@ -62,7 +60,7 @@ 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);")
_, err = x.Query("SELECT setval('" + idSequence + "', COALESCE(MAX(id), 1) )")
if err != nil {
log.Warningf("Could not reset id sequence for %s: %s", idSequence, err)
err = nil