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 ];
22
23 for sql in &migrations {
24 // Each migration file may contain multiple statements separated by semicolons.
25 for statement in sql.split(';') {
26 let trimmed = statement.trim();
27 if trimmed.is_empty() {
28 continue;
29 }
30 sqlx::query(trimmed)
31 .execute(pool)
32 .await
33 .expect("Failed to run migration");
34 }
35 }
36}