refactor: drop depth field

This commit is contained in:
divyam234 2024-07-27 23:42:16 +05:30
parent 2265ec72e3
commit 72db50208a
5 changed files with 65 additions and 6 deletions

View file

@ -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

View file

@ -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"`

View file

@ -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",

View file

@ -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 {

View file

@ -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",