replace go uuid generation by postgresql

This commit is contained in:
m
2026-07-14 08:36:11 +02:00
parent 93100a2b81
commit 18bd08a919
7 changed files with 24 additions and 37 deletions
+6 -13
View File
@@ -13,13 +13,12 @@ import (
) )
const createFile = `-- name: CreateFile :one const createFile = `-- name: CreateFile :one
INSERT INTO files (id, name, mime_type, size, storage_key, checksum, created_at, updated_at) INSERT INTO files (name, mime_type, size, storage_key, checksum, created_at, updated_at)
VALUES ($1, $2, $3, $4, $5, $6, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP) VALUES ($1, $2, $3, $4, $5, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)
RETURNING id, name, mime_type, size, storage_key, checksum, ocr_text, created_at, updated_at, parent_file_id, is_folder RETURNING id, name, mime_type, size, storage_key, checksum, ocr_text, created_at, updated_at, parent_file_id, is_folder
` `
type CreateFileParams struct { type CreateFileParams struct {
ID string `json:"id"`
Name string `json:"name"` Name string `json:"name"`
MimeType string `json:"mime_type"` MimeType string `json:"mime_type"`
Size int64 `json:"size"` Size int64 `json:"size"`
@@ -29,7 +28,6 @@ type CreateFileParams struct {
func (q *Queries) CreateFile(ctx context.Context, arg CreateFileParams) (File, error) { func (q *Queries) CreateFile(ctx context.Context, arg CreateFileParams) (File, error) {
row := q.db.QueryRowContext(ctx, createFile, row := q.db.QueryRowContext(ctx, createFile,
arg.ID,
arg.Name, arg.Name,
arg.MimeType, arg.MimeType,
arg.Size, arg.Size,
@@ -54,18 +52,13 @@ func (q *Queries) CreateFile(ctx context.Context, arg CreateFileParams) (File, e
} }
const createFolder = `-- name: CreateFolder :one const createFolder = `-- name: CreateFolder :one
INSERT INTO files (id, name, is_folder, created_at, updated_at) INSERT INTO files (name, is_folder, created_at, updated_at)
VALUES ($1, $2, true, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP) VALUES ($1, true, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)
RETURNING id, name, mime_type, size, storage_key, checksum, ocr_text, created_at, updated_at, parent_file_id, is_folder RETURNING id, name, mime_type, size, storage_key, checksum, ocr_text, created_at, updated_at, parent_file_id, is_folder
` `
type CreateFolderParams struct { func (q *Queries) CreateFolder(ctx context.Context, name string) (File, error) {
ID string `json:"id"` row := q.db.QueryRowContext(ctx, createFolder, name)
Name string `json:"name"`
}
func (q *Queries) CreateFolder(ctx context.Context, arg CreateFolderParams) (File, error) {
row := q.db.QueryRowContext(ctx, createFolder, arg.ID, arg.Name)
var i File var i File
err := row.Scan( err := row.Scan(
&i.ID, &i.ID,
@@ -0,0 +1 @@
ALTER TABLE files ALTER COLUMN id DROP DEFAULT;
@@ -0,0 +1,2 @@
ALTER TABLE files ALTER COLUMN id SET DEFAULT gen_random_uuid();
ALTER TABLE files ALTER COLUMN storage_key SET DEFAULT '';
+4 -4
View File
@@ -18,13 +18,13 @@ WHERE id = ANY($1::text[])
ORDER BY created_at DESC; ORDER BY created_at DESC;
-- name: CreateFile :one -- name: CreateFile :one
INSERT INTO files (id, name, mime_type, size, storage_key, checksum, created_at, updated_at) INSERT INTO files (name, mime_type, size, storage_key, checksum, created_at, updated_at)
VALUES ($1, $2, $3, $4, $5, $6, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP) VALUES ($1, $2, $3, $4, $5, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)
RETURNING *; RETURNING *;
-- name: CreateFolder :one -- name: CreateFolder :one
INSERT INTO files (id, name, is_folder, created_at, updated_at) INSERT INTO files (name, is_folder, created_at, updated_at)
VALUES ($1, $2, true, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP) VALUES ($1, true, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)
RETURNING *; RETURNING *;
-- name: ListFilesByParentID :many -- name: ListFilesByParentID :many
+4 -4
View File
@@ -1,6 +1,6 @@
-- name: CreateTag :one -- name: CreateTag :one
INSERT INTO tags (id, tag_name, tag_type) INSERT INTO tags (tag_name, tag_type)
VALUES ($1, $2, $3) VALUES ($1, $2)
RETURNING *; RETURNING *;
-- name: GetTag :one -- name: GetTag :one
@@ -20,8 +20,8 @@ DELETE FROM tags
WHERE id = $1; WHERE id = $1;
-- name: AddTagToFile :exec -- name: AddTagToFile :exec
INSERT INTO file_tags (id, tag_id, file_id) INSERT INTO file_tags (tag_id, file_id)
VALUES ($1, $2, $3) VALUES ($1, $2)
ON CONFLICT DO NOTHING; ON CONFLICT DO NOTHING;
-- name: RemoveTagFromFile :exec -- name: RemoveTagFromFile :exec
+6 -8
View File
@@ -11,36 +11,34 @@ import (
) )
const addTagToFile = `-- name: AddTagToFile :exec const addTagToFile = `-- name: AddTagToFile :exec
INSERT INTO file_tags (id, tag_id, file_id) INSERT INTO file_tags (tag_id, file_id)
VALUES ($1, $2, $3) VALUES ($1, $2)
ON CONFLICT DO NOTHING ON CONFLICT DO NOTHING
` `
type AddTagToFileParams struct { type AddTagToFileParams struct {
ID string `json:"id"`
TagID sql.NullString `json:"tag_id"` TagID sql.NullString `json:"tag_id"`
FileID sql.NullString `json:"file_id"` FileID sql.NullString `json:"file_id"`
} }
func (q *Queries) AddTagToFile(ctx context.Context, arg AddTagToFileParams) error { func (q *Queries) AddTagToFile(ctx context.Context, arg AddTagToFileParams) error {
_, err := q.db.ExecContext(ctx, addTagToFile, arg.ID, arg.TagID, arg.FileID) _, err := q.db.ExecContext(ctx, addTagToFile, arg.TagID, arg.FileID)
return err return err
} }
const createTag = `-- name: CreateTag :one const createTag = `-- name: CreateTag :one
INSERT INTO tags (id, tag_name, tag_type) INSERT INTO tags (tag_name, tag_type)
VALUES ($1, $2, $3) VALUES ($1, $2)
RETURNING id, parent_tag_id, tag_name, tag_type, created_at, updated_at RETURNING id, parent_tag_id, tag_name, tag_type, created_at, updated_at
` `
type CreateTagParams struct { type CreateTagParams struct {
ID string `json:"id"`
TagName string `json:"tag_name"` TagName string `json:"tag_name"`
TagType string `json:"tag_type"` TagType string `json:"tag_type"`
} }
func (q *Queries) CreateTag(ctx context.Context, arg CreateTagParams) (Tag, error) { func (q *Queries) CreateTag(ctx context.Context, arg CreateTagParams) (Tag, error) {
row := q.db.QueryRowContext(ctx, createTag, arg.ID, arg.TagName, arg.TagType) row := q.db.QueryRowContext(ctx, createTag, arg.TagName, arg.TagType)
var i Tag var i Tag
err := row.Scan( err := row.Scan(
&i.ID, &i.ID,
+1 -8
View File
@@ -45,11 +45,9 @@ func (s *FileService) Upload(file *multipart.FileHeader) (*model.UploadResult, e
return nil, fmt.Errorf("stat file: %w", err) return nil, fmt.Errorf("stat file: %w", err)
} }
id := uuid.New().String()
checksum := hex.EncodeToString(CreateSHA256Hash(data)) checksum := hex.EncodeToString(CreateSHA256Hash(data))
dbFile, err := s.queries.CreateFile(context.Background(), db.CreateFileParams{ dbFile, err := s.queries.CreateFile(context.Background(), db.CreateFileParams{
ID: id,
Name: file.Filename, Name: file.Filename,
MimeType: file.Header.Get("Content-Type"), MimeType: file.Header.Get("Content-Type"),
Size: info.Size(), Size: info.Size(),
@@ -127,7 +125,6 @@ func (s *FileService) AddTags(fileID string, tagNames []string, tagType string)
tag, err := s.queries.GetTagByName(context.Background(), name) tag, err := s.queries.GetTagByName(context.Background(), name)
if err == sql.ErrNoRows { if err == sql.ErrNoRows {
tag, err = s.queries.CreateTag(context.Background(), db.CreateTagParams{ tag, err = s.queries.CreateTag(context.Background(), db.CreateTagParams{
ID: uuid.New().String(),
TagName: name, TagName: name,
TagType: tagType, TagType: tagType,
}) })
@@ -139,7 +136,6 @@ func (s *FileService) AddTags(fileID string, tagNames []string, tagType string)
} }
err = s.queries.AddTagToFile(context.Background(), db.AddTagToFileParams{ err = s.queries.AddTagToFile(context.Background(), db.AddTagToFileParams{
ID: uuid.New().String(),
TagID: sql.NullString{String: tag.ID, Valid: true}, TagID: sql.NullString{String: tag.ID, Valid: true},
FileID: sql.NullString{String: fileID, Valid: true}, FileID: sql.NullString{String: fileID, Valid: true},
}) })
@@ -174,10 +170,7 @@ func (s *FileService) MoveFiles(fileIDs []string, parentFileID *string) error {
} }
func (s *FileService) CreateFolder(name string) (*model.File, error) { func (s *FileService) CreateFolder(name string) (*model.File, error) {
f, err := s.queries.CreateFolder(context.Background(), db.CreateFolderParams{ f, err := s.queries.CreateFolder(context.Background(), name)
ID: uuid.New().String(),
Name: name,
})
if err != nil { if err != nil {
return nil, fmt.Errorf("create folder: %w", err) return nil, fmt.Errorf("create folder: %w", err)
} }