aboutsummaryrefslogtreecommitdiff
path: root/src/db
diff options
context:
space:
mode:
Diffstat (limited to 'src/db')
-rw-r--r--src/db/backend.rs27
-rw-r--r--src/db/backend/mod.rs7
-rw-r--r--src/db/backend/sqlite.rs22
-rw-r--r--src/db/migration/change.rs8
-rw-r--r--src/db/migration/mod.rs8
5 files changed, 37 insertions, 35 deletions
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<diesel::sql_types::Text, Self> {}
-impl UsableBackend for diesel::sqlite::Sqlite {}
-
-pub trait UsableConnection: Connection where <Self as Connection>::Backend: UsableBackend {}
-impl UsableConnection for diesel::sqlite::SqliteConnection {}
-
-pub trait Connectable {
- type Connection: UsableConnection;
- type SqlGenerator: SqlGenerator;
- fn connect(&self) -> diesel::ConnectionResult<Self::Connection>;
-}
-
-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> {
- 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> {
+ 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<C: Connectable>(&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::<C::SqlGenerator>()).unwrap();
+ connection.execute(&m.make::<SqlGenerator>()).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<C: Connectable>(&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::<C>(app_name, connection);
+ change.apply(app_name, connection);
}
Ok(())