2727
2828 # fetching config
2929 new_config = store.get_model('key1')
30- print(new_config.name, new_config.base_model )
30+ print(new_config.name, new_config.base )
3131 assert new_config.key == 'key1'
3232
3333 # deleting
@@ -100,11 +100,11 @@ def _create_tables(self) -> None:
100100 """--sql
101101 CREATE TABLE IF NOT EXISTS model_config (
102102 id TEXT NOT NULL PRIMARY KEY,
103- -- These 4 fields are enums in python, unrestricted string here
104- base_model TEXT NOT NULL,
105- model_type TEXT NOT NULL,
106- model_name TEXT NOT NULL,
107- model_path TEXT NOT NULL,
103+ -- The next 3 fields are enums in python, unrestricted string here
104+ base TEXT NOT NULL,
105+ type TEXT NOT NULL,
106+ name TEXT NOT NULL,
107+ path TEXT NOT NULL,
108108 original_hash TEXT, -- could be null
109109 -- Serialized JSON representation of the whole config object,
110110 -- which will contain additional fields from subclasses
@@ -139,6 +139,15 @@ def _create_tables(self) -> None:
139139 """
140140 )
141141
142+ # Add indexes for searchable fields
143+ for stmt in [
144+ "CREATE INDEX IF NOT EXISTS base_index ON model_config(base);" ,
145+ "CREATE INDEX IF NOT EXISTS type_index ON model_config(type);" ,
146+ "CREATE INDEX IF NOT EXISTS name_index ON model_config(name);" ,
147+ "CREATE UNIQUE INDEX IF NOT EXISTS path_index ON model_config(path);" ,
148+ ]:
149+ self ._cursor .execute (stmt )
150+
142151 # Add our version to the metadata table
143152 self ._cursor .execute (
144153 """--sql
@@ -169,18 +178,18 @@ def add_model(self, key: str, config: Union[dict, ModelConfigBase]) -> ModelConf
169178 """--sql
170179 INSERT INTO model_config (
171180 id,
172- base_model ,
173- model_type ,
174- model_name ,
175- model_path ,
181+ base ,
182+ type ,
183+ name ,
184+ path ,
176185 original_hash,
177186 config
178187 )
179188 VALUES (?,?,?,?,?,?,?);
180189 """ ,
181190 (
182191 key ,
183- record .base_model ,
192+ record .base ,
184193 record .type ,
185194 record .name ,
186195 record .path ,
@@ -193,7 +202,11 @@ def add_model(self, key: str, config: Union[dict, ModelConfigBase]) -> ModelConf
193202 except sqlite3 .IntegrityError as e :
194203 self ._conn .rollback ()
195204 if "UNIQUE constraint failed" in str (e ):
196- raise DuplicateModelException (f"A model with key '{ key } ' is already installed" ) from e
205+ if "model_config.path" in str (e ):
206+ msg = f"A model with path '{ record .path } ' is already installed"
207+ else :
208+ msg = f"A model with key '{ key } ' is already installed"
209+ raise DuplicateModelException (msg ) from e
197210 else :
198211 raise e
199212 except sqlite3 .Error as e :
@@ -257,14 +270,14 @@ def update_model(self, key: str, config: Union[dict, ModelConfigBase]) -> ModelC
257270 self ._cursor .execute (
258271 """--sql
259272 UPDATE model_config
260- SET base_model =?,
261- model_type =?,
262- model_name =?,
263- model_path =?,
273+ SET base =?,
274+ type =?,
275+ name =?,
276+ path =?,
264277 config=?
265278 WHERE id=?;
266279 """ ,
267- (record .base_model , record .type , record .name , record .path , json_serialized , key ),
280+ (record .base , record .type , record .name , record .path , json_serialized , key ),
268281 )
269282 if self ._cursor .rowcount == 0 :
270283 raise UnknownModelException ("model not found" )
@@ -338,13 +351,13 @@ def search_by_name(
338351 where_clause = []
339352 bindings = []
340353 if model_name :
341- where_clause .append ("model_name =?" )
354+ where_clause .append ("name =?" )
342355 bindings .append (model_name )
343356 if base_model :
344- where_clause .append ("base_model =?" )
357+ where_clause .append ("base =?" )
345358 bindings .append (base_model )
346359 if model_type :
347- where_clause .append ("model_type =?" )
360+ where_clause .append ("type =?" )
348361 bindings .append (model_type )
349362 where = f"WHERE { ' AND ' .join (where_clause )} " if where_clause else ""
350363 with self ._lock :
0 commit comments