FINALLY run sql on the db at startup

This commit is contained in:
SadlyNotSappho 2024-01-23 11:55:13 -08:00
parent 3a5fbbfb9b
commit 70f5ffd30c
3 changed files with 891 additions and 425 deletions

1253
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -6,8 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[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_db_pools = {version="0.1.0",features=["diesel_postgres"]}
rocket_sync_db_pools = { version = "0.1.0", features = ["diesel_postgres_pool", "diesel"] }
rocket_db_pools = {version="0.1.0",features=["sqlx_postgres"]}
sqlx = { version = "0.7.3", features = ["macros", "postgres"] }
# rocket_sync_db_pools = { version = "0.1.0", features = ["diesel_postgres_pool", "diesel"] }

View File

@ -1,15 +1,19 @@
use rocket::fairing::AdHoc;
use rocket::fairing::{self, AdHoc, Fairing, Info, Kind};
use rocket::http::Status;
use rocket::response::content::{self, RawHtml};
use rocket::{Build, Request, Rocket};
use rocket_db_pools::diesel;
use rocket_db_pools::diesel::{prelude::*, PgPool, QueryResult};
use rocket_db_pools::{Connection, Database};
use rocket_sync_db_pools::database;
use rocket_db_pools::sqlx::pool::PoolConnection;
use rocket_db_pools::sqlx::{Executor, PgConnection, Postgres, Row};
use rocket_db_pools::Connection;
use std::fs;
#[macro_use]
extern crate rocket;
use rocket_db_pools::{
sqlx::{self, FromRow, PgPool},
Database,
};
use rocket::serde::{json::Json, Deserialize};
use rocket::{
fs::FileServer,
@ -77,26 +81,21 @@ async fn logout(cookies: &CookieJar<'_>) -> &'static str {
#[database("diesel_postgres")]
struct Db(PgPool);
#[derive(Queryable, Insertable)]
#[diesel(table_name = posts)]
#[derive(FromRow)]
struct Post {
id: i64,
title: String,
published: bool,
}
rocket_db_pools::diesel::table! {
posts (id) {
id -> BigInt,
title -> Text,
published -> Bool,
}
}
#[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:?}"))
#[get("/dbtest/<id>")]
async fn dbtest(mut db: Connection<Db>, id: i64) -> Option<String> {
sqlx::query("SELECT content FROM posts WHERE id = ?")
.bind(id)
.fetch_one(&mut **db)
.await
.and_then(|r| Ok(r.try_get(0)?))
.ok()
}
#[catch(default)]
@ -108,23 +107,24 @@ fn default_catcher(status: Status, _: &Request) -> RawHtml<String> {
)
}
// #[get("/migratetest")]
async fn migrate(mut rocket: Rocket<Build>) -> Rocket<Build> {
let db = Db::fetch(&rocket).unwrap().get().await;
async fn migrate(rocket: Rocket<Build>) -> Rocket<Build> {
let db = Db::fetch(&rocket).unwrap();
let mut conn: PoolConnection<Postgres> = db.acquire().await.unwrap();
let post_ids: Vec<i64> = posts::table
.select(posts::id)
.load(&mut db)
.await
.unwrap();
let row = conn
.fetch_one(sqlx::query_as::<_, Post>(
"SELECT * FROM table WHERE id = 1;",
))
.await;
rocket
rocket.manage(row)
}
#[rocket::main]
async fn main() {
let _rocket = rocket::build()
.attach(Db::init())
// .attach(Post)
.attach(AdHoc::on_ignite("DB Migrations", migrate))
.mount("/", routes![hello, get_book, delay, login, logout, dbtest])
.register("/", catchers![default_catcher])