@@ -52,12 +52,7 @@ async fn execute_statements(
52
52
53
53
for m in statements {
54
54
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) ?;
61
56
let database = databases. get ( database) . with_context ( || {
62
57
format ! (
63
58
"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(
72
67
. with_context ( || format ! ( "failed to execute sql from file '{file}'" ) ) ?;
73
68
} else {
74
69
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" ) ;
79
71
return Ok ( ( ) ) ;
80
72
} ;
81
73
default
@@ -87,6 +79,19 @@ async fn execute_statements(
87
79
Ok ( ( ) )
88
80
}
89
81
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
+
90
95
// Holds deserialized options from a `[sqlite_database.<name>]` runtime config section.
91
96
#[ derive( Clone , Debug , serde:: Deserialize ) ]
92
97
#[ serde( rename_all = "snake_case" , tag = "type" ) ]
@@ -213,3 +218,23 @@ impl TriggerHooks for SqlitePersistenceMessageHook {
213
218
Ok ( ( ) )
214
219
}
215
220
}
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