o
    ×ŸŽin_  ã                   @   s|   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 G d
d„ deƒZdS )é    N)ÚDecimal)ÚApps)ÚNotSupportedError)ÚBaseDatabaseSchemaEditor)Ú	Statement)Ústrip_quotes)ÚUniqueConstraint)Úatomicc                       sä   e Zd ZdZdZdZeZdZdZdZ	‡ fdd„Z
‡ fd	d
„Zdd„ Zdd„ Z	d)dd„Z	d*‡ fdd„	Zd+‡ fdd„	Z	d,dd„Zd*‡ fdd„	Z‡ fdd„Z‡ fdd„Z	d+dd „Zd!d"„ Z‡ fd#d$„Z‡ fd%d&„Zd'd(„ Z‡  ZS )-ÚDatabaseSchemaEditorzDROP TABLE %(table)sNzEREFERENCES %(to_table)s (%(to_column)s) DEFERRABLE INITIALLY DEFERREDz,ALTER TABLE %(table)s DROP COLUMN %(column)sz7CREATE UNIQUE INDEX %(name)s ON %(table)s (%(columns)s)zDROP INDEX %(name)sc                    s   | j  ¡ s	tdƒ‚tƒ  ¡ S )NzüSQLite schema editor cannot be used while foreign key constraint checks are enabled. Make sure to disable them before entering a transaction.atomic() context because SQLite does not support disabling them in the middle of a multi-statement transaction.)Ú
connectionÚdisable_constraint_checkingr   ÚsuperÚ	__enter__©Úself©Ú	__class__© úkC:\Users\psant\Downloads\Anu Raj Backend\school\venv\Lib\site-packages\django/db/backends/sqlite3/schema.pyr      s
   
ÿ
zDatabaseSchemaEditor.__enter__c                    s(   | j  ¡  tƒ  |||¡ | j  ¡  d S ©N)r   Zcheck_constraintsr   Ú__exit__Úenable_constraint_checking)r   Úexc_typeÚ	exc_valueÚ	tracebackr   r   r   r   %   s   
zDatabaseSchemaEditor.__exit__c                 C   sÀ   zdd l }| |¡}W n ty   Y n
 |jy   Y nw t|tƒr)tt|ƒƒS t|tt	tfƒr5t|ƒS t|tƒrBd| 
dd¡ S |d u rHdS t|tttfƒrVd| ¡  S td|t|ƒf ƒ‚)Nr   z'%s'ú'z''ÚNULLzX'%s'z*Cannot quote parameter value %r of type %s)Úsqlite3ZadaptÚImportErrorZProgrammingErrorÚ
isinstanceÚboolÚstrÚintr   ÚfloatÚreplaceÚbytesÚ	bytearrayÚ
memoryviewÚhexÚ
ValueErrorÚtype)r   Úvaluer   r   r   r   Úquote_value*   s*   ÿ

ÿz DatabaseSchemaEditor.quote_valuec                 C   s
   |   |¡S r   )r,   )r   r+   r   r   r   Úprepare_defaultI   s   
z$DatabaseSchemaEditor.prepare_defaultFc           	      C   s¤   | j  ¡ �C}| j j |¡D ]2}|r|j|krq| j j ||j¡}| ¡ D ]\}}||kr>|du s4||kr>  W d  ƒ dS q$qW d  ƒ dS 1 sKw   Y  dS )a  
        Return whether or not the provided table name is referenced by another
        one. If `column_name` is specified, only references pointing to that
        column are considered. If `ignore_self` is True, self-referential
        constraints are ignored.
        NTF)r   ÚcursorZintrospectionZget_table_listÚnameZget_relationsÚvalues)	r   Ú
