Alembic – how to create hypertable
Question:
How to generate hypertable using Alembic? Some custom call must be added I suppose, but where? I tried event.listen but Alembic does not register it.
Answers:
You can create hypertables in Alembic by adding manual, custom, migration actions.
You cannot generate it automatically, because there is no specific support in Alembic for TimescaleDB and it does not understand hypertables.
def upgrade() -> None:
op.create_table(
TABLE_NAME,
sa.Column('ts', sa.DateTime),
sa.Column('col1', sa.String(50)),
sa.Column('col2', sa.String(50)),
sa.Column('col3',sa.String(50)),
sa.Column('col4',sa.String(50)),
sa.Column('col5',sa.Integer),
sa.Column('col6',sa.Integer),
schema=SCHEMA,
if_not_exists=True,)
op.execute(f"SELECT create_hypertable('{SCHEMA}.{TABLE_NAME}', 'ts');")
This worked for me. Make sure you create the hypertable extension if needed or you’ll get errors.:
CREATE EXTENSION IF NOT EXISTS timescaledb CASCADE;
How to generate hypertable using Alembic? Some custom call must be added I suppose, but where? I tried event.listen but Alembic does not register it.
You can create hypertables in Alembic by adding manual, custom, migration actions.
You cannot generate it automatically, because there is no specific support in Alembic for TimescaleDB and it does not understand hypertables.
def upgrade() -> None:
op.create_table(
TABLE_NAME,
sa.Column('ts', sa.DateTime),
sa.Column('col1', sa.String(50)),
sa.Column('col2', sa.String(50)),
sa.Column('col3',sa.String(50)),
sa.Column('col4',sa.String(50)),
sa.Column('col5',sa.Integer),
sa.Column('col6',sa.Integer),
schema=SCHEMA,
if_not_exists=True,)
op.execute(f"SELECT create_hypertable('{SCHEMA}.{TABLE_NAME}', 'ts');")
This worked for me. Make sure you create the hypertable extension if needed or you’ll get errors.:
CREATE EXTENSION IF NOT EXISTS timescaledb CASCADE;