@@ -29,7 +29,8 @@ use superposition_sdk::Client;
2929
3030use crate :: middleware:: auth:: { validate_user, AuthResponse , ADMIN } ;
3131use crate :: types:: { ABError , AppState } ;
32- use crate :: utils:: document:: { schema_doc_to_hashmap, value_to_document} ;
32+ use crate :: utils:: document:: schema_doc_to_hashmap;
33+ use crate :: utils:: migrations:: { migrate_superposition_workspace, SuperpositionMigrationStrategy } ;
3334use diesel:: RunQueryDsl ;
3435
3536use crate :: utils:: db:: models:: { NewWorkspaceName , WorkspaceName } ;
@@ -66,7 +67,7 @@ struct ApplicationCreateRequest {
6667 application : String ,
6768}
6869
69- fn default_config < T : Clone > (
70+ pub fn default_config < T : Clone > (
7071 superposition_client : Client ,
7172 workspace_name : String ,
7273 superposition_org : String ,
@@ -321,7 +322,7 @@ async fn add_application(
321322 superposition_org_id_from_env
322323 ) ;
323324 // Insert and get the inserted row (to get the id)
324- let inserted_workspace: WorkspaceName = diesel:: insert_into ( workspace_names:: table)
325+ let mut inserted_workspace: WorkspaceName = diesel:: insert_into ( workspace_names:: table)
325326 . values ( & new_workspace_name)
326327 . get_result ( & mut conn)
327328 . map_err ( |e| {
@@ -330,6 +331,7 @@ async fn add_application(
330331
331332 let generated_id = inserted_workspace. id ;
332333 let generated_workspace_name = format ! ( "workspace{}" , generated_id) ;
334+ inserted_workspace. workspace_name = generated_workspace_name. clone ( ) ;
333335
334336 // Update the workspace_name to "workspace{id}"
335337 diesel:: update ( workspace_names:: table. filter ( workspace_names:: id. eq ( generated_id) ) )
@@ -375,37 +377,15 @@ async fn add_application(
375377 }
376378 } ;
377379
378- // Step 5: Create default configurations
379- let create_default_config_string = default_config :: < String > (
380- state. superposition_client . clone ( ) ,
381- generated_workspace_name. clone ( ) ,
382- superposition_org_id_from_env. clone ( ) , // Use ID from env
383- ) ;
384- let create_default_config_int = default_config :: < i32 > (
385- state. superposition_client . clone ( ) ,
386- generated_workspace_name. clone ( ) ,
387- superposition_org_id_from_env. clone ( ) , // Use ID from env
388- ) ;
389- let create_default_config_doc = default_config :: < Document > (
390- state. superposition_client . clone ( ) ,
391- generated_workspace_name. clone ( ) ,
392- superposition_org_id_from_env. clone ( ) , // Use ID from env
393- ) ;
394- let create_default_config_array = default_config :: < Vec < Document > > (
395- state. superposition_client . clone ( ) ,
396- generated_workspace_name. clone ( ) ,
397- superposition_org_id_from_env. clone ( ) , // Use ID from env
398- ) ;
399-
400380 // Helper function to create default config with error handling
401- async fn create_config_with_tx < T , E > (
402- create_fn : impl futures:: Future < Output = Result < T , E > > ,
381+ async fn create_config_with_tx < E > (
382+ create_fn : impl futures:: Future < Output = Result < ( ) , E > > ,
403383 key : & str ,
404384 transaction : & TransactionManager ,
405385 admin : & KeycloakAdmin ,
406386 realm : & str ,
407387 state : & web:: Data < AppState > ,
408- ) -> Result < T , actix_web :: Error >
388+ ) -> Result < ( ) , ABError >
409389 where
410390 E : std:: fmt:: Display ,
411391 {
@@ -423,183 +403,33 @@ async fn add_application(
423403 info ! ( "Rollback failed: {}" , rollback_err) ;
424404 }
425405
426- Err ( error :: ErrorInternalServerError ( format ! (
406+ Err ( ABError :: InternalServerError ( format ! (
427407 "Failed to create configuration for {}: {}" ,
428408 key, e
429409 ) ) )
430410 }
431411 }
432412 }
433413
434- // Create all configurations with transaction-aware error handling
435- create_config_with_tx (
436- create_default_config_string (
437- "config.version" . to_string ( ) ,
438- "0.0.0" . to_string ( ) ,
439- "Value indicating the version of the release config -> config" . to_string ( ) ,
440- ) ,
441- "config.version" ,
442- & transaction,
443- & admin,
444- & realm,
445- & state,
446- )
447- . await
448- . map_err ( |e| ABError :: InternalServerError ( format ! ( "{}" , e) ) ) ?;
449-
450414 create_config_with_tx (
451- create_default_config_int (
452- "config.release_config_timeout" . to_string ( ) ,
453- 1000 ,
454- "Value indicating the version of the release config" . to_string ( ) ,
455- ) ,
456- "config.release_config_timeout" ,
457- & transaction,
458- & admin,
459- & realm,
460- & state,
461- )
462- . await
463- . map_err ( |e| ABError :: InternalServerError ( format ! ( "{}" , e) ) ) ?;
464-
465- create_config_with_tx (
466- create_default_config_int (
467- "config.boot_timeout" . to_string ( ) ,
468- 1000 ,
469- "Indicating the timeout for downloading the package block" . to_string ( ) ,
470- ) ,
471- "config.boot_timeout" ,
472- & transaction,
473- & admin,
474- & realm,
475- & state,
476- )
477- . await
478- . map_err ( |e| ABError :: InternalServerError ( format ! ( "{}" , e) ) ) ?;
479-
480- create_config_with_tx (
481- create_default_config_doc (
482- "config.properties" . to_string ( ) ,
483- value_to_document ( & serde_json:: json!( { } ) ) ,
484- "Value indicating the properties of the config" . to_string ( ) ,
485- ) ,
486- "config.properties" ,
487- & transaction,
488- & admin,
489- & realm,
490- & state,
491- )
492- . await
493- . map_err ( |e| ABError :: InternalServerError ( format ! ( "{}" , e) ) ) ?;
494-
495- info ! (
496- "Creating default configuration (string): key=package.name, value={}" ,
497- generated_workspace_name
498- ) ;
499- create_config_with_tx (
500- create_default_config_string (
501- "package.name" . to_string ( ) ,
502- generated_workspace_name. clone ( ) ,
503- "Value indicating the version of the release config" . to_string ( ) ,
504- ) ,
505- "package.name" ,
506- & transaction,
507- & admin,
508- & realm,
509- & state,
510- )
511- . await
512- . map_err ( |e| ABError :: InternalServerError ( format ! ( "{}" , e) ) ) ?;
513-
514- create_config_with_tx (
515- create_default_config_int (
516- "package.version" . to_string ( ) ,
517- 0 ,
518- "Value indicating the version of the package" . to_string ( ) ,
519- ) ,
520- "package.version" ,
521- & transaction,
522- & admin,
523- & realm,
524- & state,
525- )
526- . await
527- . map_err ( |e| ABError :: InternalServerError ( format ! ( "{}" , e) ) ) ?;
528-
529- create_config_with_tx (
530- create_default_config_doc (
531- "package.properties" . to_string ( ) ,
532- value_to_document ( & serde_json:: json!( { } ) ) ,
533- "Value indicating the properties of the package" . to_string ( ) ,
534- ) ,
535- "package.properties" ,
536- & transaction,
537- & admin,
538- & realm,
539- & state,
540- )
541- . await
542- . map_err ( |e| ABError :: InternalServerError ( format ! ( "{}" , e) ) ) ?;
543-
544- create_config_with_tx (
545- create_default_config_string (
546- "package.index" . to_string ( ) ,
547- "" . to_string ( ) ,
548- "Value indicating the index of the package" . to_string ( ) ,
549- ) ,
550- "package.index" ,
551- & transaction,
552- & admin,
553- & realm,
554- & state,
555- )
556- . await
557- . map_err ( |e| ABError :: InternalServerError ( format ! ( "{}" , e) ) ) ?;
558-
559- create_config_with_tx (
560- create_default_config_array (
561- "package.important" . to_string ( ) ,
562- vec ! [ ] ,
563- "Value indicating the important block of the package" . to_string ( ) ,
564- ) ,
565- "package.important" ,
566- & transaction,
567- & admin,
568- & realm,
569- & state,
570- )
571- . await
572- . map_err ( |e| ABError :: InternalServerError ( format ! ( "{}" , e) ) ) ?;
573-
574- create_config_with_tx (
575- create_default_config_array (
576- "package.lazy" . to_string ( ) ,
577- vec ! [ ] ,
578- "Value indicating the lazy block of the package" . to_string ( ) ,
579- ) ,
580- "package.lazy" ,
581- & transaction,
582- & admin,
583- & realm,
584- & state,
585- )
586- . await
587- . map_err ( |e| ABError :: InternalServerError ( format ! ( "{}" , e) ) ) ?;
588-
589- create_config_with_tx (
590- create_default_config_array (
591- "resources" . to_string ( ) ,
592- vec ! [ ] ,
593- "Value indicating the resources block of the release config" . to_string ( ) ,
594- ) ,
595- "resources" ,
415+ async {
416+ migrate_superposition_workspace (
417+ & inserted_workspace,
418+ & state,
419+ & SuperpositionMigrationStrategy :: Patch ,
420+ )
421+ . await
422+ . map_err ( |e| {
423+ ABError :: InternalServerError ( format ! ( "Workspace migration error: {}" , e) )
424+ } )
425+ } ,
426+ "migrate_superposition_workspace" ,
596427 & transaction,
597428 & admin,
598429 & realm,
599430 & state,
600431 )
601- . await
602- . map_err ( |e| ABError :: InternalServerError ( format ! ( "{}" , e) ) ) ?;
432+ . await ?;
603433
604434 // Mark transaction as complete since all operations have succeeded
605435 transaction. set_database_inserted ( ) ;
0 commit comments