table_nameZcolumn_nameÚignore_selfr.   Zother_tableZ	relationsZconstraint_columnZconstraint_tabler   r   r   Ú_is_referenced_by_fk_constraintL   s,   	ÿÿÿõ€ú
ÿôz4DatabaseSchemaEditor._is_referenced_by_fk_constraintTc                    sh   | j jjs*|r*|  |¡r*| j jrtd| ƒ‚| j  ¡  tƒ  |||¡ | j  	¡  d S tƒ  |||¡ d S )Nz°Renaming the %r table while in a transaction is not supported on SQLite < 3.26 because it would break referential integrity. Try adding `atomic = False` to the Migration class.)
r   ÚfeaturesÚ!supports_atomic_references_renamer3   Úin_atomic_blockr   r   r   Úalter_db_tabler   )r   ÚmodelZold_db_tableZnew_db_tableÚdisable_constraintsr   r   r   r7   c   s    ÿþýûÿ
z#DatabaseSchemaEditor.alter_db_tablec              	      sœ  |   ||¡sd S |j}|jj}| ¡ \}}|j|krÂ| jjjsÂ| j||dd�rÂ| jj	r5t
d|jj|f ƒ‚t| jjƒ�b tƒ j||||d� | j ¡ �C}	|	 d¡ ¡ d }
|	 d¡ d| }| ¡ d	 }|| }|| }|	 d
||f¡ |	 d|
d	  ¡ |	 d¡ |	 d¡ W d   ƒ n1 s‘w   Y  W d   ƒ n1 s w   Y  | j ¡ �}	|	 d¡ W d   ƒ d S 1 s»w   Y  d S tƒ j||||d� d S )NT)r2   z´Renaming the %r.%r column while in a transaction is not supported on SQLite < 3.26 because it would break referential integrity. Try adding `atomic = False` to the Migration class.)ÚstrictzPRAGMA schema_versionr   zPRAGMA writable_schema = 1z REFERENCES "%s" ("%%s") é   z3UPDATE sqlite_master SET sql = replace(sql, %s, %s)zPRAGMA schema_version = %dzPRAGMA writable_schema = 0zPRAGMA integrity_checkZVACUUM)Z_field_should_be_alteredr/   Ú_metaÚdb_tableZget_attname_columnr   r4   r5   r3   r6   r   r	   Úaliasr   Úalter_fieldr.   ÚexecuteZfetchone)r   r8   Ú	old_fieldÚ	new_fieldr:   Zold_field_namer1   Ú_Zold_column_namer.   Zschema_versionZreferences_templateZnew_column_nameÚsearchÚreplacementr   r   r   r?   z   sX   ÿþÿý
ûÿÿ
þ
î€û"ÿz DatabaseSchemaEditor.alter_fieldc              	      sj  ‡fdd„‰‡fdd„ˆj jD ƒ}‡fdd„ˆj jD ƒ}i ‰d}|p#g }t|ddƒs3td	d
„ |D ƒƒr[t| ¡ ƒD ]!\‰}|jrZt‡fdd
„|D ƒƒsZd|_|}|jrZ|ˆ= ||j= q9|rs|||j	< |j
ss|jrsˆ ˆ |¡¡||j< |D ]E}	|	\}
}| |
j	d¡ | |
jd¡ |||j	< |
jr«|js«dˆ |
j¡ˆ ˆ |¡¡dœ }|||j< n	ˆ |
j¡||j< |j	ˆ|
j	< quˆ rÕ|ˆ j	= |ˆ j= ˆ j
rÕˆ jjj jrÕˆ ˆ jj¡S tƒ }‡fdd„ˆj jD ƒ}‡fdd„ˆj jD ƒ}ˆj j}ˆ rý‡ fdd„|D ƒ}tˆj jƒ}t |¡}ˆj jˆj j|||||dœ}tdd|ƒ}||d< ˆj|d< tˆj jˆj |ƒ t |¡}ˆj jdt!ˆj jƒ |||||dœ}tdd|ƒ}||d< ˆj|d< tdˆj j ˆj |ƒ}ˆ "|¡ ˆ #dˆ |j j¡d $‡fdd
„|D ƒ¡d $| %¡ ¡ˆ ˆj j¡f ¡ ˆjˆdd� ˆj&||j jˆj jdd� ˆj'D ]}ˆ #|¡ �qŸg ˆ_'|�r³d|_dS dS )a|  
        Shortcut to transform a model from old_model into new_model

        This follows the correct procedure to perform non-rename or column
        addition operations based on SQLite's documentation

        https://www.sqlite.org/lang_altertable.html#caution

        The essential steps are:
          1. Create a table with the updated definition called "new__app_model"
          2. Copy the data from the existing "app_model" table to the new table
          3. Drop the "app_model" table
          4. Rename the "new__app_model" table to "app_model"
          5. Restore any index of the previous "app_model" table.
        c                    s   | j o| jjˆ u S r   )Zis_relationÚremote_fieldr8   )Úf)r8   r   r   Úis_self_referentialÅ   s   z?DatabaseSchemaEditor._remake_table.<locals>.is_self_referentialc                    s$   i | ]}|j ˆ |ƒr| ¡ n|“qS r   )r/   Úclone©Ú.0rG   )rH   r   r   Ú
