1- use diesel:: dsl:: sql;
21use diesel:: * ;
3- use schema:: connection_without_transaction;
2+ use schema:: { connection_without_transaction, DropTable } ;
43
54table ! {
65 auto_time {
@@ -11,50 +10,66 @@ table! {
1110}
1211
1312#[ test]
14- #[ cfg( feature = "postgres" ) ]
13+ #[ cfg( any ( feature = "postgres" , feature = "sqlite" ) ) ]
1514fn managing_updated_at_for_table ( ) {
16- use self :: auto_time:: columns:: * ;
17- use self :: auto_time:: table as auto_time;
18- use diesel:: pg:: types:: date_and_time:: PgTimestamp ;
15+ use self :: auto_time:: dsl:: * ;
16+ use chrono:: NaiveDateTime ;
17+ use schema_dsl:: * ;
18+ use std:: { thread, time:: Duration } ;
1919
2020 // transactions have frozen time, so we can't use them
2121 let connection = connection_without_transaction ( ) ;
22- connection
23- . execute (
24- "CREATE TABLE auto_time (
25- id SERIAL PRIMARY KEY,
26- n INTEGER,
27- updated_at TIMESTAMP
28- );" ,
29- )
30- . unwrap ( ) ;
31- connection
32- . execute ( "SELECT diesel_manage_updated_at('auto_time');" )
22+ create_table (
23+ "auto_time" ,
24+ (
25+ integer ( "id" ) . primary_key ( ) . auto_increment ( ) ,
26+ integer ( "n" ) ,
27+ timestamp ( "updated_at" ) ,
28+ ) ,
29+ )
30+ . execute ( & connection)
31+ . unwrap ( ) ;
32+ let _guard = DropTable {
33+ connection : & connection,
34+ table_name : "auto_time" ,
35+ } ;
36+ sql_query ( "SELECT diesel_manage_updated_at('auto_time')" )
37+ . execute ( & connection)
3338 . unwrap ( ) ;
3439
35- connection
36- . execute ( "INSERT INTO auto_time (n) VALUES (2), (1), (5);" )
40+ insert_into ( auto_time)
41+ . values ( & vec ! [ n. eq( 2 ) , n. eq( 1 ) , n. eq( 5 ) ] )
42+ . execute ( & connection)
3743 . unwrap ( ) ;
38- let result = select ( sql ( "COUNT(*) FROM auto_time WHERE updated_at IS NULL" ) )
44+
45+ let result = auto_time
46+ . count ( )
47+ . filter ( updated_at. is_null ( ) )
3948 . get_result :: < i64 > ( & connection) ;
4049 assert_eq ! ( Ok ( 3 ) , result) ;
4150
42- connection
43- . execute ( "UPDATE auto_time SET n = n + 1 WHERE true;" )
51+ update ( auto_time)
52+ . set ( n. eq ( n + 1 ) )
53+ . execute ( & connection)
4454 . unwrap ( ) ;
45- let result = select ( sql ( "COUNT(*) FROM auto_time WHERE updated_at IS NULL" ) )
55+
56+ let result = auto_time
57+ . count ( )
58+ . filter ( updated_at. is_null ( ) )
4659 . get_result :: < i64 > ( & connection) ;
4760 assert_eq ! ( Ok ( 0 ) , result) ;
4861
62+ if cfg ! ( feature = "sqlite" ) {
63+ // SQLite only has second precision
64+ thread:: sleep ( Duration :: from_millis ( 1000 ) ) ;
65+ }
66+
4967 let query = auto_time. find ( 2 ) . select ( updated_at) ;
50- let old_time: PgTimestamp = query. first ( & connection) . unwrap ( ) ;
68+ let old_time: NaiveDateTime = query. first ( & connection) . unwrap ( ) ;
5169 update ( auto_time. find ( 2 ) )
5270 . set ( n. eq ( 0 ) )
5371 . execute ( & connection)
5472 . unwrap ( ) ;
55- let new_time: PgTimestamp = query. first ( & connection) . unwrap ( ) ;
73+ let new_time: NaiveDateTime = query. first ( & connection) . unwrap ( ) ;
5674 assert ! ( old_time < new_time) ;
57-
58- // clean up because we aren't in a transaction
59- connection. execute ( "DROP TABLE auto_time;" ) . unwrap ( ) ;
6075}
0 commit comments