U
    [+d .                     @   sf  d dl mZmZmZ d dlmZmZ d dlmZm	Z	m
Z
 d dlmZ d dlmZ G dd deZG dd	 d	eZG d
d deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd dZG dd dee	ZG dd dee
ZG dd deZG d d! d!eZG d"d# d#eZG d$d% d%eZG d&d' d'eZd(S ))    )get_citext_oidsget_hstore_oidsregister_type_handlers)NotSupportedErrorrouter)AddConstraintAddIndexRemoveIndex)	Operation)CheckConstraintc                   @   sL   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	e
dd ZdS )CreateExtensionTc                 C   s
   || _ d S Nname)selfr    r   F/tmp/pip-unpacked-wheel-n7e__lmp/django/contrib/postgres/operations.py__init__   s    zCreateExtension.__init__c                 C   s   d S r   r   r   	app_labelstater   r   r   state_forwards   s    zCreateExtension.state_forwardsc                 C   sb   |j jdkst|j j|s d S | || jsD|d|| j  t	
  t
  t|j  d S )N
postgresqlz!CREATE EXTENSION IF NOT EXISTS %s)
connectionvendorr   allow_migratealiasextension_existsr   execute
quote_namer   cache_clearr   r   r   r   schema_editor
from_stateto_stater   r   r   database_forwards   s     
z!CreateExtension.database_forwardsc                 C   sL   t |jj|sd S | || jr8|d|| j  t	  t
	  d S )NzDROP EXTENSION IF EXISTS %s)r   r   r   r   r   r   r   r   r   r    r   r!   r   r   r   database_backwards'   s    z"CreateExtension.database_backwardsc              
   C   s<   |j  (}|d|g t| W  5 Q R  S Q R X d S )Nz-SELECT 1 FROM pg_extension WHERE extname = %s)r   cursorr   boolZfetchone)r   r"   	extensionr'   r   r   r   r   2   s    z CreateExtension.extension_existsc                 C   s
   d| j  S )NzCreates extension %sr   r   r   r   r   describe:   s    zCreateExtension.describec                 C   s
   d| j  S )Nzcreate_extension_%sr   r*   r   r   r   migration_name_fragment=   s    z'CreateExtension.migration_name_fragmentN)__name__
__module____qualname__Z
reversibler   r   r%   r&   r   r+   propertyr,   r   r   r   r   r      s   r   c                   @   s   e Zd Zdd ZdS )BloomExtensionc                 C   s
   d| _ d S )NZbloomr   r*   r   r   r   r   C   s    zBloomExtension.__init__Nr-   r.   r/   r   r   r   r   r   r1   B   s   r1   c                   @   s   e Zd Zdd ZdS )BtreeGinExtensionc                 C   s
   d| _ d S )NZ	btree_ginr   r*   r   r   r   r   H   s    zBtreeGinExtension.__init__Nr2   r   r   r   r   r3   G   s   r3   c                   @   s   e Zd Zdd ZdS )BtreeGistExtensionc                 C   s
   d| _ d S )NZ
