3
g                 @   sR   d dl mZ d dlmZmZ d dlmZ d dlmZ ddl	m
Z
 G dd dZd	S )
    )Apps)DatabaseErrormodels)classproperty)now   )MigrationSchemaMissingc               @   sd   e Zd ZdZdZedd Zdd Zedd Z	d	d
 Z
dd Zdd Zdd Zdd Zdd ZdS )MigrationRecordera  
    Deal with storing migration records in the database.

    Because this table is actually itself used for dealing with model
    creation, it's the one thing we can't do normally via migrations.
    We manually handle table creation/schema updating (using schema backend)
    and then have a floating model to do queries with.

    If a migration is unapplied its row is removed from the table. Having
    a row in the table always means a migration is applied.
    Nc             C   s(   | j dkr"G dd dtj}|| _ | j S )zl
        Lazy load to avoid AppRegistryNotReady if installed apps import
        MigrationRecorder.
        Nc               @   sF   e Zd ZejddZejddZejedZ	G dd dZ
dd ZdS )	z.MigrationRecorder.Migration.<locals>.Migration   )
max_length)defaultc               @   s   e Zd Ze ZdZdZdS )z3MigrationRecorder.Migration.<locals>.Migration.MetaZ
migrationsZdjango_migrationsN)__name__
__module____qualname__r   ZappsZ	app_labeldb_table r   r   [/var/www/tester-filtro-web/env/lib/python3.6/site-packages/django/db/migrations/recorder.pyMeta#   s   r   c             S   s   d| j | jf S )NzMigration %s for %s)nameapp)selfr   r   r   __str__(   s    z6MigrationRecorder.Migration.<locals>.Migration.__str__N)r   r   r   r   Z	CharFieldr   r   ZDateTimeFieldr   Zappliedr   r   r   r   r   r   	Migration   s
   r   )_migration_classr   ZModel)clsr   r   r   r   r      s    
zMigrationRecorder.Migrationc             C   s
   || _ d S )N)
connection)r   r   r   r   r   __init__.   s    zMigrationRecorder.__init__c             C   s   | j jj| jjS )N)r   ZobjectsZusingr   alias)r   r   r   r   migration_qs1   s    zMigrationRecorder.migration_qsc          	   C   s2   | j j }| j jj|}W dQ R X | jjj|kS )z2Return True if the django_migrations table exists.N)r   cursorZintrospectionZtable_namesr   Z_metar   )r   r   Ztablesr   r   r   	has_table5   s    zMigrationRecorder.has_tablec             C   sf   | j  rdS y&| jj }|j| j W dQ R X W n. tk
r` } ztd| W Y dd}~X nX dS )z3Ensure the table exists and has the correct schema.Nz1Unable to create the django_migrations table (%s))r    r   Zschema_editorZcreate_modelr   r   r   )r   Zeditorexcr   r   r   ensure_schema;   s    zMigrationRecorder.ensure_schemac             C   s    | j  rdd | jD S i S dS )z}
        Return a dict mapping (app_name, migration_name) to Migration instances
        for all applied migrations.
        c             S   s   i | ]}||j |jfqS r   )r   r   ).0Z	migrationr   r   r   
<dictcomp>N   s    z8MigrationRecorder.applied_migrations.<locals>.<dictcomp>N)r    r   )r   r   r   r   applied_migrationsH   s    z$MigrationRecorder.applied_migrationsc             C   s   | j   | jj||d dS )z$Record that a migration was applied.)r   r   N)r"   r   create)r   r   r   r   r   r   record_appliedT   s    z MigrationRecorder.record_appliedc             C   s    | j   | jj||dj  dS )z&Record that a migration was unapplied.)r   r   N)r"   r   filterdelete)r   r   r   r   r   r   record_unappliedY   s    z"MigrationRecorder.record_unappliedc             C   s   | j j j  dS )z<Delete all migration records. Useful for testing migrations.N)r   allr)   )r   r   r   r   flush^   s    zMigrationRecorder.flush)r   r   r   __doc__r   r   r   r   propertyr   r    r"   r%   r'   r*   r,   r   r   r   r   r	   	   s   r	   N)Zdjango.apps.registryr   Z	django.dbr   r   Zdjango.utils.functionalr   Zdjango.utils.timezoner   
exceptionsr   r	   r   r   r   r   <module>   s
   