@@ -9,7 +9,7 @@ use diesel_async::{AsyncConnection, RunQueryDsl, TransactionManager};
99use syncstorage_db_common:: { error:: DbErrorIntrospect , params, results, util:: SyncTimestamp , Db } ;
1010use syncstorage_settings:: Quota ;
1111
12- use super :: PgDb ;
12+ use super :: { CollectionLock , PgDb } ;
1313use crate :: { pool:: Conn , schema:: user_collections, DbError , DbResult } ;
1414
1515#[ async_trait( ?Send ) ]
@@ -105,14 +105,11 @@ impl Db for PgDb {
105105 }
106106 self . session
107107 . coll_locks
108- . insert ( ( user_id as u32 , collection_id) , super :: CollectionLock :: Read ) ;
108+ . insert ( ( user_id as u32 , collection_id) , CollectionLock :: Read ) ;
109109 Ok ( ( ) )
110110 }
111111
112- async fn lock_for_write (
113- & mut self ,
114- params : params:: LockCollection ,
115- ) -> DbResult < results:: LockCollection > {
112+ async fn lock_for_write ( & mut self , params : params:: LockCollection ) -> DbResult < ( ) > {
116113 let user_id = params. user_id . legacy_id as i64 ;
117114 let collection_id = self . get_or_create_collection_id ( & params. collection ) . await ?;
118115
@@ -132,10 +129,26 @@ impl Db for PgDb {
132129 . select ( user_collections:: modified)
133130 . filter ( user_collections:: fxa_uid. eq ( user_id) )
134131 . filter ( user_collections:: collection_id. eq ( collection_id) )
135- . for_share ( )
132+ . for_update ( )
136133 . first ( & mut self . conn )
137134 . await
138135 . optional ( ) ?;
136+
137+ if let Some ( modified) = modified {
138+ let modified = SyncTimestamp :: from_i64 ( modified) ?;
139+ // Do not allow write if it would incorrectly increment timestamp.
140+ if modified >= self . timestamp ( ) {
141+ return Err ( DbError :: conflict ( ) ) ;
142+ }
143+ self . session
144+ . coll_modified_cache
145+ . insert ( ( user_id as u32 , collection_id) , modified) ;
146+ }
147+
148+ self . session
149+ . coll_locks
150+ . insert ( ( user_id as u32 , collection_id) , CollectionLock :: Write ) ;
151+ Ok ( ( ) )
139152 }
140153
141154 async fn get_collection_timestamps (
0 commit comments