From e73d62cd3a41da2e5424911b6e2216d63fb95e90 Mon Sep 17 00:00:00 2001 From: Viktor Barzin Date: Wed, 25 Feb 2026 21:03:31 +0000 Subject: [PATCH] fix: make hypertable creation conditional on TimescaleDB extension 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. --- alembic/versions/a1b2c3d4e5f6_initial_schema.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/alembic/versions/a1b2c3d4e5f6_initial_schema.py b/alembic/versions/a1b2c3d4e5f6_initial_schema.py index 6a04b4c..e22ee0d 100644 --- a/alembic/versions/a1b2c3d4e5f6_initial_schema.py +++ b/alembic/versions/a1b2c3d4e5f6_initial_schema.py @@ -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: