| 1 | from logging.config import fileConfig |
| 2 | |
| 3 | from sqlalchemy import engine_from_config |
| 4 | from sqlalchemy import pool |
| 5 | from sqlmodel import SQLModel |
| 6 | |
| 7 | from alembic import context |
| 8 | |
| 9 | # from app.db.all_models import * |
| 10 | from app.auth.models.users import User |
| 11 | from app.connectors.models import Connectors |
| 12 | from app.connectors.wazuh_indexer.models.sigma import SigmaQuery |
| 13 | from app.connectors.wazuh_indexer.models.snapshot_and_restore import SnapshotSchedule |
| 14 | |
| 15 | # from app.integrations.sap_siem.models.sap_siem import SapSiemMultipleLogins |
| 16 | from app.customer_provisioning.models.default_settings import ( |
| 17 | CustomerProvisioningDefaultSettings, |
| 18 | ) |
| 19 | |
| 20 | # from app.connectors.sublime.models.alerts import SublimeAlerts |
| 21 | # from app.connectors.wazuh_manager.models.rules import DisabledRule |
| 22 | from app.db.universal_models import Agents |
| 23 | from app.db.universal_models import Customers |
| 24 | from app.db.universal_models import CustomersMeta |
| 25 | from app.db.universal_models import LogEntry |
| 26 | from app.incidents.models import Alert |
| 27 | from app.incidents.models import AlertContext |
| 28 | from app.incidents.models import AlertTag |
| 29 | from app.incidents.models import AlertTitleFieldName |
| 30 | from app.incidents.models import AlertToIoC |
| 31 | from app.incidents.models import AlertToTag |
| 32 | from app.incidents.models import Asset |
| 33 | from app.incidents.models import AssetFieldName |
| 34 | from app.incidents.models import Case |
| 35 | from app.incidents.models import CaseAlertLink |
| 36 | from app.incidents.models import CaseDataStore |
| 37 | from app.incidents.models import CaseReportTemplateDataStore |
| 38 | from app.incidents.models import Comment |
| 39 | from app.incidents.models import CustomerCodeFieldName |
| 40 | from app.incidents.models import FieldName |
| 41 | from app.incidents.models import IoC |
| 42 | from app.incidents.models import IoCFieldName |
| 43 | from app.incidents.models import Notification |
| 44 | from app.integrations.alert_creation_settings.models.alert_creation_settings import ( |
| 45 | AlertCreationSettings, |
| 46 | ) |
| 47 | from app.integrations.github_audit.model import GitHubAuditBaseline |
| 48 | from app.integrations.github_audit.model import GitHubAuditCheckExclusion |
| 49 | from app.integrations.github_audit.model import GitHubAuditConfig |
| 50 | from app.integrations.github_audit.model import GitHubAuditReport |
| 51 | from app.integrations.models.customer_integration_settings import CustomerIntegrations |
| 52 | from app.integrations.monitoring_alert.models.monitoring_alert import MonitoringAlerts |
| 53 | from app.network_connectors.models.network_connectors import AvailableNetworkConnectors |
| 54 | from app.network_connectors.models.network_connectors import ( |
| 55 | AvailableNetworkConnectorsKeys, |
| 56 | ) |
| 57 | from app.network_connectors.models.network_connectors import CustomerNetworkConnectors |
| 58 | from app.network_connectors.models.network_connectors import ( |
| 59 | CustomerNetworkConnectorsMeta, |
| 60 | ) |
| 61 | from app.network_connectors.models.network_connectors import NetworkConnectorsConfig |
| 62 | from app.network_connectors.models.network_connectors import NetworkConnectorsService |
| 63 | from app.network_connectors.models.network_connectors import ( |
| 64 | NetworkConnectorsSubscription, |
| 65 | ) |
| 66 | from app.schedulers.models.scheduler import JobMetadata |
| 67 | |
| 68 | # this is the Alembic Config object, which provides |
| 69 | # access to the values within the .ini file in use. |
| 70 | config = context.config |
| 71 | |
| 72 | # Interpret the config file for Python logging. |
| 73 | # This line sets up loggers basically. |
| 74 | if config.config_file_name is not None: |
| 75 | fileConfig(config.config_file_name) |
| 76 | |
| 77 | # add your model's MetaData object here |
| 78 | # for 'autogenerate' support |
| 79 | # from myapp import mymodel |
| 80 | # target_metadata = mymodel.Base.metadata |
| 81 | target_metadata = SQLModel.metadata |
| 82 | |
| 83 | # other values from the config, defined by the needs of env.py, |
| 84 | # can be acquired: |
| 85 | # my_important_option = config.get_main_option("my_important_option") |
| 86 | # ... etc. |
| 87 | |
| 88 | |
| 89 | def run_migrations_offline() -> None: |
| 90 | """Run migrations in 'offline' mode. |
| 91 | |
| 92 | This configures the context with just a URL |
| 93 | and not an Engine, though an Engine is acceptable |
| 94 | here as well. By skipping the Engine creation |
| 95 | we don't even need a DBAPI to be available. |
| 96 | |
| 97 | Calls to context.execute() here emit the given string to the |
| 98 | script output. |
| 99 | |
| 100 | """ |
| 101 | url = config.get_main_option("sqlalchemy.url") |
| 102 | context.configure( |
| 103 | url=url, |
| 104 | target_metadata=target_metadata, |
| 105 | literal_binds=True, |
| 106 | dialect_opts={"paramstyle": "named"}, |
| 107 | ) |
| 108 | |
| 109 | with context.begin_transaction(): |
| 110 | context.run_migrations() |
| 111 | |
| 112 | |
| 113 | def run_migrations_online() -> None: |
| 114 | """Run migrations in 'online' mode. |
| 115 | |
| 116 | In this scenario we need to create an Engine |
| 117 | and associate a connection with the context. |
| 118 | |
| 119 | """ |
| 120 | connectable = engine_from_config( |
| 121 | config.get_section(config.config_ini_section, {}), |
| 122 | prefix="sqlalchemy.", |
| 123 | poolclass=pool.NullPool, |
| 124 | ) |
| 125 | |
| 126 | with connectable.connect() as connection: |
| 127 | context.configure(connection=connection, target_metadata=target_metadata) |
| 128 | |
| 129 | with context.begin_transaction(): |
| 130 | context.run_migrations() |
| 131 | |
| 132 | |
| 133 | if context.is_offline_mode(): |
| 134 | run_migrations_offline() |
| 135 | else: |
| 136 | run_migrations_online() |