SoftDeleteMixin: allow for None values

This is a fixup for I13c6233cfeb611b1b106eedfc9b57d2af313c46b to
address the remaining comments:

  - allow for None values to be passed (as column deleted is nullable)

  - document why column deleted is not of type boolean

Change-Id: I04f690f7a1bb8cb14f1e3322643a8cf8bc9c549a
This commit is contained in:
Roman Podoliaka 2016-11-28 18:06:06 +02:00
parent 8be136528e
commit 5b68df20fb
2 changed files with 22 additions and 1 deletions

View File

@ -84,9 +84,20 @@ class SoftDeleteInteger(TypeDecorator):
layer by the means of a custom SQLAlchemy type decorator makes sure we layer by the means of a custom SQLAlchemy type decorator makes sure we
always pass a proper integer value to a DBAPI implementation. always pass a proper integer value to a DBAPI implementation.
This is not a general purpose boolean integer type as it specifically
allows for arbitrary positive integers outside of the boolean int range
(0, 1, False, True), so that it's possible to have compound unique
constraints over multiple columns including ``deleted`` (e.g. to
soft-delete flavors with the same name in Nova without triggering
a constraint violation): ``deleted`` is set to be equal to a PK
int value on deletion, 0 denotes a non-deleted row.
""" """
impl = Integer impl = Integer
def process_bind_param(self, value, dialect): def process_bind_param(self, value, dialect):
if value is None:
return None
else:
return int(value) return int(value)

View File

@ -229,3 +229,13 @@ class SoftDeleteMixinTest(test_base.DbTestCase):
m = SoftDeletedModel(id=1, smth='test', deleted=False) m = SoftDeletedModel(id=1, smth='test', deleted=False)
self.session.add(m) self.session.add(m)
self.session.commit() self.session.commit()
def test_deleted_set_to_null(self):
m = SoftDeletedModel(id=123456, smth='test')
self.session.add(m)
self.session.commit()
m.deleted = None
self.session.commit()
self.assertIsNone(m.deleted)