From 54348c5891bf182719a49c3ded89e113266825b7 Mon Sep 17 00:00:00 2001 From: kolaente Date: Wed, 13 Jul 2022 23:43:53 +0200 Subject: [PATCH] fix(restore): make sure to reset sequences after importing a dump when using postgres Related to https://kolaente.dev/vikunja/api/issues/1199 --- pkg/db/dump.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/pkg/db/dump.go b/pkg/db/dump.go index 334b4d1e..fde6d473 100644 --- a/pkg/db/dump.go +++ b/pkg/db/dump.go @@ -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 }