|
| 1 | +"""add_langgraph_checkpoint_tables |
| 2 | +
|
| 3 | +Revision ID: d1a6cde41b3f |
| 4 | +Revises: d024851e790c |
| 5 | +Create Date: 2026-02-11 08:02:10.739927 |
| 6 | +
|
| 7 | +""" |
| 8 | +from typing import Sequence, Union |
| 9 | + |
| 10 | +from alembic import op |
| 11 | +import sqlalchemy as sa |
| 12 | +from sqlalchemy.dialects import postgresql |
| 13 | + |
| 14 | +# revision identifiers, used by Alembic. |
| 15 | +revision: str = 'd1a6cde41b3f' |
| 16 | +down_revision: Union[str, None] = 'd024851e790c' |
| 17 | +branch_labels: Union[str, Sequence[str], None] = None |
| 18 | +depends_on: Union[str, Sequence[str], None] = None |
| 19 | + |
| 20 | + |
| 21 | +def upgrade() -> None: |
| 22 | + # checkpoint_migrations |
| 23 | + op.create_table('checkpoint_migrations', |
| 24 | + sa.Column('v', sa.Integer(), nullable=False), |
| 25 | + sa.PrimaryKeyConstraint('v') |
| 26 | + ) |
| 27 | + |
| 28 | + # checkpoints |
| 29 | + op.create_table('checkpoints', |
| 30 | + sa.Column('thread_id', sa.Text(), nullable=False), |
| 31 | + sa.Column('checkpoint_ns', sa.Text(), server_default='', nullable=False), |
| 32 | + sa.Column('checkpoint_id', sa.Text(), nullable=False), |
| 33 | + sa.Column('parent_checkpoint_id', sa.Text(), nullable=True), |
| 34 | + sa.Column('type', sa.Text(), nullable=True), |
| 35 | + sa.Column('checkpoint', postgresql.JSONB(astext_type=sa.Text()), nullable=False), |
| 36 | + sa.Column('metadata', postgresql.JSONB(astext_type=sa.Text()), server_default='{}', nullable=False), |
| 37 | + sa.PrimaryKeyConstraint('thread_id', 'checkpoint_ns', 'checkpoint_id') |
| 38 | + ) |
| 39 | + op.create_index('checkpoints_thread_id_idx', 'checkpoints', ['thread_id'], unique=False) |
| 40 | + |
| 41 | + # checkpoint_blobs |
| 42 | + op.create_table('checkpoint_blobs', |
| 43 | + sa.Column('thread_id', sa.Text(), nullable=False), |
| 44 | + sa.Column('checkpoint_ns', sa.Text(), server_default='', nullable=False), |
| 45 | + sa.Column('channel', sa.Text(), nullable=False), |
| 46 | + sa.Column('version', sa.Text(), nullable=False), |
| 47 | + sa.Column('type', sa.Text(), nullable=False), |
| 48 | + sa.Column('blob', sa.LargeBinary(), nullable=True), |
| 49 | + sa.PrimaryKeyConstraint('thread_id', 'checkpoint_ns', 'channel', 'version') |
| 50 | + ) |
| 51 | + op.create_index('checkpoint_blobs_thread_id_idx', 'checkpoint_blobs', ['thread_id'], unique=False) |
| 52 | + |
| 53 | + # checkpoint_writes |
| 54 | + op.create_table('checkpoint_writes', |
| 55 | + sa.Column('thread_id', sa.Text(), nullable=False), |
| 56 | + sa.Column('checkpoint_ns', sa.Text(), server_default='', nullable=False), |
| 57 | + sa.Column('checkpoint_id', sa.Text(), nullable=False), |
| 58 | + sa.Column('task_id', sa.Text(), nullable=False), |
| 59 | + sa.Column('idx', sa.Integer(), nullable=False), |
| 60 | + sa.Column('channel', sa.Text(), nullable=False), |
| 61 | + sa.Column('type', sa.Text(), nullable=True), |
| 62 | + sa.Column('blob', sa.LargeBinary(), nullable=False), |
| 63 | + sa.Column('task_path', sa.Text(), server_default='', nullable=False), |
| 64 | + sa.PrimaryKeyConstraint('thread_id', 'checkpoint_ns', 'checkpoint_id', 'task_id', 'idx') |
| 65 | + ) |
| 66 | + op.create_index('checkpoint_writes_thread_id_idx', 'checkpoint_writes', ['thread_id'], unique=False) |
| 67 | + |
| 68 | + # Pre-populate checkpoint_migrations so LangGraph sees all its |
| 69 | + # internal migrations as already applied and skips setup(). |
| 70 | + op.execute( |
| 71 | + sa.text( |
| 72 | + "INSERT INTO checkpoint_migrations (v) VALUES (0),(1),(2),(3),(4),(5),(6),(7),(8),(9)" |
| 73 | + ) |
| 74 | + ) |
| 75 | + |
| 76 | + |
| 77 | +def downgrade() -> None: |
| 78 | + op.drop_index('checkpoint_writes_thread_id_idx', table_name='checkpoint_writes') |
| 79 | + op.drop_table('checkpoint_writes') |
| 80 | + op.drop_index('checkpoint_blobs_thread_id_idx', table_name='checkpoint_blobs') |
| 81 | + op.drop_table('checkpoint_blobs') |
| 82 | + op.drop_index('checkpoints_thread_id_idx', table_name='checkpoints') |
| 83 | + op.drop_table('checkpoints') |
| 84 | + op.drop_table('checkpoint_migrations') |
0 commit comments