2021-05-15 15:27:40 +00:00
|
|
|
use log::info;
|
|
|
|
use refinery::config::{Config, ConfigDbType};
|
|
|
|
use sqlx::sqlite::SqliteConnectOptions;
|
|
|
|
use sqlx::ConnectOptions;
|
|
|
|
use std::str::FromStr;
|
|
|
|
use thiserror::Error;
|
|
|
|
|
2022-02-02 20:56:50 +00:00
|
|
|
//pub mod migrations;
|
2021-05-15 15:27:40 +00:00
|
|
|
|
|
|
|
#[derive(Error, Debug)]
|
|
|
|
pub enum MigrationError {
|
|
|
|
#[error("sqlite connection error: {0}")]
|
|
|
|
SqlxError(#[from] sqlx::Error),
|
|
|
|
|
|
|
|
#[error("refinery migration error: {0}")]
|
|
|
|
RefineryError(#[from] refinery::Error),
|
|
|
|
}
|
|
|
|
|
2022-02-02 20:56:50 +00:00
|
|
|
mod embedded {
|
|
|
|
use refinery::embed_migrations;
|
|
|
|
embed_migrations!("src/db/sqlite/migrator/migrations");
|
|
|
|
}
|
|
|
|
|
2021-05-15 15:27:40 +00:00
|
|
|
/// Run database migrations against the sqlite database.
|
|
|
|
pub async fn migrate(db_path: &str) -> Result<(), MigrationError> {
|
|
|
|
//Create database if missing.
|
|
|
|
let conn = SqliteConnectOptions::from_str(&format!("sqlite://{}", db_path))?
|
|
|
|
.create_if_missing(true)
|
|
|
|
.connect()
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
drop(conn);
|
|
|
|
|
|
|
|
let mut conn = Config::new(ConfigDbType::Sqlite).set_db_path(db_path);
|
|
|
|
info!("Running migrations");
|
2022-02-02 20:56:50 +00:00
|
|
|
embedded::migrations::runner().run(&mut conn)?;
|
2021-05-15 15:27:40 +00:00
|
|
|
Ok(())
|
|
|
|
}
|