Nenhuma Descrição

d5a720d1b99b_add_alerts_indexes.py 2.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. """Add alerts indexes
  2. Revision ID: d5a720d1b99b
  3. Revises: 11aa5b725b8e
  4. Create Date: 2024-10-28 12:54:22.782313
  5. """
  6. from alembic import op
  7. from sqlalchemy import text
  8. from app.alembic.alembic_utils import _has_table, index_exists
  9. # revision identifiers, used by Alembic.
  10. revision = 'd5a720d1b99b'
  11. down_revision = '3715d4fac4de'
  12. branch_labels = None
  13. depends_on = None
  14. def upgrade():
  15. # Adding indexes to the Alerts table
  16. if _has_table('alerts'):
  17. if not index_exists('alerts', 'idx_alerts_title'):
  18. op.create_index('idx_alerts_title', 'alerts', ['alert_title'])
  19. if not index_exists('alerts', 'idx_alerts_creation_time'):
  20. op.create_index('idx_alerts_creation_time', 'alerts', ['alert_creation_time'])
  21. if not index_exists('alerts', 'idx_alerts_source_event_time'):
  22. op.create_index('idx_alerts_source_event_time', 'alerts', ['alert_source_event_time'])
  23. if not index_exists('alerts', 'idx_alerts_customer_id'):
  24. op.create_index('idx_alerts_customer_id', 'alerts', ['alert_customer_id'])
  25. if not index_exists('alerts', 'alert_source_ref'):
  26. op.create_index('idx_alert_source_ref', 'alerts', ['alert_source_ref'])
  27. # Adding indexes to the Ioc table
  28. if _has_table('ioc'):
  29. if not index_exists('ioc', 'idx_ioc_value_hash'):
  30. # Create an index on the MD5 hash of ioc_value to handle large values
  31. op.execute(text("CREATE INDEX idx_ioc_value_hash ON ioc (md5(ioc_value::text))"))
  32. if not index_exists('ioc', 'idx_ioc_tags'):
  33. op.create_index('idx_ioc_tags', 'ioc', ['ioc_tags'])
  34. # Adding indexes to the CaseAssets table
  35. if _has_table('case_assets'):
  36. if not index_exists('case_assets', 'idx_case_assets_name'):
  37. op.create_index('idx_case_assets_name', 'case_assets', ['asset_name'])
  38. if not index_exists('case_assets', 'idx_case_assets_case_id'):
  39. op.create_index('idx_case_assets_case_id', 'case_assets', ['case_id'])
  40. if not index_exists('case_assets', 'idx_case_assets_date_added'):
  41. op.create_index('idx_case_assets_date_added', 'case_assets', ['date_added'])
  42. if not index_exists('case_assets', 'idx_case_assets_date_update'):
  43. op.create_index('idx_case_assets_date_update', 'case_assets', ['date_update'])
  44. def downgrade():
  45. # Drop indexes
  46. op.drop_index('ix_alert_similarity_alert_id', table_name='alert_similarity')
  47. op.drop_index('ix_alert_similarity_similar_alert_id', table_name='alert_similarity')
  48. op.drop_index('ix_alert_similarity_matching_asset_id', table_name='alert_similarity')
  49. op.drop_index('ix_alert_similarity_matching_ioc_id', table_name='alert_similarity')
  50. op.drop_index('ix_alert_similarity_similarity_type', table_name='alert_similarity')
  51. # Drop AlertSimilarity table
  52. op.drop_table('alert_similarity')