o
    If                     @   s8  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	 dZ
e	jZeej edZddd	Zdd
dZede  g ZejddurTeejd  Zneejd jddZerce ZeD ]Ze	jedeed qeejd jZdd Zdd Zdd Zdd Z e	! re  dS e " #e   dS )    N)
fileConfig)MetaData)current_app)contextFzalembic.envc              	   C   sB   zt jd jj| dW S  ttfy    t jd jj|  Y S w )Nmigrate)bind)r   
extensionsdb
get_engine	TypeErrorAttributeErrorenginesgetbind_key r   e/home/ubuntu/webapp/venv/lib/python3.10/site-packages/flask_migrate/templates/aioflask-multidb/env.pyr
      s
   r
   c                 C   sF   zt | jjddddW S  ty"   tt | jdd Y S w )NF)hide_password%z%%)r
   urlrender_as_stringreplacer   strr   r   r   r   get_engine_url   s   
r   sqlalchemy.urlSQLALCHEMY_BINDSr   
bind_namesr   c                 C   sV   | dkrd} t tdrtj|  S t }tjj D ]}|jd| kr(|	| q|S )zReturn the metadata for a bind. N	metadatasr   )
hasattr	target_dbr   r   metadatatablesvaluesinfor   
tometadata)r   mtr   r   r   get_metadata@   s   


r(   c               
   C   s   ddt jdii} tD ]}i  | |< }t j|d|d< q|  D ]T\}}td|p,d  d| }td|  t|d/}t j	|d |t
|d	d
 t   t j|d W d   n1 sbw   Y  W d   n1 sqw   Y  q"dS )aF  Run migrations in 'offline' mode.

    This configures the context with just a URL
    and not an Engine, though an Engine is acceptable
    here as well.  By skipping the Engine creation
    we don't even need a DBAPI to be available.

    Calls to context.execute() here emit the given string to the
    script output.

    r   r   r   Migrating database %s	<default>z%s.sqlzWriting output to %swT)r   output_buffertarget_metadataliteral_bindsengine_nameN)r   configget_main_optionr   get_section_optionitemsloggerr$   open	configurer(   begin_transactionrun_migrations)r   namerecfile_bufferr   r   r   run_migrations_offlineO   s2   
r>   c              	   C   sf  dd }t jd j}|dd u r||d< | D ]\}}|d   |d< }tr0| |d< q| |d< qzmzK| D ](\}}t	
d|pGd	  tjd|d d
| d| t|d| tj|d q=tru| D ]}|d   ql| D ]}|d   qyW n   | D ]}|d   q W | D ]}|d   qd S | D ]}|d   qw )Nc                 S   sr   t tjddr3|d }t|jttd kr5d}|jD ]}| s#d}q|r7g |d d < td d S d S d S d S )NautogenerateFr      TzNo changes in schema detected.)	getattrr1   cmd_optslenupgrade_ops_listr   is_emptyr5   r$   )r   revision
directivesscriptemptyupgrade_opsr   r   r   process_revision_directivesz   s   
z6do_run_migrations.<locals>.process_revision_directivesr   rK   
connectionsync_connectiontransactionr)   r*   z%s_upgradesz%s_downgrades)rL   upgrade_tokendowngrade_tokenr-   r/   r   )r   r   configure_argsr   r4   _sync_connectionUSE_TWOPHASEbegin_twophasebeginr5   r$   r   r7   r(   r9   r#   preparecommitrollbackclose)_r   rK   	conf_argsr:   r;   connr   r   r   do_run_migrationsv   sJ   r]   c                     s   ddt  ii} tD ]}i  | |< }t |d|d< q
|  D ]\}}|d }|  I dH |d< q| d d t| I dH  |  D ]}|d  I dH  qCdS )zRun migrations in 'online' mode.

    In this scenario we need to create an Engine
    and associate a connection with the context.

    r   enginer   NrL   )	r
   r   r4   connectstartrun_syncr]   r#   rY   )r   r:   r;   r^   r   r   r   run_migrations_online   s   
rb   )N)$asynciologginglogging.configr   
sqlalchemyr   flaskr   alembicr   rS   r1   config_file_name	getLoggerr5   r
   r   set_main_optionr   r   listkeysrA   r   r	   get_bind_namesr   set_section_optionr    r(   r>   r]   rb   is_offline_modeget_event_looprun_until_completer   r   r   r   <module>   sB    



		'6
