o
    KDi(                     @   s  d dl mZmZ d dlmZmZ d dlmZ d dlm	Z	m
Z
 ddlZedZG dd	 d	ZG d
d deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZe
e eZdS )   )ModelBufferModel)	DateFieldStringField)	MergeTree)escapeget_subclass_names    N
migrationsc                   @      e Zd ZdZdd ZdS )	Operationz.
    Base class for migration operations.
    c                 C   s   t  N)NotImplementedErrorselfdatabase r   W/var/www/Datamplify/venv/lib/python3.10/site-packages/infi/clickhouse_orm/migrations.pyapply   s   zOperation.applyN__name__
__module____qualname____doc__r   r   r   r   r   r   
       r   c                   @       e Zd ZdZdd Zdd ZdS )ModelOperationzL
    Base class for migration operations that work on a specific model.
    c                 C   s   || _ | | _dS )z
        Initializer.
        N)model_class
table_name)r   r   r   r   r   __init__   s   zModelOperation.__init__c                 C   s&   d| j |f }t| || dS )z;
        Utility for running ALTER TABLE commands.
        zALTER TABLE $db.`%s` %sN)r   loggerdebugraw)r   r   cmdr   r   r   _alter_table   s   
zModelOperation._alter_tableN)r   r   r   r   r   r$   r   r   r   r   r      s    r   c                   @   r   )CreateTablezM
    A migration operation that creates a table for a given model class.
    c                 C   s:   t d| j t| jtr|| jjj || j d S )Nz    Create table %s)	r    infor   
issubclassr   r   create_tableengine
main_modelr   r   r   r   r   -   s   zCreateTable.applyNr   r   r   r   r   r%   (   r   r%   c                   @   r   )
AlterTablea-  
    A migration operation that compares the table of a given model class to
    the model's fields, and alters the table to match the model. The operation can:
      - add new columns
      - drop obsolete columns
      - modify column types
    Default values are not altered by this operation.
    c                 C   s$   d|j | jf }dd ||D S )NzDESC `%s`.`%s`c                 S   s   g | ]}|j |jfqS r   )nametype).0rowr   r   r   
<listcomp>@   s    z0AlterTable._get_table_fields.<locals>.<listcomp>)db_namer   select)r   r   queryr   r   r   _get_table_fields>   s   zAlterTable._get_table_fieldsc                    sb  t d| j t|  }t| t| j  }|D ]}t d| | 	 d|  ||= qd }| j 
 D ]8\}}|jpC|j }||vrnt d| d||j df }|rh|rd|d| 7 }n|d7 }| 	 | |rr|}q: fd	d
| j 
 D }	|  D ](\}
}|
|	v sJ d||	|
 krt d|
||	|
  | 	 d|
|	|
 f  qd S )Nz    Alter table %sz        Drop column %szDROP COLUMN %sz        Add column %szADD COLUMN %s %s)dbz	 AFTER %sz FIRSTc                    s    i | ]\}}||j d  dqS )F)with_default_expressionr5   )get_sql)r.   r,   fieldr   r   r   
<dictcomp>g   s    z$AlterTable.apply.<locals>.<dictcomp>z.Model fields and table columns in disagreementz.        Change type of column %s from %s to %szMODIFY COLUMN %s %s)r    r&   r   dictr4   setkeysr   fieldsr$   itemsmaterializedaliasr7   )r   r   table_fieldsdeleted_fieldsr,   	prev_namer8   is_regular_fieldr#   model_fields
field_name	field_sqlr   r9   r   r   B   sB   

zAlterTable.applyN)r   r   r   r   r4   r   r   r   r   r   r+   4   s    	r+   c                   @   r   )AlterTableWithBufferz
    A migration operation for altering a buffer table and its underlying on-disk table.
    The buffer table is dropped, the on-disk table is altered, and then the buffer table
    is re-created.
    c                 C   sX   t | jtr"t| j| t| jjj| t| j| d S t| j| d S r   )	r'   r   r   	DropTabler   r+   r)   r*   r%   r   r   r   r   r   z   s
   zAlterTableWithBuffer.applyNr   r   r   r   r   rI   s   s    rI   c                   @   r   )rJ   zL
    A migration operation that drops the table of a given model class.
    c                 C   s   t d| j || j d S )Nz    Drop table %s)r    r&   r   
drop_tabler   r   r   r   r   r      s   zDropTable.applyNr   r   r   r   r   rJ      r   rJ   c                   @   r   )AlterConstraintsa_  
    A migration operation that adds new constraints from the model to the database
    table, and drops obsolete ones. Constraints are identified by their names, so
    a change in an existing constraint will not be detected unless its name was changed too.
    ClickHouse does not check that the constraints hold for existing data in the table.
    c                 C   s   t d| j | |}| jj D ]}|j|vr+t d|j | |d|	   q|
