211 lines
10 KiB
Python
211 lines
10 KiB
Python
"""notebook schema
|
|
|
|
Revision ID: 1e1450c12dd7
|
|
Revises:
|
|
Create Date: 2026-07-26 23:25:10.030845
|
|
|
|
"""
|
|
from typing import Sequence, Union
|
|
|
|
from alembic import op
|
|
import sqlalchemy as sa
|
|
|
|
|
|
# revision identifiers, used by Alembic.
|
|
revision: str = '1e1450c12dd7'
|
|
down_revision: Union[str, Sequence[str], None] = None
|
|
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('users',
|
|
sa.Column('id', sa.String(length=36), nullable=False),
|
|
sa.Column('username', sa.String(length=64), nullable=False),
|
|
sa.Column('email', sa.String(length=255), nullable=True),
|
|
sa.Column('password_hash', sa.String(length=255), nullable=False),
|
|
sa.Column('is_active', sa.Boolean(), nullable=False),
|
|
sa.Column('created_at', sa.DateTime(), nullable=False),
|
|
sa.Column('updated_at', sa.DateTime(), nullable=False),
|
|
sa.PrimaryKeyConstraint('id'),
|
|
sa.UniqueConstraint('email')
|
|
)
|
|
with op.batch_alter_table('users', schema=None) as batch_op:
|
|
batch_op.create_index(batch_op.f('ix_users_username'), ['username'], unique=True)
|
|
|
|
op.create_table('ai_generations',
|
|
sa.Column('id', sa.String(length=36), nullable=False),
|
|
sa.Column('user_id', sa.String(length=36), nullable=False),
|
|
sa.Column('question_id', sa.String(length=36), nullable=True),
|
|
sa.Column('task', sa.String(length=32), nullable=False),
|
|
sa.Column('model', sa.String(length=128), nullable=True),
|
|
sa.Column('request_tokens', sa.Integer(), nullable=True),
|
|
sa.Column('response_tokens', sa.Integer(), nullable=True),
|
|
sa.Column('content_markdown', sa.Text(), nullable=True),
|
|
sa.Column('created_at', sa.DateTime(), nullable=False),
|
|
sa.Column('updated_at', sa.DateTime(), nullable=False),
|
|
sa.ForeignKeyConstraint(['user_id'], ['users.id'], ondelete='CASCADE'),
|
|
sa.PrimaryKeyConstraint('id')
|
|
)
|
|
with op.batch_alter_table('ai_generations', schema=None) as batch_op:
|
|
batch_op.create_index(batch_op.f('ix_ai_generations_user_id'), ['user_id'], unique=False)
|
|
|
|
op.create_table('images',
|
|
sa.Column('id', sa.String(length=36), nullable=False),
|
|
sa.Column('user_id', sa.String(length=36), nullable=False),
|
|
sa.Column('object_key', sa.String(length=512), nullable=False),
|
|
sa.Column('mime', sa.String(length=64), nullable=False),
|
|
sa.Column('size_bytes', sa.BigInteger(), nullable=False),
|
|
sa.Column('sha256', sa.String(length=64), nullable=False),
|
|
sa.Column('ocr_markdown', sa.Text(), nullable=True),
|
|
sa.Column('created_at', sa.DateTime(), nullable=False),
|
|
sa.Column('updated_at', sa.DateTime(), nullable=False),
|
|
sa.ForeignKeyConstraint(['user_id'], ['users.id'], ondelete='CASCADE'),
|
|
sa.PrimaryKeyConstraint('id')
|
|
)
|
|
with op.batch_alter_table('images', schema=None) as batch_op:
|
|
batch_op.create_index(batch_op.f('ix_images_sha256'), ['sha256'], unique=False)
|
|
batch_op.create_index(batch_op.f('ix_images_user_id'), ['user_id'], unique=False)
|
|
|
|
op.create_table('tags',
|
|
sa.Column('id', sa.String(length=36), nullable=False),
|
|
sa.Column('user_id', sa.String(length=36), nullable=False),
|
|
sa.Column('name', sa.String(length=128), nullable=False),
|
|
sa.Column('created_at', sa.DateTime(), nullable=False),
|
|
sa.Column('updated_at', sa.DateTime(), nullable=False),
|
|
sa.ForeignKeyConstraint(['user_id'], ['users.id'], ondelete='CASCADE'),
|
|
sa.PrimaryKeyConstraint('id'),
|
|
sa.UniqueConstraint('user_id', 'name', name='uq_tag_user_name')
|
|
)
|
|
with op.batch_alter_table('tags', schema=None) as batch_op:
|
|
batch_op.create_index(batch_op.f('ix_tags_user_id'), ['user_id'], unique=False)
|
|
|
|
op.create_table('user_settings',
|
|
sa.Column('id', sa.String(length=36), nullable=False),
|
|
sa.Column('user_id', sa.String(length=36), nullable=False),
|
|
sa.Column('llm_base_url', sa.String(length=512), nullable=True),
|
|
sa.Column('llm_api_key_encrypted', sa.Text(), nullable=True),
|
|
sa.Column('llm_text_model', sa.String(length=128), nullable=True),
|
|
sa.Column('llm_vision_model', sa.String(length=128), nullable=True),
|
|
sa.Column('created_at', sa.DateTime(), nullable=False),
|
|
sa.Column('updated_at', sa.DateTime(), nullable=False),
|
|
sa.ForeignKeyConstraint(['user_id'], ['users.id'], ondelete='CASCADE'),
|
|
sa.PrimaryKeyConstraint('id')
|
|
)
|
|
with op.batch_alter_table('user_settings', schema=None) as batch_op:
|
|
batch_op.create_index(batch_op.f('ix_user_settings_user_id'), ['user_id'], unique=True)
|
|
|
|
op.create_table('processing_jobs',
|
|
sa.Column('id', sa.String(length=36), nullable=False),
|
|
sa.Column('user_id', sa.String(length=36), nullable=False),
|
|
sa.Column('image_id', sa.String(length=36), nullable=True),
|
|
sa.Column('kind', sa.String(length=32), nullable=False),
|
|
sa.Column('status', sa.Enum('pending', 'ocr_running', 'ai_running', 'done', 'failed', name='jobstatus'), nullable=False),
|
|
sa.Column('result_question_ids', sa.JSON(), nullable=True),
|
|
sa.Column('error_message', sa.Text(), nullable=True),
|
|
sa.Column('engine_used', sa.String(length=32), nullable=True),
|
|
sa.Column('created_at', sa.DateTime(), nullable=False),
|
|
sa.Column('updated_at', sa.DateTime(), nullable=False),
|
|
sa.ForeignKeyConstraint(['image_id'], ['images.id'], ondelete='SET NULL'),
|
|
sa.ForeignKeyConstraint(['user_id'], ['users.id'], ondelete='CASCADE'),
|
|
sa.PrimaryKeyConstraint('id')
|
|
)
|
|
with op.batch_alter_table('processing_jobs', schema=None) as batch_op:
|
|
batch_op.create_index(batch_op.f('ix_processing_jobs_user_id'), ['user_id'], unique=False)
|
|
|
|
op.create_table('questions',
|
|
sa.Column('id', sa.String(length=36), nullable=False),
|
|
sa.Column('user_id', sa.String(length=36), nullable=False),
|
|
sa.Column('type', sa.Enum('unclassified', 'single_choice', 'multiple_choice', 'true_false', 'fill_blank', 'short_answer', name='questiontype'), nullable=False),
|
|
sa.Column('stem_markdown', sa.Text(), nullable=False),
|
|
sa.Column('options', sa.JSON(), nullable=True),
|
|
sa.Column('correct_answer', sa.JSON(), nullable=True),
|
|
sa.Column('explanation_markdown', sa.Text(), nullable=True),
|
|
sa.Column('my_note_markdown', sa.Text(), nullable=True),
|
|
sa.Column('needs_review', sa.Boolean(), nullable=False),
|
|
sa.Column('difficulty', sa.Integer(), nullable=True),
|
|
sa.Column('source_image_id', sa.String(length=36), nullable=True),
|
|
sa.Column('ocr_engine', sa.String(length=32), nullable=True),
|
|
sa.Column('created_at', sa.DateTime(), nullable=False),
|
|
sa.Column('updated_at', sa.DateTime(), nullable=False),
|
|
sa.ForeignKeyConstraint(['source_image_id'], ['images.id'], ondelete='SET NULL'),
|
|
sa.ForeignKeyConstraint(['user_id'], ['users.id'], ondelete='CASCADE'),
|
|
sa.PrimaryKeyConstraint('id')
|
|
)
|
|
with op.batch_alter_table('questions', schema=None) as batch_op:
|
|
batch_op.create_index(batch_op.f('ix_questions_needs_review'), ['needs_review'], unique=False)
|
|
batch_op.create_index(batch_op.f('ix_questions_source_image_id'), ['source_image_id'], unique=False)
|
|
batch_op.create_index(batch_op.f('ix_questions_user_id'), ['user_id'], unique=False)
|
|
|
|
op.create_table('practice_records',
|
|
sa.Column('id', sa.String(length=36), nullable=False),
|
|
sa.Column('user_id', sa.String(length=36), nullable=False),
|
|
sa.Column('question_id', sa.String(length=36), nullable=False),
|
|
sa.Column('user_answer', sa.JSON(), nullable=True),
|
|
sa.Column('is_correct', sa.Boolean(), nullable=True),
|
|
sa.Column('judged_by', sa.Enum('auto', 'ai', 'self_', name='judgedby'), nullable=False),
|
|
sa.Column('ai_feedback_markdown', sa.Text(), nullable=True),
|
|
sa.Column('created_at', sa.DateTime(), nullable=False),
|
|
sa.Column('updated_at', sa.DateTime(), nullable=False),
|
|
sa.ForeignKeyConstraint(['question_id'], ['questions.id'], ondelete='CASCADE'),
|
|
sa.ForeignKeyConstraint(['user_id'], ['users.id'], ondelete='CASCADE'),
|
|
sa.PrimaryKeyConstraint('id')
|
|
)
|
|
with op.batch_alter_table('practice_records', schema=None) as batch_op:
|
|
batch_op.create_index(batch_op.f('ix_practice_records_question_id'), ['question_id'], unique=False)
|
|
batch_op.create_index(batch_op.f('ix_practice_records_user_id'), ['user_id'], unique=False)
|
|
|
|
op.create_table('question_tags',
|
|
sa.Column('question_id', sa.String(length=36), nullable=False),
|
|
sa.Column('tag_id', sa.String(length=36), nullable=False),
|
|
sa.ForeignKeyConstraint(['question_id'], ['questions.id'], ondelete='CASCADE'),
|
|
sa.ForeignKeyConstraint(['tag_id'], ['tags.id'], ondelete='CASCADE'),
|
|
sa.PrimaryKeyConstraint('question_id', 'tag_id')
|
|
)
|
|
# ### end Alembic commands ###
|
|
|
|
|
|
def downgrade() -> None:
|
|
"""Downgrade schema."""
|
|
# ### commands auto generated by Alembic - please adjust! ###
|
|
op.drop_table('question_tags')
|
|
with op.batch_alter_table('practice_records', schema=None) as batch_op:
|
|
batch_op.drop_index(batch_op.f('ix_practice_records_user_id'))
|
|
batch_op.drop_index(batch_op.f('ix_practice_records_question_id'))
|
|
|
|
op.drop_table('practice_records')
|
|
with op.batch_alter_table('questions', schema=None) as batch_op:
|
|
batch_op.drop_index(batch_op.f('ix_questions_user_id'))
|
|
batch_op.drop_index(batch_op.f('ix_questions_source_image_id'))
|
|
batch_op.drop_index(batch_op.f('ix_questions_needs_review'))
|
|
|
|
op.drop_table('questions')
|
|
with op.batch_alter_table('processing_jobs', schema=None) as batch_op:
|
|
batch_op.drop_index(batch_op.f('ix_processing_jobs_user_id'))
|
|
|
|
op.drop_table('processing_jobs')
|
|
with op.batch_alter_table('user_settings', schema=None) as batch_op:
|
|
batch_op.drop_index(batch_op.f('ix_user_settings_user_id'))
|
|
|
|
op.drop_table('user_settings')
|
|
with op.batch_alter_table('tags', schema=None) as batch_op:
|
|
batch_op.drop_index(batch_op.f('ix_tags_user_id'))
|
|
|
|
op.drop_table('tags')
|
|
with op.batch_alter_table('images', schema=None) as batch_op:
|
|
batch_op.drop_index(batch_op.f('ix_images_user_id'))
|
|
batch_op.drop_index(batch_op.f('ix_images_sha256'))
|
|
|
|
op.drop_table('images')
|
|
with op.batch_alter_table('ai_generations', schema=None) as batch_op:
|
|
batch_op.drop_index(batch_op.f('ix_ai_generations_user_id'))
|
|
|
|
op.drop_table('ai_generations')
|
|
with op.batch_alter_table('users', schema=None) as batch_op:
|
|
batch_op.drop_index(batch_op.f('ix_users_username'))
|
|
|
|
op.drop_table('users')
|
|
# ### end Alembic commands ###
|