summaryrefslogtreecommitdiff
path: root/examples/versioned_rows
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2019-01-06 01:14:26 -0500
committermike bayer <mike_mp@zzzcomputing.com>2019-01-06 17:34:50 +0000
commit1e1a38e7801f410f244e4bbb44ec795ae152e04e (patch)
tree28e725c5c8188bd0cfd133d1e268dbca9b524978 /examples/versioned_rows
parent404e69426b05a82d905cbb3ad33adafccddb00dd (diff)
downloadsqlalchemy-1e1a38e7801f410f244e4bbb44ec795ae152e04e.tar.gz
Run black -l 79 against all source files
This is a straight reformat run using black as is, with no edits applied at all. The black run will format code consistently, however in some cases that are prevalent in SQLAlchemy code it produces too-long lines. The too-long lines will be resolved in the following commit that will resolve all remaining flake8 issues including shadowed builtins, long lines, import order, unused imports, duplicate imports, and docstring issues. Change-Id: I7eda77fed3d8e73df84b3651fd6cfcfe858d4dc9
Diffstat (limited to 'examples/versioned_rows')
-rw-r--r--examples/versioned_rows/__init__.py2
-rw-r--r--examples/versioned_rows/versioned_map.py96
-rw-r--r--examples/versioned_rows/versioned_rows.py38
-rw-r--r--examples/versioned_rows/versioned_rows_w_versionid.py61
-rw-r--r--examples/versioned_rows/versioned_update_old_row.py110
5 files changed, 187 insertions, 120 deletions
diff --git a/examples/versioned_rows/__init__.py b/examples/versioned_rows/__init__.py
index 637e1aca6..e5016a740 100644
--- a/examples/versioned_rows/__init__.py
+++ b/examples/versioned_rows/__init__.py
@@ -9,4 +9,4 @@ history row to a separate history table.
.. autosource::
-""" \ No newline at end of file
+"""
diff --git a/examples/versioned_rows/versioned_map.py b/examples/versioned_rows/versioned_map.py
index 6a5c86a3a..46bdbb783 100644
--- a/examples/versioned_rows/versioned_map.py
+++ b/examples/versioned_rows/versioned_map.py
@@ -28,11 +28,17 @@ those additional values.
"""
-from sqlalchemy import Column, String, Integer, ForeignKey, \
- create_engine
+from sqlalchemy import Column, String, Integer, ForeignKey, create_engine
from sqlalchemy.ext.declarative import declarative_base
-from sqlalchemy.orm import attributes, relationship, backref, \
- sessionmaker, make_transient, validates, Session
+from sqlalchemy.orm import (
+ attributes,
+ relationship,
+ backref,
+ sessionmaker,
+ make_transient,
+ validates,
+ Session,
+)
from sqlalchemy.ext.associationproxy import association_proxy
from sqlalchemy.orm.collections import attribute_mapped_collection
from sqlalchemy import event
@@ -45,8 +51,9 @@ def before_flush(session, flush_context, instances):
"""
for instance in session.dirty:
- if hasattr(instance, 'new_version') and \
- session.is_modified(instance, passive=True):
+ if hasattr(instance, "new_version") and session.is_modified(
+ instance, passive=True
+ ):
# make it transient
instance.new_version(session)
@@ -54,6 +61,7 @@ def before_flush(session, flush_context, instances):
# re-add
session.add(instance)
+
Base = declarative_base()
@@ -67,7 +75,8 @@ class ConfigData(Base):
string name mapped to a string/int value.
"""
- __tablename__ = 'config'
+
+ __tablename__ = "config"
id = Column(Integer, primary_key=True)
"""Primary key column of this ConfigData."""
@@ -76,7 +85,7 @@ class ConfigData(Base):
"ConfigValueAssociation",
collection_class=attribute_mapped_collection("name"),
backref=backref("config_data"),
- lazy="subquery"
+ lazy="subquery",
)
"""Dictionary-backed collection of ConfigValueAssociation objects,
keyed to the name of the associated ConfigValue.
@@ -97,7 +106,7 @@ class ConfigData(Base):
def __init__(self, data):
self.data = data
- @validates('elements')
+ @validates("elements")
def _associate_with_element(self, key, element):
"""Associate incoming ConfigValues with this
ConfigData, if not already associated.
@@ -117,11 +126,11 @@ class ConfigData(Base):
# history of the 'elements' collection.
# this is a tuple of groups: (added, unchanged, deleted)
- hist = attributes.get_history(self, 'elements')
+ hist = attributes.get_history(self, "elements")
# rewrite the 'elements' collection
# from scratch, removing all history
- attributes.set_committed_value(self, 'elements', {})
+ attributes.set_committed_value(self, "elements", {})
# new elements in the "added" group
# are moved to our new collection.
@@ -133,7 +142,8 @@ class ConfigData(Base):
# the old ones stay associated with the old ConfigData
for elem in hist.unchanged:
self.elements[elem.name] = ConfigValueAssociation(
- elem.config_value)
+ elem.config_value
+ )
# we also need to expire changes on each ConfigValueAssociation
# that is to remain associated with the old ConfigData.
@@ -144,12 +154,12 @@ class ConfigData(Base):
class ConfigValueAssociation(Base):
"""Relate ConfigData objects to associated ConfigValue objects."""
- __tablename__ = 'config_value_association'
+ __tablename__ = "config_value_association"
- config_id = Column(ForeignKey('config.id'), primary_key=True)
+ config_id = Column(ForeignKey("config.id"), primary_key=True)
"""Reference the primary key of the ConfigData object."""
- config_value_id = Column(ForeignKey('config_value.id'), primary_key=True)
+ config_value_id = Column(ForeignKey("config_value.id"), primary_key=True)
"""Reference the primary key of the ConfigValue object."""
config_value = relationship("ConfigValue", lazy="joined", innerjoin=True)
@@ -182,8 +192,7 @@ class ConfigValueAssociation(Base):
"""
if value != self.config_value.value:
- self.config_data.elements[self.name] = \
- ConfigValueAssociation(
+ self.config_data.elements[self.name] = ConfigValueAssociation(
ConfigValue(self.config_value.name, value)
)
@@ -194,13 +203,14 @@ class ConfigValue(Base):
ConfigValue is immutable.
"""
- __tablename__ = 'config_value'
+
+ __tablename__ = "config_value"
id = Column(Integer, primary_key=True)
name = Column(String(50), nullable=False)
originating_config_id = Column(
- Integer, ForeignKey('config.id'),
- nullable=False)
+ Integer, ForeignKey("config.id"), nullable=False
+ )
int_value = Column(Integer)
string_value = Column(String(255))
@@ -221,7 +231,7 @@ class ConfigValue(Base):
@property
def value(self):
- for k in ('int_value', 'string_value'):
+ for k in ("int_value", "string_value"):
v = getattr(self, k)
if v is not None:
return v
@@ -237,25 +247,23 @@ class ConfigValue(Base):
self.string_value = str(value)
self.int_value = None
-if __name__ == '__main__':
- engine = create_engine('sqlite://', echo=True)
+
+if __name__ == "__main__":
+ engine = create_engine("sqlite://", echo=True)
Base.metadata.create_all(engine)
Session = sessionmaker(engine)
sess = Session()
- config = ConfigData({
- 'user_name': 'twitter',
- 'hash_id': '4fedffca37eaf',
- 'x': 27,
- 'y': 450
- })
+ config = ConfigData(
+ {"user_name": "twitter", "hash_id": "4fedffca37eaf", "x": 27, "y": 450}
+ )
sess.add(config)
sess.commit()
version_one = config.id
- config.data['user_name'] = 'yahoo'
+ config.data["user_name"] = "yahoo"
sess.commit()
version_two = config.id
@@ -265,27 +273,29 @@ if __name__ == '__main__':
# two versions have been created.
assert config.data == {
- 'user_name': 'yahoo',
- 'hash_id': '4fedffca37eaf',
- 'x': 27,
- 'y': 450
+ "user_name": "yahoo",
+ "hash_id": "4fedffca37eaf",
+ "x": 27,
+ "y": 450,
}
old_config = sess.query(ConfigData).get(version_one)
assert old_config.data == {
- 'user_name': 'twitter',
- 'hash_id': '4fedffca37eaf',
- 'x': 27,
- 'y': 450
+ "user_name": "twitter",
+ "hash_id": "4fedffca37eaf",
+ "x": 27,
+ "y": 450,
}
# the history of any key can be acquired using
# the originating_config_id attribute
- history = sess.query(ConfigValue).\
- filter(ConfigValue.name == 'user_name').\
- order_by(ConfigValue.originating_config_id).\
- all()
+ history = (
+ sess.query(ConfigValue)
+ .filter(ConfigValue.name == "user_name")
+ .order_by(ConfigValue.originating_config_id)
+ .all()
+ )
assert [(h.value, h.originating_config_id) for h in history] == (
- [('twitter', version_one), ('yahoo', version_two)]
+ [("twitter", version_one), ("yahoo", version_two)]
)
diff --git a/examples/versioned_rows/versioned_rows.py b/examples/versioned_rows/versioned_rows.py
index ca896190d..03e1c3510 100644
--- a/examples/versioned_rows/versioned_rows.py
+++ b/examples/versioned_rows/versioned_rows.py
@@ -3,8 +3,13 @@ an UPDATE statement on a single row into an INSERT statement, so that a new
row is inserted with the new data, keeping the old row intact.
"""
-from sqlalchemy.orm import sessionmaker, relationship, make_transient, \
- backref, Session
+from sqlalchemy.orm import (
+ sessionmaker,
+ relationship,
+ make_transient,
+ backref,
+ Session,
+)
from sqlalchemy import Column, ForeignKey, create_engine, Integer, String
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import attributes
@@ -38,9 +43,10 @@ def before_flush(session, flush_context, instances):
# re-add
session.add(instance)
+
Base = declarative_base()
-engine = create_engine('sqlite://', echo=True)
+engine = create_engine("sqlite://", echo=True)
Session = sessionmaker(engine)
@@ -48,43 +54,44 @@ Session = sessionmaker(engine)
class Example(Versioned, Base):
- __tablename__ = 'example'
+ __tablename__ = "example"
id = Column(Integer, primary_key=True)
data = Column(String)
+
Base.metadata.create_all(engine)
session = Session()
-e1 = Example(data='e1')
+e1 = Example(data="e1")
session.add(e1)
session.commit()
-e1.data = 'e2'
+e1.data = "e2"
session.commit()
assert session.query(Example.id, Example.data).order_by(Example.id).all() == (
- [(1, 'e1'), (2, 'e2')]
+ [(1, "e1"), (2, "e2")]
)
# example 2, versioning with a parent
class Parent(Base):
- __tablename__ = 'parent'
+ __tablename__ = "parent"
id = Column(Integer, primary_key=True)
- child_id = Column(Integer, ForeignKey('child.id'))
- child = relationship("Child", backref=backref('parent', uselist=False))
+ child_id = Column(Integer, ForeignKey("child.id"))
+ child = relationship("Child", backref=backref("parent", uselist=False))
class Child(Versioned, Base):
- __tablename__ = 'child'
+ __tablename__ = "child"
id = Column(Integer, primary_key=True)
data = Column(String)
def new_version(self, session):
# expire parent's reference to us
- session.expire(self.parent, ['child'])
+ session.expire(self.parent, ["child"])
# create new version
Versioned.new_version(self, session)
@@ -92,18 +99,19 @@ class Child(Versioned, Base):
# re-add ourselves to the parent
self.parent.child = self
+
Base.metadata.create_all(engine)
session = Session()
-p1 = Parent(child=Child(data='c1'))
+p1 = Parent(child=Child(data="c1"))
session.add(p1)
session.commit()
-p1.child.data = 'c2'
+p1.child.data = "c2"
session.commit()
assert p1.child_id == 2
assert session.query(Child.id, Child.data).order_by(Child.id).all() == (
- [(1, 'c1'), (2, 'c2')]
+ [(1, "c1"), (2, "c2")]
)
diff --git a/examples/versioned_rows/versioned_rows_w_versionid.py b/examples/versioned_rows/versioned_rows_w_versionid.py
index 8445401c5..5fd6f9fc4 100644
--- a/examples/versioned_rows/versioned_rows_w_versionid.py
+++ b/examples/versioned_rows/versioned_rows_w_versionid.py
@@ -6,10 +6,24 @@ This example adds a numerical version_id to the Versioned class as well
as the ability to see which row is the most "current" vesion.
"""
-from sqlalchemy.orm import sessionmaker, relationship, make_transient, \
- backref, Session, column_property
-from sqlalchemy import Column, ForeignKeyConstraint, create_engine, \
- Integer, String, Boolean, select, func
+from sqlalchemy.orm import (
+ sessionmaker,
+ relationship,
+ make_transient,
+ backref,
+ Session,
+ column_property,
+)
+from sqlalchemy import (
+ Column,
+ ForeignKeyConstraint,
+ create_engine,
+ Integer,
+ String,
+ Boolean,
+ select,
+ func,
+)
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import attributes
from sqlalchemy import event
@@ -38,7 +52,8 @@ class Versioned(object):
# optional - set previous version to have is_current_version=False
old_id = self.id
session.query(self.__class__).filter_by(id=old_id).update(
- values=dict(is_current_version=False), synchronize_session=False)
+ values=dict(is_current_version=False), synchronize_session=False
+ )
# make us transient (removes persistent
# identity).
@@ -65,9 +80,10 @@ def before_flush(session, flush_context, instances):
# re-add
session.add(instance)
+
Base = declarative_base()
-engine = create_engine('sqlite://', echo=True)
+engine = create_engine("sqlite://", echo=True)
Session = sessionmaker(engine)
@@ -75,17 +91,18 @@ Session = sessionmaker(engine)
class Example(Versioned, Base):
- __tablename__ = 'example'
+ __tablename__ = "example"
data = Column(String)
+
Base.metadata.create_all(engine)
session = Session()
-e1 = Example(id=1, data='e1')
+e1 = Example(id=1, data="e1")
session.add(e1)
session.commit()
-e1.data = 'e2'
+e1.data = "e2"
session.commit()
assert session.query(
@@ -93,36 +110,36 @@ assert session.query(
Example.version_id,
Example.is_current_version,
Example.calc_is_current_version,
- Example.data).order_by(Example.id, Example.version_id).all() == (
- [(1, 1, False, False, 'e1'), (1, 2, True, True, 'e2')]
+ Example.data,
+).order_by(Example.id, Example.version_id).all() == (
+ [(1, 1, False, False, "e1"), (1, 2, True, True, "e2")]
)
# example 2, versioning with a parent
class Parent(Base):
- __tablename__ = 'parent'
+ __tablename__ = "parent"
id = Column(Integer, primary_key=True)
child_id = Column(Integer)
child_version_id = Column(Integer)
- child = relationship("Child", backref=backref('parent', uselist=False))
+ child = relationship("Child", backref=backref("parent", uselist=False))
__table_args__ = (
ForeignKeyConstraint(
- ['child_id', 'child_version_id'],
- ['child.id', 'child.version_id'],
+ ["child_id", "child_version_id"], ["child.id", "child.version_id"]
),
)
class Child(Versioned, Base):
- __tablename__ = 'child'
+ __tablename__ = "child"
data = Column(String)
def new_version(self, session):
# expire parent's reference to us
- session.expire(self.parent, ['child'])
+ session.expire(self.parent, ["child"])
# create new version
Versioned.new_version(self, session)
@@ -131,15 +148,16 @@ class Child(Versioned, Base):
# parent foreign key to be updated also
self.parent.child = self
+
Base.metadata.create_all(engine)
session = Session()
-p1 = Parent(child=Child(id=1, data='c1'))
+p1 = Parent(child=Child(id=1, data="c1"))
session.add(p1)
session.commit()
-p1.child.data = 'c2'
+p1.child.data = "c2"
session.commit()
assert p1.child_id == 1
@@ -150,6 +168,7 @@ assert session.query(
Child.version_id,
Child.is_current_version,
Child.calc_is_current_version,
- Child.data).order_by(Child.id, Child.version_id).all() == (
- [(1, 1, False, False, 'c1'), (1, 2, True, True, 'c2')]
+ Child.data,
+).order_by(Child.id, Child.version_id).all() == (
+ [(1, 1, False, False, "c1"), (1, 2, True, True, "c2")]
)
diff --git a/examples/versioned_rows/versioned_update_old_row.py b/examples/versioned_rows/versioned_update_old_row.py
index 0159d2567..17c82fdc3 100644
--- a/examples/versioned_rows/versioned_update_old_row.py
+++ b/examples/versioned_rows/versioned_update_old_row.py
@@ -6,12 +6,23 @@ to only the most recent version.
"""
from sqlalchemy import (
- create_engine, Integer, String, event, Column, DateTime,
- inspect, literal
+ create_engine,
+ Integer,
+ String,
+ event,
+ Column,
+ DateTime,
+ inspect,
+ literal,
)
from sqlalchemy.orm import (
- make_transient, Session, relationship, attributes, backref,
- make_transient_to_detached, Query
+ make_transient,
+ Session,
+ relationship,
+ attributes,
+ backref,
+ make_transient_to_detached,
+ Query,
)
from sqlalchemy.ext.declarative import declarative_base
import datetime
@@ -50,7 +61,8 @@ class VersionedStartEnd(object):
# make the "old" version of us, which we will turn into an
# UPDATE
old_copy_of_us = self.__class__(
- id=self.id, start=self.start, end=self.end)
+ id=self.id, start=self.start, end=self.end
+ )
# turn old_copy_of_us into an UPDATE
make_transient_to_detached(old_copy_of_us)
@@ -95,11 +107,11 @@ def before_compile(query):
"""ensure all queries for VersionedStartEnd include criteria """
for ent in query.column_descriptions:
- entity = ent['entity']
+ entity = ent["entity"]
if entity is None:
continue
- insp = inspect(ent['entity'])
- mapper = getattr(insp, 'mapper', None)
+ insp = inspect(ent["entity"])
+ mapper = getattr(insp, "mapper", None)
if mapper and issubclass(mapper.class_, VersionedStartEnd):
query = query.enable_assertions(False).filter(
# using a literal "now" because SQLite's "between"
@@ -107,14 +119,14 @@ def before_compile(query):
# ``func.now()`` and we'd be using PostgreSQL
literal(
current_time() + datetime.timedelta(seconds=1)
- ).between(ent['entity'].start, ent['entity'].end)
+ ).between(ent["entity"].start, ent["entity"].end)
)
return query
class Parent(VersionedStartEnd, Base):
- __tablename__ = 'parent'
+ __tablename__ = "parent"
id = Column(Integer, primary_key=True)
start = Column(DateTime, primary_key=True)
end = Column(DateTime, primary_key=True)
@@ -124,10 +136,7 @@ class Parent(VersionedStartEnd, Base):
child = relationship(
"Child",
- primaryjoin=(
- "Child.id == foreign(Parent.child_n)"
- ),
-
+ primaryjoin=("Child.id == foreign(Parent.child_n)"),
# note the primaryjoin can also be:
#
# "and_(Child.id == foreign(Parent.child_n), "
@@ -138,14 +147,13 @@ class Parent(VersionedStartEnd, Base):
# as well, it just means the criteria will be present twice for most
# parent->child load operations
#
-
uselist=False,
- backref=backref('parent', uselist=False)
+ backref=backref("parent", uselist=False),
)
class Child(VersionedStartEnd, Base):
- __tablename__ = 'child'
+ __tablename__ = "child"
id = Column(Integer, primary_key=True)
start = Column(DateTime, primary_key=True)
@@ -155,7 +163,7 @@ class Child(VersionedStartEnd, Base):
def new_version(self, session):
# expire parent's reference to us
- session.expire(self.parent, ['child'])
+ session.expire(self.parent, ["child"])
# create new version
VersionedStartEnd.new_version(self, session)
@@ -163,6 +171,7 @@ class Child(VersionedStartEnd, Base):
# re-add ourselves to the parent
self.parent.child = self
+
times = []
@@ -185,27 +194,37 @@ def time_passes(s):
assert times[-1] > times[-2]
return times[-1]
-e = create_engine("sqlite://", echo='debug')
+
+e = create_engine("sqlite://", echo="debug")
Base.metadata.create_all(e)
s = Session(e)
now = time_passes(s)
-c1 = Child(id=1, data='child 1')
-p1 = Parent(id=1, data='c1', child=c1)
+c1 = Child(id=1, data="child 1")
+p1 = Parent(id=1, data="c1", child=c1)
s.add(p1)
s.commit()
# assert raw DB data
assert s.query(Parent.__table__).all() == [
- (1, times[0] - datetime.timedelta(days=3),
- times[0] + datetime.timedelta(days=3), 'c1', 1)
+ (
+ 1,
+ times[0] - datetime.timedelta(days=3),
+ times[0] + datetime.timedelta(days=3),
+ "c1",
+ 1,
+ )
]
assert s.query(Child.__table__).all() == [
- (1, times[0] - datetime.timedelta(days=3),
- times[0] + datetime.timedelta(days=3), 'child 1')
+ (
+ 1,
+ times[0] - datetime.timedelta(days=3),
+ times[0] + datetime.timedelta(days=3),
+ "child 1",
+ )
]
now = time_passes(s)
@@ -214,7 +233,7 @@ p1_check = s.query(Parent).first()
assert p1_check is p1
assert p1_check.child is c1
-p1.child.data = 'elvis presley'
+p1.child.data = "elvis presley"
s.commit()
@@ -226,40 +245,51 @@ c2_check = p2_check.child
assert p2_check.child is c1
# new data
-assert c1.data == 'elvis presley'
+assert c1.data == "elvis presley"
# new end time
assert c1.end == now + datetime.timedelta(days=2)
# assert raw DB data
assert s.query(Parent.__table__).all() == [
- (1, times[0] - datetime.timedelta(days=3),
- times[0] + datetime.timedelta(days=3), 'c1', 1)
+ (
+ 1,
+ times[0] - datetime.timedelta(days=3),
+ times[0] + datetime.timedelta(days=3),
+ "c1",
+ 1,
+ )
]
assert s.query(Child.__table__).order_by(Child.end).all() == [
- (1, times[0] - datetime.timedelta(days=3), times[1], 'child 1'),
- (1, times[1], times[1] + datetime.timedelta(days=2), 'elvis presley')
+ (1, times[0] - datetime.timedelta(days=3), times[1], "child 1"),
+ (1, times[1], times[1] + datetime.timedelta(days=2), "elvis presley"),
]
now = time_passes(s)
-p1.data = 'c2 elvis presley'
+p1.data = "c2 elvis presley"
s.commit()
# assert raw DB data. now there are two parent rows.
assert s.query(Parent.__table__).order_by(Parent.end).all() == [
- (1, times[0] - datetime.timedelta(days=3), times[2], 'c1', 1),
- (1, times[2], times[2] + datetime.timedelta(days=2), 'c2 elvis presley', 1)
+ (1, times[0] - datetime.timedelta(days=3), times[2], "c1", 1),
+ (
+ 1,
+ times[2],
+ times[2] + datetime.timedelta(days=2),
+ "c2 elvis presley",
+ 1,
+ ),
]
assert s.query(Child.__table__).order_by(Child.end).all() == [
- (1, times[0] - datetime.timedelta(days=3), times[1], 'child 1'),
- (1, times[1], times[1] + datetime.timedelta(days=2), 'elvis presley')
+ (1, times[0] - datetime.timedelta(days=3), times[1], "child 1"),
+ (1, times[1], times[1] + datetime.timedelta(days=2), "elvis presley"),
]
# add some more rows to test that these aren't coming back for
# queries
-s.add(Parent(id=2, data='unrelated', child=Child(id=2, data='unrelated')))
+s.add(Parent(id=2, data="unrelated", child=Child(id=2, data="unrelated")))
s.commit()
@@ -274,6 +304,6 @@ c3_check = s.query(Child).filter(Child.parent == p3_check).one()
assert c3_check is c1
# one child one parent....
-c3_check = s.query(Child).join(Parent.child).filter(
- Parent.id == p3_check.id).one()
-
+c3_check = (
+ s.query(Child).join(Parent.child).filter(Parent.id == p3_check.id).one()
+)