Fix the SQlite build

This commit is contained in:
Bat
2018-09-27 22:06:40 +01:00
committed by Igor Galić
parent 535c68b423
commit 743620eb6a
20 changed files with 142 additions and 114 deletions
+29 -16
View File
@@ -26,6 +26,12 @@ extern crate webfinger;
use std::env;
#[cfg(all(feature = "sqlite", not(feature = "postgres")))]
pub type Connection = diesel::SqliteConnection;
#[cfg(all(not(feature = "sqlite"), feature = "postgres"))]
pub type Connection = diesel::PgConnection;
macro_rules! find_by {
($table:ident, $fn:ident, $($col:ident as $type:ident),+) => {
/// Try to find a $table with a given $col
@@ -66,11 +72,14 @@ macro_rules! get {
macro_rules! insert {
($table:ident, $from:ident) => {
last!($table);
pub fn insert(conn: &crate::Connection, new: $from) -> Self {
diesel::insert_into($table::table)
.values(new)
.get_result(conn)
.expect("Error saving new $table")
.execute(conn)
.expect("Error saving new $table");
Self::last(conn)
}
};
}
@@ -80,8 +89,24 @@ macro_rules! update {
pub fn update(&self, conn: &crate::Connection) -> Self {
diesel::update(self)
.set(self)
.get_result(conn)
.expect(concat!("Error updating ", stringify!($table)))
.execute(conn)
.expect(concat!("Error updating ", stringify!($table)));
Self::get(conn, self.id)
.expect(concat!(stringify!($table), " we just updated doesn't exist anymore???"))
}
};
}
macro_rules! last {
($table:ident) => {
pub fn last(conn: &crate::Connection) -> Self {
$table::table.order_by($table::id.desc())
.limit(1)
.load::<Self>(conn)
.expect(concat!("Error getting last ", stringify!($table)))
.iter().next()
.expect(concat!("No last ", stringify!($table)))
.clone()
}
};
}
@@ -105,18 +130,6 @@ pub fn ap_url(url: String) -> String {
format!("{}://{}", scheme, url)
}
#[cfg(all(not(feature = "postgres"), feature = "sqlite"))]
pub type SqlDateTime = chrono::NaiveDateTime;
#[cfg(all(not(feature = "postgres"), feature = "sqlite"))]
pub type Connection = diesel::SqliteConnection;
#[cfg(all(not(feature = "sqlite"), feature = "postgres"))]
pub type SqlDateTime = chrono::NaiveDateTime;
#[cfg(all(not(feature = "sqlite"), feature = "postgres"))]
pub type Connection = diesel::PgConnection;
pub mod admin;
pub mod blog_authors;
pub mod blogs;