#!/bin/bash # Apply schema updates to a running TimescaleDB container without wiping data echo "Applying schema updates to btc_timescale container..." # Execute the schema SQL inside the container # We use psql with the environment variables set in docker-compose docker exec -i btc_timescale psql -U btc_bot -d btc_data < INTERVAL '7 days', if_not_exists => TRUE); EXCEPTION WHEN OTHERS THEN NULL; -- Ignore if already hypertable END \$\$; -- 6. Decisions indexes CREATE INDEX IF NOT EXISTS idx_decisions_live ON decisions (symbol, interval, time DESC) WHERE backtest_id IS NULL; CREATE INDEX IF NOT EXISTS idx_decisions_backtest ON decisions (backtest_id, symbol, time DESC) WHERE backtest_id IS NOT NULL; CREATE INDEX IF NOT EXISTS idx_decisions_type ON decisions (symbol, decision_type, time DESC); -- 7. Backtest runs table CREATE TABLE IF NOT EXISTS backtest_runs ( id TEXT PRIMARY KEY, strategy TEXT NOT NULL, symbol TEXT NOT NULL DEFAULT 'BTC', start_time TIMESTAMPTZ NOT NULL, end_time TIMESTAMPTZ NOT NULL, intervals TEXT[] NOT NULL, config JSONB, results JSONB, created_at TIMESTAMPTZ DEFAULT NOW() ); -- 8. Compression policies DO \$\$ BEGIN ALTER TABLE decisions SET (timescaledb.compress, timescaledb.compress_segmentby = 'symbol,interval,strategy'); PERFORM add_compression_policy('decisions', INTERVAL '7 days', if_not_exists => TRUE); EXCEPTION WHEN OTHERS THEN NULL; -- Ignore compression errors if already set END \$\$; SELECT 'Schema update completed successfully' as status; EOF