|
| 1 | +"""Add events and event_targets |
| 2 | +
|
| 3 | +Revision ID: 22d74df9897e |
| 4 | +Revises: 5fd659afca82 |
| 5 | +Create Date: 2025-12-04 20:56:08.003504 |
| 6 | +
|
| 7 | +""" |
| 8 | + |
| 9 | +import sqlalchemy as sa |
| 10 | +import sqlalchemy_utils |
| 11 | +from alembic import op |
| 12 | + |
| 13 | +import dstack._internal.server.models |
| 14 | + |
| 15 | +# revision identifiers, used by Alembic. |
| 16 | +revision = "22d74df9897e" |
| 17 | +down_revision = "5fd659afca82" |
| 18 | +branch_labels = None |
| 19 | +depends_on = None |
| 20 | + |
| 21 | + |
| 22 | +def upgrade() -> None: |
| 23 | + # ### commands auto generated by Alembic - please adjust! ### |
| 24 | + op.create_table( |
| 25 | + "events", |
| 26 | + sa.Column("id", sqlalchemy_utils.types.uuid.UUIDType(binary=False), nullable=False), |
| 27 | + sa.Column("message", sa.Text(), nullable=False), |
| 28 | + sa.Column("recorded_at", dstack._internal.server.models.NaiveDateTime(), nullable=False), |
| 29 | + sa.Column( |
| 30 | + "actor_user_id", sqlalchemy_utils.types.uuid.UUIDType(binary=False), nullable=True |
| 31 | + ), |
| 32 | + sa.ForeignKeyConstraint( |
| 33 | + ["actor_user_id"], |
| 34 | + ["users.id"], |
| 35 | + name=op.f("fk_events_actor_user_id_users"), |
| 36 | + ondelete="CASCADE", |
| 37 | + ), |
| 38 | + sa.PrimaryKeyConstraint("id", name=op.f("pk_events")), |
| 39 | + ) |
| 40 | + with op.batch_alter_table("events", schema=None) as batch_op: |
| 41 | + batch_op.create_index( |
| 42 | + batch_op.f("ix_events_actor_user_id"), ["actor_user_id"], unique=False |
| 43 | + ) |
| 44 | + batch_op.create_index(batch_op.f("ix_events_recorded_at"), ["recorded_at"], unique=False) |
| 45 | + |
| 46 | + op.create_table( |
| 47 | + "event_targets", |
| 48 | + sa.Column("id", sqlalchemy_utils.types.uuid.UUIDType(binary=False), nullable=False), |
| 49 | + sa.Column("event_id", sqlalchemy_utils.types.uuid.UUIDType(binary=False), nullable=False), |
| 50 | + sa.Column( |
| 51 | + "entity_project_id", sqlalchemy_utils.types.uuid.UUIDType(binary=False), nullable=True |
| 52 | + ), |
| 53 | + sa.Column("entity_type", sa.String(length=100), nullable=False), |
| 54 | + sa.Column("entity_id", sqlalchemy_utils.types.uuid.UUIDType(binary=False), nullable=False), |
| 55 | + sa.Column("entity_name", sa.String(length=200), nullable=False), |
| 56 | + sa.ForeignKeyConstraint( |
| 57 | + ["entity_project_id"], |
| 58 | + ["projects.id"], |
| 59 | + name=op.f("fk_event_targets_entity_project_id_projects"), |
| 60 | + ondelete="CASCADE", |
| 61 | + ), |
| 62 | + sa.ForeignKeyConstraint( |
| 63 | + ["event_id"], |
| 64 | + ["events.id"], |
| 65 | + name=op.f("fk_event_targets_event_id_events"), |
| 66 | + ondelete="CASCADE", |
| 67 | + ), |
| 68 | + sa.PrimaryKeyConstraint("id", name=op.f("pk_event_targets")), |
| 69 | + ) |
| 70 | + with op.batch_alter_table("event_targets", schema=None) as batch_op: |
| 71 | + batch_op.create_index( |
| 72 | + batch_op.f("ix_event_targets_entity_id"), ["entity_id"], unique=False |
| 73 | + ) |
| 74 | + batch_op.create_index( |
| 75 | + batch_op.f("ix_event_targets_entity_project_id"), ["entity_project_id"], unique=False |
| 76 | + ) |
| 77 | + batch_op.create_index( |
| 78 | + batch_op.f("ix_event_targets_entity_type"), ["entity_type"], unique=False |
| 79 | + ) |
| 80 | + batch_op.create_index(batch_op.f("ix_event_targets_event_id"), ["event_id"], unique=False) |
| 81 | + |
| 82 | + # ### end Alembic commands ### |
| 83 | + |
| 84 | + |
| 85 | +def downgrade() -> None: |
| 86 | + # ### commands auto generated by Alembic - please adjust! ### |
| 87 | + with op.batch_alter_table("event_targets", schema=None) as batch_op: |
| 88 | + batch_op.drop_index(batch_op.f("ix_event_targets_event_id")) |
| 89 | + batch_op.drop_index(batch_op.f("ix_event_targets_entity_type")) |
| 90 | + batch_op.drop_index(batch_op.f("ix_event_targets_entity_project_id")) |
| 91 | + batch_op.drop_index(batch_op.f("ix_event_targets_entity_id")) |
| 92 | + |
| 93 | + op.drop_table("event_targets") |
| 94 | + with op.batch_alter_table("events", schema=None) as batch_op: |
| 95 | + batch_op.drop_index(batch_op.f("ix_events_recorded_at")) |
| 96 | + batch_op.drop_index(batch_op.f("ix_events_actor_user_id")) |
| 97 | + |
| 98 | + op.drop_table("events") |
| 99 | + # ### end Alembic commands ### |
0 commit comments