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

Log a warning for uncommitted changes on transaction exit #266

Merged
merged 2 commits into from
Feb 21, 2024
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
7 changes: 7 additions & 0 deletions src/lakefs_spec/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import logging
import random
import string
import warnings
from collections import deque
from typing import TYPE_CHECKING, Literal, TypeVar

Expand Down Expand Up @@ -132,6 +133,12 @@ def __exit__(self, exc_type, exc_val, exc_tb):
self.fs._intrans = False
self.fs._transaction = None

if any(self._ephemeral_branch.uncommitted()):
msg = f"Finished transaction on branch {self._ephemeral_branch.id!r} with uncommitted changes."
if self.delete != "never":
msg += " Objects added but not committed are lost."
warnings.warn(msg)

if success and self.automerge:
if any(self.base_branch.diff(self._ephemeral_branch)):
self._ephemeral_branch.merge_into(self.base_branch)
Expand Down
30 changes: 30 additions & 0 deletions tests/test_transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,33 @@ def test_transaction_bad_repo(fs: LakeFSFileSystem) -> None:
with pytest.raises(ValueError, match="repository .* does not exist"):
with fs.transaction(repository="REEEE"):
pass


def test_warn_uncommitted_changes(
random_file_factory: RandomFileFactory,
fs: LakeFSFileSystem,
repository: Repository,
temp_branch: Branch,
) -> None:
random_file = random_file_factory.make()

lpath = str(random_file)

with pytest.warns(match="uncommitted changes.*lost"):
with fs.transaction(repository, temp_branch) as tx:
fs.put_file(lpath, f"{repository.id}/{tx.branch.id}/{random_file.name}")


def test_warn_uncommitted_changes_on_persisted_branch(
random_file_factory: RandomFileFactory,
fs: LakeFSFileSystem,
repository: Repository,
temp_branch: Branch,
) -> None:
random_file = random_file_factory.make()

lpath = str(random_file)

with pytest.warns(match="uncommitted changes(?:(?!lost).)*$"):
with fs.transaction(repository, temp_branch, delete="never") as tx:
fs.put_file(lpath, f"{repository.id}/{tx.branch.id}/{random_file.name}")
Loading