ok

Mini Shell

Direktori : /opt/alt/python38/lib64/python3.8/site-packages/playhouse/__pycache__/
Upload File :
Current File : //opt/alt/python38/lib64/python3.8/site-packages/playhouse/__pycache__/migrate.cpython-38.pyc

U

S��W�]�@s�dZddlmZddlZddlZddlTddlmZddlmZddlmZddlm	Z	dd	lm
Z
dd
lmZGdd�de�Z
d
d�ZGdd�de�ZGdd�de�ZdZGdd�dede��ZGdd�de�ZGdd�de�Zdd�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@s8eZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�ZdS)
�	Operationz/Encapsulate a single schema altering operation.cOs||_||_||_||_dS�N)�migrator�method�args�kwargs)�selfr
rrr�r�/migrate.py�__init__uszOperation.__init__cCs|jj��}|�|�Sr)r
�database�compilerZ
parse_node)r�noderrrr�_parse_node{szOperation._parse_nodecCs"|�|�\}}|jj�||�dSr)rr
r�execute_sql)rr�sqlZparamsrrr�executeszOperation.executecCsPt|t�r|�|�n6t|t�r*|��n"t|ttf�rL|D]}|�|�q<dSr)�
isinstancer	rr�run�list�tuple�_handle_result)r�result�itemrrrr �s


zOperation._handle_resultcCs2|j��}d|d<|�t|j|j�|j|��dS)NT�generate)r�copyr �getattrr
rr)rrrrrr�s

�z
Operation.runN)	�__name__�
__module__�__qualname__�__doc__rrrr rrrrrrss	rcst����fdd��}|S)Ncs4|�dd�}|r �|f|�|�St|�jf|�|�S)Nr#F)�poprr&)rrrr#��fnrr�inner�szoperation.<locals>.inner)�	functools�wraps)r,r-rr+r�	operation�sr0c@s�eZdZdZdZdd�Zedd��Zedd��Z	edd	��Z
d
d�Zedd
��Zedd��Z
edd��Zed$dd��Zedd��Zdd�Zedd��Zedd��Zedd��Zed%dd ��Zed!d"��Zd#S)&�SchemaMigratorFcCs
||_dSr)r)rrrrrr�szSchemaMigrator.__init__cCs0t|t�rt|�St|t�r$t|�St|�SdSr)rZPostgresqlDatabase�PostgresqlMigratorZ
MySQLDatabase�
MySQLMigrator�SqliteMigrator)�clsrrrr�
from_database�s


zSchemaMigrator.from_databasecCsJ|j}t|�r|�}ttd�t|�td�tt|�tjt|�	|��dd��S)NZUPDATEZSETT)Zflat)
�default�callable�Clause�SQLrrr
ZEQZParamZdb_value)r�table�column_name�fieldr7rrr�
apply_default�s��zSchemaMigrator.apply_defaultcCsj|jd}|_||_|_|j���|�}||_td�t|�td�|g}t|t	�rb|�
|�|��t|�S)NT�ALTER TABLEz
ADD COLUMN)
�null�name�	db_columnrrZfield_definitionr:rr�ForeignKeyField�extend�get_inline_fk_sqlr9)rr;r<r=Z
field_nullZfield_clause�partsrrr�alter_add_column�s�
zSchemaMigrator.alter_add_columncCs$td�t|jjj�tt|jj��gS)N�
REFERENCES)r:r�	rel_model�_meta�db_tabler�to_fieldrB�rr=rrrrE�s�z SchemaMigrator.get_inline_fk_sqlcCst�dSr��NotImplementedError)rr;r<r=rrr�add_foreign_key_constraint�sz)SchemaMigrator.add_foreign_key_constraintcCs�|js|jdkrtd|��t|t�}|r8|js8td��|�|||�g}|jsn|�|�|||�|�	||�g�|r�|j
r�|�|�|||j
jj|jj��|S)Nz!%s is not null but has no defaultz'Foreign keys must specify a `to_field`.)r@r7�
ValueErrorrrCrLrGrDr>�add_not_null�explicit_create_foreign_key�appendrPrIrJrKrB)rr;r<r=�is_foreign_key�
operationsrrr�
add_column�s*


�
��zSchemaMigrator.add_columncCst�dSrrN�rr;r<rrr�drop_foreign_key_constraint�sz*SchemaMigrator.drop_foreign_key_constraintTcCsrtd�t|�td�t|�g}|r.|�td��t|�}dd�|j�|�D�}||krj|jrj|�||�|gS|SdS)Nr?zDROP COLUMNZCASCADEcSsg|]
}|j�qSr��column)�.0Zforeign_keyrrr�
<listcomp>s�z.SchemaMigrator.drop_column.<locals>.<listcomp>)r:rrTr9r�get_foreign_keys�explicit_delete_foreign_keyrY)rr;r<�cascade�nodesZdrop_column_nodeZ
fk_columnsrrr�drop_column�s �
�
�zSchemaMigrator.drop_columncCs*ttd�t|�td�t|�td�t|��S)Nr?z
RENAME COLUMN�TO�r9r:r)rr;�old_name�new_namerrr�
rename_columns�zSchemaMigrator.rename_columncCstd�t|�td�t|�gS)Nr?zALTER COLUMN)r:r�rr;r[rrr�
_alter_columns
�zSchemaMigrator._alter_columncCs"|�||�}|�td��t|�S)NzSET NOT NULL�rirTr:r9�rr;r[rarrrrR"szSchemaMigrator.add_not_nullcCs"|�||�}|�td��t|�S)Nz
DROP NOT NULLrjrkrrr�
drop_not_null(szSchemaMigrator.drop_not_nullcCsttd�t|�td�t|��S)Nr?z	RENAME TOrd�rrerfrrr�rename_table.s�zSchemaMigrator.rename_tablecCsL|j��}|rdnd}tt|�t|�||��td�t|�tdd�|D���S)NzCREATE UNIQUE INDEXzCREATE INDEX�ONcSsg|]}t|��qSrr�r\r[rrrr]?sz,SchemaMigrator.add_index.<locals>.<listcomp>)rrr9r:r�
index_namer)rr;�columns�uniquerZ	statementrrr�	add_index6s
�zSchemaMigrator.add_indexcCsttd�t|��S)N�
DROP INDEXrd�rr;rqrrr�
drop_indexAs�zSchemaMigrator.drop_indexN)T)F)r&r'r(rSr_r�classmethodr6r0r>rGrErPrWrYrbrgrirRrlrnrtrwrrrrr1�s>




!

	



r1cs(eZdZdd�Ze�fdd��Z�ZS)r2cCs&d}|j�||�}dd�|��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)r\�rowrrrr]Usz;PostgresqlMigrator._primary_key_columns.<locals>.<listcomp>)rr�fetchall)rZtbl�query�cursorrrr�_primary_key_columnsIs
z'PostgresqlMigrator._primary_key_columnsc
s�|�|�}tt|�}|j||dd�g}t|�dkr�d||df}d}|j�||f�}t|���r�d||df}	|�	|j||	dd��|S)NT)r#�z	%s_%s_seqrz�
                SELECT 1
                FROM information_schema.sequences
                WHERE LOWER(sequence_name) = LOWER(%s)
            )
r~�superr2rn�lenrr�bool�fetchonerT)
rrerfZpk_namesZParentClassrVZseq_namer|r}Znew_seq_name��	__class__rrrnWs 

��zPostgresqlMigrator.rename_table)r&r'r(r~r0rn�
__classcell__rrr�rr2Hsr2)rA�
definitionr@�pkr7�extrac@s:eZdZedd��Zedd��Zedd��Zd
dd	�ZdS)�MySQLColumncCs
|jdkS)NZPRI�r��rrrr�is_pkrszMySQLColumn.is_pkcCs
|jdkS)NZUNIr�r�rrr�	is_uniquevszMySQLColumn.is_uniquecCs
|jdkS)NZYES)r@r�rrr�is_nullzszMySQLColumn.is_nullNcCs�|dkr|j}|dkr|j}t|�t|j�g}|jrB|�td��|rV|�td��n|�td��|jrx|�td��|jr�|�t|j��t	|�S)NZUNIQUEZNULL�NOT NULLzPRIMARY KEY)
r�rArr:r�r�rTr�r�r9)rr<r�rFrrrr~s"�zMySQLColumn.sql)NN)r&r'r(�propertyr�r�r�rrrrrr�qs


r�Z_Columnc@s�eZdZdZdZedd��Zdd�Zedd��Zdd	�Z	ed
d��Z
dd
�Zedd��Zedd��Z
edd��Zedd��ZdS)r3TcCsttd�t|�td�t|��S)NzRENAME TABLErcrdrmrrrrn�s�zMySQLMigrator.rename_tablecCs@|j�d|�}|��}|D]}t|�}|j|kr|SqdS)NzDESCRIBE %s;F)rrr{r�rA)rr;r<r}Zrowsrzr[rrr�_get_column_definition�s

z$MySQLMigrator._get_column_definitioncCsRd|||f}ttd�t|�td�t|�td�tt|��td�t|�tt|���	S)Nzfk_%s_%s_refs_%sr?zADD CONSTRAINTzFOREIGN KEYrH)r9r:rr)rr;r<ZrelZ
rel_columnZ
constraintrrrrP�s

�z(MySQLMigrator.add_foreign_key_constraintcCs6|j�d||f�}|��}|s.td||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)rrr��AttributeError)rr;r<r}r!rrr�get_foreign_key_constraint�s���z(MySQLMigrator.get_foreign_key_constraintc	Cs&ttd�t|�td�t|�||���S)Nr?zDROP FOREIGN KEY)r9r:rr�rXrrrrY�s�z)MySQLMigrator.drop_foreign_key_constraintcCsgSrrrMrrrrE�szMySQLMigrator.get_inline_fk_sqlcCs.|�||�}ttd�t|�td�|jdd��S)Nr?�MODIFYF�r�)r�r9r:rrrhrrrrR�s
�zMySQLMigrator.add_not_nullcCs<|�||�}|jrtd��ttd�t|�td�|jdd��S)NzPrimary keys can not be nullr?r�Tr�)r�r�rQr9r:rrrhrrrrl�s
�zMySQLMigrator.drop_not_nullc	Cs�tdd�|j�|�D��}||k}|�||�}ttd�t|�td�t|�|j|d��}|r�||}|�||�||�	|||j
|j�gS|SdS)Ncss|]}|j|fVqdSrrZ)r\Zfkrrr�	<genexpr>�s�z.MySQLMigrator.rename_column.<locals>.<genexpr>r?ZCHANGE)r<)�dictrr^r�r9r:rrrYrPZ
dest_tableZdest_column)	rr;rerfZ
fk_objectsrUr[Z
rename_clauseZfk_metadatarrrrg�s0
�
�
��
zMySQLMigrator.rename_columncCsttd�t|�td�t|��S)Nrurordrvrrrrws�zMySQLMigrator.drop_indexN)r&r'r(rSr_r0rnr�rPr�rYrErRrlrgrwrrrrr3�s&
	





r3c@s�eZdZdZe�d�Ze�d�Ze�d�Ze�dej	�Z
dd�Zdd	�Ze
d
d��Zdd
�Ze
ddd��Ze
dd��Ze
dd��Ze
dd��ZdS)r4z�
    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 |j�d|�}dd�|jD�S)Nzselect * from "%s" limit 1cSsg|]}|d�qSryr)r\r"rrrr]sz4SqliteMigrator._get_column_names.<locals>.<listcomp>)rr�description�rr;�resrrr�_get_column_namessz SqliteMigrator._get_column_namescCs|j�dd|��g�}|��S)NzBselect name, sql from sqlite_master where type=? and LOWER(name)=?r;)rr�lowerr�r�rrr�_get_create_tables

�z SqliteMigrator._get_create_tablec	stdd��j�|�D��}|��|kr6td||f����|�\}}�j�|�}�j�|�t�	dd|�}�j
�|���\}}�j
�|�}	dd�|	D�}
g}g}g}
|
D]�}�j�|���\}||k�r
|||�}|�r6|�|�|
�|��j�|���\}|�|�q�|�|�|���d�s�|�|�|
�|�q�tt|
|��}|�|��d	d
�}��shdd
�}n�|k�r���fdd
�}g}|D]F}�j�|�}|dk	�r�|��d
|k�r�||�}|�r�|�|��q�|d}t�d|tj�}|�	d||�}d�|�}ttd�t|��td|��|f�g}ttd�t|�tdd�|D��td�tdd�|
D��td�t|��}|�|�|�ttd�t|���|��� ||��t!dd
�|�D]R}||j"k�r�|�t|j#��n.��r���$|j#|��}|dk	�r�|�t|���q�|S)Ncss|]}|j��VqdSr)rAr�rprrrr� s�z0SqliteMigrator._update_column.<locals>.<genexpr>z"Column "%s" does not exist on "%s"z\s+� cSsg|]}|���qSr��strip�r\�colrrrr]8sz1SqliteMigrator._update_column.<locals>.<listcomp>)ZforeignZprimarycSs|Srr��
column_defrrr�<lambda>S�z/SqliteMigrator._update_column.<locals>.<lambda>cSsdSrrr�rrrr�Vr�cs�j�d�|�S)NzFOREIGN KEY ("%s") )�fk_re�subr���
new_columnrrrr�Ys�rZ__tmp__z
("?)%s("?)z\1%s\2�, zDROP TABLE IF EXISTSz%s (%s)zINSERT INTOcSsg|]}t|��qSrrr�rrrr]vsZSELECTcSsg|]}t|��qSrrr�rrrr]xsZFROMz
DROP TABLEcSs|jSr)r)�idxrrrr��r�)%�setrZget_columnsr�rQr�Zget_indexesr^�rer��	column_re�search�groups�column_split_re�findall�column_name_re�matchrT�
startswithr��zip�getr��compile�I�joinr9r:rr�rrrn�filterrrr�
_fix_index)rr;�column_to_updater,rrZcreate_tableZindexesZ
raw_createZraw_columnsZ
split_columnsZcolumn_defsZnew_column_defsZnew_column_namesZoriginal_column_namesr�r<Znew_column_defZoriginal_to_newZfk_filter_fnZcleaned_columnsr�Z
temp_tableZrgxZcreateZqueriesZpopulate_table�indexrrr�r�_update_columns�
��



�





�
��
�
zSqliteMigrator._update_columnc
Cs�|�|�}t|�dkr"|�||�S|�dd�\}}t|�|��dkrXd||�||�fS|�dd�d�d�}dd	�|D�}g}|D]2}	t�d
||	�r�t|	t|�d�}	|�|	�q�d|d�d
d�|D��fS)N��(rz%s(%s�)r�,cSsg|]}|�d��qS)z"`[]' r�)r\�partrrrr]�sz-SqliteMigrator._fix_index.<locals>.<listcomp>z%s(?:['"`\]]?\s|$)z%s(%s)r�css|]}d|VqdS)z"%s"Nr)r\�crrrr��sz,SqliteMigrator._fix_index.<locals>.<genexpr>)	�splitr��replace�rsplitr�r�Znew_columnerTr�)
rrr�r�rFZlhsZrhsrrZcleanr[rrrr��s
zSqliteMigrator._fix_indexTcCs|�||dd��S)NcSsdSrr)�a�brrrr��r�z,SqliteMigrator.drop_column.<locals>.<lambda>�r�)rr;r<r`rrrrb�szSqliteMigrator.drop_columncs�fdd�}|�|||�S)Ncs|�|��Sr�r��r<r��rfrr�_rename�sz-SqliteMigrator.rename_column.<locals>._renamer�)rr;rerfr�rr�rrg�szSqliteMigrator.rename_columncCsdd�}|�|||�S)NcSs|dS)Nz	 NOT NULLrr�rrr�
_add_not_null�sz2SqliteMigrator.add_not_null.<locals>._add_not_nullr�)rr;r[r�rrrrR�szSqliteMigrator.add_not_nullcCsdd�}|�|||�S)NcSs|�dd�S)Nr��r�r�rrr�_drop_not_null�sz4SqliteMigrator.drop_not_null.<locals>._drop_not_nullr�)rr;r[r�rrrrl�szSqliteMigrator.drop_not_nullN)T)r&r'r(r)r�r�r�r�r�r�r�r�r�r0r�r�rbrgrRrlrrrrr4	s$



p

r4cOs|D]}|��qdSr)r)rVrr0rrr�migrate�sr�)r)�collectionsrr.r�Zpeeweerrrrr	r
�objectrr0r1r2Z_column_attributesr�r3r4r�rrrr�<module>s*e 	-'"v=

Zerion Mini Shell 1.0