Skip to content

Commit

Permalink
feat: Method to update table metadata (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
mure committed Nov 30, 2022
1 parent c5a47c5 commit 6e72835
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 1 deletion.
13 changes: 12 additions & 1 deletion nisystemlink/clients/dataframe/_data_frame_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from nisystemlink.clients import core
from nisystemlink.clients.core._uplink._base_client import BaseClient
from uplink import Body, delete, get, json, post, Query, returns
from uplink import Body, delete, get, json, patch, Path, post, Query, returns

from . import models

Expand Down Expand Up @@ -107,6 +107,17 @@ def get_table_metadata(self, id: str) -> models.TableMetadata:
"""
...

@json
@patch(_BASE_PATH + "/tables/{id}", args=(Path, Body))
def modify_table(self, id: str, update: models.ModifyTableRequest) -> None:
"""Modify properties of a table or its columns.
Args:
id: Unique ID of a DataFrame table.
update: The metadata to update.
"""
...

@delete(_BASE_PATH + "/tables/{id}")
def delete_table(self, id: str) -> None:
"""Deletes a table.
Expand Down
1 change: 1 addition & 0 deletions nisystemlink/clients/dataframe/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from ._column import Column
from ._column_type import ColumnType
from ._data_type import DataType
from ._modify_table_request import ColumnMetadataPatch, ModifyTableRequest
from ._order_by import OrderBy
from ._paged_tables import PagedTables
from ._query_tables_request import QueryTablesRequest
Expand Down
44 changes: 44 additions & 0 deletions nisystemlink/clients/dataframe/models/_modify_table_request.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from typing import Dict, List, Optional

from nisystemlink.clients.core._uplink._json_model import JsonModel


class ColumnMetadataPatch(JsonModel):
"""Specifies column properties to add, modify, or delete when editing table metadata."""

name: str
"""The name of the column to modify."""

properties: Dict[str, Optional[str]]
"""The properties to modify. A map of key value properties containing the metadata
to be added or modified. Setting a property value to ``None`` will delete the
property."""


class ModifyTableRequest(JsonModel):
"""Contains the metadata properties to modify. Values not included will remain unchanged."""

metadata_revision: Optional[int] = None
"""When specified, this is an integer that must match the last known
revision number of the table, incremented by one. If it doesn't match the
current ``metadataRevision`` incremented by one at the time of execution, the
modify request will be rejected with a 409 Conflict. This is used to ensure
that changes to this table's metadata are based on a known, previous
state."""

name: Optional[str] = None
"""The new name of the table. Setting to ``None`` will reset the name to the table's ID."""

workspace: Optional[str] = None
"""The new workspace for the table. Setting to ``None`` will reset to the
default workspace. Changing the workspace requires permission to delete the
table in its current workspace and permission to create the table in its new
workspace."""

properties: Optional[Dict[str, Optional[str]]] = None
"""The properties to modify. A map of key value properties containing the
metadata to be added or modified. Setting a property value to ``None`` will
delete the property."""

columns: Optional[List[ColumnMetadataPatch]] = None
"""Updates to the column properties. Cannot add or remove columns, or change the name of a column."""
57 changes: 57 additions & 0 deletions tests/integration/dataframe/test_dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,60 @@ def test__query_tables__returns(
second_page = client.query_tables(query)
assert len(second_page.tables) == 1
assert second_page.continuation_token is None

def test__modify_table__returns(self, client: DataFrameClient, create_table):
id = create_table(
models.CreateTableRequest(
columns=[
models.Column(
name="index",
data_type=models.DataType.Int32,
column_type=models.ColumnType.Index,
)
]
)
)

client.modify_table(
id,
models.ModifyTableRequest(
metadata_revision=2,
name="Modified table",
properties={"cow": "moo"},
columns=[
models.ColumnMetadataPatch(
name="index", properties={"sheep": "baa"}
)
],
),
)
table = client.get_table_metadata(id)

assert table.metadata_revision == 2
assert table.name == "Modified table"
assert table.properties == {"cow": "moo"}
assert table.columns[0].properties == {"sheep": "baa"}

client.modify_table(id, models.ModifyTableRequest(properties={"bee": "buzz"}))
table = client.get_table_metadata(id)

assert table.properties == {"cow": "moo", "bee": "buzz"}
assert table.name == "Modified table"

client.modify_table(
id,
models.ModifyTableRequest(
metadata_revision=4,
name=None,
properties={"cow": None},
columns=[
models.ColumnMetadataPatch(name="index", properties={"sheep": None})
],
),
)
table = client.get_table_metadata(id)

assert table.metadata_revision == 4
assert table.name == id
assert table.properties == {"bee": "buzz"}
assert table.columns[0].properties == {}

0 comments on commit 6e72835

Please sign in to comment.