Skip to content

Commit 29db67f

Browse files
Create namespace from CLI (#336)
1 parent b1e33d4 commit 29db67f

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

pyiceberg/cli/console.py

+19-2
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,23 @@ def version(ctx: Context) -> None:
206206
ctx.obj["output"].version(__version__)
207207

208208

209+
@run.group()
210+
def create() -> None:
211+
"""Operation to create a namespace."""
212+
213+
214+
@create.command()
215+
@click.argument("identifier")
216+
@click.pass_context
217+
@catch_exception()
218+
def namespace(ctx: Context, identifier: str) -> None:
219+
"""Create a namespace."""
220+
catalog, output = _catalog_and_output(ctx)
221+
222+
catalog.create_namespace(identifier)
223+
output.text(f"Created namespace: {identifier}")
224+
225+
209226
@run.group()
210227
def drop() -> None:
211228
"""Operations to drop a namespace or table."""
@@ -223,11 +240,11 @@ def table(ctx: Context, identifier: str) -> None: # noqa: F811
223240
output.text(f"Dropped table: {identifier}")
224241

225242

226-
@drop.command()
243+
@drop.command() # type: ignore
227244
@click.argument("identifier")
228245
@click.pass_context
229246
@catch_exception()
230-
def namespace(ctx: Context, identifier: str) -> None:
247+
def namespace(ctx: Context, identifier: str) -> None: # noqa: F811
231248
"""Drop a namespace."""
232249
catalog, output = _catalog_and_output(ctx)
233250

tests/cli/test_console.py

+16
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,22 @@ def test_drop_namespace_does_not_exists(catalog: InMemoryCatalog) -> None:
308308
assert result.output == "Namespace does not exist: ('doesnotexist',)\n"
309309

310310

311+
def test_create_namespace(catalog: InMemoryCatalog) -> None:
312+
runner = CliRunner()
313+
result = runner.invoke(run, ["create", "namespace", TEST_TABLE_NAMESPACE])
314+
assert result.exit_code == 0
315+
assert result.output == """Created namespace: default\n"""
316+
317+
318+
def test_create_namespace_already_exists(catalog: InMemoryCatalog) -> None:
319+
catalog.create_namespace(TEST_TABLE_NAMESPACE)
320+
321+
runner = CliRunner()
322+
result = runner.invoke(run, ["create", "namespace", TEST_TABLE_NAMESPACE])
323+
assert result.exit_code == 1
324+
assert result.output == "Namespace already exists: ('default',)\n"
325+
326+
311327
def test_rename_table(catalog: InMemoryCatalog) -> None:
312328
catalog.create_table(
313329
identifier=TEST_TABLE_IDENTIFIER,

0 commit comments

Comments
 (0)