"""v0.0.3

Revision ID: e4165ed1da22
Revises:
Create Date: 2026-01-31 19:12:22.133688

"""
from typing import Sequence, Union

import sqlmodel
from alembic import op
import sqlalchemy as sa

import app

# revision identifiers, used by Alembic.
revision: str = 'e4165ed1da22'
down_revision: Union[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('app',
    sa.Column('create_time', sa.DateTime(), nullable=False, comment='Created time'),
    sa.Column('update_time', sa.DateTime(), nullable=False, comment='Updated time'),
    sa.Column('id', app.api.models.base.GUID(), nullable=False),
    sa.Column('tenant_id', app.api.models.base.GUID(), nullable=False),
    sa.Column('workspace_id', app.api.models.base.GUID(), nullable=False),
    sa.Column('user_id', app.api.models.base.GUID(), nullable=False),
    sa.Column('app_model_config_id', app.api.models.base.GUID(), nullable=True),
    sa.Column('name', sqlmodel.sql.sqltypes.AutoString(length=255), nullable=False),
    sa.Column('mode', sqlmodel.sql.sqltypes.AutoString(length=255), nullable=False),
    sa.Column('description', sqlmodel.sql.sqltypes.AutoString(), nullable=True),
    sa.Column('icon', sqlmodel.sql.sqltypes.AutoString(), nullable=True),
    sa.Column('status', sa.Integer(), server_default=sa.text('1'), nullable=False),
    sa.Column('scope', sa.Integer(), server_default=sa.text('2'), nullable=True),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_table('book',
    sa.Column('create_time', sa.DateTime(), nullable=False, comment='Created time'),
    sa.Column('update_time', sa.DateTime(), nullable=False, comment='Updated time'),
    sa.Column('id', app.api.models.base.GUID(), nullable=False),
    sa.Column('tenant_id', app.api.models.base.GUID(), nullable=False),
    sa.Column('workspace_id', app.api.models.base.GUID(), nullable=False),
    sa.Column('user_id', app.api.models.base.GUID(), nullable=False),
    sa.Column('uuid', sqlmodel.sql.sqltypes.AutoString(length=64), nullable=False),
    sa.Column('title', sqlmodel.sql.sqltypes.AutoString(length=255), nullable=False),
    sa.Column('title_pinyin', sqlmodel.sql.sqltypes.AutoString(), nullable=True),
    sa.Column('path', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
    sa.Column('file_name', sqlmodel.sql.sqltypes.AutoString(), nullable=True),
    sa.Column('cover_name', sqlmodel.sql.sqltypes.AutoString(), nullable=True),
    sa.Column('extension', sqlmodel.sql.sqltypes.AutoString(), nullable=True),
    sa.Column('rating', sa.Integer(), nullable=True),
    sa.Column('author', sqlmodel.sql.sqltypes.AutoString(), nullable=True),
    sa.Column('language', sqlmodel.sql.sqltypes.AutoString(), nullable=True),
    sa.Column('description', sqlmodel.sql.sqltypes.AutoString(), nullable=True),
    sa.Column('publisher', sqlmodel.sql.sqltypes.AutoString(), nullable=True),
    sa.Column('published', sqlmodel.sql.sqltypes.AutoString(), nullable=True),
    sa.Column('scope', sa.Integer(), server_default=sa.text('2'), nullable=True),
    sa.PrimaryKeyConstraint('id'),
    sa.UniqueConstraint('tenant_id', 'uuid', name='unique_tenant_book')
    )
    op.create_table('book_annotation',
    sa.Column('create_time', sa.DateTime(), nullable=False, comment='Created time'),
    sa.Column('update_time', sa.DateTime(), nullable=False, comment='Updated time'),
    sa.Column('id', app.api.models.base.GUID(), nullable=False),
    sa.Column('tenant_id', app.api.models.base.GUID(), nullable=False),
    sa.Column('workspace_id', app.api.models.base.GUID(), nullable=False),
    sa.Column('user_id', app.api.models.base.GUID(), nullable=False),
    sa.Column('book_id', app.api.models.base.GUID(), nullable=False),
    sa.Column('type', sqlmodel.sql.sqltypes.AutoString(), nullable=True),
    sa.Column('value', sqlmodel.sql.sqltypes.AutoString(), nullable=True),
    sa.Column('note', sqlmodel.sql.sqltypes.AutoString(), nullable=True),
    sa.Column('color', sqlmodel.sql.sqltypes.AutoString(), nullable=True),
    sa.Column('chapter', sqlmodel.sql.sqltypes.AutoString(), nullable=True),
    sa.Column('page', sa.Integer(), nullable=True),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_table('book_collection',
    sa.Column('create_time', sa.DateTime(), nullable=False, comment='Created time'),
    sa.Column('update_time', sa.DateTime(), nullable=False, comment='Updated time'),
    sa.Column('id', app.api.models.base.GUID(), nullable=False),
    sa.Column('workspace_id', app.api.models.base.GUID(), nullable=False),
    sa.Column('user_id', app.api.models.base.GUID(), nullable=False),
    sa.Column('parent', app.api.models.base.GUID(), nullable=True),
    sa.Column('title', sqlmodel.sql.sqltypes.AutoString(length=255), nullable=False),
    sa.Column('icon', sqlmodel.sql.sqltypes.AutoString(), nullable=True),
    sa.Column('cover', sqlmodel.sql.sqltypes.AutoString(), nullable=True),
    sa.Column('color', sqlmodel.sql.sqltypes.AutoString(), nullable=True),
    sa.Column('position', sa.Integer(), nullable=True),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_table('conversation',
    sa.Column('create_time', sa.DateTime(), nullable=False, comment='Created time'),
    sa.Column('update_time', sa.DateTime(), nullable=False, comment='Updated time'),
    sa.Column('id', app.api.models.base.GUID(), nullable=False),
    sa.Column('workspace_id', app.api.models.base.GUID(), nullable=False),
    sa.Column('user_id', app.api.models.base.GUID(), nullable=False),
    sa.Column('app_id', app.api.models.base.GUID(), nullable=False),
    sa.Column('model_provider', sqlmodel.sql.sqltypes.AutoString(), nullable=True),
    sa.Column('model_name', sqlmodel.sql.sqltypes.AutoString(), nullable=True),
    sa.Column('model_type', sqlmodel.sql.sqltypes.AutoString(), nullable=True),
    sa.Column('name', sqlmodel.sql.sqltypes.AutoString(length=255), nullable=False),
    sa.Column('favorite', sa.Integer(), nullable=True),
    sa.Column('status', sa.Integer(), nullable=True),
    sa.Column('ref_id', sqlmodel.sql.sqltypes.AutoString(), nullable=True),
    sa.Column('ref_type', sqlmodel.sql.sqltypes.AutoString(), nullable=True),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_table('database_cache',
    sa.Column('create_time', sa.DateTime(), nullable=False, comment='Created time'),
    sa.Column('update_time', sa.DateTime(), nullable=False, comment='Updated time'),
    sa.Column('id', app.api.models.base.GUID(), nullable=False),
    sa.Column('key', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
    sa.Column('value', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
    sa.PrimaryKeyConstraint('id'),
    sa.UniqueConstraint('key', name='unique_key')
    )
    op.create_table('file_meta',
    sa.Column('create_time', sa.DateTime(), nullable=False, comment='Created time'),
    sa.Column('update_time', sa.DateTime(), nullable=False, comment='Updated time'),
    sa.Column('id', app.api.models.base.GUID(), nullable=False),
    sa.Column('workspace_id', app.api.models.base.GUID(), nullable=False),
    sa.Column('user_id', app.api.models.base.GUID(), nullable=False),
    sa.Column('mimetype', sqlmodel.sql.sqltypes.AutoString(), nullable=True),
    sa.Column('size', sa.Integer(), nullable=True),
    sa.Column('original_name', sqlmodel.sql.sqltypes.AutoString(), nullable=True),
    sa.Column('file_name', sqlmodel.sql.sqltypes.AutoString(), nullable=True),
    sa.Column('url', sqlmodel.sql.sqltypes.AutoString(), nullable=True),
    sa.Column('path', sqlmodel.sql.sqltypes.AutoString(), nullable=True),
    sa.Column('ref_id', sqlmodel.sql.sqltypes.AutoString(), nullable=True),
    sa.Column('ref_type', sqlmodel.sql.sqltypes.AutoString(), nullable=True),
    sa.Column('status', sa.Integer(), nullable=True),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_table('message',
    sa.Column('create_time', sa.DateTime(), nullable=False, comment='Created time'),
    sa.Column('update_time', sa.DateTime(), nullable=False, comment='Updated time'),
    sa.Column('id', app.api.models.base.GUID(), nullable=False),
    sa.Column('workspace_id', app.api.models.base.GUID(), nullable=False),
    sa.Column('user_id', app.api.models.base.GUID(), nullable=False),
    sa.Column('app_id', app.api.models.base.GUID(), nullable=False),
    sa.Column('conversation_id', app.api.models.base.GUID(), nullable=False),
    sa.Column('model_provider', sqlmodel.sql.sqltypes.AutoString(), nullable=True),
    sa.Column('model_name', sqlmodel.sql.sqltypes.AutoString(), nullable=True),
    sa.Column('model_type', sqlmodel.sql.sqltypes.AutoString(), nullable=True),
    sa.Column('message', sqlmodel.sql.sqltypes.AutoString(), nullable=True),
    sa.Column('content', sqlmodel.sql.sqltypes.AutoString(), nullable=True),
    sa.Column('reasoning_content', sqlmodel.sql.sqltypes.AutoString(), nullable=True),
    sa.Column('result', sa.Integer(), nullable=True),
    sa.Column('total_tokens', sa.Integer(), nullable=True),
    sa.Column('favorite', sa.Integer(), nullable=True),
    sa.Column('status', sa.Integer(), nullable=True),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_table('note',
    sa.Column('create_time', sa.DateTime(), nullable=False, comment='Created time'),
    sa.Column('update_time', sa.DateTime(), nullable=False, comment='Updated time'),
    sa.Column('id', app.api.models.base.GUID(), nullable=False),
    sa.Column('workspace_id', app.api.models.base.GUID(), nullable=False),
    sa.Column('user_id', app.api.models.base.GUID(), nullable=False),
    sa.Column('parent', app.api.models.base.GUID(), nullable=True),
    sa.Column('title', sqlmodel.sql.sqltypes.AutoString(length=255), nullable=False),
    sa.Column('content', app.api.models.base.JSONString(), nullable=False),
    sa.Column('content_markdown', sqlmodel.sql.sqltypes.AutoString(), nullable=True),
    sa.Column('doc', sa.LargeBinary(), nullable=True),
    sa.Column('icon', sqlmodel.sql.sqltypes.AutoString(), nullable=True),
    sa.Column('cover', sqlmodel.sql.sqltypes.AutoString(), nullable=True),
    sa.Column('favorite', sa.Integer(), nullable=True),
    sa.Column('styles', app.api.models.base.JSONString(), nullable=True),
    sa.Column('ref_id', sqlmodel.sql.sqltypes.AutoString(), nullable=True),
    sa.Column('ref_type', sqlmodel.sql.sqltypes.AutoString(), nullable=True),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_table('note_version',
    sa.Column('create_time', sa.DateTime(), nullable=False, comment='Created time'),
    sa.Column('update_time', sa.DateTime(), nullable=False, comment='Updated time'),
    sa.Column('id', app.api.models.base.GUID(), nullable=False),
    sa.Column('workspace_id', app.api.models.base.GUID(), nullable=False),
    sa.Column('user_id', app.api.models.base.GUID(), nullable=False),
    sa.Column('note_id', app.api.models.base.GUID(), nullable=False),
    sa.Column('content', app.api.models.base.JSONString(), nullable=True),
    sa.Column('doc', sa.LargeBinary(), nullable=True),
    sa.Column('title', sqlmodel.sql.sqltypes.AutoString(length=255), nullable=True),
    sa.Column('icon', sqlmodel.sql.sqltypes.AutoString(), nullable=True),
    sa.Column('cover', sqlmodel.sql.sqltypes.AutoString(), nullable=True),
    sa.Column('styles', app.api.models.base.JSONString(), nullable=True),
    sa.Column('type', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
    sa.Column('remarks', sqlmodel.sql.sqltypes.AutoString(), nullable=True),
    sa.PrimaryKeyConstraint('id')
    )
    with op.batch_alter_table('note_version', schema=None) as batch_op:
        batch_op.create_index('idx_note_version_note_id', ['note_id'], unique=False)

    op.create_table('provider',
    sa.Column('create_time', sa.DateTime(), nullable=False, comment='Created time'),
    sa.Column('update_time', sa.DateTime(), nullable=False, comment='Updated time'),
    sa.Column('id', app.api.models.base.GUID(), nullable=False),
    sa.Column('workspace_id', app.api.models.base.GUID(), nullable=False),
    sa.Column('provider', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
    sa.Column('credential_id', app.api.models.base.GUID(), nullable=True),
    sa.PrimaryKeyConstraint('id'),
    sa.UniqueConstraint('workspace_id', 'provider', name='unique_workspace_provider')
    )
    op.create_table('provider_credential',
    sa.Column('create_time', sa.DateTime(), nullable=False, comment='Created time'),
    sa.Column('update_time', sa.DateTime(), nullable=False, comment='Updated time'),
    sa.Column('id', app.api.models.base.GUID(), nullable=False),
    sa.Column('workspace_id', app.api.models.base.GUID(), nullable=False),
    sa.Column('provider', sqlmodel.sql.sqltypes.AutoString(length=255), nullable=False),
    sa.Column('name', sqlmodel.sql.sqltypes.AutoString(length=255), nullable=False),
    sa.Column('credential', app.api.models.base.JSONString(), nullable=False),
    sa.PrimaryKeyConstraint('id'),
    sa.UniqueConstraint('workspace_id', 'provider', 'name', name='unique_workspace_provider_name')
    )
    op.create_table('provider_default_model',
    sa.Column('create_time', sa.DateTime(), nullable=False, comment='Created time'),
    sa.Column('update_time', sa.DateTime(), nullable=False, comment='Updated time'),
    sa.Column('id', app.api.models.base.GUID(), nullable=False),
    sa.Column('workspace_id', app.api.models.base.GUID(), nullable=False),
    sa.Column('provider', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
    sa.Column('model_name', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
    sa.Column('model_type', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
    sa.PrimaryKeyConstraint('id'),
    sa.UniqueConstraint('workspace_id', 'model_type', name='unique_workspace_provider_model_type')
    )
    op.create_table('tenant',
    sa.Column('create_time', sa.DateTime(), nullable=False, comment='Created time'),
    sa.Column('update_time', sa.DateTime(), nullable=False, comment='Updated time'),
    sa.Column('id', app.api.models.base.GUID(), nullable=False),
    sa.Column('user_id', app.api.models.base.GUID(), nullable=False),
    sa.Column('name', sqlmodel.sql.sqltypes.AutoString(length=100), nullable=False),
    sa.Column('icon', sqlmodel.sql.sqltypes.AutoString(), nullable=True),
    sa.Column('plan', sa.String(length=32), nullable=True),
    sa.Column('type', sa.String(length=32), nullable=True),
    sa.Column('public_key', sqlmodel.sql.sqltypes.AutoString(length=255), nullable=True),
    sa.Column('status', sa.Integer(), nullable=False),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_table('user',
    sa.Column('create_time', sa.DateTime(), nullable=False, comment='Created time'),
    sa.Column('update_time', sa.DateTime(), nullable=False, comment='Updated time'),
    sa.Column('id', app.api.models.base.GUID(), nullable=False),
    sa.Column('name', sqlmodel.sql.sqltypes.AutoString(length=100), nullable=False),
    sa.Column('email', sqlmodel.sql.sqltypes.AutoString(length=255), nullable=False),
    sa.Column('password', sqlmodel.sql.sqltypes.AutoString(length=255), nullable=False),
    sa.Column('password_salt', sqlmodel.sql.sqltypes.AutoString(length=255), nullable=False),
    sa.Column('avatar', sqlmodel.sql.sqltypes.AutoString(length=255), nullable=True),
    sa.Column('bio', sqlmodel.sql.sqltypes.AutoString(length=255), nullable=True),
    sa.Column('timezone', sqlmodel.sql.sqltypes.AutoString(length=255), nullable=True),
    sa.Column('status', sa.Integer(), nullable=True),
    sa.Column('settings', sqlmodel.sql.sqltypes.AutoString(), nullable=True),
    sa.Column('last_login_time', sa.DateTime(), nullable=True),
    sa.Column('last_login_ip', sqlmodel.sql.sqltypes.AutoString(length=64), nullable=True),
    sa.Column('last_active_time', sa.DateTime(), nullable=True),
    sa.PrimaryKeyConstraint('id'),
    sa.UniqueConstraint('email', name='unique_email')
    )
    op.create_table('user_book',
    sa.Column('create_time', sa.DateTime(), nullable=False, comment='Created time'),
    sa.Column('update_time', sa.DateTime(), nullable=False, comment='Updated time'),
    sa.Column('id', app.api.models.base.GUID(), nullable=False),
    sa.Column('book_id', app.api.models.base.GUID(), nullable=False),
    sa.Column('user_id', app.api.models.base.GUID(), nullable=False),
    sa.Column('rating', sa.Integer(), nullable=True),
    sa.Column('reading_position', sqlmodel.sql.sqltypes.AutoString(), nullable=True),
    sa.Column('reading_percentage', sa.Float(), nullable=True),
    sa.Column('reading_status', sa.Integer(), server_default=sa.text('0'), nullable=False),
    sa.Column('reading_status_time', sa.DateTime(), nullable=True),
    sa.Column('tags', sa.String(), server_default='[]', nullable=False),
    sa.PrimaryKeyConstraint('id'),
    sa.UniqueConstraint('user_id', 'book_id', name='unique_user_book')
    )
    op.create_table('workspace',
    sa.Column('create_time', sa.DateTime(), nullable=False, comment='Created time'),
    sa.Column('update_time', sa.DateTime(), nullable=False, comment='Updated time'),
    sa.Column('id', app.api.models.base.GUID(), nullable=False),
    sa.Column('tenant_id', app.api.models.base.GUID(), nullable=False),
    sa.Column('user_id', app.api.models.base.GUID(), nullable=False),
    sa.Column('name', sqlmodel.sql.sqltypes.AutoString(length=100), nullable=False),
    sa.Column('icon', sqlmodel.sql.sqltypes.AutoString(), nullable=True),
    sa.Column('color', sqlmodel.sql.sqltypes.AutoString(), nullable=True),
    sa.Column('desc', sqlmodel.sql.sqltypes.AutoString(length=256), nullable=True),
    sa.Column('type', sa.String(length=32), nullable=True),
    sa.Column('status', sa.Integer(), nullable=False),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_table('workspace_book',
    sa.Column('create_time', sa.DateTime(), nullable=False, comment='Created time'),
    sa.Column('update_time', sa.DateTime(), nullable=False, comment='Updated time'),
    sa.Column('id', app.api.models.base.GUID(), nullable=False),
    sa.Column('workspace_id', app.api.models.base.GUID(), nullable=False),
    sa.Column('book_id', app.api.models.base.GUID(), nullable=False),
    sa.Column('user_id', app.api.models.base.GUID(), nullable=False),
    sa.PrimaryKeyConstraint('id'),
    sa.UniqueConstraint('workspace_id', 'book_id', 'user_id', name='unique_workspace_user_book')
    )
    op.create_table('workspace_book_collection',
    sa.Column('create_time', sa.DateTime(), nullable=False, comment='Created time'),
    sa.Column('update_time', sa.DateTime(), nullable=False, comment='Updated time'),
    sa.Column('id', app.api.models.base.GUID(), nullable=False),
    sa.Column('workspace_id', app.api.models.base.GUID(), nullable=False),
    sa.Column('book_collection_id', app.api.models.base.GUID(), nullable=False),
    sa.Column('workspace_book_id', app.api.models.base.GUID(), nullable=False),
    sa.PrimaryKeyConstraint('id'),
    sa.UniqueConstraint('workspace_book_id', 'book_collection_id', name='unique_workspace_book_collection')
    )
    op.create_table('workspace_member',
    sa.Column('create_time', sa.DateTime(), nullable=False, comment='Created time'),
    sa.Column('update_time', sa.DateTime(), nullable=False, comment='Updated time'),
    sa.Column('id', app.api.models.base.GUID(), nullable=False),
    sa.Column('workspace_id', app.api.models.base.GUID(), nullable=False),
    sa.Column('user_id', app.api.models.base.GUID(), nullable=False),
    sa.Column('role', sqlmodel.sql.sqltypes.AutoString(length=32), nullable=False),
    sa.Column('invited_by', app.api.models.base.GUID(), nullable=True),
    sa.Column('status', sa.Integer(), nullable=True),
    sa.PrimaryKeyConstraint('id'),
    sa.UniqueConstraint('workspace_id', 'user_id', name='unique_workspace_member')
    )
    # ### end Alembic commands ###


def downgrade() -> None:
    """Downgrade schema."""
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_table('workspace_member')
    op.drop_table('workspace_book_collection')
    op.drop_table('workspace_book')
    op.drop_table('workspace')
    op.drop_table('user_book')
    op.drop_table('user')
    op.drop_table('tenant')
    op.drop_table('provider_default_model')
    op.drop_table('provider_credential')
    op.drop_table('provider')
    with op.batch_alter_table('note_version', schema=None) as batch_op:
        batch_op.drop_index('idx_note_version_note_id')

    op.drop_table('note_version')
    op.drop_table('note')
    op.drop_table('message')
    op.drop_table('file_meta')
    op.drop_table('database_cache')
    op.drop_table('conversation')
    op.drop_table('book_collection')
    op.drop_table('book_annotation')
    op.drop_table('book')
    op.drop_table('app')
    # ### end Alembic commands ###
