
    EvgA                         d dl Z d dlZd dlmZ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mZmZmZ d	Z G d
 d          ZdS )    N)import_modulereload)apps)settings)MigrationGraph)MigrationRecorder   )AmbiguityErrorBadMigrationErrorInconsistentMigrationHistoryNodeNotFoundError
migrationsc                   z    e Zd ZdZ	 	 	 ddZed             Zd Zd Zd Z	d	 Z
d
 Zd Zd Zd Zd ZddZd ZdS )MigrationLoaderal  
    Load migration files from disk and their status from the database.

    Migration files are expected to live in the "migrations" directory of
    an app. Their names are entirely unimportant from a code perspective,
    but will probably follow the 1234_name.py convention.

    On initialization, this class will scan those directories, and open and
    read the Python files, looking for a class called Migration, which should
    inherit from django.db.migrations.Migration. See
    django.db.migrations.migration for what that looks like.

    Some migrations will be marked as "replacing" another set of migrations.
    These are loaded into a separate set of migrations away from the main ones.
    If all the migrations they replace are either unapplied or missing from
    disk, then they are injected into the main set, replacing the named migrations.
    Any dependency pointers to the replaced migrations are re-pointed to the
    new migration.

    This does mean that this class MUST also talk to the database as well as
    to disk, but this is probably fine. We're already not just operating
    in memory.
    TFc                 |    || _         d | _        d | _        || _        || _        |r|                                  d S d S N)
connectiondisk_migrationsapplied_migrationsignore_no_migrationsreplace_migrationsbuild_graph)selfr   loadr   r   s        X/var/www/pixelcanvas.ch/venv/lib/python3.11/site-packages/django/db/migrations/loader.py__init__zMigrationLoader.__init__-   sU     %#"&$8!"4 		 	    c                     |t           j        v rt           j        |         dfS t          j        |          j        }|dt
          dfS )z
        Return the path to the migrations module for the specified app_label
        and a boolean indicating if the module is specified in
        settings.MIGRATION_MODULE.
        T.F)r   MIGRATION_MODULESr   get_app_confignameMIGRATIONS_MODULE_NAME)cls	app_labelapp_package_names      r   migrations_modulez!MigrationLoader.migrations_module<   sS     222-i8$>>#29==B...0F0FGNNr   c           	         i | _         t                      | _        t                      | _        t	          j                    D ]<}|                     |j                  \  }}| | j                            |j                   B|t          j
        v }	 t          |          }t          |d          s | j                            |j                   t          |dd          :t          |j        t                     s | j                            |j                   |rt#          |           nd# t$          $ rW}|r| j        s#|sFt(          |j                            d          v r%| j                            |j                   Y d}~H d}~ww xY w| j                            |j                   d t/          j        |j                  D             }|D ]}|d|}		 t          |	          }
n7# t2          $ r*}dt5          |          v rt3          d|	z            | d}~ww xY wt          |
d          st7          d	|d
|j        d          |
                    ||j                  | j         |j        |f<   >dS )z6Load the migrations from all INSTALLED_APPS from disk.N__path____file__r   c                 2    h | ]\  }}}|s|d          dv|S )r   z_~ ).0_r"   is_pkgs       r   	<setcomp>z,MigrationLoader.load_disk.<locals>.<setcomp>o   sA       #AtV #'q'"5"5 "5"5"5r   zbad magic numberz9Couldn't import %r as it appears to be a stale .pyc file.	Migrationz
Migration z in app z has no Migration class)r   setunmigrated_appsmigrated_appsr   get_app_configsr'   labeladdsysmodulesr   hasattrgetattr
isinstancer)   listr   ModuleNotFoundErrorr   r#   r"   splitpkgutiliter_modulesImportErrorstrr   r1   )r   
app_configmodule_nameexplicit
was_loadedmoduleemigration_namesmigration_namemigration_pathmigration_modules              r   	load_diskzMigrationLoader.load_diskI   s    !"uu UU.00 =	 =	J$($:$::;K$L$L!K"$(()9:::$3J#&{33 vz22 (,,Z-=>>> 6:t44<ZOTF F< (,,Z-=>>> #6NNN- '    !:  %;qv||C?P?P%P%P(,,Z-=>>>HHHH. "":#3444 '.';FO'L'L  O #2  ,7KK!H	'4^'D'D$$"   )SVV33))+9:   !!
  /== ++)>>:+;+;+;=  
 %..&"(  $Z%5~%EFF#O=	 =	s7   D..
F8AF
	F

