~sirn/fanboi2

ref: 834edf0edc5dd633c0ecea16231b6ed2d728476d fanboi2/fanboi2/models/_versioned.py -rw-r--r-- 11.6 KiB
834edf0eKridsada Thanabulpong Massive cleanup in preparation for 0.30 (#25) 3 years ago
                                                                                
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
from collections import OrderedDict

from sqlalchemy import event
from sqlalchemy.ext.declarative import declared_attr
from sqlalchemy.orm import attributes, object_mapper, mapper
from sqlalchemy.orm.exc import UnmappedColumnError
from sqlalchemy.orm.properties import RelationshipProperty
from sqlalchemy.sql import func
from sqlalchemy.sql.schema import Column, Table, ForeignKeyConstraint
from sqlalchemy.sql.sqltypes import Integer, String, DateTime


_versioned_mappers = []


def _is_fk_column(column, table):
    """Returns :type:`True` if column is referencing :attr:`table`.

    :param column: SQLAlchemy column.
    :param table: SQLAlchemy table.
    """
    for fk in column.foreign_keys:
        if fk.references(table):
            return True
    return False


def _is_versioning_column(column):
    """Returns :type:`True` if column belongs to versioning table.

    :param column: SQLAlchemy column to check.
    """
    return "version_meta" in column.info


def _copy_history_column(column):
    """Create a history copy of a SQLAlchemy column. The copied column will
    share the same SQL data type with the original column, but without defaults
    and its unique constraint removed.

    :param column: SQLAlchemy column to copy.
    """
    new_column = column.copy()
    new_column.unique = False
    new_column.default = new_column.server_default = None
    if new_column.primary_key:
        new_column.autoincrement = False
    column.info['history_copy'] = new_column
    return new_column


def _history_mapper(model_mapper):
    """Configure SQLAlchemy mapper and enable history support.

    :param model_mapper: SQLAlchemy mapper object for a model.
    """
    model_class = model_mapper.class_
    model_table = model_mapper.local_table

    for prop in model_mapper.iterate_properties:
        getattr(model_class, prop.key).impl.active_history = True

    super_mapper = model_mapper.inherits
    super_history_mapper = getattr(model_class, '__history_mapper__', None)
    super_fks = []

    properties = OrderedDict()
    polymorphic_on = None

    if not super_mapper or model_table is not super_mapper.local_table:
        version_meta = {"version_meta": True}
        new_columns = []

        for column in model_table.c:
            if _is_versioning_column(column):
                continue

            try:
                original_prop = model_mapper.get_property_by_column(column)
            except UnmappedColumnError:
                continue

            new_column = _copy_history_column(column)
            new_columns.append(new_column)

            if super_mapper and \
               _is_fk_column(column, super_mapper.local_table):
                super_fks.append(
                    (new_column.key,
                     list(super_history_mapper.local_table.primary_key)[0]))

            if column is model_mapper.polymorphic_on:
                polymorphic_on = new_column

            if len(original_prop.columns) > 1 or \
               original_prop.columns[0].key != original_prop.key:
                properties[original_prop.key] = tuple(
                    c.info['history_copy'] for c in original_prop.columns)

        if super_mapper:
            super_fks.append((
                'version',
                super_history_mapper.local_table.c.version))

        new_columns.append(
            Column('version',
                   Integer,
                   primary_key=True,
                   autoincrement=False,
                   nullable=False,
                   info=version_meta))

        new_columns.append(
            Column('change_type',
                   String,
                   nullable=False,
                   info=version_meta))

        new_columns.append(
            Column('changed_at',
                   DateTime(timezone=True),
                   default=func.now(),
                   nullable=False,
                   info=version_meta))

        if super_fks:
            new_columns.append(ForeignKeyConstraint(*zip(*super_fks)))

        new_table = Table(
            model_table.name + '_history',
            model_table.metadata,
            *new_columns,
            schema=model_table.schema)
    else:
        for column in model_mapper.c:
            if column.key not in super_history_mapper.local_table.c:
                new_column = _copy_history_column(column)
                super_history_mapper.local_table.append_column(new_column)
        new_table = None

    if super_history_mapper:
        bases = (super_history_mapper.class_,)
        if new_table is not None:
            properties['change_type'] = (
                (new_table.c.change_type,) + tuple(
                    super_history_mapper.attrs.change_type.columns))
            properties['changed_at'] = (
                (new_table.c.changed_at,) + tuple(
                    super_history_mapper.attrs.changed_at.columns))
    else:
        bases = model_mapper.base_mapper.class_.__bases__

    model_class.__history_mapper__ = mapper(
        type.__new__(type, "%sHistory" % (model_class.__name__), bases, {}),
        new_table,
        inherits=super_history_mapper,
        polymorphic_on=polymorphic_on,
        polymorphic_identity=model_mapper.polymorphic_identity,
        properties=properties)

    if not super_history_mapper:
        model_table.append_column(
            Column('version',
                   Integer,
                   default=1,
                   nullable=False))
        model_mapper.add_property('version', model_table.c.version)


def setup_versioned(retrieve=None):
    """Enable the versioned for the mapper. If ``retrieve`` is given
    the function will be used for retrieving a list of mapper objects
    (see also :func:`make_versioned_class`).

    :param retrieve: Function for retrieve a list of mapper objects.
    """
    if retrieve is None:
        retrieve = lambda: _versioned_mappers  # noqa: E731

    for model_mapper in retrieve():
        _history_mapper(model_mapper)


def make_versioned_class(register=None):
    """Factory for creating a Versioned mixin. If ``register`` is given, the
    function will be used for registering the mapper object (see also
    :func:`setup_versioned`).

    :param register: Function for registering a mapper object.
    """
    if register is None:
        register = lambda m: _versioned_mappers.append(m)  # noqa: E731

    class _Versioned(object):
        """Mixin for enabling versioning for a model."""

        @declared_attr
        def __mapper_cls__(cls):
            def _map(cls, *args, **kwargs):
                model_mapper = mapper(cls, *args, **kwargs)
                register(model_mapper)
                return model_mapper
            return _map

    return _Versioned


def _is_versioned_object(obj):
    """Returns `True` if object is version-enabled.

    :param obj: SQLAlchemy model object.
    """
    return hasattr(obj, '__history_mapper__')


def _create_version(obj, session, type_=None, force=False):
    """Create a new version for the given :attr:`obj`.

    :param obj: SQLAlchemy model object.
    :param session: SQLAlchemy session object.
    :param type_: Type of a change.
    :param force: Flag to always create version.
    """
    obj_mapper = object_mapper(obj)
    history_mapper = obj.__history_mapper__
    history_class = history_mapper.class_

    obj_state = attributes.instance_state(obj)
    obj_changed = False
    attr = {}

    for obj_mapper_, history_mapper_ in zip(
            obj_mapper.iterate_to_root(),
            history_mapper.iterate_to_root()):

        if history_mapper_.single:
            continue

        for history_column in history_mapper_.local_table.c:
            if _is_versioning_column(history_column):
                continue

            obj_column = obj_mapper_.local_table.c[history_column.key]

            try:
                prop = obj_mapper.get_property_by_column(obj_column)
            except UnmappedColumnError:
                continue

            # Force deferred columns to load.
            if prop.key not in obj_state.dict:
                getattr(obj, prop.key)

            added_, unchanged_, deleted_ = attributes.get_history(
                obj,
                prop.key)

            if deleted_:
                attr[prop.key] = deleted_[0]
                obj_changed = True
            elif unchanged_:
                attr[prop.key] = unchanged_[0]
            elif added_:  # pragma: no cover
                obj_changed = True

    if not obj_changed:
        for prop in obj_mapper.iterate_properties:
            if isinstance(prop, RelationshipProperty) and \
               attributes.get_history(
                   obj,
                   prop.key,
                   passive=attributes.PASSIVE_NO_INITIALIZE).has_changes():
                for p in prop.local_columns:
                    if p.foreign_keys:
                        obj_changed = True
                        break
                if obj_changed is True:
                    break

    if not obj_changed and not force:  # pragma: no cover
        return

    attr['version'] = obj.version
    attr['change_type'] = type_
    history = history_class()
    for key, value in attr.items():
        setattr(history, key, value)

    session.add(history)
    obj.version += 1


def _create_history_dirty(session, objs):
    """Create a new version for dirty objects.

    :param session: SQLAlchemy sesion object.
    :param objs: Dirty objects usually obtained by ``session.dirty``
    """
    for obj in objs:
        if _is_versioned_object(obj):
            _create_version(obj, session, type_='update')


def _create_history_deleted(session, objs):
    """Create a new version for deleted objects. This method will also create
    a new version for relation that do not have ``cascade`` parameter set in
    which SQLAlchemy will default to set its foreign keys to ``NULL``.

    :param session: SQLAlchemy sesion object.
    :param objs: Dirty objects usually obtained by ``session.deleted``
    """
    for obj in objs:
        if _is_versioned_object(obj):
            _create_version(obj, session, type_='delete', force=True)

        # Also handle NULL fks from SQLAlchemy Cascades. See also:
        # 6e1f34 lib/sqlalchemy/orm/dependency.py#L423-L424
        obj_mapper = object_mapper(obj)
        related = None

        def _create_cascade_version(target_obj):
            if target_obj not in objs:
                _create_version(target_obj,
                                session,
                                type_='update.cascade',
                                force=True)

        for prop in obj_mapper.iterate_properties:
            if isinstance(prop, RelationshipProperty) and \
               not prop.cascade.delete and \
               not prop.passive_deletes == 'all' and \
               _is_versioned_object(prop.mapper.class_):
                skip_history = False
                for c in prop.local_columns:
                    if c.foreign_keys:
                        skip_history = True
                        break
                if skip_history:
                    continue
                related = getattr(obj, prop.key)
                try:
                    for target_obj in related:
                        _create_cascade_version(target_obj)
                except TypeError:  # pragma: no cover
                    _create_cascade_version(related)


def make_history_event(session):
    """Registers an event for recording versioned object.

    :param session: SQLAlchemy session object.
    """
    @event.listens_for(session, 'before_flush')
    def update_history(session_, context, instances):
        _create_history_dirty(session_, session_.dirty)
        _create_history_deleted(session_, session_.deleted)
    return session