52 lines
1.5 KiB
Python
52 lines
1.5 KiB
Python
"""proxy leasing columns
|
||
|
||
Revision ID: abe00f7f8f72
|
||
Revises: 175f03f1c9f7
|
||
Create Date: 2025‑05‑05 18:12:44
|
||
"""
|
||
|
||
from alembic import op
|
||
import sqlalchemy as sa
|
||
from sqlalchemy import inspect
|
||
|
||
# revision identifiers, used by Alembic.
|
||
revision = "abe00f7f8f72"
|
||
down_revision = "175f03f1c9f7"
|
||
branch_labels = None
|
||
depends_on = None
|
||
|
||
|
||
def _has_column(bind, table: str, column: str) -> bool:
|
||
insp = inspect(bind)
|
||
return column in {c["name"] for c in insp.get_columns(table)}
|
||
|
||
|
||
def upgrade() -> None:
|
||
bind = op.get_bind()
|
||
|
||
# add only the columns that are missing
|
||
add_in_use = not _has_column(bind, "proxies", "in_use")
|
||
add_last_fail = not _has_column(bind, "proxies", "last_fail")
|
||
|
||
if add_in_use or add_last_fail:
|
||
with op.batch_alter_table("proxies") as batch:
|
||
if add_in_use:
|
||
batch.add_column(sa.Column("in_use", sa.Integer(), server_default="0"))
|
||
if add_last_fail:
|
||
batch.add_column(sa.Column("last_fail", sa.DateTime()))
|
||
|
||
|
||
def downgrade() -> None:
|
||
# downgrade assumes the columns exist, so drop them only if present
|
||
bind = op.get_bind()
|
||
|
||
drop_in_use = _has_column(bind, "proxies", "in_use")
|
||
drop_last_fail = _has_column(bind, "proxies", "last_fail")
|
||
|
||
if drop_in_use or drop_last_fail:
|
||
with op.batch_alter_table("proxies") as batch:
|
||
if drop_last_fail:
|
||
batch.drop_column("last_fail")
|
||
if drop_in_use:
|
||
batch.drop_column("in_use")
|