mirror of
https://github.com/gogs/gogs.git
synced 2025-12-20 15:20:01 +01:00
restore: reset table sequences for PostgreSQL (#4357)
This commit is contained in:
@@ -83,7 +83,7 @@ func runRestore(c *cli.Context) error {
|
||||
|
||||
// Database
|
||||
dbDir := path.Join(archivePath, "db")
|
||||
if err = models.ImportDatabase(dbDir); err != nil {
|
||||
if err = models.ImportDatabase(dbDir, c.Bool("verbose")); err != nil {
|
||||
log.Fatal(0, "Fail to import database: %v", err)
|
||||
}
|
||||
|
||||
|
||||
2
gogs.go
2
gogs.go
@@ -16,7 +16,7 @@ import (
|
||||
"github.com/gogits/gogs/pkg/setting"
|
||||
)
|
||||
|
||||
const APP_VER = "0.11.10.0517"
|
||||
const APP_VER = "0.11.11.0521"
|
||||
|
||||
func init() {
|
||||
setting.AppVer = APP_VER
|
||||
|
||||
@@ -302,7 +302,9 @@ func DumpDatabase(dirPath string) (err error) {
|
||||
}
|
||||
|
||||
// ImportDatabase imports data from backup archive.
|
||||
func ImportDatabase(dirPath string) (err error) {
|
||||
func ImportDatabase(dirPath string, verbose bool) (err error) {
|
||||
snakeMapper := core.SnakeMapper{}
|
||||
|
||||
// Purposely create a local variable to not modify global variable
|
||||
tables := append(tables, new(Version))
|
||||
for _, table := range tables {
|
||||
@@ -312,6 +314,10 @@ func ImportDatabase(dirPath string) (err error) {
|
||||
continue
|
||||
}
|
||||
|
||||
if verbose {
|
||||
log.Trace("Importing table '%s'...", tableName)
|
||||
}
|
||||
|
||||
if err = x.DropTables(table); err != nil {
|
||||
return fmt.Errorf("fail to drop table '%s': %v", tableName, err)
|
||||
} else if err = x.Sync2(table); err != nil {
|
||||
@@ -353,6 +359,15 @@ func ImportDatabase(dirPath string) (err error) {
|
||||
return fmt.Errorf("fail to insert strcut: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// PostgreSQL needs manually reset table sequence for auto increment keys
|
||||
if setting.UsePostgreSQL {
|
||||
rawTableName := snakeMapper.Obj2Table(tableName)
|
||||
seqName := rawTableName + "_id_seq"
|
||||
if _, err = x.Exec(fmt.Sprintf(`SELECT setval('%s', COALESCE((SELECT MAX(id)+1 FROM "%s"), 1), false);`, seqName, rawTableName)); err != nil {
|
||||
return fmt.Errorf("fail to reset table '%s' sequence: %v", rawTableName, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -1 +1 @@
|
||||
0.11.10.0517
|
||||
0.11.11.0521
|
||||
Reference in New Issue
Block a user