o
    JhY                     @   s  d dl mZ d dlmZ d dlmZ d dlZd dlZd dlZd dl	Z	d dl
Z
d dlZd dlZd dlZd dlZd dlmZ d dlmZ d dlmZmZmZ d dlmZ d	d
lmZ d dlmZmZmZmZ edZ dZ!edZ"edZ#dd Z$de%fddZ&dd Z'dd Z(dd Z)dS )    )date)dedent)NamedTemporaryFileN)default_migration_table) CONFIG_NEW_MIGRATION_COMMAND_KEY)read_migrationsheads	Migration)utils   )InvalidArgument)pathstatunlinkrenamezyoyo.migrations_tmp_yoyonewz    """
    {message}
    """

    from yoyo import step

    __depends__ = {{{depends}}}

    steps = [
        step("")
    ]
    z0    -- {message}
    -- depends: {depends}

    c                 C   sz   |j d| gdd}|jtd |jddddd	 |jd
ddd |jdddtdd |jdddd |jddd dd d S )NnewzCreate a new migration)parentshelp)funcz	--messagez-mMessage )r   defaultz--sqlzCreate file in SQL format
store_true)r   actionz--migration-tablemigration_tablestorez3Name of table to use for storing migration metadata)destr   r   r   sources*z%Source directory of migration scripts)nargsr   z-dz
--databasezIDatabase, eg 'sqlite:///path/to/sqlite.db' or 'postgresql://user@host/db')r   r   )
add_parserset_defaultsnew_migrationadd_argumentr   )global_parser
subparsers
parser_new r(   [/var/www/html/pro-man-master/venv/lib/python3.10/site-packages/yoyo/scripts/newmigration.pyinstall_argparsers@   s0   
r*   returnc                    sn  z| j d }W n ty   tdw | j}t|}tt|dd d}| jr4t}d	dd |D }nt
}d		d
d |D }|j||d}| jrLdnd}	| jrvt||||	 tj ddd}
|
| W d    n1 spw   Y  nt||||	  d u rdS z"|dt} fddt|D }tdd	| t| W n
 tjy   Y nw td  dS )Nr   z%Please specify a migrations directoryc                 S   s   | j S Nid)mr(   r(   r)   <lambda>d   s    znew_migration.<locals>.<lambda>)keyz  c                 s   s    | ]}|j V  qd S r,   r-   .0r/   r(   r(   r)   	<genexpr>g   s    z new_migration.<locals>.<genexpr>z, c                 s   s    | ]	}d  |jV  qdS )z{!r}N)formatr.   r2   r(   r(   r)   r4   j   s    )messagedependsz.sqlz.pywUTF-8)encodingr   DEFAULTc                    s   g | ]}|  qS r(   )r5   r3   partpr(   r)   
<listcomp>y   s    z!new_migration.<locals>.<listcomp>zRunning command: %s zCreated file)r   
IndexErrorr   r6   r   sortedr   sqlmigration_sql_templatejoinmigration_templater5   
batch_modemake_filenameioopenwritecreate_with_editorgetr   shlexsplitloggerinfo
subprocesscallconfigparserNoOptionErrorprint)argsconfig	directoryr6   
migrationsr7   templatedepends_strmigration_source	extensionfcommandr(   r>   r)   r#   \   sD   
r#   c                 C   s>   t | }ttdd| }tdd|d}|S )Nz[^-a-z0-9]+-z-{2,})r
   	unidecoderesubcompilelowerstrip)r6   sr(   r(   r)   slugify   s   
rj   c                 C   s$  dd | dD }dd |D }t|d }|rdt| }nd}t d}d}td	}z| d
d}	W n t	j
yC   d}	Y nw tt|d|	|D ]2}
t|
t|	t| d d   dd }z||krytt|d d}W qQ ty   Y qQw t|d|	|||||S )Nc                 s   s    | ]}|  V  qd S r,   )rh   r3   liner(   r(   r)   r4          z make_filename.<locals>.<genexpr>
c                 s   s    | ]}|r|V  qd S r,   r(   rk   r(   r(   r)   r4      rm   rb   r   z%Y%m%d01   r;   prefixz{}{}_*r   _r      z{}{}_{}_{}{}{})rP   nextrj   r   todaystrftimer
   get_random_stringrN   rU   rV   globr   rF   r5   basenamelenstrintzfill
ValueError)rY   rZ   r6   r_   linesslugdatestrnumberrandrq   r?   nr(   r(   r)   rI      s6   

,rI   c                    st  t | }tdd|t|dd z }|| W d    n1 s#w   Y   fddt|D }t fdd|D sE| j	 t
 j	j}tjd	| 	 zt| W n tyg   td Y n!w t
 j	j|krtd W zt j	 W d S  ty   Y d S w ztd  j	d }|  t|jdd}W nb ty   d}td tt  t  t dd}	|	dkrY W zt j	 W d S  ty   Y d S w |	dkrY qR|	dkrY n"|	dkrtd td td td td td Y qRY nw qStjdd  t_t| |||}
t j	|
 |
W zt j	 W S  ty'   Y S w zt j	 W w  ty9   Y w w )Nr8   r9   F)moder:   dirrq   suffixdeletec                    s   g | ]}|  jqS r(   )r5   namer<   tmpfiler(   r)   r@      s    z&create_with_editor.<locals>.<listcomp>c                 3   s    | ]} j |v V  qd S r,   )r   r<   r   r(   r)   r4      s    z%create_with_editor.<locals>.<genexpr>r   TzError: could not open editor!zabort: no changes made__doc__r   zError loading migrationzRetry editing?zYnq?qyr   ?z+y: reopen the migration file in your editorz/n: save the migration as-is, without re-editingz$q: quit without saving the migrationz?: show this helpr   )r
   
get_editorr   tempfile_prefixrL   rO   rP   anyappendr   r   st_mtimesysr   insertrS   rT   OSErrorrW   r   r	   loadgetattrmodule	Exception	traceback
format_excpromptrI   r   )rY   rZ   r^   r_   editorr`   mtime	migrationr6   rfilenamer(   r   r)   rM      s   
"$rM   )*datetimer   textwrapr   tempfiler   rU   rx   loggingrJ   rd   rO   rS   r   r   yoyor   yoyo.configr   yoyo.migrationsr   r   r	   r
   mainr   osr   r   r   r   	getLoggerrQ   r   rG   rE   r*   r|   r#   rj   rI   rM   r(   r(   r(   r)   <module>   s>   
	'"