Lines
100 %
Functions
Branches
//! The store-level tag writers give up with `WriteConflict` when every
//! attempt finds their tag renamed.
use super::lock_protocol::link_raw_tag;
use super::*;
use crate::command::CmdError;
use crate::command::account::SetAccountTag;
use crate::command::account_merge::MergeAccounts;
use crate::db::DBError;
use crate::error::ServerError;
use finance::commodity::Commodity;
use sqlx::Connection;
/// Every attempt of `write` finds the tag row `linked` (`type=k0`) renamed by
/// a holder of its key; returns what `write` ended with.
async fn three_renames<T>(
fx: &Fixture,
name: &str,
linked: Uuid,
write: impl std::future::Future<Output = T>,
) -> anyhow::Result<T> {
let mut conns = Vec::new();
for _ in 0..3 {
conns.push(fx.user.get_connection().await?);
}
let mut holders = Vec::new();
for ((conn, held), next) in conns
.iter_mut()
.zip(["k0", "k1", "k2"])
.zip(["k1", "k2", "k3"])
{
let mut holder = conn.begin().await?;
hold_key(&mut holder, name, held).await?;
holders.push((holder, held, next));
let steer = rename_in_turn(fx, name, linked, holders);
let (written, steered) = tokio::join!(write, steer);
steered?;
Ok(written)
fn is_conflict<T>(outcome: &Result<T, ServerError>) -> bool {
matches!(outcome, Err(ServerError::DB(DBError::WriteConflict)))
#[local_db_sqlx_test]
async fn three_renames_fail_set_commodity_tag(pool: PgPool) -> anyhow::Result<()> {
let fx = fixture().await?;
let commodity = Commodity { id: fx.commodity };
fx.user
.set_commodity_tag(&commodity, &tag("type", "k0"))
.await?;
let linked: Uuid =
sqlx::query_scalar("SELECT id FROM tags WHERE tag_name = 'type' AND tag_value = 'k0'")
.fetch_one(&pool)
let new = tag("type", "new");
let written = three_renames(
&fx,
"type",
linked,
fx.user.set_commodity_tag(&commodity, &new),
)
assert!(is_conflict(&written), "{written:?}");
let types: Vec<_> = fx
.user
.get_commodity_tags(&commodity)
.await?
.into_iter()
.filter(|t| t.tag_name == "type")
.map(|t| t.tag_value)
.collect();
assert_eq!(types, vec!["k3"]);
async fn three_renames_fail_update_tag(pool: PgPool) -> anyhow::Result<()> {
let tx_id = fx.create_tx(None).await?;
let linked = link_raw_tag(&pool, tx_id, "type", "k0").await?;
let update = fx
.rename_tag(linked, "type".to_string(), "new".to_string(), None);
let written = three_renames(&fx, "type", linked, update).await?;
assert_eq!(fx.tx_tags(tx_id).await?, vec![pair("type", "k3")]);
async fn three_renames_fail_delete_tag(pool: PgPool) -> anyhow::Result<()> {
let written = three_renames(&fx, "type", linked, fx.user.delete_tag(linked)).await?;
async fn tag_row(pool: &PgPool, name: &str, value: &str) -> anyhow::Result<Uuid> {
let id = sqlx::query_scalar("SELECT id FROM tags WHERE tag_name = $1 AND tag_value = $2")
.bind(name)
.bind(value)
.fetch_one(pool)
Ok(id)
async fn three_renames_fail_set_transaction_tag(pool: PgPool) -> anyhow::Result<()> {
fx.user.set_transaction_tag(tx_id, &new),
async fn three_renames_fail_delete_script(pool: PgPool) -> anyhow::Result<()> {
let id = fx
.create_script(vec![0, 0x61, 0x73, 0x6d], Some("k0".to_string()))
let linked = tag_row(&pool, "name", "k0").await?;
let deleted = three_renames(&fx, "name", linked, fx.user.delete_script(id)).await?;
assert!(is_conflict(&deleted), "{deleted:?}");
assert_eq!(fx.user.get_script(id).await?.name.as_deref(), Some("k3"));
async fn three_renames_fail_update_script_name(pool: PgPool) -> anyhow::Result<()> {
let renamed = fx.user.update_script_name(id, Some("new".to_string()));
let renamed = three_renames(&fx, "name", linked, renamed).await?;
assert!(is_conflict(&renamed), "{renamed:?}");
async fn three_renames_fail_the_account_merge(pool: PgPool) -> anyhow::Result<()> {
let [into, from] = fx.accounts;
SetAccountTag::new()
.user_id(fx.user.id)
.account_id(from)
.tag_name("type".to_string())
.tag_value("k0".to_string())
.run()
let linked = tag_row(&pool, "type", "k0").await?;
let merge = MergeAccounts::new()
.into(into)
.from(from)
.run();
let merged = three_renames(&fx, "type", linked, merge).await?;
assert!(
matches!(
merged,
Err(CmdError::Server(ServerError::DB(DBError::WriteConflict)))
),
"{merged:?}"
);