U
    h                     @   sp  d Z ddlmZ ddlZddlZddlZddlT 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 zddlmZ W n ek
r   dZY nX G dd deZdd Zdd ZG dd deZG dd deZG dd deZG dd dedd ZG d!d" d"eZG d#d$ d$eZd%d& ZdS )'ah  
Lightweight schema migrations.

Example Usage
-------------

Instantiate a migrator:

    # Postgres example:
    my_db = PostgresqlDatabase(...)
    migrator = PostgresqlMigrator(my_db)

    # SQLite example:
    my_db = SqliteDatabase('my_database.db')
    migrator = SqliteMigrator(my_db)

Then you will use the `migrate` function to run various `Operation`s which
are generated by the migrator:

    migrate(
        migrator.add_column('some_table', 'column_name', CharField(default=''))
    )

Migrations are not run inside a transaction, so if you wish the migration to
run in a transaction you will need to wrap the call to `migrate` in a
transaction block, e.g.:

    with my_db.transaction():
        migrate(...)

Supported Operations
--------------------

Add new field(s) to an existing model:

    # Create your field instances. For non-null fields you must specify a
    # default value.
    pubdate_field = DateTimeField(null=True)
    comment_field = TextField(default='')

    # Run the migration, specifying the database table, field name and field.
    migrate(
        migrator.add_column('comment_tbl', 'pub_date', pubdate_field),
        migrator.add_column('comment_tbl', 'comment', comment_field),
    )

Renaming a field:

    # Specify the table, original name of the column, and its new name.
    migrate(
        migrator.rename_column('story', 'pub_date', 'publish_date'),
        migrator.rename_column('story', 'mod_date', 'modified_date'),
    )

Dropping a field:

    migrate(
        migrator.drop_column('story', 'some_old_field'),
    )

Making a field nullable or not nullable:

    # Note that when making a field not null that field must not have any
    # NULL values present.
    migrate(
        # Make `pub_date` allow NULL values.
        migrator.drop_not_null('story', 'pub_date'),

        # Prevent `modified_date` from containing NULL values.
        migrator.add_not_null('story', 'modified_date'),
    )

Renaming a table:

    migrate(
        migrator.rename_table('story', 'stories_tbl'),
    )

Adding an index:

    # Specify the table, column names, and whether the index should be
    # UNIQUE or not.
    migrate(
        # Create an index on the `pub_date` column.
        migrator.add_index('story', ('pub_date',), False),

        # Create a multi-column index on the `pub_date` and `status` fields.
        migrator.add_index('story', ('pub_date', 'status'), False),

        # Create a unique index on the category and title fields.
        migrator.add_index('story', ('category_id', 'title'), True),
    )

Dropping an index:

    # Specify the index name.
    migrate(migrator.drop_index('story', 'story_pub_date_status'))

Adding or dropping table constraints:

.. code-block:: python

    # Add a CHECK() constraint to enforce the price cannot be negative.
    migrate(migrator.add_constraint(
        'products',
        'price_check',
        Check('price >= 0')))

    # Remove the price check constraint.
    migrate(migrator.drop_constraint('products', 'price_check'))

    # Add a UNIQUE constraint on the first and last names.
    migrate(migrator.add_unique('person', 'first_name', 'last_name'))
    )
namedtupleN)*)CommaNodeList)EnclosedNodeListEntity)
Expression)Node)NodeList)OP)	callable_)sort_models)sqlite3)_truncate_constraint_name)CockroachDatabasec                   @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )	Operationz/Encapsulate a single schema altering operation.c                 O   s   || _ || _|| _|| _d S N)migratormethodargskwargs)selfr   r   r   r    r   5/tmp/pip-unpacked-wheel-5j60pwdk/playhouse/migrate.py__init__   s    zOperation.__init__c                 C   s   | j j| d S r   )r   databaseexecute)r   noder   r   r   r      s    zOperation.executec                 C   sT   t |ttfr| | n6t |tr.|  n"t |ttfrP|D ]}| | q@d S r   )	
isinstancer	   Contextr   r   runlisttuple_handle_result)r   resultitemr   r   r   r#      s    

