File tree Expand file tree Collapse file tree 3 files changed +14
-5
lines changed
examples/postgres/preferred-crates Expand file tree Collapse file tree 3 files changed +14
-5
lines changed Original file line number Diff line number Diff line change @@ -34,7 +34,7 @@ async fn main() -> anyhow::Result<()> {
34
34
uses_rust_decimal:: create_table ( & mut conn) . await ?;
35
35
uses_time:: create_table ( & mut conn) . await ?;
36
36
37
- let user_id = sqlx:: query !(
37
+ let user_id = sqlx:: query_scalar !(
38
38
"insert into users(username, password_hash) values($1, $2) returning id" ,
39
39
"user_foo" ,
40
40
"<pretend this is a password hash>" ,
@@ -46,6 +46,8 @@ async fn main() -> anyhow::Result<()> {
46
46
. fetch_one ( & mut conn)
47
47
. await ?;
48
48
49
+ println ! ( "Created user: {user:?}" ) ;
50
+
49
51
let session =
50
52
uses_time:: create_session ( & mut conn, SessionData { user_id } , SESSION_DURATION ) . await ?;
51
53
@@ -62,5 +64,7 @@ async fn main() -> anyhow::Result<()> {
62
64
. await ?
63
65
. expect ( "expected purchase" ) ;
64
66
67
+ println ! ( "Created purchase: {purchase:?}" ) ;
68
+
65
69
Ok ( ( ) )
66
70
}
Original file line number Diff line number Diff line change 1
1
use chrono:: { DateTime , Utc } ;
2
2
use sqlx:: PgExecutor ;
3
3
4
- #[ derive( sqlx:: FromRow ) ]
5
- struct Purchase {
4
+ #[ derive( sqlx:: FromRow , Debug ) ]
5
+ pub struct Purchase {
6
6
pub id : Uuid ,
7
7
pub user_id : Uuid ,
8
8
pub amount : Decimal ,
@@ -21,7 +21,7 @@ pub async fn create_table(e: impl PgExecutor<'_>) -> sqlx::Result<()> {
21
21
user_id uuid not null, \
22
22
amount numeric not null check(amount > 0), \
23
23
description text not null, \
24
- created_at timestamptz not null \
24
+ created_at timestamptz not null default now() \
25
25
);
26
26
" ,
27
27
)
Original file line number Diff line number Diff line change @@ -37,7 +37,12 @@ pub async fn create_session<D: Serialize>(
37
37
data : D ,
38
38
valid_duration : Duration ,
39
39
) -> sqlx:: Result < Session < D > > {
40
- let created_at = OffsetDateTime :: now_utc ( ) ;
40
+ // Round down to the nearest second because
41
+ // Postgres doesn't support precision higher than 1 microsecond anyway.
42
+ let created_at = OffsetDateTime :: now_utc ( )
43
+ . replace_nanosecond ( 0 )
44
+ . expect ( "0 nanoseconds should be in range" ) ;
45
+
41
46
let expires_at = created_at + valid_duration;
42
47
43
48
let id: Uuid = sqlx:: query_scalar (
You can’t perform that action at this time.
0 commit comments