o
    h                     @  s`  d dl m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 dlmZ 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 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 erd dlmZ 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! d dlm"Z" d dl#m$Z$ ddl%m&Z& G dd deZ'eedd(d&d'Z(dS ))    )annotationsN)Any)Dict)Optional)TYPE_CHECKING)Union)cast)JSON)schema)sql)compiles   )alter_table)format_table_name)RenameTable)DefaultImpl   )util)	Inspector)DDLCompiler)Cast)ClauseElement)Column)
Constraint)Table)
TypeEngine)BatchOperationsImplc                      sz   e Zd ZdZdZ	 d1ddZd2ddZd2ddZd3ddZd4ddZ	d5d!d"Z
	d6d7 fd&d'Zd8d-d.Zd/d0 Z  ZS )9
SQLiteImplsqliteFbatch_opr   returnboolc                 C  s~   |j D ]9}|d dkr3|d d }t|jtjr#t|jjtjr# dS t|jtj	j
r2|jjr2 dS q|d dvr< dS qdS )zReturn True if the given :class:`.BatchOperationsImpl`
        would need the table to be recreated and copied in order to
        proceed.

        Normally, only returns True on SQLite when operations other
        than add_column are present.

        r   
add_columnr   T)create_index
drop_indexF)batch
isinstanceserver_defaultr
   DefaultClauseargr   r   r   sqla_compatComputed	persisted)selfr   opcol r0   U/var/www/html/aiguide_backend/venv/lib/python3.10/site-packages/alembic/ddl/sqlite.pyrequires_recreate_in_batch+   s&   
z%SQLiteImpl.requires_recreate_in_batchconstr   c                 C  s.   |j d u r	td| | rtd d S d S )NNo support for ALTER of constraints in SQLite dialect. Please refer to the batch mode feature which allows for SQLite migrations using a copy-and-move strategy.zSkipping unsupported ALTER for creation of implicit constraint. Please refer to the batch mode feature which allows for SQLite migrations using a copy-and-move strategy.)_create_ruleNotImplementedErrorr   warnr-   r3   r0   r0   r1   add_constraintG   s   

zSQLiteImpl.add_constraintc                 C  s   |j d u r	tdd S )Nr4   )r5   r6   r8   r0   r0   r1   drop_constraintX   s
   
zSQLiteImpl.drop_constraintinspector_columnColumn[Any]metadata_columnrendered_metadata_defaultOptional[str]rendered_inspector_defaultc                 C  sP   |d urt dd|}t dd|}|d ur$t dd|}t dd|}||kS )Nz
^\((.+)\)$z\1z^\"?'(.+)'\"?$)resub)r-   r;   r=   r>   r@   r0   r0   r1   compare_server_default`   s   z!SQLiteImpl.compare_server_defaultexprc                 C  s<   |sdS t d|rdS t d|rdS t d|rdS dS )a  Determine if a server default is a SQL expression or a constant.

        There are too many assertions that expect server defaults to round-trip
        identically without parenthesis added so we will add parens only in
        very specific cases.

        Fz	^[0-9\.]$z^'.+'$z^\(.+\)$T)rA   match)r-   rD   r0   r0   r1   -_guess_if_default_is_unparenthesized_sql_expr{   s   
z8SQLiteImpl._guess_if_default_is_unparenthesized_sql_expr	inspectorr   tabler   column_infoDict[str, Any]Nonec                 C  s,   |  |dd rd|d f |d< d S d S )Ndefault(%s))rF   get)r-   rG   rH   rI   r0   r0   r1   autogen_column_reflect   s
   
z!SQLiteImpl.autogen_column_reflectr   is_server_defaultstrc                   s4   t  j|fd|i|}|r| |rd|f }|S )NrP   rM   )superrender_ddl_sql_exprrF   )r-   rD   rP   kwstr_expr	__class__r0   r1   rS      s   
zSQLiteImpl.render_ddl_sql_exprexistingexisting_transfer"Dict[str, Union[TypeEngine, Cast]]new_typer   c                 C  s6   |j j|jurt|tst|d ||d< d S d S d S )NrD   )type_type_affinityr&   r	   r   )r-   rX   rY   r[   r0   r0   r1   cast_for_batch_migrate   s   z!SQLiteImpl.cast_for_batch_migratec                 C  s   |  || d S )N)_skip_functional_indexes)r-   conn_unique_constraintsconn_indexesmetadata_unique_constraintsmetadata_indexesr0   r0   r1   correct_for_autogen_constraints   s   z*SQLiteImpl.correct_for_autogen_constraints)r   r   r    r!   )r3   r   )
r;   r<   r=   r<   r>   r?   r@   r?   r    r!   )rD   r?   r    r!   )rG   r   rH   r   rI   rJ   r    rK   )F)rD   r   rP   r!   r    rQ   )rX   r<   rY   rZ   r[   r   r    rK   )__name__
__module____qualname____dialect__transactional_ddlr2   r9   r:   rC   rF   rO   rS   r^   rd   __classcell__r0   r0   rV   r1   r   #   s    






r   r   elementr   compilerr   r    rQ   c                 K  s"   dt || j| jt|| jd f S )Nz%s RENAME TO %s)r   
table_namer
   r   new_table_name)rk   rl   rT   r0   r0   r1   visit_rename_table   s   ro   )rk   r   rl   r   r    rQ   ))
__future__r   rA   typingr   r   r   r   r   
sqlalchemyr   r	   r
   r   sqlalchemy.ext.compilerr   baser   r   r   implr    r   sqlalchemy.engine.reflectionr   sqlalchemy.sql.compilerr   sqlalchemy.sql.elementsr   r   sqlalchemy.sql.schemar   r   r   sqlalchemy.sql.type_apir   operations.batchr   r   ro   r0   r0   r0   r1   <module>   s>     $