Skip to content

Commit c5a52d5

Browse files
committed
warn users if they are trying to upload a dump
This is a common mistake, so worth checking explicitly.
1 parent 5a92d35 commit c5a52d5

1 file changed

Lines changed: 29 additions & 0 deletions

File tree

internal/cmd/group_flag.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,10 +190,38 @@ func countFlags(flags ...string) (count int) {
190190
}
191191

192192
const MaxAWSDBSizeBytes = 1024 * 1024 * 1024 * 20 // 20 GB
193+
func checkIfDump(filename string) (bool, error) {
194+
file, err := os.Open(filename)
195+
if err != nil {
196+
return false, err
197+
}
198+
defer file.Close()
199+
scanner := bufio.NewScanner(file)
200+
if scanner.Scan() {
201+
firstLine := scanner.Text()
202+
return strings.TrimSpace(firstLine) == "PRAGMA foreign_keys=OFF;", nil
203+
} else {
204+
return false, scanner.Err()
205+
}
206+
}
207+
193208
func sqliteFileIntegrityChecks(file string) error {
194209
if flags.Debug() {
195210
log.Printf("Running integrity checks on database file %s", file)
196211
}
212+
213+
if flags.Debug() {
214+
log.Printf("Checking if this is a sqlite dump: common mistake!...")
215+
}
216+
217+
isDump, err := checkIfDump(file)
218+
if err != nil {
219+
return fmt.Errorf("failed to get file header: %w", err)
220+
}
221+
if isDump {
222+
return fmt.Errorf("%s is a sqlite3 dump, not a sqlite3 database. Please import a sqlite database", file)
223+
}
224+
197225
if flags.Debug() {
198226
log.Printf("Checking file size...")
199227
}
@@ -212,6 +240,7 @@ func sqliteFileIntegrityChecks(file string) error {
212240
output, err := exec.Command("sqlite3", file, ".mode line",
213241
"select journal_mode as j, page_size as p, auto_vacuum as a, encoding as e from pragma_journal_mode, pragma_page_size, pragma_auto_vacuum, pragma_encoding;").CombinedOutput()
214242
if err != nil {
243+
215244
return fmt.Errorf("failed to check database settings: %w", err)
216245
}
217246

0 commit comments

Comments
 (0)