Expose theme_from_str_hash to SQL.

To be used in a db migration for storing the previously implicit value
This commit is contained in:
Magnus Hovland Hoff 2018-09-20 23:17:25 +02:00
parent ca1e072d9b
commit b777a92a48

View file

@ -5,6 +5,7 @@ use r2d2::{CustomizeConnection, Pool};
use r2d2_diesel::{self, ConnectionManager};
use rendering;
use theme;
embed_migrations!();
@ -15,6 +16,7 @@ struct SqliteInitializer;
mod sqlfunc {
use diesel::sql_types::Text;
sql_function!(fn markdown_to_fts(text: Text) -> Text);
sql_function!(fn theme_from_str_hash(text: Text) -> Text);
}
impl CustomizeConnection<SqliteConnection, r2d2_diesel::Error> for SqliteInitializer {
@ -28,6 +30,11 @@ impl CustomizeConnection<SqliteConnection, r2d2_diesel::Error> for SqliteInitial
|text: String| rendering::render_markdown_for_fts(&text)
).map_err(|x| r2d2_diesel::Error::QueryError(x))?;
sqlfunc::theme_from_str_hash::register_impl(
conn,
|title: String| theme::theme_from_str_hash(&title)
).map_err(|x| r2d2_diesel::Error::QueryError(x))?;
Ok(())
}
}
@ -53,3 +60,42 @@ pub fn test_connection() -> SqliteConnection {
conn
}
#[cfg(test)]
mod test {
use super::*;
use diesel::sql_query;
#[test]
fn markdown_to_fts() {
let conn = test_connection();
#[derive(QueryableByName, PartialEq, Eq, Debug)]
struct Row { #[sql_type = "Text"] text: String }
let res = sql_query("SELECT markdown_to_fts('[link](url)') as text")
.load::<Row>(&conn)
.unwrap();
let expected = rendering::render_markdown_for_fts("[link](url)");
assert_eq!(expected, res[0].text);
}
#[test]
fn theme_from_str_hash() {
let conn = test_connection();
#[derive(QueryableByName, PartialEq, Eq, Debug)]
struct Row { #[sql_type = "Text"] theme: theme::Theme }
let res = sql_query("SELECT theme_from_str_hash('Bartefjes') as theme")
.load::<Row>(&conn)
.unwrap();
let expected = theme::theme_from_str_hash("Bartefjes");
assert_eq!(expected, res[0].theme);
}
}