[ Avaa Bypassed ]




Upload:

Command:

hmhc3928@3.145.105.85: ~ $


R6�W�]�@sGdZddlmZddlZddlZddlTddlmZddlmZddlmZddlm	Z	dd	lm
Z
dd
lmZGdd�de�Z
d
d�ZGdd�de�ZGdd�de�Zd"ZGdd�dede��ZGdd�de�ZGdd�de�Zd d!�ZdS)#a�

Lightweight schema migrations.

NOTE: Currently tested with SQLite and Postgresql. MySQL may be missing some
features.

Example Usage
-------------

Instantiate a migrator:

    # Postgres example:
    my_db = PostgresqlDatabase(...)
    migrator = PostgresqlMigrator(my_db)

    # SQLite example:
    my_db = SqliteDatabase('my_database.db')
    migrator = SqliteMigrator(my_db)

Then you will use the `migrate` function to run various `Operation`s which
are generated by the migrator:

    migrate(
        migrator.add_column('some_table', 'column_name', CharField(default=''))
    )

Migrations are not run inside a transaction, so if you wish the migration to
run in a transaction you will need to wrap the call to `migrate` in a
transaction block, e.g.:

    with my_db.transaction():
        migrate(...)

Supported Operations
--------------------

Add new field(s) to an existing model:

    # Create your field instances. For non-null fields you must specify a
    # default value.
    pubdate_field = DateTimeField(null=True)
    comment_field = TextField(default='')

    # Run the migration, specifying the database table, field name and field.
    migrate(
        migrator.add_column('comment_tbl', 'pub_date', pubdate_field),
        migrator.add_column('comment_tbl', 'comment', comment_field),
    )

Renaming a field:

    # Specify the table, original name of the column, and its new name.
    migrate(
        migrator.rename_column('story', 'pub_date', 'publish_date'),
        migrator.rename_column('story', 'mod_date', 'modified_date'),
    )

Dropping a field:

    migrate(
        migrator.drop_column('story', 'some_old_field'),
    )

Making a field nullable or not nullable:

    # Note that when making a field not null that field must not have any
    # NULL values present.
    migrate(
        # Make `pub_date` allow NULL values.
        migrator.drop_not_null('story', 'pub_date'),

        # Prevent `modified_date` from containing NULL values.
        migrator.add_not_null('story', 'modified_date'),
    )

Renaming a table:

    migrate(
        migrator.rename_table('story', 'stories_tbl'),
    )

Adding an index:

    # Specify the table, column names, and whether the index should be
    # UNIQUE or not.
    migrate(
        # Create an index on the `pub_date` column.
        migrator.add_index('story', ('pub_date',), False),

        # Create a multi-column index on the `pub_date` and `status` fields.
        migrator.add_index('story', ('pub_date', 'status'), False),

        # Create a unique index on the category and title fields.
        migrator.add_index('story', ('category_id', 'title'), True),
    )

Dropping an index:

    # Specify the index name.
    migrate(migrator.drop_index('story', 'story_pub_date_status'))
�)�
namedtupleN)�*)�CommaClause)�EnclosedClause)�Entity)�
Expression)�Node)�OPc@sReZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�ZdS)
�	Operationz/Encapsulate a single schema altering operation.cOs(||_||_||_||_dS)N)�migrator�method�args�kwargs)�selfrrr
r�r�/migrate.py�__init__us			zOperation.__init__cCs|jjj�}|j|�S)N)r�database�compilerZ
parse_node)r�noderrrr�_parse_node{szOperation._parse_nodecCs/|j|�\}}|jjj||�dS)N)rrr�execute_sql)rr�sqlZparamsrrr�executeszOperation.executecCsrt|t�r|j|�nOt|t�r;|j�n3t|ttf�rnx|D]}|j|�qWWdS)N)�
isinstancerrr
�run�list�tuple�_handle_result)r�result�itemrrrr�s

