2017-08-19 23:48:51 +03:00
|
|
|
#[macro_use] extern crate quote;
|
2018-06-13 23:46:53 +03:00
|
|
|
#[macro_use] extern crate diesel;
|
2017-12-15 18:19:44 +03:00
|
|
|
extern crate diesel_migrations;
|
2017-08-19 23:48:51 +03:00
|
|
|
extern crate walkdir;
|
|
|
|
|
|
|
|
use diesel::Connection;
|
|
|
|
use diesel::prelude::*;
|
|
|
|
use std::env;
|
|
|
|
use std::fs::File;
|
|
|
|
use std::io::prelude::*;
|
|
|
|
use std::path::Path;
|
|
|
|
use walkdir::WalkDir;
|
|
|
|
|
2018-06-13 23:46:53 +03:00
|
|
|
#[allow(dead_code)]
|
|
|
|
mod sqlfunc {
|
|
|
|
use diesel::sql_types::Text;
|
|
|
|
sql_function!(fn markdown_to_fts(text: Text) -> Text);
|
2018-01-19 18:49:15 +03:00
|
|
|
}
|
|
|
|
|
2017-08-19 23:48:51 +03:00
|
|
|
fn main() {
|
|
|
|
let out_dir = env::var("OUT_DIR").expect("cargo must set OUT_DIR");
|
|
|
|
let db_path = Path::new(&out_dir).join("build.db");
|
|
|
|
let db_path = db_path.to_str().expect("Will only work for Unicode-representable paths");
|
|
|
|
|
|
|
|
let _ignore_failure = std::fs::remove_file(db_path);
|
|
|
|
|
2018-06-13 23:46:53 +03:00
|
|
|
let connection = SqliteConnection::establish(db_path)
|
2017-08-19 23:48:51 +03:00
|
|
|
.expect(&format!("Error esablishing a database connection to {}", db_path));
|
|
|
|
|
|
|
|
// Integer is a dummy placeholder. Compiling fails when passing ().
|
2018-01-19 18:49:15 +03:00
|
|
|
diesel::expression::sql_literal::sql::<(diesel::sql_types::Integer)>("PRAGMA foreign_keys = ON")
|
2017-08-19 23:48:51 +03:00
|
|
|
.execute(&connection)
|
|
|
|
.expect("Should be able to enable foreign keys");
|
|
|
|
|
2018-06-13 23:46:53 +03:00
|
|
|
sqlfunc::markdown_to_fts::register_impl(&connection, |_: String| -> String { unreachable!() }).unwrap();
|
2018-01-19 18:49:15 +03:00
|
|
|
|
2017-12-15 18:19:44 +03:00
|
|
|
diesel_migrations::run_pending_migrations(&connection).unwrap();
|
2017-08-19 23:48:51 +03:00
|
|
|
|
|
|
|
let infer_schema_path = Path::new(&out_dir).join("infer_schema.rs");
|
|
|
|
let mut file = File::create(infer_schema_path).expect("Unable to open file for writing");
|
2017-10-24 17:48:16 +03:00
|
|
|
|
2017-08-19 23:48:51 +03:00
|
|
|
file.write_all(quote! {
|
2017-10-24 17:48:16 +03:00
|
|
|
mod __diesel_infer_schema_articles {
|
|
|
|
infer_table_from_schema!(#db_path, "articles");
|
|
|
|
}
|
|
|
|
pub use self::__diesel_infer_schema_articles::*;
|
|
|
|
|
|
|
|
mod __diesel_infer_schema_article_revisions {
|
|
|
|
infer_table_from_schema!(#db_path, "article_revisions");
|
|
|
|
}
|
|
|
|
pub use self::__diesel_infer_schema_article_revisions::*;
|
2017-08-19 23:48:51 +03:00
|
|
|
}.as_str().as_bytes()).expect("Unable to write to file");
|
|
|
|
|
|
|
|
for entry in WalkDir::new("migrations").into_iter().filter_map(|e| e.ok()) {
|
|
|
|
println!("cargo:rerun-if-changed={}", entry.path().display());
|
|
|
|
}
|
2017-11-20 12:26:35 +03:00
|
|
|
|
|
|
|
// For build_config.rs
|
|
|
|
for env_var in &[
|
|
|
|
"CONTINUOUS_INTEGRATION",
|
|
|
|
"TRAVIS_BRANCH",
|
|
|
|
"TRAVIS_COMMIT",
|
|
|
|
] {
|
|
|
|
println!("cargo:rerun-if-env-changed={}", env_var);
|
|
|
|
}
|
2017-08-19 23:48:51 +03:00
|
|
|
}
|