309b11ae30
- kline.entity.ts: compress_segmentby 移除 interval(基表固定 1m 无需分段),schedule_interval 365d→30d 与 init-db SQL 一致 - data-source.ts: 生产环境关闭 synchronize,以 init-db SQL 脚本为建表唯一来源 - 新增 data/db/init-db/ 初始化 SQL 链: 01-timescaledb.sql — 启用 TimescaleDB 扩展 02-init-tables.sql — 核心业务表 + klines hypertable(7d chunk / 30d 压缩) 03-continuous-aggregates.sql — 分层连续聚合视图(5m→15m→30m→1h→4h→1d→1w)
31 lines
1.3 KiB
SQL
31 lines
1.3 KiB
SQL
-- ============================================================
|
||
-- 01-timescaledb.sql — TimescaleDB 扩展初始化
|
||
-- ============================================================
|
||
-- Docker 容器首次启动时自动执行(/docker-entrypoint-initdb.d/)
|
||
-- 确保 TimescaleDB 扩展在数据库级别启用。
|
||
--
|
||
-- init-db 完整执行链(按字母序自动执行):
|
||
-- 01-timescaledb.sql — 本文件:启用 TimescaleDB 扩展
|
||
-- 02-init-tables.sql — 核心业务表(exchanges / trading_pairs / klines)
|
||
-- 03-continuous-aggregates.sql — K 线分层连续聚合视图(5m → 1w)
|
||
--
|
||
-- 注意:
|
||
-- - klines 基表由 02-init-tables.sql 创建为 TimescaleDB hypertable
|
||
-- - 连续聚合视图由 03-continuous-aggregates.sql 创建
|
||
-- - TypeORM 的 synchronize:true 与 SQL 脚本互为 fallback(开发/生产双路径)
|
||
-- - 本脚本为 init-db 链的第一环,仅负责扩展启用
|
||
-- ============================================================
|
||
|
||
-- 启用 TimescaleDB 扩展(必须最先执行)
|
||
CREATE EXTENSION IF NOT EXISTS timescaledb CASCADE;
|
||
|
||
-- 验证扩展已启用
|
||
DO $$
|
||
BEGIN
|
||
IF NOT EXISTS (
|
||
SELECT 1 FROM pg_extension WHERE extname = 'timescaledb'
|
||
) THEN
|
||
RAISE EXCEPTION 'TimescaleDB extension is not installed';
|
||
END IF;
|
||
END $$;
|