zOperation._handle_resultc                 C   s6   | j  }d|d< t| j| j}| || j| d S )NTwith_context)r   copygetattrr   r   r#   r   )r   r   r   r   r   r   r       s    
zOperation.runN)__name__
__module____qualname____doc__r   r   r#   r    r   r   r   r   r      s
   	r   c                    s   t   fdd}|S )Nc                    s4   | dd}|r  | f||S t|  jf||S )Nr&   F)popr   r)   )r   r   r   r&   fnr   r   inner   s    zoperation.<locals>.inner)	functoolswraps)r/   r0   r   r.   r   	operation   s    r3   c                 C   sT   d | ft| }t|dkrPt|d }d|d d |d d f }|S )N_@   zutf-8z%s_%s8      )joinr"   lenhashlibmd5encode	hexdigest)
table_namecolumns
index_nameZ
index_hashr   r   r   make_index_name   s
    rA   c                   @   s(  e Zd ZdZdZdd Zdd Zedd Ze	dd	 Z
d
d Zdd Ze	dd Ze	dd Ze	dd Ze	dd Zdd Ze	d4ddZe	dd Ze	dd Ze	d5d d!Ze	d"d# Ze	d$d% Ze	d&d' Ze	d(d) Ze	d*d+ Ze	d6d,d-Ze	d.d/ Ze	d7d0d1Ze	d2d3 ZdS )8SchemaMigratorFc                 C   s
   || _ d S r   )r   )r   r   r   r   r   r      s    zSchemaMigrator.__init__c                 C   s
   | j  S r   )r   Zget_sql_contextr   r   r   r   make_context   s    zSchemaMigrator.make_contextc                 C   s\   t rt|t rt|S t|tr(t|S t|tr:t|S t|trLt|S t	d| d S )NzUnsupported database: %s)
r   r   CockroachDBMigratorZPostgresqlDatabasePostgresqlMigratorZMySQLDatabaseMySQLMigratorZSqliteDatabaseSqliteMigrator
ValueError)clsr   r   r   r   from_database   s    


zSchemaMigrator.from_databasec                 C   sP   |j }t|r| }|  dt|dtt|tj|	|ddS )NzUPDATE z SET T)Zflat)
defaultr   rD   literalsqlr   r   r   ZEQZdb_value)r   tablecolumn_namefieldrL   r   r   r   apply_default   s"    zSchemaMigrator.apply_defaultc                 C   s   | dt|S )NALTER TABLE )rM   rN   r   )r   ctxrO   r   r   r   _alter_table   s    zSchemaMigrator._alter_tablec                 C   s   |  ||dt|S )N ALTER COLUMN rU   rM   rN   r   r   rT   rO   columnr   r   r   _alter_column   s     zSchemaMigrator._alter_columnc                 C   sj   |   }|jd }|_|j|kr,| |_|_| ||d|| ||_t|t	rf| 
|| |S )NTz ADD COLUMN )rD   nullrP   namerU   rM   rN   ddlr   ForeignKeyFieldadd_inline_fk_sql)r   rO   rP   rQ   rT   Z
field_nullr   r   r   alter_add_column   s     
 
zSchemaMigrator.alter_add_columnc                 C   s,   |  |  |dt|d|S )N ADD CONSTRAINT  rU   rD   rM   rN   r   r   rO   r\   
constraintr   r   r   add_constraint   s     zSchemaMigrator.add_constraintc                 G   s:   dd | }ttdtdd |D f}| |||S )Nzuniq_%sr4   UNIQUEc                 S   s   g | ]}t |qS r   r   .0rY   r   r   r   
<listcomp>	  s     z-SchemaMigrator.add_unique.<locals>.<listcomp>)r8   r
   SQLr   rf   )r   rO   Zcolumn_namesconstraint_namere   r   r   r   
