AlkantarClanX12

Your IP : 18.223.206.84


Current Path : /opt/imunify360/venv/lib64/python3.11/site-packages/playhouse/__pycache__/
Upload File :
Current File : //opt/imunify360/venv/lib64/python3.11/site-packages/playhouse/__pycache__/migrate.cpython-311.pyc

�

m��fpx����dZddlmZddlZddlZddlZddlTddlmZddlmZddlm	Z	ddlm
Z
dd	lmZdd
lmZddlm
Z
ddlmZdd
lmZddlmZ	ddlmZn
#e$rdZYnwxYwGd�de��Zd�Zd�ZGd�de��ZGd�de��ZGd�de��ZGd�dedd����ZGd�de��ZGd �d!e��Zd"�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'))

Adding or dropping table constraints:

.. code-block:: python

    # Add a CHECK() constraint to enforce the price cannot be negative.
    migrate(migrator.add_constraint(
        'products',
        'price_check',
        Check('price >= 0')))

    # Remove the price check constraint.
    migrate(migrator.drop_constraint('products', 'price_check'))

    # Add a UNIQUE constraint on the first and last names.
    migrate(migrator.add_unique('person', 'first_name', 'last_name'))
�)�
namedtupleN)�*)�
CommaNodeList)�EnclosedNodeList��Entity)�
Expression)�Node)�NodeList)�OP)�	callable_)�sort_models)�_truncate_constraint_name)�CockroachDatabasec�*�eZdZdZd�Zd�Zd�Zd�ZdS)�	Operationz/Encapsulate a single schema altering operation.c�>�||_||_||_||_dS�N)�migrator�method�args�kwargs)�selfrrrrs     �H/opt/imunify360/venv/lib64/python3.11/site-packages/playhouse/migrate.py�__init__zOperation.__init__�s"�� ��
������	������c�D�|jj�|��dSr)r�database�execute)r�nodes  rrzOperation.execute�s!���
��&�&�t�,�,�,�,�,rc�2�t|ttf��r|�|��dSt|t��r|���dSt|ttf��r|D]}|�|���dSdSr)	�
isinstancer
�Contextrr�run�list�tuple�_handle_result)r�result�items   rr'zOperation._handle_result�s����f�t�W�o�.�.�	*��L�L�� � � � � �
��	�
*�
*�	*��J�J�L�L�L�L�L�
���u�
�
.�
.�	*��
*�
*���#�#�D�)�)�)�)�	*�	*�
*�
*rc��|j���}d|d<t|j|j��}|�||ji|����dS)NT�with_context)r�copy�getattrrrr'r)rrrs   rr$z
Operation.run�s]����!�!�#�#��!%��~�������4�4�����F�F�D�I�8��8�8�9�9�9�9�9rN)�__name__�
__module__�__qualname__�__doc__rrr'r$�rrrr�sV������9�9����-�-�-�*�*�*�:�:�:�:�:rrc�F��tj����fd���}|S)Nc�x��|�dd��}|r�|g|�Ri|��St|�jg|�Ri|��S)Nr+F)�poprr.)rrrr+�fns    �r�innerzoperation.<locals>.inner�s`����z�z�.�%�8�8���	-��2�d�,�T�,�,�,�V�,�,�,���r�{�<�T�<�<�<�V�<�<�<r)�	functools�wraps)r6r7s` r�	operationr:�s8����_�R���=�=�=�=���=�
�Lrc��d�|ft|��z��}t|��dkrPtj|�d�������}|dd��d|dd���}|S)N�_�@zutf-8�8�)�joinr&�len�hashlib�md5�encode�	hexdigest)�
table_name�columns�
index_name�
index_hashs    r�make_index_namerJ�s������:�-�%��.�.�8�9�9�J�
�:�������[��!2�!2�7�!;�!;�<�<�F�F�H�H�
� *�3�B�3�����B�Q�B���@�
��rc��eZdZdZdZd�Zd�Zed���Ze	d���Z
d�Zd�Ze	d���Z
e	d	���Ze	d
���Ze	d���Zd�Ze		dd���Ze	d���Ze	d���Ze	dd���Ze	d���Ze	d���Ze	d���Ze	dd���Ze	d���Ze	dd���Ze	d���Zd
S)�SchemaMigratorFc��||_dSr)r)rrs  rrzSchemaMigrator.__init__�s
�� ��
�
�
rc�4�|j���Sr)r�get_sql_context�rs r�make_contextzSchemaMigrator.make_context�s���}�,�,�.�.�.rc�T�tr$t|t��rt|��St|t��rt	|��St|t
��rt
|��St|t��rt|��Std|z���)NzUnsupported database: %s)
rr"�CockroachDBMigrator�PostgresqlDatabase�PostgresqlMigrator�
MySQLDatabase�
MySQLMigrator�SqliteDatabase�SqliteMigrator�
ValueError)�clsrs  r�
from_databasezSchemaMigrator.from_database�s����	,��H�6G�!H�!H�	,�&�x�0�0�0�
��"4�
5�
5�	,�%�h�/�/�/�
��-�
0�
0�	,� ��*�*�*�
��.�
1�
1�	,�!�(�+�+�+��3�h�>�?�?�?rc	��|j}t|��r
|��}|����d���t|�����d���t
t|��tj|�	|��d�����S)NzUPDATE z SET T)�flat)
�defaultr
rQ�literal�sqlrr	r�EQ�db_value)r�table�column_name�fieldr_s     r�
apply_defaultzSchemaMigrator.apply_default�s����-���W���	 ��g�i�i�G��!�!�#�#����#�#���V�E�]�]�#�#����!�!���Z��;�'�'��E��N�N�7�+�+��	��� � �		!rc�l�|�d���t|����S)N�ALTER TABLE )r`rar)r�ctxrds   r�_alter_tablezSchemaMigrator._alter_table�s(���{�{�>�*�*�.�.�v�e�}�}�=�=�=rc��|�||���d���t|����S)Nz ALTER COLUMN �rkr`rar�rrjrd�columns    r�
_alter_columnzSchemaMigrator._alter_column�s:�����c�5�)�)���)�*�*���V�F�^�^�$�$�	&rc��|���}|jdc}|_|j|kr|x|_|_|�||���d���|�|����||_t|t��r|�
||��|S)NTz ADD COLUMN )rQ�nullre�namerkr`ra�ddlr"�ForeignKeyField�add_inline_fk_sql)rrdrerfrj�
field_nulls      r�alter_add_columnzSchemaMigrator.alter_add_column�s������!�!��!&��T��
�E�J����+�+�-8�8�E�J��*�	
�
�,�s�E�
"�
"�
�'�.�
!�
!�

�#�e�i�i��n�n�
�
�
���
��e�_�-�-�	/��"�"�3��.�.�.��
rc��|�|���|���d���t	|�����d���|��S)N� ADD CONSTRAINT � �rkrQr`rar�rrdrs�
constraints    r�add_constraintzSchemaMigrator.add_constraint�s\�����d�/�/�1�1�5�9�9���+�,�,���V�D�\�\�"�"��������Z���	"rc���dd�|��z}ttd��td�|D����f��}|�|||��S)Nzuniq_%sr<�UNIQUEc�,�g|]}t|����Sr2r��.0ros  r�
<listcomp>z-SchemaMigrator.add_unique.<locals>.<listcomp>s��H�H�H��f�V�n�n�H�H�Hr)r@r�SQLrr)rrd�column_names�constraint_namer~s     r�
add_uniquezSchemaMigrator.add_uniquesl��#�c�h�h�|�&<�&<�<�����M�M��H�H�<�H�H�H�I�I�K�L�L�
��"�"�5�/�:�F�F�Frc��|�|���|���d���t	|����S)Nz DROP CONSTRAINT r|�rrdrss   r�drop_constraintzSchemaMigrator.drop_constraintsF�����d�/�/�1�1�5�9�9���,�-�-���V�D�\�\�"�"�	$rc��|�d���t|jjj�����d���t
t|jj��f����}|j	�|�d|j	z��}|j
�|�d|j
z��}|S)N� REFERENCES r{�
 ON DELETE %s�
 ON UPDATE %s)r`rar�	rel_model�_metarFr�	rel_fieldre�	on_delete�	on_update�rrjrfs   rrvz SchemaMigrator.add_inline_fk_sqls�������'�'���F�5�?�0�;�<�<�=�=��������$�f�U�_�-H�&I�&I�%K�L�L�M�M�		�
�?�&��+�+�o���?�@�@�C��?�&��+�+�o���?�@�@�C��
rNc��d|�d|�d|��}|����d���t|�����d���tt	|�������d���tt|��f�����d���t|�����d���t|�����d	��}|�|�d
|z��}|�|�d|z��}|S)N�fk_r<�_refs_rirzz
 FOREIGN KEY r�� (�)r�r�)rQr`rarrr)	rrdre�rel�
rel_columnr�r�r~rjs	         r�add_foreign_key_constraintz)SchemaMigrator.add_foreign_key_constraint!s1���,1�5�5�+�+�+�s�s�C�
���������'�'���F�5�M�M�"�"���*�+�+���F�4�Z�@�@�A�A�B�B����(�(���$�f�[�&9�&9�%;�<�<�=�=����'�'���F�3�K�K� � ����
�
���F�:�&�&�'�'������	�� ��+�+�o�	�9�:�:�C�� ��+�+�o�	�9�:�:�C��
rc
��|js|j�td|z���t|t��}|r|jstd���|�|||��g}|js@|�|�|||��|�	||��g��|rW|j
rP|�|�|||j
jj|jj|j|j����|js|jrBt+|dd��}|�|�||f|j|����|S)Nz!%s is not null but has no defaultz$Foreign keys must specify a `field`.�
index_type)rrr_rZr"rur�rx�extendrg�add_not_null�explicit_create_foreign_key�appendr�r�r�rFrer�r��index�uniquer-�	add_index)rrdrerf�is_foreign_key�
operations�usings       r�
add_columnzSchemaMigrator.add_column8s����z�	P�e�m�3��@�;�N�O�O�O�#�E�?�;�;���	E�%�/�	E��C�D�D�D��+�+�E�;��F�F�G�
��z�	8�����"�"�5�+�u�=�=��!�!�%��5�5�7�
8�
8�
8��	&�d�>�	&�����/�/����O�)�4��O�/��O��O�
%�%�
&�
&�
&��;�	C�%�,�	C��E�<��6�6�E����d�n�n�U�[�N�-2�\�5�B�B�
C�
C�
C��rc��t�r��NotImplementedError)rrdres   r�drop_foreign_key_constraintz*SchemaMigrator.drop_foreign_key_constraint_���!�!rTc�|�|���}|�||���d���t	|����|r|�d��d�|j�|��D��}||vr|jr|�||��|gS|S)Nz
 DROP COLUMN � CASCADEc��g|]	}|j��
