From 9829bbfcd57c57e237c6aacb96a78d0b9f5bab68 Mon Sep 17 00:00:00 2001 From: Melody Horn Date: Tue, 29 Jun 2021 22:44:11 -0600 Subject: lol no generics --- src/db/backend.rs | 27 --------------------------- src/db/backend/mod.rs | 7 +++++++ src/db/backend/sqlite.rs | 22 ++++++++++++++++++++++ src/db/migration/change.rs | 8 ++++---- src/db/migration/mod.rs | 8 ++++---- 5 files changed, 37 insertions(+), 35 deletions(-) delete mode 100644 src/db/backend.rs create mode 100644 src/db/backend/mod.rs create mode 100644 src/db/backend/sqlite.rs (limited to 'src/db') diff --git a/src/db/backend.rs b/src/db/backend.rs deleted file mode 100644 index 5950747..0000000 --- a/src/db/backend.rs +++ /dev/null @@ -1,27 +0,0 @@ -use barrel::backend::SqlGenerator; -pub use diesel::connection::Connection; - -pub trait UsableBackend: diesel::backend::Backend where *const str: diesel::deserialize::FromSql {} -impl UsableBackend for diesel::sqlite::Sqlite {} - -pub trait UsableConnection: Connection where ::Backend: UsableBackend {} -impl UsableConnection for diesel::sqlite::SqliteConnection {} - -pub trait Connectable { - type Connection: UsableConnection; - type SqlGenerator: SqlGenerator; - fn connect(&self) -> diesel::ConnectionResult; -} - -pub struct Sqlite { - pub db_file: &'static str, -} - -impl Connectable for Sqlite { - type Connection = diesel::sqlite::SqliteConnection; - type SqlGenerator = barrel::backend::Sqlite; - - fn connect(&self) -> diesel::ConnectionResult { - Self::Connection::establish(self.db_file) - } -} diff --git a/src/db/backend/mod.rs b/src/db/backend/mod.rs new file mode 100644 index 0000000..b266f1e --- /dev/null +++ b/src/db/backend/mod.rs @@ -0,0 +1,7 @@ +#[cfg(feature = "sqlite")] +mod sqlite; +#[cfg(feature = "sqlite")] +pub use sqlite::*; + +#[cfg(not(feature = "sqlite"))] +compile_error!("pick a backend pls"); diff --git a/src/db/backend/sqlite.rs b/src/db/backend/sqlite.rs new file mode 100644 index 0000000..002d0d3 --- /dev/null +++ b/src/db/backend/sqlite.rs @@ -0,0 +1,22 @@ +use diesel::connection::Connection as _; + +pub use barrel::backend::Sqlite as SqlGenerator; +pub use diesel::sqlite::SqliteConnection as Connection; + +pub struct Database { + pub db_file: &'static str, +} + +impl Database { + pub fn connect(&self) -> diesel::ConnectionResult { + Connection::establish(self.db_file) + } +} + +impl Default for Database { + fn default() -> Self { + Self { + db_file: "db.sqlite3", + } + } +} diff --git a/src/db/migration/change.rs b/src/db/migration/change.rs index 8bb8bf6..a07ef24 100644 --- a/src/db/migration/change.rs +++ b/src/db/migration/change.rs @@ -1,6 +1,6 @@ -use diesel::Connection; +use diesel::Connection as _; -use crate::db::backend::Connectable; +use crate::db::backend::{Connection, SqlGenerator}; use crate::db::models::Field; pub enum DatabaseChange { @@ -12,7 +12,7 @@ pub enum DatabaseChange { } impl DatabaseChange { - pub fn apply(&self, app_name: &str, connection: &C::Connection) { + pub fn apply(&self, app_name: &str, connection: &Connection) { use barrel::{types, Migration, Table}; match self { @@ -55,7 +55,7 @@ impl DatabaseChange { m.create_table(table_name, callback); } - connection.execute(&m.make::()).unwrap(); + connection.execute(&m.make::()).unwrap(); } } } diff --git a/src/db/migration/mod.rs b/src/db/migration/mod.rs index 839608f..e6b3266 100644 --- a/src/db/migration/mod.rs +++ b/src/db/migration/mod.rs @@ -1,8 +1,8 @@ pub use tosin_macros::gather_migrations as gather; -use diesel::{result::Error as DieselError, Connection}; +use diesel::{result::Error as DieselError, Connection as _}; -use crate::db::backend::Connectable; +use crate::db::backend::Connection; mod change; @@ -16,12 +16,12 @@ pub struct Migration { } impl Migration { - pub fn apply(&self, app_name: &str, connection: &C::Connection) { + pub fn apply(&self, app_name: &str, connection: &Connection) { // TODO prevent double-applying connection .transaction::<_, DieselError, _>(|| { for change in self.changes { - change.apply::(app_name, connection); + change.apply(app_name, connection); } Ok(()) -- cgit v1.2.3