Skip to content

Commit 1b3a95c

Browse files
committed
refactor: update snapshot expiration method calls for consistency and clarity
1 parent bf9427a commit 1b3a95c

4 files changed

Lines changed: 14 additions & 25 deletions

File tree

mkdocs/docs/api.md

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1363,12 +1363,12 @@ def cleanup_old_snapshots(table_name: str, snapshot_ids: list[int]):
13631363
"""Remove specific snapshots from a table."""
13641364
catalog = load_catalog("production")
13651365
table = catalog.load_table(table_name)
1366-
1366+
13671367
# Use context manager for safe transaction handling
13681368
with table.maintenance.expire_snapshots() as expire:
13691369
for snapshot_id in snapshot_ids:
13701370
expire.by_id(snapshot_id)
1371-
1371+
13721372
print(f"Expired {len(snapshot_ids)} snapshots from {table_name}")
13731373

13741374
# Usage
@@ -1384,17 +1384,6 @@ The maintenance API leverages Iceberg's transaction system:
13841384
- **Rollback**: If an error occurs in a context manager, changes are automatically rolled back
13851385
- **Atomic Operations**: All operations within a single transaction are applied atomically
13861386

1387-
<!-- prettier-ignore-start -->
1388-
1389-
!!! note "Future Maintenance Operations"
1390-
The maintenance API is designed to be extensible. Future versions may include additional maintenance operations such as:
1391-
1392-
* `table.maintenance.compact_data()` - Data file compaction
1393-
* `table.maintenance.optimize_metadata()` - Metadata optimization
1394-
* `table.maintenance.rewrite_manifests()` - Manifest rewriting
1395-
1396-
<!-- prettier-ignore-end -->
1397-
13981387
## Views
13991388

14001389
PyIceberg supports view operations.

pyiceberg/table/inspect.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -706,4 +706,4 @@ def all_data_files(self) -> "pa.Table":
706706
return self._all_files({DataFileContent.DATA})
707707

708708
def all_delete_files(self) -> "pa.Table":
709-
return self._all_files({DataFileContent.POSITION_DELETES, DataFileContent.EQUALITY_DELETES})
709+
return self._all_files({DataFileContent.POSITION_DELETES, DataFileContent.EQUALITY_DELETES})