zOperation._handle_resultcCsE|jj�}d|d<|jt|j|j�|j|��dS)NT�generate)r�copyr�getattrrrr
)rrrrrr�s
z
Operation.runN)	�__name__�
__module__�__qualname__�__doc__rrrrrrrrrr
ss	r
cs%tj���fdd��}|S)Ncs>|jdd�}|r(�|||�St|�j||�S)Nr!F)�popr
r$)rr
rr!)�fnrr�inner�szoperation.<locals>.inner)�	functools�wraps)r)r*r)r)r�	operation�s!r-c@s0eZdZdZdZdd�Zedd��Zedd��Z	edd	��Z
d
d�Zedd
��Zedd��Z
edd��Zeddd��Zedd��Zdd�Zedd��Zedd��Zedd��Zeddd ��Zed!d"��Zd#S)$�SchemaMigratorFcCs
||_dS)N)r)rrrrrr�szSchemaMigrator.__init__cCs@t|t�rt|�St|t�r2t|�St|�SdS)N)rZPostgresqlDatabase�PostgresqlMigratorZ
MySQLDatabase�
MySQLMigrator�SqliteMigrator)�clsrrrr�
from_database�s


zSchemaMigrator.from_databasec
Csm|j}t|�r|�}ttd�t|�td�tt|�tjt|j	|��dd��S)NZUPDATEZSETZflatT)
�default�callable�Clause�SQLrrr	ZEQZParamZdb_value)r�table�column_name�fieldr4rrr�
apply_default�s						zSchemaMigrator.apply_defaultcCs�|jd}|_||_|_|jj�j|�}||_td�t|�td�|g}t|t	�r�|j
|j|��t|�S)NTzALTER TABLEz
ADD COLUMN)
�null�name�	db_columnrrZfield_definitionr7rr�ForeignKeyField�extend�get_inline_fk_sqlr6)rr8r9r:Z
field_nullZfield_clause�partsrrr�alter_add_column�s					zSchemaMigrator.alter_add_columncCs4td�t|jjj�tt|jj��gS)N�
REFERENCES)r7r�	rel_model�_meta�db_tabler�to_fieldr>)rr:rrrrA�s	z SchemaMigrator.get_inline_fk_sqlcCs
t�dS)N)�NotImplementedError)rr8r9r:rrr�add_foreign_key_constraint�sz)SchemaMigrator.add_foreign_key_constraintcCs�|jr)|jdkr)td|��t|t�}|rT|jrTtd��|j|||�g}|js�|j|j|||�|j	||�g�|r�|j
r�|j|j|||j
jj|jj��|S)Nz!%s is not null but has no defaultz'Foreign keys must specify a `to_field`.)r<r4�
ValueErrorrr?rHrCr@r;�add_not_null�explicit_create_foreign_key�appendrJrErFrGr>)rr8r9r:�is_foreign_key�
operationsrrr�
add_column�s$	zSchemaMigrator.add_columncCs
t�dS)N)rI)rr8r9rrr�drop_foreign_key_constraint�sz*SchemaMigrator.drop_foreign_key_constraintTcCs�td�t|�td�t|�g}|rC|jtd��t|�}dd�|jj|�D�}||kr�|jr�|j||�|gS|SdS)NzALTER TABLEzDROP COLUMNZCASCADEcSsg|]}|j�qSr)�column)�.0Zforeign_keyrrr�
<listcomp>s	z.SchemaMigrator.drop_column.<locals>.<listcomp>)r7rrNr6r�get_foreign_keys�explicit_delete_foreign_keyrR)rr8r9�cascade�nodesZdrop_column_nodeZ
fk_columnsrrr�drop_column�s				zSchemaMigrator.drop_columncCs=ttd�t|�td�t|�td�t|��S)NzALTER TABLEz
RENAME COLUMN�TO)r6r7r)rr8�old_name�new_namerrr�
rename_columns					zSchemaMigrator.rename_columncCs(td�t|�td�t|�gS)NzALTER TABLEzALTER COLUMN)r7r)rr8rSrrr�
_alter_columns			zSchemaMigrator._alter_columncCs/|j||�}|jtd��t|�S)NzSET NOT NULL)r_rNr7r6)rr8rSrYrrrrL"szSchemaMigrator.add_not_nullcCs/|j||�}|jtd��t|�S)Nz
DROP NOT NULL)r_rNr7r6)rr8rSrYrrr�
drop_not_null(szSchemaMigrator.drop_not_nullcCs+ttd�t|�td�t|��S)NzALTER TABLEz	RENAME TO)r6r7r)rr\r]rrr�rename_table.s
			zSchemaMigrator.rename_tablecCsn|jj�}|rdnd}tt|�t|j||��td�t|�tdd�|D���S)NzCREATE UNIQUE INDEXzCREATE INDEX�ONcSsg|]}t|��qSr)r)rTrSrrrrU?s	z,SchemaMigrator.add_index.<locals>.<listcomp>)rrr6r7r�
index_namer)rr8�columns�uniquerZ	statementrrr�	add_index6s			zSchemaMigrator.add_indexcCsttd�t|��S)Nz
DROP INDEX)r6r7r)rr8rcrrr�
drop_indexAs	zSchemaMigrator.drop_indexN)r$r%r&rMrWr�classmethodr3r-r;rCrArJrQrRrZr^r_rLr`rarfrgrrrrr.�s(	"

r.cs4eZdZdd�Ze�fdd��Z�S)r/cCs3d}|jj||�}dd�|j�D�S)Nai
            SELECT pg_attribute.attname
            FROM pg_index, pg_class, pg_attribute
            WHERE
                pg_class.oid = '%s'::regclass AND
                indrelid = pg_class.oid AND
                pg_attribute.attrelid = pg_class.oid AND
                pg_attribute.attnum = any(pg_index.indkey) AND
                indisprimary;
        cSsg|]}|d�qS)rr)rT�rowrrrrUUs	z;PostgresqlMigrator._primary_key_columns.<locals>.<listcomp>)rr�fetchall)rZtbl�query�cursorrrr�_primary_key_columnsIs
z'PostgresqlMigrator._primary_key_columnsc
s�|j|�}tt|�}|j||dd�g}t|�dkr�d||df}d}|jj||f�}t|j��r�d||df}	|j	|j||	dd��|S)Nr!T�z	%s_%s_seqrz�
                SELECT 1
                FROM information_schema.sequences
                WHERE LOWER(sequence_name) = LOWER(%s)
            )
rm�superr/ra�lenrr�bool�fetchonerN)
rr\r]Zpk_namesZParentClassrPZseq_namerkrlZnew_seq_name)�	__class__rrraWszPostgresqlMigrator.rename_table)r$r%r&rmr-rarr)rsrr/Hsr/r=�
definitionr<�pkr4�extrac@sXeZdZedd��Zedd��Zedd��Zdddd	�ZdS)
�MySQLColumncCs
|jdkS)NZPRI)ru)rrrr�is_pkrszMySQLColumn.is_pkcCs
|jdkS)NZUNI)ru)rrrr�	is_uniquevszMySQLColumn.is_uniquecCs
|jdkS)NZYES)r<)rrrr�is_nullzszMySQLColumn.is_nullNcCs�|dkr|j}|dkr*|j}t|�t|j�g}|jra|jtd��|r}|jtd��n|jtd��|jr�|jtd��|jr�|jt|j��t	|�S)NZUNIQUEZNULLzNOT NULLzPRIMARY KEY)
rzr=rr7rtryrNrxrvr6)rr9rzrBrrrr~s 						zMySQLColumn.sql)r$r%r&�propertyrxryrzrrrrrrwqsrwZ_Columnc@s�eZdZdZdZedd��Zdd�Zedd��Zdd	�Z	ed
d��Z
dd
�Zedd��Zedd��Z
edd��Zedd��ZdS)r0TcCs+ttd�t|�td�t|��S)NzRENAME TABLEr[)r6r7r)rr\r]rrrra�s
			zMySQLMigrator.rename_tablecCsV|jjd|�}|j�}x-|D]%}t|�}|j|kr)|Sq)WdS)NzDESCRIBE %s;F)rrrjrwr=)rr8r9rlZrowsrirSrrr�_get_column_definition�s
z$MySQLMigrator._get_column_definitioncCswd|||f}ttd�t|�td�t|�td�tt|��td�t|�tt|���	S)Nzfk_%s_%s_refs_%szALTER TABLEzADD CONSTRAINTzFOREIGN KEYrD)r6r7rr)rr8r9ZrelZ
rel_columnZ
constraintrrrrJ�s							z(MySQLMigrator.add_foreign_key_constraintcCsK|jjd||f�}|j�}|sCtd||f��|dS)Nz�SELECT constraint_name FROM information_schema.key_column_usage WHERE table_schema = DATABASE() AND table_name = %s AND column_name = %s;z=Unable to find foreign key constraint for "%s" on table "%s".r)rrrr�AttributeError)rr8r9rlrrrr�get_foreign_key_constraint�s	z(MySQLMigrator.get_foreign_key_constraintcCs7ttd�t|�td�t|j||���S)NzALTER TABLEzDROP FOREIGN KEY)r6r7rr~)rr8r9rrrrR�s
			z)MySQLMigrator.drop_foreign_key_constraintcCsgS)Nr)rr:rrrrA�szMySQLMigrator.get_inline_fk_sqlcCsC|j||�}ttd�t|�td�|jdd��S)NzALTER TABLE�MODIFYrzF)r|r6r7rr)rr8rSrrrrL�s			zMySQLMigrator.add_not_nullcCsX|j||�}|jr'td��ttd�t|�td�|jdd��S)NzPrimary keys can not be nullzALTER TABLErrzT)r|rxrKr6r7rr)rr8rSrrrr`�s				zMySQLMigrator.drop_not_nullc	Cs�tdd�|jj|�D��}||k}|j||�}ttd�t|�td�t|�|jd|��}|r�||}|j||�||j	|||j
