13f7c1326a
- RESTful API: POST /heartbeat, POST /checkpoints, GET /status, GET /summaries - State-change-only checkpoint model with extensible StateType enum - PostgreSQL backend with sqlx, auto-migration on startup - pg_cron scheduled aggregation (state_summaries) and offline detection - Heartbeat-based liveness with 60s timeout auto-offline - LEAD() window function for state duration calculation - JSONB content field for extensible checkpoint metadata BREAKING CHANGE: Complete rewrite from Hello World to full API service.
21 lines
601 B
SQL
21 lines
601 B
SQL
-- 001_init.sql
|
|
-- Checkpoint 服务初始建表
|
|
|
|
CREATE TABLE IF NOT EXISTS checkpoints (
|
|
id BIGSERIAL PRIMARY KEY,
|
|
user_id VARCHAR(128) NOT NULL,
|
|
state VARCHAR(64) NOT NULL,
|
|
timestamp BIGINT NOT NULL,
|
|
content JSONB,
|
|
|
|
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
|
|
);
|
|
|
|
-- 核心查询索引:按用户 + 时间范围
|
|
CREATE INDEX IF NOT EXISTS idx_checkpoints_user_ts
|
|
ON checkpoints (user_id, timestamp);
|
|
|
|
-- 快速获取用户最新检查点
|
|
CREATE INDEX IF NOT EXISTS idx_checkpoints_user_latest
|
|
ON checkpoints (user_id, timestamp DESC);
|