Sr2�ro)r��foreign_keys  rr�z.SchemaMigrator.drop_column.<locals>.<listcomp>ms1��F�F�F��
��F�F�Fr)	rQrkr`rarr�get_foreign_keys�explicit_delete_foreign_keyr�)rrdre�cascaderj�
fk_columnss      r�drop_columnzSchemaMigrator.drop_columncs������!�!��	
�	�	�3��	&�	&�
�'�/�
"�
"�

�#�f�[�!�!�
"�
"�
"��	$��K�K�
�#�#�#�F�F�#�}�=�=�e�D�D�F�F�F�
��*�$�$��)I�$��4�4�U�K�H�H�#�N�N��
rc��|�|���|���d���t	|�����d���t	|����S)Nz RENAME COLUMN � TO r|)rrd�old_name�new_names    r�
rename_columnzSchemaMigrator.rename_columnush�����d�/�/�1�1�5�9�9���*�+�+���V�H�%�%�&�&��������V�H�%�%�&�&�	(rc�z�|�|���||���d��S)Nz
 SET NOT NULL�rprQr`�rrdros   rr�zSchemaMigrator.add_not_null~s4�����t�0�0�2�2�E�6�B�B����)�)�	+rc�z�|�|���||���d��S)Nz DROP NOT NULLr�r�s   r�
