romanzdk
romanzdk

Reputation: 1332

Alembic - how to create hypertable

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.

Upvotes: 0

Views: 894

Answers (2)

Charles Horel
Charles Horel

Reputation: 11

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;

Upvotes: 0

Mikko Ohtamaa
Mikko Ohtamaa

Reputation: 83576

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.

Upvotes: 1

Related Questions