FINALLY run sql on the db at startup
This commit is contained in:
parent
3a5fbbfb9b
commit
70f5ffd30c
File diff suppressed because it is too large
Load Diff
|
@ -6,8 +6,7 @@ edition = "2021"
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
diesel = { version = "2.1.4", features = ["postgres"] }
|
|
||||||
diesel_migrations = { version = "2.1.0", features = ["postgres"] }
|
|
||||||
rocket = {version="0.5.0",features=["secrets","json"]}
|
rocket = {version="0.5.0",features=["secrets","json"]}
|
||||||
rocket_db_pools = {version="0.1.0",features=["diesel_postgres"]}
|
rocket_db_pools = {version="0.1.0",features=["sqlx_postgres"]}
|
||||||
rocket_sync_db_pools = { version = "0.1.0", features = ["diesel_postgres_pool", "diesel"] }
|
sqlx = { version = "0.7.3", features = ["macros", "postgres"] }
|
||||||
|
# rocket_sync_db_pools = { version = "0.1.0", features = ["diesel_postgres_pool", "diesel"] }
|
||||||
|
|
56
src/main.rs
56
src/main.rs
|
@ -1,15 +1,19 @@
|
||||||
use rocket::fairing::AdHoc;
|
use rocket::fairing::{self, AdHoc, Fairing, Info, Kind};
|
||||||
use rocket::http::Status;
|
use rocket::http::Status;
|
||||||
use rocket::response::content::{self, RawHtml};
|
use rocket::response::content::{self, RawHtml};
|
||||||
use rocket::{Build, Request, Rocket};
|
use rocket::{Build, Request, Rocket};
|
||||||
use rocket_db_pools::diesel;
|
use rocket_db_pools::sqlx::pool::PoolConnection;
|
||||||
use rocket_db_pools::diesel::{prelude::*, PgPool, QueryResult};
|
use rocket_db_pools::sqlx::{Executor, PgConnection, Postgres, Row};
|
||||||
use rocket_db_pools::{Connection, Database};
|
use rocket_db_pools::Connection;
|
||||||
use rocket_sync_db_pools::database;
|
|
||||||
use std::fs;
|
use std::fs;
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate rocket;
|
extern crate rocket;
|
||||||
|
|
||||||
|
use rocket_db_pools::{
|
||||||
|
sqlx::{self, FromRow, PgPool},
|
||||||
|
Database,
|
||||||
|
};
|
||||||
|
|
||||||
use rocket::serde::{json::Json, Deserialize};
|
use rocket::serde::{json::Json, Deserialize};
|
||||||
use rocket::{
|
use rocket::{
|
||||||
fs::FileServer,
|
fs::FileServer,
|
||||||
|
@ -77,26 +81,21 @@ async fn logout(cookies: &CookieJar<'_>) -> &'static str {
|
||||||
#[database("diesel_postgres")]
|
#[database("diesel_postgres")]
|
||||||
struct Db(PgPool);
|
struct Db(PgPool);
|
||||||
|
|
||||||
#[derive(Queryable, Insertable)]
|
#[derive(FromRow)]
|
||||||
#[diesel(table_name = posts)]
|
|
||||||
struct Post {
|
struct Post {
|
||||||
id: i64,
|
id: i64,
|
||||||
title: String,
|
title: String,
|
||||||
published: bool,
|
published: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
rocket_db_pools::diesel::table! {
|
#[get("/dbtest/<id>")]
|
||||||
posts (id) {
|
async fn dbtest(mut db: Connection<Db>, id: i64) -> Option<String> {
|
||||||
id -> BigInt,
|
sqlx::query("SELECT content FROM posts WHERE id = ?")
|
||||||
title -> Text,
|
.bind(id)
|
||||||
published -> Bool,
|
.fetch_one(&mut **db)
|
||||||
}
|
.await
|
||||||
}
|
.and_then(|r| Ok(r.try_get(0)?))
|
||||||
|
.ok()
|
||||||
#[get("/dbtest")]
|
|
||||||
async fn dbtest(mut db: Connection<Db>) -> QueryResult<String> {
|
|
||||||
let post_ids: Vec<i64> = posts::table.select(posts::id).load(&mut db).await?;
|
|
||||||
Ok(format!("{post_ids:?}"))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[catch(default)]
|
#[catch(default)]
|
||||||
|
@ -108,23 +107,24 @@ fn default_catcher(status: Status, _: &Request) -> RawHtml<String> {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
// #[get("/migratetest")]
|
async fn migrate(rocket: Rocket<Build>) -> Rocket<Build> {
|
||||||
async fn migrate(mut rocket: Rocket<Build>) -> Rocket<Build> {
|
let db = Db::fetch(&rocket).unwrap();
|
||||||
let db = Db::fetch(&rocket).unwrap().get().await;
|
let mut conn: PoolConnection<Postgres> = db.acquire().await.unwrap();
|
||||||
|
|
||||||
let post_ids: Vec<i64> = posts::table
|
let row = conn
|
||||||
.select(posts::id)
|
.fetch_one(sqlx::query_as::<_, Post>(
|
||||||
.load(&mut db)
|
"SELECT * FROM table WHERE id = 1;",
|
||||||
.await
|
))
|
||||||
.unwrap();
|
.await;
|
||||||
|
|
||||||
rocket
|
rocket.manage(row)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[rocket::main]
|
#[rocket::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
let _rocket = rocket::build()
|
let _rocket = rocket::build()
|
||||||
.attach(Db::init())
|
.attach(Db::init())
|
||||||
|
// .attach(Post)
|
||||||
.attach(AdHoc::on_ignite("DB Migrations", migrate))
|
.attach(AdHoc::on_ignite("DB Migrations", migrate))
|
||||||
.mount("/", routes![hello, get_book, delay, login, logout, dbtest])
|
.mount("/", routes![hello, get_book, delay, login, logout, dbtest])
|
||||||
.register("/", catchers![default_catcher])
|
.register("/", catchers![default_catcher])
|
||||||
|
|
Loading…
Reference in New Issue