Skip to content

Commit

Permalink
Merge pull request #256 from ZeusWPI/feat/update-diesel
Browse files Browse the repository at this point in the history
  • Loading branch information
rien authored May 25, 2023
2 parents 449bf55 + 97c84e4 commit e28a946
Show file tree
Hide file tree
Showing 8 changed files with 317 additions and 131 deletions.
382 changes: 284 additions & 98 deletions Cargo.lock

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ urlencoding = "2.1"
toml = "0.5"
rand = "0.8"
regex = "1.6"
rocket = { version = "0.5.0-rc.2", features = [ "json", "secrets" ] }
rocket_sync_db_pools = { version = "0.1.0-rc.2", features = [ "diesel_postgres_pool" ] }
rocket = { version = "0.5.0-rc.3", features = [ "json", "secrets" ] }
rocket_sync_db_pools = { version = "0.1.0-rc.3", features = [ "diesel_postgres_pool" ] }
serde = "1.0"
serde_json = "1.0"
serde_derive = "1.0"
serde_urlencoded = "0.7"
simple_logger = "4.0"
diesel = { version = "1.4", features = ["postgres", "r2d2", "chrono"] }
diesel-derive-enum = { version = "1", features = ["postgres"] }
diesel_migrations = "1.4"
diesel = { version = "2.0", features = ["postgres", "r2d2", "chrono"] }
diesel-derive-enum = { version = "2.0", features = ["postgres"] }
diesel_migrations = "2.0"
tempfile = "3.1"
parking_lot = { version = "0.12" }
thiserror = "1.0"
Expand Down
16 changes: 8 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ pub mod models;
pub mod token_store;
pub mod util;

use diesel_migrations::MigrationHarness;
use lettre::message::Mailbox;
use rocket::fairing::AdHoc;
use rocket::figment::Figment;
Expand All @@ -64,6 +65,8 @@ pub struct DbConn(PgConnection);
pub type ConcreteConnection = PgConnection;

pub const ZAUTH_VERSION: &str = env!("CARGO_PKG_VERSION");
pub const MIGRATIONS: diesel_migrations::EmbeddedMigrations =
embed_migrations!("migrations");

#[get("/favicon.ico")]
pub fn favicon() -> &'static str {
Expand Down Expand Up @@ -161,17 +164,14 @@ fn assemble(rocket: Rocket<Build>) -> Rocket<Build> {
}

