"""add min/max timestamps

Revision ID: 9f0f8ebe3914
Revises: 298875b23096
Create Date: 2025-10-03 14:36:51.645541

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision: str = '9f0f8ebe3914'
down_revision: Union[str, None] = '298875b23096'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    """Upgrade schema."""
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('parquet_files',
    sa.Column('topic_name', sa.String(), nullable=False),
    sa.Column('partition_number', sa.Integer(), nullable=False),
    sa.Column('uri', sa.Text(), nullable=False),
    sa.Column('total_bytes', sa.BigInteger(), nullable=False),
    sa.Column('row_count', sa.BigInteger(), nullable=False),
    sa.Column('min_offset', sa.BigInteger(), nullable=False),
    sa.Column('max_offset', sa.BigInteger(), nullable=False),
    sa.Column('min_timestamp', sa.TIMESTAMP(timezone=True), nullable=True),
    sa.Column('max_timestamp', sa.TIMESTAMP(timezone=True), nullable=True),
    sa.Column('generation', sa.Integer(), nullable=False),
    sa.Column('compacted_at', sa.TIMESTAMP(timezone=True), nullable=True),
    sa.Column('id', sa.BigInteger().with_variant(sa.Integer(), 'sqlite'), sa.Identity(always=True), nullable=False),
    sa.Column('created_at', sa.TIMESTAMP(timezone=True), server_default=sa.text('CURRENT_TIMESTAMP'), nullable=False),
    sa.Column('updated_at', sa.TIMESTAMP(timezone=True), server_default=sa.text('CURRENT_TIMESTAMP'), nullable=False),
    sa.ForeignKeyConstraint(['topic_name', 'partition_number'], ['partitions.topic_name', 'partitions.partition_number'], ondelete='CASCADE'),
    sa.ForeignKeyConstraint(['topic_name'], ['topics.name'], ondelete='CASCADE'),
    sa.PrimaryKeyConstraint('id'),
    sa.UniqueConstraint('uri')
    )
    op.create_index('ix_pf_topic_partition', 'parquet_files', ['topic_name', 'partition_number'], unique=False)
    op.create_index('ix_pf_topic_partition_max', 'parquet_files', ['topic_name', 'partition_number', 'max_offset'], unique=False)
    op.create_index('ix_pf_topic_partition_min', 'parquet_files', ['topic_name', 'partition_number', 'min_offset'], unique=False)
    op.create_table('parquet_file_parents',
    sa.Column('child_parquet_file_id', sa.BigInteger().with_variant(sa.Integer(), 'sqlite'), nullable=False),
    sa.Column('parent_parquet_file_id', sa.BigInteger().with_variant(sa.Integer(), 'sqlite'), nullable=False),
    sa.Column('id', sa.BigInteger().with_variant(sa.Integer(), 'sqlite'), sa.Identity(always=True), nullable=False),
    sa.Column('created_at', sa.TIMESTAMP(timezone=True), server_default=sa.text('CURRENT_TIMESTAMP'), nullable=False),
    sa.Column('updated_at', sa.TIMESTAMP(timezone=True), server_default=sa.text('CURRENT_TIMESTAMP'), nullable=False),
    sa.ForeignKeyConstraint(['child_parquet_file_id'], ['parquet_files.id'], ondelete='CASCADE'),
    sa.ForeignKeyConstraint(['parent_parquet_file_id'], ['parquet_files.id'], ondelete='CASCADE'),
    sa.PrimaryKeyConstraint('id'),
    sa.UniqueConstraint('child_parquet_file_id', 'parent_parquet_file_id')
    )
    op.create_index('ix_pfp_child', 'parquet_file_parents', ['child_parquet_file_id'], unique=False)
    op.create_index('ix_pfp_parent', 'parquet_file_parents', ['parent_parquet_file_id'], unique=False)
    op.create_table('parquet_file_sources',
    sa.Column('parquet_file_id', sa.BigInteger().with_variant(sa.Integer(), 'sqlite'), nullable=False),
    sa.Column('wal_file_id', sa.BigInteger().with_variant(sa.Integer(), 'sqlite'), nullable=False),
    sa.Column('id', sa.BigInteger().with_variant(sa.Integer(), 'sqlite'), sa.Identity(always=True), nullable=False),
    sa.Column('created_at', sa.TIMESTAMP(timezone=True), server_default=sa.text('CURRENT_TIMESTAMP'), nullable=False),
    sa.Column('updated_at', sa.TIMESTAMP(timezone=True), server_default=sa.text('CURRENT_TIMESTAMP'), nullable=False),
    sa.ForeignKeyConstraint(['parquet_file_id'], ['parquet_files.id'], ondelete='CASCADE'),
    sa.ForeignKeyConstraint(['wal_file_id'], ['wal_files.id'], ondelete='CASCADE'),
    sa.PrimaryKeyConstraint('id'),
    sa.UniqueConstraint('parquet_file_id', 'wal_file_id')
    )
    op.create_index('ix_pfs_pf', 'parquet_file_sources', ['parquet_file_id'], unique=False)
    op.create_index('ix_pfs_wf', 'parquet_file_sources', ['wal_file_id'], unique=False)
    op.add_column('topics', sa.Column('schema', sa.Text(), nullable=True))
    op.add_column('wal_file_offsets', sa.Column('min_timestamp', sa.BigInteger(), nullable=True))
    op.add_column('wal_file_offsets', sa.Column('max_timestamp', sa.BigInteger(), nullable=True))
    op.create_index('ix_wal_file_offset_topic_part_ts', 'wal_file_offsets', ['topic_name', 'partition_number', 'min_timestamp', 'max_timestamp'], unique=False)
    # ### end Alembic commands ###


def downgrade() -> None:
    """Downgrade schema."""
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_index('ix_wal_file_offset_topic_part_ts', table_name='wal_file_offsets')
    op.drop_column('wal_file_offsets', 'max_timestamp')
    op.drop_column('wal_file_offsets', 'min_timestamp')
    op.drop_column('topics', 'schema')
    op.drop_index('ix_pfs_wf', table_name='parquet_file_sources')
    op.drop_index('ix_pfs_pf', table_name='parquet_file_sources')
    op.drop_table('parquet_file_sources')
    op.drop_index('ix_pfp_parent', table_name='parquet_file_parents')
    op.drop_index('ix_pfp_child', table_name='parquet_file_parents')
    op.drop_table('parquet_file_parents')
    op.drop_index('ix_pf_topic_partition_min', table_name='parquet_files')
    op.drop_index('ix_pf_topic_partition_max', table_name='parquet_files')
    op.drop_index('ix_pf_topic_partition', table_name='parquet_files')
    op.drop_table('parquet_files')
    # ### end Alembic commands ###