add_unique  s    zSchemaMigrator.add_uniquec                 C   s    |  |  |dt|S )Nz DROP CONSTRAINT rc   r   rO   r\   r   r   r   drop_constraint  s     zSchemaMigrator.drop_constraintc                 C   sl   | dt|jjj dtt|jjf}|j	d k	rN| d|j	 }|j
d k	rh| d|j
 }|S )N REFERENCES rb    ON DELETE %s ON UPDATE %s)rM   rN   r   	rel_model_metar>   r   	rel_fieldrP   	on_delete	on_updater   rT   rQ   r   r   r   r_     s    

z SchemaMigrator.add_inline_fk_sqlNc           
      C   s   |pd|||f }|   dt|dtt|dtt|fdt|dt|d}	|d k	r|	d| }	|d k	r|	d	| }	|	S )
Nzfk_%s_%s_refs_%srS   ra   z FOREIGN KEY rp   z ()rq   rr   )rD   rM   rN   r   r   r   )
r   rO   rP   relZ
rel_columnrv   rw   rl   re   rT   r   r   r   add_foreign_key_constraint  s@    
	
z)SchemaMigrator.add_foreign_key_constraintc              
   C   s   |j s|jd krtd| t|t}|r8|js8td| |||g}|j sn|| |||| 	||g |r| j
r|| |||jjj|jj|j|j |js|jrt|dd }|| ||f|j| |S )Nz!%s is not null but has no defaultz$Foreign keys must specify a `field`.Z
index_type)r[   rL   rI   r   r^   ru   r`   extendrR   add_not_nullexplicit_create_foreign_keyappendr{   rs   rt   r>   rP   rv   rw   indexuniquer(   	add_index)r   rO   rP   rQ   is_foreign_key
operationsusingr   r   r   
add_column9  s:    



	 zSchemaMigrator.add_columnc                 C   s   t d S r   NotImplementedError)r   rO   rP   r   r   r   drop_foreign_key_constraint`  s    z*SchemaMigrator.drop_foreign_key_constraintTc                 C   sj   |   }| ||dt| |r2|d dd | j|D }||krf| jrf| |||gS |S )N DROP COLUMN  CASCADEc                 S   s   g | ]
}|j qS r   rY   )ri   Zforeign_keyr   r   r   rj   n  s   z.SchemaMigrator.drop_column.<locals>.<listcomp>)	rD   rU   rM   rN   r   r   get_foreign_keysexplicit_delete_foreign_keyr   )r   rO   rP   cascaderT   Z
fk_columnsr   r   r   drop_columnd  s    

zSchemaMigrator.drop_columnc                 C   s0   |  |  |dt|dt|S )N RENAME COLUMN  TO rc   )r   rO   old_namenew_namer   r   r   rename_columnv  s     zSchemaMigrator.rename_columnc                 C   s   |  |  ||dS )Nz SET NOT NULLrZ   rD   rM   r   rO   rY   r   r   r   r}     s      zSchemaMigrator.add_not_nullc                 C   s   |  |  ||dS )Nz DROP NOT NULLr   r   r   r   r   drop_not_null  s      zSchemaMigrator.drop_not_nullc                 C   sf   |d krt dt|r| }t|tr:|dr:t|}| |  |d	t
|d	|S )N `default` must be not None/NULL.ry   'rV   z SET DEFAULT )rI   r   r   strendswithrk   rU   rD   rM   rN   r   )r   rO   rY   rL   r   r   r   add_column_default  s$     z!SchemaMigrator.add_column_defaultc                 C   s&   |  |  |dt|dS )NrV   z DROP DEFAULTrc   r   r   r   r   drop_column_default  s     z"SchemaMigrator.drop_column_defaultc                 C   sV   |   }| |||d||}|d k	rRt|tsBt|}|d|}|S )Nz TYPE z USING )rD   rZ   rM   rN   Zddl_datatyper   r	   rk   r   rO   rY   rQ   castrT   r   r   r   alter_column_type  s      
z SchemaMigrator.alter_column_typec                 C   s    |  |  |dt|S )Nz RENAME TO rc   r   r   r   r   r   r   rename_table  s     zSchemaMigrator.rename_tablec           	         sH   |   }t||}t|  fdd|D }t| |||d}||S )Nc                    s   g | ]}t  j|qS r   )r(   crh   Z	table_objr   r   rj     s     z,SchemaMigrator.add_index.<locals>.<listcomp>)r   r   )rD   rA   ZTableZIndexrN   )	r   rO   r?   r   r   rT   r@   colsr   r   r   r   r     s    
zSchemaMigrator.add_indexc                 C   s   |   dt|S )NDROP INDEX rD   rM   rN   r   r   rO   r@   r   r   r   
drop_index  s
    zSchemaMigrator.drop_index)NNN)T)N)FN)r)   r*   r+   r~   r   r   rD   classmethodrK   r3   rR   rU   rZ   r`   rf   rm   ro   r_   r{   r   r   r   r   r}   r   r   r   r   r   r   r   r   r   r   r   rB      s`   





    
