@@ -606,6 +606,92 @@ database_id = "auth"
606606 assert_eq ! ( resolved. project_id. 0 , "myproj" ) ;
607607 }
608608
609+ #[ test]
610+ fn cli_db_overrides_profile_db_url_keeps_database_id ( ) {
611+ let toml = r#"
612+ [profiles.billing]
613+ db_url = "postgres://prod/billing"
614+ database_id = "billing"
615+ "# ;
616+ let config = ProjectConfig :: parse ( toml) . unwrap ( ) ;
617+ let resolved = config
618+ . resolve_profile (
619+ Some ( "postgres://localhost/other" ) ,
620+ None ,
621+ Some ( "billing" ) ,
622+ Path :: new ( "/project" ) ,
623+ )
624+ . unwrap ( ) ;
625+ assert_eq ! ( resolved. name, "billing" ) ;
626+ assert_eq ! (
627+ resolved. db_url. as_deref( ) ,
628+ Some ( "postgres://localhost/other" )
629+ ) ;
630+ assert_eq ! (
631+ resolved. database_id. as_ref( ) . map( |d| d. 0 . as_str( ) ) ,
632+ Some ( "billing" )
633+ ) ;
634+ }
635+
636+ #[ test]
637+ fn cli_schema_overrides_profile_schema_file_keeps_database_id ( ) {
638+ let toml = r#"
639+ [profiles.staging]
640+ schema_file = ".dryrun/staging.json"
641+ database_id = "stg"
642+ "# ;
643+ let config = ProjectConfig :: parse ( toml) . unwrap ( ) ;
644+ let override_path = PathBuf :: from ( "/tmp/other-schema.json" ) ;
645+ let resolved = config
646+ . resolve_profile (
647+ None ,
648+ Some ( & override_path) ,
649+ Some ( "staging" ) ,
650+ Path :: new ( "/project" ) ,
651+ )
652+ . unwrap ( ) ;
653+ assert_eq ! ( resolved. name, "staging" ) ;
654+ assert_eq ! (
655+ resolved. schema_file. as_deref( ) ,
656+ Some ( override_path. as_path( ) )
657+ ) ;
658+ assert_eq ! (
659+ resolved. database_id. as_ref( ) . map( |d| d. 0 . as_str( ) ) ,
660+ Some ( "stg" )
661+ ) ;
662+ }
663+
664+ #[ test]
665+ fn explicit_profile_missing_errors ( ) {
666+ let config = ProjectConfig :: parse ( "" ) . unwrap ( ) ;
667+ let result = config. resolve_profile ( None , None , Some ( "nope" ) , Path :: new ( "/tmp" ) ) ;
668+ let err = result. unwrap_err ( ) . to_string ( ) ;
669+ assert ! ( err. contains( "'nope'" ) , "got: {err}" ) ;
670+ }
671+
672+ #[ test]
673+ fn default_profile_missing_with_cli_db_falls_back_to_cli ( ) {
674+ let config = ProjectConfig :: parse ( "[default]\n profile = \" prod\" " ) . unwrap ( ) ;
675+ let resolved = config
676+ . resolve_profile (
677+ Some ( "postgres://localhost/x" ) ,
678+ None ,
679+ None ,
680+ Path :: new ( "/tmp" ) ,
681+ )
682+ . unwrap ( ) ;
683+ assert_eq ! ( resolved. name, "<cli>" ) ;
684+ assert ! ( resolved. database_id. is_none( ) ) ;
685+ }
686+
687+ #[ test]
688+ fn default_profile_missing_without_cli_args_errors ( ) {
689+ let config = ProjectConfig :: parse ( "[default]\n profile = \" missing\" " ) . unwrap ( ) ;
690+ let result = config. resolve_profile ( None , None , None , Path :: new ( "/tmp" ) ) ;
691+ let err = result. unwrap_err ( ) . to_string ( ) ;
692+ assert ! ( err. contains( "'missing'" ) , "got: {err}" ) ;
693+ }
694+
609695 #[ test]
610696 fn project_id_falls_back_to_default_for_root_path ( ) {
611697 let config = ProjectConfig :: parse ( "" ) . unwrap ( ) ;
0 commit comments