Skip to content

Commit

Permalink
Allow sqlite migrations for non-default databases.
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
rylev committed Jul 22, 2024
1 parent 44fb8c1 commit f83eaa5
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions crates/trigger/src/runtime_config/sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,24 +49,30 @@ async fn execute_statements(
if statements.is_empty() {
return Ok(());
}
let Some(default) = databases.get("default") else {
debug_assert!(
false,
"the 'default' sqlite database should always be available but for some reason was not"
);
return Ok(());
};

for m in statements {
if let Some(file) = m.strip_prefix('@') {
let database = file.strip_suffix(".sql").unwrap_or(file);
let database = databases.get(database).with_context(|| {
format!(
"based on the sql file '{file}' a registered database named '{database}' was expected but not found. The registered databases are '{:?}'", databases.keys()
)
})?;
let sql = std::fs::read_to_string(file).with_context(|| {
format!("could not read file '{file}' containing sql statements")
})?;
default
database
.execute_batch(&sql)
.await
.with_context(|| format!("failed to execute sql from file '{file}'"))?;
} else {
let Some(default) = databases.get("default") else {
debug_assert!(
false,
"the 'default' sqlite database should always be available but for some reason was not"
);
return Ok(());
};
default
.query(m, Vec::new())
.await
Expand Down

0 comments on commit f83eaa5

Please sign in to comment.