-- 1. Enable TimescaleDB extension CREATE EXTENSION IF NOT EXISTS timescaledb; -- 2. Create candles table (main data storage) CREATE TABLE IF NOT EXISTS candles ( time TIMESTAMPTZ NOT NULL, symbol TEXT NOT NULL, interval TEXT NOT NULL, open DECIMAL(18,8) NOT NULL, high DECIMAL(18,8) NOT NULL, low DECIMAL(18,8) NOT NULL, close DECIMAL(18,8) NOT NULL, volume DECIMAL(18,8) NOT NULL, validated BOOLEAN DEFAULT FALSE, source TEXT DEFAULT 'hyperliquid', created_at TIMESTAMPTZ DEFAULT NOW() ); -- 3. Convert to hypertable (partitioned by time) SELECT create_hypertable('candles', 'time', chunk_time_interval => INTERVAL '7 days', if_not_exists => TRUE ); -- 4. Create unique constraint for upserts (required by ON CONFLICT) ALTER TABLE candles ADD CONSTRAINT candles_unique_candle UNIQUE (time, symbol, interval); -- 5. Create indexes for efficient queries CREATE INDEX IF NOT EXISTS idx_candles_symbol_time ON candles (symbol, interval, time DESC); CREATE INDEX IF NOT EXISTS idx_candles_validated ON candles (validated) WHERE validated = FALSE; -- 5. Create indicators table (computed values) CREATE TABLE IF NOT EXISTS indicators ( time TIMESTAMPTZ NOT NULL, symbol TEXT NOT NULL, interval TEXT NOT NULL, indicator_name TEXT NOT NULL, value DECIMAL(18,8) NOT NULL, parameters JSONB, computed_at TIMESTAMPTZ DEFAULT NOW() ); -- 6. Convert indicators to hypertable SELECT create_hypertable('indicators', 'time', chunk_time_interval => INTERVAL '7 days', if_not_exists => TRUE ); -- 7. Create index for indicators CREATE INDEX IF NOT EXISTS idx_indicators_lookup ON indicators (symbol, interval, indicator_name, time DESC); -- 8. Create data quality log table CREATE TABLE IF NOT EXISTS data_quality ( time TIMESTAMPTZ NOT NULL DEFAULT NOW(), check_type TEXT NOT NULL, severity TEXT NOT NULL, symbol TEXT, details JSONB, resolved BOOLEAN DEFAULT FALSE ); CREATE INDEX IF NOT EXISTS idx_quality_unresolved ON data_quality (resolved) WHERE resolved = FALSE; CREATE INDEX IF NOT EXISTS idx_quality_time ON data_quality (time DESC); -- 9. Create collector state tracking table CREATE TABLE IF NOT EXISTS collector_state ( id SERIAL PRIMARY KEY, symbol TEXT NOT NULL UNIQUE, last_candle_time TIMESTAMPTZ, last_validation_time TIMESTAMPTZ, total_candles BIGINT DEFAULT 0, updated_at TIMESTAMPTZ DEFAULT NOW() ); -- 10. Insert initial state for cbBTC INSERT INTO collector_state (symbol, last_candle_time) VALUES ('cbBTC', NULL) ON CONFLICT (symbol) DO NOTHING; -- 11. Enable compression for old data (after 7 days) ALTER TABLE candles SET ( timescaledb.compress, timescaledb.compress_segmentby = 'symbol,interval' ); ALTER TABLE indicators SET ( timescaledb.compress, timescaledb.compress_segmentby = 'symbol,interval,indicator_name' ); -- 12. Add compression policies SELECT add_compression_policy('candles', INTERVAL '7 days', if_not_exists => TRUE); SELECT add_compression_policy('indicators', INTERVAL '7 days', if_not_exists => TRUE); -- 13. Create function to update collector state CREATE OR REPLACE FUNCTION update_collector_state() RETURNS TRIGGER AS $$ BEGIN INSERT INTO collector_state (symbol, last_candle_time, total_candles) VALUES (NEW.symbol, NEW.time, 1) ON CONFLICT (symbol) DO UPDATE SET last_candle_time = NEW.time, total_candles = collector_state.total_candles + 1, updated_at = NOW(); RETURN NEW; END; $$ LANGUAGE plpgsql; -- 14. Create trigger to auto-update state DROP TRIGGER IF EXISTS trigger_update_state ON candles; CREATE TRIGGER trigger_update_state AFTER INSERT ON candles FOR EACH ROW EXECUTE FUNCTION update_collector_state(); -- 15. Create view for data health check CREATE OR REPLACE VIEW data_health AS SELECT symbol, COUNT(*) as total_candles, COUNT(*) FILTER (WHERE validated) as validated_candles, MAX(time) as latest_candle, MIN(time) as earliest_candle, NOW() - MAX(time) as time_since_last FROM candles GROUP BY symbol; -- Success message SELECT 'Database schema initialized successfully' as status;