|
5 | 5 | Create Date: 2025-10-31 20:35:10.057534 |
6 | 6 |
|
7 | 7 | """ |
8 | | -from typing import Sequence, Union |
| 8 | +from collections.abc import Sequence |
9 | 9 |
|
10 | | -from alembic import op |
11 | 10 | import sqlalchemy as sa |
| 11 | +from alembic import op |
12 | 12 | from sqlalchemy.dialects import postgresql |
13 | 13 |
|
14 | 14 | # revision identifiers, used by Alembic. |
15 | | -revision: str = '53914fdc7590' |
16 | | -down_revision: Union[str, Sequence[str], None] = '37309a4b1b2f' |
17 | | -branch_labels: Union[str, Sequence[str], None] = None |
18 | | -depends_on: Union[str, Sequence[str], None] = None |
| 15 | +revision: str = "53914fdc7590" |
| 16 | +down_revision: str | Sequence[str] | None = "37309a4b1b2f" |
| 17 | +branch_labels: str | Sequence[str] | None = None |
| 18 | +depends_on: str | Sequence[str] | None = None |
| 19 | + |
| 20 | + |
| 21 | +optimization_status_enum = postgresql.ENUM( |
| 22 | + "pending", |
| 23 | + "running", |
| 24 | + "completed", |
| 25 | + "failed", |
| 26 | + name="optimization_status", |
| 27 | + create_type=False, |
| 28 | +) |
19 | 29 |
|
20 | 30 |
|
21 | 31 | def upgrade() -> None: |
22 | 32 | """Upgrade schema.""" |
23 | 33 | # ### commands auto generated by Alembic - please adjust! ### |
24 | | - op.create_table('optimization', |
25 | | - sa.Column('id', sa.Integer(), autoincrement=True, nullable=False), |
26 | | - sa.Column('task_id', sa.Integer(), nullable=False), |
27 | | - sa.Column('status', sa.Enum('PENDING', 'RUNNING', 'COMPLETED', 'FAILED', name='optimization_status'), nullable=False), |
28 | | - sa.Column('started_at', sa.DateTime(timezone=True), nullable=True), |
29 | | - sa.Column('completed_at', sa.DateTime(timezone=True), nullable=True), |
30 | | - sa.Column('error', sa.Text(), nullable=True), |
31 | | - sa.Column('max_iterations', sa.Integer(), nullable=False), |
32 | | - sa.Column('changeable_fields', sa.JSON().with_variant(postgresql.JSONB(astext_type=sa.Text()), 'postgresql'), nullable=False), |
33 | | - sa.Column('max_consecutive_no_improvements', sa.Integer(), nullable=False), |
34 | | - sa.Column('iterations_run', sa.Integer(), nullable=False), |
35 | | - sa.Column('current_iteration', sa.Integer(), nullable=True), |
36 | | - sa.Column('best_implementation_id', sa.Integer(), nullable=True), |
37 | | - sa.Column('best_score', sa.Float(), nullable=True), |
38 | | - sa.Column('iterations', sa.JSON().with_variant(postgresql.JSONB(astext_type=sa.Text()), 'postgresql'), nullable=False), |
39 | | - sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False), |
40 | | - sa.Column('updated_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False), |
41 | | - sa.ForeignKeyConstraint(['best_implementation_id'], ['implementation.id'], name=op.f('fk_optimization_best_implementation_id_implementation'), ondelete='SET NULL'), |
42 | | - sa.ForeignKeyConstraint(['task_id'], ['task.id'], name=op.f('fk_optimization_task_id_task'), ondelete='CASCADE'), |
43 | | - sa.PrimaryKeyConstraint('id', name=op.f('pk_optimization')) |
| 34 | + bind = op.get_bind() |
| 35 | + optimization_status_enum.create(bind, checkfirst=True) |
| 36 | + |
| 37 | + op.create_table( |
| 38 | + "optimization", |
| 39 | + sa.Column("id", sa.Integer(), autoincrement=True, nullable=False), |
| 40 | + sa.Column("task_id", sa.Integer(), nullable=False), |
| 41 | + sa.Column("status", optimization_status_enum, nullable=False), |
| 42 | + sa.Column("started_at", sa.DateTime(timezone=True), nullable=True), |
| 43 | + sa.Column("completed_at", sa.DateTime(timezone=True), nullable=True), |
| 44 | + sa.Column("error", sa.Text(), nullable=True), |
| 45 | + sa.Column("max_iterations", sa.Integer(), nullable=False), |
| 46 | + sa.Column( |
| 47 | + "changeable_fields", |
| 48 | + sa.JSON().with_variant(postgresql.JSONB(astext_type=sa.Text()), "postgresql"), |
| 49 | + nullable=False, |
| 50 | + ), |
| 51 | + sa.Column("max_consecutive_no_improvements", sa.Integer(), nullable=False), |
| 52 | + sa.Column("iterations_run", sa.Integer(), nullable=False), |
| 53 | + sa.Column("current_iteration", sa.Integer(), nullable=True), |
| 54 | + sa.Column("best_implementation_id", sa.Integer(), nullable=True), |
| 55 | + sa.Column("best_score", sa.Float(), nullable=True), |
| 56 | + sa.Column( |
| 57 | + "iterations", |
| 58 | + sa.JSON().with_variant(postgresql.JSONB(astext_type=sa.Text()), "postgresql"), |
| 59 | + nullable=False, |
| 60 | + ), |
| 61 | + sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.text("now()"), nullable=False), |
| 62 | + sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.text("now()"), nullable=False), |
| 63 | + sa.ForeignKeyConstraint( |
| 64 | + ["best_implementation_id"], |
| 65 | + ["implementation.id"], |
| 66 | + name=op.f("fk_optimization_best_implementation_id_implementation"), |
| 67 | + ondelete="SET NULL", |
| 68 | + ), |
| 69 | + sa.ForeignKeyConstraint( |
| 70 | + ["task_id"], |
| 71 | + ["task.id"], |
| 72 | + name=op.f("fk_optimization_task_id_task"), |
| 73 | + ondelete="CASCADE", |
| 74 | + ), |
| 75 | + sa.PrimaryKeyConstraint("id", name=op.f("pk_optimization")), |
44 | 76 | ) |
45 | | - op.create_index('ix_optimization_started_at', 'optimization', ['started_at'], unique=False) |
46 | | - op.create_index('ix_optimization_status', 'optimization', ['status'], unique=False) |
47 | | - op.create_index('ix_optimization_task_id', 'optimization', ['task_id'], unique=False) |
| 77 | + op.create_index("ix_optimization_started_at", "optimization", ["started_at"], unique=False) |
| 78 | + op.create_index("ix_optimization_status", "optimization", ["status"], unique=False) |
| 79 | + op.create_index("ix_optimization_task_id", "optimization", ["task_id"], unique=False) |
48 | 80 | # ### end Alembic commands ### |
49 | 81 |
|
50 | 82 |
|
51 | 83 | def downgrade() -> None: |
52 | 84 | """Downgrade schema.""" |
53 | 85 | # ### commands auto generated by Alembic - please adjust! ### |
54 | | - op.drop_index('ix_optimization_task_id', table_name='optimization') |
55 | | - op.drop_index('ix_optimization_status', table_name='optimization') |
56 | | - op.drop_index('ix_optimization_started_at', table_name='optimization') |
57 | | - op.drop_table('optimization') |
| 86 | + op.drop_index("ix_optimization_task_id", table_name="optimization") |
| 87 | + op.drop_index("ix_optimization_status", table_name="optimization") |
| 88 | + op.drop_index("ix_optimization_started_at", table_name="optimization") |
| 89 | + op.drop_table("optimization") |
| 90 | + bind = op.get_bind() |
| 91 | + optimization_status_enum.drop(bind, checkfirst=True) |
58 | 92 | # ### end Alembic commands ### |
0 commit comments