Browse Source

restore: reset table sequences for PostgreSQL (#4357)

pull/4527/head
Unknwon 7 years ago
parent
commit
b17995a332
No known key found for this signature in database
GPG Key ID: 25B575AE3213B2B3
  1. 2
      cmd/restore.go
  2. 2
      gogs.go
  3. 17
      models/models.go
  4. 2
      templates/.VERSION

2
cmd/restore.go

@ -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

@ -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

17
models/models.go

@ -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
}

2
templates/.VERSION

@ -1 +1 @@
0.11.10.0517
0.11.11.0521
Loading…
Cancel
Save