hyper_scripter/migration/
mod.rs

1use sqlx::migrate::MigrateError;
2use sqlx::{sqlite::SqliteConnectOptions, SqlitePool};
3use std::path::Path;
4
5pub async fn do_migrate_with_pre_sql(
6    file: impl AsRef<Path>,
7    pre_sql: Option<&str>,
8) -> Result<SqlitePool, MigrateError> {
9    log::info!("進行資料庫遷移 {:?}!", file.as_ref());
10    let pool = SqlitePool::connect_with(
11        SqliteConnectOptions::new()
12            .filename(file)
13            .create_if_missing(true),
14    )
15    .await?;
16
17    if let Some(pre_sql) = pre_sql {
18        log::info!("Apply db pre script {}!", pre_sql);
19        sqlx::query(pre_sql).execute(&pool).await?;
20    }
21
22    sqlx::migrate!("./migrations").run(&pool).await?;
23    Ok(pool)
24}