@@ -26,7 +26,7 @@ pub async fn connect(url: &str) -> PgPool {
2626/// These can have "global" as the deployment ID.
2727#[ derive( Debug , Clone ) ]
2828struct DbCostModel {
29- pub deployment : String ,
29+ pub deployment : Option < String > ,
3030 pub model : Option < String > ,
3131 pub variables : Option < Value > ,
3232}
@@ -46,7 +46,12 @@ impl TryFrom<DbCostModel> for CostModel {
4646
4747 fn try_from ( db_model : DbCostModel ) -> Result < Self , Self :: Error > {
4848 Ok ( Self {
49- deployment : DeploymentId :: from_str ( & db_model. deployment ) ?,
49+ deployment : DeploymentId :: from_str ( & db_model. deployment . ok_or (
50+ ParseDeploymentIdError :: InvalidIpfsHashLength {
51+ value : String :: new ( ) ,
52+ length : 0 ,
53+ } ,
54+ ) ?) ?,
5055 model : db_model. model ,
5156 variables : db_model. variables ,
5257 } )
@@ -57,7 +62,7 @@ impl From<CostModel> for DbCostModel {
5762 fn from ( model : CostModel ) -> Self {
5863 let deployment = model. deployment ;
5964 DbCostModel {
60- deployment : format ! ( "{deployment:#x}" ) ,
65+ deployment : Some ( format ! ( "{deployment:#x}" ) ) ,
6166 model : model. model ,
6267 variables : model. variables ,
6368 }
@@ -210,28 +215,11 @@ mod test {
210215
211216 use super :: * ;
212217
213- async fn setup_cost_models_table ( pool : & PgPool ) {
214- sqlx:: query!(
215- r#"
216- CREATE TABLE "CostModels"(
217- id INT,
218- deployment VARCHAR NOT NULL,
219- model TEXT,
220- variables JSONB,
221- PRIMARY KEY( deployment )
222- );
223- "# ,
224- )
225- . execute ( pool)
226- . await
227- . expect ( "Create test instance in db" ) ;
228- }
229-
230218 async fn add_cost_models ( pool : & PgPool , models : Vec < DbCostModel > ) {
231219 for model in models {
232220 sqlx:: query!(
233221 r#"
234- INSERT INTO "CostModels " (deployment, model)
222+ INSERT INTO "CostModelsHistory " (deployment, model)
235223 VALUES ($1, $2);
236224 "# ,
237225 model. deployment,
@@ -249,7 +237,7 @@ mod test {
249237
250238 fn global_cost_model ( ) -> DbCostModel {
251239 DbCostModel {
252- deployment : "global" . to_string ( ) ,
240+ deployment : Some ( "global" . to_string ( ) ) ,
253241 model : Some ( "default => 0.00001;" . to_string ( ) ) ,
254242 variables : None ,
255243 }
@@ -281,15 +269,14 @@ mod test {
281269 ]
282270 }
283271
284- #[ sqlx:: test]
272+ #[ sqlx:: test( migrations = "../migrations" ) ]
285273 async fn success_cost_models ( pool : PgPool ) {
286274 let test_models = test_data ( ) ;
287275 let test_deployments = test_models
288276 . iter ( )
289277 . map ( |model| model. deployment )
290278 . collect :: < HashSet < _ > > ( ) ;
291279
292- setup_cost_models_table ( & pool) . await ;
293280 add_cost_models ( & pool, to_db_models ( test_models. clone ( ) ) ) . await ;
294281
295282 // First test: query without deployment filter
@@ -344,7 +331,7 @@ mod test {
344331 }
345332 }
346333
347- #[ sqlx:: test]
334+ #[ sqlx:: test( migrations = "../migrations" ) ]
348335 async fn global_fallback_cost_models ( pool : PgPool ) {
349336 let test_models = test_data ( ) ;
350337 let test_deployments = test_models
@@ -353,7 +340,6 @@ mod test {
353340 . collect :: < HashSet < _ > > ( ) ;
354341 let global_model = global_cost_model ( ) ;
355342
356- setup_cost_models_table ( & pool) . await ;
357343 add_cost_models ( & pool, to_db_models ( test_models. clone ( ) ) ) . await ;
358344 add_cost_models ( & pool, vec ! [ global_model. clone( ) ] ) . await ;
359345
@@ -436,9 +422,8 @@ mod test {
436422 assert_eq ! ( missing_model. model, global_model. model) ;
437423 }
438424
439- #[ sqlx:: test]
425+ #[ sqlx:: test( migrations = "../migrations" ) ]
440426 async fn success_cost_model ( pool : PgPool ) {
441- setup_cost_models_table ( & pool) . await ;
442427 add_cost_models ( & pool, to_db_models ( test_data ( ) ) ) . await ;
443428
444429 let deployment_id_from_bytes = DeploymentId :: from_str (
@@ -459,12 +444,11 @@ mod test {
459444 assert_eq ! ( model. model, Some ( "default => 0.00025;" . to_string( ) ) ) ;
460445 }
461446
462- #[ sqlx:: test]
447+ #[ sqlx:: test( migrations = "../migrations" ) ]
463448 async fn global_fallback_cost_model ( pool : PgPool ) {
464449 let test_models = test_data ( ) ;
465450 let global_model = global_cost_model ( ) ;
466451
467- setup_cost_models_table ( & pool) . await ;
468452 add_cost_models ( & pool, to_db_models ( test_models. clone ( ) ) ) . await ;
469453 add_cost_models ( & pool, vec ! [ global_model. clone( ) ] ) . await ;
470454
0 commit comments