
    ug%              
          d dl Z d dlmZ d dlmZmZ d dlmZ d dlZd dl	Z	d dl
mZmZ d dlmZ d dlmZmZ d dlmZmZmZ  G d	 d
e      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z ej@                  d      Z!de"de"dedefd Z#d!ee   d"ee   dee   fd#Z$	 d)ded$e"d%e"dee   fd&Z%d'ed%e"defd(Z&y)*    N)Sequence)	TypedDictNotRequired)Traversable)SqlDBCursor)abstractmethod)SystemSettings)OpenTelemetryClientOpenTelemetryGranularitytrace_methodc                   F    e Zd ZU ee   ed<   eed<   eed<   eed<   eed<   y)MigrationFilepathdirfilenameversionscopeN)__name__
__module____qualname__r   r   __annotations__strint     K/var/www/openai/venv/lib/python3.12/site-packages/chromadb/db/migrations.pyr   r      s!    
k
""	HMLJr   r   c                   "    e Zd ZU eed<   eed<   y)	MigrationhashsqlN)r   r   r   r   r   r   r   r   r    r       s    
I	Hr   r    c                         e Zd Zd fdZ xZS )UninitializedMigrationsErrorc                 $    t         |   d       y )Nz$Migrations have not been initializedsuper__init__)self	__class__s    r   r(   z%UninitializedMigrationsError.__init__   s    ?@r   returnN)r   r   r   r(   __classcell__r*   s   @r   r$   r$      s    A Ar   r$   c                   (     e Zd Zdedef fdZ xZS )UnappliedMigrationsErrorr   r   c                 L    || _         || _        t        |   d| d|        y )NzUnapplied migrations in z, starting with version )r   r   r'   r(   )r)   r   r   r*   s      r   r(   z!UnappliedMigrationsError.__init__$   s/    &se+CG9M	
r   r   r   r   r   r   r(   r-   r.   s   @r   r0   r0   #   s    
C 
# 
 
r   r0   c                   ,     e Zd Zdededef fdZ xZS )InconsistentVersionErrorr   
db_versionsource_versionc                 F    t         |   d| dd| d| dz   dz          y )Nz#Inconsistent migration versions in :zdb version was z, source version was .zH Has the migration sequence been modified since being applied to the DB?r&   )r)   r   r5   r6   r*   s       r   r(   z!InconsistentVersionError.__init__-   s@    1#a8
|+@@PPQRSXY	
r   r2   r.   s   @r   r4   r4   ,   s"    
C 
S 
# 
 
