Skip to content

Commit 6e72835

Browse files
authored
feat: Method to update table metadata (#33)
1 parent c5a47c5 commit 6e72835

File tree

4 files changed

+114
-1
lines changed

4 files changed

+114
-1
lines changed

nisystemlink/clients/dataframe/_data_frame_client.py

+12-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
from nisystemlink.clients import core
99
from nisystemlink.clients.core._uplink._base_client import BaseClient
10-
from uplink import Body, delete, get, json, post, Query, returns
10+
from uplink import Body, delete, get, json, patch, Path, post, Query, returns
1111

1212
from . import models
1313

@@ -107,6 +107,17 @@ def get_table_metadata(self, id: str) -> models.TableMetadata:
107107
"""
108108
...
109109

110+
@json
111+
@patch(_BASE_PATH + "/tables/{id}", args=(Path, Body))
112+
def modify_table(self, id: str, update: models.ModifyTableRequest) -> None:
113+
"""Modify properties of a table or its columns.
114+
115+
Args:
116+
id: Unique ID of a DataFrame table.
117+
update: The metadata to update.
118+
"""
119+
...
120+
110121
@delete(_BASE_PATH + "/tables/{id}")
111122
def delete_table(self, id: str) -> None:
112123
"""Deletes a table.

nisystemlink/clients/dataframe/models/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from ._column import Column
44
from ._column_type import ColumnType
55
from ._data_type import DataType
6+
from ._modify_table_request import ColumnMetadataPatch, ModifyTableRequest
67
from ._order_by import OrderBy
78
from ._paged_tables import PagedTables
89
from ._query_tables_request import QueryTablesRequest
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
from typing import Dict, List, Optional
2+
3+
from nisystemlink.clients.core._uplink._json_model import JsonModel
4+
5+
6+
class ColumnMetadataPatch(JsonModel):
7+
"""Specifies column properties to add, modify, or delete when editing table metadata."""
8+
9+
name: str
10+
"""The name of the column to modify."""
11+
12+
properties: Dict[str, Optional[str]]
13+
"""The properties to modify. A map of key value properties containing the metadata
14+
to be added or modified. Setting a property value to ``None`` will delete the
15+
property."""
16+
17+
18+
class ModifyTableRequest(JsonModel):
19+
"""Contains the metadata properties to modify. Values not included will remain unchanged."""
20+
21+
metadata_revision: Optional[int] = None
22+
"""When specified, this is an integer that must match the last known
23+
revision number of the table, incremented by one. If it doesn't match the
24+
current ``metadataRevision`` incremented by one at the time of execution, the
25+
modify request will be rejected with a 409 Conflict. This is used to ensure
26+
that changes to this table's metadata are based on a known, previous
27+
state."""
28+
29+
name: Optional[str] = None
30+
"""The new name of the table. Setting to ``None`` will reset the name to the table's ID."""
31+
32+
workspace: Optional[str] = None
33+
"""The new workspace for the table. Setting to ``None`` will reset to the
34+
default workspace. Changing the workspace requires permission to delete the
35+
table in its current workspace and permission to create the table in its new
36+
workspace."""
37+
38+
properties: Optional[Dict[str, Optional[str]]] = None
39+
"""The properties to modify. A map of key value properties containing the
40+
metadata to be added or modified. Setting a property value to ``None`` will
41+
delete the property."""
42+
43+
columns: Optional[List[ColumnMetadataPatch]] = None
44+
"""Updates to the column properties. Cannot add or remove columns, or change the name of a column."""

tests/integration/dataframe/test_dataframe.py

+57
Original file line numberDiff line numberDiff line change
@@ -156,3 +156,60 @@ def test__query_tables__returns(
156156
second_page = client.query_tables(query)
157157
assert len(second_page.tables) == 1
158158
assert second_page.continuation_token is None
159+
160+
def test__modify_table__returns(self, client: DataFrameClient, create_table):
161+
id = create_table(
162+
models.CreateTableRequest(
163+
columns=[
164+
models.Column(
165+
name="index",
166+
data_type=models.DataType.Int32,
167+
column_type=models.ColumnType.Index,
168+
)
169+
]
170+
)
171+
)
172+
173+
client.modify_table(
174+
id,
175+
models.ModifyTableRequest(
176+
metadata_revision=2,
177+
name="Modified table",
178+
properties={"cow": "moo"},
179+
columns=[
180+
models.ColumnMetadataPatch(
181+
name="index", properties={"sheep": "baa"}
182+
)
183+
],
184+
),
185+
)
186+
table = client.get_table_metadata(id)
187+
188+
assert table.metadata_revision == 2
189+
assert table.name == "Modified table"
190+
assert table.properties == {"cow": "moo"}
191+
assert table.columns[0].properties == {"sheep": "baa"}
192+
193+
client.modify_table(id, models.ModifyTableRequest(properties={"bee": "buzz"}))
194+
table = client.get_table_metadata(id)
195+
196+
assert table.properties == {"cow": "moo", "bee": "buzz"}
197+
assert table.name == "Modified table"
198+
199+
client.modify_table(
200+
id,
201+
models.ModifyTableRequest(
202+
metadata_revision=4,
203+
name=None,
204+
properties={"cow": None},
205+
columns=[
206+
models.ColumnMetadataPatch(name="index", properties={"sheep": None})
207+
],
208+
),
209+
)
210+
table = client.get_table_metadata(id)
211+
212+
assert table.metadata_revision == 4
213+
assert table.name == id
214+
assert table.properties == {"bee": "buzz"}
215+
assert table.columns[0].properties == {}

0 commit comments

Comments
 (0)