Skip to content

Commit

Permalink
Create namespace from CLI (apache#336)
Browse files Browse the repository at this point in the history
  • Loading branch information
hendrikmakait authored Feb 1, 2024
1 parent b1e33d4 commit 29db67f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
21 changes: 19 additions & 2 deletions pyiceberg/cli/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,23 @@ def version(ctx: Context) -> None:
ctx.obj["output"].version(__version__)


@run.group()
def create() -> None:
"""Operation to create a namespace."""


@create.command()
@click.argument("identifier")
@click.pass_context
@catch_exception()
def namespace(ctx: Context, identifier: str) -> None:
"""Create a namespace."""
catalog, output = _catalog_and_output(ctx)

catalog.create_namespace(identifier)
output.text(f"Created namespace: {identifier}")


@run.group()
def drop() -> None:
"""Operations to drop a namespace or table."""
Expand All @@ -223,11 +240,11 @@ def table(ctx: Context, identifier: str) -> None: # noqa: F811
output.text(f"Dropped table: {identifier}")


@drop.command()
@drop.command() # type: ignore
@click.argument("identifier")
@click.pass_context
@catch_exception()
def namespace(ctx: Context, identifier: str) -> None:
def namespace(ctx: Context, identifier: str) -> None: # noqa: F811
"""Drop a namespace."""
catalog, output = _catalog_and_output(ctx)

Expand Down
16 changes: 16 additions & 0 deletions tests/cli/test_console.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,22 @@ def test_drop_namespace_does_not_exists(catalog: InMemoryCatalog) -> None:
assert result.output == "Namespace does not exist: ('doesnotexist',)\n"


def test_create_namespace(catalog: InMemoryCatalog) -> None:
runner = CliRunner()
result = runner.invoke(run, ["create", "namespace", TEST_TABLE_NAMESPACE])
assert result.exit_code == 0
assert result.output == """Created namespace: default\n"""


def test_create_namespace_already_exists(catalog: InMemoryCatalog) -> None:
catalog.create_namespace(TEST_TABLE_NAMESPACE)

runner = CliRunner()
result = runner.invoke(run, ["create", "namespace", TEST_TABLE_NAMESPACE])
assert result.exit_code == 1
assert result.output == "Namespace already exists: ('default',)\n"


def test_rename_table(catalog: InMemoryCatalog) -> None:
catalog.create_table(
identifier=TEST_TABLE_IDENTIFIER,
Expand Down

0 comments on commit 29db67f

Please sign in to comment.