r   r4   c                   ,     e Zd Zdededef fdZ xZS )InconsistentHashErrorr   db_hashsource_hashc                 F    t         |   d| dd| d| dz   dz          y )NzInconsistent hashes in r8   zdb hash was z, source has was r9   z? Was the migration file modified after being applied to the DB?r&   )r)   r   r<   r=   r*   s       r   r(   zInconsistentHashError.__init__6   s>    %dV1-WI%6{m1EFOP	
r   r   r   r   r   r(   r-   r.   s   @r   r;   r;   5   s"    
S 
3 
S 
 
r   r;   c                   $     e Zd Zdef fdZ xZS )InvalidHashErroralgc                 *    t         |   d|        y )Nz"Invalid hash algorithm specified: r&   )r)   rB   r*   s     r   r(   zInvalidHashError.__init__?   s    =cUCDr   r?   r.   s   @r   rA   rA   >   s    EC E Er   rA   c                       e Zd Zy)InvalidMigrationFilenameN)r   r   r   r   r   r   rE   rE   C   s    r   rE   c                   J    e Zd ZU dZeed<   deddf fdZee	de
fd              Ze	dee   fd       Ze	dd	       Ze	defd
       Ze	dedee   fd       Ze	dededdfd       ZddZ edej2                        dd       Z edej2                        dd       Z xZS )MigratableDBa  Simple base class for databases which support basic migrations.

    Migrations are SQL files stored as package resources and accessed via
    importlib_resources.

    All migrations in the same directory are assumed to be dependent on previous
    migrations in the same directory, where "previous" is defined on lexographical
    ordering of filenames.

    Migrations have a ascending numeric version number and a hash of the file contents.
    When migrations are applied, the hashes of previous migrations are checked to ensure
    that the database is consistent with the source repository. If they are not, an
    error is thrown and no migrations will be applied.

    Migration files must follow the naming convention:
    <version>.<description>.<scope>.sql, where <version> is a 5-digit zero-padded
    integer, <description> is a short textual description, and <scope> is a short string
    identifying the database implementation.
    	_settingssystemr,   Nc                 z    |j                   | _        |j                  t              | _        t
        |   |       y )N)settingsrH   requirer   _opentelemetry_clientr'   r(   )r)   rI   r*   s     r   r(   zMigratableDB.__init__^   s-    %+^^4G%H" r   c                       y)zFThe database implementation to use for migrations (e.g, sqlite, pgsql)Nr   r   r   r   migration_scopezMigratableDB.migration_scopec        	r   c                      y)zYDirectories containing the migration sequences that should be applied to this
        DB.Nr   r)   s    r   migration_dirszMigratableDB.migration_dirsi   rP   r   c                      y)z)Idempotently creates the migrations tableNr   rR   s    r   setup_migrationszMigratableDB.setup_migrationso        	r   c                      y)z*Return true if the migrations table existsNr   rR   s    r   migrations_initializedz#MigratableDB.migrations_initializedt   rV   r   r   c                      y)z~Return a list of all migrations already applied to this database, from the
        given source directory, in ascending order.Nr   )r)   r   s     r   db_migrationszMigratableDB.db_migrationsy   rP   r   cur	migrationc                      y)z(Apply a single migration to the databaseNr   )r)   r[   r\   s      r   apply_migrationzMigratableDB.apply_migration   rV   r   c                     | j                   j                  d      }|dk(  r| j                          |dk(  r| j                          yy)z!Initialize migrations for this DB
migrationsvalidateapplyN)rH   rL   validate_migrationsapply_migrations)r)   migrates     r   initialize_migrationsz"MigratableDB.initialize_migrations   sC    ..((6j $$&g!!# r   z MigratableDB.validate_migrationsc                 ^   | j                         s
t               | j                         D ]  }| j                  |      }t	        || j                         | j                  j                  d            }t        ||      }t        |      dkD  sc|d   d   }t        |j                  |       y)zpValidate all migrations and throw an exception if there are any unapplied
        migrations in the source repo.migrations_hash_algorithmr   r   )r   r   N)rX   r$   rS   rZ   find_migrationsrO   rH   rL   verify_migration_sequencelenr0   name)r)   r   rZ   source_migrationsunapplied_migrationsr   s         r   rc   z MigratableDB.validate_migrations   s     **,.00&&(C ..s3M /$$&&&'BC!
 $=0$  '(1,.q1)<.388WMM )r   zMigratableDB.apply_migrationsc                 n   | j                          | j                         D ]  }| j                  |      }t        || j	                         | j
                  j                  d            }t        ||      }| j                         5 }|D ]  }| j                  ||        	 ddd        y# 1 sw Y   xY w)z5Validate existing migrations, and apply all new ones.rh   N)
