Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

block updates to read only registry tables #503

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading