Skip to content

Commit 356ba64

Browse files
committed
changed my mind on maintenance
1 parent 0fdf741 commit 356ba64

3 files changed

Lines changed: 46 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`.
@@ -254,3 +255,26 @@ To request purge for the underlying table files through the configured catalog,
254255
➜ pyiceberg drop table nyc.taxis --purge
255256
Dropped table: nyc.taxis (purge requested)
256257
```
258+
259+
## Expire snapshots
260+
261+
`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.
262+
263+
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.
264+
265+
```sh
266+
➜ pyiceberg expire-snapshots nyc.taxis --snapshot-id 5937117119577207079
267+
Expired snapshots on nyc.taxis
268+
```
269+
270+
```sh
271+
➜ pyiceberg expire-snapshots nyc.taxis \
272+
--snapshot-id 5937117119577207079 \
273+
--snapshot-id 4123987645210000000
274+
Expired snapshots on nyc.taxis
275+
```
276+
277+
```sh
278+
➜ pyiceberg expire-snapshots nyc.taxis --older-than 2024-01-01T00:00:00
279+
Expired snapshots on nyc.taxis
280+
```

‎pyiceberg/cli/console.py‎

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

561561

562-
@run.group()
563-
def maintenance() -> None:
564-
"""Run maintenance operations on a table."""
565-
566-
567-
@maintenance.command("expire-snapshots")
562+
@run.command("expire-snapshots")
568563
@click.argument("identifier")
569564
@click.option(
570565
"--snapshot-id",

‎tests/cli/test_console.py‎

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1332,7 +1332,9 @@ def _create_table_with_expirable_snapshot(catalog: InMemoryCatalog) -> int:
13321332
)
13331333
table.append(pa.Table.from_pylist([{"x": 1, "y": 2, "z": 3}], schema=arrow_schema))
13341334
table.refresh()
1335-
older_snapshot_id = table.current_snapshot().snapshot_id
1335+
older_snapshot = table.current_snapshot()
1336+
assert older_snapshot is not None
1337+
older_snapshot_id = older_snapshot.snapshot_id
13361338
table.append(pa.Table.from_pylist([{"x": 4, "y": 5, "z": 6}], schema=arrow_schema))
13371339
return older_snapshot_id
13381340

@@ -1346,17 +1348,15 @@ def test_expire_snapshots_requires_option(catalog: InMemoryCatalog) -> None:
13461348
)
13471349

13481350
runner = CliRunner()
1349-
result = runner.invoke(run, ["maintenance", "expire-snapshots", "default.my_table"])
1351+
result = runner.invoke(run, ["expire-snapshots", "default.my_table"])
13501352

13511353
assert result.exit_code == 1
13521354
assert "Must provide at least one of --snapshot-id or --older-than." in result.output
13531355

13541356

13551357
def test_expire_snapshots_table_does_not_exists(catalog: InMemoryCatalog) -> None:
1356-
# pylint: disable=unused-argument
1357-
13581358
runner = CliRunner()
1359-
result = runner.invoke(run, ["maintenance", "expire-snapshots", "default.doesnotexist", "--snapshot-id", "1"])
1359+
result = runner.invoke(run, ["expire-snapshots", "default.doesnotexist", "--snapshot-id", "1"])
13601360

13611361
assert result.exit_code == 1
13621362
assert result.output == "Table does not exist: default.doesnotexist\n"
@@ -1366,7 +1366,7 @@ def test_expire_snapshots_by_id(catalog: InMemoryCatalog) -> None:
13661366
snapshot_id = _create_table_with_expirable_snapshot(catalog)
13671367

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

13711371
assert result.exit_code == 0
13721372
assert result.output == "Expired snapshots on default.my_table\n"
@@ -1383,7 +1383,6 @@ def test_expire_snapshots_older_than(catalog: InMemoryCatalog) -> None:
13831383
result = runner.invoke(
13841384
run,
13851385
[
1386-
"maintenance",
13871386
"expire-snapshots",
13881387
"default.my_table",
13891388
"--older-than",
@@ -1407,7 +1406,7 @@ def test_expire_snapshots_unknown_snapshot_id(catalog: InMemoryCatalog) -> None:
14071406
)
14081407

14091408
runner = CliRunner()
1410-
result = runner.invoke(run, ["maintenance", "expire-snapshots", "default.my_table", "--snapshot-id", "999"])
1409+
result = runner.invoke(run, ["expire-snapshots", "default.my_table", "--snapshot-id", "999"])
14111410

14121411
assert result.exit_code == 1
14131412
assert "Snapshot with ID 999 does not exist." in result.output
@@ -1419,7 +1418,7 @@ def test_json_expire_snapshots_by_id(catalog: InMemoryCatalog) -> None:
14191418
runner = CliRunner()
14201419
result = runner.invoke(
14211420
run,
1422-
["--output=json", "maintenance", "expire-snapshots", "default.my_table", "--snapshot-id", str(snapshot_id)],
1421+
["--output=json", "expire-snapshots", "default.my_table", "--snapshot-id", str(snapshot_id)],
14231422
)
14241423

14251424
assert result.exit_code == 0

0 commit comments

Comments
 (0)