drop_not_nullzSchemaMigrator.drop_not_null�s5�����t�0�0�2�2�E�6�B�B���)�*�*�	,rc�j�|���}|�|||���d���|�|����}|�Lt|t��st|��}|�d���|��}|S)Nz TYPE z USING )rQrpr`ra�ddl_datatyper"r
r��rrdrorf�castrjs      r�alter_column_typez SchemaMigrator.alter_column_type�s������!�!����
�c�5�&�1�1����!�!���E�&�&�s�+�+�,�,�	����d�D�)�)�
!��4�y�y���+�+�i�(�(�,�,�T�2�2�C��
rc��|�|���|���d���t	|����S)Nz RENAME TO r|�rr�r�s   r�rename_tablezSchemaMigrator.rename_table�sG�����d�/�/�1�1�8�<�<����'�'���V�H�%�%�&�&�	(rc���	�|���}t||��}t|���	�	fd�|D��}t|�	|||���}|�|��S)Nc�:��g|]}t�j|����Sr2)r-�c)r�ro�	table_objs  �rr�z,SchemaMigrator.add_index.<locals>.<listcomp>�s%���C�C�C���	��V�,�,�C�C�Cr)r�r�)rQrJ�Table�Indexra)
rrdrGr�r�rjrH�colsr�r�s
         @rr�zSchemaMigrator.add_index�so������!�!��$�U�G�4�4�
��%�L�L�	�C�C�C�C�7�C�C�C���j�)�T�&��N�N�N���w�w�u�~�~�rc��|����d���t|����S)N�DROP INDEX �rQr`rar�rrdrHs   r�
drop_indexzSchemaMigrator.drop_index�s5����������'�'���V�J�'�'�(�(�	*r�NN�Tr)FN)r.r/r0r�r�rrQ�classmethodr\r:rgrkrprxrr�r�rvr�r�r�r�r�r�r�r�r�r�r�r2rrrLrL�sT������"'��"'��!�!�!�/�/�/��	@�	@��[�	@��
!�
!��Y�
!�>�>�>�&�&�&�����Y��(�"�"��Y�"��G�G��Y�G��$�$��Y�$�
�
�
��=A�����Y��,�$�$��Y�$�L�"�"��Y�"������Y��"�(�(��Y�(��+�+��Y�+�
�,�,��Y�,�
�����Y���(�(��Y�(������Y���*�*��Y�*�*�*rrLc�J��eZdZd�Zed���Ze�fd���Z�xZS)rUc�|�d}|j�||z��}d�|���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;
        c��g|]
}|d��S�rr2)r��rows  rr�z;PostgresqlMigrator._primary_key_columns.<locals>.<listcomp>�s��4�4�4�3��A��4�4�4r)r�execute_sql�fetchall)r�tbl�query�cursors    r�_primary_key_columnsz'PostgresqlMigrator._primary_key_columns�sB��	����*�*�5�3�;�7�7��4�4�&�/�/�"3�"3�4�4�4�4rc�V�|����d|z��S)NzSET search_path TO %s)rQr`)r�schema_names  r�set_search_pathz"PostgresqlMigrator.set_search_path�s'���������0�;�>�?�?�	Arc���|�|��}tt|��}|�||d���g}t	|��dkr�|�d|d�d�}d}|j�||f��}t|�����r7|�d|d�d�}	|�	|�||	����|S)NT)r+�r<r�_seqz�
                SELECT 1
                FROM information_schema.sequences
                WHERE LOWER(sequence_name) = LOWER(%s)
            )
