use TEXT for all backends
This commit is contained in:
@ -58,7 +58,6 @@ class BaseField(FieldInfo):
|
|||||||
encrypt_secret: str
|
encrypt_secret: str
|
||||||
encrypt_backend: EncryptBackends = EncryptBackends.NONE
|
encrypt_backend: EncryptBackends = EncryptBackends.NONE
|
||||||
encrypt_custom_backend: Optional[Type[EncryptBackend]] = None
|
encrypt_custom_backend: Optional[Type[EncryptBackend]] = None
|
||||||
encrypt_max_length: int = 5000
|
|
||||||
|
|
||||||
default: Any
|
default: Any
|
||||||
server_default: Any
|
server_default: Any
|
||||||
@ -304,7 +303,6 @@ class BaseField(FieldInfo):
|
|||||||
encrypt_secret=cls.encrypt_secret,
|
encrypt_secret=cls.encrypt_secret,
|
||||||
encrypt_backend=cls.encrypt_backend,
|
encrypt_backend=cls.encrypt_backend,
|
||||||
encrypt_custom_backend=cls.encrypt_custom_backend,
|
encrypt_custom_backend=cls.encrypt_custom_backend,
|
||||||
encrypt_max_length=cls.encrypt_max_length,
|
|
||||||
),
|
),
|
||||||
nullable=cls.nullable,
|
nullable=cls.nullable,
|
||||||
index=cls.index,
|
index=cls.index,
|
||||||
|
|||||||
@ -189,14 +189,12 @@ def ForeignKey( # noqa CFQ002
|
|||||||
encrypt_secret = kwargs.pop("encrypt_secret", None)
|
encrypt_secret = kwargs.pop("encrypt_secret", None)
|
||||||
encrypt_backend = kwargs.pop("encrypt_backend", None)
|
encrypt_backend = kwargs.pop("encrypt_backend", None)
|
||||||
encrypt_custom_backend = kwargs.pop("encrypt_custom_backend", None)
|
encrypt_custom_backend = kwargs.pop("encrypt_custom_backend", None)
|
||||||
encrypt_max_length = kwargs.pop("encrypt_max_length", None)
|
|
||||||
|
|
||||||
not_supported = [
|
not_supported = [
|
||||||
default,
|
default,
|
||||||
encrypt_secret,
|
encrypt_secret,
|
||||||
encrypt_backend,
|
encrypt_backend,
|
||||||
encrypt_custom_backend,
|
encrypt_custom_backend,
|
||||||
encrypt_max_length,
|
|
||||||
]
|
]
|
||||||
if any(x is not None for x in not_supported):
|
if any(x is not None for x in not_supported):
|
||||||
raise ModelDefinitionError(
|
raise ModelDefinitionError(
|
||||||
|
|||||||
@ -100,14 +100,12 @@ def ManyToMany(
|
|||||||
encrypt_secret = kwargs.pop("encrypt_secret", None)
|
encrypt_secret = kwargs.pop("encrypt_secret", None)
|
||||||
encrypt_backend = kwargs.pop("encrypt_backend", None)
|
encrypt_backend = kwargs.pop("encrypt_backend", None)
|
||||||
encrypt_custom_backend = kwargs.pop("encrypt_custom_backend", None)
|
encrypt_custom_backend = kwargs.pop("encrypt_custom_backend", None)
|
||||||
encrypt_max_length = kwargs.pop("encrypt_max_length", None)
|
|
||||||
|
|
||||||
not_supported = [
|
not_supported = [
|
||||||
default,
|
default,
|
||||||
encrypt_secret,
|
encrypt_secret,
|
||||||
encrypt_backend,
|
encrypt_backend,
|
||||||
encrypt_custom_backend,
|
encrypt_custom_backend,
|
||||||
encrypt_max_length,
|
|
||||||
]
|
]
|
||||||
if any(x is not None for x in not_supported):
|
if any(x is not None for x in not_supported):
|
||||||
raise ModelDefinitionError(
|
raise ModelDefinitionError(
|
||||||
|
|||||||
@ -124,7 +124,6 @@ class EncryptedString(types.TypeDecorator):
|
|||||||
**kwargs: Any,
|
**kwargs: Any,
|
||||||
) -> None:
|
) -> None:
|
||||||
_field_type = kwargs.pop("_field_type")
|
_field_type = kwargs.pop("_field_type")
|
||||||
encrypt_max_length = kwargs.pop("encrypt_max_length", 5000)
|
|
||||||
super().__init__()
|
super().__init__()
|
||||||
if not cryptography: # pragma: nocover
|
if not cryptography: # pragma: nocover
|
||||||
raise ModelDefinitionError(
|
raise ModelDefinitionError(
|
||||||
@ -138,7 +137,6 @@ class EncryptedString(types.TypeDecorator):
|
|||||||
self._field_type: Type["BaseField"] = _field_type
|
self._field_type: Type["BaseField"] = _field_type
|
||||||
self._underlying_type: Any = _field_type.column_type
|
self._underlying_type: Any = _field_type.column_type
|
||||||
self._key: Union[str, Callable] = encrypt_secret
|
self._key: Union[str, Callable] = encrypt_secret
|
||||||
self.max_length: int = encrypt_max_length
|
|
||||||
type_ = self._field_type.__type__
|
type_ = self._field_type.__type__
|
||||||
if type_ is None: # pragma: nocover
|
if type_ is None: # pragma: nocover
|
||||||
raise ModelDefinitionError(
|
raise ModelDefinitionError(
|
||||||
@ -147,12 +145,10 @@ class EncryptedString(types.TypeDecorator):
|
|||||||
self.type_: Any = type_
|
self.type_: Any = type_
|
||||||
|
|
||||||
def __repr__(self) -> str: # pragma: nocover
|
def __repr__(self) -> str: # pragma: nocover
|
||||||
return f"VARCHAR({self.max_length})"
|
return f"TEXT()"
|
||||||
|
|
||||||
def load_dialect_impl(self, dialect: DefaultDialect) -> Any:
|
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.TEXT())
|
|
||||||
return dialect.type_descriptor(types.VARCHAR(self.max_length))
|
|
||||||
|
|
||||||
def _refresh(self) -> None:
|
def _refresh(self) -> None:
|
||||||
key = self._key() if callable(self._key) else self._key
|
key = self._key() if callable(self._key) else self._key
|
||||||
|
|||||||
@ -54,7 +54,6 @@ class Author(ormar.Model):
|
|||||||
birth_year: int = ormar.Integer(
|
birth_year: int = ormar.Integer(
|
||||||
nullable=True,
|
nullable=True,
|
||||||
encrypt_secret="secure89key%^&psdijfipew",
|
encrypt_secret="secure89key%^&psdijfipew",
|
||||||
encrypt_max_length=200,
|
|
||||||
encrypt_backend=ormar.EncryptBackends.FERNET,
|
encrypt_backend=ormar.EncryptBackends.FERNET,
|
||||||
)
|
)
|
||||||
test_text: str = ormar.Text(default="", **default_fernet)
|
test_text: str = ormar.Text(default="", **default_fernet)
|
||||||
@ -146,7 +145,6 @@ def test_wrong_backend():
|
|||||||
|
|
||||||
def test_db_structure():
|
def test_db_structure():
|
||||||
assert Author.Meta.table.c.get("name").type.__class__ == EncryptedString
|
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
|
@pytest.mark.asyncio
|
||||||
|
|||||||
Reference in New Issue
Block a user