Skip to content

Commit 0174447

Browse files
committed
Corrections for issue 1462
Adjusts the drop table logic so it occurs only once. Adds line comment filtering since a few line comments followed by an `ALTER TABLE` statement could be excluded using simpler skip logic for empty queries. Improved the show details output as well as it runs through any additional queries that may be present in the `-schema.sql` files, although in the majority of cases the files should only contain the single `CREATE TABLE` query.
1 parent 3287a73 commit 0174447

File tree

1 file changed

+56
-20
lines changed

1 file changed

+56
-20
lines changed

internal/dumper/loader.go

Lines changed: 56 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -304,32 +304,68 @@ func (l *Loader) restoreTableSchema(overwrite bool, tables []string, conn *Conne
304304
return err
305305
}
306306

307+
// Drop table once before processing queries (if overwrite is enabled)
308+
if overwrite {
309+
l.log.Info(
310+
"drop(overwrite.is.true)",
311+
zap.String("database", db),
312+
zap.String("table ", tbl),
313+
)
314+
315+
if l.cfg.ShowDetails {
316+
l.cfg.Printer.Println("Dropping Existing Table (if it exists): " + printer.BoldBlue(name))
317+
}
318+
dropQuery := fmt.Sprintf("DROP TABLE IF EXISTS %s", name)
319+
err = conn.Execute(dropQuery)
320+
if err != nil {
321+
return err
322+
}
323+
}
324+
325+
// Execute each valid SQL statement (skip comments)
307326
for _, query := range queries {
308-
if !strings.HasPrefix(query, "/*") && query != "" {
309-
if overwrite {
310-
l.log.Info(
311-
"drop(overwrite.is.true)",
312-
zap.String("database", db),
313-
zap.String("table ", tbl),
314-
)
327+
// Skip empty queries and block comments
328+
trimmedQuery := strings.TrimSpace(query)
329+
if trimmedQuery == "" || strings.HasPrefix(trimmedQuery, "/*") {
330+
continue
331+
}
315332

316-
if l.cfg.ShowDetails {
317-
l.cfg.Printer.Println("Dropping Existing Table (if it exists): " + printer.BoldBlue(name))
318-
}
319-
dropQuery := fmt.Sprintf("DROP TABLE IF EXISTS %s", name)
320-
err = conn.Execute(dropQuery)
321-
if err != nil {
322-
return err
323-
}
333+
// Filter out line comments (--) but keep the rest of the query
334+
var cleanedLines []string
335+
for _, line := range strings.Split(query, "\n") {
336+
trimmedLine := strings.TrimSpace(line)
337+
// Skip empty lines and comment-only lines
338+
if trimmedLine != "" && !strings.HasPrefix(trimmedLine, "--") {
339+
cleanedLines = append(cleanedLines, line)
324340
}
341+
}
325342

326-
if l.cfg.ShowDetails {
343+
// Skip if no non-comment content remains
344+
if len(cleanedLines) == 0 {
345+
continue
346+
}
347+
348+
// Reconstruct the query without comment lines
349+
cleanedQuery := strings.Join(cleanedLines, "\n")
350+
trimmedCleanedQuery := strings.TrimSpace(cleanedQuery)
351+
352+
if l.cfg.ShowDetails {
353+
// Detect query type and provide appropriate output
354+
upperQuery := strings.ToUpper(trimmedCleanedQuery)
355+
if strings.HasPrefix(upperQuery, "CREATE TABLE") {
327356
l.cfg.Printer.Printf("Creating Table: %s (Table %d of %d)\n", printer.BoldBlue(name), (idx + 1), numberOfTables)
357+
} else if strings.HasPrefix(upperQuery, "ALTER TABLE") {
358+
l.cfg.Printer.Printf("Altering Table: %s (Table %d of %d)\n", printer.BoldBlue(name), (idx + 1), numberOfTables)
359+
l.cfg.Printer.Printf("Query: %s\n", cleanedQuery)
360+
} else {
361+
// For any other query type, show what's being executed
362+
l.cfg.Printer.Printf("Executing Query for Table: %s (Table %d of %d)\n", printer.BoldBlue(name), (idx + 1), numberOfTables)
363+
l.cfg.Printer.Printf("Query: %s\n", cleanedQuery)
328364
}
329-
err = conn.Execute(query)
330-
if err != nil {
331-
return err
332-
}
365+
}
366+
err = conn.Execute(cleanedQuery)
367+
if err != nil {
368+
return err
333369
}
334370
}
335371
l.log.Info("restoring schema",

0 commit comments

Comments
 (0)