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
No known key found for this signature in database
GPG key ID: F40E70337AB24C9B

View file

@ -17,10 +17,8 @@
package db package db
import ( import (
"encoding/json"
"strconv"
"code.vikunja.io/api/pkg/log" "code.vikunja.io/api/pkg/log"
"encoding/json"
"xorm.io/xorm/schemas" "xorm.io/xorm/schemas"
) )
@ -62,7 +60,7 @@ func Restore(table string, contents []map[string]interface{}) (err error) {
if Type() == schemas.POSTGRES { if Type() == schemas.POSTGRES {
idSequence := table + "_id_seq" 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 { if err != nil {
log.Warningf("Could not reset id sequence for %s: %s", idSequence, err) log.Warningf("Could not reset id sequence for %s: %s", idSequence, err)
err = nil err = nil