Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

api: don't override next version with new draft #232

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions invenio_drafts_resources/records/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,10 +214,10 @@ def edit(cls, record):
# Note, other values like bucket_id values was kept in the
# soft-deleted record, so we are not setting them again here.
except NoResultFound:
# If a draft was ever force deleted, then we will create the draft.
# This is a very exceptional case as normally, when we edit a
# record then the soft-deleted draft exists and we are in above
# case.
# If a draft was ever force deleted, then we re-create it.
# A classic scenario for this case is editing a published record
# after enough time has passed for its original draft to have
# been cleaned up. It then needs to be recreated.
draft = cls.create(
{},
# A draft to edit a record must share the id and uuid.
Expand Down
4 changes: 2 additions & 2 deletions invenio_drafts_resources/records/systemfields/versions.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,8 @@ def post_create(self, record):
# The parent record is created on pre_create.
versions = self.obj(record)
if self._create and self._set_next:
# if fork is none - it's a new?
versions.set_next()
if not record.is_published:
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the fix. When interacting with the site in the browser it fixes the issue.

versions.set_next()
elif self._create and self._set_latest:
versions.set_latest()

Expand Down
30 changes: 30 additions & 0 deletions tests/records/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
# details.

"""Data access layer tests."""
from datetime import timedelta

import pytest
from invenio_search import current_search_client
Expand Down Expand Up @@ -133,6 +134,35 @@ def test_draft_parent_state_hard_delete(app, db, location):
assert RecordMetadata.query.count() == 0


def test_new_draft_of_published_record_doesnt_override_next_draft_id(app, db, location):
"""Test multiple drafts when editing multiple versions of the same record."""
# Classic draft of published record having been cleaned up scenario
record_1 = Record.publish(Draft.create({}))
db.session.commit()
Draft.cleanup_drafts(timedelta(0))
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The most typical case (but relies on cleanup_drafts).


# Wider test for any situation where draft of record doesn't exist anymore
# e.g. admin force deletion
parent = ParentRecord.create({})
record_2 = Record.create({}, parent=parent)
record_2.register()
Comment on lines +146 to +148
Copy link
Collaborator Author

@fenekku fenekku Jul 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The wider case that covers the above if cleanup_drafts works correctly and covers other scenarios e.g. direct forced deletion etc . In other words, anything that gets us to a published record without a draft.


db.session.commit()

for record in [record_1, record_2]:
# Create new_version before a record's draft exists
new_version_draft = Draft.new_version(record)
db.session.commit()
assert record.versions.next_draft_id == new_version_draft.id
assert record.versions.latest_id == record.id

# Check next_draft_id still holds after edit
Draft.edit(record)
db.session.commit()
assert record.versions.next_draft_id == new_version_draft.id
assert record.versions.latest_id == record.id


def test_draft_parent_state_hard_delete_with_parent(app, db, location):
"""Test force deletion of a draft."""
# Initial state: A previous reccord version exists, in addition to draft
Expand Down
Loading