1+ """ORM Models for v2 tables and objects."""
2+
13from datetime import datetime
24from typing import Any
35from uuid import NAMESPACE_DNS , UUID , uuid5
46
5- from pydantic import AliasChoices , ValidationInfo , model_validator
7+ from pydantic import AliasChoices
68from sqlalchemy .dialects import postgresql
79from sqlalchemy .ext .mutable import MutableDict , MutableList
810from sqlalchemy .types import PickleType
@@ -43,15 +45,6 @@ def jsonb_column(name: str, aliases: list[str] | None = None) -> Any:
4345 )
4446
4547
46- # NOTES
47- # - model validation is not triggered when table=True
48- # - Every object model needs to have three flavors:
49- # 1. the declarative model of the object's database table
50- # 2. the model of the manifest when creating a new object
51- # 3. the model of the manifest when updating an object
52- # 4. a response model for APIs related to the object
53-
54-
5548class BaseSQLModel (SQLModel ):
5649 __table_args__ = {"schema" : config .db .table_schema }
5750 metadata = metadata
@@ -72,26 +65,7 @@ class CampaignBase(BaseSQLModel):
7265 configuration : dict = jsonb_column ("configuration" , aliases = ["configuration" , "data" , "spec" ])
7366
7467
75- class CampaignModel (CampaignBase ):
76- """model used for resource creation."""
77-
78- @model_validator (mode = "before" )
79- @classmethod
80- def custom_model_validator (cls , data : Any , info : ValidationInfo ) -> Any :
81- """Validates the model based on different types of raw inputs,
82- where some default non-optional fields can be auto-populated.
83- """
84- if isinstance (data , dict ):
85- if "name" not in data :
86- raise ValueError ("'name' must be specified." )
87- if "namespace" not in data :
88- data ["namespace" ] = _default_campaign_namespace
89- if "id" not in data :
90- data ["id" ] = uuid5 (namespace = data ["namespace" ], name = data ["name" ])
91- return data
92-
93-
94- class Campaign (CampaignModel , table = True ):
68+ class Campaign (CampaignBase , table = True ):
9569 """Model used for database operations involving campaigns_v2 table rows"""
9670
9771 __tablename__ : str = "campaigns_v2" # type: ignore[misc]
@@ -127,25 +101,7 @@ class NodeBase(BaseSQLModel):
127101 configuration : dict = jsonb_column ("configuration" , aliases = ["configuration" , "data" , "spec" ])
128102
129103
130- class NodeModel (NodeBase ):
131- """model validating class for Nodes"""
132-
133- @model_validator (mode = "before" )
134- @classmethod
135- def custom_model_validator (cls , data : Any , info : ValidationInfo ) -> Any :
136- if isinstance (data , dict ):
137- if "version" not in data :
138- data ["version" ] = 1
139- if "name" not in data :
140- raise ValueError ("'name' must be specified." )
141- if "namespace" not in data :
142- data ["namespace" ] = _default_campaign_namespace
143- if "id" not in data :
144- data ["id" ] = uuid5 (namespace = data ["namespace" ], name = f"""{ data ["name" ]} .{ data ["version" ]} """ )
145- return data
146-
147-
148- class Node (NodeModel , table = True ):
104+ class Node (NodeBase , table = True ):
149105 __tablename__ : str = "nodes_v2" # type: ignore[misc]
150106
151107 machine : UUID | None = Field (foreign_key = "machines_v2.id" , default = None , ondelete = "CASCADE" )
@@ -163,28 +119,12 @@ class EdgeBase(BaseSQLModel):
163119 configuration : dict = jsonb_column ("configuration" , aliases = ["configuration" , "data" , "spec" ])
164120
165121
166- class EdgeModel (EdgeBase ):
167- """model validating class for Edges"""
168-
169- @model_validator (mode = "before" )
170- @classmethod
171- def custom_model_validator (cls , data : Any , info : ValidationInfo ) -> Any :
172- if isinstance (data , dict ):
173- if "name" not in data :
174- raise ValueError ("'name' must be specified." )
175- if "namespace" not in data :
176- raise ValueError ("Edges may only exist in a 'namespace'." )
177- if "id" not in data :
178- data ["id" ] = uuid5 (namespace = data ["namespace" ], name = data ["name" ])
179- return data
180-
181-
182- class EdgeResponseModel (EdgeModel ):
122+ class EdgeResponseModel (EdgeBase ):
183123 source : Any
184124 target : Any
185125
186126
187- class Edge (EdgeModel , table = True ):
127+ class Edge (EdgeBase , table = True ):
188128 __tablename__ : str = "edges_v2" # type: ignore[misc]
189129
190130
@@ -216,24 +156,6 @@ class ManifestBase(BaseSQLModel):
216156 spec : dict = jsonb_column ("spec" , aliases = ["spec" , "configuration" , "data" ])
217157
218158
219- class ManifestModel (ManifestBase ):
220- """model validating class for Manifests"""
221-
222- @model_validator (mode = "before" )
223- @classmethod
224- def custom_model_validator (cls , data : Any , info : ValidationInfo ) -> Any :
225- if isinstance (data , dict ):
226- if "version" not in data :
227- data ["version" ] = 1
228- if "name" not in data :
229- raise ValueError ("'name' must be specified." )
230- if "namespace" not in data :
231- data ["namespace" ] = _default_campaign_namespace
232- if "id" not in data :
233- data ["id" ] = uuid5 (namespace = data ["namespace" ], name = f"""{ data ["name" ]} .{ data ["version" ]} """ )
234- return data
235-
236-
237159class Manifest (ManifestBase , table = True ):
238160 __tablename__ : str = "manifests_v2" # type: ignore[misc]
239161
0 commit comments