diff --git a/sqlalchemy_utils/__init__.py b/sqlalchemy_utils/__init__.py index aa81c5d1..b9eefb2a 100644 --- a/sqlalchemy_utils/__init__.py +++ b/sqlalchemy_utils/__init__.py @@ -89,6 +89,7 @@ ScalarListException, ScalarListType, StringEncryptedType, + TextEncryptedType, TimezoneType, TSVectorType, URLType, diff --git a/sqlalchemy_utils/types/__init__.py b/sqlalchemy_utils/types/__init__.py index 33242a6f..2f26c9fe 100644 --- a/sqlalchemy_utils/types/__init__.py +++ b/sqlalchemy_utils/types/__init__.py @@ -10,7 +10,8 @@ from .email import EmailType # noqa from .encrypted.encrypted_type import ( # noqa EncryptedType, - StringEncryptedType + StringEncryptedType, + TextEncryptedType ) from .enriched_datetime.enriched_date_type import EnrichedDateType # noqa from .ip_address import IPAddressType # noqa diff --git a/sqlalchemy_utils/types/encrypted/encrypted_type.py b/sqlalchemy_utils/types/encrypted/encrypted_type.py index 173bcabc..a10d0942 100644 --- a/sqlalchemy_utils/types/encrypted/encrypted_type.py +++ b/sqlalchemy_utils/types/encrypted/encrypted_type.py @@ -4,7 +4,7 @@ import os import warnings -from sqlalchemy.types import LargeBinary, String, TypeDecorator +from sqlalchemy.types import LargeBinary, String, Text, TypeDecorator from sqlalchemy_utils.exceptions import ImproperlyConfigured from sqlalchemy_utils.types.encrypted.padding import PADDING_MECHANISM @@ -456,6 +456,28 @@ def _coerce(self, value): return value +class TextEncryptedType(StringEncryptedType): + """ + The 'TextEncryptedType' creates a Text column instead of a String column + """ + impl = Text + + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + + def process_bind_param(self, value, dialect): + value = super().process_bind_param(value=value, dialect=dialect) + if isinstance(value, str): + value = value.encode() + return value + + def process_result_value(self, value, dialect): + if isinstance(value, bytes): + value = value.decode() + value = super().process_result_value(value=value, dialect=dialect) + return value + + class EncryptedType(StringEncryptedType): """ The 'EncryptedType' class will change implementation from