btree_gistr   r*   r   r   r   r   M   s    zBtreeGistExtension.__init__Nr2   r   r   r   r   r4   L   s   r4   c                   @   s   e Zd Zdd ZdS )CITextExtensionc                 C   s
   d| _ d S )NZcitextr   r*   r   r   r   r   R   s    zCITextExtension.__init__Nr2   r   r   r   r   r5   Q   s   r5   c                   @   s   e Zd Zdd ZdS )CryptoExtensionc                 C   s
   d| _ d S )NZpgcryptor   r*   r   r   r   r   W   s    zCryptoExtension.__init__Nr2   r   r   r   r   r6   V   s   r6   c                   @   s   e Zd Zdd ZdS )HStoreExtensionc                 C   s
   d| _ d S )NZhstorer   r*   r   r   r   r   \   s    zHStoreExtension.__init__Nr2   r   r   r   r   r7   [   s   r7   c                   @   s   e Zd Zdd ZdS )TrigramExtensionc                 C   s
   d| _ d S )NZpg_trgmr   r*   r   r   r   r   a   s    zTrigramExtension.__init__Nr2   r   r   r   r   r8   `   s   r8   c                   @   s   e Zd Zdd ZdS )UnaccentExtensionc                 C   s
   d| _ d S )NZunaccentr   r*   r   r   r   r   f   s    zUnaccentExtension.__init__Nr2   r   r   r   r   r9   e   s   r9   c                   @   s   e Zd Zdd ZdS )NotInTransactionMixinc                 C   s   |j jrtd| jj d S )Nz_The %s operation cannot be executed inside a transaction (set atomic = False on the migration).)r   Zin_atomic_blockr   	__class__r-   r   r"   r   r   r   _ensure_not_in_transactionk   s    z0NotInTransactionMixin._ensure_not_in_transactionN)r-   r.   r/   r=   r   r   r   r   r:   j   s   r:   c                   @   s,   e Zd ZdZdZdd Zdd Zdd Zd	S )
AddIndexConcurrentlyzDCreate an index using PostgreSQL's CREATE INDEX CONCURRENTLY syntax.Fc                 C   s   d| j jd| j j| jf S )Nz7Concurrently create index %s on field(s) %s of model %s, )indexr   joinfields
model_namer*   r   r   r   r+   x   s
    zAddIndexConcurrently.describec                 C   s@   |  | |j|| j}| |jj|r<|j|| jdd d S NT)Zconcurrently)	r=   apps	get_modelrC   allow_migrate_modelr   r   	add_indexr@   r   r   r"   r#   r$   modelr   r   r   r%      s    
z&AddIndexConcurrently.database_forwardsc                 C   s@   |  | |j|| j}| |jj|r<|j|| jdd d S rD   )	r=   rE   rF   rC   rG   r   r   remove_indexr@   rI   r   r   r   r&      s    
z'AddIndexConcurrently.database_backwardsNr-   r.   r/   __doc__Zatomicr+   r%   r&   r   r   r   r   r>   s   s
   r>   c                   @   s,   e Zd ZdZdZdd Zdd Zdd Zd	S )
RemoveIndexConcurrentlyzBRemove an index using PostgreSQL's DROP INDEX CONCURRENTLY syntax.Fc                 C   s   d| j | jf S )Nz$Concurrently remove index %s from %sr   rC   r*   r   r   r   r+      s    z RemoveIndexConcurrently.describec                 C   sZ   |  | |j|| j}| |jj|rV|j|| jf }|	| j
}|j||dd d S rD   )r=   rE   rF   rC   rG   r   r   modelsmodel_name_lowerget_index_by_namer   rK   )r   r   r"   r#   r$   rJ   Zfrom_model_stater@   r   r   r   r%      s    
z)RemoveIndexConcurrently.database_forwardsc                 C   sZ   |  | |j|| j}| |jj|rV|j|| jf }|	| j
}|j||dd d S rD   )r=   rE   rF   rC   rG   r   r   rP   rQ   rR   r   rH   )r   r   r"   r#   r$   rJ   Zto_model_stater@   r   r   r   r&      s    
z*RemoveIndexConcurrently.database_backwardsNrL   r   r   r   r   rN      s
   rN   c                   @   s<   e Zd ZdddddZdd Zdd	 Zd
d Zdd ZdS )CollationOperationlibcT)providerdeterministicc                C   s   || _ || _|| _|| _d S r   )r   localerU   rV   )r   r   rW   rU   rV   r   r   r   r      s    zCollationOperation.__init__c                 C   s   d S r   r   r   r   r   r   r      s    z!CollationOperation.state_forwardsc                 C   sJ   | j | jd}| jr(| jdkr(| j|d< | jdkr<| j|d< | jjg |fS )N)r   rW   rT   rU   FrV   )r   rW   rU   rV   r;   r/   )r   kwargsr   r   r   deconstruct   s    


zCollationOperation.deconstructc              	   C   s   | j dkr|jjjstdd|| ji}| jdkrF|| j|d< | j dkrXd|d< |d|| j	d	
d
d | D d  d S )NFz4Non-deterministic collations require PostgreSQL 12+.rW   rT   rU   falserV   z$CREATE COLLATION %(name)s (%(args)s)r?   c                 s   s    | ]\}}| d | V  qdS )=Nr   ).0optionvaluer   r   r   	<genexpr>   s    z6CollationOperation.create_collation.<locals>.<genexpr>)r   args)rV   r   featuresZ%supports_non_deterministic_collationsr   r   rW   rU   r   r   rA   items)r   r"   r`   r   r   r   create_collation   s(    




z#CollationOperation.create_collationc                 C   s   | d|| j  d S )NzDROP COLLATION %s)r   r   r   r<   r   r   r   remove_collation   s    z#CollationOperation.remove_collationN)r-   r.   r/   r   r   rY   rc   rd   r   r   r   r   rS      s
   rS   c                   @   s4   e Zd ZdZdd Zdd Zdd Zedd	 Zd
S )CreateCollationzCreate a collation.c                 C   s.   |j jdkst|j j|s d S | | d S Nr   )r   r   r   r   r   rc   r!   r   r   r   r%      s     z!CreateCollation.database_forwardsc                 C   s"   t |jj|sd S | | d S r   )r   r   r   r   rd   r!   r   r   r   r&      s    z"CreateCollation.database_backwardsc                 C   s   d| j  S )NzCreate collation r   r*   r   r   r   r+      s    zCreateCollation.describec                 C   s   d| j   S )Nzcreate_collation_%sr   lowerr*   r   r   r   r,      s    z'CreateCollation.migration_name_fragmentN	r-   r.   r/   rM   r%   r&   r+   r0   r,   r   r   r   r   re      s   re   c                   @   s4   e Zd ZdZdd Zdd Zdd Zedd	 Zd
S )RemoveCollationzRemove a collation.c                 C   s.   |j jdkst|j j|s d S | | d S rf   )r   r   r   r   r   rd   r!   r   r   r   r%      s     z!RemoveCollation.database_forwardsc                 C   s"   t |jj|sd S | | d S r   )r   r   r   r   rc   r!   r   r   r   r&      s    z"RemoveCollation.database_backwardsc                 C   s   d| j  S )NzRemove collation r   r*   r   r   r   r+      s    zRemoveCollation.describec                 C   s   d| j   S )Nzremove_collation_%srg   r*   r   r   r   r,      s    z'RemoveCollation.migration_name_fragmentNri   r   r   r   r   rj      s   rj   c                       s@   e Zd ZdZ fddZdd Zdd Ze fdd	Z  Z	S )
AddConstraintNotValidzg
    Add a table constraint without enforcing validation, using PostgreSQL's
    NOT VALID syntax.
    c                    s$   t |tstdt || d S )Nz<AddConstraintNotValid.constraint must be a check constraint.)
