-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpeewee_versioned.py
More file actions
274 lines (226 loc) · 10.9 KB
/
peewee_versioned.py
File metadata and controls
274 lines (226 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
'''
Provides a subclass of peewee Module ``VersionModule`` that automatically
adds a *_versions class and connects it to the proper signals
'''
import datetime
from six import with_metaclass # py2 compat
from peewee import (BaseModel, Model, DateTimeField, ForeignKeyField, IntegerField, BooleanField,
PrimaryKeyField, RelationDescriptor)
class MetaModel(BaseModel):
'''
A MetaClass that automatically creates a nested subclass to track changes
The nested subclass is referred to as ``VersionModel``
'''
# Attribute of the parent class where the ``VersionModel`` can be accessed: Parent._VersionModel
_version_model_attr_name = '_VersionModel'
_version_model_name_suffix = 'Version' # Example, People -> PeopleVersion
_version_model_related_name = '_versions' # Example People._versions.get()
_RECURSION_BREAK_TEST = object()
def __new__(self, name, bases, attrs):
# Because the nested VersionModel shares this metaclass, we need to
# test for it and act like :class:`peewee.BaseModel`
if (attrs.pop('_RECURSION_BREAK_TEST', None) or
name == 'VersionedModel'): # We don't want versions for the mixin
VersionModel = BaseModel.__new__(self, name, bases, attrs)
# Because ``VersionModel`` inherits from the initial class
# we need to mask the reference to itself that is inherited to avoid
# infinite recursion and for detection
setattr(VersionModel, self._version_model_attr_name, None)
return VersionModel
# Instantiate the fields we want to add
# These fields will be added to the nested ``VersionModel``
_version_fields = {'_valid_from': DateTimeField(default=datetime.datetime.now),
'_valid_until': DateTimeField(null=True, default=None,),
'_deleted': BooleanField(default=False),
'_original_record': None, # ForeignKeyField. Added later.
'_original_record_id': None, # added later by peewee
'_version_id': IntegerField(default=1, index=True),
'_id': PrimaryKeyField(primary_key=True)} # Make an explicit primary key
# Create the class, create the nested ``VersionModel``, link them together.
for field in attrs.keys():
if field in _version_fields:
raise ValueError('You can not declare the attribute {}. '
'It is automatically created by VersionedModel'.format(field))
# Create the top level ``VersionedModel`` class
new_class = super(MetaModel, self).__new__(self, name, bases, attrs)
# Mung up the attributes for our ``VersionModel``
version_model_attrs = _version_fields.copy()
version_model_attrs['__qualname__'] = name + self._version_model_name_suffix
# Add ForeignKeyField linking to the original record
version_model_attrs['_original_record'] = ForeignKeyField(
new_class, related_name=self._version_model_related_name,
null=True, on_delete="SET NULL"
)
# Mask all ``peewee.RelationDescriptor`` fields to avoid related name conflicts
for field, value in vars(new_class).items():
if isinstance(value, RelationDescriptor):
version_model_attrs[field] = None
# needed to avoid infinite recursion
version_model_attrs['_RECURSION_BREAK_TEST'] = self._RECURSION_BREAK_TEST
# Create the nested ``VersionedModel`` class that inherits from the top level new_class
VersionModel = type(name + self._version_model_name_suffix, # Name
(new_class,), # bases
version_model_attrs) # attributes
# Modify the nested ``VersionedModel``
setattr(VersionModel, '_version_fields', _version_fields)
# Modify the newly created class before returning
setattr(new_class, self._version_model_attr_name, VersionModel)
setattr(new_class, '_version_model_attr_name', self._version_model_attr_name)
return new_class
# Needed to allow subclassing with differing metaclasses. In this case, BaseModel and Type
class VersionedModel(with_metaclass(MetaModel, Model)):
@classmethod
def _is_version_model(cls):
'''
If this class is a nested ``VersionModel`` class created by :class:`MetaModel`
this will return ``True``
:return: bool
'''
return cls._get_version_model() is None
@classmethod
def _get_version_model(cls):
'''
:return: nested ``VersionModel``
'''
version_model = getattr(cls, cls._version_model_attr_name, None)
return version_model
def save(self, *args, **kwargs):
# Default behaviour if this is a ``VersionModel``
# Only update ``VersionModel if something has changed
if (self._is_version_model() or
not self.is_dirty()):
return super(VersionedModel, self).save(*args, **kwargs)
# wrap everything in a transaction: all or none
with self._meta.database.atomic():
# Save the parent
super(VersionedModel, self).save(*args, **kwargs)
# Finalize the previous version
self._finalize_current_version()
# Save the new version
self._create_new_version()
def delete_instance(self, *args, **kwargs):
if not self._is_version_model():
# wrap everything in a transaction: all or none
with self._meta.database.atomic():
# finalize the previous version
self._finalize_current_version()
# create a new version initialized to current values
new_version = self._create_new_version(save=False)
new_version._deleted = True
new_version.save()
# default behaviour
return super(VersionedModel, self).delete_instance(*args, **kwargs)
@classmethod
def create_table(cls, *args, **kwargs):
# create the normal table schema
super(VersionedModel, cls).create_table(*args, **kwargs)
if not cls._is_version_model():
# Create the tables for the nested version model, skip if it is the nested version model
version_model = getattr(cls, cls._version_model_attr_name, None)
version_model.create_table(*args, **kwargs)
@classmethod
def drop_table(cls, *args, **kwargs):
# drop the nested ``VersionModel`` table first
if not cls._is_version_model():
version_model = getattr(cls, cls._version_model_attr_name, None)
version_model.drop_table(*args, **kwargs)
# default behaviour
super(VersionedModel, cls).drop_table(*args, **kwargs)
@property
def version_id(self):
'''
:return: the version_id of the current version or ``None``
'''
if not self._is_version_model():
current_version = self._get_current_version()
return current_version.version_id
else:
return self._version_id
def revert(self, version):
'''
Changes all attributes to match what was saved in ``version``
This, in itself creates a new version.
:param version:
* type ``VersionModel`` match the passed in ``version``
* type int
* positive: ``version`` matches ``VersionModel._version_id``
* negative: negative indexing on ``version``:
-1 matches the previous version,
-2 matches two versions ago etc.
'''
if self._is_version_model():
raise RuntimeError('method revert can not be called on a VersionModel')
VersionModel = self._get_version_model()
if isinstance(version, VersionModel):
version_model = version
elif version >= 0:
version_model = self._versions.filter(VersionModel._version_id == version).get()
else: # version < 0
version_model = (self._versions
.order_by(VersionModel._version_id.desc())
.offset(-version)
.limit(1))[0]
fields_to_copy = self._get_fields_to_copy()
for field in fields_to_copy:
setattr(self, field, getattr(version_model, field))
self.save()
@classmethod
def _get_fields_to_copy(cls):
VersionModel = cls._get_version_model()
version_model_fields_dict = VersionModel._meta.fields
fields = []
for key in version_model_fields_dict.keys():
if key not in VersionModel._version_fields:
fields.append(key)
return fields
def _create_new_version(self, save=True):
'''
Creates a new row of ``VersionModel`` and initializes
it's fields to match the parent.
:param bool save: should the new_version be saved before returning?
:return: the newly created instance of ``VersionModel``
'''
VersionModel = self._get_version_model()
# Increment the version id to be one higher than the previous
try:
old_version = (self._versions
.select()
.order_by(VersionModel._version_id.desc())
.limit(1))[0]
new_version_id = old_version.version_id + 1
except IndexError:
new_version_id = 1
new_version = VersionModel()
fields_to_copy = self._get_fields_to_copy()
for field in fields_to_copy:
setattr(new_version, field, getattr(self, field))
new_version._original_record = self
new_version._version_id = new_version_id
if save is True:
new_version.save()
return new_version
def _get_current_version(self):
'''
:return: current version or ``None`` if not found
'''
VersionModel = self._get_version_model()
try:
current_version = (self._versions.select()
.where(VersionModel._valid_until.is_null())
) # null record
assert(len(current_version) == 1)
return current_version[0]
except VersionModel.DoesNotExist:
return None
except AssertionError:
if len(current_version) == 0:
return None
else:
raise RuntimeError('Problem with the database. '
'More than one current version was found for {}'
.format(self.__class__))
def _finalize_current_version(self):
current_version = self._get_current_version()
if current_version is not None:
current_version._valid_until = datetime.datetime.now()
current_version.save()