diff --git a/build.rs b/build.rs index c460933..3dadbc5 100644 --- a/build.rs +++ b/build.rs @@ -1,6 +1,6 @@ #[macro_use] extern crate quote; +#[macro_use] extern crate diesel; extern crate diesel_migrations; -extern crate diesel; extern crate walkdir; use diesel::Connection; @@ -11,9 +11,10 @@ use std::io::prelude::*; use std::path::Path; use walkdir::WalkDir; -use std::ffi::CString; -fn markdown_to_fts(_: &::diesel::sqlite::Context) -> CString { - panic!("Should never be called when running migrations on build.db") +#[allow(dead_code)] +mod sqlfunc { + use diesel::sql_types::Text; + sql_function!(fn markdown_to_fts(text: Text) -> Text); } fn main() { @@ -23,7 +24,7 @@ fn main() { let _ignore_failure = std::fs::remove_file(db_path); - let mut connection = SqliteConnection::establish(db_path) + let connection = SqliteConnection::establish(db_path) .expect(&format!("Error esablishing a database connection to {}", db_path)); // Integer is a dummy placeholder. Compiling fails when passing (). @@ -31,12 +32,7 @@ fn main() { .execute(&connection) .expect("Should be able to enable foreign keys"); - connection.create_scalar_function( - "markdown_to_fts", - 1, - true, - markdown_to_fts, - ).unwrap(); + sqlfunc::markdown_to_fts::register_impl(&connection, |_: String| -> String { unreachable!() }).unwrap(); diesel_migrations::run_pending_migrations(&connection).unwrap(); diff --git a/src/db.rs b/src/db.rs index a1654f3..b8c73e1 100644 --- a/src/db.rs +++ b/src/db.rs @@ -9,11 +9,10 @@ embed_migrations!(); #[derive(Debug)] struct SqliteInitializer; -use std::ffi::CString; - -fn markdown_to_fts(ctx: &::diesel::sqlite::Context) -> CString { - use rendering; - CString::new(rendering::render_markdown_for_fts(&ctx.get::(0))).unwrap() +#[allow(dead_code)] +mod sqlfunc { + use diesel::sql_types::Text; + sql_function!(fn markdown_to_fts(text: Text) -> Text); } impl CustomizeConnection for SqliteInitializer { @@ -22,11 +21,12 @@ impl CustomizeConnection for SqliteInitial .execute(conn) .map_err(|x| r2d2_diesel::Error::QueryError(x))?; - conn.create_scalar_function( - "markdown_to_fts", - 1, - true, - markdown_to_fts, + sqlfunc::markdown_to_fts::register_impl( + conn, + |text: String| { + use rendering; + rendering::render_markdown_for_fts(&text) + } ).map_err(|x| r2d2_diesel::Error::QueryError(x))?; Ok(())