Convert to SQLx and SQLite #64

Manually merged
projectmoon merged 17 commits from sqlite into master 2021-05-20 15:34:52 +00:00
19 changed files with 36 additions and 40 deletions
Showing only changes of commit e539dcac1f - Show all commits

View File

@ -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"

View File

@ -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 {

View File

@ -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),

View File

@ -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();

View File

@ -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(),

View File

@ -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();

View File

@ -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),

View File

@ -0,0 +1,2 @@
use refinery::include_migration_mods;
include_migration_mods!("src/db/sqlite/migrator/migrations");

View File

@ -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<Database, DataError> {
Self::new("sqlite::memory:").await
}
}
impl Clone for Database {

View File

@ -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,

View File

@ -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;

View File

@ -1,6 +1,5 @@
use std::env;
pub mod migrator;
use tenebrous_dicebot::db::sqlite::migrator;
#[tokio::main]
async fn main() -> Result<(), migrator::MigrationError> {

View File

@ -1,2 +0,0 @@
use refinery::include_migration_mods;
include_migration_mods!("src/migrator/migrations");