006_add_user_restrictions.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. """Add blocked_users and throttled_users tables
  2. Revision ID: 006_add_user_restrictions
  3. Revises: 005_add_backup_config
  4. Create Date: 2026-04-07 00:00:00.000000
  5. """
  6. from collections.abc import Sequence
  7. import sqlalchemy as sa
  8. from sqlalchemy import inspect
  9. from alembic import op
  10. # revision identifiers, used by Alembic.
  11. revision: str = "006_add_user_restrictions"
  12. down_revision: str | None = "005_add_backup_config"
  13. branch_labels: str | Sequence[str] | None = None
  14. depends_on: str | Sequence[str] | None = None
  15. def upgrade() -> None:
  16. """Create blocked_users and throttled_users tables."""
  17. conn = op.get_bind()
  18. inspector = inspect(conn)
  19. if not inspector.has_table("blocked_users"):
  20. op.create_table(
  21. "blocked_users",
  22. sa.Column("id", sa.Integer(), nullable=False),
  23. sa.Column("username", sa.String(length=255), nullable=False),
  24. sa.Column("ip", sa.String(length=45), nullable=False),
  25. sa.Column("notes", sa.Text(), nullable=True),
  26. sa.Column("active", sa.Integer(), nullable=False, server_default="1"),
  27. sa.Column("created_at", sa.DateTime(), nullable=False),
  28. sa.Column("updated_at", sa.DateTime(), nullable=True),
  29. sa.PrimaryKeyConstraint("id"),
  30. )
  31. op.create_index("ix_blocked_users_username", "blocked_users", ["username"])
  32. op.create_index("ix_blocked_users_ip", "blocked_users", ["ip"])
  33. else:
  34. print("Skipping creation of 'blocked_users' because it already exists")
  35. if not inspector.has_table("throttled_users"):
  36. op.create_table(
  37. "throttled_users",
  38. sa.Column("id", sa.Integer(), nullable=False),
  39. sa.Column("username", sa.String(length=255), nullable=False),
  40. sa.Column("ip", sa.String(length=45), nullable=False),
  41. sa.Column("pool_number", sa.Integer(), nullable=False),
  42. sa.Column("active", sa.Integer(), nullable=False, server_default="1"),
  43. sa.Column("created_at", sa.DateTime(), nullable=False),
  44. sa.Column("updated_at", sa.DateTime(), nullable=True),
  45. sa.PrimaryKeyConstraint("id"),
  46. )
  47. op.create_index("ix_throttled_users_username", "throttled_users", ["username"])
  48. op.create_index("ix_throttled_users_ip", "throttled_users", ["ip"])
  49. else:
  50. print("Skipping creation of 'throttled_users' because it already exists")
  51. def downgrade() -> None:
  52. """Drop blocked_users and throttled_users tables."""
  53. conn = op.get_bind()
  54. inspector = inspect(conn)
  55. if inspector.has_table("throttled_users"):
  56. op.drop_index("ix_throttled_users_ip", table_name="throttled_users")
  57. op.drop_index("ix_throttled_users_username", table_name="throttled_users")
  58. op.drop_table("throttled_users")
  59. else:
  60. print("Skipping drop of 'throttled_users' because it does not exist")
  61. if inspector.has_table("blocked_users"):
  62. op.drop_index("ix_blocked_users_ip", table_name="blocked_users")
  63. op.drop_index("ix_blocked_users_username", table_name="blocked_users")
  64. op.drop_table("blocked_users")
  65. else:
  66. print("Skipping drop of 'blocked_users' because it does not exist")