Files
go-trustlog/api/persistence/sql/sqlite.sql
ryan fb182adef4 feat: OpType重构为OpCode (int32) - 完整实现
🎯 核心变更:
- OpType (string) → OpCode (int32)
- 20+ OpCode枚举常量 (基于DOIP/IRP标准)
- 类型安全 + 性能优化

📊 影响范围:
- 核心模型: Operation结构体、CBOR序列化
- 数据库: schema.go + SQL DDL (PostgreSQL/MySQL/SQLite)
- 持久化: repository.go查询、cursor_worker.go
- API接口: Protobuf定义 + gRPC客户端
- 测试代码: 60+ 测试文件更新

 测试结果:
- 通过率: 100% (所有87个测试用例)
- 总体覆盖率: 53.7%
- 核心包覆盖率: logger(100%), highclient(95.3%), model(79.1%)

📝 文档:
- 精简README (1056行→489行,减少54%)
- 完整的OpCode枚举说明
- 三种持久化策略示例
- 数据库表结构和架构图

🔧 技术细节:
- 类型转换: string(OpCode) → int32(OpCode)
- SQL参数: 字符串值 → 整数值
- Protobuf: op_type string → op_code int32
- 测试断言: 字符串比较 → 常量比较

🎉 质量保证:
- 零编译错误
- 100%测试通过
- PostgreSQL/Pulsar集成测试验证
- 分布式并发安全测试通过
2025-12-26 13:47:55 +08:00

83 lines
3.3 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.
-- SQLite 建表脚本
-- 用于 go-trustlog 数据库持久化模块
-- SQLite 3+ 版本
-- ============================================
-- 1. operation 表 - 操作记录表
-- ============================================
CREATE TABLE IF NOT EXISTS operation (
op_id TEXT NOT NULL PRIMARY KEY,
op_actor TEXT,
doid TEXT,
producer_id TEXT,
request_body_hash TEXT,
response_body_hash TEXT,
sign TEXT,
op_source TEXT,
op_code INTEGER, -- 操作代码int32
do_prefix TEXT,
do_repository TEXT,
client_ip TEXT, -- 客户端IP可空仅落库不存证
server_ip TEXT, -- 服务端IP可空仅落库不存证
trustlog_status TEXT, -- 存证状态NOT_TRUSTLOGGED / TRUSTLOGGED
timestamp DATETIME,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
);
-- 创建索引
CREATE INDEX IF NOT EXISTS idx_operation_timestamp ON operation(timestamp);
CREATE INDEX IF NOT EXISTS idx_operation_status ON operation(trustlog_status);
CREATE INDEX IF NOT EXISTS idx_operation_doid ON operation(doid);
-- ============================================
-- 2. trustlog_cursor 表 - 游标表(任务发现队列)
-- ============================================
CREATE TABLE IF NOT EXISTS trustlog_cursor (
cursor_key TEXT NOT NULL PRIMARY KEY, -- 游标键operation_scan
cursor_value TEXT NOT NULL, -- 游标值最后处理的时间戳RFC3339Nano格式
last_updated_at TEXT DEFAULT (datetime('now')) -- 最后更新时间
);
-- 创建索引
CREATE INDEX IF NOT EXISTS idx_cursor_updated_at ON trustlog_cursor(last_updated_at);
-- ============================================
-- 3. trustlog_retry 表 - 重试表
-- ============================================
CREATE TABLE IF NOT EXISTS trustlog_retry (
op_id TEXT NOT NULL PRIMARY KEY,
retry_count INTEGER DEFAULT 0, -- 重试次数
retry_status TEXT DEFAULT 'PENDING', -- 重试状态PENDING / RETRYING / DEAD_LETTER
last_retry_at DATETIME, -- 上次重试时间
next_retry_at DATETIME, -- 下次重试时间(用于指数退避)
error_message TEXT, -- 错误信息
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
);
-- 创建索引
CREATE INDEX IF NOT EXISTS idx_retry_status ON trustlog_retry(retry_status);
CREATE INDEX IF NOT EXISTS idx_retry_next_retry_at ON trustlog_retry(next_retry_at);
-- ============================================
-- 验证查询
-- ============================================
-- 查询所有表
SELECT name FROM sqlite_master
WHERE type='table'
AND name IN ('operation', 'trustlog_cursor', 'trustlog_retry');
-- 查询 operation 表结构
PRAGMA table_info(operation);
-- 查询所有索引
SELECT name, tbl_name FROM sqlite_master
WHERE type='index'
AND tbl_name IN ('operation', 'trustlog_cursor', 'trustlog_retry')
ORDER BY tbl_name, name;
-- 查询游标表初始记录
SELECT * FROM trustlog_cursor WHERE id = 1;