Skip to content

Commit f131ccc

Browse files
authored
Merge pull request #402 from A10tech/restore-issue
Quick fix for restore of backups made by dump fails silently
2 parents 47e5513 + 0d767ad commit f131ccc

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

pkg/database/restore.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ import (
99
"regexp"
1010
)
1111

12+
const (
13+
// used to define default max buffer size Scanner, counter part of dump
14+
defaultMaxAllowedPacket = 4194304
15+
)
16+
1217
var (
1318
useRegex = regexp.MustCompile(`(?i)^(USE\s*` + "`" + `)([^\s]+)(` + "`" + `\s*;)$`)
1419
createRegex = regexp.MustCompile(`(?i)^(CREATE\s+DATABASE\s*(\/\*.*\*\/\s*)?` + "`" + `)([^\s]+)(` + "`" + `\s*(\s*\/\*.*\*\/\s*)?\s*;$)`)
@@ -27,7 +32,10 @@ func Restore(ctx context.Context, dbconn Connection, databasesMap map[string]str
2732
if err != nil {
2833
return fmt.Errorf("failed to restore database: %w", err)
2934
}
35+
scanBuf := []byte{}
3036
scanner := bufio.NewScanner(r)
37+
// increase the buffer size
38+
scanner.Buffer(scanBuf, defaultMaxAllowedPacket) //TODO should be a configurable option like with dump
3139
var current string
3240
for scanner.Scan() {
3341
line := scanner.Text()
@@ -58,6 +66,9 @@ func Restore(ctx context.Context, dbconn Connection, databasesMap map[string]str
5866
}
5967
current = ""
6068
}
69+
if err := scanner.Err(); err != nil {
70+
return fmt.Errorf("failed to restore database: %w", err)
71+
}
6172
if err := tx.Commit(); err != nil {
6273
return fmt.Errorf("failed to restore database: %w", err)
6374
}

0 commit comments

Comments
 (0)