| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
pub mod migrations; |
| 15 |
pub mod repository; |
| 16 |
pub mod utils; |
| 17 |
|
| 18 |
use sqlx::sqlite::{SqliteConnectOptions, SqliteJournalMode, SqlitePoolOptions}; |
| 19 |
use sqlx::SqlitePool; |
| 20 |
use std::str::FromStr; |
| 21 |
use std::time::Duration; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
pub async fn init_pool(database_path: Option<&str>) -> Result<SqlitePool, sqlx::Error> { |
| 26 |
let path = database_path.unwrap_or("goingson.db"); |
| 27 |
|
| 28 |
let options = SqliteConnectOptions::from_str(&format!("sqlite:{}", path))? |
| 29 |
.create_if_missing(true) |
| 30 |
.foreign_keys(true) |
| 31 |
.journal_mode(SqliteJournalMode::Wal); |
| 32 |
|
| 33 |
SqlitePoolOptions::new() |
| 34 |
.max_connections(5) |
| 35 |
.acquire_timeout(Duration::from_secs(3)) |
| 36 |
.connect_with(options) |
| 37 |
.await |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
pub async fn run_migrations(pool: &SqlitePool) -> Result<(), sqlx::migrate::MigrateError> { |
| 42 |
sqlx::migrate!("../../migrations/sqlite") |
| 43 |
.run(pool) |
| 44 |
.await?; |
| 45 |
|
| 46 |
|
| 47 |
sqlx::query("PRAGMA foreign_keys = ON") |
| 48 |
.execute(pool) |
| 49 |
.await |
| 50 |
.map_err(|e| sqlx::migrate::MigrateError::Execute(e))?; |
| 51 |
|
| 52 |
Ok(()) |
| 53 |
} |
| 54 |
|
| 55 |
pub use repository::{ |
| 56 |
SqliteAttachmentRepository, |
| 57 |
SqliteBackupSettingsRepository, |
| 58 |
SqliteContactRepository, |
| 59 |
SqliteDailyNoteRepository, |
| 60 |
SqliteProjectRepository, |
| 61 |
SqliteTaskRepository, |
| 62 |
SqliteEventRepository, |
| 63 |
SqliteEmailRepository, |
| 64 |
SqliteUserRepository, |
| 65 |
SqliteEmailAccountRepository, |
| 66 |
SqliteStatsRepository, |
| 67 |
SqliteSearchRepository, |
| 68 |
SqliteMilestoneRepository, |
| 69 |
SqliteMonthlyReviewRepository, |
| 70 |
SqliteSavedViewRepository, |
| 71 |
SqliteSyncAccountRepository, |
| 72 |
SqliteWeeklyReviewRepository, |
| 73 |
}; |
| 74 |
|