fix: make hypertable creation conditional on TimescaleDB extension
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed

The alembic migration unconditionally called create_hypertable() which
fails if TimescaleDB isn't installed on the PostgreSQL instance. Wrap
in a DO block that checks for the extension first.
This commit is contained in:
Viktor Barzin 2026-02-25 21:03:31 +00:00
parent efc91e9ad0
commit e73d62cd3a
No known key found for this signature in database
GPG key ID: 0EB088298288D958

View file

@ -254,11 +254,17 @@ def upgrade() -> None:
sa.Column("sharpe_ratio", sa.Float, nullable=True),
)
# Convert timeseries tables to TimescaleDB hypertables.
# These calls are idempotent-safe when the extension is loaded.
op.execute("SELECT create_hypertable('market_data', 'timestamp', if_not_exists => TRUE)")
op.execute("SELECT create_hypertable('portfolio_snapshots', 'timestamp', if_not_exists => TRUE)")
op.execute("SELECT create_hypertable('strategy_metrics', 'timestamp', if_not_exists => TRUE)")
# Convert timeseries tables to TimescaleDB hypertables if the extension is available.
op.execute("""
DO $$
BEGIN
IF EXISTS (SELECT 1 FROM pg_extension WHERE extname = 'timescaledb') THEN
PERFORM create_hypertable('market_data', 'timestamp', if_not_exists => TRUE);
PERFORM create_hypertable('portfolio_snapshots', 'timestamp', if_not_exists => TRUE);
PERFORM create_hypertable('strategy_metrics', 'timestamp', if_not_exists => TRUE);
END IF;
END $$;
""")
def downgrade() -> None: