403Webshell
Server IP : 192.158.238.246  /  Your IP : 13.58.76.154
Web Server : LiteSpeed
System : Linux uniform.iwebfusion.net 4.18.0-553.27.1.lve.1.el8.x86_64 #1 SMP Wed Nov 20 15:58:00 UTC 2024 x86_64
User : jenniferflocom ( 1321)
PHP Version : 8.1.32
Disable Function : NONE
MySQL : OFF  |  cURL : ON  |  WGET : ON  |  Perl : ON  |  Python : ON  |  Sudo : OFF  |  Pkexec : OFF
Directory :  /opt/alt/python38/lib64/python3.8/site-packages/playhouse/__pycache__/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Command :


[ Back ]     

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�D/opt/alt/python38/lib64/python3.8/site-packages/playhouse/migrate.py�__init__uszOperation.__init__cCs|jj��}|�|�Sr)r
�database�compiler�
parse_node)r�noderrrr�_parse_node{szOperation._parse_nodecCs"|�|�\}}|jj�||�dSr)rr
r�execute_sql)rr�sql�paramsrrr�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�sr2c@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)r�PostgresqlDatabase�PostgresqlMigrator�
MySQLDatabase�
MySQLMigrator�SqliteMigrator)�clsrrrr�
from_database�s


zSchemaMigrator.from_databasecCsJ|j}t|�r|�}ttd�t|�td�tt|�tjt|�	|��dd��S)N�UPDATE�SETT)�flat)
�default�callable�Clause�SQLrrr
�EQ�Param�db_value)r�table�column_name�fieldr>rrr�
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_columnrr�field_definitionrArr�ForeignKeyField�extend�get_inline_fk_sqlr@)rrErFrGZ
field_nullZfield_clause�partsrrr�alter_add_column�s�
zSchemaMigrator.alter_add_columncCs$td�t|jjj�tt|jj��gS)N�
REFERENCES)rAr�	rel_model�_meta�db_tabler�to_fieldrL�rrGrrrrP�s�z SchemaMigrator.get_inline_fk_sqlcCst�dSr��NotImplementedError)rrErFrGrrr�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`.)rJr>�
ValueErrorrrNrWrRrOrH�add_not_null�explicit_create_foreign_key�appendr[rTrUrVrL)rrErFrG�is_foreign_key�
operationsrrr�
add_column�s*


�
��zSchemaMigrator.add_columncCst�dSrrY�rrErFrrr�drop_foreign_key_constraint�sz*SchemaMigrator.drop_foreign_key_constraintTcCsrtd�t|�td�t|�g}|r.|�td��t|�}dd�|j�|�D�}||krj|jrj|�||�|gS|SdS)NrIzDROP COLUMN�CASCADEcSsg|]
}|j�qSr��column)�.0�foreign_keyrrr�
<listcomp>s�z.SchemaMigrator.drop_column.<locals>.<listcomp>)rArr_r@r�get_foreign_keys�explicit_delete_foreign_keyrd)rrErF�cascade�nodesZdrop_column_nodeZ
fk_columnsrrr�drop_column�s �
�
�zSchemaMigrator.drop_columncCs*ttd�t|�td�t|�td�t|��S)NrIz
RENAME COLUMN�TO�r@rAr)rrE�old_name�new_namerrr�
rename_columns�zSchemaMigrator.rename_columncCstd�t|�td�t|�gS)NrIzALTER COLUMN)rAr�rrErgrrr�
_alter_columns
�zSchemaMigrator._alter_columncCs"|�||�}|�td��t|�S)NzSET NOT NULL�rvr_rAr@�rrErgrnrrrr]"szSchemaMigrator.add_not_nullcCs"|�||�}|�td��t|�S)Nz
DROP NOT NULLrwrxrrr�
drop_not_null(szSchemaMigrator.drop_not_nullcCsttd�t|�td�t|��S)NrIz	RENAME TOrq�rrrrsrrr�rename_table.s�zSchemaMigrator.rename_tablecCsL|j��}|rdnd}tt|�t|�||��td�t|�tdd�|D���S)NzCREATE UNIQUE INDEXzCREATE INDEX�ONcSsg|]}t|��qSrr�rhrgrrrrj?sz,SchemaMigrator.add_index.<locals>.<listcomp>)rrr@rAr�
index_namer)rrE�columns�uniquer�	statementrrr�	add_index6s
�zSchemaMigrator.add_indexcCsttd�t|��S)N�
DROP INDEXrq�rrEr~rrr�
drop_indexAs�zSchemaMigrator.drop_indexN)T)F)r(r)r*r^rlr�classmethodr:r2rHrRrPr[rbrdrortrvr]ryr{r�r�rrrrr3�s>




!

	



r3cs(eZdZdd�Ze�fdd��Z�ZS)r5cCs&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)rh�rowrrrrjUsz;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��superr5r{�lenrr�bool�fetchoner_)
rrrrsZpk_namesZParentClassraZseq_namer�r�Znew_seq_name��	__class__rrr{Ws 

��zPostgresqlMigrator.rename_table)r(r)r*r�r2r{�
__classcell__rrr�rr5Hsr5)rK�
definitionrJ�pkr>�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)N�YES)rJr�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)NZUNIQUE�NULL�NOT NULLzPRIMARY KEY)
r�rKrrAr�r�r_r�r�r@)rrFr�rQrrrr~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)r7TcCsttd�t|�td�t|��S)NzRENAME TABLErprqrzrrrr{�s�zMySQLMigrator.rename_tablecCs@|j�d|�}|��}|D]}t|�}|j|kr|SqdS)NzDESCRIBE %s;F)rrr�r�rK)rrErFr��rowsr�rgrrr�_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_%srIzADD CONSTRAINTzFOREIGN KEYrS)r@rArr)rrErF�relZ
rel_column�
constraintrrrr[�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)rrErFr�r#rrr�get_foreign_key_constraint�s���z(MySQLMigrator.get_foreign_key_constraintc	Cs&ttd�t|�td�t|�||���S)NrIzDROP FOREIGN KEY)r@rArr�rcrrrrd�s�z)MySQLMigrator.drop_foreign_key_constraintcCsgSrrrXrrrrP�szMySQLMigrator.get_inline_fk_sqlcCs.|�||�}ttd�t|�td�|jdd��S)NrI�MODIFYF�r�)r�r@rArrrurrrr]�s
�zMySQLMigrator.add_not_nullcCs<|�||�}|jrtd��ttd�t|�td�|jdd��S)NzPrimary keys can not be nullrIr�Tr�)r�r�r\r@rArrrurrrry�s
�zMySQLMigrator.drop_not_nullc	Cs�tdd�|j�|�D��}||k}|�||�}ttd�t|�td�t|�|j|d��}|r�||}|�||�||�	|||j
|j�gS|SdS)Ncss|]}|j|fVqdSrrf)rh�fkrrr�	<genexpr>�s�z.MySQLMigrator.rename_column.<locals>.<genexpr>rIZCHANGE)rF)�dictrrkr�r@rArrrdr[�
dest_table�dest_column)	rrErrrsZ
fk_objectsr`rgZ
rename_clauseZfk_metadatarrrrt�s0
�
�
��
zMySQLMigrator.rename_columncCsttd�t|�td�t|��S)Nr�r|rqr�rrrr�s�zMySQLMigrator.drop_indexN)r(r)r*r^rlr2r{r�r[r�rdrPr]ryrtr�rrrrr7�s&
	





r7c@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)r8z�
    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�qSr�r)rhr$rrrrjsz4SqliteMigrator._get_column_names.<locals>.<listcomp>)rr�description�rrE�resrrr�_get_column_namessz SqliteMigrator._get_column_namescCs|j�dd|��g�}|��S)NzBselect name, sql from sqlite_master where type=? and LOWER(name)=?rE)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)rKr�r}rrrr� s�z0SqliteMigrator._update_column.<locals>.<genexpr>z"Column "%s" does not exist on "%s"z\s+� cSsg|]}|���qSr��strip�rh�colrrrrj8sz1SqliteMigrator._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�rrrrjvs�SELECTcSsg|]}t|��qSrrr�rrrrjxs�FROMz
DROP TABLEcSs|jSr)r)�idxrrrr��r�)%�setr�get_columnsr�r\r��get_indexesrk�rer��	column_re�search�groups�column_split_re�findall�column_name_re�matchr_�
startswithr��zip�getr��compile�I�joinr@rArr�rrr{�filterrr�
_fix_index)rrE�column_to_updater.r�create_table�indexesZ
raw_createZraw_columnsZ
split_columnsZcolumn_defsZnew_column_defsZnew_column_namesZoriginal_column_namesr�rFZnew_column_defZoriginal_to_newZfk_filter_fnZcleaned_columnsr�Z
temp_tableZrgx�create�queriesZpopulate_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��(r�z%s(%s�)r�,cSsg|]}|�d��qS)z"`[]' r�)rh�partrrrrj�sz-SqliteMigrator._fix_index.<locals>.<listcomp>z%s(?:['"`\]]?\s|$)z%s(%s)r�css|]}d|VqdS)z"%s"Nr)rh�crrrr��sz,SqliteMigrator._fix_index.<locals>.<genexpr>)	�splitr��replace�rsplitr�r�Znew_columner_r�)
rrr�r�rQ�lhs�rhsr�cleanrgrrrr��s
zSqliteMigrator._fix_indexTcCs|�||dd��S)NcSsdSrr)�a�brrrr��r�z,SqliteMigrator.drop_column.<locals>.<lambda>�r�)rrErFrmrrrro�szSqliteMigrator.drop_columncs�fdd�}|�|||�S)Ncs|�|��Sr�r��rFr��rsrr�_rename�sz-SqliteMigrator.rename_column.<locals>._renamer�)rrErrrsr�rr�rrt�szSqliteMigrator.rename_columncCsdd�}|�|||�S)NcSs|dS)Nz	 NOT NULLrr�rrr�
_add_not_null�sz2SqliteMigrator.add_not_null.<locals>._add_not_nullr�)rrErgr�rrrr]�szSqliteMigrator.add_not_nullcCsdd�}|�|||�S)NcSs|�dd�S)Nr��r�r�rrr�_drop_not_null�sz4SqliteMigrator.drop_not_null.<locals>._drop_not_nullr�)rrErgr�rrrry�szSqliteMigrator.drop_not_nullN)T)r(r)r*r+r�r�r�r�r�r�r�r�r�r2r�r�rortr]ryrrrrr8	s$



p

r8cOs|D]}|��qdSr)r)rarr2rrr�migrate�sr�)r+�collectionsrr0r��peeweerrrrr	r
�objectrr2r3r5Z_column_attributesr�r7r8r�rrrr�<module>s*e 	-'"v=

Youez - 2016 - github.com/yon3zu
LinuXploit