o 3a)2@s\ddlmZddlmZddlmZmZeGdddZGdddeZGd d d Z d S) )total_ordering) ProjectState)CircularDependencyErrorNodeNotFoundErrorc@sXeZdZdZddZddZddZdd Zd d Zd d Z ddZ ddZ ddZ dS)Nodezp A single node in the migration graph. Contains direct links to adjacent nodes in either direction. cCs||_t|_t|_dSN)keysetchildrenparents)selfr rrr) __class____name__r rrrr__repr__"sz Node.__repr__cC|j|dSr)r add)r childrrr add_child%zNode.add_childcCr"r)r r#)r parentrrr add_parent(r&zNode.add_parentN) r __module__ __qualname____doc__rrrrrrr!r%r(rrrrrs rcs(eZdZdZfddZddZZS) DummyNodea A node that doesn't correspond to a migration file on disk. (A squashed migration that was removed, for example.) After the migration graph is processed, all dummy nodes should be removed. If there are any left, a nonexistent dependency error is raised. cst|||_||_dSr)superrorigin error_message)r r r.r/rrrr4s  zDummyNode.__init__cCst|j|j|jd)N)r.)rr/r r.rrrr raise_error9szDummyNode.raise_error)r r)r*r+rr1 __classcell__rrr0rr,,s r,c@seZdZdZddZddZddZd+d d Zd d Zd dZ ddZ ddZ ddZ d,ddZ d-ddZd-ddZddZdd Zd!d"Zd#d$Zd%d&Zd.d'd(Zd)d*ZdS)/MigrationGraphaH Represent the digraph of all migrations in a project. Each migration is a node, and each dependency is an edge. There are no implicit dependencies between numbered migrations - the numbering is merely a convention to aid file listing. Every new numbered migration has a declared dependency to the previous number, meaning that VCS branch merges can be detected and resolved. Migrations files can be marked as replacing another set of migrations - this is to support the "squash" feature. The graph handler isn't responsible for these; instead, the code to load them in here should examine the migration files and if the replaced migrations are all either unapplied or not present, it should ignore the replaced ones, load in just the replacing migration, and repoint any dependencies that pointed to the replaced migrations to point to the replacing one. A node should be a tuple: (app_path, migration_name). The tree special-cases things within an app - namely, root nodes and leaf nodes ignore dependencies to other apps. cCsi|_i|_dSr)node_mapnodesrrrrrTs zMigrationGraph.__init__cCs.||jvsJt|}||j|<||j|<dSr)r4rr5)r r migrationnoderrradd_nodeXs zMigrationGraph.add_nodecCs$t|||}||j|<d|j|<dSr)r,r4r5)r r r.r/r7rrradd_dummy_node^s  zMigrationGraph.add_dummy_nodeFcCs||jvrd||f}||||||jvr$d||f}|||||j||j||j||j||sB|dSdS)z This may create dummy nodes if they don't yet exist. If `skip_validation=True`, validate_consistency() should be called afterwards. z=Migration %s dependencies reference nonexistent child node %rz>Migration %s dependencies reference nonexistent parent node %rN)r5r9r4r(r%validate_consistency)r r6r$r'skip_validationr/rrradd_dependencycs"   zMigrationGraph.add_dependencyc Cst|}z|j|}Wnty }z td|f||d}~ww|D]H}|j|d|j|d}|rk|jD]}|j||j |vrO| || |q8|jD]}|j||j |vrj| || |qSq#dS)z Remove each of the `replaced` nodes (when they exist). Any dependencies that were referencing them are changed to reference the `replacement` node instead. zjUnable to find replacement node %r. It was either never added to the migration graph, or has been removed.N) r r4KeyErrorrr5popr r remover r%r() r replacementreplacedreplacement_nodeerr replaced_key replaced_noder$r'rrrremove_replaced_nodeszs>          z$MigrationGraph.remove_replaced_nodesc Cs|j|dz|j|}Wnty$}z td|f||d}~wwt}t}|D]}|j|}|rA||||jO}q-||8}|j D]} | j ||D] }| | | |qSqI|jD]} | j |qddS)a* The inverse operation to `remove_replaced_nodes`. Almost. Remove the replacement node `replacement` and remap its child nodes to `replaced` - the list of nodes it would have replaced. Don't remap its parent nodes as they are expected to be correct already. NztUnable to remove replacement node %r. It was either never added to the migration graph, or has been removed already.) r5r>r4r=rr getr#r r r?r%r() r r@rArBrCreplaced_nodesreplaced_nodes_parentsr rEr$r'rrrremove_replacement_nodes@        z&MigrationGraph.remove_replacement_nodecCsdd|jDdS)z7Ensure there are no dummy nodes remaining in the graph.cSsg|] }t|tr|qSr) isinstancer,r1.0nrrr sz7MigrationGraph.validate_consistency..N)r4valuesrrrrr:sz#MigrationGraph.validate_consistencycCs*||jvr td|f|||j|S)z Given a node, return a list of which previous nodes (dependencies) must be applied, ending with the node itself. This is the list you would follow if applying the migrations to a database. Node %r not a valid noder5r iterative_dfsr4r targetrrr forwards_plans zMigrationGraph.forwards_plancCs.||jvr td|f||j|j|ddS)z Given a node, return a list of which dependent nodes (dependencies) must be unapplied, ending with the node itself. This is the list you would follow if removing the migrations from a database. rQF)forwardsrRrTrrrbackwards_plans zMigrationGraph.backwards_planTcCsg}t}|dfg}|r?|\}}||vrn&|r%||||jn||df|ddt|r6|jn|jD7}|s |S)z6Iterative depth-first search for finding dependencies.FTcSsg|]}|dfqSFrrLrrrrOsz0MigrationGraph.iterative_dfs..)r r>r#appendr sortedr r )r startrWvisited visited_setstackr7 processedrrrrSs   " zMigrationGraph.iterative_dfsNcRt}|jD]tfdd|jjDr$|r|dkr$|qt|S)z Return all root nodes - that is, nodes with no dependencies inside their app. These are the starting point for an app. c3 |] }|ddkVqdSrNrrMr r7rr z,MigrationGraph.root_nodes..r)r r5allr4r r#r[)r approotsrrer root_nodess  . zMigrationGraph.root_nodescra)am Return all leaf nodes - that is, nodes with no dependents in their app. These are the "most current" version of an app's schema. Having more than one per app is technically an error, but one that gets handled further up, in the interactive command - it's usually the result of a VCS merge and needs some user input. c3rbrcrrdrerrrfrgz,MigrationGraph.leaf_nodes..r)r r5rhr4r r#r[)r rileavesrrer leaf_nodess  . zMigrationGraph.leaf_nodescCst|j}|rS|}|g}|rO|d}|j|jD].}|j}||vr8|||d}tddd|D||vrH| || |nq|}|s|sdSdS)Nz, css|]}d|VqdS)z%s.%sNrrLrrrrfsz3MigrationGraph.ensure_not_cyclic..) r r5r>r4r r indexrjoinrZr?)r todor7r_topr$cyclerrrensure_not_cyclics&    z MigrationGraph.ensure_not_cycliccCs d|S)NzGraph: %s nodes, %s edges)_nodes_and_edgesrrrrrs zMigrationGraph.__str__cCs|\}}d|jj||fS)Nz<%s: nodes=%s, edges=%s>)rurr )r r5edgesrrrr!s zMigrationGraph.__repr__cCs"t|jtdd|jDfS)Ncss|]}t|jVqdSr)lenr )rMr7rrrrf"sz2MigrationGraph._nodes_and_edges..)rwr5sumr4rPrrrrru!s"zMigrationGraph._nodes_and_edgescCs@g}|D]}||D]}||vr|s||vr||q q|Sr)rVrZ)r r5at_endplanr7r6rrr_generate_plan$s zMigrationGraph._generate_plancCsj|dur t|}|stSt|dts|g}|||}t|d}|D] }|j|j|dd}q&|S)z Given a migration node or nodes, return a complete ProjectState for it. If at_end is False, return the state before the migration has run. If nodes is not provided, return the overall most current project state. Nr) real_appsF)preserve)listrmrrKtupler{r5 mutate_state)r r5ryr|rz project_stater7rrr make_state,s   zMigrationGraph.make_statecCs ||jvSr)r5)r r7rrr __contains__>rzMigrationGraph.__contains__rY)Tr)NTN)r r)r*r+rr8r9r<rFrJr:rVrXrSrkrmrtrr!rur{rrrrrrr3=s* #$    r3N) functoolsrdjango.db.migrations.stater exceptionsrrrr,r3rrrrs #