&






rB   c                       s4   e Zd Zdd Zedd Ze fddZ  ZS )rF   c                 C   s&   d}| j || }dd | D S )Nai  
            SELECT pg_attribute.attname
            FROM pg_index, pg_class, pg_attribute
            WHERE
                pg_class.oid = '%s'::regclass AND
                indrelid = pg_class.oid AND
                pg_attribute.attrelid = pg_class.oid AND
                pg_attribute.attnum = any(pg_index.indkey) AND
                indisprimary;
        c                 S   s   g | ]}|d  qS r   r   )ri   rowr   r   r   rj     s     z;PostgresqlMigrator._primary_key_columns.<locals>.<listcomp>)r   execute_sqlfetchall)r   Ztblquerycursorr   r   r   _primary_key_columns  s    
z'PostgresqlMigrator._primary_key_columnsc                 C   s   |   d| S )NzSET search_path TO %s)rD   rM   )r   Zschema_namer   r   r   set_search_path  s    z"PostgresqlMigrator.set_search_pathc           
         s   |  |}tt| }|j||ddg}t|dkrd||d f }d}| j||f}t| rd||d f }	|	|||	 |S )NT)r&      z	%s_%s_seqr   z
                SELECT 1
                FROM information_schema.sequences
                WHERE LOWER(sequence_name) = LOWER(%s)
            )
r   superrF   r   r9   r   r   boolfetchoner   )
r   r   r   Zpk_namesZParentClassr   Zseq_namer   r   Znew_seq_name	__class__r   r   r     s    

 zPostgresqlMigrator.rename_table)r)   r*   r+   r   r3   r   r   __classcell__r   r   r   r   rF     s
   
rF   c                   @   s$   e Zd ZdZdd Zedd ZdS )rE   Tc                 C   s   d S r   r   rx   r   r   r   r_     s    z%CockroachDBMigrator.add_inline_fk_sqlc                 C   s   |   dt|dS )Nr   r   r   r   r   r   r   r     s    zCockroachDBMigrator.drop_indexN)r)   r*   r+   r~   r_   r3   r   r   r   r   r   rE     s   rE   c                   @   s:   e Zd Zedd Zedd Zedd Zd
dd	ZdS )MySQLColumnc                 C   s
   | j dkS )NZPRIpkrC   r   r   r   is_pk
  s    zMySQLColumn.is_pkc                 C   s
   | j dkS )NZUNIr   rC   r   r   r   	is_unique  s    zMySQLColumn.is_uniquec                 C   s
   | j dkS )NZYES)r[   rC   r   r   r   is_null  s    zMySQLColumn.is_nullNc                 C   s   |d kr| j }|d kr| j}t|t| jg}| jrB|td |rV|td n|td | jrx|td | jr|t| j t	|S )Nrg   ZNULLNOT NULLzPRIMARY KEY)
r   r\   r   rk   
definitionr   r   r   extrar
   )r   rP   r   partsr   r   r   rN     s"    zMySQLColumn.sql)NN)r)   r*   r+   propertyr   r   r   rN   r   r   r   r   r     s   


