|
| 1 | +""" |
| 2 | +Add ticket and comment tables |
| 3 | +""" |
| 4 | +from alembic import op |
| 5 | +import sqlalchemy as sa |
| 6 | +import sqlmodel.sql.sqltypes |
| 7 | +from sqlalchemy.dialects.postgresql import UUID |
| 8 | +from uuid import uuid4 |
| 9 | + |
| 10 | + |
| 11 | +# revision identifiers, used by Alembic. |
| 12 | +revision = 'f23a9c45d178' |
| 13 | +down_revision = '1a31ce608336' |
| 14 | +branch_labels = None |
| 15 | +depends_on = None |
| 16 | + |
| 17 | + |
| 18 | +def upgrade(): |
| 19 | + # Create ticket table |
| 20 | + op.create_table( |
| 21 | + 'ticket', |
| 22 | + sa.Column("id", UUID(), nullable=False, server_default=sa.text("gen_random_uuid()")), |
| 23 | + sa.Column("title", sqlmodel.sql.sqltypes.AutoString(), nullable=False), |
| 24 | + sa.Column("description", sqlmodel.sql.sqltypes.AutoString(), nullable=True), |
| 25 | + sa.Column("status", sqlmodel.sql.sqltypes.AutoString(), nullable=False), |
| 26 | + sa.Column("priority", sqlmodel.sql.sqltypes.AutoString(), nullable=True), |
| 27 | + sa.Column("created_at", sa.DateTime(), nullable=False, server_default=sa.text("now()")), |
| 28 | + sa.Column("updated_at", sa.DateTime(), nullable=True), |
| 29 | + sa.Column("owner_id", UUID(), nullable=False), |
| 30 | + sa.PrimaryKeyConstraint("id"), |
| 31 | + sa.ForeignKeyConstraint( |
| 32 | + ["owner_id"], ["user.id"], ondelete="CASCADE" |
| 33 | + ), |
| 34 | + ) |
| 35 | + |
| 36 | + # Create index for ticket lookup by owner |
| 37 | + op.create_index(op.f('ix_ticket_owner_id'), 'ticket', ['owner_id'], unique=False) |
| 38 | + |
| 39 | + # Create comment table |
| 40 | + op.create_table( |
| 41 | + 'comment', |
| 42 | + sa.Column("id", UUID(), nullable=False, server_default=sa.text("gen_random_uuid()")), |
| 43 | + sa.Column("content", sqlmodel.sql.sqltypes.AutoString(), nullable=False), |
| 44 | + sa.Column("created_at", sa.DateTime(), nullable=False, server_default=sa.text("now()")), |
| 45 | + sa.Column("ticket_id", UUID(), nullable=False), |
| 46 | + sa.Column("user_id", UUID(), nullable=False), |
| 47 | + sa.PrimaryKeyConstraint("id"), |
| 48 | + sa.ForeignKeyConstraint( |
| 49 | + ["ticket_id"], ["ticket.id"], ondelete="CASCADE" |
| 50 | + ), |
| 51 | + sa.ForeignKeyConstraint( |
| 52 | + ["user_id"], ["user.id"], ondelete="CASCADE" |
| 53 | + ), |
| 54 | + ) |
| 55 | + |
| 56 | + # Create indexes for faster comment lookups |
| 57 | + op.create_index(op.f('ix_comment_ticket_id'), 'comment', ['ticket_id'], unique=False) |
| 58 | + op.create_index(op.f('ix_comment_user_id'), 'comment', ['user_id'], unique=False) |
| 59 | + |
| 60 | + |
| 61 | +def downgrade(): |
| 62 | + # Drop tables in reverse order (comments first, then tickets) |
| 63 | + op.drop_index(op.f('ix_comment_user_id'), table_name='comment') |
| 64 | + op.drop_index(op.f('ix_comment_ticket_id'), table_name='comment') |
| 65 | + op.drop_table('comment') |
| 66 | + |
| 67 | + op.drop_index(op.f('ix_ticket_owner_id'), table_name='ticket') |
| 68 | + op.drop_table('ticket') |
0 commit comments