async fn prepare_database(rocket: Rocket<Build>) -> Rocket<Build> {
// This macro from `diesel_migrations` defines an `embedded_migrations`
// module containing a function named `run` that runs the migrations in the
// specified directory, initializing the database.
embed_migrations!("migrations");

eprintln!("Requesting a database connection.");
let db = DbConn::get_one(&rocket).await.expect("database connection");
eprintln!("Running migrations.");
db.run(|conn| embedded_migrations::run(conn))
.await
.expect("diesel migrations");
db.run(|conn| {
conn.run_pending_migrations(MIGRATIONS)
.expect("diesel migrations");
})
.await;

if rocket.figment().profile() == "debug" {
eprintln!("Seeding database.");
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ extern crate rocket;
extern crate zauth;

#[launch]
fn zauth() -> _ {
async fn zauth() -> _ {
zauth::prepare()
}
6 changes: 3 additions & 3 deletions src/models/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub struct NewClient {
}

#[derive(Insertable, Debug, Clone)]
#[table_name = "clients"]
#[diesel(table_name = clients)]
pub struct NewClientWithSecret {
pub name: String,
pub secret: String,
Expand Down Expand Up @@ -76,7 +76,7 @@ impl Client {
secret: Self::generate_random_secret(),
};
db.run(move |conn| {
conn.transaction(|| {
conn.transaction(|conn| {
// Create a new client
diesel::insert_into(clients::table)
.values(&client)
Expand Down Expand Up @@ -111,7 +111,7 @@ impl Client {
pub async fn update(self, db: &DbConn) -> Result<Self> {
let id = self.id;
db.run(move |conn| {
conn.transaction(|| {
conn.transaction(|conn| {
// Update a client
diesel::update(clients::table.find(id))
.set(self)
Expand Down
4 changes: 2 additions & 2 deletions src/models/mail.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub struct Mail {
}

#[derive(Clone, Debug, Deserialize, FromForm, Insertable, Validate)]
#[table_name = "mails"]
#[diesel(table_name = mails)]
pub struct NewMail {
pub author: String,
#[validate(length(min = 3, max = 255))]
Expand All @@ -51,7 +51,7 @@ impl NewMail {
self.validate()?;

db.run(move |conn| {
conn.transaction::<_, DieselError, _>(|| {
conn.transaction::<_, DieselError, _>(|conn| {
// Insert the new mail
diesel::insert_into(mails::table)
.values(&self)
Expand Down
14 changes: 7 additions & 7 deletions src/models/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ pub mod schema {
}

#[derive(Serialize, AsChangeset, Queryable, Associations, Debug, Clone)]
#[belongs_to(User)]
#[belongs_to(Client)]
#[table_name = "sessions"]
#[diesel(belongs_to(User))]
#[diesel(belongs_to(Client))]
#[diesel(table_name = sessions)]
pub struct Session {
pub id: i32,
pub key: Option<String>,
Expand All @@ -41,7 +41,7 @@ pub struct Session {
}

#[derive(Insertable, Debug, Clone)]
#[table_name = "sessions"]
#[diesel(table_name = sessions)]
pub struct NewSession {
pub key: Option<String>,
pub user_id: i32,
Expand All @@ -66,7 +66,7 @@ impl Session {
expires_at,
};
db.run(move |conn| {
conn.transaction(|| {
conn.transaction(|conn| {
// Create a new session
diesel::insert_into(sessions::table)
.values(&session)
Expand Down Expand Up @@ -96,7 +96,7 @@ impl Session {
expires_at,
};
db.run(move |conn| {
conn.transaction(|| {
conn.transaction(|conn| {
// Create a new session
diesel::insert_into(sessions::table)
.values(&session)
Expand All @@ -112,7 +112,7 @@ impl Session {
pub async fn update(self, db: &DbConn) -> Result<Self> {
let id = self.id;
db.run(move |conn| {
conn.transaction(|| {
conn.transaction(|conn| {
// Update a session
diesel::update(sessions::table.find(id))
.set(self)
Expand Down
14 changes: 7 additions & 7 deletions src/models/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ pub mod schema {
}

#[derive(Validate, Serialize, AsChangeset, Queryable, Debug, Clone)]
#[table_name = "users"]
#[changeset_options(treat_none_as_null = "true")]
#[diesel(table_name = users)]
#[diesel(treat_none_as_null = true)]
#[serde(crate = "rocket::serde")]
pub struct User {
pub id: i32,
Expand Down Expand Up @@ -133,7 +133,7 @@ pub struct NewUser {
}

#[derive(Serialize, Insertable, Debug, Clone)]
#[table_name = "users"]
#[diesel(table_name = users)]
struct PendingUserHashed {
username: String,
hashed_password: String,
Expand All @@ -147,7 +147,7 @@ struct PendingUserHashed {
}

#[derive(Serialize, Insertable, Debug, Clone)]
#[table_name = "users"]
#[diesel(table_name = users)]
struct NewUserHashed {
username: String,
hashed_password: String,
Expand Down Expand Up @@ -343,7 +343,7 @@ impl User {
last_login: Utc::now().naive_utc(),
};
db.run(move |conn| {
conn.transaction(|| {
conn.transaction(|conn| {
// Create a new user
diesel::insert_into(users::table)
.values(&user)
Expand Down Expand Up @@ -388,7 +388,7 @@ impl User {
last_login: Utc::now().naive_utc(),
};
db.run(move |conn| {
conn.transaction(|| {
conn.transaction(|conn| {
// Create a new user
diesel::insert_into(users::table)
.values(&user)
Expand Down Expand Up @@ -442,7 +442,7 @@ impl User {
pub async fn update(self, db: &DbConn) -> errors::Result<Self> {
let id = self.id;
db.run(move |conn| {
conn.transaction(|| {
conn.transaction(|conn| {
// Create a new user
diesel::update(users::table.find(id))
.set(self)
Expand Down

0 comments on commit e28a946

Please sign in to comment.