pyiceberg/table/update/snapshot.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -985,7 +985,7 @@ def by_ids(self, snapshot_ids: List[int]) -> "ExpireSnapshots":
985985
This for method chaining.
986986
"""
987987
for snapshot_id in snapshot_ids:
988-
self.expire_snapshot_by_id(snapshot_id)
988+
self.by_id(snapshot_id)
989989
return self
990990

991991
def older_than(self, timestamp_ms: int) -> "ExpireSnapshots":
@@ -1002,4 +1002,4 @@ def older_than(self, timestamp_ms: int) -> "ExpireSnapshots":
10021002
for snapshot in self._transaction.table_metadata.snapshots:
10031003
if snapshot.timestamp_ms < timestamp_ms and snapshot.snapshot_id not in protected_ids:
10041004
self._snapshot_ids_to_expire.add(snapshot.snapshot_id)
1005-
return self
1005+
return self

tests/table/test_expire_snapshots.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ def test_cannot_expire_protected_head_snapshot(table_v2: Table) -> None:
4242
assert any(ref.snapshot_id == HEAD_SNAPSHOT for ref in table_v2.metadata.refs.values())
4343

4444
# Attempt to expire the HEAD snapshot and expect a ValueError
45-
with pytest.raises(ValueError, match=f"Snapshot with ID {HEAD_SNAPSHOT} is protected and cannot be expired."):
46-
table_v2.expire_snapshots().expire_snapshot_by_id(HEAD_SNAPSHOT).commit()
45+
with pytest.raises(ValueError, match=f"Cannot expire snapshot {HEAD_SNAPSHOT} as it is referenced by a branch or tag."):
46+
table_v2.maintenance.expire_snapshots().by_id(HEAD_SNAPSHOT).commit()
4747

4848
table_v2.catalog.commit_table.assert_not_called()
4949

@@ -65,8 +65,8 @@ def test_cannot_expire_tagged_snapshot(table_v2: Table) -> None:
6565
)
6666
assert any(ref.snapshot_id == TAGGED_SNAPSHOT for ref in table_v2.metadata.refs.values())
6767

68-
with pytest.raises(ValueError, match=f"Snapshot with ID {TAGGED_SNAPSHOT} is protected and cannot be expired."):
69-
table_v2.expire_snapshots().expire_snapshot_by_id(TAGGED_SNAPSHOT).commit()
68+
with pytest.raises(ValueError, match=f"Cannot expire snapshot {TAGGED_SNAPSHOT} as it is referenced by a branch or tag."):
69+
table_v2.maintenance.expire_snapshots().by_id(TAGGED_SNAPSHOT).commit()
7070

7171
table_v2.catalog.commit_table.assert_not_called()
7272

@@ -98,7 +98,7 @@ def test_expire_unprotected_snapshot(table_v2: Table) -> None:
9898
assert all(ref.snapshot_id != EXPIRE_SNAPSHOT for ref in table_v2.metadata.refs.values())
9999

100100
# Expire the snapshot
101-
table_v2.expire_snapshots().expire_snapshot_by_id(EXPIRE_SNAPSHOT).commit()
101+
table_v2.maintenance.expire_snapshots().by_id(EXPIRE_SNAPSHOT).commit()
102102

103103
table_v2.catalog.commit_table.assert_called_once()
104104
remaining_snapshots = table_v2.metadata.snapshots
@@ -114,7 +114,7 @@ def test_expire_nonexistent_snapshot_raises(table_v2: Table) -> None:
114114
table_v2.metadata = table_v2.metadata.model_copy(update={"refs": {}})
115115

116116
with pytest.raises(ValueError, match=f"Snapshot with ID {NONEXISTENT_SNAPSHOT} does not exist."):
117-
table_v2.expire_snapshots().expire_snapshot_by_id(NONEXISTENT_SNAPSHOT).commit()
117+
table_v2.maintenance.expire_snapshots().by_id(NONEXISTENT_SNAPSHOT).commit()
118118

119119
table_v2.catalog.commit_table.assert_not_called()
120120

@@ -152,7 +152,7 @@ def test_expire_snapshots_by_timestamp_skips_protected(table_v2: Table) -> None:
152152
)
153153
table_v2.catalog.commit_table.return_value = mock_response
154154

155-
table_v2.expire_snapshots().expire_snapshots_older_than(future_timestamp).commit()
155+
table_v2.maintenance.expire_snapshots().older_than(future_timestamp).commit()
156156
# Update metadata to reflect the commit (as in other tests)
157157
table_v2.metadata = mock_response.metadata
158158

@@ -215,10 +215,10 @@ def test_expire_snapshots_by_ids(table_v2: Table) -> None:
215215
assert all(ref.snapshot_id not in (EXPIRE_SNAPSHOT_1, EXPIRE_SNAPSHOT_2) for ref in table_v2.metadata.refs.values())
216216

217217
# Expire the snapshots
218-
table_v2.expire_snapshots().expire_snapshots_by_ids([EXPIRE_SNAPSHOT_1, EXPIRE_SNAPSHOT_2]).commit()
218+
table_v2.maintenance.expire_snapshots().by_ids([EXPIRE_SNAPSHOT_1, EXPIRE_SNAPSHOT_2]).commit()
219219

220220
table_v2.catalog.commit_table.assert_called_once()
221221
remaining_snapshots = table_v2.metadata.snapshots
222222
assert EXPIRE_SNAPSHOT_1 not in remaining_snapshots
223223
assert EXPIRE_SNAPSHOT_2 not in remaining_snapshots
224-
assert len(table_v2.metadata.snapshots) == 1
224+
assert len(table_v2.metadata.snapshots) == 1

0 commit comments

Comments
 (0)