use TEXT for all backends

This commit is contained in:
collerek
2021-03-09 20:41:17 +01:00
parent fbde6d4624
commit 082405d8ef
5 changed files with 2 additions and 14 deletions

View File

@ -58,7 +58,6 @@ class BaseField(FieldInfo):
encrypt_secret: str
encrypt_backend: EncryptBackends = EncryptBackends.NONE
encrypt_custom_backend: Optional[Type[EncryptBackend]] = None
encrypt_max_length: int = 5000
default: Any
server_default: Any
@ -304,7 +303,6 @@ class BaseField(FieldInfo):
encrypt_secret=cls.encrypt_secret,
encrypt_backend=cls.encrypt_backend,
encrypt_custom_backend=cls.encrypt_custom_backend,
encrypt_max_length=cls.encrypt_max_length,
),
nullable=cls.nullable,
index=cls.index,

View File

@ -189,14 +189,12 @@ def ForeignKey( # noqa CFQ002
encrypt_secret = kwargs.pop("encrypt_secret", None)
encrypt_backend = kwargs.pop("encrypt_backend", None)
encrypt_custom_backend = kwargs.pop("encrypt_custom_backend", None)
encrypt_max_length = kwargs.pop("encrypt_max_length", None)
not_supported = [
default,
encrypt_secret,
encrypt_backend,
encrypt_custom_backend,
encrypt_max_length,
]
if any(x is not None for x in not_supported):
raise ModelDefinitionError(

View File

@ -100,14 +100,12 @@ def ManyToMany(
encrypt_secret = kwargs.pop("encrypt_secret", None)
encrypt_backend = kwargs.pop("encrypt_backend", None)
encrypt_custom_backend = kwargs.pop("encrypt_custom_backend", None)
encrypt_max_length = kwargs.pop("encrypt_max_length", None)
not_supported = [
default,
encrypt_secret,
encrypt_backend,
encrypt_custom_backend,
encrypt_max_length,
]
if any(x is not None for x in not_supported):
raise ModelDefinitionError(

View File

@ -124,7 +124,6 @@ class EncryptedString(types.TypeDecorator):
**kwargs: Any,
) -> None:
_field_type = kwargs.pop("_field_type")
encrypt_max_length = kwargs.pop("encrypt_max_length", 5000)
super().__init__()
if not cryptography: # pragma: nocover
raise ModelDefinitionError(
@ -138,7 +137,6 @@ class EncryptedString(types.TypeDecorator):
self._field_type: Type["BaseField"] = _field_type
self._underlying_type: Any = _field_type.column_type
self._key: Union[str, Callable] = encrypt_secret
self.max_length: int = encrypt_max_length
type_ = self._field_type.__type__
if type_ is None: # pragma: nocover
raise ModelDefinitionError(
@ -147,12 +145,10 @@ class EncryptedString(types.TypeDecorator):
self.type_: Any = type_
def __repr__(self) -> str: # pragma: nocover
return f"VARCHAR({self.max_length})"
return f"TEXT()"
def load_dialect_impl(self, dialect: DefaultDialect) -> Any:
if dialect.name == 'mysql': # pragma: nocover
return dialect.type_descriptor(types.TEXT())
return dialect.type_descriptor(types.VARCHAR(self.max_length))
def _refresh(self) -> None:
key = self._key() if callable(self._key) else self._key

View File

@ -54,7 +54,6 @@ class Author(ormar.Model):
birth_year: int = ormar.Integer(
nullable=True,
encrypt_secret="secure89key%^&psdijfipew",
encrypt_max_length=200,
encrypt_backend=ormar.EncryptBackends.FERNET,
)
test_text: str = ormar.Text(default="", **default_fernet)
@ -146,7 +145,6 @@ def test_wrong_backend():
def test_db_structure():
assert Author.Meta.table.c.get("name").type.__class__ == EncryptedString
assert Author.Meta.table.c.get("birth_year").type.max_length == 200
@pytest.mark.asyncio