1
//! Legacy-shaped rows: two transactions sharing one price, and an ordinary
2
//! transaction with a price of its own.
3

            
4
use sqlx::PgPool;
5
use sqlx::types::Uuid;
6

            
7
pub(crate) struct Rows {
8
    pub(crate) tx_a: Uuid,
9
    pub(crate) tx_b: Uuid,
10
    pub(crate) tx_own: Uuid,
11
    pub(crate) cross_price: Uuid,
12
    pub(crate) own_price: Uuid,
13
}
14

            
15
462
fn id(n: u128) -> Uuid {
16
462
    Uuid::from_u128(n)
17
462
}
18

            
19
33
async fn put_transaction(pool: &PgPool, tx: u128, splits: [u128; 2]) -> anyhow::Result<()> {
20
33
    sqlx::query("INSERT INTO transactions (id, post_date, enter_date) VALUES ($1, now(), now())")
21
33
        .bind(id(tx))
22
33
        .execute(pool)
23
33
        .await?;
24
33
    for (split, value) in splits.into_iter().zip([-5_i64, 5]) {
25
66
        sqlx::query(
26
66
            "INSERT INTO splits (id, tx_id, account_id, commodity_id, value_num, value_denom) \
27
66
             VALUES ($1, $2, $3, $4, $5, 1)",
28
66
        )
29
66
        .bind(id(split))
30
66
        .bind(id(tx))
31
66
        .bind(id(2))
32
66
        .bind(id(1))
33
66
        .bind(value)
34
66
        .execute(pool)
35
66
        .await?;
36
    }
37
33
    Ok(())
38
33
}
39

            
40
22
async fn put_price(pool: &PgPool, price: u128, legs: [u128; 2]) -> anyhow::Result<()> {
41
22
    sqlx::query(
42
22
        "INSERT INTO prices (id, commodity_id, currency_id, commodity_split_id, \
43
22
         currency_split_id, price_date, value_num, value_denom) \
44
22
         VALUES ($1, $2, $2, $3, $4, now(), 1, 1)",
45
22
    )
46
22
    .bind(id(price))
47
22
    .bind(id(1))
48
22
    .bind(id(legs[0]))
49
22
    .bind(id(legs[1]))
50
22
    .execute(pool)
51
22
    .await?;
52
22
    Ok(())
53
22
}
54

            
55
11
pub(crate) async fn seed(pool: &PgPool) -> anyhow::Result<Rows> {
56
11
    sqlx::query("INSERT INTO commodities (id) VALUES ($1)")
57
11
        .bind(id(1))
58
11
        .execute(pool)
59
11
        .await?;
60
11
    sqlx::query("INSERT INTO accounts (id) VALUES ($1)")
61
11
        .bind(id(2))
62
11
        .execute(pool)
63
11
        .await?;
64
11
    put_transaction(pool, 10, [11, 12]).await?;
65
11
    put_transaction(pool, 20, [21, 22]).await?;
66
11
    put_transaction(pool, 30, [31, 32]).await?;
67
11
    put_price(pool, 100, [11, 21]).await?;
68
11
    put_price(pool, 200, [31, 32]).await?;
69
11
    Ok(Rows {
70
11
        tx_a: id(10),
71
11
        tx_b: id(20),
72
11
        tx_own: id(30),
73
11
        cross_price: id(100),
74
11
        own_price: id(200),
75
11
    })
76
11
}