
9^c           @   s   d  Z  d d l Z d d l Z d d l m Z d d l m Z m Z d d l m	 Z	 d d l
 m Z m Z m Z d d l m Z d d l m Z d	 e f d
     YZ d S(   s\   
reset_db command

originally from http://www.djangosnippets.org/snippets/828/ by dnordberg
iN(   t   settings(   t   BaseCommandt   CommandError(   t   input(   t   SQLITE_ENGINESt   POSTGRESQL_ENGINESt   MYSQL_ENGINES(   t   parse_mysql_cnf(   t   signalcommandt   Commandc           B   s&   e  Z d  Z d   Z e d    Z RS(   s%   Resets the database for this project.c         C   sT  t  t |   j |  | j d d d d d d t d d | j d	 d d
 d d d t d d | j d d d d d d d d  d d | j d d d d d d d d  d d | j d d d d d d d d  d d | j d d d d d d d d  d d | j d d d d d d  d d d d! | j d" d# d d
 d d$ d t d d% d  S(&   Ns	   --noinputt   actiont   store_falset   destt   interactivet   defaultt   helps:   Tells Django to NOT prompt the user for input of any kind.s	   --no-utf8t
   store_truet   no_utf8_supports3   Tells Django to not create a UTF-8 charset databases   -Us   --usert   storet   users=   Use another user for the database than defined in settings.pys   -Os   --ownert   owners[   Use another owner for creating the database than the user defined in settings or via --users   -Ps
   --passwordt   passwordsA   Use another password for the database than defined in settings.pys   -Ds   --dbnamet   dbnames5   Use another database name than defined in settings.pys   -Rs   --routert   routers:   Use this router-database other than defined in settings.pys   -cs   --close-sessionst   close_sessionssE   Close database connections before dropping database (PostgreSQL only)(   t   superR	   t   add_argumentst   add_argumentt   Truet   Falset   None(   t   selft   parser(    (    sP   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/reset_db.pyR      s6    c         O   s  | d } t  j j |  } | d k r; t d |   n  | j d  } d } } } }	 }
 | d k r t |  \ } } } }	 }
 n  | d p | j d  p | } | d p | j d	  p | } | d
 p | } | d p | j d  p | } | d k rt d   n  | j d  p$|	 }	 | j d  p9|
 }
 | d } | d rft d | f  } n d } | d k rd GHd S| t k ry! t j	 d |  t
 j |  Wqt k
 rqXn| t k rd d l } i | d 6| d 6} |	 j d  r|	 | d <n
 |	 | d <|
 r0t |
  | d <n  | j |   } d | } | d rYd n d } d  | | f } t j	 d! |  | j |  t j	 d! |  | j | j    n| t k rd d l } i d" d# 6} | r| | d <n  | r| | d <n  |	 r|	 | d <n  |
 r#|
 | d <n  | j |   } | j d$  | j   } | d% rd& | } t j	 d! | j    y | j |  Wq| j k
 r} t j d' t |   qXn  d( | } t j	 d! |  y | j |  Wn, | j k
 r} t j d' t |   n Xd) | } | r3| d* | 7} n  | d+ 7} t  j rZ| d, t  j 7} n
 | d- 7} t j	 d! |  | j |  n t d. |   | d/ k s| d rd0 GHn  d S(1   s   
        Reset the database for this project.

        Note: Transaction wrappers are in reverse as a work around for
        autocommit, anybody know how to do this the right way?
        R   s   Unknown database router %st   ENGINEt    t   mysqlR   t   USERR   t   PASSWORDR   R   t   NAMEs?   You need to specify DATABASE_NAME in your Django settings file.t   HOSTt   PORTt	   verbosityR   s   
You have requested a database reset.
This will IRREVERSIBLY DESTROY
ALL data in the database "%s".
Are you sure you want to do this?

Type 'yes' to continue, or 'no' to cancel: t   yess   Reset cancelled.Ns   Unlinking %s databaseit   passwdt   /t   unix_sockett   hostt   ports   DROP DATABASE IF EXISTS `%s`R   s   CHARACTER SET utf8s   CREATE DATABASE `%s` %ss   Executing... "%s"t	   template1t   databasei    R   s   
                    SELECT pg_terminate_backend(pg_stat_activity.pid)
                    FROM pg_stat_activity
                    WHERE pg_stat_activity.datname = '%s';
                s	   Error: %ss   DROP DATABASE "%s";s   CREATE DATABASE "%s"s    WITH OWNER = "%s" s    ENCODING = 'UTF8's    TABLESPACE = %s;t   ;s   Unknown database engine %si   s   Reset successful.(   R    t	   DATABASESt   getR   R   R   R   R   t   loggingt   infot   ost   unlinkt   OSErrorR   t   MySQLdbt
   startswitht   intt   connectt   queryt   stripR   t   psycopg2t   set_isolation_levelt   cursort   executet   ProgrammingErrort	   exceptiont   strt   DEFAULT_TABLESPACE(   R   t   argst   optionsR   t   dbinfot   engineR   R   t   database_namet   database_hostt   database_portR   R)   t   confirmt   Databaset   kwargst
   connectiont
   drop_queryt   utf8_supportt   create_queryt   conn_paramsRB   t   close_sessions_queryt   e(    (    sP   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/reset_db.pyt   handle;   s    










	
(   t   __name__t
   __module__R   R   R   RY   (    (    (    sP   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/reset_db.pyR	      s   	%(   t   __doc__R7   R5   t   django.confR    t   django.core.management.baseR   R   t	   six.movesR   t   django_extensions.settingsR   R   R   t"   django_extensions.management.mysqlR   t"   django_extensions.management.utilsR   R	   (    (    (    sP   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/reset_db.pyt   <module>   s   