isinstancer   	TypeErrorsuperr   )r   rC   
constraintr;   r   r   r     s
    
zAddConstraintNotValid.__init__c                 C   s   d| j j| jf S )Nz*Create not valid constraint %s on model %s)ro   r   rC   r*   r   r   r   r+     s    zAddConstraintNotValid.describec                 C   sL   |j || j}| |jj|rH| j||}|rH|jt	|d d d d S )Nz
 NOT VALID)params)
rE   rF   rC   rG   r   r   ro   Z
create_sqlr   str)r   r   r"   r#   r$   rJ   Zconstraint_sqlr   r   r   r%     s
    z'AddConstraintNotValid.database_forwardsc                    s   t  jd S )NZ
_not_valid)rn   r,   r*   rp   r   r   r,   "  s    z-AddConstraintNotValid.migration_name_fragment)
r-   r.   r/   rM   r   r+   r%   r0   r,   __classcell__r   r   rp   r   rk     s   
rk   c                   @   sL   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Ze	dd Z
dd ZdS )ValidateConstraintz&Validate a table NOT VALID constraint.c                 C   s   || _ || _d S r   rC   r   )r   rC   r   r   r   r   r   *  s    zValidateConstraint.__init__c                 C   s   d| j | jf S )Nz"Validate constraint %s on model %srO   r*   r   r   r   r+   .  s    zValidateConstraint.describec                 C   sH   |j || j}| |jj|rD|d||jj	|| j
f  d S )Nz%ALTER TABLE %s VALIDATE CONSTRAINT %s)rE   rF   rC   rG   r   r   r   r   Z_metaZdb_tabler   rI   r   r   r   r%   1  s    
z$ValidateConstraint.database_forwardsc                 C   s   d S r   r   r!   r   r   r   r&   <  s    z%ValidateConstraint.database_backwardsc                 C   s   d S r   r   r   r   r   r   r   @  s    z!ValidateConstraint.state_forwardsc                 C   s   d| j  | j f S )Nz%s_validate_%s)rC   rh   r   r*   r   r   r   r,   C  s    z*ValidateConstraint.migration_name_fragmentc                 C   s   | j jg | j| jdfS )Nru   )r;   r-   rC   r   r*   r   r   r   rY   G  s    zValidateConstraint.deconstructN)r-   r.   r/   rM   r   r+   r%   r&   r   r0   r,   rY   r   r   r   r   rt   '  s   
rt   N) Zdjango.contrib.postgres.signalsr   r   r   Z	django.dbr   r   Zdjango.db.migrationsr   r   r	   Z$django.db.migrations.operations.baser
   Zdjango.db.models.constraintsr   r   r1   r3   r4   r5   r6   r7   r8   r9   r:   r>   rN   rS   re   rj   rk   rt   r   r   r   r   <module>   s*   6	2"