https://linen.dev logo
Connecting and updating the schema
# help
b
Heya, can someone explain to me how the schema.sql relates to the postgres connection? I have been following the example for Authorization and everything works fine locally (After needing to psql into the db and create the schema) I am assuming this is the missing step server side as well however trying to use the psql with the provided connection url does not connect with cant translate "pg.shuttle.rs", either i'm blind (likely) or there seems to be little info on how to connect and update the schema manually, unless it is supposed to do it automatically. cc: @thousands-monkey-34609
k
Hey 👋
The schema should be ran on startup using sqlx. Which example are you trying to run specifically?
You shouldn't need to use psql.
Here is an example with rocket, the
pool.execute
will run the sql in
schema.sql
on application startup:
Copy code
rust
#[shuttle_service::main]
async fn rocket(#[shuttle_shared_db::Postgres] pool: PgPool) -> shuttle_service::ShuttleRocket {
    pool.execute(include_str!("../schema.sql"))
        .await
        .map_err(CustomError::new)?;

    let state = MyState { pool };
    let rocket = rocket::build()
        .mount("/todo", routes![retrieve, add])
        .manage(state);

    Ok(rocket)
}
t
Heya, I was following this https://docs.shuttle.rs/guide/authentication-tutorial.html And thank you, That example pointed me in the right direction to look explicitly for sqlx and the schema execution and found this in the full example
Copy code
rust
sqlx::Executor::execute(&pool, include_str!("../schema.sql"))
        .await
        .map_err(shuttle_service::error::CustomError::new)?;
hehe now i just have to get past the deployment not running like the rest of us i guess
k
Nice!
Hehe yes, there are some issues currently. Btw, while the docs post you linked should be updated, the linked source code isn't yet.
Just fyi, since there may be some confusion there 😂
We'll try to get to it soon
t
yea i understand that examples and source are hard to keep up to date as fast development occurs
After updating to 0.7.0 and getting a deployment running that chunk of code seems to not work D: the logs for where the it returns a internal error
Copy code
2022-10-18T21:53:10.625193868Z  INFO shuttle_deployer::deployment::run: starting up service
2022-10-18T22:01:52.285922718Z DEBUG hyper::proto::h1::io: parsed 11 headers
2022-10-18T22:01:52.286041024Z DEBUG hyper::proto::h1::conn: incoming body is content-length (409 bytes)
2022-10-18T22:01:52.286180716Z DEBUG hyper::proto::h1::conn: incoming body completed
2022-10-18T22:01:52.526699876Z  INFO sqlx::query: INSERT INTO users (username, …; rows affected: 0, rows returned: 0, elapsed: 461.389µs

INSERT INTO
  users (username, password)
VALUES
  ($1, $2) RETURNING id;
<HERE IS THE ERROR?>
2022-10-18T22:01:52.527118872Z DEBUG hyper::proto::h1::io: flushed 114 bytes
k
Hmm, strange! I'll see about updating the source code for this example and get back to you on this.
Hello again @thousands-monkey-34609, I updated the source now and it seems to work: . Let me know if this fixes your issues 🙂
b
Sorry i try the button at wrong place…