From b0a177fdc51ea74103f513bcb7a51a41be9483f0 Mon Sep 17 00:00:00 2001 From: jrconlin Date: Thu, 3 Oct 2019 15:06:11 -0700 Subject: [PATCH 1/2] bug: Fix spanner batches to use new fxa_uid as userid Closes #242 --- src/db/spanner/batch.rs | 41 ++++++++++++++++++----------------------- 1 file changed, 18 insertions(+), 23 deletions(-) diff --git a/src/db/spanner/batch.rs b/src/db/spanner/batch.rs index 4ec2cce1e9..cd281aae1f 100644 --- a/src/db/spanner/batch.rs +++ b/src/db/spanner/batch.rs @@ -36,14 +36,14 @@ fn batch_string_to_bsos(bsos: &str) -> Result> { } pub fn create(db: &SpannerDb, params: params::CreateBatch) -> Result { - let user_id = params.user_id.legacy_id as i32; - let collection_id = db.get_collection_id(¶ms.collection)?; + let collection_id = db.get_collection_id(¶ms.collection)?.to_string(); + let fxa_uid = params.user_id.fxa_uid; let timestamp = db.timestamp()?.as_i64(); if params.bsos.is_empty() { - db.sql("INSERT INTO batches (userid, collection, id, bsos, expiry, timestamp) VALUES (@userid, @collectionid, @bsoid, @bsos, @expiry, @timestamp)")? + db.sql("INSERT INTO batches (userid, collection, id, bsos, expiry, timestamp) VALUES (@fxa_uid, @collectionid, @bsoid, @bsos, @expiry, @timestamp)")? .params(params! { - "userid" => user_id.to_string(), - "collectionid" => collection_id.to_string(), + "fxa_uid" => fxa_uid.clone(), + "collectionid" => collection_id.clone(), "bsoid" => to_rfc3339(timestamp)?, "timestamp" => to_rfc3339(timestamp)?, "bsos" => "".to_string(), @@ -65,10 +65,10 @@ pub fn create(db: &SpannerDb, params: params::CreateBatch) -> Result user_id.to_string(), - "collectionid" => collection_id.to_string(), + "fxa_uid" => fxa_uid.clone(), + "collectionid" => collection_id.clone(), "bsoid" => to_rfc3339(timestamp + i as i64)?, "timestamp" => to_rfc3339(timestamp)?, "bsos" => bsos, @@ -86,11 +86,10 @@ pub fn create(db: &SpannerDb, params: params::CreateBatch) -> Result Result { - let user_id = params.user_id.legacy_id as i32; let collection_id = db.get_collection_id(¶ms.collection)?; - let exists = db.sql("SELECT expiry FROM batches WHERE userid = @userid AND collection = @collectionid AND timestamp = @timestamp AND expiry > @expiry")? + let exists = db.sql("SELECT expiry FROM batches WHERE userid = @fxa_uid AND collection = @collectionid AND timestamp = @timestamp AND expiry > @expiry")? .params(params! { - "userid" => user_id.to_string(), + "fxa_uid" => params.user_id.fxa_uid, "collectionid" => collection_id.to_string(), "timestamp" => to_rfc3339(params.id)?, "expiry" => to_rfc3339(db.timestamp()?.as_i64())?, @@ -105,11 +104,10 @@ pub fn validate(db: &SpannerDb, params: params::ValidateBatch) -> Result { } pub fn select_max_id(db: &SpannerDb, params: params::ValidateBatch) -> Result { - let user_id = params.user_id.legacy_id as i32; let collection_id = db.get_collection_id(¶ms.collection)?; - let exists = db.sql("SELECT UNIX_MILLIS(id) FROM batches WHERE userid = @userid AND collection = @collectionid AND timestamp = @timestamp AND expiry > @expiry ORDER BY id DESC")? + let exists = db.sql("SELECT UNIX_MILLIS(id) FROM batches WHERE userid = @fxa_uid AND collection = @collectionid AND timestamp = @timestamp AND expiry > @expiry ORDER BY id DESC")? .params(params! { - "userid" => user_id.to_string(), + "fxa_uid" => params.user_id.fxa_uid, "collectionid" => collection_id.to_string(), "timestamp" => to_rfc3339(params.id)?, "expiry" => to_rfc3339(db.timestamp()?.as_i64())?, @@ -131,7 +129,6 @@ pub fn select_max_id(db: &SpannerDb, params: params::ValidateBatch) -> Result Result<()> { - let user_id = params.user_id.legacy_id as i32; let collection_id = db.get_collection_id(¶ms.collection)?; let timestamp = params.id; if let Ok(max_id) = select_max_id( @@ -151,9 +148,9 @@ pub fn append(db: &SpannerDb, params: params::AppendToBatch) -> Result<()> { "ttl": bso.ttl, }) .to_string(); - db.sql("INSERT INTO batches (userid, collection, id, bsos, expiry, timestamp) VALUES (@userid, @collectionid, @bsoid, @bsos, @expiry, @timestamp)")? + db.sql("INSERT INTO batches (userid, collection, id, bsos, expiry, timestamp) VALUES (@fxa_uid, @collectionid, @bsoid, @bsos, @expiry, @timestamp)")? .params(params! { - "userid" => user_id.to_string(), + "fxa_uid" => params.user_id.fxa_uid.clone(), "collectionid" => collection_id.to_string(), "bsoid" => to_rfc3339(params.id + i)?, "timestamp" => to_rfc3339(params.id)?, @@ -175,13 +172,12 @@ pub fn append(db: &SpannerDb, params: params::AppendToBatch) -> Result<()> { } pub fn get(db: &SpannerDb, params: params::GetBatch) -> Result> { - let user_id = params.user_id.legacy_id as i32; let collection_id = db.get_collection_id(¶ms.collection)?; let timestamp = db.timestamp()?.as_i64(); - let result = db.sql("SELECT id, bsos, expiry FROM batches WHERE userid = @userid AND collection = @collectionid AND timestamp = @bsoid AND expiry > @expiry")? + let result = db.sql("SELECT id, bsos, expiry FROM batches WHERE userid = @fxa_uid AND collection = @collectionid AND timestamp = @bsoid AND expiry > @expiry")? .params(params! { - "userid" => user_id.to_string(), + "fxa_uid" => params.user_id.fxa_uid, "collectionid" => collection_id.to_string(), "bsoid" => to_rfc3339(params.id)?, "expiry" => to_rfc3339(timestamp)?, @@ -206,14 +202,13 @@ pub fn get(db: &SpannerDb, params: params::GetBatch) -> Result Result<()> { - let user_id = params.user_id.legacy_id as i32; let collection_id = db.get_collection_id(¶ms.collection)?; db.sql( - "DELETE FROM batches WHERE userid = @userid AND collection = @collectionid AND timestamp = @bsoid", + "DELETE FROM batches WHERE userid = @fxa_uid AND collection = @collectionid AND timestamp = @bsoid", )? .params(params! { - "userid" => user_id.to_string(), + "fxa_uid" => params.user_id.fxa_uid, "collectionid" => collection_id.to_string(), "bsoid" => to_rfc3339(params.id)?, }) From 3ccd90d1ab6000673911fa76a91e5081e7c38558 Mon Sep 17 00:00:00 2001 From: jrconlin Date: Fri, 4 Oct 2019 08:49:00 -0700 Subject: [PATCH 2/2] f add fxa_kid to batch tables * evening brain is bad brain --- src/db/spanner/batch.rs | 149 ++++++++++++++++++++++++++-------------- 1 file changed, 98 insertions(+), 51 deletions(-) diff --git a/src/db/spanner/batch.rs b/src/db/spanner/batch.rs index cd281aae1f..351373aa66 100644 --- a/src/db/spanner/batch.rs +++ b/src/db/spanner/batch.rs @@ -37,24 +37,27 @@ fn batch_string_to_bsos(bsos: &str) -> Result> { pub fn create(db: &SpannerDb, params: params::CreateBatch) -> Result { let collection_id = db.get_collection_id(¶ms.collection)?.to_string(); - let fxa_uid = params.user_id.fxa_uid; let timestamp = db.timestamp()?.as_i64(); if params.bsos.is_empty() { - db.sql("INSERT INTO batches (userid, collection, id, bsos, expiry, timestamp) VALUES (@fxa_uid, @collectionid, @bsoid, @bsos, @expiry, @timestamp)")? - .params(params! { - "fxa_uid" => fxa_uid.clone(), - "collectionid" => collection_id.clone(), - "bsoid" => to_rfc3339(timestamp)?, - "timestamp" => to_rfc3339(timestamp)?, - "bsos" => "".to_string(), - "expiry" => to_rfc3339(timestamp + BATCH_LIFETIME)?, - }) - .param_types(param_types! { - "bsoid" => SpannerType::Timestamp, - "expiry" => SpannerType::Timestamp, - "timestamp" => SpannerType::Timestamp, - }) - .execute(&db.conn)?; + db.sql( + "INSERT INTO batches (userid, fxa_kid, collection, id, bsos, expiry, timestamp) + VALUES (@fxa_uid, @fxa_kid, @collectionid, @bsoid, @bsos, @expiry, @timestamp)", + )? + .params(params! { + "fxa_uid" => params.user_id.fxa_uid.clone(), + "fxa_kid" => params.user_id.fxa_kid.clone(), + "collectionid" => collection_id.clone(), + "bsoid" => to_rfc3339(timestamp)?, + "timestamp" => to_rfc3339(timestamp)?, + "bsos" => "".to_string(), + "expiry" => to_rfc3339(timestamp + BATCH_LIFETIME)?, + }) + .param_types(param_types! { + "bsoid" => SpannerType::Timestamp, + "expiry" => SpannerType::Timestamp, + "timestamp" => SpannerType::Timestamp, + }) + .execute(&db.conn)?; } for (i, bso) in (¶ms.bsos).iter().enumerate() { let bsos = json!({ @@ -65,21 +68,25 @@ pub fn create(db: &SpannerDb, params: params::CreateBatch) -> Result fxa_uid.clone(), - "collectionid" => collection_id.clone(), - "bsoid" => to_rfc3339(timestamp + i as i64)?, - "timestamp" => to_rfc3339(timestamp)?, - "bsos" => bsos, - "expiry" => to_rfc3339(timestamp + BATCH_LIFETIME)?, - }) - .param_types(param_types! { - "bsoid" => SpannerType::Timestamp, - "expiry" => SpannerType::Timestamp, - "timestamp" => SpannerType::Timestamp, - }) - .execute(&db.conn)?; + db.sql( + "INSERT INTO batches (userid, fxa_kid, collection, id, bsos, expiry, timestamp) + VALUES (@fxa_uid, @fxa_kid, @collectionid, @bsoid, @bsos, @expiry, @timestamp)", + )? + .params(params! { + "fxa_uid" => params.user_id.fxa_uid.clone(), + "fxa_kid" => params.user_id.fxa_kid.clone(), + "collectionid" => collection_id.clone(), + "bsoid" => to_rfc3339(timestamp + i as i64)?, + "timestamp" => to_rfc3339(timestamp)?, + "bsos" => bsos, + "expiry" => to_rfc3339(timestamp + BATCH_LIFETIME)?, + }) + .param_types(param_types! { + "bsoid" => SpannerType::Timestamp, + "expiry" => SpannerType::Timestamp, + "timestamp" => SpannerType::Timestamp, + }) + .execute(&db.conn)?; } Ok(timestamp) @@ -87,9 +94,18 @@ pub fn create(db: &SpannerDb, params: params::CreateBatch) -> Result Result { let collection_id = db.get_collection_id(¶ms.collection)?; - let exists = db.sql("SELECT expiry FROM batches WHERE userid = @fxa_uid AND collection = @collectionid AND timestamp = @timestamp AND expiry > @expiry")? + let exists = db + .sql( + "SELECT expiry FROM batches + WHERE userid = @fxa_uid + AND fxa_kid = @fxa_kid + AND collection = @collectionid + AND timestamp = @timestamp + AND expiry > @expiry", + )? .params(params! { "fxa_uid" => params.user_id.fxa_uid, + "fxa_kid" => params.user_id.fxa_kid, "collectionid" => collection_id.to_string(), "timestamp" => to_rfc3339(params.id)?, "expiry" => to_rfc3339(db.timestamp()?.as_i64())?, @@ -105,9 +121,20 @@ pub fn validate(db: &SpannerDb, params: params::ValidateBatch) -> Result { pub fn select_max_id(db: &SpannerDb, params: params::ValidateBatch) -> Result { let collection_id = db.get_collection_id(¶ms.collection)?; - let exists = db.sql("SELECT UNIX_MILLIS(id) FROM batches WHERE userid = @fxa_uid AND collection = @collectionid AND timestamp = @timestamp AND expiry > @expiry ORDER BY id DESC")? + let exists = db + .sql( + "SELECT UNIX_MILLIS(id) + FROM batches + WHERE userid = @fxa_uid + AND fxa_kid = @fxa_kid + AND collection = @collectionid + AND timestamp = @timestamp + AND expiry > @expiry + ORDER BY id DESC", + )? .params(params! { "fxa_uid" => params.user_id.fxa_uid, + "fxa_kid" => params.user_id.fxa_kid, "collectionid" => collection_id.to_string(), "timestamp" => to_rfc3339(params.id)?, "expiry" => to_rfc3339(db.timestamp()?.as_i64())?, @@ -148,21 +175,25 @@ pub fn append(db: &SpannerDb, params: params::AppendToBatch) -> Result<()> { "ttl": bso.ttl, }) .to_string(); - db.sql("INSERT INTO batches (userid, collection, id, bsos, expiry, timestamp) VALUES (@fxa_uid, @collectionid, @bsoid, @bsos, @expiry, @timestamp)")? - .params(params! { - "fxa_uid" => params.user_id.fxa_uid.clone(), - "collectionid" => collection_id.to_string(), - "bsoid" => to_rfc3339(params.id + i)?, - "timestamp" => to_rfc3339(params.id)?, - "expiry" => to_rfc3339(timestamp + BATCH_LIFETIME)?, - "bsos" => bsos, - }) - .param_types(param_types! { - "bsoid" => SpannerType::Timestamp, - "timestamp" => SpannerType::Timestamp, - "expiry" => SpannerType::Timestamp, - }) - .execute(&db.conn)?; + db.sql( + "INSERT INTO batches (userid, fxa_kid, collection, id, bsos, expiry, timestamp) + VALUES (@fxa_uid, @fxa_kid, @collectionid, @bsoid, @bsos, @expiry, @timestamp)", + )? + .params(params! { + "fxa_uid" => params.user_id.fxa_uid.clone(), + "fxa_kid" => params.user_id.fxa_kid.clone(), + "collectionid" => collection_id.to_string(), + "bsoid" => to_rfc3339(params.id + i)?, + "timestamp" => to_rfc3339(params.id)?, + "expiry" => to_rfc3339(timestamp + BATCH_LIFETIME)?, + "bsos" => bsos, + }) + .param_types(param_types! { + "bsoid" => SpannerType::Timestamp, + "timestamp" => SpannerType::Timestamp, + "expiry" => SpannerType::Timestamp, + }) + .execute(&db.conn)?; i += 1; } Ok(()) @@ -175,9 +206,19 @@ pub fn get(db: &SpannerDb, params: params::GetBatch) -> Result @expiry")? + let result = db + .sql( + "SELECT id, bsos, expiry + FROM batches + WHERE userid = @fxa_uid + AND fxa_kid = @fxa_kid + AND collection = @collectionid + AND timestamp = @bsoid + AND expiry > @expiry", + )? .params(params! { "fxa_uid" => params.user_id.fxa_uid, + "fxa_kid" => params.user_id.fxa_kid, "collectionid" => collection_id.to_string(), "bsoid" => to_rfc3339(params.id)?, "expiry" => to_rfc3339(timestamp)?, @@ -186,7 +227,8 @@ pub fn get(db: &SpannerDb, params: params::GetBatch) -> Result SpannerType::Timestamp, "expiry" => SpannerType::Timestamp, }) - .execute(&db.conn)?.all_or_none(); + .execute(&db.conn)? + .all_or_none(); if let Some(result) = result { Ok(Some(params::Batch { id: params.id, @@ -205,10 +247,15 @@ pub fn delete(db: &SpannerDb, params: params::DeleteBatch) -> Result<()> { let collection_id = db.get_collection_id(¶ms.collection)?; db.sql( - "DELETE FROM batches WHERE userid = @fxa_uid AND collection = @collectionid AND timestamp = @bsoid", + "DELETE FROM batches + WHERE userid = @fxa_uid + AND fxa_kid = @fxa_kid + AND collection = @collectionid + AND timestamp = @bsoid", )? .params(params! { "fxa_uid" => params.user_id.fxa_uid, + "fxa_kid" => params.user_id.fxa_kid, "collectionid" => collection_id.to_string(), "bsoid" => to_rfc3339(params.id)?, })