refactor: Update ParentID field to use sql.NullString for nullable values

This commit is contained in:
divyam234 2024-08-03 00:36:14 +05:30
parent 149d8c6140
commit 2a7cedb6fb
6 changed files with 31 additions and 8 deletions

View file

@ -0,0 +1,8 @@
-- +goose Up
-- +goose StatementBegin
DROP INDEX IF EXISTS teldrive.idx_files_unique_folder;
CREATE UNIQUE INDEX idx_files_unique_folder
ON teldrive.files
USING btree (name, COALESCE(parent_id, '00000000-0000-0000-0000-000000000000'::uuid), user_id)
WHERE (type = 'folder'::text);
-- +goose StatementEnd

View file

@ -19,7 +19,7 @@ func ToFileOut(file models.File) *schemas.FileOut {
Encrypted: file.Encrypted,
Size: size,
Starred: file.Starred,
ParentID: file.ParentID,
ParentID: file.ParentID.String,
UpdatedAt: file.UpdatedAt,
}
}

View file

@ -1,6 +1,7 @@
package models
import (
"database/sql"
"time"
"github.com/divyam234/teldrive/pkg/schemas"
@ -18,7 +19,7 @@ type File struct {
Encrypted bool `gorm:"default:false"`
UserID int64 `gorm:"type:bigint;not null"`
Status string `gorm:"type:text"`
ParentID string `gorm:"type:uuid;index"`
ParentID sql.NullString `gorm:"type:uuid;index"`
Parts datatypes.JSONSlice[schemas.Part] `gorm:"type:jsonb"`
ChannelID *int64 `gorm:"type:bigint"`
CreatedAt time.Time `gorm:"default:timezone('utc'::text, now())"`

View file

@ -94,6 +94,7 @@ func (as *AuthService) LogIn(c *gin.Context, session *schemas.TgSession) (*schem
MimeType: "drive/folder",
UserID: session.UserID,
Status: "active",
Parts: nil,
}
if err := as.db.Clauses(clause.OnConflict{DoNothing: true}).Create(file).Error; err != nil {
return err

View file

@ -3,6 +3,7 @@ package services
import (
"context"
"crypto/rand"
"database/sql"
"encoding/binary"
"errors"
"fmt"
@ -100,9 +101,15 @@ func (fs *FileService) CreateFile(c *gin.Context, userId int64, fileIn *schemas.
if err != nil {
return nil, &types.AppError{Error: err, Code: http.StatusNotFound}
}
fileDB.ParentID = parent.Id
fileDB.ParentID = sql.NullString{
String: parent.Id,
Valid: true,
}
} else if fileIn.ParentID != "" {
fileDB.ParentID = fileIn.ParentID
fileDB.ParentID = sql.NullString{
String: fileIn.ParentID,
Valid: true,
}
} else {
return nil, &types.AppError{Error: fmt.Errorf("parent id or path is required"), Code: http.StatusBadRequest}
@ -110,6 +117,7 @@ func (fs *FileService) CreateFile(c *gin.Context, userId int64, fileIn *schemas.
if fileIn.Type == "folder" {
fileDB.MimeType = "drive/folder"
fileDB.Parts = nil
} else if fileIn.Type == "file" {
channelId := fileIn.ChannelID
if fileIn.ChannelID == 0 {
@ -151,8 +159,11 @@ func (fs *FileService) UpdateFile(id string, userId int64, update *schemas.FileU
)
updateDb := models.File{
Name: update.Name,
ParentID: update.ParentID,
Name: update.Name,
ParentID: sql.NullString{
String: update.ParentID,
Valid: true,
},
UpdatedAt: update.UpdatedAt,
Size: update.Size,
CreatedAt: update.CreatedAt,
@ -576,7 +587,10 @@ func (fs *FileService) CopyFile(c *gin.Context) (*schemas.FileOut, *types.AppErr
dbFile.UserID = userId
dbFile.Starred = false
dbFile.Status = "active"
dbFile.ParentID = dest.Id
dbFile.ParentID = sql.NullString{
String: dest.Id,
Valid: true,
}
dbFile.ChannelID = &channelId
dbFile.Encrypted = file.Encrypted
dbFile.Category = file.Category

View file

@ -31,7 +31,6 @@ func (s *FileServiceSuite) SetupTest() {
MimeType: "drive/folder",
UserID: 123456,
Status: "active",
ParentID: "root",
})
}