Skip to content

Commit 990d56a

Browse files
Handle falsy check, correctly clean up file storage
1 parent 4d76165 commit 990d56a

3 files changed

Lines changed: 42 additions & 16 deletions

File tree

apps/sim/background/cleanup-soft-deletes.ts

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
DEFAULT_MAX_BATCHES_PER_TABLE,
2323
} from '@/lib/cleanup/batch-delete'
2424
import { prepareChatCleanup } from '@/lib/cleanup/chat-cleanup'
25+
import type { StorageContext } from '@/lib/uploads'
2526
import { isUsingCloudStorage, StorageService } from '@/lib/uploads'
2627

2728
const logger = createLogger('CleanupSoftDeletes')
@@ -34,26 +35,46 @@ async function cleanupWorkspaceFileStorage(
3435

3536
if (!isUsingCloudStorage() || workspaceIds.length === 0) return stats
3637

37-
const filesToDelete = await db
38-
.select({ key: workspaceFiles.key })
39-
.from(workspaceFiles)
40-
.where(
41-
and(
42-
inArray(workspaceFiles.workspaceId, workspaceIds),
43-
isNotNull(workspaceFiles.deletedAt),
44-
lt(workspaceFiles.deletedAt, retentionDate)
38+
const limit = DEFAULT_BATCH_SIZE * DEFAULT_MAX_BATCHES_PER_TABLE
39+
40+
const [legacyFiles, multiContextFiles] = await Promise.all([
41+
db
42+
.select({ key: workspaceFile.key })
43+
.from(workspaceFile)
44+
.where(
45+
and(
46+
inArray(workspaceFile.workspaceId, workspaceIds),
47+
isNotNull(workspaceFile.deletedAt),
48+
lt(workspaceFile.deletedAt, retentionDate)
49+
)
4550
)
46-
)
47-
.limit(DEFAULT_BATCH_SIZE * DEFAULT_MAX_BATCHES_PER_TABLE)
51+
.limit(limit),
52+
db
53+
.select({ key: workspaceFiles.key, context: workspaceFiles.context })
54+
.from(workspaceFiles)
55+
.where(
56+
and(
57+
inArray(workspaceFiles.workspaceId, workspaceIds),
58+
isNotNull(workspaceFiles.deletedAt),
59+
lt(workspaceFiles.deletedAt, retentionDate)
60+
)
61+
)
62+
.limit(limit),
63+
])
64+
65+
const toDelete: Array<{ key: string; context: StorageContext }> = [
66+
...legacyFiles.map((f) => ({ key: f.key, context: 'workspace' as StorageContext })),
67+
...multiContextFiles.map((f) => ({ key: f.key, context: f.context as StorageContext })),
68+
]
4869

4970
await Promise.all(
50-
filesToDelete.map(async (file) => {
71+
toDelete.map(async ({ key, context }) => {
5172
try {
52-
await StorageService.deleteFile({ key: file.key, context: 'workspace' })
73+
await StorageService.deleteFile({ key, context })
5374
stats.filesDeleted++
5475
} catch (error) {
5576
stats.filesFailed++
56-
logger.error(`Failed to delete storage file ${file.key}:`, { error })
77+
logger.error(`Failed to delete storage file ${key} (context: ${context}):`, { error })
5778
}
5879
})
5980
)

apps/sim/ee/data-retention/components/data-retention-settings.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ interface RetentionFieldProps {
4545
}
4646

4747
function RetentionField({ label, description, value, onChange, disabled }: RetentionFieldProps) {
48+
const standard = DAY_OPTIONS.find((o) => o.value === value)
49+
const options = standard
50+
? DAY_OPTIONS
51+
: [...DAY_OPTIONS, { value, label: `${value} days (custom)` } as const]
52+
4853
return (
4954
<div className='flex items-center justify-between py-2'>
5055
<div className='flex flex-col gap-0.5'>
@@ -53,15 +58,15 @@ function RetentionField({ label, description, value, onChange, disabled }: Reten
5358
</div>
5459
{disabled ? (
5560
<span className='text-[13px] text-[var(--text-muted)]'>
56-
{DAY_OPTIONS.find((o) => o.value === value)?.label ?? `${value} days`}
61+
{standard?.label ?? `${value} days`}
5762
</span>
5863
) : (
5964
<Select value={value} onValueChange={onChange}>
6065
<SelectTrigger className='w-[140px] text-[13px]'>
6166
<SelectValue />
6267
</SelectTrigger>
6368
<SelectContent>
64-
{DAY_OPTIONS.map((opt) => (
69+
{options.map((opt) => (
6570
<SelectItem key={opt.value} value={opt.value} className='text-[13px]'>
6671
{opt.label}
6772
</SelectItem>

apps/sim/lib/billing/cleanup-dispatcher.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ export async function resolveCleanupScope(
126126
.where(eq(workspace.id, payload.workspaceId))
127127
.limit(1)
128128

129-
if (!ws?.hours) return null
129+
if (ws?.hours == null) return null
130130

131131
return {
132132
workspaceIds: [payload.workspaceId],

0 commit comments

Comments
 (0)