2021-07-10 16:27:20 +08:00
|
|
|
pub mod schema;
|
2021-07-09 23:31:44 +08:00
|
|
|
|
|
|
|
#[macro_use]
|
|
|
|
extern crate diesel;
|
2021-07-10 16:27:20 +08:00
|
|
|
pub use diesel::*;
|
|
|
|
|
2021-07-09 23:31:44 +08:00
|
|
|
#[macro_use]
|
|
|
|
extern crate diesel_derives;
|
2021-07-10 16:27:20 +08:00
|
|
|
pub use diesel_derives::*;
|
|
|
|
|
2021-07-09 23:31:44 +08:00
|
|
|
#[macro_use]
|
|
|
|
extern crate diesel_migrations;
|
|
|
|
|
2021-07-10 16:27:20 +08:00
|
|
|
pub use flowy_sqlite::{DBConnection, Database};
|
2021-07-09 23:31:44 +08:00
|
|
|
|
|
|
|
use diesel_migrations::*;
|
2021-07-10 16:27:20 +08:00
|
|
|
use flowy_sqlite::{Error, PoolConfig};
|
|
|
|
use std::{io, path::Path};
|
2021-07-09 23:31:44 +08:00
|
|
|
|
|
|
|
embed_migrations!("../flowy-database/migrations/");
|
|
|
|
pub const DB_NAME: &str = "flowy-database.db";
|
|
|
|
|
2021-07-10 16:27:20 +08:00
|
|
|
pub fn init(storage_path: &str) -> Result<Database, io::Error> {
|
2021-07-09 23:31:44 +08:00
|
|
|
if !Path::new(storage_path).exists() {
|
|
|
|
std::fs::create_dir_all(storage_path)?;
|
|
|
|
}
|
|
|
|
let pool_config = PoolConfig::default();
|
2021-07-10 16:27:20 +08:00
|
|
|
let database = Database::new(storage_path, DB_NAME, pool_config).map_err(as_io_error)?;
|
|
|
|
let conn = database.get_connection().map_err(as_io_error)?;
|
|
|
|
embedded_migrations::run(&*conn);
|
2021-07-09 23:31:44 +08:00
|
|
|
Ok(database)
|
|
|
|
}
|
2021-07-10 16:27:20 +08:00
|
|
|
|
|
|
|
fn as_io_error(e: Error) -> io::Error {
|
|
|
|
let msg = format!("{:?}", e);
|
|
|
|
io::Error::new(io::ErrorKind::NotConnected, msg)
|
|
|
|
}
|