r��superrUr�rArr��bool�fetchoner�)rr�r��pk_names�ParentClassr��seq_namer�r��new_seq_name�	__class__s          �rr�zPostgresqlMigrator.rename_table�s�����,�,�X�6�6���.��5�5��
�$�$�X�x�d�$�K�K�M�
��x�=�=�A���&.�h�h������<�H��E�
�]�.�.�u�x�k�B�B�F��F�O�O�%�%�&�&�
-�.6�h�h������D���!�!�+�":�":��l�#,�#,�-�-�-��r)r.r/r0r�r:r�r��
__classcell__)r�s@rrUrU�sq�������5�5�5��A�A��Y�A�
������Y�����rrUc�.�eZdZdZd�Zed���ZdS)rSTc��dSrr2r�s   rrvz%CockroachDBMigrator.add_inline_fk_sql�����rc��|����d���t|�����d��S)Nr�r�r�r�s   rr�zCockroachDBMigrator.drop_index�sB����������'�'���V�J�'�'�(�(����$�$�		&rN)r.r/r0r�rvr:r�r2rrrSrS�sD������"&��
�
�
��&�&��Y�&�&�&rrSc�X�eZdZed���Zed���Zed���Zdd�ZdS)�MySQLColumnc��|jdkS)N�PRI��pkrPs r�is_pkzMySQLColumn.is_pk�����w�%��rc��|jdkS)N�UNIr�rPs r�	is_uniquezMySQLColumn.is_unique�rrc��|jdkS)N�YES)rrrPs r�is_nullzMySQLColumn.is_null�s���y�E�!�!rNc��|�|j}|�|j}t|��t|j��g}|jr"|�td����|r#|�td����n"|�td����|jr"|�td����|jr'|�t|j����t|��S)Nr��NULL�NOT NULLzPRIMARY KEY)
rrsrr��
definitionrr�r�extrar)rrer�partss    rrazMySQLColumn.sql�s����?��l�G����)�K��;������ � �"���>�	(��L�L��X���'�'�'��	*��L�L��V���%�%�%�%��L�L��Z���)�)�)��:�	-��L�L��]�+�+�,�,�,��:�	*��L�L��T�Z���)�)�)�����rr�)r.r/r0�propertyrrrrar2rrr�r��sw������
� � ��X� �� � ��X� ��"�"��X�"������rr��_Column)rsrrrr�r_rc���eZdZdZdZd�Zed���Zd�Zd�Z	ed���Z
d�Zed���Zed	���Z
ed
���Zedd���Zed
���ZdS)rWTc��|�||���d���t|����S)N� MODIFY rmrns    rrpzMySQLMigrator._alter_columns9�����c�5�)�)����$�$���V�F�^�^�$�$�	&rc��|����d���t|�����d���t|����S)Nz
RENAME TABLE r�r�r�s   rr�zMySQLMigrator.rename_tablesU����������)�)���V�H�%�%�&�&��������V�H�%�%�&�&�	(rc��|j�d|z��}|���}|D]}t|�}|j|kr|cS�dS)NzDESCRIBE `%s`;F)rr�r�r�rs)rrdrer��rowsr�ros       r�_get_column_definitionz$MySQLMigrator._get_column_definition#se����*�*�+;�e�+C�D�D����� � ���	�	�C� �#�&�F��{�k�)�)��
�
�
�*��urc��|j�d||f��}|���}|std|�d|�d����|dS)Nz�SELECT constraint_name FROM information_schema.key_column_usage WHERE table_schema = DATABASE() AND table_name = %s AND column_name = %s AND referenced_table_name IS NOT NULL AND referenced_column_name IS NOT NULL;z+Unable to find foreign key constraint for "z" on table "z".r)rr�r��AttributeError)rrdrer�r(s     r�get_foreign_key_constraintz(MySQLMigrator.get_foreign_key_constraint,ss����*�*�3��K� �"�"�����"�"���	>� �.�).������=�>�>�
>��a�y�rc���|�||��}|�|���|���d���t|����S)Nz DROP FOREIGN KEY )rrkrQr`rar)rrdre�
fk_constraints    rr�z)MySQLMigrator.drop_foreign_key_constraint=s]���7�7��{�K�K�
����d�/�/�1�1�5�9�9���-�.�.���V�M�*�*�+�+�	-rc��dSrr2r�s   rrvzMySQLMigrator.add_inline_fk_sqlEr�rc���|�||��}|�|���|���d���|�d�����}td�|j�|��D����}||vr|S||}|�||��||�	|||j
|j��fS)NrF�rc3�(K�|]
}|j|fV��dSrr��r��fks  r�	<genexpr>z-MySQLMigrator.add_not_null.<locals>.<genexpr>P�<����=�=���Y��O�=�=�=�=�=�=r)rrkrQr`ra�dictrr�r�r��
dest_table�dest_column)rrdro�
column_defr��
fk_objects�fk_metadatas       rr�zMySQLMigrator.add_not_nullHs���0�0���?�?�
��%��d�&7�&7�&9�&9�5�A�A� ���,�,���Z�^�^�E�^�:�:�;�;�	�
�=�=��m�4�4�U�;�;�=�=�=�=�=�
���#�#��� ��(���0�0���?�?���/�/����*��+�	-�-�.�	.rc��|�||��}|jrtd���|�|���|���d���|�d�����S)NzPrimary keys can not be nullrTr)rrrZrkrQr`rar�s   rr�zMySQLMigrator.drop_not_null_s{���,�,�U�F�;�;���<�	=��;�<�<�<����d�/�/�1�1�5�9�9����$�$���V�Z�Z��Z�-�-�.�.�	0rc�L�td�|j�|��D����}||v}|�||��}|�|���|���d���t|�����d���|�|�����}|rA||}|�	||��||�
|||j|j��gS|S)Nc3�(K�|]
}|j|fV��dSrr�r s  rr"z.MySQLMigrator.rename_column.<locals>.<genexpr>kr#rz CHANGE r{)re)
r$rr�rrkrQr`rarr�r�r%r&)	rrdr�r�r(r�ro�
rename_ctxr)s	         rr�zMySQLMigrator.rename_columnis*���=�=��m�4�4�U�;�;�=�=�=�=�=�
�"�Z�/���,�,�U�H�=�=���#�|�D�$5�$5�$7�$7��?�?��w�z�*�*��s�6�(�+�+�,�,��w�s�|�|��s�6�:�:�(�:�;�;�<�<�	��	�$�X�.�K��0�0���A�A���/�/����*��+�	-�-��
��rNc�P�|�td���|���}|�||���d���t|�����d���|�|����S)Nz5alter_column_type() does not support cast with MySQL.rr{)rZrQrkr`rarrtr�s      rr�zMySQLMigrator.alter_column_type�s������&�'�'�
'����!�!�����c�5�)�)����$�$���V�F�^�^�$�$��������U�Y�Y�s�^�^�$�$�	&rc��|����d���t|�����d���t|����S)Nr�z ON r�r�s   rr�zMySQLMigrator.drop_index�sS����������'�'���V�J�'�'�(�(��������V�E�]�]�#�#�	%rr)r.r/r0r�r�rpr:r�rrr�rvr�r�r�r�r�r2rrrWrWs������"&��"&��&�&�&��(�(��Y�(�������"�-�-��Y�-�
�
�
��.�.��Y�.�,�0�0��Y�0�����Y��6�
&�
&�
&��Y�
&��%�%��Y�%�%�%rrWc�~�eZdZdZejd��Zejd��Zejd��Zejdej	��Z
d�Zd�Ze
d���Zd	�Ze
dd���Ze
d���Ze
d
���Ze
d���Ze
dd���Ze
d���Ze
d���Ze
	dd���ZdS)rYz�
    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+c�^�|j�d|z��}d�|jD��S)Nzselect * from "%s" limit 1c��g|]
}|d��Sr�r2)r�r)s  rr�z4SqliteMigrator._get_column_names.<locals>.<listcomp>�s��4�4�4�D��Q��4�4�4r)rr��description�rrd�ress   r�_get_column_namesz SqliteMigrator._get_column_names�s3���m�'�'�(D�u�(L�M�M��4�4�C�O�4�4�4�4rc��|j�dd|���g��}|���S)NzBselect name, sql from sqlite_master where type=? and LOWER(name)=?rd)rr��lowerr�r4s   r�_get_create_tablez SqliteMigrator._get_create_table�s>���m�'�'�.�
�e�k�k�m�m�$�&�&���|�|�~�~�rc��	���td��j�|��D����}|���|vrt	d|�d|�d������|��\}}�j�|��}�j�|��tj	dd|��}�j
�|�����\}}�j
�|��}	d�|	D��}
g}g}g}
d}|
D�]}�j�|�����\}||kr||||��}|rm|�|��|
�|���j�|�����\}|�|����|�|��|����|��s*|�|��|
�|����t't)|
|����}|�|���d	�}�sd
�}n�|kr��fd�}g}|D]^}�j�|��}|�)|���d|kr||��}|r|�|���_|d
z}tjd|ztj��}|�	d|z|��}d�|��}t5t7d��t9|��g��t7|����d|�d���g}t5t7d��t9|��t=d�|D����t7d��t?d�|
D����t7d��t9|��f��}t5t7d��t9|��g��}|||�� ||��gz
}tCd�|��D]u}||j"vr(|�t7|j#�����3�r@��$|j#|���}|�"|�t7|�����v|S)Nc3�HK�|]}|j���V��dSr)rsr8r�s  rr"z0SqliteMigrator._update_column.<locals>.<genexpr>�sL����F�F� ��k�'�'�)�)�F�F�F�F�F�FrzColumn "z" does not exist on "�"z\s+r{c�6�g|]}|�����Sr2��strip�r��cols  rr�z1SqliteMigrator._update_column.<locals>.<listcomp>�s ��<�<�<�s�s�y�y�{�{�<�<�<r)zforeign zprimary zconstraint zcheck c��|Srr2�r's r�<lambda>z/SqliteMigrator._update_column.<locals>.<lambda>�s��*�rc��dSrr2rCs rrDz/SqliteMigrator._update_column.<locals>.<lambda>�s��d�rc�@���j�d�z|��S)NzFOREIGN KEY ("%s") )�fk_re�sub)r'�
new_columnrs ��rrDz/SqliteMigrator._update_column.<locals>.<lambda>�s"���d�j�n�n�%�
�2��/�/�rr�__tmp__z
("?)%s("?)z\1%s\2�, zDROP TABLE IF EXISTSr�r�zINSERT INTOc�,�g|]}t|����Sr2rr@s  rr�z1SqliteMigrator._update_column.<locals>.<listcomp>s��F�F�F�c�f�S�k�k�F�F�Fr�SELECTc�,�g|]}t|����Sr2rr@s  rr�z1SqliteMigrator._update_column.<locals>.<listcomp>s��H�H�H�3�6�#�;�;�H�H�Hr�FROMz
DROP TABLEc��|jSr)ra)�idxs rrDz/SqliteMigrator._update_column.<locals>.<lambda>s����r)%�setr�get_columnsr8rZr9�get_indexesr��rerH�	column_re�search�groups�column_split_re�findall�column_name_re�matchr��
startswithr$�zip�getrG�compile�Ir@rr�rr?rrr��filterrGra�
_fix_index)rrd�column_to_updater6rG�create_table�indexes�
raw_create�raw_columns�
split_columns�column_defs�new_column_defs�new_column_names�original_column_names�constraint_termsr're�new_column_def�original_to_new�fk_filter_fn�cleaned_columnsr\�
temp_table�rgx�create�queries�populate_table�
drop_originalr�rarIs`                             @r�_update_columnzSqliteMigrator._update_column�sD�����F�F�$(�M�$=�$=�e�$D�$D�F�F�F�F�F���!�!�#�#�7�2�2��*�.�.�.����7�8�8�
8�#�4�4�U�;�;���|��-�+�+�E�2�2��	
�
�&�&�u�-�-�-��v�f�c�<�8�8��#'�.�"7�"7��"E�"E�"L�"L�"N�"N��
�K��,�4�4�[�A�A�
�<�<�m�<�<�<������ "��L��%�	>�	>�J��.�4�4�Z�@�@�G�G�I�I�L�K��.�.�.�!#��K��!<�!<��!�9�#�*�*�>�:�:�:�)�0�0��=�=�=�#'�#6�#<�#<�&�$(�$(�(.����!�K�$�+�+�K�8�8�8���&�&�z�2�2�2�"�'�'�)�)�4�4�5E�F�F�>�$�+�+�K�8�8�8�)�0�0��=�=�=���s�#8�:J�K�K�L�L��$�(�(�)9�:�:�
�4�4���	�2�2�L�L�
�+�
+�
+������L���)�	3�	3�J��J�$�$�Z�0�0�E�� �U�\�\�^�^�A�%6�:J�%J�%J�)�\�*�5�5�
��
3��&�&�z�2�2�2���Y�&�
��j���-�r�t�4�4�������#�����
�)�)�O�,�,���c�0�1�1�6�*�3E�3E�F�G�G��V�\�\�^�^�^�^�W�W�W�5�6�6�8��
"��
����:����F�F�5E�F�F�F�G�G���M�M��H�H�2G�H�H�H�I�I���K�K��5�M�M�#����!�#�l�"3�"3�V�E�]�]�!C�D�D�
�	������j�%�0�0�2�	2���/�/��9�9�	-�	-�E��u�}�4�4����s�5�9�~�~�.�.�.�.��
-��o�o�e�i�1A�:�N�N���?��N�N�3�s�8�8�,�,�,���rc��|�|��}t|��dkr|�||��S|�dd��\}}t|�|����dkr|�d|�||����S|�dd��d�d��}d�|D��}g}|D]I}	t	jd|z|	��r||	t|��d�z}	|�|	���J|�dd	�d
�|D�����d�S)N��(r�r�r�,c�8�g|]}|�d����S)z"`[]' r>)r��parts  rr�z-SqliteMigrator._fix_index.<locals>.<listcomp>7s$��;�;�;�T�4�:�:�i�(�(�;�;�;rz%s(?:['"`\]]?\s|$)rKc3� K�|]	}d|zV��
dS)z"%s"Nr2)r�r�s  rr"z,SqliteMigrator._fix_index.<locals>.<genexpr>Bs&����)D�)D��&�1�*�)D�)D�)D�)D�)D�)Dr)�splitrA�replace�rsplitrUr\r�r@)
rrardrIr
�lhs�rhsrG�cleanros
          rrczSqliteMigrator._fix_index%sf���	�	�*�+�+���u�:�:��?�?��;�;�/��<�<�<��:�:�c�1�%�%���S��s�y�y�)�*�*�+�+�q�0�0�!�c�c�3�;�;�/?��#L�#L�#L�M�M��
�
�3��"�"�1�%�+�+�C�0�0��;�;�U�;�;�;��
���	!�	!�F��x�-�0@�@�&�I�I�
E�#�f�S�1A�-B�-B�-C�-C�&D�D���L�L�� � � � ��3�3��	�	�)D�)D�e�)D�)D�)D� D� D� D� D�E�ErTc�2�|�||d���S)Nc��dSrr2)�a�bs  rrDz,SqliteMigrator.drop_column.<locals>.<lambda>Fs��D�r�ry)rrdrer�s    rr�zSqliteMigrator.drop_columnDs���"�"�5�+�7H�7H�I�I�Irc�<���fd�}|�|||��S)Nc�0��|�|���Sr�r�)rer'r�s  �r�_renamez-SqliteMigrator.rename_column.<locals>._renameJs����%�%�k�8�<�<�<rr�)rrdr�r�r�s   ` rr�zSqliteMigrator.rename_columnHs4���	=�	=�	=�	=�	=��"�"�5�(�G�<�<�<rc�6�d�}|�|||��S)Nc��|dzS)Nz	 NOT NULLr2�rer's  r�
_add_not_nullz2SqliteMigrator.add_not_null.<locals>._add_not_nullPs
����+�+rr�)rrdror�s    rr�zSqliteMigrator.add_not_nullNs)��	,�	,�	,��"�"�5�&�-�@�@�@rc�6�d�}|�|||��S)Nc�.�|�dd��S)Nr
�r�r�s  r�_drop_not_nullz4SqliteMigrator.drop_not_null.<locals>._drop_not_nullVs���%�%�j�"�5�5�5rr�)rrdror�s    rr�zSqliteMigrator.drop_not_nullTs)��	6�	6�	6��"�"�5�&�.�A�A�ArNc�����|�td���|�������fd�}|�|�|��S)Nz6alter_column_type() does not support cast with Sqlite.c���������}��t������|�����\}}|Sr)rtrarr�)rer'�	node_listrar<rorjrfs     ���r�_alter_column_typez<SqliteMigrator.alter_column_type.<locals>._alter_column_type`sL����	�	�#���I��W�W�V�F�^�^�,�,�0�0��;�;�A�A�C�C�F�C���Jr)rZrQry)rrdrorfr�r�rjs  ``  @rr�z SqliteMigrator.alter_column_typeZsq��������'�(�(�
(����!�!��	�	�	�	�	�	�	��"�"�5�&�2D�E�E�Erc��t�rr�r}s    rrzSqliteMigrator.add_constraintfr�rc��t�rr�r�s   rr�zSqliteMigrator.drop_constraintjr�rc��t�rr�)rrdrerfr�r�s      rr�z)SqliteMigrator.add_foreign_key_constraintns
��"�!rr�rr�)r.r/r0r1rUr`rVrYr[rarGr6r9r:ryrcr�r�r�r�r�rr�r�r2rrrYrY�s�����������
�?�+�+�I� �b�j�!8�9�9�O��R�Z� 4�5�5�N��B�J�:�B�D�A�A�E�5�5�5�����q�q��Y�q�fF�F�F�>�J�J�J��Y�J��=�=��Y�=�
�A�A��Y�A�
�B�B��Y�B�
�	F�	F�	F��Y�	F��"�"��Y�"��"�"��Y�"��=A�"�"�"��Y�"�"�"rrYc�8�|D]}|����dSr)r$)r�rr:s   r�migrater�ts*�����	��
�
������r)r1�collectionsrr8rBrU�peeweerrrr	r
rrr
rr�playhouse.cockroachdbr�ImportError�objectrr:rJrLrUrSr�rWrYr�r2rr�<module>r�s���t�t�j#�"�"�"�"�"���������	�	�	�	����� � � � � � �#�#�#�#�#�#�������������������������������������������,�,�,�,�,�,��7�7�7�7�7�7�7��������������:�:�:�:�:��:�:�:�6������t*�t*�t*�t*�t*�V�t*�t*�t*�n+�+�+�+�+��+�+�+�\&�&�&�&�&�,�&�&�&� � � � � �*�*�Y�)=�>�>� � � �FI%�I%�I%�I%�I%�N�I%�I%�I%�XU"�U"�U"�U"�U"�^�U"�U"�U"�p����s�A�A'�&A'