OwlCyberSecurity - MANAGER
Edit File: migrate.cpython-311.pyc
� a�nh � �� � d Z ddlZddlZddlZddlZddlZddlZddlmZ ddl m Z ddlmZ ddl mZ ddlZddlmZ ddlmZ dd lmZ dd lmZ ddlmZ ddlmZ dd lmZ ddlm Z ddl!m"Z"m#Z#m$Z$ ddl%m&Z& e e'� � Z(dZ)ej* de+de,fd�� � Z-dedee+ fd�Z. e/� � fdee+ de/e/e+e+f df fd�Z0d� Z1ded�d�Z2e'dk r e2� � dS dS ) zbThis module import peewee_migrate and apply migrations, for Imunify-AV it's entrypoint for service� N)�Iterable)� getLogger)�migrator)�SqliteExtDatabase)�app)� configure)�Core)�Model)�Router)�systemd_notifier)�db)� tls_check)�write_pid_file�IM360_RESIDENT_PID_PATH�cleanup_pid_file)�recreate_schema_modelsz/usr/bin/imunify-resident�log_msg�reraisec # �v K � dV � dS # t $ r$ t � | | �� � |r� Y dS w xY w)z� Logs error in case of exception. Depending on `reraise`: - re-raise exception and don't include exception info in the log operation - do not re-raise exception and include exception info in the log operation N)�exc_info)� Exception�logger�error)r r s �L/opt/imunify360/venv/lib/python3.11/site-packages/defence360agent/migrate.py�exc_handlerr ) se � � � �� �������� � � ����W�7�{��3�3�3�� �� � � ����s � �*8�8r �migrations_dirsc �~ � t | |t �� � }t t _ |� � � dS )z4Apply migrations: restructure db, config files, etc.)r r N)r r r �LOGGER�run)r r �routers r �apply_migrationsr! : s; � � � �'��� � �F� �H�O� �J�J�L�L�L�L�L� �attached_dbs.c � � t j � � t j t j � � g }|D ]1\ }}t j d||f� � |� |� � �2 t � d� � t j t j j � � t j d� � 5 t dd�� � 5 t! t | � � ddd� � n# 1 swxY w Y ddd� � n# 1 swxY w Y t � d� � t j d� � 5 t d d �� � 5 t# t |� � t dd �� � 5 t! t | � � ddd� � n# 1 swxY w Y ddd� � n# 1 swxY w Y ddd� � n# 1 swxY w Y t j � � dS # t j � � w xY w)a> Apply migrations and recreate attached databases. The workflow: 1. Apply migrations 2. Regardless whether the migrations were applied - recreate attached databases 3. If the recreation of the attached databases was successful - apply migrations again - this is done to verify that migrations will successfully apply in future for the recreated databases - the recreation + the migrations in this step are within the same transaction, so databases will only be recreated if the migrations can applied after the recreation. z ATTACH ? AS ?zApplying database migrations...� EXCLUSIVEzError applying migrationsF)r Nz Recreating attached databases...z#Error recreating attached databasesTz=Error applying migrations after recreating attached databases)r �reset�db_instance�initr �PATH�execute_sql�appendr �infor �notify� AgentState� MIGRATING�atomicr r! r �close)r r# �attached_schemas�db_path�schema_names r �prepare_databasesr5 H s2 � �"