feat: 后端 SQLite→PostgreSQL 迁移(双驱动)——codes/showcase/sync/chibi/payment/security 全链路支持 pg,修复 grant 保留字,schema 双 DDL 导出

This commit is contained in:
Sisyphus committed 2026-08-19 19:12:26 +08:00
1 parent 346f41d391
commit 605af4cdd3
17 files changed
+1516 -2202

No files matched your search

+122 -13
View File
@@ -1,15 +1,13 @@
/**
* SQLite database schema for PERLER LAB.
* Database schema for PERLER LAB.
*
* DB choice: better-sqlite3
* - Synchronous API (no async overhead for SQLite): simpler code, faster local operations.
* - Well-typed TypeScript bindings via @types/better-sqlite3.
* - Mature, widely used, zero-dependency native bindings.
* - No external server process needed — database is a single file.
* 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. */
export const SCHEMA_SQL = `
/** All DDL statements for creating tables on SQLite. */
export const SCHEMA_SQL_SQLITE = `
-- 兑换码表
CREATE TABLE IF NOT EXISTS codes (
code TEXT PRIMARY KEY,
@@ -108,10 +106,6 @@ CREATE TABLE IF NOT EXISTS download_grants (
CREATE INDEX IF NOT EXISTS idx_download_grants_code ON download_grants(code);
-- chibi Showcase 作品表
-- title: 作品标题;image_data: base64 dataURL 或 http(s) URL;
-- tags: 标签数组 JSON 文本(如 '["宠物","猫"]');source: 作品来源(如 chibi/manual)
-- 规格列(T-131 详情页需要):width/height 图纸宽高(格);brand 材料品牌(如 MARD);
-- materials 用料清单 JSON 文本(如 '[{"code":"A7","count":980}]',name 可选),缺省为 NULL
CREATE TABLE IF NOT EXISTS showcase_works (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT NOT NULL,
@@ -127,4 +121,119 @@ CREATE TABLE IF NOT EXISTS showcase_works (
-- 索引:按创建时间倒序查询作品列表(Showcase 展示页主路径)
CREATE INDEX IF NOT EXISTS idx_showcase_works_created_at ON showcase_works(created_at DESC, id 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',
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
);
-- 索引:按创建时间倒序查询作品列表
CREATE INDEX IF NOT EXISTS idx_showcase_works_created_at ON showcase_works(created_at DESC, id DESC);
`;
/** 保留旧名以便渐进切换(SQLite DDL,用于 createTestDatabase)。 */
export const SCHEMA_SQL = SCHEMA_SQL_SQLITE;