Skip to content

Commit

Permalink
Alter telegram_userid type from i64 to String (#12)
Browse files Browse the repository at this point in the history
* alter database schema and prettify message

Database originally stored telegram_userid as an i64 even though the return type was u64. Converted to varchar(20) to be able to store correctly.

* Update Cargo.toml versions
  • Loading branch information
reubenwong97 authored Oct 10, 2023
1 parent e4a229c commit 0ebf2bd
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 21 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "telerun"
version = "0.0.5"
version = "0.0.6"
edition = "2021"
license = "MIT"
description = "Rust telegram bot for tracking runs."
Expand All @@ -12,8 +12,8 @@ publish = true

[dependencies]
log = "0.4"
pretty_env_logger = "0.4"
shuttle-runtime = "0.24.0"
pretty_env_logger = "0.5"
shuttle-runtime = "0.29.0"
tokio = { version = "1.26.0" }
sqlx = { version = "0.7.1", features = [
"runtime-tokio-native-tls",
Expand All @@ -22,8 +22,8 @@ sqlx = { version = "0.7.1", features = [
"chrono",
] }
teloxide = { version = "0.12.0", features = ["macros"] }
shuttle-shared-db = { version = "0.24.0", features = ["postgres", "sqlx"] }
shuttle-secrets = "0.24.0"
shuttle-shared-db = { version = "0.29.0", features = ["postgres", "sqlx"] }
shuttle-secrets = "0.29.0"
reqwest = "0.11.18"
askama = "0.12.0"
tracing = "0.1.37"
2 changes: 2 additions & 0 deletions migrations/20231010143842_schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- Add migration script here
ALTER TABLE users ALTER COLUMN telegram_userid TYPE varchar(20) USING telegram_userid::varchar(20);
2 changes: 1 addition & 1 deletion src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ pub async fn get_users_in_chat(
.iter()
.map(|user_row| User {
id: user_row.id,
telegram_userid: user_row.telegram_userid,
telegram_userid: user_row.telegram_userid.clone(),
chat_id: user_row.chat_id.clone(),
user_name: user_row.user_name.clone(),
})
Expand Down
21 changes: 12 additions & 9 deletions src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ impl fmt::Display for User {

impl fmt::Display for Score {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{} {} {}", self.user_name, self.medals, self.distance)
write!(
f,
"{} {}πŸ… {}km",
self.user_name, self.medals, self.distance
)
}
}

Expand Down Expand Up @@ -147,7 +151,6 @@ mod tests {
},
];
let render = list_runs(Some(runs));
// TODO: i actually dont want this kind of html templates anyway
let ans = "#. RunID Distance RunTime
1. 1 1 1970-01-01 00:01:01 1
2. 2 2 1970-01-01 00:01:22 2
Expand All @@ -168,13 +171,13 @@ mod tests {
let users = vec![
User {
id: 1,
telegram_userid: 1,
telegram_userid: 1.to_string(),
chat_id: "chat1".into(),
user_name: "meme".into(),
},
User {
id: 2,
telegram_userid: 2,
telegram_userid: 2.to_string(),
chat_id: "chat1".into(),
user_name: "youyou".into(),
},
Expand Down Expand Up @@ -226,11 +229,11 @@ mod tests {
];
let render = display_tally(Some(scores));
let ans = "#. UserName Medals Distance (km)
πŸ₯‡ 1. reuben 5 20
πŸ₯ˆ 2. milton 2 10
πŸ₯‰ 3. jerrell 1 1
πŸƒ 4. taigy 1 0.2
🀑 5. riley 2 0.1
πŸ₯‡ 1. reuben 5πŸ… 20km
πŸ₯ˆ 2. milton 2πŸ… 10km
πŸ₯‰ 3. jerrell 1πŸ… 1km
πŸƒ 4. taigy 1πŸ… 0.2km
🀑 5. riley 2πŸ… 0.1km
";
assert_eq!(render, ans);
}
Expand Down
2 changes: 1 addition & 1 deletion src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub struct User {
/// PostgreSQL does not natively support u64 values,
/// and thus we will attempt to cast the values from Telegram
/// as i64 first before storing in DB.
pub telegram_userid: i64,
pub telegram_userid: String,
/// Id of telegram chat
pub chat_id: String,
/// Self-specified username
Expand Down

0 comments on commit 0ebf2bd

Please sign in to comment.