r   Z_Column)r\   r   r[   r   rL   r   c                   @   s   e Zd ZdZdZdd Zedd Zdd Zdd	 Z	ed
d Z
dd Zedd Zedd Zedd ZedddZedd ZdS )rG   Tc                 C   s   |  ||dt|S )N MODIFY rW   rX   r   r   r   rZ   /  s     zMySQLMigrator._alter_columnc                 C   s(   |   dt|dt|S )NzRENAME TABLE r   r   r   r   r   r   r   5  s    zMySQLMigrator.rename_tablec                 C   s@   | j d| }| }|D ]}t| }|j|kr|  S qdS )NzDESCRIBE `%s`;F)r   r   r   r   r\   )r   rO   rP   r   Zrowsr   rY   r   r   r   _get_column_definition>  s    

z$MySQLMigrator._get_column_definitionc                 C   s6   | j d||f}| }|s.td||f |d S )NzSELECT constraint_name FROM information_schema.key_column_usage WHERE table_schema = DATABASE() AND table_name = %s AND column_name = %s AND referenced_table_name IS NOT NULL AND referenced_column_name IS NOT NULL;z=Unable to find foreign key constraint for "%s" on table "%s".r   )r   r   r   AttributeError)r   rO   rP   r   r$   r   r   r   get_foreign_key_constraintG  s    	z(MySQLMigrator.get_foreign_key_constraintc                 C   s,   |  ||}| |  |dt|S )Nz DROP FOREIGN KEY )r   rU   rD   rM   rN   r   )r   rO   rP   Zfk_constraintr   r   r   r   X  s     z)MySQLMigrator.drop_foreign_key_constraintc                 C   s   d S r   r   rx   r   r   r   r_   `  s    zMySQLMigrator.add_inline_fk_sqlc                 C   s   |  ||}| |  |d|jdd}tdd | j|D }||krV|S || }| |||| 	|||j
|jfS )Nr   Fr   c                 s   s   | ]}|j |fV  qd S r   r   ri   Zfkr   r   r   	<genexpr>k  s   z-MySQLMigrator.add_not_null.<locals>.<genexpr>)r   rU   rD   rM   rN   dictr   r   r   r{   
dest_tabledest_column)r   rO   rY   
column_defr}   
fk_objectsfk_metadatar   r   r   r}   c  s0     


zMySQLMigrator.add_not_nullc                 C   s>   |  ||}|jrtd| |  |d|jddS )NzPrimary keys can not be nullr   Tr   )r   r   rI   rU   rD   rM   rN   r   r   r   r   r   z  s     
zMySQLMigrator.drop_not_nullc           	      C   s   t dd | j|D }||k}| ||}| |  |dt|d|j|d}|r|| }| 	|||| 
|||j|jgS |S d S )Nc                 s   s   | ]}|j |fV  qd S r   r   r   r   r   r   r     s   z.MySQLMigrator.rename_column.<locals>.<genexpr>z CHANGE rb   )rP   )r   r   r   r   rU   rD   rM   rN   r   r   r{   r   r   )	r   rO   r   r   r   r   rY   Z
rename_ctxr   r   r   r   r     s:    
 


zMySQLMigrator.rename_columnNc                 C   sF   |d k	rt d|  }| ||dt|d||S )Nz5alter_column_type() does not support cast with MySQL.r   rb   )rI   rD   rU   rM   rN   r   r]   r   r   r   r   r     s     zMySQLMigrator.alter_column_typec                 C   s(   |   dt|dt|S )Nr   z ON r   r   r   r   r   r     s    zMySQLMigrator.drop_index)N)r)   r*   r+   r~   r   rZ   r3   r   r   r   r   r_   r}   r   r   r   r   r   r   r   r   rG   +  s(   
	


	
rG   c                   @   s   e Zd ZdZedZedZedZedej	Z
dd Zdd	 Zed
d Zdd Zed%ddZed&ddZedd Zedd Zedd Zedd Zed'ddZedd  Zed!d" Zed(d#d$ZdS ))rH   z
    SQLite supports a subset of ALTER TABLE queries, view the docs for the
    full details http://sqlite.org/lang_altertable.html
    z(.+?)\((.+)\)z(?:[^,(]|\([^)]*\))+z["`']?([\w]+)z FOREIGN KEY\s+\("?([\w]+)"?\)\s+c                 C   s    | j d| }dd |jD S )Nzselect * from "%s" limit 1c                 S   s   g | ]}|d  qS r   r   )ri   r%   r   r   r   rj     s     z4SqliteMigrator._get_column_names.<locals>.<listcomp>)r   r   descriptionr   rO   resr   r   r   _get_column_names  s    z SqliteMigrator._get_column_namesc                 C   s   | j dd| g}| S )NzBselect name, sql from sqlite_master where type=? and LOWER(name)=?rO   )r   r   lowerr   r   r   r   r   _get_create_table  s
    
