mirror of
https://github.com/LukeMathWalker/zero-to-production.git
synced 2024-11-23 09:11:01 +00:00
Update sqlx to 0.7
This commit is contained in:
parent
789c359e47
commit
384916fddd
14 changed files with 1021 additions and 715 deletions
4
.github/workflows/general.yml
vendored
4
.github/workflows/general.yml
vendored
|
@ -14,7 +14,7 @@ on:
|
|||
|
||||
env:
|
||||
CARGO_TERM_COLOR: always
|
||||
SQLX_VERSION: 0.6.2
|
||||
SQLX_VERSION: 0.7.1
|
||||
SQLX_FEATURES: "rustls,postgres"
|
||||
|
||||
jobs:
|
||||
|
@ -49,7 +49,7 @@ jobs:
|
|||
SKIP_DOCKER=true ./scripts/init_db.sh
|
||||
- name: Check sqlx-data.json is up-to-date
|
||||
run: |
|
||||
cargo sqlx prepare --check -- --bin zero2prod
|
||||
cargo sqlx prepare --workspace --check
|
||||
- name: Run tests
|
||||
run: cargo test
|
||||
|
||||
|
|
|
@ -0,0 +1,17 @@
|
|||
{
|
||||
"db_name": "PostgreSQL",
|
||||
"query": "\n INSERT INTO subscriptions (id, email, name, subscribed_at, status)\n VALUES ($1, $2, $3, $4, 'pending_confirmation')\n ",
|
||||
"describe": {
|
||||
"columns": [],
|
||||
"parameters": {
|
||||
"Left": [
|
||||
"Uuid",
|
||||
"Text",
|
||||
"Text",
|
||||
"Timestamptz"
|
||||
]
|
||||
},
|
||||
"nullable": []
|
||||
},
|
||||
"hash": "51c9c995452d3359e3da7e2f2ff8a6e68690f740a36d2a32ec7c40b08931ebdb"
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
{
|
||||
"db_name": "PostgreSQL",
|
||||
"query": "\n INSERT INTO subscription_tokens (subscription_token, subscriber_id)\n VALUES ($1, $2)\n ",
|
||||
"describe": {
|
||||
"columns": [],
|
||||
"parameters": {
|
||||
"Left": [
|
||||
"Text",
|
||||
"Uuid"
|
||||
]
|
||||
},
|
||||
"nullable": []
|
||||
},
|
||||
"hash": "753c8ecfac0ea7d052e60cb582e3b3ebac5e50eb133152712ca18ab5d5e202f3"
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
{
|
||||
"db_name": "PostgreSQL",
|
||||
"query": "\n SELECT email\n FROM subscriptions\n WHERE status = 'confirmed'\n ",
|
||||
"describe": {
|
||||
"columns": [
|
||||
{
|
||||
"ordinal": 0,
|
||||
"name": "email",
|
||||
"type_info": "Text"
|
||||
}
|
||||
],
|
||||
"parameters": {
|
||||
"Left": []
|
||||
},
|
||||
"nullable": [
|
||||
false
|
||||
]
|
||||
},
|
||||
"hash": "7b57e2776a245ba1602f638121550485e2219a6ccaaa62b5ec3e4683e33a3b5f"
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
{
|
||||
"db_name": "PostgreSQL",
|
||||
"query": "UPDATE subscriptions SET status = 'confirmed' WHERE id = $1",
|
||||
"describe": {
|
||||
"columns": [],
|
||||
"parameters": {
|
||||
"Left": [
|
||||
"Uuid"
|
||||
]
|
||||
},
|
||||
"nullable": []
|
||||
},
|
||||
"hash": "a71a1932b894572106460ca2e34a63dc0cb8c1ba7a70547add1cddbb68133c2b"
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
{
|
||||
"db_name": "PostgreSQL",
|
||||
"query": "\n SELECT user_id, password_hash\n FROM users\n WHERE username = $1\n ",
|
||||
"describe": {
|
||||
"columns": [
|
||||
{
|
||||
"ordinal": 0,
|
||||
"name": "user_id",
|
||||
"type_info": "Uuid"
|
||||
},
|
||||
{
|
||||
"ordinal": 1,
|
||||
"name": "password_hash",
|
||||
"type_info": "Text"
|
||||
}
|
||||
],
|
||||
"parameters": {
|
||||
"Left": [
|
||||
"Text"
|
||||
]
|
||||
},
|
||||
"nullable": [
|
||||
false,
|
||||
false
|
||||
]
|
||||
},
|
||||
"hash": "acf1b96c82ddf18db02e71a0e297c822b46f10add52c54649cf599b883165e58"
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
{
|
||||
"db_name": "PostgreSQL",
|
||||
"query": "SELECT subscriber_id FROM subscription_tokens WHERE subscription_token = $1",
|
||||
"describe": {
|
||||
"columns": [
|
||||
{
|
||||
"ordinal": 0,
|
||||
"name": "subscriber_id",
|
||||
"type_info": "Uuid"
|
||||
}
|
||||
],
|
||||
"parameters": {
|
||||
"Left": [
|
||||
"Text"
|
||||
]
|
||||
},
|
||||
"nullable": [
|
||||
false
|
||||
]
|
||||
},
|
||||
"hash": "ad120337ee606be7b8d87238e2bb765d0da8ee61b1a3bc142414c4305ec5e17f"
|
||||
}
|
1482
Cargo.lock
generated
1482
Cargo.lock
generated
File diff suppressed because it is too large
Load diff
|
@ -17,7 +17,7 @@ actix-web = "4"
|
|||
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }
|
||||
serde = "1.0.115"
|
||||
config = { version = "0.13", default-features = false, features = ["yaml"] }
|
||||
sqlx = { version = "0.6", default-features = false, features = ["runtime-actix-rustls", "macros", "postgres", "uuid", "chrono", "migrate", "offline"] }
|
||||
sqlx = { version = "0.7", default-features = false, features = ["runtime-tokio-rustls", "macros", "postgres", "uuid", "chrono", "migrate"] }
|
||||
uuid = { version = "1", features = ["v4"] }
|
||||
chrono = { version = "0.4.22", default-features = false, features = ["clock"] }
|
||||
reqwest = { version = "0.11", default-features = false, features = ["json", "rustls-tls"] }
|
||||
|
|
|
@ -10,7 +10,7 @@ fi
|
|||
if ! [ -x "$(command -v sqlx)" ]; then
|
||||
echo >&2 "Error: sqlx is not installed."
|
||||
echo >&2 "Use:"
|
||||
echo >&2 " cargo install --version='~0.6' sqlx-cli --no-default-features --features rustls,postgres"
|
||||
echo >&2 " cargo install --version='~0.7' sqlx-cli --no-default-features --features rustls,postgres"
|
||||
echo >&2 "to install it."
|
||||
exit 1
|
||||
fi
|
||||
|
|
107
sqlx-data.json
107
sqlx-data.json
|
@ -1,107 +0,0 @@
|
|||
{
|
||||
"db": "PostgreSQL",
|
||||
"51c9c995452d3359e3da7e2f2ff8a6e68690f740a36d2a32ec7c40b08931ebdb": {
|
||||
"query": "\n INSERT INTO subscriptions (id, email, name, subscribed_at, status)\n VALUES ($1, $2, $3, $4, 'pending_confirmation')\n ",
|
||||
"describe": {
|
||||
"columns": [],
|
||||
"parameters": {
|
||||
"Left": [
|
||||
"Uuid",
|
||||
"Text",
|
||||
"Text",
|
||||
"Timestamptz"
|
||||
]
|
||||
},
|
||||
"nullable": []
|
||||
}
|
||||
},
|
||||
"753c8ecfac0ea7d052e60cb582e3b3ebac5e50eb133152712ca18ab5d5e202f3": {
|
||||
"query": "\n INSERT INTO subscription_tokens (subscription_token, subscriber_id)\n VALUES ($1, $2)\n ",
|
||||
"describe": {
|
||||
"columns": [],
|
||||
"parameters": {
|
||||
"Left": [
|
||||
"Text",
|
||||
"Uuid"
|
||||
]
|
||||
},
|
||||
"nullable": []
|
||||
}
|
||||
},
|
||||
"7b57e2776a245ba1602f638121550485e2219a6ccaaa62b5ec3e4683e33a3b5f": {
|
||||
"query": "\n SELECT email\n FROM subscriptions\n WHERE status = 'confirmed'\n ",
|
||||
"describe": {
|
||||
"columns": [
|
||||
{
|
||||
"ordinal": 0,
|
||||
"name": "email",
|
||||
"type_info": "Text"
|
||||
}
|
||||
],
|
||||
"parameters": {
|
||||
"Left": []
|
||||
},
|
||||
"nullable": [
|
||||
false
|
||||
]
|
||||
}
|
||||
},
|
||||
"a71a1932b894572106460ca2e34a63dc0cb8c1ba7a70547add1cddbb68133c2b": {
|
||||
"query": "UPDATE subscriptions SET status = 'confirmed' WHERE id = $1",
|
||||
"describe": {
|
||||
"columns": [],
|
||||
"parameters": {
|
||||
"Left": [
|
||||
"Uuid"
|
||||
]
|
||||
},
|
||||
"nullable": []
|
||||
}
|
||||
},
|
||||
"acf1b96c82ddf18db02e71a0e297c822b46f10add52c54649cf599b883165e58": {
|
||||
"query": "\n SELECT user_id, password_hash\n FROM users\n WHERE username = $1\n ",
|
||||
"describe": {
|
||||
"columns": [
|
||||
{
|
||||
"ordinal": 0,
|
||||
"name": "user_id",
|
||||
"type_info": "Uuid"
|
||||
},
|
||||
{
|
||||
"ordinal": 1,
|
||||
"name": "password_hash",
|
||||
"type_info": "Text"
|
||||
}
|
||||
],
|
||||
"parameters": {
|
||||
"Left": [
|
||||
"Text"
|
||||
]
|
||||
},
|
||||
"nullable": [
|
||||
false,
|
||||
false
|
||||
]
|
||||
}
|
||||
},
|
||||
"ad120337ee606be7b8d87238e2bb765d0da8ee61b1a3bc142414c4305ec5e17f": {
|
||||
"query": "SELECT subscriber_id FROM subscription_tokens WHERE subscription_token = $1",
|
||||
"describe": {
|
||||
"columns": [
|
||||
{
|
||||
"ordinal": 0,
|
||||
"name": "subscriber_id",
|
||||
"type_info": "Uuid"
|
||||
}
|
||||
],
|
||||
"parameters": {
|
||||
"Left": [
|
||||
"Text"
|
||||
]
|
||||
},
|
||||
"nullable": [
|
||||
false
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
|
@ -2,7 +2,6 @@ use crate::domain::SubscriberEmail;
|
|||
use secrecy::{ExposeSecret, Secret};
|
||||
use serde_aux::field_attributes::deserialize_number_from_string;
|
||||
use sqlx::postgres::{PgConnectOptions, PgSslMode};
|
||||
use sqlx::ConnectOptions;
|
||||
use std::convert::{TryFrom, TryInto};
|
||||
|
||||
#[derive(serde::Deserialize, Clone)]
|
||||
|
@ -47,9 +46,7 @@ impl DatabaseSettings {
|
|||
}
|
||||
|
||||
pub fn with_db(&self) -> PgConnectOptions {
|
||||
let mut options = self.without_db().database(&self.database_name);
|
||||
options.log_statements(tracing::log::LevelFilter::Trace);
|
||||
options
|
||||
self.without_db().database(&self.database_name)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -7,7 +7,7 @@ use anyhow::Context;
|
|||
use chrono::Utc;
|
||||
use rand::distributions::Alphanumeric;
|
||||
use rand::{thread_rng, Rng};
|
||||
use sqlx::{PgPool, Postgres, Transaction};
|
||||
use sqlx::{Executor, PgPool, Postgres, Transaction};
|
||||
use std::convert::{TryFrom, TryInto};
|
||||
use uuid::Uuid;
|
||||
|
||||
|
@ -135,7 +135,7 @@ pub async fn insert_subscriber(
|
|||
new_subscriber: &NewSubscriber,
|
||||
) -> Result<Uuid, sqlx::Error> {
|
||||
let subscriber_id = Uuid::new_v4();
|
||||
sqlx::query!(
|
||||
let query = sqlx::query!(
|
||||
r#"
|
||||
INSERT INTO subscriptions (id, email, name, subscribed_at, status)
|
||||
VALUES ($1, $2, $3, $4, 'pending_confirmation')
|
||||
|
@ -144,9 +144,8 @@ pub async fn insert_subscriber(
|
|||
new_subscriber.email.as_ref(),
|
||||
new_subscriber.name.as_ref(),
|
||||
Utc::now()
|
||||
)
|
||||
.execute(transaction)
|
||||
.await?;
|
||||
);
|
||||
transaction.execute(query).await?;
|
||||
Ok(subscriber_id)
|
||||
}
|
||||
|
||||
|
@ -159,17 +158,15 @@ pub async fn store_token(
|
|||
subscriber_id: Uuid,
|
||||
subscription_token: &str,
|
||||
) -> Result<(), StoreTokenError> {
|
||||
sqlx::query!(
|
||||
let query = sqlx::query!(
|
||||
r#"
|
||||
INSERT INTO subscription_tokens (subscription_token, subscriber_id)
|
||||
VALUES ($1, $2)
|
||||
"#,
|
||||
subscription_token,
|
||||
subscriber_id
|
||||
)
|
||||
.execute(transaction)
|
||||
.await
|
||||
.map_err(StoreTokenError)?;
|
||||
);
|
||||
transaction.execute(query).await.map_err(StoreTokenError)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
|
|
@ -59,7 +59,6 @@ impl Application {
|
|||
|
||||
pub async fn get_connection_pool(configuration: &DatabaseSettings) -> Result<PgPool, sqlx::Error> {
|
||||
PgPoolOptions::new()
|
||||
.acquire_timeout(std::time::Duration::from_secs(2))
|
||||
.connect_with(configuration.with_db())
|
||||
.await
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue