Datasets:
DOI:
License:
| use std::path::Path; | |
| use sqlx::{Connection, SqliteConnection, sqlite::SqliteConnectOptions}; | |
| use crate::database::migrations::run_migrations; | |
| pub async fn open_or_create_database(path: &Path) -> anyhow::Result<()> { | |
| if let Some(parent) = path.parent() { | |
| std::fs::create_dir_all(parent)?; | |
| } | |
| let options = SqliteConnectOptions::new() | |
| .filename(path) | |
| .create_if_missing(true); | |
| let mut connection = SqliteConnection::connect_with(&options).await?; | |
| run_migrations(&mut connection).await?; | |
| connection.close().await?; | |
| Ok(()) | |
| } | |
| pub async fn open_existing_database(path: &Path) -> anyhow::Result<()> { | |
| if !path.exists() { | |
| anyhow::bail!("数据库文件不存在:{}", path.display()); | |
| } | |
| let options = SqliteConnectOptions::new() | |
| .filename(path) | |
| .create_if_missing(false); | |
| let mut connection = SqliteConnection::connect_with(&options).await?; | |
| run_migrations(&mut connection).await?; | |
| connection.close().await?; | |
| Ok(()) | |
| } | |