-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement writing users & user ratings to the database. (#14)
- Loading branch information
1 parent
279a1b1
commit 2bc63fe
Showing
21 changed files
with
486 additions
and
31 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
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
Empty file.
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,2 @@ | ||
DROP FUNCTION IF EXISTS diesel_manage_updated_at(_tbl regclass); | ||
DROP FUNCTION IF EXISTS diesel_set_updated_at(); |
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,36 @@ | ||
-- This file was automatically created by Diesel to setup helper functions | ||
-- and other internal bookkeeping. This file is safe to edit, any future | ||
-- changes will be added to existing projects as new migrations. | ||
|
||
|
||
|
||
|
||
-- Sets up a trigger for the given table to automatically set a column called | ||
-- `updated_at` whenever the row is modified (unless `updated_at` was included | ||
-- in the modified columns) | ||
-- | ||
-- # Example | ||
-- | ||
-- ```sql | ||
-- CREATE TABLE users (id SERIAL PRIMARY KEY, updated_at TIMESTAMP NOT NULL DEFAULT NOW()); | ||
-- | ||
-- SELECT diesel_manage_updated_at('users'); | ||
-- ``` | ||
CREATE OR REPLACE FUNCTION diesel_manage_updated_at(_tbl regclass) RETURNS VOID AS $$ | ||
BEGIN | ||
EXECUTE format('CREATE TRIGGER set_updated_at BEFORE UPDATE ON %s | ||
FOR EACH ROW EXECUTE PROCEDURE diesel_set_updated_at()', _tbl); | ||
END; | ||
$$ LANGUAGE plpgsql; | ||
|
||
CREATE OR REPLACE FUNCTION diesel_set_updated_at() RETURNS trigger AS $$ | ||
BEGIN | ||
IF ( | ||
NEW IS DISTINCT FROM OLD AND | ||
NEW.updated_at IS NOT DISTINCT FROM OLD.updated_at | ||
) THEN | ||
NEW.updated_at := current_timestamp; | ||
END IF; | ||
RETURN NEW; | ||
END; | ||
$$ LANGUAGE plpgsql; |
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 @@ | ||
drop table users cascade; |
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,6 @@ | ||
create table users ( | ||
user_id serial primary key, | ||
bot bool not null default false, | ||
discriminator varchar not null, | ||
name varchar not null | ||
); |
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 @@ | ||
drop table user_ratings cascade; |
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,5 @@ | ||
create table user_ratings ( | ||
id serial primary key, | ||
user_id integer references users, | ||
rating numeric(2) not null | ||
); |
1 change: 1 addition & 0 deletions
1
migrations/2018-02-11-225942_alter_user_ratings_user_id_non_null/down.sql
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 @@ | ||
alter table user_ratings alter column user_id drop not null; |
1 change: 1 addition & 0 deletions
1
migrations/2018-02-11-225942_alter_user_ratings_user_id_non_null/up.sql
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 @@ | ||
alter table user_ratings alter column user_id set not null; |
1 change: 1 addition & 0 deletions
1
migrations/2018-02-18-031306_users_discriminator_name_unique_together/down.sql
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 @@ | ||
alter table users drop constraint "users_discriminator_name_key"; |
1 change: 1 addition & 0 deletions
1
migrations/2018-02-18-031306_users_discriminator_name_unique_together/up.sql
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 @@ | ||
alter table users add unique(discriminator, name); |
1 change: 1 addition & 0 deletions
1
migrations/2018-02-18-034202_alter_column_users_discriminator_as_integer/down.sql
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 @@ | ||
alter table users alter column discriminator type varchar; |
1 change: 1 addition & 0 deletions
1
migrations/2018-02-18-034202_alter_column_users_discriminator_as_integer/up.sql
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 @@ | ||
alter table users alter column discriminator type integer using discriminator::integer; |
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 |
---|---|---|
@@ -0,0 +1,71 @@ | ||
use diesel::{ Connection, RunQueryDsl, PgConnection }; | ||
use diesel::insert_into; | ||
use r2d2; | ||
use r2d2_diesel::ConnectionManager; | ||
use serenity::model::user::User; | ||
use std::env; | ||
use std::ops::Deref; | ||
use typemap::Key; | ||
|
||
use schema::users::dsl::*; | ||
use schema::user_ratings::dsl::*; | ||
use tables::insert::{ Users as IUsers, UserRatings as IUserRatings }; | ||
use tables::query::{ Users as QUsers, UserRatings as QUserRatings }; | ||
|
||
// Connection request guard type: a wrapper around an r2d2 pooled connection. | ||
pub struct DbConn(pub r2d2::PooledConnection<ConnectionManager<PgConnection>>); | ||
|
||
// For the convenience of using an &DbConn as an &SqliteConnection. | ||
impl Deref for DbConn { | ||
type Target = PgConnection; | ||
|
||
fn deref(&self) -> &Self::Target { | ||
&self.0 | ||
} | ||
} | ||
|
||
pub struct Pool; | ||
|
||
impl Key for Pool { | ||
type Value = r2d2::Pool<ConnectionManager<PgConnection>>; | ||
} | ||
|
||
/// Initializes a database pool. | ||
pub fn init_pool(db_url: Option<String>) -> r2d2::Pool<ConnectionManager<PgConnection>> { | ||
let database_url = match db_url { | ||
Some(url) => url, | ||
_ => env::var("DATABASE_URL").expect("DATABASE_URL must be defined") | ||
}; | ||
let manager = ConnectionManager::<PgConnection>::new(database_url); | ||
r2d2::Pool::builder().build(manager).expect("Failed to create pool.") | ||
} | ||
|
||
pub fn create_user_and_ratings( | ||
conn: r2d2::PooledConnection<ConnectionManager<PgConnection>>, | ||
user: User | ||
) -> Result<(), String> { | ||
|
||
match insert_into(users).values(&IUsers::from(user)).get_result::<QUsers>(&*conn) { | ||
Ok(user_record) => if let Ok(_ratings_record) = insert_into(user_ratings) | ||
.values(&IUserRatings::from(user_record)) | ||
.get_result::<QUserRatings>(&*conn) { | ||
Ok(()) | ||
} else { | ||
Err("No user_ratings reckid created".to_string()) | ||
}, | ||
Err(e) => Err(format!("{:?}", e)) | ||
} | ||
|
||
// if let Ok(user_record) = insert_into(users).values(&IUsers::from(user)).get_result::<QUsers>(&*conn) { | ||
// if let Ok(_ratings_record) = insert_into(user_ratings) | ||
// .values(&IUserRatings::from(user_record)) | ||
// .get_result::<QUserRatings>(&*conn) | ||
// { | ||
// Ok(()) | ||
// } else { | ||
// Err("Could not create user_ratings record".to_string()) | ||
// } | ||
// } else { | ||
// Err("Could not create users record".to_string()) | ||
// } | ||
} |
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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
table! { | ||
user_ratings (id) { | ||
id -> Int4, | ||
user_id -> Int4, | ||
rating -> Numeric, | ||
} | ||
} | ||
|
||
table! { | ||
users (user_id) { | ||
user_id -> Int4, | ||
bot -> Bool, | ||
discriminator -> Int4, | ||
name -> Varchar, | ||
} | ||
} | ||
|
||
joinable!(user_ratings -> users (user_id)); |
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,43 @@ | ||
pub mod query { | ||
use bigdecimal::BigDecimal; | ||
|
||
#[primary_key(user_id)] | ||
#[table_name="users"] | ||
#[derive(Queryable, Associations)] | ||
pub struct Users { | ||
pub user_id: i32, | ||
pub bot: bool, | ||
pub discriminator: i32, | ||
pub name: String | ||
} | ||
|
||
#[table_name = "user_ratings"] | ||
#[derive(Queryable, Associations)] | ||
#[belongs_to(Users)] | ||
pub struct UserRatings { | ||
pub id: i32, | ||
pub user_id: i32, | ||
pub rating: BigDecimal | ||
} | ||
} | ||
|
||
pub mod insert { | ||
use bigdecimal::BigDecimal; | ||
use schema::*; | ||
|
||
#[table_name="users"] | ||
#[derive(Insertable)] | ||
pub struct Users { | ||
pub bot: bool, | ||
pub discriminator: i32, | ||
pub name: String | ||
} | ||
|
||
#[table_name = "user_ratings"] | ||
#[derive(Insertable)] | ||
#[belongs_to(Users)] | ||
pub struct UserRatings { | ||
pub user_id: i32, | ||
pub rating: BigDecimal | ||
} | ||
} |
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