063_add_sora_client_tables.sql 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. -- Migration: 063_add_sora_client_tables
  2. -- Sora 客户端功能所需的数据库变更:
  3. -- 1. 新增 sora_generations 表:记录 Sora 客户端 UI 的生成历史
  4. -- 2. users 表新增存储配额字段
  5. -- 3. groups 表新增存储配额字段
  6. -- ============================================================
  7. -- 1. sora_generations 表(生成记录)
  8. -- ============================================================
  9. CREATE TABLE IF NOT EXISTS sora_generations (
  10. id BIGSERIAL PRIMARY KEY,
  11. user_id BIGINT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
  12. api_key_id BIGINT,
  13. -- 生成参数
  14. model VARCHAR(64) NOT NULL,
  15. prompt TEXT NOT NULL DEFAULT '',
  16. media_type VARCHAR(16) NOT NULL DEFAULT 'video', -- video / image
  17. -- 结果
  18. status VARCHAR(16) NOT NULL DEFAULT 'pending', -- pending / generating / completed / failed / cancelled
  19. media_url TEXT NOT NULL DEFAULT '',
  20. media_urls JSONB, -- 多图时的 URL 数组
  21. file_size_bytes BIGINT NOT NULL DEFAULT 0,
  22. storage_type VARCHAR(16) NOT NULL DEFAULT 'none', -- s3 / local / upstream / none
  23. s3_object_keys JSONB, -- S3 object key 数组
  24. -- 上游信息
  25. upstream_task_id VARCHAR(128) NOT NULL DEFAULT '',
  26. error_message TEXT NOT NULL DEFAULT '',
  27. -- 时间
  28. created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
  29. completed_at TIMESTAMPTZ
  30. );
  31. -- 按用户+时间查询(作品库列表、历史记录)
  32. CREATE INDEX IF NOT EXISTS idx_sora_gen_user_created
  33. ON sora_generations(user_id, created_at DESC);
  34. -- 按用户+状态查询(恢复进行中任务)
  35. CREATE INDEX IF NOT EXISTS idx_sora_gen_user_status
  36. ON sora_generations(user_id, status);
  37. -- ============================================================
  38. -- 2. users 表新增 Sora 存储配额字段
  39. -- ============================================================
  40. ALTER TABLE users
  41. ADD COLUMN IF NOT EXISTS sora_storage_quota_bytes BIGINT NOT NULL DEFAULT 0,
  42. ADD COLUMN IF NOT EXISTS sora_storage_used_bytes BIGINT NOT NULL DEFAULT 0;
  43. -- ============================================================
  44. -- 3. groups 表新增 Sora 存储配额字段
  45. -- ============================================================
  46. ALTER TABLE groups
  47. ADD COLUMN IF NOT EXISTS sora_storage_quota_bytes BIGINT NOT NULL DEFAULT 0;