fix: postgres tag inserts

This commit is contained in:
Kieran 2023-02-07 21:47:28 +00:00 committed by Greg Heartsfield
parent 692925942a
commit 1daa25600d
2 changed files with 8 additions and 8 deletions

View File

@ -66,7 +66,7 @@ impl NostrRepo for PostgresRepo {
// replaceable event or parameterized replaceable event. // replaceable event or parameterized replaceable event.
if e.is_replaceable() { if e.is_replaceable() {
let repl_count = sqlx::query( let repl_count = sqlx::query(
"SELECT e.id FROM event e WHERE e.pub_key=? AND e.kind=? AND e.created_at >= ? LIMIT 1;") "SELECT e.id FROM event e WHERE e.pub_key=$1 AND e.kind=$2 AND e.created_at >= $3 LIMIT 1;")
.bind(&pubkey_blob) .bind(&pubkey_blob)
.bind(e.kind as i64) .bind(e.kind as i64)
.bind(Utc.timestamp_opt(e.created_at as i64, 0).unwrap()) .bind(Utc.timestamp_opt(e.created_at as i64, 0).unwrap())
@ -134,20 +134,20 @@ ON CONFLICT (id) DO NOTHING"#,
let tag_val = &tag[1]; let tag_val = &tag[1];
// only single-char tags are searchable // only single-char tags are searchable
let tag_char_opt = single_char_tagname(tag_name); let tag_char_opt = single_char_tagname(tag_name);
let query = "INSERT INTO tag (event_id, \"name\", value) VALUES($1, $2, $3) \
ON CONFLICT (event_id, \"name\", value) DO NOTHING";
match &tag_char_opt { match &tag_char_opt {
Some(_) => { Some(_) => {
// if tag value is lowercase hex; // if tag value is lowercase hex;
if is_lower_hex(tag_val) && (tag_val.len() % 2 == 0) { if is_lower_hex(tag_val) && (tag_val.len() % 2 == 0) {
sqlx::query(query) sqlx::query("INSERT INTO tag (event_id, \"name\", value, value_hex) VALUES($1, $2, NULL, $3) \
ON CONFLICT (event_id, \"name\", value, value_hex) DO NOTHING")
.bind(&id_blob) .bind(&id_blob)
.bind(tag_name) .bind(tag_name)
.bind(hex::decode(tag_val).ok()) .bind(hex::decode(tag_val).ok())
.execute(&mut tx) .execute(&mut tx)
.await?; .await?;
} else { } else {
sqlx::query(query) sqlx::query("INSERT INTO tag (event_id, \"name\", value, value_hex) VALUES($1, $2, $3, NULL) \
ON CONFLICT (event_id, \"name\", value, value_hex) DO NOTHING")
.bind(&id_blob) .bind(&id_blob)
.bind(tag_name) .bind(tag_name)
.bind(tag_val.as_bytes()) .bind(tag_val.as_bytes())

View File

@ -213,7 +213,7 @@ CREATE INDEX tag_value_hex_idx ON tag USING btree (value_hex);
// insert as BLOB if we can restore it losslessly. // insert as BLOB if we can restore it losslessly.
// this means it needs to be even length and lowercase. // this means it needs to be even length and lowercase.
if (tagval.len() % 2 == 0) && is_lower_hex(tagval) { if (tagval.len() % 2 == 0) && is_lower_hex(tagval) {
let q = "INSERT INTO tag (event_id, \"name\", value_hex) VALUES ($1, $2, $3) ON CONFLICT DO NOTHING;"; let q = "INSERT INTO tag (event_id, \"name\", value, value_hex) VALUES ($1, $2, NULL, $3) ON CONFLICT DO NOTHING;";
sqlx::query(q) sqlx::query(q)
.bind(&event_id) .bind(&event_id)
.bind(tagname) .bind(tagname)
@ -221,7 +221,7 @@ CREATE INDEX tag_value_hex_idx ON tag USING btree (value_hex);
.execute(&mut update_tx) .execute(&mut update_tx)
.await?; .await?;
} else { } else {
let q = "INSERT INTO tag (event_id, \"name\", value) VALUES ($1, $2, $3) ON CONFLICT DO NOTHING;"; let q = "INSERT INTO tag (event_id, \"name\", value, value_hex) VALUES ($1, $2, $3, NULL) ON CONFLICT DO NOTHING;";
sqlx::query(q) sqlx::query(q)
.bind(&event_id) .bind(&event_id)
.bind(tagname) .bind(tagname)
@ -250,7 +250,7 @@ mod m003 {
sql: vec![ sql: vec![
r#" r#"
-- Add unique constraint on tag -- Add unique constraint on tag
ALTER TABLE tag ADD CONSTRAINT unique_constraint_name UNIQUE (event_id, "name", value); ALTER TABLE tag ADD CONSTRAINT unique_constraint_name UNIQUE (event_id, "name", value, value_hex);
"#, "#,
], ],
} }