rU   rS   rZ   ri   rO   rH   rL   rj   txr^   )r)   r   rZ   rm   rn   r[   r\   s          r   rd   zMigratableDB.apply_migrations   s     	&&(C ..s3M /$$&&&'BC!
 $=0$  c!5I((i8 "6  ) s   B++B4	r+   )r   r   r   __doc__r   r   r
   r(   staticmethodr	   r   rO   r   r   rS   rU   boolrX   r    rZ   r   r^   rf   r   r   ALLrc   rd   r-   r.   s   @r   rG   rG   G   s4   ( !v !$ !
 S     5  
       )1D  
 6 i D  $ 46N6R6RSN TN& 13K3O3OP9 Q9r   rG   z(\d+)-(.+)\.(.+)\.sqlr   r   r   r,   c                     t         j                  |      }|t        d|z         |j                         \  }}}|| |t	        |      |dS )z6Parse a migration filename into a MigrationFile objectzInvalid migration filename: )r   r   r   r   r   )filename_regexmatchrE   groupsr   )r   r   r   rw   r   _r   s          r   _parse_migration_filenamerz      sY       *E}&'E'PQQGQw< r   rZ   rm   c                     t        | |      D ]R  \  }}|d   |d   k7  rt        |d   |d   |d         |d   |d   k7  s4t        |d   dz   |d   z   |d   |d          |t        |       d S )	a  Given a list of migrations already applied to a database, and a list of
    migrations from the source code, validate that the applied migrations are correct
    and match the expected migrations.

    Throws an exception if any migrations are missing, out of order, or if the source
    hash does not match.

    Returns a list of all unapplied migrations, or an empty list if all migrations are
    applied and the database is up to date.r   r   )r   r5   r6   r!   /r   )r   r<   r=   N)zipr4   r;   rk   )rZ   rm   db_migrationsource_migrations       r   rj   rj      s     +.m=N*O&&	"&6y&AA* ''	2/	:  #3F#;;'!%(3.j1II$V,,V4  +P S/122r   r   hash_algc                 F   | j                         D cg c]>  }|j                  j                  d      r!t        | j                  |j                  |      @ }}t	        t        fd|            }t        |d       }|D cg c]  }t        ||       c}S c c}w c c}w )zgReturn a list of all migration present in the given directory, in ascending
    order. Filter by scope.z.sqlc                     | d   k(  S )Nr   r   )fr   s    r   <lambda>z!find_migrations.<locals>.<lambda>   s    !G*"5r   c                     | d   S )Nr   r   )r   s    r   r   z!find_migrations.<locals>.<lambda>   s    )r   )key)iterdirrl   endswithrz   listfiltersorted_read_migration_file)r   r   r   tfilesr   s    `    r   ri   ri      s     A66??6" 	"#((AFFA6 
 
 5u=>E545E7<=u! H-u== >s   ABBfilec                 L   d| vs| d   j                         s#t        d| d    d| d    d| d    d| d	          | d   j                         }|d
k(  rzt        j                  dk\  r4t        j                  |j                  d      d      j                         n1t        j                  |j                  d            j                         }nD|dk(  r3t        j                  |j                  d            j                         }nt        |      ||| d   | d   | d	   | d   dS )zRead a migration filer   z No migration file found for dir r   z with filename r   z and scope r   z at version r   md5)   	   zutf-8F)usedforsecuritysha256)rB   )r!   r"   r   r   r   r   )is_fileFileNotFoundError	read_textsysversion_infohashlibr   encode	hexdigestr   rA   )r   r   r"   r!   s       r   r   r      sK   Tf!5!5!7.tE{m?4PZK[J\\ghlmthugv  wC  DH  IR  DS  CT  U
 	
 v,
 
 
"C5 6) KK

7+UCMMOSZZ01;;= 	
 
X	~~cjj12<<>8,, E{$	?g r   )r   )'r   typingr   typing_extensionsr   r   importlib_resources.abcr   rer   chromadb.db.baser   r   abcr	   chromadb.configr
   r    chromadb.telemetry.opentelemetryr   r   r   r   r    	Exceptionr$   r0   r4   r;   rA   rE   rG   compilerv   r   rz   rj   ri   r   r   r   r   <module>r      sM   
  4 / 	  *  , I  
A9 A

y 

y 

I 
Ey E
	y 	k95 k9` 45	#."3I&3	*3 i3B 38>	> >,/>i>}  	 r   