use super::common::pgwire_harness::TestServer;
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn bulk_update_reconciles_secondary_index() {
let server = TestServer::start().await;
server
.exec("CREATE COLLECTION idx_bulk_update")
.await
.unwrap();
server
.exec("CREATE INDEX ON idx_bulk_update(status)")
.await
.unwrap();
server
.exec("INSERT INTO idx_bulk_update { id: 'a', status: 'active' }")
.await
.unwrap();
server
.exec("INSERT INTO idx_bulk_update { id: 'b', status: 'active' }")
.await
.unwrap();
server
.exec("UPDATE idx_bulk_update SET status = 'archived' WHERE status = 'active'")
.await
.unwrap();
let mut archived = server
.query_text("SELECT id FROM idx_bulk_update WHERE status = 'archived'")
.await
.expect("indexed SELECT on new value must succeed");
archived.sort();
assert_eq!(
archived,
vec!["a".to_string(), "b".to_string()],
"index lookup on the new value must return both updated rows; got: {archived:?}"
);
let stale = server
.query_text("SELECT id FROM idx_bulk_update WHERE status = 'active'")
.await
.expect("indexed SELECT on old value must succeed");
assert!(
stale.is_empty(),
"index lookup on the old value must return no rows after the UPDATE; \
a stale secondary-index entry survived: {stale:?}"
);
let primary = server
.query_text("SELECT status FROM idx_bulk_update WHERE id = 'a'")
.await
.expect("primary read must succeed");
assert_eq!(
primary,
vec!["archived".to_string()],
"primary document for id 'a' must show the updated status; got: {primary:?}"
);
}
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn point_update_reconciles_secondary_index() {
let server = TestServer::start().await;
server
.exec("CREATE COLLECTION idx_point_update")
.await
.unwrap();
server
.exec("CREATE INDEX ON idx_point_update(email)")
.await
.unwrap();
server
.exec("INSERT INTO idx_point_update { id: 'a', email: 'old@x.z' }")
.await
.unwrap();
server
.exec("UPDATE idx_point_update SET email = 'new@x.z' WHERE id = 'a'")
.await
.unwrap();
let updated = server
.query_text("SELECT id FROM idx_point_update WHERE email = 'new@x.z'")
.await
.expect("indexed SELECT on new value must succeed");
assert_eq!(
updated,
vec!["a".to_string()],
"index lookup on the new value must return the updated row; got: {updated:?}"
);
let stale = server
.query_text("SELECT id FROM idx_point_update WHERE email = 'old@x.z'")
.await
.expect("indexed SELECT on old value must succeed");
assert!(
stale.is_empty(),
"index lookup on the old value must return no rows after the UPDATE; \
a stale secondary-index entry survived: {stale:?}"
);
let primary = server
.query_text("SELECT email FROM idx_point_update WHERE id = 'a'")
.await
.expect("primary read must succeed");
assert_eq!(
primary,
vec!["new@x.z".to_string()],
"primary document for id 'a' must show the updated email; got: {primary:?}"
);
}