z SqliteMigrator._get_create_tablec              	      s  t dd j|D }| |kr6td||f |\}}j|}j| t	dd|}j
| \}}j|}	dd |	D }
g }g }g }d}|
D ]}j| \}||kr|||}|r:|| || j| \}|| q|| | |s|| || qtt||}|| d	d
 } sldd
 }n |kr fdd
}g }|D ]F}j|}|d k	r| d |kr||}|r|| q|d }td| tj}|	d| |}d|}ttdt|gtd| |f g}ttdt|tdd |D tdtdd |D tdt|f}ttdt|g}||| ||g7 }t!dd
 |D ]R}||j"kr|t|j# n. r$|j#| }|d k	r|t| q|S )Nc                 s   s   | ]}|j  V  qd S r   )r\   r   rh   r   r   r   r     s   z0SqliteMigrator._update_column.<locals>.<genexpr>z"Column "%s" does not exist on "%s"z\s+rb   c                 S   s   g | ]}|  qS r   stripri   colr   r   r   rj     s     z1SqliteMigrator._update_column.<locals>.<listcomp>)zforeign zprimary zconstraint zcheck c                 S   s   | S r   r   r   r   r   r   <lambda>      z/SqliteMigrator._update_column.<locals>.<lambda>c                 S   s   d S r   r   r   r   r   r   r     r   c                    s   j d  | S )NzFOREIGN KEY ("%s") )fk_resubr   
new_columnr   r   r   r   
  s   r   Z__tmp__z
("?)%s("?)z\1%s\2, zDROP TABLE IF EXISTSz%s (%s)zINSERT INTOc                 S   s   g | ]}t |qS r   r   r   r   r   r   rj   '  s     ZSELECTc                 S   s   g | ]}t |qS r   r   r   r   r   r   rj   )  s     ZFROMz
DROP TABLEc                 S   s   | j S r   )rN   )idxr   r   r   r   6  r   )%setr   Zget_columnsr   rI   r   Zget_indexesr   rer   	column_researchgroupscolumn_split_refindallcolumn_name_rematchr   
startswithr   zipgetr   compileIr8   r
   rk   r   r   r   r   r   filterr?   rN   
_fix_index)r   rO   column_to_updater/   r?   Zcreate_tableZindexesZ
raw_createZraw_columnsZsplit_columnsZcolumn_defsZnew_column_defsZnew_column_namesZoriginal_column_namesZconstraint_termsr   rP   Znew_column_defZoriginal_to_newZfk_filter_fnZcleaned_columnsr   Z
temp_tableZrgxcreateZqueriesZpopulate_tableZdrop_originalr   rN   r   r   r   _update_column  s    













zSqliteMigrator._update_columnc           
      C   s   | |}t|dkr"|||S |dd\}}t| |dkrXd||||f S |ddd  d}dd	 |D }g }|D ]2}	td
