mirror of
https://github.com/amtoaer/bili-sync.git
synced 2026-09-04 23:18:02 +08:00
feat: 支持在程序启动时执行 migration
This commit is contained in:
+16
-1
@@ -1,6 +1,21 @@
|
|||||||
|
use migration::{Migrator, MigratorTrait};
|
||||||
use sea_orm::{Database, DatabaseConnection};
|
use sea_orm::{Database, DatabaseConnection};
|
||||||
|
use tokio::fs;
|
||||||
|
|
||||||
use crate::Result;
|
use crate::Result;
|
||||||
pub async fn database_connection() -> Result<DatabaseConnection> {
|
pub async fn database_connection() -> Result<DatabaseConnection> {
|
||||||
Ok(Database::connect("sqlite://./data.sqlite?mode=rwc").await?)
|
let config_dir = dirs::config_dir().ok_or("No config path found")?;
|
||||||
|
let target = config_dir.join("bili-sync").join("data.sqlite");
|
||||||
|
if let Some(parent) = target.parent() {
|
||||||
|
fs::create_dir_all(parent).await?;
|
||||||
|
}
|
||||||
|
Ok(Database::connect(format!(
|
||||||
|
"sqlite://{}?mode=rwc",
|
||||||
|
config_dir.join("bili-sync").join("data.sqlite").to_str().unwrap()
|
||||||
|
))
|
||||||
|
.await?)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn migrate_database(connection: &DatabaseConnection) -> Result<()> {
|
||||||
|
Ok(Migrator::up(connection, None).await?)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user