forked from diesel-rs/diesel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnection.rs
More file actions
60 lines (53 loc) · 1.71 KB
/
Copy pathconnection.rs
File metadata and controls
60 lines (53 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
use schema::connection_without_transaction;
use diesel::*;
use diesel::dsl::sql;
table! {
auto_time {
id -> Integer,
n -> Integer,
updated_at -> Timestamp,
}
}
#[test]
#[cfg(feature = "postgres")]
fn managing_updated_at_for_table() {
use self::auto_time::columns::*;
use self::auto_time::table as auto_time;
use diesel::pg::types::date_and_time::PgTimestamp;
// transactions have frozen time, so we can't use them
let connection = connection_without_transaction();
connection
.execute(
"CREATE TABLE auto_time (
id SERIAL PRIMARY KEY,
n INTEGER,
updated_at TIMESTAMP
);",
)
.unwrap();
connection
.execute("SELECT diesel_manage_updated_at('auto_time');")
.unwrap();
connection
.execute("INSERT INTO auto_time (n) VALUES (2), (1), (5);")
.unwrap();
let result = select(sql("COUNT(*) FROM auto_time WHERE updated_at IS NULL"))
.get_result::<i64>(&connection);
assert_eq!(Ok(3), result);
connection
.execute("UPDATE auto_time SET n = n + 1 WHERE true;")
.unwrap();
let result = select(sql("COUNT(*) FROM auto_time WHERE updated_at IS NULL"))
.get_result::<i64>(&connection);
assert_eq!(Ok(0), result);
let query = auto_time.find(2).select(updated_at);
let old_time: PgTimestamp = query.first(&connection).unwrap();
update(auto_time.find(2))
.set(n.eq(0))
.execute(&connection)
.unwrap();
let new_time: PgTimestamp = query.first(&connection).unwrap();
assert!(old_time < new_time);
// clean up because we aren't in a transaction
connection.execute("DROP TABLE auto_time;").unwrap();
}