mirror of
https://github.com/beilunyang/moemail.git
synced 2026-09-04 23:06:37 +08:00
Optimize email cleanup and add database indexes
This commit is contained in:
+7
-3
@@ -1,4 +1,4 @@
|
|||||||
import { integer, sqliteTable, text, primaryKey, uniqueIndex } from "drizzle-orm/sqlite-core"
|
import { integer, sqliteTable, text, primaryKey, uniqueIndex, index } from "drizzle-orm/sqlite-core"
|
||||||
import type { AdapterAccountType } from "next-auth/adapters"
|
import type { AdapterAccountType } from "next-auth/adapters"
|
||||||
import { relations } from 'drizzle-orm';
|
import { relations } from 'drizzle-orm';
|
||||||
|
|
||||||
@@ -46,7 +46,9 @@ export const emails = sqliteTable("email", {
|
|||||||
.notNull()
|
.notNull()
|
||||||
.$defaultFn(() => new Date()),
|
.$defaultFn(() => new Date()),
|
||||||
expiresAt: integer("expires_at", { mode: "timestamp_ms" }).notNull(),
|
expiresAt: integer("expires_at", { mode: "timestamp_ms" }).notNull(),
|
||||||
})
|
}, (table) => ({
|
||||||
|
expiresAtIdx: index("email_expires_at_idx").on(table.expiresAt),
|
||||||
|
}))
|
||||||
|
|
||||||
export const messages = sqliteTable("message", {
|
export const messages = sqliteTable("message", {
|
||||||
id: text("id").primaryKey().$defaultFn(() => crypto.randomUUID()),
|
id: text("id").primaryKey().$defaultFn(() => crypto.randomUUID()),
|
||||||
@@ -60,7 +62,9 @@ export const messages = sqliteTable("message", {
|
|||||||
receivedAt: integer("received_at", { mode: "timestamp_ms" })
|
receivedAt: integer("received_at", { mode: "timestamp_ms" })
|
||||||
.notNull()
|
.notNull()
|
||||||
.$defaultFn(() => new Date()),
|
.$defaultFn(() => new Date()),
|
||||||
})
|
}, (table) => ({
|
||||||
|
emailIdIdx: index("message_email_id_idx").on(table.emailId),
|
||||||
|
}))
|
||||||
|
|
||||||
export const webhooks = sqliteTable('webhook', {
|
export const webhooks = sqliteTable('webhook', {
|
||||||
id: text('id').primaryKey().$defaultFn(() => crypto.randomUUID()),
|
id: text('id').primaryKey().$defaultFn(() => crypto.randomUUID()),
|
||||||
|
|||||||
+15
-43
@@ -6,9 +6,6 @@ const CLEANUP_CONFIG = {
|
|||||||
// Whether to delete expired emails
|
// Whether to delete expired emails
|
||||||
DELETE_EXPIRED_EMAILS: true,
|
DELETE_EXPIRED_EMAILS: true,
|
||||||
|
|
||||||
// Whether to delete messages from expired emails if not deleting the emails themselves
|
|
||||||
DELETE_MESSAGES_FROM_EXPIRED: true,
|
|
||||||
|
|
||||||
// Batch processing size
|
// Batch processing size
|
||||||
BATCH_SIZE: 100,
|
BATCH_SIZE: 100,
|
||||||
} as const
|
} as const
|
||||||
@@ -18,50 +15,25 @@ const main = {
|
|||||||
const now = Date.now()
|
const now = Date.now()
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Find expired emails
|
if (!CLEANUP_CONFIG.DELETE_EXPIRED_EMAILS) {
|
||||||
const { results: expiredEmails } = await env.DB
|
console.log('Expired email deletion is disabled')
|
||||||
.prepare(`
|
|
||||||
SELECT id
|
|
||||||
FROM email
|
|
||||||
WHERE expires_at < ?
|
|
||||||
LIMIT ?
|
|
||||||
`)
|
|
||||||
.bind(now, CLEANUP_CONFIG.BATCH_SIZE)
|
|
||||||
.all()
|
|
||||||
|
|
||||||
if (!expiredEmails?.length) {
|
|
||||||
console.log('No expired emails found')
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
const expiredEmailIds = expiredEmails.map(email => email.id)
|
// Directly delete expired emails (messages will be cascade-deleted via foreign key constraint)
|
||||||
const placeholders = expiredEmailIds.map(() => '?').join(',')
|
const result = await env.DB
|
||||||
|
.prepare(`
|
||||||
if (CLEANUP_CONFIG.DELETE_EXPIRED_EMAILS) {
|
|
||||||
// First delete associated messages
|
|
||||||
await env.DB.prepare(`
|
|
||||||
DELETE FROM message
|
|
||||||
WHERE emailId IN (${placeholders})
|
|
||||||
`).bind(...expiredEmailIds).run()
|
|
||||||
|
|
||||||
// Then delete the emails
|
|
||||||
await env.DB.prepare(`
|
|
||||||
DELETE FROM email
|
DELETE FROM email
|
||||||
WHERE id IN (${placeholders})
|
WHERE expires_at < ?
|
||||||
`).bind(...expiredEmailIds).run()
|
LIMIT ?
|
||||||
|
RETURNING id
|
||||||
console.log(`Deleted ${expiredEmails.length} expired emails and their messages`)
|
`)
|
||||||
} else if (CLEANUP_CONFIG.DELETE_MESSAGES_FROM_EXPIRED) {
|
.bind(now, CLEANUP_CONFIG.BATCH_SIZE)
|
||||||
// Only delete messages from expired emails
|
.run()
|
||||||
await env.DB.prepare(`
|
|
||||||
DELETE FROM message
|
const deletedCount = result?.meta?.changes || 0
|
||||||
WHERE emailId IN (${placeholders})
|
console.log(`Deleted ${deletedCount} expired emails and their associated messages`)
|
||||||
`).bind(...expiredEmailIds).run()
|
|
||||||
|
|
||||||
console.log(`Deleted messages from ${expiredEmails.length} expired emails`)
|
|
||||||
} else {
|
|
||||||
console.log('No cleanup actions performed (disabled in config)')
|
|
||||||
}
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Failed to cleanup:', error)
|
console.error('Failed to cleanup:', error)
|
||||||
throw error
|
throw error
|
||||||
|
|||||||
Reference in New Issue
Block a user