From e539dcac1f0d886612da7237a1f26b268ff3e6d7 Mon Sep 17 00:00:00 2001 From: projectmoon Date: Tue, 18 May 2021 14:50:49 +0000 Subject: [PATCH] Move migrations to sqlite directory. Remove in-memory temp db until refinery supports sqlx. --- Cargo.toml | 4 +--- src/bin/dicebot-cmd.rs | 10 +++++++++- src/bin/dicebot.rs | 3 --- src/cofd/dice.rs | 10 +++++++--- src/commands.rs | 7 ++++++- src/cthulhu/dice.rs | 6 +++--- src/db/sqlite/errors.rs | 16 +--------------- .../sqlite}/migrator/migrations/V1__variables.rs | 0 .../sqlite}/migrator/migrations/V2__room_info.rs | 0 .../sqlite}/migrator/migrations/V3__dbstate.rs | 0 .../migrator/migrations/V4__room_events.rs | 0 .../migrator/migrations/V5__room_users.rs | 0 src/db/sqlite/migrator/migrations/mod.rs | 2 ++ src/{migrator.rs => db/sqlite/migrator/mod.rs} | 0 src/db/sqlite/mod.rs | 8 ++++---- src/error.rs | 4 ++-- src/lib.rs | 1 - src/migrate_cli.rs | 3 +-- src/migrator/migrations/mod.rs | 2 -- 19 files changed, 36 insertions(+), 40 deletions(-) rename src/{ => db/sqlite}/migrator/migrations/V1__variables.rs (100%) rename src/{ => db/sqlite}/migrator/migrations/V2__room_info.rs (100%) rename src/{ => db/sqlite}/migrator/migrations/V3__dbstate.rs (100%) rename src/{ => db/sqlite}/migrator/migrations/V4__room_events.rs (100%) rename src/{ => db/sqlite}/migrator/migrations/V5__room_users.rs (100%) create mode 100644 src/db/sqlite/migrator/migrations/mod.rs rename src/{migrator.rs => db/sqlite/migrator/mod.rs} (100%) delete mode 100644 src/migrator/migrations/mod.rs diff --git a/Cargo.toml b/Cargo.toml index 88de99b..b166367 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -38,6 +38,7 @@ phf = { version = "0.8", features = ["macros"] } matrix-sdk = { git = "https://github.com/matrix-org/matrix-rust-sdk", branch = "master" } refinery = { version = "0.5", features = ["rusqlite"]} barrel = { version = "0.6", features = ["sqlite3"] } +tempfile = "3" [dependencies.sqlx] version = "0.5" @@ -50,6 +51,3 @@ features = ['derive'] [dependencies.tokio] version = "1" features = [ "full" ] - -[dev-dependencies] -tempfile = "3" \ No newline at end of file diff --git a/src/bin/dicebot-cmd.rs b/src/bin/dicebot-cmd.rs index b80fc12..dc38d71 100644 --- a/src/bin/dicebot-cmd.rs +++ b/src/bin/dicebot-cmd.rs @@ -15,9 +15,17 @@ async fn main() -> Result<(), BotError> { }; let homeserver = Url::parse("http://example.com")?; + let db_path = tempfile::NamedTempFile::new_in(".").unwrap(); + let db = Database::new( + db_path + .path() + .to_str() + .expect("Could not get path to temporary db"), + ) + .await?; let context = Context { - db: Database::new_temp().await?, + db: db, matrix_client: &matrix_sdk::Client::new(homeserver) .expect("Could not create matrix client"), room: RoomContext { diff --git a/src/bin/dicebot.rs b/src/bin/dicebot.rs index 8be23db..a5abbb1 100644 --- a/src/bin/dicebot.rs +++ b/src/bin/dicebot.rs @@ -7,7 +7,6 @@ use tenebrous_dicebot::bot::DiceBot; use tenebrous_dicebot::config::*; use tenebrous_dicebot::db::sqlite::Database; use tenebrous_dicebot::error::BotError; -use tenebrous_dicebot::migrator; use tenebrous_dicebot::state::DiceBotState; use tracing_subscriber::filter::EnvFilter; @@ -38,8 +37,6 @@ async fn run() -> Result<(), BotError> { let db = Database::new(&sqlite_path).await?; let state = Arc::new(RwLock::new(DiceBotState::new(&cfg))); - migrator::migrate(&sqlite_path).await?; - match DiceBot::new(&cfg, &state, &db) { Ok(bot) => bot.run().await?, Err(e) => println!("Error connecting: {:?}", e), diff --git a/src/cofd/dice.rs b/src/cofd/dice.rs index f269b1c..aae34b8 100644 --- a/src/cofd/dice.rs +++ b/src/cofd/dice.rs @@ -475,8 +475,12 @@ mod tests { #[tokio::test(flavor = "multi_thread", worker_threads = 1)] async fn rejects_large_expression_test() { + let db_path = tempfile::NamedTempFile::new_in(".").unwrap(); let homeserver = Url::parse("http://example.com").unwrap(); - let db = Database::new_temp().await.unwrap(); + let db = Database::new(db_path.path().to_str().unwrap()) + .await + .unwrap(); + let ctx = Context { db: db, matrix_client: &matrix_sdk::Client::new(homeserver).unwrap(), @@ -508,7 +512,7 @@ mod tests { #[tokio::test(flavor = "multi_thread", worker_threads = 1)] async fn converts_to_chance_die_test() { let db_path = tempfile::NamedTempFile::new_in(".").unwrap(); - crate::migrator::migrate(db_path.path().to_str().unwrap()) + crate::db::sqlite::migrator::migrate(db_path.path().to_str().unwrap()) .await .unwrap(); @@ -545,7 +549,7 @@ mod tests { #[tokio::test(flavor = "multi_thread", worker_threads = 1)] async fn can_resolve_variables_test() { let db_path = tempfile::NamedTempFile::new_in(".").unwrap(); - crate::migrator::migrate(db_path.path().to_str().unwrap()) + crate::db::sqlite::migrator::migrate(db_path.path().to_str().unwrap()) .await .unwrap(); diff --git a/src/commands.rs b/src/commands.rs index f0aa160..4dd24cd 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -138,8 +138,13 @@ mod tests { #[tokio::test(flavor = "multi_thread", worker_threads = 1)] async fn unrecognized_command() { - let db = crate::db::sqlite::Database::new_temp().await.unwrap(); + let db_path = tempfile::NamedTempFile::new_in(".").unwrap(); + let db = crate::db::sqlite::Database::new(db_path.path().to_str().unwrap()) + .await + .unwrap(); + let homeserver = Url::parse("http://example.com").unwrap(); + let ctx = Context { db: db, matrix_client: &matrix_sdk::Client::new(homeserver).unwrap(), diff --git a/src/cthulhu/dice.rs b/src/cthulhu/dice.rs index 95a0469..fffaeaa 100644 --- a/src/cthulhu/dice.rs +++ b/src/cthulhu/dice.rs @@ -493,7 +493,7 @@ mod tests { }; let db_path = tempfile::NamedTempFile::new_in(".").unwrap(); - crate::migrator::migrate(db_path.path().to_str().unwrap()) + crate::db::sqlite::migrator::migrate(db_path.path().to_str().unwrap()) .await .unwrap(); @@ -529,7 +529,7 @@ mod tests { }; let db_path = tempfile::NamedTempFile::new_in(".").unwrap(); - crate::migrator::migrate(db_path.path().to_str().unwrap()) + crate::db::sqlite::migrator::migrate(db_path.path().to_str().unwrap()) .await .unwrap(); @@ -565,7 +565,7 @@ mod tests { }; let db_path = tempfile::NamedTempFile::new_in(".").unwrap(); - crate::migrator::migrate(db_path.path().to_str().unwrap()) + crate::db::sqlite::migrator::migrate(db_path.path().to_str().unwrap()) .await .unwrap(); diff --git a/src/db/sqlite/errors.rs b/src/db/sqlite/errors.rs index dd06b77..2132120 100644 --- a/src/db/sqlite/errors.rs +++ b/src/db/sqlite/errors.rs @@ -3,20 +3,6 @@ use std::num::TryFromIntError; use sled::transaction::{TransactionError, UnabortableTransactionError}; use thiserror::Error; -#[derive(Error, Debug)] -pub enum MigrationError { - #[error("cannot downgrade to an older database version")] - CannotDowngrade, - - #[error("migration for version {0} not defined")] - MigrationNotFound(u32), - - #[error("migration failed: {0}")] - MigrationFailed(String), -} - -//TODO better combining of key and value in certain errors (namely -//I32SchemaViolation). #[derive(Error, Debug)] pub enum DataError { #[error("value does not exist for key: {0}")] @@ -47,7 +33,7 @@ pub enum DataError { UnabortableTransactionError(#[from] UnabortableTransactionError), #[error("data migration error: {0}")] - MigrationError(#[from] MigrationError), + MigrationError(#[from] super::migrator::MigrationError), #[error("deserialization error: {0}")] DeserializationError(#[from] bincode::Error), diff --git a/src/migrator/migrations/V1__variables.rs b/src/db/sqlite/migrator/migrations/V1__variables.rs similarity index 100% rename from src/migrator/migrations/V1__variables.rs rename to src/db/sqlite/migrator/migrations/V1__variables.rs diff --git a/src/migrator/migrations/V2__room_info.rs b/src/db/sqlite/migrator/migrations/V2__room_info.rs similarity index 100% rename from src/migrator/migrations/V2__room_info.rs rename to src/db/sqlite/migrator/migrations/V2__room_info.rs diff --git a/src/migrator/migrations/V3__dbstate.rs b/src/db/sqlite/migrator/migrations/V3__dbstate.rs similarity index 100% rename from src/migrator/migrations/V3__dbstate.rs rename to src/db/sqlite/migrator/migrations/V3__dbstate.rs diff --git a/src/migrator/migrations/V4__room_events.rs b/src/db/sqlite/migrator/migrations/V4__room_events.rs similarity index 100% rename from src/migrator/migrations/V4__room_events.rs rename to src/db/sqlite/migrator/migrations/V4__room_events.rs diff --git a/src/migrator/migrations/V5__room_users.rs b/src/db/sqlite/migrator/migrations/V5__room_users.rs similarity index 100% rename from src/migrator/migrations/V5__room_users.rs rename to src/db/sqlite/migrator/migrations/V5__room_users.rs diff --git a/src/db/sqlite/migrator/migrations/mod.rs b/src/db/sqlite/migrator/migrations/mod.rs new file mode 100644 index 0000000..a7ece4d --- /dev/null +++ b/src/db/sqlite/migrator/migrations/mod.rs @@ -0,0 +1,2 @@ +use refinery::include_migration_mods; +include_migration_mods!("src/db/sqlite/migrator/migrations"); diff --git a/src/migrator.rs b/src/db/sqlite/migrator/mod.rs similarity index 100% rename from src/migrator.rs rename to src/db/sqlite/migrator/mod.rs diff --git a/src/db/sqlite/mod.rs b/src/db/sqlite/mod.rs index 426e2eb..480f502 100644 --- a/src/db/sqlite/mod.rs +++ b/src/db/sqlite/mod.rs @@ -9,6 +9,7 @@ use std::str::FromStr; use crate::models::RoomInfo; pub mod errors; +pub mod migrator; pub mod rooms; pub mod state; pub mod variables; @@ -93,6 +94,9 @@ impl Database { drop(conn); + //Migrate database. + migrator::migrate(&path).await?; + //Return actual conncetion pool. let conn = SqlitePoolOptions::new() .max_connections(5) @@ -101,10 +105,6 @@ impl Database { Self::new_db(conn) } - - pub async fn new_temp() -> Result { - Self::new("sqlite::memory:").await - } } impl Clone for Database { diff --git a/src/error.rs b/src/error.rs index f091d98..3cc9376 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,6 +1,6 @@ use crate::config::ConfigError; use crate::db::errors::DataError; -use crate::{commands::CommandError, migrator::migrations}; +use crate::{commands::CommandError, db::sqlite::migrator}; use thiserror::Error; #[derive(Error, Debug)] @@ -77,7 +77,7 @@ pub enum BotError { DatabaseError(#[from] sled::Error), #[error("database migration error: {0}")] - SqliteError(#[from] crate::migrator::MigrationError), + SqliteError(#[from] migrator::MigrationError), #[error("too many commands or message was too large")] MessageTooLarge, diff --git a/src/lib.rs b/src/lib.rs index 5c27f63..d67ade8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -11,7 +11,6 @@ pub mod error; mod help; pub mod logic; pub mod matrix; -pub mod migrator; pub mod models; mod parser; pub mod state; diff --git a/src/migrate_cli.rs b/src/migrate_cli.rs index 11cbbbc..9478b13 100644 --- a/src/migrate_cli.rs +++ b/src/migrate_cli.rs @@ -1,6 +1,5 @@ use std::env; - -pub mod migrator; +use tenebrous_dicebot::db::sqlite::migrator; #[tokio::main] async fn main() -> Result<(), migrator::MigrationError> { diff --git a/src/migrator/migrations/mod.rs b/src/migrator/migrations/mod.rs deleted file mode 100644 index b237433..0000000 --- a/src/migrator/migrations/mod.rs +++ /dev/null @@ -1,2 +0,0 @@ -use refinery::include_migration_mods; -include_migration_mods!("src/migrator/migrations");