@@ -134,11 +134,14 @@ impl ProjectConfig {
134134 }
135135
136136 // resolution order:
137- // 1. explicit cli_db or cli_schema (CLI flags)
138- // 2. cli_profile flag (--profile)
139- // 3. PROFILE env var
140- // 4. [default].profile in toml
141- // 5. auto-discovery of .dryrun/schema.json
137+ // 1. cli_profile flag (--profile)
138+ // 2. PROFILE env var
139+ // 3. [default].profile in toml
140+ // 4. auto-discovery of .dryrun/schema.json
141+ //
142+ // CLI flags (cli_db, cli_schema) override the resolved profile's matching
143+ // fields for the current invocation. So `--profile billing --db $OTHER`
144+ // connects to $OTHER but keeps billing's database_id for snapshot keying.
142145 pub fn resolve_profile (
143146 & self ,
144147 cli_db : Option < & str > ,
@@ -148,6 +151,33 @@ impl ProjectConfig {
148151 ) -> Result < ResolvedProfile > {
149152 let project_id = self . project_id ( project_root) ;
150153
154+ let explicit_profile = cli_profile
155+ . map ( |s| s. to_string ( ) )
156+ . or_else ( || std:: env:: var ( "PROFILE" ) . ok ( ) ) ;
157+ let default_profile = self . default . as_ref ( ) . and_then ( |d| d. profile . clone ( ) ) ;
158+ let profile_name = explicit_profile. clone ( ) . or ( default_profile) ;
159+
160+ if let Some ( name) = profile_name {
161+ if let Some ( profile) = self . profiles . get ( & name) {
162+ let mut resolved = resolve_profile_config ( & name, profile, project_root, project_id) ;
163+ if let Some ( db) = cli_db {
164+ resolved. db_url = Some ( expand_env_vars ( db) ) ;
165+ }
166+ if let Some ( schema) = cli_schema {
167+ resolved. schema_file = Some ( schema. to_path_buf ( ) ) ;
168+ }
169+ return Ok ( resolved) ;
170+ }
171+
172+ // Missing profile causes error.
173+ if explicit_profile. is_some ( ) || ( cli_db. is_none ( ) && cli_schema. is_none ( ) ) {
174+ return Err ( Error :: Config ( format ! (
175+ "profile '{name}' not found in dryrun.toml"
176+ ) ) ) ;
177+ }
178+ }
179+
180+ // No profile resolved: fall back to <cli> or <auto>.
151181 if let Some ( db) = cli_db {
152182 return Ok ( ResolvedProfile {
153183 name : "<cli>" . into ( ) ,
@@ -167,23 +197,6 @@ impl ProjectConfig {
167197 } ) ;
168198 }
169199
170- let profile_name = cli_profile
171- . map ( |s| s. to_string ( ) )
172- . or_else ( || std:: env:: var ( "PROFILE" ) . ok ( ) )
173- . or_else ( || self . default . as_ref ( ) . and_then ( |d| d. profile . clone ( ) ) ) ;
174-
175- if let Some ( name) = profile_name {
176- let profile = self . profiles . get ( & name) . ok_or_else ( || {
177- Error :: Config ( format ! ( "profile '{name}' not found in dryrun.toml" ) )
178- } ) ?;
179- return Ok ( resolve_profile_config (
180- & name,
181- profile,
182- project_root,
183- project_id,
184- ) ) ;
185- }
186-
187200 let auto_schema = project_root. join ( ".dryrun/schema.json" ) ;
188201 if auto_schema. is_file ( ) {
189202 return Ok ( ResolvedProfile {
0 commit comments