Fix resource deletion

This commit is contained in:
Abhishek Shroff
2024-03-05 12:50:56 +05:30
parent ebfcbf095a
commit 00b7586fcb
6 changed files with 73 additions and 6 deletions
+44
View File
@@ -124,6 +124,50 @@ func (q *Queries) FindRoot(ctx context.Context, name string) (Resource, error) {
return i, err
}
const hardDeleteRecursive = `-- name: HardDeleteRecursive :many
WITH RECURSIVE nodes(id, parent) AS (
SELECT r.id, r.parent
FROM resources r WHERE r.id = $1::uuid
UNION ALL
SELECT r.id, r.parent
FROM resources r JOIN nodes n on r.parent = n.id
WHERE deleted IS NULL
)
DELETE FROM resources
WHERE id in (SELECT id FROM nodes)
RETURNING id, parent, name, dir, created, modified, deleted, size, etag
`
func (q *Queries) HardDeleteRecursive(ctx context.Context, id uuid.UUID) ([]Resource, error) {
rows, err := q.db.Query(ctx, hardDeleteRecursive, id)
if err != nil {
return nil, err
}
defer rows.Close()
var items []Resource
for rows.Next() {
var i Resource
if err := rows.Scan(
&i.ID,
&i.Parent,
&i.Name,
&i.Dir,
&i.Created,
&i.Modified,
&i.Deleted,
&i.Size,
&i.Etag,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const readDir = `-- name: ReadDir :many
WITH RECURSIVE nodes(id, parent, name, dir, created, modified, size, etag, depth, path) AS (
SELECT r.id, r.parent, r.name, r.dir, r.created, r.modified, r.size, r.etag, 0, ''::text
+1 -1
View File
@@ -52,7 +52,7 @@ func (l localStorage) Create(id uuid.UUID, callback func(hash.Hash, int, error))
}
func (l localStorage) Delete(id uuid.UUID) error {
return os.Remove(filepath.Join(string(l), id.String()))
return os.Remove(l.path(id))
}
func (l localStorage) path(id uuid.UUID) string {
+13 -3
View File
@@ -29,9 +29,19 @@ func (fs Vfs) ReadDir(ctx context.Context, id uuid.UUID, recursive bool) ([]sql.
return fs.queries.ReadDir(ctx, sql.ReadDirParams{ID: id, Recursive: recursive})
}
func (fs Vfs) DeleteRecursive(ctx context.Context, id uuid.UUID) error {
_, err := fs.queries.DeleteRecursive(ctx, id)
// TODO: Delete Contents
func (fs Vfs) DeleteRecursive(ctx context.Context, id uuid.UUID, hardDelete bool) error {
query := fs.queries.DeleteRecursive
if hardDelete {
query = fs.queries.HardDeleteRecursive
}
deleted, err := query(ctx, id)
if err == nil && hardDelete {
for _, res := range deleted {
if !res.Dir {
fs.cs.Delete(res.ID)
}
}
}
return err
}
+1 -1
View File
@@ -44,7 +44,7 @@ func (b VfsManager) CreateVfs(ctx context.Context, id uuid.UUID, name string) er
func (b VfsManager) DeleteVfs(ctx context.Context, name string) error {
if fs, err := b.GetVfs(ctx, name); err == nil {
return fs.DeleteRecursive(ctx, fs.root)
return fs.DeleteRecursive(ctx, fs.root, true)
} else {
return fmt.Errorf("root directory does not exist: %s", name)
}
+1 -1
View File
@@ -94,7 +94,7 @@ func (p adapter) RemoveAll(ctx context.Context, name string) error {
if err != nil {
return iofs.ErrNotExist
}
return p.fs.DeleteRecursive(ctx, resource.ID)
return p.fs.DeleteRecursive(ctx, resource.ID, false)
}
func (p adapter) Mkdir(ctx context.Context, name string) error {
+13
View File
@@ -56,4 +56,17 @@ WITH RECURSIVE nodes(id, parent) AS (
UPDATE resources
SET deleted = NOW()
WHERE id in (SELECT id FROM nodes)
RETURNING *;
-- name: HardDeleteRecursive :many
WITH RECURSIVE nodes(id, parent) AS (
SELECT r.id, r.parent
FROM resources r WHERE r.id = @id::uuid
UNION ALL
SELECT r.id, r.parent
FROM resources r JOIN nodes n on r.parent = n.id
WHERE deleted IS NULL
)
DELETE FROM resources
WHERE id in (SELECT id FROM nodes)
RETURNING *;