-- 0002_upload_sessions.sql — 分片上传会话与分片记录(纯增量,不改既有表)
CREATE TABLE attachment_upload_sessions (
  id TEXT PRIMARY KEY,
  attachment_id TEXT NOT NULL,
  user_id TEXT NOT NULL,
  object_key TEXT NOT NULL,
  upload_id TEXT NOT NULL,
  status TEXT NOT NULL DEFAULT 'active',
  content_type TEXT NOT NULL,
  part_count INTEGER NOT NULL DEFAULT 0,
  byte_size INTEGER NOT NULL DEFAULT 0,
  created_at TEXT NOT NULL,
  completed_at TEXT,
  aborted_at TEXT
);

CREATE INDEX idx_upload_sessions_active ON attachment_upload_sessions(user_id, attachment_id, status);

CREATE TABLE attachment_upload_parts (
  session_id TEXT NOT NULL,
  user_id TEXT NOT NULL,
  attachment_id TEXT NOT NULL,
  part_number INTEGER NOT NULL,
  etag TEXT NOT NULL,
  byte_size INTEGER NOT NULL,
  created_at TEXT NOT NULL,
  PRIMARY KEY (session_id, part_number)
);

CREATE INDEX idx_upload_parts_order ON attachment_upload_parts(session_id, part_number);
