From 72db50208a7abd6bd2ebb15333eb47ae1efe7b05 Mon Sep 17 00:00:00 2001 From: divyam234 <47589864+divyam234@users.noreply.github.com> Date: Sat, 27 Jul 2024 23:42:16 +0530 Subject: [PATCH] refactor: drop depth field --- .../migrations/20240727233913_modify.sql | 65 +++++++++++++++++++ pkg/models/file.go | 1 - pkg/services/auth.go | 2 - pkg/services/file.go | 1 - pkg/services/file_test.go | 2 - 5 files changed, 65 insertions(+), 6 deletions(-) create mode 100644 internal/database/migrations/20240727233913_modify.sql diff --git a/internal/database/migrations/20240727233913_modify.sql b/internal/database/migrations/20240727233913_modify.sql new file mode 100644 index 0000000..326f1fc --- /dev/null +++ b/internal/database/migrations/20240727233913_modify.sql @@ -0,0 +1,65 @@ +-- +goose Up +-- +goose StatementBegin +ALTER TABLE teldrive.files DROP COLUMN depth; + +CREATE OR REPLACE PROCEDURE teldrive.update_size() +LANGUAGE plpgsql +AS $procedure$ +begin + + WITH RECURSIVE folder_hierarchy AS ( + + SELECT + id, + name, + parent_id, + ARRAY[id] AS path + FROM + teldrive.files + WHERE + type = 'folder' AND parent_id = 'root' + + UNION ALL + + SELECT + f.id, + f.name, + f.parent_id, + fh.path || f.id + FROM + teldrive.files f + JOIN + folder_hierarchy fh ON f.parent_id = fh.id + WHERE + f.type = 'folder' + ) + , folder_sizes AS ( + SELECT + f.id, + f.path, + COALESCE(SUM(CASE WHEN c.type != 'folder' THEN c.size ELSE 0 END), 0) AS direct_size + FROM + folder_hierarchy f + LEFT JOIN + teldrive.files c ON f.id = c.parent_id + GROUP BY + f.id, f.path + ) + , cumulative_sizes AS ( + SELECT + fs.id, + SUM(fs2.direct_size) AS total_size + FROM + folder_sizes fs + JOIN + folder_sizes fs2 ON fs2.path @> fs.path + GROUP BY + fs.id + ) + UPDATE teldrive.files f + SET size = cs.total_size + FROM cumulative_sizes cs + WHERE f.id = cs.id AND f.type = 'folder'; +END; +$procedure$; +-- +goose StatementEnd \ No newline at end of file diff --git a/pkg/models/file.go b/pkg/models/file.go index 69fbf92..e8b700b 100644 --- a/pkg/models/file.go +++ b/pkg/models/file.go @@ -14,7 +14,6 @@ type File struct { MimeType string `gorm:"type:text;not null"` Size *int64 `gorm:"type:bigint"` Starred bool `gorm:"default:false"` - Depth *int `gorm:"type:integer"` Category string `gorm:"type:text"` Encrypted bool `gorm:"default:false"` UserID int64 `gorm:"type:bigint;not null"` diff --git a/pkg/services/auth.go b/pkg/services/auth.go index 39c6c2f..ed38db0 100644 --- a/pkg/services/auth.go +++ b/pkg/services/auth.go @@ -20,7 +20,6 @@ import ( "github.com/divyam234/teldrive/internal/cache" "github.com/divyam234/teldrive/internal/config" "github.com/divyam234/teldrive/internal/tgc" - "github.com/divyam234/teldrive/internal/utils" "github.com/divyam234/teldrive/pkg/models" "github.com/divyam234/teldrive/pkg/schemas" "github.com/divyam234/teldrive/pkg/types" @@ -93,7 +92,6 @@ func (as *AuthService) LogIn(c *gin.Context, session *schemas.TgSession) (*schem Name: "root", Type: "folder", MimeType: "drive/folder", - Depth: utils.IntPointer(0), UserID: session.UserID, Status: "active", ParentID: "root", diff --git a/pkg/services/file.go b/pkg/services/file.go index cfd6661..54e85af 100644 --- a/pkg/services/file.go +++ b/pkg/services/file.go @@ -110,7 +110,6 @@ func (fs *FileService) CreateFile(c *gin.Context, userId int64, fileIn *schemas. if fileIn.Type == "folder" { fileDB.MimeType = "drive/folder" - fileDB.Depth = utils.IntPointer(*parent.Depth + 1) } else if fileIn.Type == "file" { channelId := fileIn.ChannelID if fileIn.ChannelID == 0 { diff --git a/pkg/services/file_test.go b/pkg/services/file_test.go index cf4f645..ce000a5 100644 --- a/pkg/services/file_test.go +++ b/pkg/services/file_test.go @@ -6,7 +6,6 @@ import ( "github.com/divyam234/teldrive/internal/database" "github.com/gin-gonic/gin" - "github.com/divyam234/teldrive/internal/utils" "github.com/divyam234/teldrive/pkg/models" "github.com/divyam234/teldrive/pkg/schemas" "github.com/stretchr/testify/suite" @@ -30,7 +29,6 @@ func (s *FileServiceSuite) SetupTest() { Name: "root", Type: "folder", MimeType: "drive/folder", - Depth: utils.IntPointer(0), UserID: 123456, Status: "active", ParentID: "root",