|j�gS|SdS)Ncss|]}|j|fVqdS)N)rS)rTZfkrrr�	<genexpr>�sz.MySQLMigrator.rename_column.<locals>.<genexpr>zALTER TABLEZCHANGEr9)�dictrrVr|r6r7rrrRrJZ
dest_tableZdest_column)	rr8r\r]Z
fk_objectsrOrSZ
rename_clauseZfk_metadatarrrr^�s*					

zMySQLMigrator.rename_columncCs+ttd�t|�td�t|��S)Nz
DROP INDEXrb)r6r7r)rr8rcrrrrgs
			zMySQLMigrator.drop_indexN)r$r%r&rMrWr-rar|rJr~rRrArLr`r^rgrrrrr0�s		r0c@s�eZdZdZejd�Zejd�Zejd�Zejdej	�Z
dd�Zdd	�Ze
d
d��Zdd
�Ze
ddd��Ze
dd��Ze
dd��Ze
dd��ZdS)r1z�
    SQLite supports a subset of ALTER TABLE queries, view the docs for the
    full details http://sqlite.org/lang_altertable.html
    z
(.+?)\((.+)\)z(?:[^,(]|\([^)]*\))+z
["`']?([\w]+)z FOREIGN KEY\s+\("?([\w]+)"?\)\s+cCs*|jjd|�}dd�|jD�S)Nzselect * from "%s" limit 1cSsg|]}|d�qS)rr)rTr rrrrUs	z4SqliteMigrator._get_column_names.<locals>.<listcomp>)rr�description)rr8�resrrr�_get_column_namessz SqliteMigrator._get_column_namescCs+|jjdd|j�g�}|j�S)NzBselect name, sql from sqlite_master where type=? and LOWER(name)=?r8)rr�lowerrr)rr8r�rrr�_get_create_tables	z SqliteMigrator._get_create_tablec	s>tdd��jj|�D��}|j�|krMtd||f���j|�\}}�jj|�}�jj|�tj	dd|�}�j
j|�j�\}}�j
j|�}	dd�|	D�}
g}g}g}
x�|
D]�}�jj|�j�\}||kr||||�}|r�|j|�|
j|��jj|�j�\}|j|�q�|j|�|j�jd�s�|j|�|
j|�q�Wtt|
|��}|j|��d
d�}�sdd�}n!�|kr"��fd
d�}g}xa|D]Y}�jj|�}|dk	ru|j�d|kru||�}|r/|j|�q/W|d}tjd|tj�}|j	d||�}dj|�}ttd�t|��td|j�|f�g}ttd�t|�tdd�|D��td�tdd�|
D��td�t|��}|j|�|jttd�t|���|j�j ||��x�t!dd�|�D]k}||j"kr�|jt|j#��q��r��j$|j#|��}|dk	r�|jt|��q�W|S)Ncss|]}|jj�VqdS)N)r=r�)rTrSrrrr� sz0SqliteMigrator._update_column.<locals>.<genexpr>z"Column "%s" does not exist on "%s"z\s+� cSsg|]}|j��qSr)�strip)rT�colrrrrU8s	z1SqliteMigrator._update_column.<locals>.<listcomp>�foreign�primarycSs|S)Nr)�
column_defrrr�<lambda>Ssz/SqliteMigrator._update_column.<locals>.<lambda>cSsdS)Nr)r�rrrr�Vscs�jjd�|�S)NzFOREIGN KEY ("%s") )�fk_re�sub)r�)�
new_columnrrrr�Ys	rZ__tmp__z
("?)%s("?)z\1%s\2z, zDROP TABLE IF EXISTSz%s (%s)zINSERT INTOcSsg|]}t|��qSr)r)rTr�rrrrUvs	ZSELECTcSsg|]}t|��qSr)r)rTr�rrrrUxs	ZFROMz
DROP TABLEcSs|jS)N)r)�idxrrrr��s)r�r�)%�setrZget_columnsr�rKr�Zget_indexesrV�rer��	column_re�search�groups�column_split_re�findall�column_name_re�matchrN�
startswithr��zip�getr��compile�I�joinr6r7rr�rrra�filterrdr�
_fix_index)rr8�column_to_updater)rdZcreate_tableZindexesZ
raw_createZraw_columnsZ
split_columnsZcolumn_defsZnew_column_defsZnew_column_namesZoriginal_column_namesr�r9Znew_column_defZoriginal_to_newZfk_filter_fnZcleaned_columnsr�Z
temp_tableZrgxZcreateZqueriesZpopulate_table�indexrr)r�rr�_update_columns�


	


