From f83eaa537be4155f3f180accfc737e8ea8a74fb6 Mon Sep 17 00:00:00 2001 From: Ryan Levick Date: Mon, 1 Jul 2024 17:14:04 +0200 Subject: [PATCH] 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 --- crates/trigger/src/runtime_config/sqlite.rs | 22 +++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/crates/trigger/src/runtime_config/sqlite.rs b/crates/trigger/src/runtime_config/sqlite.rs index 6fc7c482e..8c48a296f 100644 --- a/crates/trigger/src/runtime_config/sqlite.rs +++ b/crates/trigger/src/runtime_config/sqlite.rs @@ -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