@@ -52,12 +52,7 @@ async fn execute_statements(
5252
5353 for m in statements {
5454 if let Some ( config) = m. strip_prefix ( '@' ) {
55- let config = config. trim ( ) ;
56- let ( file, database) = match config. split_once ( ':' ) {
57- Some ( ( file, database) ) if database. trim ( ) . is_empty ( ) => ( file. trim ( ) , "default" ) ,
58- Some ( ( file, database) ) => ( file. trim ( ) , database. trim ( ) ) ,
59- None => ( config, "default" ) ,
60- } ;
55+ let ( file, database) = parse_file_and_label ( config) ?;
6156 let database = databases. get ( database) . with_context ( || {
6257 format ! (
6358 "based on the '@{config}' a registered database named '{database}' was expected but not found. The registered databases are '{:?}'" , databases. keys( )
@@ -72,10 +67,7 @@ async fn execute_statements(
7267 . with_context ( || format ! ( "failed to execute sql from file '{file}'" ) ) ?;
7368 } else {
7469 let Some ( default) = databases. get ( "default" ) else {
75- debug_assert ! (
76- false ,
77- "the 'default' sqlite database should always be available but for some reason was not"
78- ) ;
70+ debug_assert ! ( false , "the 'default' sqlite database should always be available but for some reason was not" ) ;
7971 return Ok ( ( ) ) ;
8072 } ;
8173 default
@@ -87,6 +79,19 @@ async fn execute_statements(
8779 Ok ( ( ) )
8880}
8981
82+ /// Parses a @{file:label} sqlite statement
83+ fn parse_file_and_label ( config : & str ) -> anyhow:: Result < ( & str , & str ) > {
84+ let config = config. trim ( ) ;
85+ let ( file, label) = match config. split_once ( ':' ) {
86+ Some ( ( _, label) ) if label. trim ( ) . is_empty ( ) => {
87+ anyhow:: bail!( "database label is empty in the '@{config}' sqlite statement" )
88+ }
89+ Some ( ( file, label) ) => ( file. trim ( ) , label. trim ( ) ) ,
90+ None => ( config, "default" ) ,
91+ } ;
92+ Ok ( ( file, label) )
93+ }
94+
9095// Holds deserialized options from a `[sqlite_database.<name>]` runtime config section.
9196#[ derive( Clone , Debug , serde:: Deserialize ) ]
9297#[ serde( rename_all = "snake_case" , tag = "type" ) ]
@@ -213,3 +218,23 @@ impl TriggerHooks for SqlitePersistenceMessageHook {
213218 Ok ( ( ) )
214219 }
215220}
221+
222+ #[ cfg( test) ]
223+ mod tests {
224+ use super :: * ;
225+
226+ #[ test]
227+ fn can_parse_file_and_label ( ) {
228+ let config = "file:label" ;
229+ let result = parse_file_and_label ( config) . unwrap ( ) ;
230+ assert_eq ! ( result, ( "file" , "label" ) ) ;
231+
232+ let config = "file:" ;
233+ let result = parse_file_and_label ( config) ;
234+ assert ! ( result. is_err( ) ) ;
235+
236+ let config = "file" ;
237+ let result = parse_file_and_label ( config) . unwrap ( ) ;
238+ assert_eq ! ( result, ( "file" , "default" ) ) ;
239+ }
240+ }
0 commit comments