-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move to postgresql, bump minor version
- Loading branch information
1 parent
dd49c09
commit f710277
Showing
10 changed files
with
234 additions
and
210 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,4 +18,6 @@ Cargo.lock | |
|
||
/target | ||
|
||
Secrets.toml | ||
Secrets.toml | ||
|
||
.sqlx/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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>` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
pub mod mongo; | ||
pub mod postgres; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
Oops, something went wrong.