Skip to content

Commit f83eaa5

Browse files
committed
Allow sqlite migrations for non-default databases.
The database will be selected by the name of the migration file. This makes the name of the migration file significant, whereas previously it was not. If the name of the file and a known database don't line up, `spin up` will fail with an error message. Signed-off-by: Ryan Levick <[email protected]>
1 parent 44fb8c1 commit f83eaa5

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

crates/trigger/src/runtime_config/sqlite.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,24 +49,30 @@ async fn execute_statements(
4949
if statements.is_empty() {
5050
return Ok(());
5151
}
52-
let Some(default) = databases.get("default") else {
53-
debug_assert!(
54-
false,
55-
"the 'default' sqlite database should always be available but for some reason was not"
56-
);
57-
return Ok(());
58-
};
5952

6053
for m in statements {
6154
if let Some(file) = m.strip_prefix('@') {
55+
let database = file.strip_suffix(".sql").unwrap_or(file);
56+
let database = databases.get(database).with_context(|| {
57+
format!(
58+
"based on the sql file '{file}' a registered database named '{database}' was expected but not found. The registered databases are '{:?}'", databases.keys()
59+
)
60+
})?;
6261
let sql = std::fs::read_to_string(file).with_context(|| {
6362
format!("could not read file '{file}' containing sql statements")
6463
})?;
65-
default
64+
database
6665
.execute_batch(&sql)
6766
.await
6867
.with_context(|| format!("failed to execute sql from file '{file}'"))?;
6968
} else {
69+
let Some(default) = databases.get("default") else {
70+
debug_assert!(
71+
false,
72+
"the 'default' sqlite database should always be available but for some reason was not"
73+
);
74+
return Ok(());
75+
};
7076
default
7177
.query(m, Vec::new())
7278
.await

0 commit comments

Comments
 (0)