mirror of
https://github.com/tgdrive/teldrive.git
synced 2024-11-10 09:02:52 +08:00
refactor: Update ParentID field to use sql.NullString for nullable values
This commit is contained in:
parent
149d8c6140
commit
2a7cedb6fb
6 changed files with 31 additions and 8 deletions
8
internal/database/migrations/20240803003234_index.sql
Normal file
8
internal/database/migrations/20240803003234_index.sql
Normal 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
|
|
@ -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,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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())"`
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -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 {
|
||||||
|
@ -152,7 +160,10 @@ 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
|
||||||
|
|
|
@ -31,7 +31,6 @@ func (s *FileServiceSuite) SetupTest() {
|
||||||
MimeType: "drive/folder",
|
MimeType: "drive/folder",
|
||||||
UserID: 123456,
|
UserID: 123456,
|
||||||
Status: "active",
|
Status: "active",
|
||||||
ParentID: "root",
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue