Sfoglia il codice sorgente

restore: reset table sequences for PostgreSQL (#4357)

Unknwon 7 anni fa
parent
commit
b17995a332
4 ha cambiato i file con 19 aggiunte e 4 eliminazioni
  1. 1 1
      cmd/restore.go
  2. 1 1
      gogs.go
  3. 16 1
      models/models.go
  4. 1 1
      templates/.VERSION

+ 1 - 1
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)
 	}
 

+ 1 - 1
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

+ 16 - 1
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
 }

+ 1 - 1
templates/.VERSION

@@ -1 +1 @@
-0.11.10.0517
+0.11.11.0521