| |	r||	t|d   }	||	 qd|ddd |D f S )N   (r   z%s(%sry   r   ,c                 S   s   g | ]}| d qS )z"`[]' r   )ri   partr   r   r   rj   R  s     z-SqliteMigrator._fix_index.<locals>.<listcomp>z%s(?:[\'"`\]]?\s|$)z%s(%s)r   c                 s   s   | ]}d | V  qdS )z"%s"Nr   )ri   r   r   r   r   r   ]  s     z,SqliteMigrator._fix_index.<locals>.<genexpr>)splitr9   replacersplitr   r   r   r8   )
r   rN   r   r   r   lhsrhsr?   cleanrY   r   r   r   r   @  s    
zSqliteMigrator._fix_indexTFc                 C   sH   t jdkr6|s6|  }| ||dt| |S | ||dd S )N)   #   r   r   c                 S   s   d S r   r   )abr   r   r   r   g  r   z,SqliteMigrator.drop_column.<locals>.<lambda>)r   sqlite_version_inforD   rU   rM   rN   r   r   )r   rO   rP   r   legacyrT   r   r   r   r   _  s    zSqliteMigrator.drop_columnc                    sX   t jdkr>|s>| |  |dt|dt S  fdd}| |||S )N)r     r   r   r   c                    s   | |  S r   r   rP   r   r   r   r   _renamer  s    z-SqliteMigrator.rename_column.<locals>._rename)r   r  rU   rD   rM   rN   r   r   )r   rO   r   r   r	  r  r   r  r   r   i  s     zSqliteMigrator.rename_columnc                 C   s   dd }|  |||S )Nc                 S   s   |d S )Nz	 NOT NULLr   r  r   r   r   _add_not_nullx  s    z2SqliteMigrator.add_not_null.<locals>._add_not_nullr   )r   rO   rY   r  r   r   r   r}   v  s    zSqliteMigrator.add_not_nullc                 C   s   dd }|  |||S )Nc                 S   s   | ddS )Nr    r  r  r   r   r   _drop_not_null~  s    z4SqliteMigrator.drop_not_null.<locals>._drop_not_nullr  )r   rO   rY   r  r   r   r   r   |  s    zSqliteMigrator.drop_not_nullc                    s\    d krt dt r   t trB dsB  sBd    fdd}| |||S )Nr   r   z'%s'c                    s   |d   S )Nz DEFAULT %sr   r  rL   r   r   _add_default  s    z7SqliteMigrator.add_column_default.<locals>._add_default)rI   r   r   r   r   isdigitr   )r   rO   rY   rL   r  r   r  r   r     s    z!SqliteMigrator.add_column_defaultc                 C   s   dd }|  |||S )Nc                 S   s   t dd|t j}| S )NzDEFAULT\s+[\w"\'\(\)]+(\s|$)r  )r   r   r   r   )rP   r   r   r   r   r   _drop_default  s    z9SqliteMigrator.drop_column_default.<locals>._drop_defaultr  )r   rO   rY   r  r   r   r   r     s    z"SqliteMigrator.drop_column_defaultNc                    s6   |d k	rt d|   fdd}| | |S )Nz6alter_column_type() does not support cast with Sqlite.c                    s*    }t | \}}|S r   )r]   rN   r   r   )rP   r   Z	node_listrN   r4   rY   rT   rQ   r   r   _alter_column_type  s    
z<SqliteMigrator.alter_column_type.<locals>._alter_column_type)rI   rD   r   )r   rO   rY   rQ   r   r  r   r  r   r     s
    z SqliteMigrator.alter_column_typec                 C   s   t d S r   r   rd   r   r   r   rf     s    zSqliteMigrator.add_constraintc                 C   s   t d S r   r   rn   r   r   r   ro     s    zSqliteMigrator.drop_constraintc                 C   s   t d S r   r   )r   rO   rP   rQ   rv   rw   r   r   r   r{     s    z)SqliteMigrator.add_foreign_key_constraint)TF)F)N)NN)r)   r*   r+   r,   r   r   r   r   r   r   r   r   r   r3   r   r   r   r   r}   r   r   r   r   rf   ro   r{   r   r   r   r   rH     s@   



s	





   rH   c                  O   s   | D ]}|   qd S r   )r    )r   r   r3   r   r   r   migrate  s    r  ) r,   collectionsr   r1   r:   r   Zpeeweer   r   r   r   r	   r
   r   r   r   r   r   Zplayhouse.cockroachdbr   ImportErrorobjectr   r3   rA   rB   rF   rE   r   rG   rH   r  r   r   r   r   <module>   sD   r

  .#  {