[server][core] Maintain separate trash table

This commit is contained in:
Abhishek Shroff
2025-04-10 17:27:18 +05:30
parent dd5bbe3688
commit 683f511a78
6 changed files with 35 additions and 13 deletions

View File

@@ -35,7 +35,11 @@ func setupDeleteCommand() *cobra.Command {
}
}
r.DeleteRecursive(false)
if _, err := r.DeleteRecursive(false); err != nil {
fmt.Println("cannot remove '" + id.String() + "': " + err.Error())
os.Exit(1)
}
}
},
}

View File

@@ -32,7 +32,7 @@ func setupListCommand() *cobra.Command {
}
},
}
cmd.Flags().IntP("num", "n", 5, "How many items to show")
cmd.Flags().UintP("num", "n", 5, "How many items to show")
cmd.Flags().StringP("cursor", "c", "", "Pagination Cursor")
return &cmd
}

View File

@@ -1,7 +0,0 @@
CREATE VIEW linked_resources AS
SELECT r.*, (SELECT jsonb_agg(l.name) FROM publinks l WHERE l.root = r.id AND l.deleted IS NULL) AS links
FROM resources r;
---- create above / drop below ----
DROP VIEW linked_resources;

View File

@@ -0,0 +1,7 @@
CREATE TABLE trash(
id UUID PRIMARY KEY REFERENCES resources ON DELETE CASCADE
);
---- create above / drop below ----
DROP TABLE trash;

View File

@@ -123,6 +123,10 @@ func (r Resource) RestoreDeleted(parentPathOrUUID string, name string, autoRenam
if del, err := f.markNotDeleted(r.id); err != nil {
return err
} else {
q, args, _ := pg.Delete(goqu.T("trash")).Where(goqu.C("id").Eq(r.id)).ToSQL()
if _, err := f.db.Exec(q, args...); err != nil {
return err
}
count = len(del)
for _, l := range del {
size += l
@@ -143,6 +147,19 @@ func (f filesystem) deleteRecursive(id, parent uuid.UUID, softDelete, preserveRo
if _, ids, err = f.markDeleted(id, softDelete, preserveRoot); err != nil {
return err
}
if softDelete {
// Add to trash
insert := pg.Insert(goqu.T("trash")).Cols("id")
if preserveRoot {
insert = insert.FromQuery(pg.From("resources").Select("id").Where(goqu.C("parent").Eq(id)))
} else {
insert = insert.Vals(goqu.Vals{id})
}
q, args, _ := insert.ToSQL()
if _, err := f.db.Exec(q, args...); err != nil {
return err
}
}
return f.updateResourceModified(parent)
})

View File

@@ -13,10 +13,11 @@ import (
func (f filesystem) TrashList(cursor string, n uint) ([]Resource, string, error) {
l := goqu.T("publinks").As("l")
t := goqu.T("trash")
r := goqu.T("resources")
q := pg.From(r).
Select(r.All(), pg.Select(goqu.L("jsonb_agg(?)", l.Col("name"))).From(l).Where(l.Col("root").Eq(r.Col("id")), l.Col("deleted").IsNull())).
Where(goqu.C("deleted").IsNotNull())
q := pg.From(t).
Join(r, goqu.On(t.Col("id").Eq(r.Col("id")))).
Select(r.All(), pg.Select(goqu.L("jsonb_agg(?)", l.Col("name"))).From(l).Where(l.Col("root").Eq(r.Col("id")), l.Col("deleted").IsNull()))
if !f.fullAccess {
q = q.Where(goqu.L("permissions[?]::INTEGER <> 0", f.username))
}
@@ -41,7 +42,7 @@ func (f filesystem) TrashList(cursor string, n uint) ([]Resource, string, error)
query, params, _ := q.
Order(goqu.C("deleted").Desc()).
OrderAppend(goqu.C("id").Desc()).
OrderAppend(r.Col("id").Desc()).
Limit(n).
ToSQL()