"""initial schema

Revision ID: 0d7469ec9bff
Revises: 
Create Date: 2026-02-04 23:31:37.430171

"""
from collections.abc import Sequence

import sqlalchemy as sa
from alembic import op

# revision identifiers, used by Alembic.
revision: str = '0d7469ec9bff'
down_revision: str | Sequence[str] | None = None
branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None


def upgrade() -> None:
    """Upgrade schema."""
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('orgs',
    sa.Column('org_id', sa.String(length=26), nullable=False),
    sa.Column('name', sa.String(length=255), nullable=False),
    sa.Column('created_at', sa.DateTime(timezone=True), nullable=False),
    sa.PrimaryKeyConstraint('org_id')
    )
    op.create_table('projects',
    sa.Column('project_id', sa.String(length=26), nullable=False),
    sa.Column('org_id', sa.String(length=26), nullable=False),
    sa.Column('name', sa.String(length=255), nullable=False),
    sa.Column('created_at', sa.DateTime(timezone=True), nullable=False),
    sa.ForeignKeyConstraint(['org_id'], ['orgs.org_id'], ),
    sa.PrimaryKeyConstraint('project_id')
    )
    op.create_index('ix_projects_org_created', 'projects', ['org_id', 'created_at'], unique=False)
    op.create_table('batches',
    sa.Column('batch_id', sa.String(length=26), nullable=False),
    sa.Column('project_id', sa.String(length=26), nullable=False),
    sa.Column('name', sa.String(length=255), nullable=False),
    sa.Column('created_at', sa.DateTime(timezone=True), nullable=False),
    sa.ForeignKeyConstraint(['project_id'], ['projects.project_id'], ),
    sa.PrimaryKeyConstraint('batch_id')
    )
    op.create_index('ix_batches_project_created', 'batches', ['project_id', 'created_at'], unique=False)
    op.create_table('dp_jobs',
    sa.Column('job_id', sa.String(length=26), nullable=False),
    sa.Column('batch_id', sa.String(length=26), nullable=False),
    sa.Column('mode', sa.String(length=20), nullable=False),
    sa.Column('recompute', sa.Integer(), nullable=False),
    sa.Column('status', sa.String(length=20), nullable=False),
    sa.Column('requested_by', sa.String(length=255), nullable=True),
    sa.Column('created_at', sa.DateTime(timezone=True), nullable=False),
    sa.Column('started_at', sa.DateTime(timezone=True), nullable=True),
    sa.Column('finished_at', sa.DateTime(timezone=True), nullable=True),
    sa.Column('error_text', sa.Text(), nullable=True),
    sa.ForeignKeyConstraint(['batch_id'], ['batches.batch_id'], ),
    sa.PrimaryKeyConstraint('job_id')
    )
    op.create_index('ix_dp_jobs_batch_created', 'dp_jobs', ['batch_id', 'created_at'], unique=False)
    op.create_index('ix_dp_jobs_status_created', 'dp_jobs', ['status', 'created_at'], unique=False)
    op.create_table('runs',
    sa.Column('run_id', sa.String(length=26), nullable=False),
    sa.Column('batch_id', sa.String(length=26), nullable=False),
    sa.Column('name', sa.String(length=255), nullable=False),
    sa.Column('status', sa.String(length=20), nullable=False),
    sa.Column('tags_json', sa.Text(), nullable=False),
    sa.Column('note', sa.Text(), nullable=True),
    sa.Column('created_at', sa.DateTime(timezone=True), nullable=False),
    sa.Column('updated_at', sa.DateTime(timezone=True), nullable=False),
    sa.ForeignKeyConstraint(['batch_id'], ['batches.batch_id'], ),
    sa.PrimaryKeyConstraint('run_id')
    )
    op.create_index('ix_runs_batch_created', 'runs', ['batch_id', 'created_at'], unique=False)
    op.create_index('ix_runs_batch_status_created', 'runs', ['batch_id', 'status', 'created_at'], unique=False)
    op.create_table('artifacts',
    sa.Column('artifact_id', sa.String(length=26), nullable=False),
    sa.Column('run_id', sa.String(length=26), nullable=False),
    sa.Column('kind', sa.String(length=20), nullable=False),
    sa.Column('type', sa.String(length=50), nullable=False),
    sa.Column('label', sa.String(length=255), nullable=False),
    sa.Column('payload_text', sa.Text(), nullable=True),
    sa.Column('payload_number', sa.Float(), nullable=True),
    sa.Column('meta_json', sa.Text(), nullable=False),
    sa.Column('created_at', sa.DateTime(timezone=True), nullable=False),
    sa.ForeignKeyConstraint(['run_id'], ['runs.run_id'], ),
    sa.PrimaryKeyConstraint('artifact_id')
    )
    op.create_index('ix_artifacts_run_created', 'artifacts', ['run_id', 'created_at'], unique=False)
    op.create_index('ix_artifacts_type_created', 'artifacts', ['type', 'created_at'], unique=False)
    op.create_table('comparison_indicators',
    sa.Column('ci_id', sa.String(length=26), nullable=False),
    sa.Column('run_id', sa.String(length=26), nullable=False),
    sa.Column('key', sa.String(length=100), nullable=False),
    sa.Column('value_json', sa.Text(), nullable=False),
    sa.Column('baseline_ref', sa.String(length=26), nullable=True),
    sa.Column('computed_at', sa.DateTime(timezone=True), nullable=False),
    sa.Column('version', sa.Integer(), nullable=False),
    sa.ForeignKeyConstraint(['run_id'], ['runs.run_id'], ),
    sa.PrimaryKeyConstraint('ci_id'),
    sa.UniqueConstraint('run_id', 'key', 'version', name='uq_ci_run_key_version')
    )
    op.create_index('ix_comparison_indicators_run_computed', 'comparison_indicators', ['run_id', 'computed_at'], unique=False)
    op.create_table('quality_metrics',
    sa.Column('qm_id', sa.String(length=26), nullable=False),
    sa.Column('run_id', sa.String(length=26), nullable=False),
    sa.Column('key', sa.String(length=100), nullable=False),
    sa.Column('value_json', sa.Text(), nullable=False),
    sa.Column('source', sa.String(length=20), nullable=False),
    sa.Column('computed_at', sa.DateTime(timezone=True), nullable=False),
    sa.Column('version', sa.Integer(), nullable=False),
    sa.ForeignKeyConstraint(['run_id'], ['runs.run_id'], ),
    sa.PrimaryKeyConstraint('qm_id'),
    sa.UniqueConstraint('run_id', 'key', 'version', name='uq_qm_run_key_version')
    )
    op.create_index('ix_quality_metrics_run_computed', 'quality_metrics', ['run_id', 'computed_at'], unique=False)
    op.create_table('run_pins',
    sa.Column('pin_id', sa.String(length=26), nullable=False),
    sa.Column('run_id', sa.String(length=26), nullable=False),
    sa.Column('batch_id', sa.String(length=26), nullable=False),
    sa.Column('pin_type', sa.String(length=20), nullable=False),
    sa.Column('pinned_by', sa.String(length=255), nullable=True),
    sa.Column('pinned_at', sa.DateTime(timezone=True), nullable=False),
    sa.ForeignKeyConstraint(['batch_id'], ['batches.batch_id'], ),
    sa.ForeignKeyConstraint(['run_id'], ['runs.run_id'], ),
    sa.PrimaryKeyConstraint('pin_id'),
    sa.UniqueConstraint('batch_id', 'pin_type', name='uq_batch_champion', sqlite_on_conflict='REPLACE')
    )
    op.create_index('ix_run_pins_batch_type', 'run_pins', ['batch_id', 'pin_type', 'pinned_at'], unique=False)
    # ### end Alembic commands ###


def downgrade() -> None:
    """Downgrade schema."""
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_index('ix_run_pins_batch_type', table_name='run_pins')
    op.drop_table('run_pins')
    op.drop_index('ix_quality_metrics_run_computed', table_name='quality_metrics')
    op.drop_table('quality_metrics')
    op.drop_index('ix_comparison_indicators_run_computed', table_name='comparison_indicators')
    op.drop_table('comparison_indicators')
    op.drop_index('ix_artifacts_type_created', table_name='artifacts')
    op.drop_index('ix_artifacts_run_created', table_name='artifacts')
    op.drop_table('artifacts')
    op.drop_index('ix_runs_batch_status_created', table_name='runs')
    op.drop_index('ix_runs_batch_created', table_name='runs')
    op.drop_table('runs')
    op.drop_index('ix_dp_jobs_status_created', table_name='dp_jobs')
    op.drop_index('ix_dp_jobs_batch_created', table_name='dp_jobs')
    op.drop_table('dp_jobs')
    op.drop_index('ix_batches_project_created', table_name='batches')
    op.drop_table('batches')
    op.drop_index('ix_projects_org_created', table_name='projects')
    op.drop_table('projects')
    op.drop_table('orgs')
    # ### end Alembic commands ###
