Skip to content

Commit

Permalink
Fix null version bug in existing data (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
pkonieczny authored Jun 9, 2022
1 parent 5beb50a commit f65de3a
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 14 deletions.
4 changes: 2 additions & 2 deletions chrononaut/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def before_flush(session, flush_context, instances):
for obj in session.dirty:
if hasattr(obj, "__versioned__") and is_modified(obj):
# Objects cannot be updated in the `after_flush` step hence bumping the version here
obj.version = obj.version + 1
obj.version = obj.version + 1 if obj.version is not None else 1
if hasattr(obj, "__chrononaut_record_change_info__"):
append_recorded_changes(obj, session)

Expand All @@ -56,7 +56,7 @@ def before_flush(session, flush_context, instances):
):
raise ChrononautException("Cannot commit version removal")
elif hasattr(obj, "__versioned__"):
obj.version = obj.version + 1
obj.version = obj.version + 1 if obj.version is not None else 1
create_version(obj, session, deleted=True)

@event.listens_for(session, "after_flush")
Expand Down
18 changes: 9 additions & 9 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,18 @@
master_doc = "index"

# General information about the project.
project = u"Chrononaut"
copyright = u"2018, Reference Genomics, Inc."
author = u"Reference Genomics, Inc."
project = "Chrononaut"
copyright = "2018, Reference Genomics, Inc."
author = "Reference Genomics, Inc."

# The version info for the project you're documenting, acts as replacement for
# |version| and |release|, also used in various other places throughout the
# built documents.
#
# The short X.Y version.
version = u"0.2.3"
version = "0.2.3"
# The full version, including alpha/beta/rc tags.
release = u"0.2.3"
release = "0.2.3"

# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
Expand Down Expand Up @@ -145,8 +145,8 @@
(
master_doc,
"Chrononaut.tex",
u"Chrononaut Documentation",
u"Reference Genomics, Inc.",
"Chrononaut Documentation",
"Reference Genomics, Inc.",
"manual",
)
]
Expand All @@ -156,7 +156,7 @@

# One entry per manual page. List of tuples
# (source start file, name, description, authors, manual section).
man_pages = [(master_doc, "chrononaut", u"Chrononaut Documentation", [author], 1)]
man_pages = [(master_doc, "chrononaut", "Chrononaut Documentation", [author], 1)]


# -- Options for Texinfo output -------------------------------------------
Expand All @@ -168,7 +168,7 @@
(
master_doc,
"Chrononaut",
u"Chrononaut Documentation",
"Chrononaut Documentation",
author,
"Chrononaut",
"One line description of project.",
Expand Down
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Requirements
Flask==1.1.2
Flask==2.1.0
Flask-SQLAlchemy==2.5.1
SQLAlchemy==1.4.25
psycopg2-binary>=2.8.1
Expand All @@ -9,9 +9,9 @@ python-dateutil

# Testing & documentation requirements (note these must be installable across all Tox environment)
pytest>=3.0.7
codecov
pytest-cov==2.4.0
Flask-Security-Fork==2.0.1
Werkzeug==2.0.0
sphinx==1.5.5
sphinx-autobuild==0.6.0
Flask-Sphinx-Themes==1.0.1
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
version=__version__, # noqa
packages=find_packages(exclude=["*test*"]),
install_requires=[
"Flask>=1.1.2",
"Flask>=2.1.0",
"Flask-SQLAlchemy>=2.5.1",
"SQLAlchemy>=1.4.0",
"psycopg2>=2.7.1",
Expand All @@ -34,6 +34,7 @@
author="Nick Greenfield",
author_email="opensource@onecodex.com",
long_description=__doc__,
long_description_content_type="text/markdown",
license="MIT License",
keywords="Chrononaut",
url="https://github.com/onecodex/chrononaut",
Expand Down
3 changes: 3 additions & 0 deletions tests/files/seed_null_version.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
INSERT INTO todos (id, title, text, todo_type, priority, version, created_at)
VALUES
(13, 'Todo #10', 'Text', 'basic', 'mid', NULL, '2016-06-11 20:10:00.134125-01');
20 changes: 20 additions & 0 deletions tests/test_versioning.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from chrononaut.flask_versioning import serialize_datetime, UTC
from datetime import datetime
from sqlalchemy import text

import pytest
import chrononaut
Expand Down Expand Up @@ -339,3 +340,22 @@ def test_version_fetching_and_diffing(db, session):
set(todo.diff(time_0).keys()) == {"text"}
set(todo.diff(time_0, include_hidden=True).keys()) == {"text", "done"}
assert todo.diff(time_0, include_hidden=True)["done"] == (None, True)


def test_null_version(db, session):
"""NULL version may be encountered when adding `Versioned` mixin to existing table."""
sql = text(open("tests/files/seed_null_version.sql", "r").read())
session.execute(sql)
session.commit()

todo = db.Todo.query.get(13)
assert todo.versions() == []

# Update the Task
todo.title = "Task 0.1"
session.commit()

# Check old versions
prior_todos = todo.versions()
assert len(prior_todos) == 1
assert prior_todos[0].version == 1

0 comments on commit f65de3a

Please sign in to comment.