"
					
		zSqliteMigrator._update_columnc
Cs(|j|�}t|�dkr1|j||�S|jdd�\}}t|j|��dkr~d||j||�fS|jdd�djd�}dd	�|D�}g}xK|D]C}	tjd
||	�r�t|	t|�d�}	|j|	�q�Wd|djd
d�|D��fS)N��(rnz%s(%s�)r�,cSsg|]}|jd��qS)z"`[]' )r�)rT�partrrrrU�s	z-SqliteMigrator._fix_index.<locals>.<listcomp>z%s(?:['"`\]]?\s|$)z%s(%s)z, css|]}d|VqdS)z"%s"Nr)rT�crrrr��sz,SqliteMigrator._fix_index.<locals>.<genexpr>)	�splitrp�replace�rsplitr�r�Znew_columnerNr�)
rrr�r�rBZlhsZrhsrdZcleanrSrrrr��s
zSqliteMigrator._fix_indexTcCs|j||dd��S)NcSsdS)Nr)�a�brrrr��sz,SqliteMigrator.drop_column.<locals>.<lambda>)r�)rr8r9rXrrrrZ�szSqliteMigrator.drop_columncs%�fdd�}|j|||�S)Ncs|j|��S)N)r�)r9r�)r]rr�_rename�sz-SqliteMigrator.rename_column.<locals>._rename)r�)rr8r\r]r�r)r]rr^�szSqliteMigrator.rename_columncCsdd�}|j|||�S)NcSs|dS)Nz	 NOT NULLr)r9r�rrr�
_add_not_null�sz2SqliteMigrator.add_not_null.<locals>._add_not_null)r�)rr8rSr�rrrrL�szSqliteMigrator.add_not_nullcCsdd�}|j|||�S)NcSs|jdd�S)NzNOT NULL�)r�)r9r�rrr�_drop_not_null�sz4SqliteMigrator.drop_not_null.<locals>._drop_not_null)r�)rr8rSr�rrrr`�szSqliteMigrator.drop_not_nullN)r$r%r&r'r�r�r�r�r�r�r�r�r�r-r�r�rZr^rLr`rrrrr1	sqr1cOsx|D]}|j�qWdS)N)r)rPrr-rrr�migrate�s
r�)r=rtr<rur4rv)r'�collectionsrr+r�Zpeeweerrrrrr	�objectr
r-r.r/Z_column_attributesrwr0r1r�rrrr�<module>es&
 	�'"v�

