Skip to content

Commit

Permalink
move to postgresql, bump minor version
Browse files Browse the repository at this point in the history
  • Loading branch information
duckfromdiscord committed Dec 27, 2023
1 parent dd49c09 commit f710277
Show file tree
Hide file tree
Showing 10 changed files with 234 additions and 210 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ Cargo.lock

/target

Secrets.toml
Secrets.toml

.sqlx/
9 changes: 4 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "mljboard-bot"
version = "0.1.0"
version = "0.2.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand All @@ -13,10 +13,8 @@ shuttle = ["dep:shuttle-runtime", "dep:shuttle-secrets", "dep:shuttle-serenity",
[dependencies]
log = "0.4.20"
clap = "4.4.8"
mongodb = "2.7.1"
serenity = { version = "0.12.0", default-features = false, features = ["client", "gateway", "rustls_backend", "model"] }
tokio = { version = "1.34.0", features = ["rt", "time", "macros"] }
bson = "2.7.0"
rand = "0.8.5"
prefixed-api-key = { version = "0.1.0", features = ["sha2"] }
reqwest = "0.11.22"
Expand All @@ -30,5 +28,6 @@ lastfm = "0.6.1"
url = "2.5.0"
shuttle-runtime = { optional = true, version = "0.34.0" }
shuttle-secrets = { optional = true, version = "0.34.0" }
shuttle-serenity = { optional = true, version = "0.34.0" }
shuttle-shared-db = { optional = true, version = "0.34.0", features = ["mongodb"] }
shuttle-serenity = { optional = true, version = "0.34.1", features = ["serenity-0-12-rustls_backend"] }
shuttle-shared-db = { optional = true, version = "0.34.0", features = ["postgres-rustls"] }
sqlx = { version = "0.7.3", features = ["postgres", "runtime-tokio"] }
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ Create a `Secrets.toml` following the example in `Secrets.toml.example`. Keep in

- A Discord bot token. `-d <DISCORD BOT TOKEN>`
- A [HOS server](https://github.com/duckfromdiscord/hos-rv) and its password. `-j <IP> -k <PORT> -s <PASSWD>` and supply `--hos-https` if it's secure (recommended).
- A MongoDB database. `-m mongodb://x:x@x/x`. `mljboard-bot` should create any missing collections on its own.
- A PostgreSQL database in your `DATABASE_URL` env variable. `postgres://user:[email protected]:5432/mljboard`. `mljboard-bot` should create any missing tables on its own. Not needed when using with Shuttle of course.
- A Last.FM API key. `-l <API>`
16 changes: 16 additions & 0 deletions schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
CREATE TABLE IF NOT EXISTS discord_pairing_codes (
discord_username VARCHAR(45),
pairing_code VARCHAR(60)
);


CREATE TABLE IF NOT EXISTS discord_websites (
discord_username VARCHAR(45),
website VARCHAR(2083)
);


CREATE TABLE IF NOT EXISTS lastfm_usernames (
discord_username VARCHAR(45),
lastfm_username VARCHAR(50)
);
2 changes: 1 addition & 1 deletion src/db/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
pub mod mongo;
pub mod postgres;
11 changes: 0 additions & 11 deletions src/db/mongo.rs

This file was deleted.

104 changes: 104 additions & 0 deletions src/db/postgres.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
use sqlx::PgPool;

pub async fn start_db(postgres_url: String) -> Result<PgPool, sqlx::Error> {
PgPool::connect(&postgres_url).await
}

pub struct DiscordWebsiteUser {
pub discord_username: Option<String>,
pub website: Option<String>,
}

pub struct DiscordPairingCodeUser {
pub discord_username: Option<String>,
pub pairing_code: Option<String>,
}

pub async fn get_websites(pool: &PgPool, formatted_user: String) -> Vec<DiscordWebsiteUser> {
sqlx::query_as!(
DiscordWebsiteUser,
r#"
SELECT * FROM discord_websites
WHERE discord_username = $1
"#,
formatted_user
)
.fetch_all(pool)
.await
.expect("Failed to query DB for websites")
.into_iter()
.collect()
}

pub async fn get_discord_pairing_code(
pool: &PgPool,
formatted_user: String,
) -> Vec<DiscordPairingCodeUser> {
sqlx::query_as!(
DiscordPairingCodeUser,
r#"
SELECT * FROM discord_pairing_codes
WHERE discord_username = $1
"#,
formatted_user
)
.fetch_all(pool)
.await
.expect("Failed to query DB for pairing codes")
}

pub async fn insert_website(pool: &PgPool, formatted_user: String, website: String) {
let _ = sqlx::query!(
r#"
INSERT INTO discord_websites
VALUES ( $1, $2 )
"#,
formatted_user,
website
)
.execute(pool)
.await
.expect("Failed to add website to DB");
}

pub async fn insert_discord_pairing_code(pool: &PgPool, formatted_user: String, key: String) {
let _ = sqlx::query!(
r#"
INSERT INTO discord_pairing_codes
VALUES ( $1, $2 )
"#,
formatted_user,
key
)
.execute(pool)
.await
.expect("Failed to add pairing code to DB");
}

pub async fn delete_discord_pairing_code(pool: &PgPool, formatted_user: String) -> u64 {
sqlx::query!(
r#"
DELETE FROM discord_pairing_codes
WHERE discord_username = $1
"#,
formatted_user
)
.execute(pool)
.await
.expect("Failed to delete pairing code")
.rows_affected()
}

pub async fn delete_website(pool: &PgPool, formatted_user: String) -> u64 {
sqlx::query!(
r#"
DELETE FROM discord_websites
WHERE discord_username = $1
"#,
formatted_user
)
.execute(pool)
.await
.expect("Failed to delete website")
.rows_affected()
}
Loading

0 comments on commit f710277

Please sign in to comment.