From 002ca673e77155f746886450c0e1638cd331cd70 Mon Sep 17 00:00:00 2001 From: jason3gb Date: Tue, 22 Jun 2021 01:08:50 +0800 Subject: [PATCH] Fixes: #6646 --- doc/build/changelog/unreleased_14/6646.rst | 8 ++++++++ lib/sqlalchemy/sql/sqltypes.py | 14 +++++++++++++- test/sql/test_types.py | 5 +++++ 3 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 doc/build/changelog/unreleased_14/6646.rst diff --git a/doc/build/changelog/unreleased_14/6646.rst b/doc/build/changelog/unreleased_14/6646.rst new file mode 100644 index 0000000000..76d4aade3e --- /dev/null +++ b/doc/build/changelog/unreleased_14/6646.rst @@ -0,0 +1,8 @@ +.. change:: + :tags: usecase, type + :tickets: 6646 + + Add a impl parameter to :class:`.PickleType` constructor, so the user + can customize the default implementation to avoid the issue that the + default LargeBinary impl might not be big enough to store the binary + object. \ No newline at end of file diff --git a/lib/sqlalchemy/sql/sqltypes.py b/lib/sqlalchemy/sql/sqltypes.py index 4f8654afd9..b700445f39 100644 --- a/lib/sqlalchemy/sql/sqltypes.py +++ b/lib/sqlalchemy/sql/sqltypes.py @@ -1783,7 +1783,11 @@ class PickleType(TypeDecorator): cache_ok = True def __init__( - self, protocol=pickle.HIGHEST_PROTOCOL, pickler=None, comparator=None + self, + protocol=pickle.HIGHEST_PROTOCOL, + pickler=None, + comparator=None, + impl=None, ): """ Construct a PickleType. @@ -1798,12 +1802,20 @@ class PickleType(TypeDecorator): to compare values of this type. If left as ``None``, the Python "equals" operator is used to compare values. + :param impl: Any implementation class that support storing + binary object. (e.g. :class: `.mysql.LONGBLOB`). + If let as ``None``, the impl would use class's default impl + `:class:`.LargeBinary` + """ self.protocol = protocol self.pickler = pickler or pickle self.comparator = comparator super(PickleType, self).__init__() + if impl: + self.impl = to_instance(impl) + def __reduce__(self): return PickleType, (self.protocol, None, self.comparator) diff --git a/test/sql/test_types.py b/test/sql/test_types.py index 9db0fee3b0..d4ef25b4b9 100644 --- a/test/sql/test_types.py +++ b/test/sql/test_types.py @@ -3809,6 +3809,11 @@ class PickleTest(fixtures.TestBase): ): assert p1.compare_values(p1.copy_value(obj), obj) + def test_customized_impl(self): + p1 = PickleType(impl=mysql.LONGBLOB) + + assert isinstance(p1.impl, mysql.LONGBLOB) + class CallableTest(fixtures.TestBase): @testing.provide_metadata -- 2.47.2