"""Add custom fields and task custom values tables Revision ID: 011 Revises: 010 Create Date: 2026-01-05 FEAT-001: Add custom fields feature for flexible task data extension. """ from typing import Sequence, Union from alembic import op import sqlalchemy as sa revision: str = '011' down_revision: Union[str, None] = '010' branch_labels: Union[str, Sequence[str], None] = None depends_on: Union[str, Sequence[str], None] = None def upgrade() -> None: # Create field_type_enum field_type_enum = sa.Enum( 'text', 'number', 'dropdown', 'date', 'person', 'formula', name='field_type_enum' ) field_type_enum.create(op.get_bind(), checkfirst=True) # Create pjctrl_custom_fields table op.create_table( 'pjctrl_custom_fields', sa.Column('id', sa.String(36), primary_key=True), sa.Column('project_id', sa.String(36), sa.ForeignKey('pjctrl_projects.id', ondelete='CASCADE'), nullable=False), sa.Column('name', sa.String(100), nullable=False), sa.Column('field_type', field_type_enum, nullable=False), sa.Column('options', sa.JSON, nullable=True), sa.Column('formula', sa.Text, nullable=True), sa.Column('is_required', sa.Boolean, default=False, nullable=False), sa.Column('position', sa.Integer, default=0, nullable=False), sa.Column('created_at', sa.DateTime, server_default=sa.func.now(), nullable=False), sa.Column('updated_at', sa.DateTime, server_default=sa.func.now(), onupdate=sa.func.now(), nullable=False), ) # Create indexes for custom_fields op.create_index('ix_pjctrl_custom_fields_project_id', 'pjctrl_custom_fields', ['project_id']) # Create pjctrl_task_custom_values table op.create_table( 'pjctrl_task_custom_values', sa.Column('id', sa.String(36), primary_key=True), sa.Column('task_id', sa.String(36), sa.ForeignKey('pjctrl_tasks.id', ondelete='CASCADE'), nullable=False), sa.Column('field_id', sa.String(36), sa.ForeignKey('pjctrl_custom_fields.id', ondelete='CASCADE'), nullable=False), sa.Column('value', sa.Text, nullable=True), sa.Column('updated_at', sa.DateTime, server_default=sa.func.now(), onupdate=sa.func.now(), nullable=False), sa.UniqueConstraint('task_id', 'field_id', name='uq_task_field'), ) # Create indexes for task_custom_values op.create_index('ix_pjctrl_task_custom_values_task_id', 'pjctrl_task_custom_values', ['task_id']) op.create_index('ix_pjctrl_task_custom_values_field_id', 'pjctrl_task_custom_values', ['field_id']) def downgrade() -> None: op.drop_index('ix_pjctrl_task_custom_values_field_id', table_name='pjctrl_task_custom_values') op.drop_index('ix_pjctrl_task_custom_values_task_id', table_name='pjctrl_task_custom_values') op.drop_table('pjctrl_task_custom_values') op.drop_index('ix_pjctrl_custom_fields_project_id', table_name='pjctrl_custom_fields') op.drop_table('pjctrl_custom_fields') # Drop the enum type sa.Enum(name='field_type_enum').drop(op.get_bind(), checkfirst=True)