
    Wwg                     Z    d dl mZ d dlmZ d dlmZmZ d dlmZ d dl	m
Z
  G d de      Zy)	    )ceil)settings)CommandErrorCommandParser)BaseCommand)LogEntryc                   >    e Zd ZdZdZdefdZd Zd Zd Z	d Z
d	 Zy
)Commandz3Migrates changes from changes_text to json changes.Tparserc                     |j                         }|j                  dddd       |j                  ddd dd	d
t        g d       |j                  dddddt               y )Nz--check
store_truez&Just check the status of the migrationcheck)actionhelpdestz-dz
--databasezThe database enginezjIf provided, the script will use native db operations. Otherwise, it will use LogEntry.objects.bulk_updatedb)postgresmysqloracle)defaultmetavarr   r   typechoicesz-bz--batch-sizei  zSplit the migration into multiple batches. If 0, then no batching will be done. When passing a -d/database, the batch value will be ignored.
batch_size)r   r   r   r   )add_argument_groupadd_argumentstrint)selfr   groups      g/var/www/horilla/myenv/lib/python3.12/site-packages/auditlog/management/commands/auditlogmigratejson.pyadd_argumentszCommand.add_arguments   s    ))+9	 	 	
 	)B3 	 
	
 	K 	 	
    c                    |d   }|d   }|d   }| j                         r|ry |rJ| j                  |      }| j                  j                  | j                  j                  d| d             nI| j                  |      }| j                  j                  | j                  j                  d| d             | j                          y )Nr   r   r   zUpdated z* records using native database operations.z! records using django operations.)
check_logsmigrate_using_sqlstdoutwritestyleSUCCESSmigrate_using_django)r   argsoptionsdatabaser   r   results          r!   handlezCommand.handle+   s    4=\*
 !e++H5FKK

""vh&PQ ..z:FKK

""XfX5V#WX 	r#   c                    | j                         j                         }|r | j                  j                  d| d       y| j                  j                  | j                  j                  d             t        j                  r:| j                  j                  d      }| j                  j                  d| d       y)	Nz
There are z records that needs migration.TzAll records have been migrated.0AUDITLOG_USE_TEXT_CHANGES_IF_JSON_IS_NOT_PRESENTzYou can now set z
 to False.F)	get_logscountr'   r(   r)   r*   r   r2   WARNING)r   r4   var_msgs      r!   r%   zCommand.check_logsB   s    %%'KK
5'1OPQ$**,,-NOPDDjj((BG KK 0	DEr#   c                 d    t         j                  j                  dd      j                  d      S )NFT)changes_text__isnullchanges__isnull )changes_text__exact)r   objectsfilterexclude)r   s    r!   r3   zCommand.get_logsQ   s1    &&!& ' 

'b'
)	*r#   c                      dt         f fd} j                         }|s ||      S d}t        t        |j	                         |z              D ]  }| | j                         d |       z  }  |S )Nreturnc                    dd l }g }g }| D ]4  }	 |j                  |j                        |_        |j	                  |       6 t        j                  j                  |dg       |r7j                  j                  j                  j                  d|              t        |      S # t
        $ r |j	                  |j                         Y w xY w)Nr   changes)fieldsz}ValueError was raised while converting the logs with these ids into json.They where not be included in this migration batch.
)jsonloadschanges_textrB   append
ValueErroridr   r<   bulk_updatestderrr(   r)   ERRORlen)_logsrD   updatederrorslogr   s        r!   _apply_django_migrationz=Command.migrate_using_django.<locals>._apply_django_migrationW   s    GF (("&**S-=-=">CK NN3'( (()(E!!JJ$$ "($ w< " *MM#&&)*s    B))$CCr   )r   r3   ranger   r4   )r   r   rR   logstotal_updated_s   `     r!   r+   zCommand.migrate_using_djangoV   sx    	 c 	 2 }}*400tDJJL:567 	SA4T]]_[j5QRRM	Sr#   c                 P    ddl m fd}|dk(  r |       S t        d| d      )Nr   )
connectionc                      j                         5 } | j                  d       | j                   j                  cd d d        S # 1 sw Y   y xY w)Nz:UPDATE auditlog_logentry SET changes="changes_text"::jsonb)cursorexecuterowcount)rZ   rX   s    r!   r   z+Command.migrate_using_sql.<locals>.postgres}   sD    ""$ .P }}--	. . .s   'AAr   zMigrating the records using zZ is not implemented. Run this management command without passing a -d/--database argument.)	django.dbrX   r   )r   r.   r   rX   s      @r!   r&   zCommand.migrate_using_sqlz   s=    (	. z!:*8* 5T U
 	
r#   N)__name__
__module____qualname__r   requires_migrations_checksr   r"   r0   r%   r3   r+   r&    r#   r!   r
   r
   
   s3    @D!%
M 
:.*
"H
r#   r
   N)mathr   django.confr   django.core.managementr   r   django.core.management.baser   auditlog.modelsr   r
   rb   r#   r!   <module>rh      s"       > 3 $@
k @
r#   