Filemanager

Name Type Size Permission Actions
__init__.cpython-35.opt-1.pyc File 95 B 0644
__init__.cpython-35.pyc File 95 B 0644
apsw_ext.cpython-35.opt-1.pyc File 7.47 KB 0644
apsw_ext.cpython-35.pyc File 7.47 KB 0644
berkeleydb.cpython-35.opt-1.pyc File 3.2 KB 0644
berkeleydb.cpython-35.pyc File 3.2 KB 0644
csv_loader.cpython-35.opt-1.pyc File 135 B 0644
csv_loader.cpython-35.pyc File 135 B 0644
csv_utils.cpython-35.opt-1.pyc File 11.43 KB 0644
csv_utils.cpython-35.pyc File 11.43 KB 0644
dataset.cpython-35.opt-1.pyc File 13.38 KB 0644
dataset.cpython-35.pyc File 13.38 KB 0644
db_url.cpython-35.opt-1.pyc File 3.16 KB 0644
db_url.cpython-35.pyc File 3.16 KB 0644
djpeewee.cpython-35.opt-1.pyc File 5.76 KB 0644
djpeewee.cpython-35.pyc File 5.76 KB 0644
fields.cpython-35.opt-1.pyc File 12.7 KB 0644
fields.cpython-35.pyc File 12.7 KB 0644
flask_utils.cpython-35.opt-1.pyc File 5.93 KB 0644
flask_utils.cpython-35.pyc File 5.97 KB 0644
gfk.cpython-35.opt-1.pyc File 6.02 KB 0644
gfk.cpython-35.pyc File 6.02 KB 0644
hybrid.cpython-35.opt-1.pyc File 2.15 KB 0644
hybrid.cpython-35.pyc File 2.15 KB 0644
kv.cpython-35.opt-1.pyc File 6.77 KB 0644
kv.cpython-35.pyc File 6.77 KB 0644
migrate.cpython-35.opt-1.pyc File 22.68 KB 0644
migrate.cpython-35.pyc File 22.68 KB 0644
pool.cpython-35.opt-1.pyc File 7.96 KB 0644
pool.cpython-35.pyc File 7.96 KB 0644
postgres_ext.cpython-35.opt-1.pyc File 16.86 KB 0644
postgres_ext.cpython-35.pyc File 16.86 KB 0644
read_slave.cpython-35.opt-1.pyc File 1.75 KB 0644
read_slave.cpython-35.pyc File 1.75 KB 0644
reflection.cpython-35.opt-1.pyc File 18.58 KB 0644
reflection.cpython-35.pyc File 18.58 KB 0644
shortcuts.cpython-35.opt-1.pyc File 5.65 KB 0644
shortcuts.cpython-35.pyc File 5.65 KB 0644
signals.cpython-35.opt-1.pyc File 3.04 KB 0644
signals.cpython-35.pyc File 3.04 KB 0644
sqlcipher_ext.cpython-35.opt-1.pyc File 4.52 KB 0644
sqlcipher_ext.cpython-35.pyc File 4.52 KB 0644
sqlite_ext.cpython-35.opt-1.pyc File 36.56 KB 0644
sqlite_ext.cpython-35.pyc File 36.65 KB 0644
sqlite_udf.cpython-35.opt-1.pyc File 16.76 KB 0644
sqlite_udf.cpython-35.pyc File 16.76 KB 0644
test_utils.cpython-35.opt-1.pyc File 4.03 KB 0644
test_utils.cpython-35.pyc File 4.08 KB 0644