Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Detailed the errors of the apply subcommand #92

Merged
merged 1 commit into from
Aug 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 25 additions & 15 deletions pkg/ddlctl/apply/ddlctl_apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,21 +123,26 @@ func Apply(ctx context.Context, dialect, dsn, ddlStr string) error {
continue
}
if _, err := db.ExecContext(ctx, q); err != nil {
// not error, not log, go next ddl
if errorz.Contains(err, "already exists") || errorz.Contains(err, "Duplicate column name") {
// If the error is one of the following, do not error. go to the next DDL;
switch {
case errorz.Contains(err, "already exists"),
errorz.Contains(err, "Duplicate column name"):
continue
}

err = apperr.Errorf("db.ExecContext: q=%s: %w", q, err)
outerErr = err
// error, but not log, go next ddl
// If the error is one of the following, error but not log. go to the next DDL;
if errorz.Contains(err, "Cannot add foreign key constraint") {
continue
}
// error and log, go next ddl
logs.Warn.Printf("db.ExecContext: %v", err)

// If the error is not one of the above, error and log. go to the next DDL;
logs.Warn.Printf(err.Error())
}
}
if outerErr != nil {
return apperr.Errorf("db.ExecContext: %w", err)
return err
}
return nil
}); err != nil {
Expand All @@ -150,31 +155,36 @@ func Apply(ctx context.Context, dialect, dsn, ddlStr string) error {
return apperr.Errorf("db.Conn: %w", err)
}
defer func() {
if cerr := conn.Close(); err == nil && cerr != nil {
err = apperr.Errorf("conn.Close: %w", cerr)
if err2 := conn.Close(); err == nil && err2 != nil {
err = apperr.Errorf("conn.Close: %w", err2)
}
}()
if _, err := conn.ExecContext(ctx, "START BATCH DDL"); err != nil {
return apperr.Errorf("conn.ExecContext: %w", err)
{
q := "START BATCH DDL"
if _, err := conn.ExecContext(ctx, q); err != nil {
return apperr.Errorf("conn.ExecContext: q=%s: %w", q, err)
}
}

commentTrimmedDDL := stringz.ReadLine(ddlStr, "\n", stringz.ReadLineFuncRemoveCommentLine("--"))
for _, q := range strings.Split(commentTrimmedDDL, ";\n") {
if len(q) == 0 {
// skip empty query
continue
}
if _, err := conn.ExecContext(ctx, q); err != nil {
return apperr.Errorf("conn.ExecContext: %w", err)
return apperr.Errorf("conn.ExecContext: q=%s: %w", q, err)
}
}

if _, err := conn.ExecContext(ctx, "RUN BATCH"); err != nil {
return apperr.Errorf("conn.ExecContext: %w", err)
{
q := "RUN BATCH"
if _, err := conn.ExecContext(ctx, q); err != nil {
return apperr.Errorf("conn.ExecContext: q=%s: %w", q, err)
}
}
default:
if _, err := db.ExecContext(ctx, ddlStr); err != nil {
return apperr.Errorf("db.ExecContext: %w", err)
return apperr.Errorf("db.ExecContext: q=%s: %w", ddlStr, err)
}
}

Expand Down
Loading