Skip to content

Commit

Permalink
views
Browse files Browse the repository at this point in the history
  • Loading branch information
joocer committed Jul 31, 2024
1 parent c557b47 commit 6209288
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 5 deletions.
49 changes: 46 additions & 3 deletions tarchia/api/v1/view_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

from tarchia.interfaces.catalog import catalog_factory
from tarchia.models import CreateViewRequest
from tarchia.models import UpdateMetadataRequest
from tarchia.models import UpdateValueRequest
from tarchia.models import ViewCatalogEntry
from tarchia.utils import get_base_url
from tarchia.utils.constants import IDENTIFIER_REG_EX
Expand Down Expand Up @@ -90,7 +92,7 @@ async def create_view(
last_updated_ms=timestamp,
)
# Save the table to the Catalog - do this last
catalog_provider.update_view(table_id=new_view.table_id, entry=new_view)
catalog_provider.update_view(view_id=new_view.view_id, entry=new_view)

# trigger webhooks - this should be async so we don't wait for the outcome
owner_entry.trigger_event(
Expand All @@ -103,8 +105,8 @@ async def create_view(
)

return {
"message": "Table Created",
"table": f"{owner}.{table_definition.name}",
"message": "View Created",
"view": f"{owner}.{view_definition.name}",
}


Expand Down Expand Up @@ -144,3 +146,44 @@ async def delete_view(
"message": "View Deleted",
"view": f"{owner}.{view}",
}


@router.patch("/views/{owner}/{view}/{attribute}", response_class=ORJSONResponse)
async def update_view(
value: UpdateValueRequest,
attribute: str,
owner: str = Path(description="The owner of the table.", pattern=IDENTIFIER_REG_EX),
view: str = Path(description="The name of the view.", pattern=IDENTIFIER_REG_EX),
):
from tarchia.utils.catalogs import identify_view

if attribute not in {"statement", "description"}:
raise ValueError(f"Data attribute {attribute} cannot be modified via the API")

catalog_entry = identify_view(owner, view)
setattr(catalog_entry, attribute, value.value)
catalog_provider.update_view(view_id=catalog_entry.view_id, entry=catalog_entry)

return {
"message": "View updated",
"view": f"{owner}.{view}",
}


@router.patch("/views/{owner}/{view}/metadata")
async def update_metadata(
metadata: UpdateMetadataRequest,
owner: str = Path(description="The owner of the table.", pattern=IDENTIFIER_REG_EX),
view: str = Path(description="The name of the view.", pattern=IDENTIFIER_REG_EX),
):
from tarchia.utils.catalogs import identify_view

catalog_entry = identify_view(owner, view)
view_id = catalog_entry.view_id
catalog_entry.metadata = metadata.metadata
catalog_provider.update_view(view_id=view_id, entry=catalog_entry)

return {
"message": "Metadata updated",
"view": f"{owner}.{view}",
}
12 changes: 12 additions & 0 deletions tarchia/interfaces/catalog/dev_catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from tarchia.interfaces.catalog.provider_base import CatalogProvider
from tarchia.models import OwnerEntry
from tarchia.models import TableCatalogEntry
from tarchia.models import ViewCatalogEntry


class DevelopmentCatalogProvider(CatalogProvider):
Expand Down Expand Up @@ -117,3 +118,14 @@ def delete_view(self, view_id: str) -> None:
table_id (str): The identifier of the table to be deleted.
"""
self.store.delete("views", {"view_id": view_id})

def update_view(self, view_id: str, entry: ViewCatalogEntry) -> None:
"""
Update the metadata for a specified table.
Parameters:
table_id (str): The identifier of the table.
metadata (Dict[str, Any]): A dictionary containing the metadata to be updated.
"""

self.store.upsert("views", entry.as_dict(), {"view_id": view_id})
13 changes: 13 additions & 0 deletions tarchia/interfaces/catalog/gcs_firestore.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from tarchia.interfaces.catalog.provider_base import CatalogProvider
from tarchia.models import OwnerEntry
from tarchia.models import TableCatalogEntry
from tarchia.models import ViewCatalogEntry
from tarchia.utils import config

GCP_PROJECT_ID = config.GCP_PROJECT_ID
Expand Down Expand Up @@ -187,3 +188,15 @@ def delete_view(self, view_id: str) -> None:
view_id (str): The identifier of the table to be deleted.
"""
self.database.collection(self.collection).document(f"view-{view_id}").delete()

def update_view(self, view_id: str, entry: ViewCatalogEntry) -> None:
"""
Update the metadata for a specified table.
Parameters:
table_id (str): The identifier of the table.
metadata (Dict[str, Any]): A dictionary containing the metadata to be updated.
"""
self.database.collection(self.collection).document(f"view-{entry.view_id}").set(
entry.as_dict()
)
13 changes: 13 additions & 0 deletions tarchia/interfaces/catalog/provider_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from tarchia.models import OwnerEntry
from tarchia.models import TableCatalogEntry
from tarchia.models import ViewCatalogEntry


class CatalogProvider: # pragma: no cover
Expand Down Expand Up @@ -112,3 +113,15 @@ def delete_view(self, view_id: str) -> None:
raise NotImplementedError(
f"{self.__class__.__name__}.{inspect.currentframe().f_code.co_name} is not implemented."
)

def update_view(self, view_id: str, entry: ViewCatalogEntry) -> None:
"""
Update the metadata for a specified table.
Parameters:
table_id (str): The identifier of the table.
entry (Dict[str, Any]): A dictionary containing the metadata to be updated.
"""
raise NotImplementedError(
f"{self.__class__.__name__}.{inspect.currentframe().f_code.co_name} is not implemented."
)
2 changes: 1 addition & 1 deletion tarchia/utils/catalogs.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def identify_owner(name: str) -> OwnerEntry:

def identify_view(owner: str, view: str) -> ViewCatalogEntry:
"""Get the catalog entry for a table name/identifier"""
catalog_entry = catalog_provider.get_view(view=view)
catalog_entry = catalog_provider.get_view(owner=owner, view=view)
if catalog_entry is None:
raise ViewNotFoundError(owner=owner, view=view)
return ViewCatalogEntry(**catalog_entry)
Expand Down
2 changes: 1 addition & 1 deletion tests/endpoints/test_view_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def test_create_read_update_delete_view():
# can we retrieve this table?
response = client.get(url=f"/v1/views/{TEST_OWNER}/test_view")
assert response.status_code == 200, f"{response.status_code} - {response.content}"
assert response.json()["visibility"] == "PRIVATE"
assert response.json()["statement"] == "SELECT * FROM $planets"

# we shouldn't be able to update the table_id
response = client.patch(url=f"/v1/tables/{TEST_OWNER}/test_view/view_id", content="1234")
Expand Down

0 comments on commit 6209288

Please sign in to comment.