|j q|D ]}t d| | |d|  q4d S )Nz    Alter constraints for %sz        Add constraint %sADD %sz        Drop constraint %szDROP CONSTRAINT `%s`)r    r&   r   _get_constraint_namesr   _constraintsvaluesr,   r$   create_table_sqlremove)r   r   existing
constraintr,   r   r   r   r      s   

zAlterConstraints.applyc                 C   ,   ddl }|d| j }|d|}t|S )zZ
        Returns a set containing the names of existing constraints in the table.
        r	   NSHOW CREATE TABLE $db.`%s`z"\sCONSTRAINT\s+`?(.+?)`?\s+CHECK\srer"   r   findallr<   r   r   rX   	table_defmatchesr   r   r   rN         z&AlterConstraints._get_constraint_namesN)r   r   r   r   r   rN   r   r   r   r   rL      s    rL   c                       s2   e Zd ZdZd	 fdd	Zdd Zdd Z  ZS )
AlterIndexesz
    A migration operation that adds new indexes from the model to the database
    table, and drops obsolete ones. Indexes are identified by their names, so
    a change in an existing index will not be detected unless its name was changed too.
    Fc                    s   t  | || _dS )z
        Initializer.
        By default ClickHouse does not build indexes over existing data, only for
        new data. Passing `reindex=True` will run `OPTIMIZE TABLE` in order to build
        the indexes over the existing data.
        N)superr   reindex)r   r   r`   	__class__r   r   r      s   
zAlterIndexes.__init__c                 C   s   t d| j | |}t | | jj D ]}|j|vr0t d|j | |d|	   q|
|j q|D ]}t d| | |d|  q9| jr\t d |d| j  d S d S )Nz    Alter indexes for %sz        Add index %srM   z        Drop index %szDROP INDEX `%s`z        Build indexes on tablezOPTIMIZE TABLE $db.`%s` FINAL)r    r&   r   _get_index_namesr   _indexesrP   r,   r$   rQ   rR   r`   r"   )r   r   rS   indexr,   r   r   r   r      s   



zAlterIndexes.applyc                 C   rU   )zV
        Returns a set containing the names of existing indexes in the table.
        r	   NrV   z\sINDEX\s+`?(.+?)`?\s+rW   rZ   r   r   r   rc      r]   zAlterIndexes._get_index_names)F)r   r   r   r   r   r   rc   __classcell__r   r   ra   r   r^      s
    
r^   c                   @   r   )	RunPythonz@
    A migration operation that executes a Python function.
    c                 C   s   t |sJ d|| _dS )z
        Initializer. The given Python function will be called with a single
        argument - the Database instance to apply the migration to.
        z 'func' argument must be functionN)callable_func)r   funcr   r   r   r      s   
zRunPython.__init__c                 C   s   t d| jj | | d S )Nz!    Executing python operation %s)r    r&   ri   r   r   r   r   r   r      s   zRunPython.applyNr   r   r   r   r   r   r   r   r   r   rg      s    rg   c                   @   r   )RunSQLzG
    A migration operation that executes arbitrary SQL statements.
    c                 C   s,   t |tr|g}t |tsJ d|| _dS )zr
        Initializer. The given sql argument must be a valid SQL statement or
        list of statements.
        z0'sql' argument must be string or list of stringsN)
isinstancestrlist_sql)r   sqlr   r   r   r      s   

zRunSQL.__init__c                 C   s$   t d | jD ]}|| qd S )Nz     Executing raw SQL operations)r    r&   rp   r"   )r   r   itemr   r   r   r      s   

zRunSQL.applyNrk   r   r   r   r   rl      s    
rl   c                   @   s8   e Zd ZdZe Ze Ze Ze	ddZ
edd ZdS )MigrationHistoryz_
    A model for storing which migrations were already applied to the containing database.
    applied)package_namemodule_namec                 C   s   dS )Ninfi_clickhouse_orm_migrationsr   )clsr   r   r   r     s   zMigrationHistory.table_nameN)r   r   r   r   r   ru   rv   r   rt   r   r)   classmethodr   r   r   r   r   rs     s    
rs   )modelsr   r   r>   r   r   enginesr   utilsr   r   logging	getLoggerr    r   r   r%   r+   rI   rJ   rL   r^   rg   rl   rs   locals__all__r   r   r   r   <module>   s$    
	?
"0