Skip to content

Commit

Permalink
block updates to read only registry tables
Browse files Browse the repository at this point in the history
Signed-off-by: Ayush Kamat <[email protected]>
  • Loading branch information
ayushkamat committed Oct 22, 2024
1 parent 06fe2bf commit 219d2f8
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions latch/registry/table.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
import sys
import time
from contextlib import contextmanager
Expand Down Expand Up @@ -76,6 +77,7 @@ class _ColumnNode(TypedDict("_ColumnNodeReserved", {"def": DBValue})):

@dataclass
class _Cache:
read_only: Optional[bool] = None
display_name: Optional[str] = None
columns: Optional[Dict[str, Column]] = None
project_id: Optional[str] = None
Expand Down Expand Up @@ -123,6 +125,7 @@ def load(self) -> None:
}
}
projectId
metadata
}
}
"""),
Expand All @@ -134,6 +137,13 @@ def load(self) -> None:
f"table does not exist or you lack permissions: id={self.id}"
)

try:
metadata = data["metadata"]
read_only = "benchlingSchemaId" in json.loads(metadata)
except (KeyError, TypeError, json.JSONDecodeError):
read_only = False

self._cache.read_only = read_only
self._cache.project_id = data["projectId"]
self._cache.display_name = data["displayName"]

Expand Down Expand Up @@ -233,6 +243,28 @@ def get_columns(

return self._cache.columns

@overload
def read_only(self, *, load_if_missing: Literal[True] = True) -> bool: ...

@overload
def read_only(self, *, load_if_missing: bool) -> Optional[bool]: ...

def read_only(self, *, load_if_missing: bool = True) -> Optional[bool]:
"""Check whether or not this table is read-only
Args:
load_if_missing:
If true, :meth:`load` the read_only status if not in cache.
If false, return `None` if not in cache.
Returns:
Mapping between column keys and :class:`columns <latch.registry.types.Column>`.
"""
if self._cache.read_only is None and load_if_missing:
self.load()

return self._cache.read_only

def list_records(self, *, page_size: int = 100) -> Iterator[Dict[str, Record]]:
"""List Registry records contained in this table.
Expand Down Expand Up @@ -364,6 +396,9 @@ def update(self, *, reload_on_commit: bool = True) -> Iterator["TableUpdate"]:
Context manager for the new transaction.
"""

if self.read_only():
raise RuntimeError(f"cannot update read-only table: id={self.id}")

upd = TableUpdate(self)
yield upd
upd.commit()
Expand Down

0 comments on commit 219d2f8

Please sign in to comment.