sky/migrations/00002_media.sql
root 6ed55254a0 chore: initial import of sky project
Go + PostgreSQL + Valkey backend (cmd/internal/migrations),
uni-app mobile frontend, PC web (web/), admin console (admin/),
Android WebView container (android_project/).
Build artifacts, node_modules, local env and debug screenshots excluded.
2026-11-09 22:22:44 +08:00

59 lines
2.2 KiB
SQL
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

-- +goose Up
-- +goose StatementBegin
-- ============================================================
-- 00002_media.sql — 媒体资源元数据
--
-- 设计要点:应用服务器不存字节,只保存「谁传了什么、真实尺寸多少、
-- 对象存储的 key 在哪」。字节全在对象存储里。
-- ============================================================
CREATE TABLE media_assets (
id UUID PRIMARY KEY,
owner_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
-- 对象存储里的 key,格式 {year}/{month}/{sha256[:2]}/{sha256}.{ext}
-- 内容寻址天然去重:同一张图重复上传只占一份空间
object_key TEXT NOT NULL,
bucket TEXT NOT NULL,
-- 上传时的原始文件名,仅用于展示
original_name TEXT NOT NULL DEFAULT '',
content_type TEXT NOT NULL,
size_bytes BIGINT NOT NULL CHECK (size_bytes >= 0),
-- 真实尺寸由 worker 解析文件头得到,不信任客户端上报
width INTEGER,
height INTEGER,
duration_seconds INTEGER,
-- pending: 已签发预签名 URL,等待上传
-- ready: 上传完成,元数据已解析
-- failed: 客户端声明上传但对象不存在 / 解析失败
status TEXT NOT NULL DEFAULT 'pending'
CHECK (status IN ('pending','ready','failed')),
sha256 TEXT,
-- 多档缩略图地址,由图片处理 worker 回填
variants JSONB NOT NULL DEFAULT '{}'::jsonb,
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
completed_at TIMESTAMPTZ,
-- 同一个 key 只应有一条 ready 记录(同内容多用户上传时复用)
CONSTRAINT chk_media_key_unique UNIQUE (bucket, object_key)
);
CREATE INDEX idx_media_owner ON media_assets (owner_id, created_at DESC);
CREATE INDEX idx_media_status ON media_assets (status) WHERE status = 'pending';
-- 预签名凭证不需要持久化:URL 本身就带签名和过期时间,
-- 客户端上传完回来调 confirm 就行。这里不建表是为了少一次写。
-- +goose StatementEnd
-- +goose Down
-- +goose StatementBegin
DROP TABLE IF EXISTS media_assets;
-- +goose StatementEnd