From b777a92a4850b52a700e810539bfdcb7943a1b65 Mon Sep 17 00:00:00 2001 From: Magnus Hovland Hoff Date: Thu, 20 Sep 2018 23:17:25 +0200 Subject: [PATCH] Expose theme_from_str_hash to SQL. To be used in a db migration for storing the previously implicit value --- src/db.rs | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/src/db.rs b/src/db.rs index e406de4..12c80a5 100644 --- a/src/db.rs +++ b/src/db.rs @@ -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 for SqliteInitializer { @@ -28,6 +30,11 @@ impl CustomizeConnection 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::(&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::(&conn) + .unwrap(); + + let expected = theme::theme_from_str_hash("Bartefjes"); + + assert_eq!(expected, res[0].theme); + } +}