� ���g�%� �.�ddlZddlmZddlmZmZddlmZddlZddl Z ddl m Z m Z ddl mZddlmZmZddlmZmZmZGd �d e��ZGd �d e��ZGd �de��ZGd�de��ZGd�de��ZGd�de��ZGd�de��ZGd�de��ZGd�de ��Zej d��Z!de"de"dedefd �Z#d!eed"eedeefd#�Z$ d*ded%e"d&e"deefd'�Z%d(ed&e"defd)�Z&dS)+�N)�Sequence)� TypedDict� NotRequired)� Traversable)�SqlDB�Cursor)�abstractmethod)�System�Settings)�OpenTelemetryClient�OpenTelemetryGranularity� trace_methodc�N�eZdZUeeed<eed<eed<eed<eed<dS)� MigrationFile�path�dir�filename�version�scopeN)�__name__� __module__� __qualname__rr�__annotations__�str�int���f/home/asafur/pinokio/api/open-webui.git/app/env/lib/python3.11/site-packages/chromadb/db/migrations.pyrrsE������� �k� "�"�"�"� �H�H�H��M�M�M� �L�L�L� �J�J�J�J�Jrrc�$�eZdZUeed<eed<dS)� Migration�hash�sqlN)rrrrrrrrr r s"������� �I�I�I� �H�H�H�H�Hrr c� ��eZdZd�fd� Z�xZS)�UninitializedMigrationsError�returnNc�J��t���d��dS)Nz$Migrations have not been initialized��super�__init__)�self� __class__s �rr)z%UninitializedMigrationsError.__init__s"��� �����?�@�@�@�@�@r�r%N)rrrr)� __classcell__�r+s@rr$r$sG�������A�A�A�A�A�A�A�A�A�Arr$c�(��eZdZdedef�fd� Z�xZS)�UnappliedMigrationsErrorrrc�r��||_||_t���d|�d|����dS)NzUnapplied migrations in z, starting with version )rrr(r))r*rrr+s �rr)z!UnappliedMigrationsError.__init__$sJ�������� � ����� M�s� M� M�G� M� M� � � � � r�rrrrrr)r-r.s@rr0r0#sK������� �C� �#� � � � � � � � � � rr0c�,��eZdZdededef�fd� Z�xZS)�InconsistentVersionErrorr� db_version�source_versionc�l��t���d|�d�d|�d|�d�zdz��dS)Nz#Inconsistent migration versions in �:zdb version was z, source version was �.zH Has the migration sequence been modified since being applied to the DB?r')r*rr5r6r+s �rr)z!InconsistentVersionError.__init__-s\��� ����� 8�#� 8� 8� 8�R� �R�R��R�R�R� S�X� Y� � � � � rr2r.s@rr4r4,sR������� �C� �S� �#� � � � � � � � � � rr4c�,��eZdZdededef�fd� Z�xZS)�InconsistentHashErrorr�db_hash� source_hashc�l��t���d|�d�d|�d|�d�zdz��dS)NzInconsistent hashes in r8z db hash was z, source has was r9z? Was the migration file modified after being applied to the DB?r')r*rr<r=r+s �rr)zInconsistentHashError.__init__6s\��� ����� -�d� -� -� -�E�W�E�E�{�E�E�E� F�O� P� � � � � r�rrrrr)r-r.s@rr;r;5sR������� �S� �3� �S� � � � � � � � � � rr;c�$��eZdZdef�fd� Z�xZS)�InvalidHashError�algc�P��t���d|����dS)Nz"Invalid hash algorithm specified: r')r*rBr+s �rr)zInvalidHashError.__init__?s*��� �����C�c�C�C�D�D�D�D�Drr?r.s@rrArA>sO�������E�C�E�E�E�E�E�E�E�E�E�ErrAc��eZdZdS)�InvalidMigrationFilenameN)rrrrrrrErECs�������DrrEc���eZdZUdZeed<deddf�fd� Zee de fd�����Z e de e fd���Ze dd ���Ze defd ���Ze d e de efd ���Ze d ededdfd���Zdd�Zedej��dd���Zedej��dd���Z�xZS)� 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���|j|_|�t��|_t ���|��dS)N)�settingsrH�requirer �_opentelemetry_clientr(r))r*rIr+s �rr)zMigratableDB.__init__^s?�������%+�^�^�4G�%H�%H��"� ������ � � � � rc��dS)zFThe database implementation to use for migrations (e.g, sqlite, pgsql)Nrrrr�migration_scopezMigratableDB.migration_scopec� �� �rc��dS)zYDirectories containing the migration sequences that should be applied to this DB.Nr�r*s r�migration_dirszMigratableDB.migration_dirsirPrc��dS)z)Idempotently creates the migrations tableNrrRs r�setup_migrationszMigratableDB.setup_migrationso� �� �rc��dS)z*Return true if the migrations table existsNrrRs r�migrations_initializedz#MigratableDB.migrations_initializedtrVrrc��dS)z~Return a list of all migrations already applied to this database, from the given source directory, in ascending order.Nr)r*rs r� db_migrationszMigratableDB.db_migrationsyrPr�cur� migrationc��dS)z(Apply a single migration to the databaseNr)r*r[r\s r�apply_migrationzMigratableDB.apply_migrationrVrc��|j�d��}|dkr|���|dkr|���dSdS)z!Initialize migrations for this DB� migrations�validate�applyN)rHrL�validate_migrations�apply_migrations)r*�migrates r�initialize_migrationsz"MigratableDB.initialize_migrations�s_���.�(�(��6�6�� �j� � � � $� $� &� &� &� �g� � � � !� !� #� #� #� #� #� � rz MigratableDB.validate_migrationsc��|���st���|���D]�}|�|��}t ||���|j�d����}t||��}t|��dkr$|dd}t|j |������dS)zpValidate all migrations and throw an exception if there are any unapplied migrations in the source repo.�migrations_hash_algorithmrr)rrN) rXr$rSrZ�find_migrationsrOrHrL�verify_migration_sequence�lenr0�name)r*rrZ�source_migrations�unapplied_migrationsrs rrcz MigratableDB.validate_migrations�s����*�*�,�,� 1�.�0�0� 0��&�&�(�(� N� N�C� �.�.�s�3�3�M� /���$�$�&�&���&�&�'B�C�C�!�!� � $=��0�$�$� ��'�(�(�1�,�,�.�q�1�)�<��.�3�8�W�M�M�M�M�-� N� NrzMigratableDB.apply_migrationsc��|���|���D]�}|�|��}t||���|j�d����}t||��}|���5}|D]}|� ||��� ddd��n #1swxYwY��dS)z5Validate existing migrations, and apply all new ones.rhN) rUrSrZrirOrHrLrj�txr^)r*rrZrmrnr[r\s rrdzMigratableDB.apply_migrations�s,�� �������&�&�(�(� 9� 9�C� �.�.�s�3�3�M� /���$�$�&�&���&�&�'B�C�C�!�!� � $=��0�$�$� ������ 9�c�!5�9�9�I��(�(��i�8�8�8�8�9� 9� 9� 9� 9� 9� 9� 9� 9� 9� 9� 9���� 9� 9� 9� 9�� 9� 9s�C�C �C r,)rrr�__doc__r rr r)� staticmethodr rrOrrrSrU�boolrXr rZrr^rfrr �ALLrcrdr-r.s@rrGrGGs�����������(����!�v�!�$�!�!�!�!�!�!� �� �S� � � ��^��\� �� ��� 5� � � ��^� � � � � ��^� �� �� � � ��^� �� �� ��)�1D� � � ��^� � � �6� �i� �D� � � ��^� �$�$�$�$��\�4�6N�6R�S�S�N�N�N�T�S�N�&�\�1�3K�3O�P�P�9�9�9�Q�P�9�9�9�9�9rrGz(\d+)-(.+)\.(.+)\.sqlrrrr%c��t�|��}|�td|z���|���\}}}|||t |��|d�S)z6Parse a migration filename into a MigrationFile objectNzInvalid migration filename: )rrrrr)�filename_regex�matchrE�groupsr)rrrrwr�_rs r�_parse_migration_filenamerz�sh�� � � �� *� *�E� �}�&�'E��'P�Q�Q�Q�� � ����G�Q������w�<�<��  � �rrZrmc�L�t||��D]}\}}|d|dkr$t|d|d|d����|d|dkr0t|ddz|dz|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.rr)rr5r6r!�/r)rr<r=N)�zipr4r;rk)rZrm� db_migration�source_migrations rrjrj�s���+.�m�=N�*O�*O� � �&� �&� � � "�&6�y�&A� A� A�*� ��'�'� �2�/� �:���� � �� �#3�F�#;� ;� ;�'�!�%�(�3�.��j�1I�I�$�V�,�,�V�4���� � <� �S��/�/�1�1� 2�2r�md5r�hash_algc�������fd�����D��}tt�fd�|����}t|d����}�fd�|D��S)zgReturn a list of all migration present in the given directory, in ascending order. Filter by scope.c�z��g|]7}|j�d���t�j|j|����8S)z.sql)rl�endswithrz)�.0�trs �r� <listcomp>z#find_migrations.<locals>.<listcomp>�sM��� � � � � �6�?�?�6� "� "� �!�#�(�A�F�A�6�6� � � rc���|d�kS)Nrr)�frs �r�<lambda>z!find_migrations.<locals>.<lambda>�s���!�G�*��"5�rc��|dS)Nrr)r�s rr�z!find_migrations.<locals>.<lambda>�s ���)� �r)�keyc�0��g|]}t|�����Sr)�_read_migration_file)r�r�r�s �rr�z#find_migrations.<locals>.<listcomp>�s$��� =� =� =�!� ��H� -� -� =� =� =r)�iterdir�list�filter�sorted)rrr��filess``` rriri�s������  � � � ������ � � �E� ��5�5�5�5�u�=�=� >� >�E� �5�4�4� 5� 5� 5�E� =� =� =� =�u� =� =� =�=r�filec ���d|vs|d���s3td|d�d|d�d|d�d|d �����|d���}|d kr�tjd kr;t j|�d ��d ������n8t j|�d �������}nP|dkr:t j |�d �������}nt|����|||d|d|d |dd�S)zRead a migration filerz No migration file found for dir rz with filename rz and scope rz at version rr�)�� zutf-8F)�usedforsecurity�sha256)rB)r!r"rrrr) �is_file�FileNotFoundError� read_text�sys� version_info�hashlibr��encode� hexdigestr�rA)r�r�r"r!s rr�r��s��� �T����f��!5�!5�!7�!7��� U�t�E�{� U� U�4�PZ�K[� U� U�hl�mt�hu� U� U�DH�IR�DS� U� U� � � � �v�,� � � "� "�C��5�����6�)�)� �K�� � �7�+�+�U� C� C� C� M� M� O� O� O���S�Z�Z��0�0�1�1�;�;�=�=� �� �X� � ��~�c�j�j��1�1�2�2�<�<�>�>����8�,�,�,�,����E�{���$�� �?��g��  � �r)r�)'r��typingr�typing_extensionsrr�importlib_resources.abcr�rer��chromadb.db.baserr�abcr �chromadb.configr r � chromadb.telemetry.opentelemetryr r rrr � Exceptionr$r0r4r;rArErG�compilervrrzrjrir�rrr�<module>r�s_�� � � � �������4�4�4�4�4�4�4�4�/�/�/�/�/�/� � � � �����*�*�*�*�*�*�*�*�������,�,�,�,�,�,�,�,����������������I���� � � � � � � � � � A�A�A�A�A�9�A�A�A�  � � � � �y� � � � � � � � �y� � � � � � � � �I� � � �E�E�E�E�E�y�E�E�E�  � � � � �y� � � �k9�k9�k9�k9�k9�5�k9�k9�k9�`���4�5�5��� ����#.�������"3��I�&�3�� �*�3��i��3�3�3�3�B38� >� >� � >� � >�,/� >� �i�� >� >� >� >��}���� ������r
Memory