fix: add FK constraint to attachments.dm_message_id via table rebuild

Rebuilds the attachments table with ON DELETE CASCADE on dm_message_id.
Dangling records are excluded during copy — their files become standard
disk orphans detectable by the storage janitor. Also updates the raw SQL
in index.ts to include width/height/duration columns for fresh installs.
This commit is contained in:
Jannis Braun
2026-03-23 01:55:26 +01:00
parent 8bcb5cc977
commit f634f35450
2 changed files with 61 additions and 13 deletions
+16 -13
View File
@@ -89,19 +89,6 @@ function createTables(db: Database.Database): void {
created_at INTEGER NOT NULL
);
CREATE TABLE IF NOT EXISTS attachments (
id TEXT PRIMARY KEY,
message_id TEXT REFERENCES messages(id) ON DELETE CASCADE,
dm_message_id TEXT,
uploader_id TEXT,
filename TEXT NOT NULL,
original_name TEXT NOT NULL,
mimetype TEXT NOT NULL,
size INTEGER NOT NULL,
thumbnail_filename TEXT,
created_at INTEGER NOT NULL
);
CREATE TABLE IF NOT EXISTS dm_channels (
id TEXT PRIMARY KEY,
owner_id TEXT,
@@ -125,6 +112,22 @@ function createTables(db: Database.Database): void {
created_at INTEGER NOT NULL
);
CREATE TABLE IF NOT EXISTS attachments (
id TEXT PRIMARY KEY,
message_id TEXT REFERENCES messages(id) ON DELETE CASCADE,
dm_message_id TEXT REFERENCES dm_messages(id) ON DELETE CASCADE,
uploader_id TEXT,
filename TEXT NOT NULL,
original_name TEXT NOT NULL,
mimetype TEXT NOT NULL,
size INTEGER NOT NULL,
thumbnail_filename TEXT,
width INTEGER,
height INTEGER,
duration REAL,
created_at INTEGER NOT NULL
);
CREATE TABLE IF NOT EXISTS friends (
user_id TEXT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
friend_id TEXT NOT NULL REFERENCES users(id) ON DELETE CASCADE,