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, Encrypted: file.Encrypted,
Size: size, Size: size,
Starred: file.Starred, Starred: file.Starred,
ParentID: file.ParentID, ParentID: file.ParentID.String,
UpdatedAt: file.UpdatedAt, UpdatedAt: file.UpdatedAt,
} }
} }

View file

@ -1,6 +1,7 @@
package models package models
import ( import (
"database/sql"
"time" "time"
"github.com/divyam234/teldrive/pkg/schemas" "github.com/divyam234/teldrive/pkg/schemas"
@ -18,7 +19,7 @@ type File struct {
Encrypted bool `gorm:"default:false"` Encrypted bool `gorm:"default:false"`
UserID int64 `gorm:"type:bigint;not null"` UserID int64 `gorm:"type:bigint;not null"`
Status string `gorm:"type:text"` 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"` Parts datatypes.JSONSlice[schemas.Part] `gorm:"type:jsonb"`
ChannelID *int64 `gorm:"type:bigint"` ChannelID *int64 `gorm:"type:bigint"`
CreatedAt time.Time `gorm:"default:timezone('utc'::text, now())"` 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", MimeType: "drive/folder",
UserID: session.UserID, UserID: session.UserID,
Status: "active", Status: "active",
Parts: nil,
} }
if err := as.db.Clauses(clause.OnConflict{DoNothing: true}).Create(file).Error; err != nil { if err := as.db.Clauses(clause.OnConflict{DoNothing: true}).Create(file).Error; err != nil {
return err return err

View file

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

View file

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