o 3aA@sddlZddlZddlmZddlmZddlmZmZm Z ddl m Z m Z ddl mZmZmZddlmZddlmZdd lmZdd lmZmZdd lmZdd lmZGd ddeZdS)N) import_module)apps) BaseCommand CommandErrorno_translations)emit_post_migrate_signalemit_pre_migrate_signal)DEFAULT_DB_ALIAS connectionsrouter)MigrationAutodetector)MigrationExecutor)AmbiguityError) ModelState ProjectState)module_has_submodule) Truncatorc@sFeZdZdZgZddZeddZddd Zd d Z e d d Z dS)CommandzMUpdates database schema. Manages both apps with migrations and those without.cCs|jdddd|jdddd|jd dd d|jd d d ddd|jdtdd|jdddd|jdddd|jdddd|jdddd|jddddddS)Nz --skip-checks store_truezSkip system checks.)actionhelp app_label?z5App label of an application to synchronize the state.)nargsrmigration_namezpDatabase state will be brought to the state after that migration. Use the name "zero" to unapply all migrations.z --noinputz --no-input store_false interactivez:Tells Django to NOT prompt the user for input of any kind.)rdestrz --databasezHNominates a database to synchronize. Defaults to the "default" database.)defaultrz--fakez5Mark migrations as run without actually running them.z--fake-initialzDetect if tables already exist and fake-apply initial migrations if so. Make sure that the current database schema matches your initial migration before using this flag. Django will only check for an existing table name.z--planz=Shows a list of the migration actions that will be performed.z --run-syncdbz+Creates tables for apps without migrations.z--checkcheck_unappliedz;Exits with a non-zero status if unapplied migrations exist.) add_argumentr )selfparserr#I/usr/lib/python3/dist-packages/django/core/management/commands/migrate.py add_argumentssR zCommand.add_argumentsc! s|d}|ds|j|gd|d|_|d|_tD]}t|jdr+td|jqt |}| t ||j }|j ||j }|rYdd d |D}td ||d } d } |dr|dztWnty} ztt| d} ~ ww| r|j jvrtdn |j jvrtd|dr|dr|d} | dkrdfg} n-z |j | }Wntytd| ftytd| fw|jfg} d} n|drfdd|j jD} n|j j} || }|o|d}|dr[|jd|jj |s|jd|D]1\}}|jt||jj!|j"D]}|#||\}}|rB|jj$nd}|jd||q1q|rYt%&ddS|rct%&d|d ok|j j'} |jdkr|j|j!d| r|dr|j|j dn|j|j d d!t(|j j'| r|j|j d"d!t(d#d$| Dpd%n4| d&ddur|j|j d't| d&d&n|j|j d(d)| d&d| d&d&f|j)d d*}|j}t*|j|j|j+||d+| r2|jdkr|j|j!d,|dr*|,|gn|,||j j'|jdkrB|j|j!d-|s|jdkr|jd.t-|j .t/0t}|j1|j jd/}|r|j|j2d0d!d1d t(|D|j|j2d2d}d}n|d3}|d4}|j3| ||4||d5}|5|j}|6!g}|j7D]}|j8|j9f} |:| |j;| qWdn 1swY|es  z!Command.handle..zConflicting migrations detected; multiple leaf nodes in the migration graph: (%s). To fix them run 'python manage.py makemigrations --merge' run_syncdbTrz8Can't use run_syncdb with app '%s' as it has migrations.z"App '%s' does not have migrations.rzerozJMore than one migration matches '%s' in app '%s'. Please be more specific.z4Cannot find a migration matching '%s' from app '%s'.Fcsg|] }|dkr|qS)rr#)r-keyrr#r$ sz"Command.handle..rplanzPlanned operations:z" No planned migration operations.z zOperations to perform:z Synchronize unmigrated app: %sz Synchronize unmigrated apps: r+z Apply all migrations: cSsh|]\}}|qSr#r#)r-anr#r#r$ sz!Command.handle..z(none)rz Unapply all migrations: z Target specific migration: z %s, from %s)with_applied_migrations)rr6z&Synchronizing apps without migrations:zRunning migrations:z No migrations to apply.)graphzl Your models in app(s): %s have changes that are not yet reflected in a migration, and so won't be applied.css|]}t|VqdSN)repr)r-r.r#r#r$r0szk Run 'manage.py makemigrations' to make new migrations, and then re-run 'manage.py migrate' to apply them.fake fake_initial)r6stater?r@cSsg|] }ttj|qSr#)r from_modelr get_model)r-modelr#r#r$r5s)>checkr)rrget_app_configsrmodulernamer prepare_databaser migration_progress_callbackloadercheck_consistent_historydetect_conflictsr,itemsrget_app_config LookupErrorstr migrated_appsget_migration_by_prefixrKeyErrorr< leaf_nodesmigration_planstdoutwritestyle MIGRATE_LABELMIGRATE_HEADING operationsdescribe_operationWARNINGsysexitunmigrated_appssorted_create_project_stateralias sync_appsr project_stater from_appschangesNOTICEmigratecloneclear_delayed_apps_cache bulk_update real_modelsr name_lowerappendunregister_modelrender_multipler)!r!argsoptionsr& app_config connectionexecutor conflictsname_strr1target_app_labels_onlyerrrtargets migrationr6exit_dry backwards operationmessageis_errorrYpre_migrate_statepre_migrate_apps autodetectorrhr?r@post_migrate_statepost_migrate_apps model_keys model_state model_keyr#r4r$handleGsJ                                               zCommand.handleNFcCs|jdkr|jdk}|dkr&|rt|_|jjd|dd|jdS|dkrU|r5dt|jnd}|rG|j|jd|dS|j|jd |dS|d krq|r`t|_|jjd |dd|jdS|d kr|rdt|jnd}|r|j|jd|dS|j|jd |dS|d kr|rt|_|jjddd|jdS|dkr|rdt|jnd}|j|jd|dSdSdS)Nr7 apply_startz Applying %s...)ending apply_successz (%.3fs)z FAKEDz OK unapply_startz Unapplying %s...unapply_success render_startz Rendering model states...render_successz DONE) r)time monotonicstartrWrXflushrYSUCCESS)r!rr}r? compute_timeelapsedr#r#r$rJs@     z#Command.migration_progress_callbackc sJ}j|Wdn1swYfddtD}fddfdd|D}|jdkrA|jd V}| D]5\}}|D].} | j sYqP|jd krj|jd || j j f|jdkry|jd | j j || qPqJ|jdkr|jd WddSWddS1swYdS)z;Run the old syncdb-style operation on a list of app_labels.Ncs8g|]}|jdur|jvr|jtj|jddfqS)NF)include_auto_created) models_modulelabelr get_migratable_modelsrd)r-ru) app_labelsrvr#r$r58sz%Command.sync_apps..cs6|j}jj}||jvp|jo||jjjv Sr=)_meta introspectionidentifier_converterdb_table auto_created)rDopts converter)rvtablesr#r$model_installedAs z*Command.sync_apps..model_installedcs i|] \}}|tt|qSr#)listfilter)r-app_name model_list)rr#r$ Isz%Command.sync_apps..r7z Creating tables...z Processing %s.%s modelz Creating table %sz Running deferred SQL...)cursorr table_namesrrFr)rWrX schema_editorrNr can_migrate object_namer create_model) r!rvrr all_modelsmanifesteditorrrrDr#)rrvrrr$re2s<           "zCommand.sync_appscCsd}d}t|dr|r|jn|j}|r|jpdnd}nt|dr)|r%|jn|j}nd}|r/d}|dur () hasattr reverse_coder__doc__ reverse_sqlrrQreplacerdescribechars)rrprefixrrr truncatedr#r#r$r]cs*   zCommand.describe_operation)NF) __name__ __module__ __qualname__rrequires_system_checksr%rrrJre staticmethodr]r#r#r#r$rs.  I"1r)r_r importlibr django.appsrdjango.core.management.baserrrdjango.core.management.sqlrr django.dbr r r !django.db.migrations.autodetectorr django.db.migrations.executorr django.db.migrations.loaderrdjango.db.migrations.staterrdjango.utils.module_loadingrdjango.utils.textrrr#r#r#r$s