Skip to content

A continuation of HomeBox the inventory and organization system built for the Home User

fix: foreign key issue with thumbnails (#824)

Mattf30c9d915650
diff --git a/Taskfile.yml b/Taskfile.yml
index e63b37ee..f6c8fc5d 100644
--- a/Taskfile.yml
+++ b/Taskfile.yml
@@ -176,7 +176,7 @@ tasks:
cmds:
- cd backend && go build ./app/api
- backend/api &
- - sleep 10
+ - sleep 15
- cd frontend && pnpm run test:ci
silent: true
@@ -193,7 +193,7 @@ tasks:
cmds:
- cd backend && go build ./app/api
- backend/api &
- - sleep 10
+ - sleep 15
- cd frontend && pnpm run test:ci
silent: true
diff --git a/backend/go.sum b/backend/go.sum
index 29ecd521..68680caa 100644
--- a/backend/go.sum
+++ b/backend/go.sum
@@ -350,8 +350,6 @@ github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
-github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0=
-github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/mattn/go-sqlite3 v1.14.28 h1:ThEiQrnbtumT+QMknw63Befp/ce/nUPgBPMlRFEum7A=
github.com/mattn/go-sqlite3 v1.14.28/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
github.com/mfridman/interpolate v0.0.2 h1:pnuTK7MQIxxFz1Gr+rjSIx9u7qVjf5VOoM/u6BbAxPY=
@@ -375,8 +373,6 @@ github.com/ncruces/go-strftime v0.1.9/go.mod h1:Fwc5htZGVVkseilnfgOVb9mKy6w1naJm
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/olahol/melody v1.2.1 h1:xdwRkzHxf+B0w4TKbGpUSSkV516ZucQZJIWLztOWICQ=
github.com/olahol/melody v1.2.1/go.mod h1:GgkTl6Y7yWj/HtfD48Q5vLKPVoZOH+Qqgfa7CvJgJM4=
-github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
-github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=
github.com/onsi/ginkgo/v2 v2.9.2 h1:BA2GMJOtfGAfagzYtrAlufIP0lq6QERkFmHLMLPwFSU=
github.com/onsi/ginkgo/v2 v2.9.2/go.mod h1:WHcJJG2dIlcCqVfBAwUCrJxSPFb6v4azBwgxeMeDuts=
github.com/onsi/gomega v1.27.6 h1:ENqfyGeS5AX/rlXDd/ETokDz93u0YufY1Pgxuy/PvWE=
@@ -417,10 +413,6 @@ github.com/shirou/gopsutil/v4 v4.25.5 h1:rtd9piuSMGeU8g1RMXjZs9y9luK5BwtnG7dZaQU
github.com/shirou/gopsutil/v4 v4.25.5/go.mod h1:PfybzyydfZcN+JMMjkF6Zb8Mq1A/VcogFFg7hj50W9c=
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e h1:MRM5ITcdelLK2j1vwZ3Je0FKVCfqOLp5zO6trqMLYs0=
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e/go.mod h1:XV66xRDqSt+GTGFMVlhk3ULuV0y9ZmzeVGR4mloJI3M=
-github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I=
-github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0=
-github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
-github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
diff --git a/backend/internal/core/services/service_items_attachments.go b/backend/internal/core/services/service_items_attachments.go
index 95444002..cef45539 100644
--- a/backend/internal/core/services/service_items_attachments.go
+++ b/backend/internal/core/services/service_items_attachments.go
@@ -55,7 +55,7 @@ func (svc *ItemService) AttachmentAdd(ctx Context, itemID uuid.UUID, filename st
return svc.repo.Items.GetOneByGroup(ctx, ctx.GID, itemID)
}
-func (svc *ItemService) AttachmentDelete(ctx context.Context, gid, itemID, attachmentID uuid.UUID) error {
+func (svc *ItemService) AttachmentDelete(ctx context.Context, gid uuid.UUID, id uuid.UUID, attachmentID uuid.UUID) error {
// Delete the attachment
err := svc.repo.Attachments.Delete(ctx, attachmentID)
if err != nil {
diff --git a/backend/internal/data/migrations/postgres/20250629112901_fix_thumbnail_foriegn.sql b/backend/internal/data/migrations/postgres/20250629112901_fix_thumbnail_foriegn.sql
new file mode 100644
index 00000000..9f8aa5cb
--- /dev/null
+++ b/backend/internal/data/migrations/postgres/20250629112901_fix_thumbnail_foriegn.sql
@@ -0,0 +1,8 @@
+-- +goose Up
+alter table public.attachments
+ drop constraint attachments_attachments_thumbnail;
+
+alter table public.attachments
+ add constraint attachments_attachments_thumbnail
+ foreign key (attachment_thumbnail) references public.attachments
+ on delete set null;
\ No newline at end of file
diff --git a/backend/internal/data/migrations/sqlite3/20250629112902_fix_thumbnail_foriegn.sql b/backend/internal/data/migrations/sqlite3/20250629112902_fix_thumbnail_foriegn.sql
new file mode 100644
index 00000000..3e4004f2
--- /dev/null
+++ b/backend/internal/data/migrations/sqlite3/20250629112902_fix_thumbnail_foriegn.sql
@@ -0,0 +1,45 @@
+-- +goose Up
+create table attachments_dg_tmp
+(
+ id uuid not null
+ primary key,
+ created_at datetime not null,
+ updated_at datetime not null,
+ type text default 'attachment' not null,
+ "primary" bool default false not null,
+ path text not null,
+ title text not null,
+ mime_type text default 'application/octet-stream' not null,
+ item_attachments uuid
+ constraint attachments_items_attachments
+ references items
+ on delete cascade,
+ attachment_thumbnail uuid
+ constraint attachments_attachments_thumbnail
+ references attachments
+ on delete set null
+);
+
+insert into attachments_dg_tmp(id, created_at, updated_at, type, "primary", path, title, mime_type, item_attachments,
+ attachment_thumbnail)
+select id,
+ created_at,
+ updated_at,
+ type,
+ "primary",
+ path,
+ title,
+ mime_type,
+ item_attachments,
+ attachment_thumbnail
+from attachments;
+
+drop table attachments;
+
+alter table attachments_dg_tmp
+ rename to attachments;
+
+CREATE INDEX IF NOT EXISTS idx_attachments_item_id ON attachments(item_attachments);
+CREATE INDEX IF NOT EXISTS idx_attachments_path ON attachments(path);
+CREATE INDEX IF NOT EXISTS idx_attachments_type ON attachments(type);
+CREATE INDEX IF NOT EXISTS idx_attachments_thumbnail ON attachments(attachment_thumbnail);
diff --git a/backend/internal/data/repo/repo_item_attachments.go b/backend/internal/data/repo/repo_item_attachments.go
index 6705c24d..12336f05 100644
--- a/backend/internal/data/repo/repo_item_attachments.go
+++ b/backend/internal/data/repo/repo_item_attachments.go
@@ -316,6 +316,28 @@ func (r *AttachmentRepo) Delete(ctx context.Context, id uuid.UUID) error {
}
// If this is the last attachment for this path, delete the file
if len(all) == 1 {
+ thumb, err := doc.QueryThumbnail().First(ctx)
+ if err != nil && !ent.IsNotFound(err) {
+ log.Err(err).Msg("failed to query thumbnail for attachment")
+ return err
+ }
+ if thumb != nil {
+ thumbBucket, err := blob.OpenBucket(ctx, r.GetConnString())
+ if err != nil {
+ log.Err(err).Msg("failed to open bucket for thumbnail deletion")
+ return err
+ }
+ err = thumbBucket.Delete(ctx, thumb.Path)
+ if err != nil {
+ return err
+ }
+ _ = doc.Update().SetNillableThumbnailID(nil).SaveX(ctx)
+ _ = thumb.Update().SetNillableThumbnailID(nil).SaveX(ctx)
+ err = r.db.Attachment.DeleteOneID(thumb.ID).Exec(ctx)
+ if err != nil {
+ return err
+ }
+ }
bucket, err := blob.OpenBucket(ctx, r.GetConnString())
if err != nil {
log.Err(err).Msg("failed to open bucket")
diff --git a/frontend/composables/use-location-helpers.ts b/frontend/composables/use-location-helpers.ts
index f429fe06..d6610800 100644
--- a/frontend/composables/use-location-helpers.ts
+++ b/frontend/composables/use-location-helpers.ts
@@ -57,7 +57,7 @@ function filterOutSubtree(tree: TreeItem[], excludeId: string): TreeItem[] {
export function useFlatLocations(excludeSubtreeForLocation?: LocationSummary): Ref<FlatTreeItem[]> {
const locations = useLocationStore();
-
+
if (locations.tree === null) {
locations.refreshTree();
}
diff --git a/frontend/pages/items.vue b/frontend/pages/items.vue
index 6370d50a..fc07b508 100644
--- a/frontend/pages/items.vue
+++ b/frontend/pages/items.vue
@@ -509,11 +509,7 @@
<MdiSelectSearch class="size-10" />
<p>{{ $t("items.no_results") }}</p>
</div>
- <div
- v-else
- ref="cardgrid"
- class="mt-4 grid grid-cols-1 gap-4 sm:grid-cols-2 md:grid-cols-3 lg:grid-cols-4"
- >
+ <div v-else ref="cardgrid" class="mt-4 grid grid-cols-1 gap-4 sm:grid-cols-2 md:grid-cols-3 lg:grid-cols-4">
<ItemCard v-for="item in items" :key="item.id" :item="item" :location-flat-tree="locationFlatTree" />
</div>
<Pagination