Skip to content

Commit 2512a5c

Browse files
committed
changed my mind on maintenance
1 parent 9654de6 commit 2512a5c

3 files changed

Lines changed: 47 additions & 28 deletions

File tree

mkdocs/docs/cli.md

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,19 +43,20 @@ Options:
4343
--help Show this message and exit.
4444

4545
Commands:
46-
create Operation to create a namespace.
47-
describe Describe a namespace or a table.
48-
drop Operations to drop a namespace or table.
49-
files List all the files of the table.
50-
list List tables or namespaces.
51-
list-refs List all the refs in the provided table.
52-
location Return the location of the table.
53-
properties Properties on tables/namespaces.
54-
rename Rename a table.
55-
schema Get the schema of the table.
56-
spec Return the partition spec of the table.
57-
uuid Return the UUID of the table.
58-
version Print pyiceberg version.
46+
create Operation to create a namespace.
47+
describe Describe a namespace or a table.
48+
drop Operations to drop a namespace or table.
49+
expire-snapshots Expire snapshots from a table by ID or age.
50+
files List all the files of the table.
51+
list List tables or namespaces.
52+
list-refs List all the refs in the provided table.
53+
location Return the location of the table.
54+
properties Properties on tables/namespaces.
55+
rename Rename a table.
56+
schema Get the schema of the table.
57+
spec Return the partition spec of the table.
58+
uuid Return the UUID of the table.
59+
version Print pyiceberg version.
5960
```
6061

6162
This example assumes that you have a default catalog set. If you want to load another catalog, for example, the rest example above. Then you need to set `--catalog rest`.
@@ -240,3 +241,26 @@ Property write.metadata.delete-after-commit.enabled removed from nyc.taxis
240241
➜ pyiceberg properties get table nyc.taxis write.metadata.delete-after-commit.enabled
241242
Could not find property write.metadata.delete-after-commit.enabled on nyc.taxis
242243
```
244+
245+
## Expire snapshots
246+
247+
`expire-snapshots` removes snapshots from a table. Snapshots that are the HEAD of a branch or that are referenced by a tag are protected and will be skipped.
248+
249+
Pass `--snapshot-id` one or more times to expire snapshots by ID, and/or `--older-than <ISO datetime>` to expire all unprotected snapshots older than the given timestamp. At least one of the two options is required.
250+
251+
```sh
252+
➜ pyiceberg expire-snapshots nyc.taxis --snapshot-id 5937117119577207079
253+
Expired snapshots on nyc.taxis
254+
```
255+
256+
```sh
257+
➜ pyiceberg expire-snapshots nyc.taxis \
258+
--snapshot-id 5937117119577207079 \
259+
--snapshot-id 4123987645210000000
260+
Expired snapshots on nyc.taxis
261+
```
262+
263+
```sh
264+
➜ pyiceberg expire-snapshots nyc.taxis --older-than 2024-01-01T00:00:00
265+
Expired snapshots on nyc.taxis
266+
```

pyiceberg/cli/console.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -481,12 +481,7 @@ def _retention_properties(ref: SnapshotRef, table_properties: dict[str, str]) ->
481481
return retention_properties
482482

483483

484-
@run.group()
485-
def maintenance() -> None:
486-
"""Run maintenance operations on a table."""
487-
488-
489-
@maintenance.command("expire-snapshots")
484+
@run.command("expire-snapshots")
490485
@click.argument("identifier")
491486
@click.option(
492487
"--snapshot-id",

tests/cli/test_console.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1095,7 +1095,9 @@ def _create_table_with_expirable_snapshot(catalog: InMemoryCatalog) -> int:
10951095
)
10961096
table.append(pa.Table.from_pylist([{"x": 1, "y": 2, "z": 3}], schema=arrow_schema))
10971097
table.refresh()
1098-
older_snapshot_id = table.current_snapshot().snapshot_id
1098+
older_snapshot = table.current_snapshot()
1099+
assert older_snapshot is not None
1100+
older_snapshot_id = older_snapshot.snapshot_id
10991101
table.append(pa.Table.from_pylist([{"x": 4, "y": 5, "z": 6}], schema=arrow_schema))
11001102
return older_snapshot_id
11011103

@@ -1109,17 +1111,15 @@ def test_expire_snapshots_requires_option(catalog: InMemoryCatalog) -> None:
11091111
)
11101112

11111113
runner = CliRunner()
1112-
result = runner.invoke(run, ["maintenance", "expire-snapshots", "default.my_table"])
1114+
result = runner.invoke(run, ["expire-snapshots", "default.my_table"])
11131115

11141116
assert result.exit_code == 1
11151117
assert "Must provide at least one of --snapshot-id or --older-than." in result.output
11161118

11171119

1118-
def test_expire_snapshots_table_does_not_exists(catalog: InMemoryCatalog) -> None:
1119-
# pylint: disable=unused-argument
1120-
1120+
def test_expire_snapshots_table_does_not_exists() -> None:
11211121
runner = CliRunner()
1122-
result = runner.invoke(run, ["maintenance", "expire-snapshots", "default.doesnotexist", "--snapshot-id", "1"])
1122+
result = runner.invoke(run, ["expire-snapshots", "default.doesnotexist", "--snapshot-id", "1"])
11231123

11241124
assert result.exit_code == 1
11251125
assert result.output == "Table does not exist: default.doesnotexist\n"
@@ -1129,7 +1129,7 @@ def test_expire_snapshots_by_id(catalog: InMemoryCatalog) -> None:
11291129
snapshot_id = _create_table_with_expirable_snapshot(catalog)
11301130

11311131
runner = CliRunner()
1132-
result = runner.invoke(run, ["maintenance", "expire-snapshots", "default.my_table", "--snapshot-id", str(snapshot_id)])
1132+
result = runner.invoke(run, ["expire-snapshots", "default.my_table", "--snapshot-id", str(snapshot_id)])
11331133

11341134
assert result.exit_code == 0
11351135
assert result.output == "Expired snapshots on default.my_table\n"
@@ -1170,7 +1170,7 @@ def test_expire_snapshots_unknown_snapshot_id(catalog: InMemoryCatalog) -> None:
11701170
)
11711171

11721172
runner = CliRunner()
1173-
result = runner.invoke(run, ["maintenance", "expire-snapshots", "default.my_table", "--snapshot-id", "999"])
1173+
result = runner.invoke(run, ["expire-snapshots", "default.my_table", "--snapshot-id", "999"])
11741174

11751175
assert result.exit_code == 1
11761176
assert "Snapshot with ID 999 does not exist." in result.output
@@ -1182,7 +1182,7 @@ def test_json_expire_snapshots_by_id(catalog: InMemoryCatalog) -> None:
11821182
runner = CliRunner()
11831183
result = runner.invoke(
11841184
run,
1185-
["--output=json", "maintenance", "expire-snapshots", "default.my_table", "--snapshot-id", str(snapshot_id)],
1185+
["--output=json", "expire-snapshots", "default.my_table", "--snapshot-id", str(snapshot_id)],
11861186
)
11871187

11881188
assert result.exit_code == 0

0 commit comments

Comments
 (0)