Skip to content

Commit 2d8193c

Browse files
committed
feat: Introduce SQLite database module with connection pooling, schema definitions, and migration logic.
1 parent be05717 commit 2d8193c

1 file changed

Lines changed: 32 additions & 0 deletions

File tree

backend/src/db.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1027,8 +1027,40 @@ async fn apply_comment_migrations(
10271027
async fn apply_vote_migration(
10281028
tx: &mut sqlx::Transaction<'_, sqlx::Sqlite>,
10291029
) -> Result<(), sqlx::Error> {
1030+
// Create comment_votes table
10301031
sqlx::query(include_str!("../migrations/20241119_create_comment_votes.sql"))
10311032
.execute(&mut **tx)
10321033
.await?;
1034+
1035+
// Add votes column to comments if missing
1036+
let has_votes: bool = sqlx::query_scalar(
1037+
"SELECT COUNT(*) FROM pragma_table_info('comments') WHERE name='votes'",
1038+
)
1039+
.fetch_one(&mut **tx)
1040+
.await
1041+
.map(|count: i64| count > 0)?;
1042+
1043+
if !has_votes {
1044+
tracing::info!("Adding votes column to comments table");
1045+
sqlx::query("ALTER TABLE comments ADD COLUMN votes INTEGER NOT NULL DEFAULT 0")
1046+
.execute(&mut **tx)
1047+
.await?;
1048+
}
1049+
1050+
// Add is_admin column to comments if missing
1051+
let has_is_admin: bool = sqlx::query_scalar(
1052+
"SELECT COUNT(*) FROM pragma_table_info('comments') WHERE name='is_admin'",
1053+
)
1054+
.fetch_one(&mut **tx)
1055+
.await
1056+
.map(|count: i64| count > 0)?;
1057+
1058+
if !has_is_admin {
1059+
tracing::info!("Adding is_admin column to comments table");
1060+
sqlx::query("ALTER TABLE comments ADD COLUMN is_admin BOOLEAN NOT NULL DEFAULT FALSE")
1061+
.execute(&mut **tx)
1062+
.await?;
1063+
}
1064+
10331065
Ok(())
10341066
}

0 commit comments

Comments
 (0)