<dictcomp>É   s    ÿÿz6DatabaseSchemaEditor._remake_table.<locals>.<dictcomp>c                    s   i | ]
}|j ˆ  |j ¡“qS r   )ÚcolumnÚ
quote_namerJ   r   r   r   rL   Ï   s    ÿÿNÚprimary_keyFc                 s   s    � | ]\}}t |d dƒV  qdS )rO   FN)Úgetattr©rK   rC   rB   r   r   r   Ú	<genexpr>Ù   s   € 
ÿz5DatabaseSchemaEditor._remake_table.<locals>.<genexpr>c                 3   s   � | ]
\}}ˆ |j kV  qd S r   ©r/   rQ   rS   r   r   rR   Ý   s
   € ÿ
ýzcoalesce(%(col)s, %(default)s))ÚcolÚdefaultc                    ó   g | ]}‡ fd d„|D ƒ‘qS )c                    ó   g | ]}ˆ   ||¡‘qS r   ©Úget©rK   Ún©Úrename_mappingr   r   Ú
<listcomp>  ó    úADatabaseSchemaEditor._remake_table.<locals>.<listcomp>.<listcomp>r   )rK   Úuniquer\   r   r   r^     ó    ÿÿz6DatabaseSchemaEditor._remake_table.<locals>.<listcomp>c                    rV   )c                    rW   r   rX   rZ   r\   r   r   r^     r_   r`   r   ©rK   Úindexr\   r   r   r^     rb   c                    s   g | ]
}ˆ j |jvr|‘qS r   )r/   Úfieldsrc   ©Údelete_fieldr   r   r^     s
    ÿÿ)Ú	app_labelr=   Úunique_togetherÚindex_togetherÚindexesÚconstraintsÚappsZMetar   Ú
__module__znew__%szNew%sú%INSERT INTO %s (%s) SELECT %s FROM %sú, c                 3   s   � | ]}ˆ   |¡V  qd S r   )rN   )rK   Úxr   r   r   rR   R  s   € )Úhandle_autom2m)r9   T)(r<   Zlocal_concrete_fieldsrP   ÚanyÚlistÚitemsrO   Úauto_createdrM   r/   Úmany_to_manyZconcreter-   Úeffective_defaultÚpopÚnullrN   rF   ÚthroughÚdelete_modelr   ri   rj   rk   rl   ÚcopyÚdeepcopyrh   r=   r*   rn   Zobject_nameÚ	__bases__r   Úcreate_modelr@   Újoinr0   r7   Údeferred_sql)r   r8   Úcreate_fieldrg   Úalter_fieldsÚbodyÚmappingZrestore_pk_fieldÚfieldr?   rA   rB   Zcase_sqlrm   ri   rj   rk   rl   Z	body_copyZmeta_contentsÚmetaZ	new_modelÚsqlr   )rg   rH   r8   r/   r]   r   r   Ú_remake_table¯   sà   
þ
þÿü€

ÿ

þÿ
þ
þ
þ
ÿ
ù	

ù	

üÿÿü

ÿz"DatabaseSchemaEditor._remake_tablec                    sj   |r
t ƒ  |¡ d S |  | jd|  |jj¡i ¡ t| jƒD ]}t	|t
ƒr2| |jj¡r2| j |¡ qd S )NÚtable)r   r|   r@   Úsql_delete_tablerN   r<   r=   rt   r‚   r   r   Zreferences_tableÚremove)r   r8   rr   r‰   r   r   r   r|   k  s   ÿÿÿÿ€üz!DatabaseSchemaEditor.delete_modelc                    sh   |j r|jjjjr|  |jj¡ dS |js"|js"|jr"|  	|¡dur+| j
||d� dS tƒ  ||¡ dS )zCreate a field on a model.N)rƒ   )rw   rF   r{   r<   rv   r€   rO   ra   rz   rx   rŠ   r   Ú	add_field©r   r8   r‡   r   r   r   rŽ   }  s   ýü	÷
özDatabaseSchemaEditor.add_fieldc                    sŽ   |j r|jjjjr|  |jj¡ dS dS | jjjr1|j	s1|j
s1|js1|jr(|js1tƒ  ||¡ dS |j| jd�d du r>dS | j||d� dS )z…
        Remove a field from a model. Usually involves deleting a column,
        but for M2Ms may involve deleting a table.
        )r   r*   Nrf   )rw   rF   r{   r<   rv   r|   r   r4   Zcan_alter_table_drop_columnrO   ra   Zdb_indexÚdb_constraintr   Úremove_fieldZdb_parametersrŠ   r�   r   r   r   r‘   ’  s(   ÿÿüûúùù	z!DatabaseSchemaEditor.remove_fieldc	                 C   sX  | j jjr0|j|jkr0|  ||¡|  ||¡kr0|jr|js0|jr#|js0|  |  |j	j
|||¡¡S | j|||fgd� | d¡}	| d¡}
|jr¦||ksO|	|
kr¨tƒ }|jj	}|jD ]&}|j|kraqY|jsq|j|jkrp| |j¡ qY|jr|jj	jr| |j¡ qY|jrœ|jD ]}|j|krŽq†|jjj	jr›| |jj¡ q†|D ]}|  |¡ qždS dS dS )z3Perform a "physical" (non-ManyToMany) field update.©r„   Ú	collationN)r   r4   Zcan_alter_table_rename_columnrM   Z
column_sqlrF   r�   r@   Z_rename_field_sqlr<   r=   rŠ   rY   ra   Úsetr8   Zrelated_objectsÚrelated_modelrw   Ú
field_namer/   ÚaddrO   r{   rv   )r   r8   rA   rB   Zold_typeÚnew_typeZold_db_paramsZnew_db_paramsr:   Zold_collationZnew_collationZrelated_modelsÚoptsrF   rw   r•   r   r   r   Ú_alter_field®  sd   ÿ
þýûúùøÿÿ

ÿÿ

€€

€êz!DatabaseSchemaEditor._alter_fieldc                 C   sô   |j jjj|j jjjkr=| j|j j|j jj | ¡ ¡|j jj | ¡ ¡f|j jj | ¡ ¡|j jj | ¡ ¡fgd� dS |  |j j¡ |  	d|  
|j jjj¡d d| ¡ | ¡ g¡d d| ¡ | ¡ g¡|  
|j jjj¡f ¡ |  |j j¡ dS )z*Alter M2Ms to repoint their to= endpoints.r’   Nro   rp   Úid)rF   r{   r<   r=   rŠ   Ú	get_fieldZm2m_reverse_field_nameZm2m_field_namer€   r@   rN   r�   Zm2m_column_nameZm2m_reverse_namer|   )r   r8   rA   rB   r:   r   r   r   Ú_alter_many_to_manyé  sX   

ÿÿ
ÿ
ÿù
ÿ
ÿùôþýÿýÿðÿÿz(DatabaseSchemaEditor._alter_many_to_manyc                    óB   t |tƒr|js|js|js|jrtƒ  ||¡ d S |  |¡ d S r   )	r   r   Ú	conditionÚcontains_expressionsÚincludeÚ
deferrabler   Úadd_constraintrŠ   ©r   r8   Ú
constraintr   r   r   r£   )  ó   
ÿþýüz#DatabaseSchemaEditor.add_constraintc                    rž   r   )	r   r   rŸ   r    r¡   r¢   r   Úremove_constraintrŠ   r¤   r   r   r   r§   4  r¦   z&DatabaseSchemaEditor.remove_constraintc                 C   s   d| S )NzCOLLATE r   )r   r“   r   r   r   Ú_collate_sql?  s   z!DatabaseSchemaEditor._collate_sql)NF)T)F)NNN)Ú__name__rn   Ú__qualname__rŒ   Zsql_create_fkZsql_create_inline_fkZsql_create_column_inline_fkZsql_delete_columnZsql_create_uniqueZsql_delete_uniquer   r   r,   r-   r3   r7   r?   rŠ   r|   rŽ   r‘   rš   r�   r£   r§   r¨   Ú__classcell__r   r   r   r   r
      s<    ÿ
ÿÿ6
ÿ =%
÷;@r
   )r}   Údecimalr   Zdjango.apps.registryr   Z	django.dbr   Zdjango.db.backends.base.schemar   Z!django.db.backends.ddl_referencesr   Zdjango.db.backends.utilsr   Zdjango.db.modelsr   Zdjango.db.transactionr	   r
   r   r   r   r   Ú<module>   s    