F G00
H$:%HH$c                 *    | j         j        ||f         S )z6Return the named migration or raise NodeNotFoundError.graphnodes)r   r%   name_prefixs      r   get_migrationzMigrationLoader.get_migration   s    z	; 677r   c                 ,   g }| j         D ]7\  }}||k    r,|                    |          r|                    ||f           8t          |          dk    rt	          d|d|d          |st          d| d| d          | j         |d                  S )zZ
        Return the migration(s) which match the given app label and name_prefix.
        r	   z&There is more than one migration for 'z' with the prefix ''zThere is no migration for 'r   )r   
startswithappendlenr
   KeyError)r   r%   rS   resultsmigration_app_labelrK   s         r   get_migration_by_prefixz'MigrationLoader.get_migration_by_prefix   s    
 373G 	F 	F/"i//N4M4M5 5/  3^DEEEw<<! .99kkk+    	4#i # ## # #  
 '
33r   c                    |d         dk    r|d         dk    s	|| j         v r|S |d         |k    rd S |d         | j        v rd S |d         | j        v r	 |d         dk    r&| j                             |d                   d         S | j                             |d                   d         S # t
          $ r# | j        rY d S t          d|d         z            w xY wt          d|d         z            )Nr	   	__first__
__latest__r   z(Dependency on app with no migrations: %szDependency on unknown app: %s)rQ   r3   r4   
root_nodes
leaf_nodes
IndexErrorr   
ValueError)r   keycurrent_apps      r   	check_keyzMigrationLoader.check_key   s(   Fk!!c!f&<&<
ARARJ
 q6[  Fq6T))) Fq6T'''q6[((:00Q88;;:00Q88;;   , 44$BSVK  	 83q6ABBBs   1B+ %B+ +C?Cc                     |j         D ]>}|d         |d         k    r*|d         dk    r| j                            |||d           ?dS )z
        Internal dependencies need to be added first to ensure `__first__`
        dependencies find the correct root node.
        r   r	   r_   Tskip_validationN)dependenciesrQ   add_dependency)r   re   	migrationparents       r   add_internal_dependenciesz)MigrationLoader.add_internal_dependencies   sf    
  , 	X 	XFayCF""vayK'?'?
)))S&RV)WWW	X 	Xr   c                 D   |j         D ]Q}|d         |d         k    r|                     ||d                   }|| j                            |||d           R|j        D ]>}|                     ||d                   }|| j                            |||d           ?d S )Nr   Tri   )rk   rg   rQ   rl   
run_before)r   re   rm   rn   childs        r   add_external_dependenciesz)MigrationLoader.add_external_dependencies   s    , 	X 	XF1v""^^FCF33F!
)))S&RV)WWW) 	W 	WENN5#a&11E 
)))UCQU)VVV	W 	Wr   c           	                                              j        i  _        n-t           j                  }|                                 _        t	                       _        i  _         j                                        D ]1\  }} j        	                    ||           |j
        r
| j        |<   2 j                                        D ]\  }}                     ||            j                                        D ]\  }}                     ||            j        rƉ j                                        D ]\  }} fd|j
        D             }t          |          r| j        |<   n j                            |d           t          |          st!          |          s! j                            ||j
                    j                            ||j
                   	  j                                         n'# t(          $ r}i } j                                        D ]D\  }}|j
        D ]7}|                    |t-                                                    |           8E|j        |v r|                    |j        t-                                }t!           fd|D                       }	|	sfd                    d |D                       }
t)          d                    |j        |j        d         |j        d         |
          |j                  | d}~ww xY w j                                         dS )	z
        Build a migration dependency graph using both the disk and database.
        You'll need to rebuild the graph if you apply migrations. This isn't
        usually a problem as generally migration stuff runs in a one-shot process.
        Nc                 "    g | ]}|j         v S r,   )r   )r-   targetr   s     r   
<listcomp>z/MigrationLoader.build_graph.<locals>.<listcomp>   s.     $ $ $<BVt66$ $ $r   c              3   4   K   | ]}|j         j        v V  d S r   rP   )r-   	candidater   s     r   	<genexpr>z.MigrationLoader.build_graph.<locals>.<genexpr>"  s>       " "6?I!11" " " " " "r   z, c              3       K   | ]	}d |z  V  
dS )z%s.%sNr,   )r-   cs     r   rz   z.MigrationLoader.build_graph.<locals>.<genexpr>&  s&      %F%Fagk%F%F%F%F%F%Fr   zMigration {0} depends on nonexistent node ('{1}', '{2}'). Django tried to replace migration {1}.{2} with any of [{3}] but wasn't able to because some of the replaced migrations are already applied.r   r	   )rN   r   r   r   r   rQ   replacementsr   itemsadd_nodereplacesro   rs   r   allpopanyremove_replaced_nodesremove_replacement_nodevalidate_consistencyr   
setdefaultr2   r7   nodegetjoinformatoriginensure_not_cyclic)r   recorderre   rm   applied_statusesexcreverse_replacementsreplaced
candidatesis_replacedtriess   `          r   r   zMigrationLoader.build_graph   s    	?"&(D##(99H&.&A&A&C&CD# $%%
"288:: 	3 	3NCJY///! 3)2!#&"288:: 	; 	;NC**3	::::"288:: 	; 	;NC**3	::::" 	P"&"3"9"9";"; P PY$ $ $ $FOFX$ $ $ 
 '(( ;3<D+C00+//T::: '(( P5E1F1F PJ44S):LMMMM
 J66sI<NOOOO	J++----  	 	 	 $& "&"3"9"9";"; N NY ) 2 N NH(33HceeDDHHMMMMN x///155chFF
! " " " "CM" " "   # 
 II%F%F:%F%F%FFFE+/ 06vJSXa[%0 0    7	8 	
$$&&&&&s   /H
 
L.DL))L.c                    t          |          }|                                D ]}|| j        j        vr| j        j        |         j        D ]~}|vrx|| j        v r,t          fd| j        |         j        D                       r;t          d
                    |d         |d         |d         |d         |j                            dS )zs
        Raise InconsistentMigrationHistory if any applied migrations have
        unapplied dependencies.
        c              3       K   | ]}|v V  	d S r   r,   )r-   mapplieds     r   rz   z;MigrationLoader.check_consistent_history.<locals>.<genexpr>C  s8        -.AL     r   zHMigration {}.{} is applied before its dependency {}.{} on database '{}'.r   r	   N)r   r   rQ   rR   node_mapparentsr}   r   r   r   r   alias)r   r   r   rm   rn   r   s        @r   check_consistent_historyz(MigrationLoader.check_consistent_history3  s    
 %Z00--//  	 	I
 000*-i8@  (( !222    262CF2K2T     % %6228&%aL%aL"1I"1I&,3 3	 	 	 )		 	r   c                    i t                      }| j                                        D ]S\  }}|v r|                    |                               |t                                                    |           Tfd|D             S )z
        Look through the loaded graph and detect any conflicts - apps
        with more than one leaf migration. Return a dict of the app labels
        that conflict with the migration names that conflict.
        c                 <    i | ]}|t          |                   S r,   )sorted)r-   r%   	seen_appss     r   
<dictcomp>z4MigrationLoader.detect_conflicts.<locals>.<dictcomp>^  s5     
 
 
8AIvi	233
 
 
r   )r2   rQ   rb   r7   r   )r   conflicting_appsr%   rK   r   s       @r   detect_conflictsz MigrationLoader.detect_conflictsR  s     	55)-)>)>)@)@ 	G 	G%I~I%% $$Y///  CEE2266~FFFF
 
 
 
EU
 
 
 	
r   Nc                 F    | j                             ||| j                  S )z
        Return a ProjectState object representing the most recent state
        that the loaded migrations represent.

        See graph.make_state() for the meaning of "nodes" and "at_end".
        )rR   at_end	real_apps)rQ   
make_stater3   )r   rR   r   s      r   project_statezMigrationLoader.project_stateb  s-     z$$$2F % 
 
 	
r   c                 t   g }d}|D ]\  }}| j                             d|j                  5 }|#|                     |j        |j        fd          }|s|                    ||d          }n|                    ||d          }ddd           n# 1 swxY w Y   |                    |j	                   |S )z
        Take a migration plan and return a list of collected SQL statements
        that represent the best-efforts version of that plan.
        NT)collect_sqlatomicF)r   )r   )
r   schema_editorr   r   r%   r"   applyunapplyextendcollected_sql)r   plan
statementsstaterm   	backwardsr   s          r   r   zMigrationLoader.collect_sqlm  s9   
 
$( 	; 	; Iy.. )9 /   
V= ..",in=e /  E ! V%OOE=dOSSEE%--e]PT-UUE
V 
V 
V 
V 
V 
V 
V 
V 
V 
V 
V 
V 
V 
V 
V m9::::s   ABB	B	)TFT)NT)__name__
__module____qualname____doc__r   classmethodr'   rN   rT   r]   rg   ro   rs   r   r   r   r   r   r,   r   r   r   r      s        6 "    
O 
O [
OB B BH8 8 84 4 40C C C<X X XW W WS' S' S'j  >
 
 
 	
 	
 	
 	
    r   r   )r@   r8   	importlibr   r   django.appsr   django.confr   django.db.migrations.graphr   django.db.migrations.recorderr   
exceptionsr
   r   r   r   r#   r   r,   r   r   <module>r      s     



 + + + + + + + +                   5 5 5 5 5 5 ; ; ; ; ; ;            & m m m m m m m m m mr   