make queries resilient against duplictate data

This commit is contained in:
Maximilian Schneider 2023-04-16 23:30:58 +02:00
parent d399bde703
commit ed56f1231a
1 changed files with 20 additions and 4 deletions

View File

@ -181,7 +181,7 @@ impl PostgresSession {
query.push('(');
for i in 0..args {
if row == 0 && types.len() > 0 {
if row == 0 && types.len() > 0 {
query.push_str(&format!("(${arg_index})::{}", types[i]));
} else {
query.push_str(&format!("${arg_index}"));
@ -241,6 +241,8 @@ impl PostgresSession {
Self::multiline_query(&mut query, NUMBER_OF_ARGS, txs.len(), &[]);
query.push_str("ON CONFLICT (signature) DO NOTHING");
self.client.execute(&query, &args).await?;
Ok(())
@ -273,7 +275,7 @@ impl PostgresSession {
let mut query = String::from(
r#"
INSERT INTO lite_rpc.Blocks
INSERT INTO lite_rpc.Blocks
(slot, leader_id, parent_slot, cluster_time, local_time)
VALUES
"#,
@ -281,6 +283,16 @@ impl PostgresSession {
Self::multiline_query(&mut query, NUMBER_OF_ARGS, blocks.len(), &[]);
query.push_str(
r#"
ON CONFLICT (slot) DO UPDATE SET
leader_id = EXCLUDED.leader_id
parent_slot = EXCLUDED.parent_slot
cluster_time = EXCLUDED.cluster_time
local_time = EXCLUDED.local_time
"#,
);
self.client.execute(&query, &args).await?;
Ok(())
@ -319,7 +331,12 @@ impl PostgresSession {
"#,
);
Self::multiline_query(&mut query, NUMBER_OF_ARGS, txs.len(), &["text", "bigint", "bigint", "bigint"]);
Self::multiline_query(
&mut query,
NUMBER_OF_ARGS,
txs.len(),
&["text", "bigint", "bigint", "bigint"],
);
query.push_str(
r#"
@ -475,7 +492,6 @@ fn multiline_query_test() {
assert_eq!(query, "($1,$2,$3),($4,$5,$6)");
}
#[test]
fn multiline_query_test_types() {
let mut query = String::new();