From 1daa25600dc636d3967d039fb8fd89fe52c3becf Mon Sep 17 00:00:00 2001 From: Kieran Date: Tue, 7 Feb 2023 21:47:28 +0000 Subject: [PATCH] fix: postgres tag inserts --- src/repo/postgres.rs | 10 +++++----- src/repo/postgres_migration.rs | 6 +++--- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/repo/postgres.rs b/src/repo/postgres.rs index 457060b..251a188 100644 --- a/src/repo/postgres.rs +++ b/src/repo/postgres.rs @@ -66,7 +66,7 @@ impl NostrRepo for PostgresRepo { // replaceable event or parameterized replaceable event. if e.is_replaceable() { 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(e.kind as i64) .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]; // only single-char tags are searchable 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 { Some(_) => { // if tag value is lowercase hex; 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(tag_name) .bind(hex::decode(tag_val).ok()) .execute(&mut tx) .await?; } 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(tag_name) .bind(tag_val.as_bytes()) diff --git a/src/repo/postgres_migration.rs b/src/repo/postgres_migration.rs index c27ece8..9347054 100644 --- a/src/repo/postgres_migration.rs +++ b/src/repo/postgres_migration.rs @@ -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. // this means it needs to be even length and lowercase. 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) .bind(&event_id) .bind(tagname) @@ -221,7 +221,7 @@ CREATE INDEX tag_value_hex_idx ON tag USING btree (value_hex); .execute(&mut update_tx) .await?; } 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) .bind(&event_id) .bind(tagname) @@ -250,7 +250,7 @@ mod m003 { sql: vec![ r#" -- 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); "#, ], }