Skip to content

Commit

Permalink
UUIDExists test func.
Browse files Browse the repository at this point in the history
more tracing.
  • Loading branch information
bengarrett committed Sep 11, 2024
1 parent 9d829f7 commit eb55a4e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
13 changes: 13 additions & 0 deletions model/exists.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,16 @@ func HashExists(ctx context.Context, exec boil.ContextExecutor, hash string) (bo
}
return ok, nil
}

// UUIDExists returns true if the file record exists in the database using a UUID.
func UUIDExists(ctx context.Context, exec boil.ContextExecutor, uuid string) (bool, error) {
if invalidExec(exec) {
return false, ErrDB
}
ok, err := models.Files(models.FileWhere.UUID.EQ(null.StringFrom(uuid)),
qm.WithDeleted()).Exists(ctx, exec)
if err != nil {
return false, fmt.Errorf("exist file uuid %s: %w", uuid, err)
}
return ok, nil
}
11 changes: 9 additions & 2 deletions model/insert.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"database/sql"
"fmt"
"net/url"
"os"
"time"

"github.com/Defacto2/server/handler/pouet"
Expand Down Expand Up @@ -84,6 +85,11 @@ func InsertUpload(ctx context.Context, tx *sql.Tx, values url.Values, key string
return 0, noID, fmt.Errorf("uuid.NewV7: %w", err)
}
unique := null.StringFrom(uid.String())
if exist, err := UUIDExists(ctx, tx, uid.String()); err != nil {
return 0, noID, fmt.Errorf("UUIDExists: %w", err)
} else if exist {
return 0, noID, fmt.Errorf("insert uload %w, does the uuid already exist in the table?: %s", ErrUUID, uid.String())
}
deleteT := null.TimeFromPtr(&now)
if !deleteT.Valid || deleteT.Time.IsZero() {
return 0, noID, fmt.Errorf("%w: %v", ErrTime, deleteT.Time)
Expand All @@ -101,11 +107,12 @@ func InsertUpload(ctx context.Context, tx *sql.Tx, values url.Values, key string
if err != nil {
return 0, noID, fmt.Errorf("upload: %w", err)
}
fmt.Fprintf(os.Stderr, "\ninsert upload f.Insert: %+v\n", f)
if err = f.Insert(ctx, tx, boil.Infer()); err != nil {
return 0, noID, fmt.Errorf("f.Insert: %w", err)
return 0, noID, fmt.Errorf("insert upload key %q: %w", key, err)
}
if err = tx.Commit(); err != nil {
return 0, noID, fmt.Errorf("tx.Commit: %w", err)
return 0, noID, fmt.Errorf("insert upload key %q tx.commit: %w", key, err)
}
return f.ID, uid, nil
}
Expand Down

0 comments on commit eb55a4e

Please sign in to comment.