o
    iM                     @   sP  d Z ddlmZ ddlZddlZddlmZ ddlmZ ddlm	Z	 ddl
mZ ejdeee jj ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZmZmZmZ ddlm Z m!Z! ddl"m#Z#m$Z$ ddl%m&Z& ej'Z'ej(r~e')dej( e'j*duree'j* ej+Z,dddZ-dddZ.dddZ/e0 re.  dS e/  dS )u;   
Alembic 环境配置
用于数据库迁移的环境设置
    )
fileConfigN)Path)engine_from_config)pool)context)settings)Base)User)Tenant)KnowledgeBaseKnowledgeDocumentDocumentVectorKnowledgeCategory)AgentChatMessage)ModelProviderModel)AuditLogsqlalchemy.urlreturnc           
      C   s   ddl m} ddlm} | |d}dd |D }| }|s"d S |D ]@}z| jd |id}tjj	||dd	 | |d
 W q$ t
yd } zdd l}	|	td| d|  W Y d }~q$d }~ww d S )Nr   )text)TenantSchemaManagerzUSELECT schema_name FROM information_schema.schemata WHERE schema_name LIKE 'tenant_%'c                 S   s   g | ]}|d  qS )r    ).0rowr   r   3/lsinfo/ai/hellotax_ai/base_platform/alembic/env.py
<listcomp>4   s    z(_sync_tenant_schemas.<locals>.<listcomp>)schema_translate_mapT)bindtables
checkfirstCOMMITzFailed to sync schema z: )
sqlalchemyr   app.db.tenant_schemar   executeget_tenant_table_definitionsexecution_optionsr   metadata
create_all	Exceptionlogging	getLogger__name__error)

connectionr   r   resulttenant_schemastenant_tablesschema_nametenant_binder*   r   r   r   _sync_tenant_schemas-   s(   (r5   c                  C   sZ   t d} tj| tdddiddd t  t  W d   dS 1 s&w   Y  dS )u}   
    离线模式运行迁移

    在离线模式下，不需要实际的数据库连接，
    只生成 SQL 脚本。
    r   T
paramstylenamed)urltarget_metadataliteral_bindsdialect_optscompare_typecompare_server_defaultN)configget_main_optionr   	configurer9   begin_transactionrun_migrations)r8   r   r   r   run_migrations_offlineD   s   

	
"rC   c               	   C   s   t ttji dtjd} |  .}tj|t	ddd t
  t  W d   n1 s.w   Y  t| W d   dS 1 sBw   Y  dS )um   
    在线模式运行迁移

    在在线模式下，创建实际的数据库连接并运行迁移。
    zsqlalchemy.)prefix	poolclassT)r.   r9   r<   r=   N)r   r>   get_sectionconfig_ini_sectionr   NullPoolconnectr   r@   r9   rA   rB   r5   )connectabler.   r   r   r   run_migrations_onlineY   s"   



"rK   )r   N)1__doc__logging.configr   ossyspathlibr   r"   r   r   alembicr   pathinsertstr__file__resolveparent
app.configr   app.db.baser   app.models.userr	   app.models.tenantr
   app.models.knowledge_baser   r   r   r   app.models.agentr   r   app.models.providerr   r   app.models.audit_logr   r>   DATABASE_URLset_main_optionconfig_file_namer'   r9   r5   rC   rK   is_offline_moder   r   r   r   <module>   s:    






