o 3a#@sZddlmZddlmZddlmZddlmZGdddZGdd d e Z d d Z d S) ) operations)get_migration_name_timestamp)atomic)IrreversibleErrorc@sveZdZdZgZgZgZgZdZdZ ddZ ddZ dd Z d d Z d d ZdddZdddZdddZddZdS) Migrationa The base class for all migrations. Migration files will import this from django.db.migrations.Migration and subclass it as a class called Migration. It will have one or more of the following attributes: - operations: A list of Operation instances, probably from django.db.migrations.operations - dependencies: A list of tuples of (app_path, migration_name) - run_before: A list of tuples of (app_path, migration_name) - replaces: A list of migration_names Note that all migrations come out of migrations and into the Loader or Graph as instances, having been initialized with their app label and name. NTcCsH||_||_t|jj|_t|jj|_t|jj|_t|jj|_dSN)name app_labellist __class__r dependencies run_beforereplaces)selfr r r@/usr/lib/python3/dist-packages/django/db/migrations/migration.py__init__5s zMigration.__init__cCs"t|to|j|jko|j|jkSr) isinstancerr r )rotherrrr__eq__>s   zMigration.__eq__cCd|j|jfS)Nzr r rrrr__repr__EzMigration.__repr__cCrNz%s.%srrrrr__str__HrzMigration.__str__cCstd|j|jfSr)hashr r rrrr__hash__KszMigration.__hash__cCs.|}|r|}|jD] }||j|q |S)z Take a ProjectState and return a new one with the migration's operations applied to it. Preserve the original object state by default and return a mutated state from a copy. )clonerstate_forwardsr )r project_statepreserve new_state operationrrr mutate_stateNs  zMigration.mutate_stateFc Cs|jD]k}|r*|jd|js|jd|jd||jd|js*q|}||j||jp?|jo?|jdu}|j se|ret|j j | |j|||Wdn1s_wYq| |j|||q|S)a Take a project_state representing all migrations prior to this one and a schema_editor for a live database and apply the migration in a forwards order. Return the resulting project state for efficient reuse by following Migrations. --B-- MIGRATION NOW PERFORMS OPERATION THAT CANNOT BE WRITTEN AS SQL:-- %sFN) r collected_sqlappendreduces_to_sqldescriber r!r ratomic_migration connectionaliasdatabase_forwards)rr" schema_editor collect_sqlr% old_stateatomic_operationrrrapply\s*    zMigration.applyc Cs*g}|}|jD]%}|jstd||f|}|}||j||d|||fq|D]c\}}} |rY|jd|j sE|jd|jd| |jd|j sYq/|j pc|j oc|j du} |j s| rt |j j||j|| |Wdn1swYq/||j|| |q/|S)a Take a project_state representing all migrations prior to this one and a schema_editor for a live database and apply the migration in a reverse order. The backwards migration process consists of two phases: 1. The intermediate states from right before the first until right after the last operation inside this migration are preserved. 2. The operations are applied in reverse order using the states recorded in step 1. z$Operation %s in %s is not reversiblerr'r(r)FN)r reversiblerr r!r insertr*r+r,r-rr.r/r0database_backwards) rr"r2r3to_runr$r%r4to_state from_stater5rrrunapplys8    zMigration.unapplycCs|d}t|jdkr|jdj}nt|jdkr.tdd|jDr.dtdd|jD}|dur<|jr7dnd t}|S) z Suggest a name for the operations this migration might represent. Names are not guaranteed to be unique, but put some effort into the fallback name to avoid VCS conflicts if possible. Nrrcss|] }t|tjVqdSr)rr CreateModel.0orrr sz)Migration.suggest_name.._css|]}|jVqdSr)migration_name_fragmentr?rrrrBsinitialzauto_%s)lenrrDalljoinsortedrEr)rr rrr suggest_nameszMigration.suggest_name)T)F)__name__ __module__ __qualname____doc__rr rrrErrrrrrr&r6r=rJrrrrrs"    % 3rc@seZdZdZddZdS)SwappableTuplez Subclass of tuple so Django can tell this was originally a swappable dependency when it reads the migration file. cCst||}||_|Sr)tuple__new__setting)clsvaluerRrrrrrQs zSwappableTuple.__new__N)rKrLrMrNrQrrrrrOs rOcCst|ddddf|S)z'Turn a setting value into a dependency..rr __first__)rOsplit)rTrrrswappable_dependencysrXN) django.db.migrationsrdjango.db.migrations.utilsrdjango.db.transactionr exceptionsrrrPrOrXrrrrs   @