this repo has no description
1use sqlx::sqlite::{SqliteConnectOptions, SqlitePoolOptions};
2use sqlx::SqlitePool;
3use std::str::FromStr;
4
5pub async fn init_pool(database_url: &str) -> SqlitePool {
6 let options = SqliteConnectOptions::from_str(database_url)
7 .expect("Invalid DATABASE_URL")
8 .create_if_missing(true);
9
10 SqlitePoolOptions::new()
11 .max_connections(5)
12 .connect_with(options)
13 .await
14 .expect("Failed to create database pool")
15}
16
17pub async fn run_migrations(pool: &SqlitePool) {
18 let migrations = [
19 include_str!("../migrations/001_create_users.sql"),
20 include_str!("../migrations/002_create_progress.sql"),
21 include_str!("../migrations/003_create_lesson_state.sql"),
22 include_str!("../migrations/004_add_streak_freezes.sql"),
23 ];
24
25 for sql in &migrations {
26 // Each migration file may contain multiple statements separated by semicolons.
27 for statement in sql.split(';') {
28 let trimmed = statement.trim();
29 if trimmed.is_empty() {
30 continue;
31 }
32 if let Err(e) = sqlx::query(trimmed).execute(pool).await {
33 // ALTER TABLE ADD COLUMN is not idempotent in SQLite —
34 // ignore "duplicate column" errors on restart.
35 let msg = e.to_string();
36 if msg.contains("duplicate column") {
37 continue;
38 }
39 panic!("Failed to run migration: {e}");
40 }
41 }
42 }
43}