replace go uuid generation by postgresql
This commit is contained in:
@@ -13,13 +13,12 @@ import (
|
||||
)
|
||||
|
||||
const createFile = `-- name: CreateFile :one
|
||||
INSERT INTO files (id, name, mime_type, size, storage_key, checksum, created_at, updated_at)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)
|
||||
INSERT INTO files (name, mime_type, size, storage_key, checksum, created_at, updated_at)
|
||||
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
|
||||
`
|
||||
|
||||
type CreateFileParams struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
MimeType string `json:"mime_type"`
|
||||
Size int64 `json:"size"`
|
||||
@@ -29,7 +28,6 @@ type CreateFileParams struct {
|
||||
|
||||
func (q *Queries) CreateFile(ctx context.Context, arg CreateFileParams) (File, error) {
|
||||
row := q.db.QueryRowContext(ctx, createFile,
|
||||
arg.ID,
|
||||
arg.Name,
|
||||
arg.MimeType,
|
||||
arg.Size,
|
||||
@@ -54,18 +52,13 @@ func (q *Queries) CreateFile(ctx context.Context, arg CreateFileParams) (File, e
|
||||
}
|
||||
|
||||
const createFolder = `-- name: CreateFolder :one
|
||||
INSERT INTO files (id, name, is_folder, created_at, updated_at)
|
||||
VALUES ($1, $2, true, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)
|
||||
INSERT INTO files (name, is_folder, created_at, updated_at)
|
||||
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
|
||||
`
|
||||
|
||||
type CreateFolderParams struct {
|
||||
ID string `json:"id"`
|
||||
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)
|
||||
func (q *Queries) CreateFolder(ctx context.Context, name string) (File, error) {
|
||||
row := q.db.QueryRowContext(ctx, createFolder, name)
|
||||
var i File
|
||||
err := row.Scan(
|
||||
&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 '';
|
||||
@@ -18,13 +18,13 @@ WHERE id = ANY($1::text[])
|
||||
ORDER BY created_at DESC;
|
||||
|
||||
-- name: CreateFile :one
|
||||
INSERT INTO files (id, name, mime_type, size, storage_key, checksum, created_at, updated_at)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)
|
||||
INSERT INTO files (name, mime_type, size, storage_key, checksum, created_at, updated_at)
|
||||
VALUES ($1, $2, $3, $4, $5, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)
|
||||
RETURNING *;
|
||||
|
||||
-- name: CreateFolder :one
|
||||
INSERT INTO files (id, name, is_folder, created_at, updated_at)
|
||||
VALUES ($1, $2, true, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)
|
||||
INSERT INTO files (name, is_folder, created_at, updated_at)
|
||||
VALUES ($1, true, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)
|
||||
RETURNING *;
|
||||
|
||||
-- name: ListFilesByParentID :many
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
-- name: CreateTag :one
|
||||
INSERT INTO tags (id, tag_name, tag_type)
|
||||
VALUES ($1, $2, $3)
|
||||
INSERT INTO tags (tag_name, tag_type)
|
||||
VALUES ($1, $2)
|
||||
RETURNING *;
|
||||
|
||||
-- name: GetTag :one
|
||||
@@ -20,8 +20,8 @@ DELETE FROM tags
|
||||
WHERE id = $1;
|
||||
|
||||
-- name: AddTagToFile :exec
|
||||
INSERT INTO file_tags (id, tag_id, file_id)
|
||||
VALUES ($1, $2, $3)
|
||||
INSERT INTO file_tags (tag_id, file_id)
|
||||
VALUES ($1, $2)
|
||||
ON CONFLICT DO NOTHING;
|
||||
|
||||
-- name: RemoveTagFromFile :exec
|
||||
|
||||
@@ -11,36 +11,34 @@ import (
|
||||
)
|
||||
|
||||
const addTagToFile = `-- name: AddTagToFile :exec
|
||||
INSERT INTO file_tags (id, tag_id, file_id)
|
||||
VALUES ($1, $2, $3)
|
||||
INSERT INTO file_tags (tag_id, file_id)
|
||||
VALUES ($1, $2)
|
||||
ON CONFLICT DO NOTHING
|
||||
`
|
||||
|
||||
type AddTagToFileParams struct {
|
||||
ID string `json:"id"`
|
||||
TagID sql.NullString `json:"tag_id"`
|
||||
FileID sql.NullString `json:"file_id"`
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
const createTag = `-- name: CreateTag :one
|
||||
INSERT INTO tags (id, tag_name, tag_type)
|
||||
VALUES ($1, $2, $3)
|
||||
INSERT INTO tags (tag_name, tag_type)
|
||||
VALUES ($1, $2)
|
||||
RETURNING id, parent_tag_id, tag_name, tag_type, created_at, updated_at
|
||||
`
|
||||
|
||||
type CreateTagParams struct {
|
||||
ID string `json:"id"`
|
||||
TagName string `json:"tag_name"`
|
||||
TagType string `json:"tag_type"`
|
||||
}
|
||||
|
||||
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
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
|
||||
@@ -45,11 +45,9 @@ func (s *FileService) Upload(file *multipart.FileHeader) (*model.UploadResult, e
|
||||
return nil, fmt.Errorf("stat file: %w", err)
|
||||
}
|
||||
|
||||
id := uuid.New().String()
|
||||
checksum := hex.EncodeToString(CreateSHA256Hash(data))
|
||||
|
||||
dbFile, err := s.queries.CreateFile(context.Background(), db.CreateFileParams{
|
||||
ID: id,
|
||||
Name: file.Filename,
|
||||
MimeType: file.Header.Get("Content-Type"),
|
||||
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)
|
||||
if err == sql.ErrNoRows {
|
||||
tag, err = s.queries.CreateTag(context.Background(), db.CreateTagParams{
|
||||
ID: uuid.New().String(),
|
||||
TagName: name,
|
||||
TagType: tagType,
|
||||
})
|
||||
@@ -139,7 +136,6 @@ func (s *FileService) AddTags(fileID string, tagNames []string, tagType string)
|
||||
}
|
||||
|
||||
err = s.queries.AddTagToFile(context.Background(), db.AddTagToFileParams{
|
||||
ID: uuid.New().String(),
|
||||
TagID: sql.NullString{String: tag.ID, 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) {
|
||||
f, err := s.queries.CreateFolder(context.Background(), db.CreateFolderParams{
|
||||
ID: uuid.New().String(),
|
||||
Name: name,
|
||||
})
|
||||
f, err := s.queries.CreateFolder(context.Background(), name)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("create folder: %w", err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user