Skip to content

Commit

Permalink
Shorter schema version "v", and forward compatibility check
Browse files Browse the repository at this point in the history
  • Loading branch information
scosman committed Aug 19, 2024
1 parent 1f97488 commit 6310c56
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 9 deletions.
12 changes: 10 additions & 2 deletions libs/core/kiln_ai/datamodel/basemodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,20 @@


class KilnBaseModel(BaseModel):
version: int = 1
v: int = 1 # schema_version
path: Optional[Path] = None

@classmethod
def load_from_file(cls: Type[T], path: Path) -> T:
with open(path, "r") as file:
m = cls.model_validate_json(file.read(), strict=True)
m.path = path
if m.v > m.max_schema_version():
raise ValueError(
f"Cannot load from file because the schema version is higher than the current version. Upgrade kiln to the latest version. "
f"Class: {m.__class__.__name__}, id: {getattr(m, 'id', None)}, path: {path}, "
f"version: {m.v}, max version: {m.max_schema_version()}"
)
return m

def save_to_file(self) -> None:
Expand All @@ -28,4 +34,6 @@ def save_to_file(self) -> None:
with open(self.path, "w") as file:
json.dump(data, file, indent=4)

print(f"Project saved to {self.path}")
# increment for breaking changes
def max_schema_version(self) -> int:
return 1
4 changes: 4 additions & 0 deletions libs/core/kiln_ai/datamodel/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@

class KilnProject(KilnBaseModel):
name: str


class KilnTask(KilnBaseModel):
name: str
25 changes: 21 additions & 4 deletions libs/core/kiln_ai/datamodel/test_basemodel.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
import json
import pytest
from kiln_ai.datamodel.basemodel import KilnBaseModel
from pathlib import Path


@pytest.fixture
def test_file(tmp_path):
def test_file(tmp_path) -> Path:
test_file_path = tmp_path / "test_model.json"
data = {"version": 1}
data = {"v": 1}

with open(test_file_path, "w") as file:
json.dump(data, file, indent=4)

return test_file_path


@pytest.fixture
def test_newer_file(tmp_path) -> Path:
test_file_path = tmp_path / "test_model_sv.json"
data = {"v": 99}

with open(test_file_path, "w") as file:
json.dump(data, file, indent=4)
Expand All @@ -16,7 +28,7 @@ def test_file(tmp_path):

def test_load_from_file(test_file):
model = KilnBaseModel.load_from_file(test_file)
assert model.version == 1
assert model.v == 1
assert model.path == test_file


Expand All @@ -27,10 +39,15 @@ def test_save_to_file(test_file):
with open(test_file, "r") as file:
data = json.load(file)

assert data["version"] == 1
assert data["v"] == 1


def test_save_to_file_without_path():
model = KilnBaseModel()
with pytest.raises(ValueError):
model.save_to_file()


def test_max_schema_version(test_newer_file):
with pytest.raises(ValueError):
KilnBaseModel.load_from_file(test_newer_file)
6 changes: 3 additions & 3 deletions libs/core/kiln_ai/datamodel/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
@pytest.fixture
def test_file(tmp_path):
test_file_path = tmp_path / "test_project.json"
data = {"version": 1, "name": "Test Project"}
data = {"v": 1, "name": "Test Project"}

with open(test_file_path, "w") as file:
json.dump(data, file, indent=4)
Expand All @@ -16,7 +16,7 @@ def test_file(tmp_path):

def test_load_from_file(test_file):
project = KilnProject.load_from_file(test_file)
assert project.version == 1
assert project.v == 1
assert project.name == "Test Project"
assert project.path == test_file

Expand All @@ -28,7 +28,7 @@ def test_save_to_file(test_file):
with open(test_file, "r") as file:
data = json.load(file)

assert data["version"] == 1
assert data["v"] == 1
assert data["name"] == "Test Project"


Expand Down

0 comments on commit 6310c56

Please sign in to comment.