Skip to content

Commit 9da2e25

Browse files
committed
refactor: DB schema
1 parent 3012f47 commit 9da2e25

File tree

8 files changed

+25
-64
lines changed

8 files changed

+25
-64
lines changed

Cargo.lock

Lines changed: 2 additions & 37 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@ rust-embed = "8.0"
2121
tempfile = "3.9"
2222
tokio = { version = "1.0", features = ["macros", "rt-multi-thread"] }
2323
uuid = { version = "1.0", features = ["v4"] }
24-
tower = { version = "0.4", features = ["util"] }
24+
tower = { version = "0.5.2", features = ["util"] }

config.example.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ bot:
33
default_invite_max_age: 300 # Default 5 minutes
44

55
database:
6-
path: "data/bot.db"
6+
path: "sqlite:data/bot.db"
77

88
server:
99
external_url: "https://your-domain.com:8080" # External access URL

src/http_server.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,9 +180,7 @@ mod tests {
180180
token: "test_token".to_string(),
181181
default_invite_max_age: 300,
182182
},
183-
database: DatabaseConfig {
184-
path: "test.db".to_string(),
185-
},
183+
database: DatabaseConfig { uri: db_url },
186184
server: ServerConfig {
187185
external_url: "http://localhost:8080".to_string(),
188186
bind: "127.0.0.1:8080".to_string(),

src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ async fn main() -> Result<(), Error> {
6666
let config = Config::load(std::env::var("CONFIG_PATH").unwrap().as_str())?;
6767

6868
// Initialize database connection pool
69-
let db = utils::db::create_pool(&config.database.path).await?;
69+
let db = utils::db::create_pool(&config.database.uri).await?;
7070

7171
// Start HTTP server
7272
let server_config = config.clone();

src/utils/config.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ pub struct BotConfig {
2525

2626
#[derive(Debug, Clone, Serialize, Deserialize)]
2727
pub struct DatabaseConfig {
28-
pub path: String,
28+
pub uri: String,
2929
}
3030

3131
#[derive(Debug, Clone, Serialize, Deserialize)]
@@ -91,16 +91,17 @@ mod tests {
9191
use super::*;
9292
use std::io::Write;
9393
use tempfile::NamedTempFile;
94+
use uuid::Uuid;
9495

9596
fn create_test_config() -> (NamedTempFile, Config) {
97+
let db_url = format!("sqlite:file:{}?mode=memory", Uuid::new_v4());
98+
9699
let config = Config {
97100
bot: BotConfig {
98101
token: "test_token".to_string(),
99102
default_invite_max_age: 300,
100103
},
101-
database: DatabaseConfig {
102-
path: "test.db".to_string(),
103-
},
104+
database: DatabaseConfig { uri: db_url },
104105
server: ServerConfig {
105106
external_url: "http://localhost:8080".to_string(),
106107
bind: "127.0.0.1:8080".to_string(),

src/utils/db.rs

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use sqlx::types::time::OffsetDateTime;
2-
use sqlx::SqlitePool;
2+
type Pool = sqlx::Pool<sqlx::Sqlite>;
33

4-
pub async fn setup_database(pool: &SqlitePool) -> Result<(), sqlx::Error> {
4+
pub async fn setup_database(pool: &Pool) -> Result<(), sqlx::Error> {
55
sqlx::query!(
66
"CREATE TABLE IF NOT EXISTS invites (
77
id TEXT PRIMARY KEY,
@@ -19,14 +19,14 @@ pub async fn setup_database(pool: &SqlitePool) -> Result<(), sqlx::Error> {
1919
Ok(())
2020
}
2121

22-
pub async fn create_pool(database_path: &str) -> Result<SqlitePool, sqlx::Error> {
23-
let pool = SqlitePool::connect(&format!("sqlite:{}", database_path)).await?;
22+
pub async fn create_pool(database_path: &str) -> Result<Pool, sqlx::Error> {
23+
let pool = Pool::connect(database_path).await?;
2424
setup_database(&pool).await?;
2525
Ok(pool)
2626
}
2727

2828
pub async fn create_invite(
29-
pool: &SqlitePool,
29+
pool: &Pool,
3030
invite_id: &str,
3131
guild_id: &str,
3232
creator_id: &str,
@@ -45,7 +45,7 @@ pub async fn create_invite(
4545
}
4646

4747
pub async fn get_unused_invite(
48-
pool: &SqlitePool,
48+
pool: &Pool,
4949
invite_id: &str,
5050
) -> Result<Option<InviteRecord>, sqlx::Error> {
5151
sqlx::query_as!(
@@ -58,7 +58,7 @@ pub async fn get_unused_invite(
5858
}
5959

6060
pub async fn update_invite_code(
61-
pool: &SqlitePool,
61+
pool: &Pool,
6262
invite_id: &str,
6363
discord_code: &str,
6464
) -> Result<(), sqlx::Error> {
@@ -73,7 +73,7 @@ pub async fn update_invite_code(
7373
}
7474

7575
pub async fn count_used_invites(
76-
pool: &SqlitePool,
76+
pool: &Pool,
7777
creator_id: &str,
7878
guild_id: &str,
7979
days: i32,
@@ -112,7 +112,7 @@ pub struct InviteInfo {
112112
}
113113

114114
pub async fn get_user_invite_info(
115-
pool: &SqlitePool,
115+
pool: &Pool,
116116
user_id: &str,
117117
) -> Result<Option<InviteInfo>, sqlx::Error> {
118118
sqlx::query_as!(
@@ -131,7 +131,7 @@ pub async fn get_user_invite_info(
131131

132132
// 新增:記錄使用邀請的用戶
133133
pub async fn record_invite_use(
134-
pool: &SqlitePool,
134+
pool: &Pool,
135135
invite_id: &str,
136136
user_id: &str,
137137
) -> Result<(), sqlx::Error> {
@@ -152,7 +152,7 @@ pub async fn record_invite_use(
152152
}
153153

154154
pub async fn find_invite_by_code(
155-
pool: &SqlitePool,
155+
pool: &Pool,
156156
discord_code: &str,
157157
) -> Result<Option<String>, sqlx::Error> {
158158
sqlx::query_scalar!(
@@ -171,7 +171,7 @@ pub struct InviteLeaderboardEntry {
171171
}
172172

173173
pub async fn get_invite_leaderboard(
174-
pool: &SqlitePool,
174+
pool: &Pool,
175175
guild_id: &str,
176176
days: i32,
177177
) -> Result<Vec<InviteLeaderboardEntry>, sqlx::Error> {
@@ -203,10 +203,9 @@ pub async fn get_invite_leaderboard(
203203
#[cfg(test)]
204204
mod tests {
205205
use super::*;
206-
use sqlx::SqlitePool;
207206
use uuid::Uuid;
208207

209-
async fn setup_test_db() -> SqlitePool {
208+
async fn setup_test_db() -> Pool {
210209
let db_url = format!("sqlite:file:{}?mode=memory", Uuid::new_v4());
211210
let pool = create_pool(&db_url).await.unwrap();
212211
sqlx::migrate!().run(&pool).await.unwrap();

src/utils/test_helpers.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@ impl TestContext {
1717
token: "test_token".to_string(),
1818
default_invite_max_age: 300,
1919
},
20-
database: crate::utils::config::DatabaseConfig {
21-
path: "test.db".to_string(),
22-
},
20+
database: crate::utils::config::DatabaseConfig { uri: db_url },
2321
server: crate::utils::config::ServerConfig {
2422
external_url: "http://localhost:8080".to_string(),
2523
bind: "127.0.0.1:8080".to_string(),

0 commit comments

Comments
 (0)