Skip to content

Commit

Permalink
- formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
zzzeek committed Nov 26, 2014
1 parent 028f7b8 commit a88be57
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 53 deletions.
59 changes: 42 additions & 17 deletions examples/versioned_history/history_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,18 @@
import datetime
from sqlalchemy.orm.properties import RelationshipProperty


def col_references_table(col, table):
for fk in col.foreign_keys:
if fk.references(table):
return True
return False


def _is_versioning_col(col):
return "version_meta" in col.info


def _history_mapper(local_mapper):
cls = local_mapper.class_

Expand All @@ -38,32 +41,45 @@ def _col_copy(col):
col.default = col.server_default = None
return col

if not super_mapper or local_mapper.local_table is not super_mapper.local_table:
if not super_mapper or \
local_mapper.local_table is not super_mapper.local_table:
cols = []
for column in local_mapper.local_table.c:
if _is_versioning_col(column):
continue

col = _col_copy(column)

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

cols.append(col)

if column is local_mapper.polymorphic_on:
polymorphic_on = col

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

version_meta = {"version_meta": True} # add column.info to identify
# columns specific to versioning

# "version" stores the integer version id. This column is
# required.
cols.append(Column('version', Integer, primary_key=True,
autoincrement=False, info=version_meta))
cols.append(
Column(
'version', Integer, primary_key=True,
autoincrement=False, info=version_meta))

# "changed" column stores the UTC timestamp of when the
# history row was created.
Expand All @@ -75,10 +91,11 @@ def _col_copy(col):
if super_fks:
cols.append(ForeignKeyConstraint(*zip(*super_fks)))

table = Table(local_mapper.local_table.name + '_history',
local_mapper.local_table.metadata,
*cols,
schema=local_mapper.local_table.schema
table = Table(
local_mapper.local_table.name + '_history',
local_mapper.local_table.metadata,
*cols,
schema=local_mapper.local_table.schema
)
else:
# single table inheritance. take any additional columns that may have
Expand Down Expand Up @@ -108,7 +125,8 @@ def _col_copy(col):
local_mapper.local_table.append_column(
Column('version', Integer, default=1, nullable=False)
)
local_mapper.add_property("version", local_mapper.local_table.c.version)
local_mapper.add_property(
"version", local_mapper.local_table.c.version)


class Versioned(object):
Expand All @@ -126,6 +144,7 @@ def versioned_objects(iter):
if hasattr(obj, '__history_mapper__'):
yield obj


def create_version(obj, session, deleted=False):
obj_mapper = object_mapper(obj)
history_mapper = obj.__history_mapper__
Expand All @@ -137,7 +156,10 @@ def create_version(obj, session, deleted=False):

obj_changed = False

for om, hm in zip(obj_mapper.iterate_to_root(), history_mapper.iterate_to_root()):
for om, hm in zip(
obj_mapper.iterate_to_root(),
history_mapper.iterate_to_root()
):
if hm.single:
continue

Expand All @@ -157,11 +179,12 @@ def create_version(obj, session, deleted=False):
# in the case of single table inheritance, there may be
# columns on the mapped table intended for the subclass only.
# the "unmapped" status of the subclass column on the
# base class is a feature of the declarative module as of sqla 0.5.2.
# base class is a feature of the declarative module.
continue

# expired object attributes and also deferred cols might not be in the
# dict. force it to load no matter what by using getattr().
# expired object attributes and also deferred cols might not
# be in the dict. force it to load no matter what by
# using getattr().
if prop.key not in obj_state.dict:
getattr(obj, prop.key)

Expand All @@ -182,8 +205,9 @@ def create_version(obj, session, deleted=False):
# check those too
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():
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
Expand All @@ -201,6 +225,7 @@ def create_version(obj, session, deleted=False):
session.add(hist)
obj.version += 1


def versioned_session(session):
@event.listens_for(session, 'before_flush')
def before_flush(session, flush_context, instances):
Expand Down
Loading

0 comments on commit a88be57

Please sign in to comment.