008_add_ldap_encryption_key.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. """Add encryption key column to ldap_config
  2. Revision ID: 008_add_ldap_encryption_key
  3. Revises: 007_add_ldap_config
  4. Create Date: 2026-04-12 00:30: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 = "008_add_ldap_encryption_key"
  12. down_revision: str | None = "007_add_ldap_config"
  13. branch_labels: str | Sequence[str] | None = None
  14. depends_on: str | Sequence[str] | None = None
  15. def upgrade() -> None:
  16. conn = op.get_bind()
  17. inspector = inspect(conn)
  18. if inspector.has_table("ldap_config"):
  19. columns = [col["name"] for col in inspector.get_columns("ldap_config")]
  20. if "encryption_key" not in columns:
  21. op.add_column(
  22. "ldap_config",
  23. sa.Column("encryption_key", sa.String(length=512), nullable=True),
  24. )
  25. else:
  26. print("Skipping creation of 'encryption_key' because it already exists")
  27. else:
  28. print("Skipping alter of 'ldap_config' because table does not exist")
  29. def downgrade() -> None:
  30. conn = op.get_bind()
  31. inspector = inspect(conn)
  32. if inspector.has_table("ldap_config"):
  33. columns = [col["name"] for col in inspector.get_columns("ldap_config")]
  34. if "encryption_key" in columns:
  35. op.drop_column("ldap_config", "encryption_key")
  36. else:
  37. print("Skipping drop of 'encryption_key' because it does not exist")
  38. else:
  39. print("Skipping drop of 'encryption_key' because table does not exist")