/** * Database schema for PERLER LAB. * * Two DDL exports: * SCHEMA_SQL_SQLITE — SQLite DDL (used by tests + legacy sqlite file mode) * SCHEMA_SQL_PG — PostgreSQL DDL (used by production initDatabase) */ /** All DDL statements for creating tables on SQLite. */ export const SCHEMA_SQL_SQLITE = ` -- 兑换码表 CREATE TABLE IF NOT EXISTS codes ( code TEXT PRIMARY KEY, package_type TEXT NOT NULL, total_uses INTEGER NOT NULL CHECK(total_uses > 0), used_uses INTEGER NOT NULL DEFAULT 0 CHECK(used_uses >= 0), status TEXT NOT NULL DEFAULT 'active' CHECK(status IN ('active', 'used', 'disabled')), created_at TEXT NOT NULL DEFAULT (datetime('now')), expired_at TEXT ); -- 订单表 CREATE TABLE IF NOT EXISTS orders ( order_no TEXT PRIMARY KEY, package_name TEXT NOT NULL, amount_cents INTEGER NOT NULL CHECK(amount_cents > 0), payment_method TEXT NOT NULL, status TEXT NOT NULL DEFAULT 'pending' CHECK(status IN ('pending', 'paid', 'failed')), access_code TEXT REFERENCES codes(code), created_at TEXT NOT NULL DEFAULT (datetime('now')), paid_at TEXT ); -- 核销日志表 CREATE TABLE IF NOT EXISTS redemption_logs ( id INTEGER PRIMARY KEY AUTOINCREMENT, code TEXT NOT NULL REFERENCES codes(code), action TEXT NOT NULL CHECK(action IN ('redeem', 'consume')), used_delta INTEGER NOT NULL DEFAULT 0 CHECK(used_delta >= 0), created_at TEXT NOT NULL DEFAULT (datetime('now')) ); -- AI 生成记录表 CREATE TABLE IF NOT EXISTS chibi_generations ( id INTEGER PRIMARY KEY AUTOINCREMENT, code TEXT NOT NULL REFERENCES codes(code), category TEXT NOT NULL, subtype TEXT NOT NULL, grid_size INTEGER NOT NULL, color_count INTEGER NOT NULL, status TEXT NOT NULL DEFAULT 'pending', image_data TEXT, created_at TEXT NOT NULL DEFAULT (datetime('now')) ); -- 索引:按 code 查询日志 CREATE INDEX IF NOT EXISTS idx_redemption_logs_code ON redemption_logs(code); -- 索引:按状态查询兑换码 CREATE INDEX IF NOT EXISTS idx_codes_status ON codes(status); -- 索引:按 order_no 查询订单 CREATE INDEX IF NOT EXISTS idx_orders_order_no ON orders(order_no); -- 设备绑定表(云端同步权益:换设备/清缓存后恢复兑换码) -- 复合主键 (device_id, code):一个设备可绑定多个兑换码, -- 同一兑换码也可绑定到多个设备(家庭/多端共用)。 CREATE TABLE IF NOT EXISTS device_bindings ( device_id TEXT NOT NULL, code TEXT NOT NULL REFERENCES codes(code), created_at TEXT NOT NULL DEFAULT (datetime('now')), PRIMARY KEY (device_id, code) ); -- 索引:按 code 反查绑定设备 CREATE INDEX IF NOT EXISTS idx_device_bindings_code ON device_bindings(code); -- 安全事件表(防刷:IP 追踪 + 滥用检测) -- ip 可空:兑换成功/失败等带来源 IP 的事件写入,禁用事件(code_disabled)无来源 CREATE TABLE IF NOT EXISTS security_events ( id INTEGER PRIMARY KEY AUTOINCREMENT, ip TEXT, code TEXT NOT NULL, action TEXT NOT NULL, event_type TEXT NOT NULL CHECK(event_type IN ('redeem_ok', 'redeem_fail', 'code_disabled')), created_at TEXT NOT NULL DEFAULT (datetime('now')) ); -- 索引:按兑换码查询安全事件(防刷检测主路径) CREATE INDEX IF NOT EXISTS idx_security_events_code ON security_events(code, event_type, created_at); -- 索引:按 IP 查询安全事件(同一 IP 刷码检测) CREATE INDEX IF NOT EXISTS idx_security_events_ip ON security_events(ip, event_type, created_at); -- 下载授权表(一次性下载凭证) -- AI 生成成功后写入一行:consume 时必须携带未使用的 grant 且归属同一兑换码, -- 核销时置 used=1,防止绕过生成流程直接消耗次数下载。 CREATE TABLE IF NOT EXISTS download_grants ( grant TEXT PRIMARY KEY, code TEXT NOT NULL REFERENCES codes(code), order_no TEXT, used INTEGER NOT NULL DEFAULT 0 CHECK(used IN (0, 1)), created_at TEXT NOT NULL DEFAULT (datetime('now')) ); -- 索引:按兑换码查询下载授权 CREATE INDEX IF NOT EXISTS idx_download_grants_code ON download_grants(code); -- chibi Showcase 作品表 CREATE TABLE IF NOT EXISTS showcase_works ( id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT NOT NULL, image_data TEXT NOT NULL, tags TEXT NOT NULL DEFAULT '[]', source TEXT NOT NULL DEFAULT 'manual', created_at TEXT NOT NULL DEFAULT (datetime('now')), width INTEGER, height INTEGER, brand TEXT, materials TEXT, is_hidden INTEGER NOT NULL DEFAULT 0 ); -- 索引:按创建时间倒序查询作品列表(Showcase 展示页主路径) CREATE INDEX IF NOT EXISTS idx_showcase_works_created_at ON showcase_works(created_at DESC, id DESC); -- 用户表(账号体系:手机号 + bcrypt 密码哈希) CREATE TABLE IF NOT EXISTS users ( id TEXT PRIMARY KEY, phone TEXT NOT NULL UNIQUE, password_hash TEXT NOT NULL, nickname TEXT, role TEXT NOT NULL DEFAULT 'user', created_at TEXT NOT NULL DEFAULT (datetime('now')) ); -- 云端豆仓表(按用户隔离,色号 → 数量) CREATE TABLE IF NOT EXISTS bead_inventories ( id INTEGER PRIMARY KEY AUTOINCREMENT, user_id TEXT NOT NULL REFERENCES users(id), brand_id TEXT NOT NULL DEFAULT 'MARD', code TEXT NOT NULL, count INTEGER NOT NULL DEFAULT 0 CHECK(count >= 0), updated_at TEXT NOT NULL DEFAULT (datetime('now')), UNIQUE(user_id, code) ); -- 索引:按用户查询库存(豆仓主路径) CREATE INDEX IF NOT EXISTS idx_bead_inventories_user ON bead_inventories(user_id); -- 公告/更新日志表(管理员发布,公开可读) CREATE TABLE IF NOT EXISTS announcements ( id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT NOT NULL, body TEXT NOT NULL, version TEXT, published_at TEXT NOT NULL DEFAULT (datetime('now')), is_published INTEGER NOT NULL DEFAULT 0 CHECK(is_published IN (0, 1)) ); -- 索引:按发布时间倒序查询已发布公告 CREATE INDEX IF NOT EXISTS idx_announcements_published ON announcements(is_published, published_at DESC); `; /** All DDL statements for creating tables on PostgreSQL. */ export const SCHEMA_SQL_PG = ` -- 兑换码表 CREATE TABLE IF NOT EXISTS codes ( code TEXT PRIMARY KEY, package_type TEXT NOT NULL, total_uses INTEGER NOT NULL CHECK(total_uses > 0), used_uses INTEGER NOT NULL DEFAULT 0 CHECK(used_uses >= 0), status TEXT NOT NULL DEFAULT 'active' CHECK(status IN ('active', 'used', 'disabled')), created_at TIMESTAMPTZ NOT NULL DEFAULT now(), expired_at TEXT ); -- 订单表 CREATE TABLE IF NOT EXISTS orders ( order_no TEXT PRIMARY KEY, package_name TEXT NOT NULL, amount_cents INTEGER NOT NULL CHECK(amount_cents > 0), payment_method TEXT NOT NULL, status TEXT NOT NULL DEFAULT 'pending' CHECK(status IN ('pending', 'paid', 'failed')), access_code TEXT REFERENCES codes(code), created_at TIMESTAMPTZ NOT NULL DEFAULT now(), paid_at TIMESTAMPTZ ); -- 核销日志表 CREATE TABLE IF NOT EXISTS redemption_logs ( id SERIAL PRIMARY KEY, code TEXT NOT NULL REFERENCES codes(code), action TEXT NOT NULL CHECK(action IN ('redeem', 'consume')), used_delta INTEGER NOT NULL DEFAULT 0 CHECK(used_delta >= 0), created_at TIMESTAMPTZ NOT NULL DEFAULT now() ); -- AI 生成记录表 CREATE TABLE IF NOT EXISTS chibi_generations ( id SERIAL PRIMARY KEY, code TEXT NOT NULL REFERENCES codes(code), category TEXT NOT NULL, subtype TEXT NOT NULL, grid_size INTEGER NOT NULL, color_count INTEGER NOT NULL, status TEXT NOT NULL DEFAULT 'pending', image_data TEXT, created_at TIMESTAMPTZ NOT NULL DEFAULT now() ); -- 索引:按 code 查询日志 CREATE INDEX IF NOT EXISTS idx_redemption_logs_code ON redemption_logs(code); -- 索引:按状态查询兑换码 CREATE INDEX IF NOT EXISTS idx_codes_status ON codes(status); -- 索引:按 order_no 查询订单 CREATE INDEX IF NOT EXISTS idx_orders_order_no ON orders(order_no); -- 设备绑定表(云端同步权益) CREATE TABLE IF NOT EXISTS device_bindings ( device_id TEXT NOT NULL, code TEXT NOT NULL REFERENCES codes(code), created_at TIMESTAMPTZ NOT NULL DEFAULT now(), PRIMARY KEY (device_id, code) ); -- 索引:按 code 反查绑定设备 CREATE INDEX IF NOT EXISTS idx_device_bindings_code ON device_bindings(code); -- 安全事件表 CREATE TABLE IF NOT EXISTS security_events ( id SERIAL PRIMARY KEY, ip TEXT, code TEXT NOT NULL, action TEXT NOT NULL, event_type TEXT NOT NULL CHECK(event_type IN ('redeem_ok', 'redeem_fail', 'code_disabled')), created_at TIMESTAMPTZ NOT NULL DEFAULT now() ); -- 索引:按兑换码查询安全事件 CREATE INDEX IF NOT EXISTS idx_security_events_code ON security_events(code, event_type, created_at); -- 索引:按 IP 查询安全事件 CREATE INDEX IF NOT EXISTS idx_security_events_ip ON security_events(ip, event_type, created_at); -- 下载授权表 CREATE TABLE IF NOT EXISTS download_grants ( "grant" TEXT PRIMARY KEY, code TEXT NOT NULL REFERENCES codes(code), order_no TEXT, used INTEGER NOT NULL DEFAULT 0 CHECK(used IN (0, 1)), created_at TIMESTAMPTZ NOT NULL DEFAULT now() ); -- 索引:按兑换码查询下载授权 CREATE INDEX IF NOT EXISTS idx_download_grants_code ON download_grants(code); -- chibi Showcase 作品表 CREATE TABLE IF NOT EXISTS showcase_works ( id SERIAL PRIMARY KEY, title TEXT NOT NULL, image_data TEXT NOT NULL, tags TEXT NOT NULL DEFAULT '[]', source TEXT NOT NULL DEFAULT 'manual', created_at TIMESTAMPTZ NOT NULL DEFAULT now(), width INTEGER, height INTEGER, brand TEXT, materials TEXT, is_hidden INTEGER NOT NULL DEFAULT 0 ); -- 索引:按创建时间倒序查询作品列表 CREATE INDEX IF NOT EXISTS idx_showcase_works_created_at ON showcase_works(created_at DESC, id DESC); -- 用户表(账号体系:手机号 + bcrypt 密码哈希) CREATE TABLE IF NOT EXISTS users ( id TEXT PRIMARY KEY, phone TEXT NOT NULL UNIQUE, password_hash TEXT NOT NULL, nickname TEXT, role TEXT NOT NULL DEFAULT 'user', created_at TIMESTAMPTZ NOT NULL DEFAULT now() ); -- 云端豆仓表(按用户隔离,色号 → 数量) CREATE TABLE IF NOT EXISTS bead_inventories ( id SERIAL PRIMARY KEY, user_id TEXT NOT NULL REFERENCES users(id), brand_id TEXT NOT NULL DEFAULT 'MARD', code TEXT NOT NULL, count INTEGER NOT NULL DEFAULT 0 CHECK(count >= 0), updated_at TIMESTAMPTZ NOT NULL DEFAULT now(), UNIQUE(user_id, code) ); -- 索引:按用户查询库存 CREATE INDEX IF NOT EXISTS idx_bead_inventories_user ON bead_inventories(user_id); -- 公告/更新日志表(管理员发布,公开可读) CREATE TABLE IF NOT EXISTS announcements ( id SERIAL PRIMARY KEY, title TEXT NOT NULL, body TEXT NOT NULL, version TEXT, published_at TIMESTAMPTZ NOT NULL DEFAULT now(), is_published INTEGER NOT NULL DEFAULT 0 CHECK(is_published IN (0, 1)) ); -- 索引:按发布时间倒序查询已发布公告 CREATE INDEX IF NOT EXISTS idx_announcements_published ON announcements(is_published, published_at DESC); `; /** 保留旧名以便渐进切换(SQLite DDL,用于 createTestDatabase)。 */ export const SCHEMA_SQL = SCHEMA_SQL_SQLITE;