Skip to content

Commit

Permalink
updating author field to be an array of TEXT
Browse files Browse the repository at this point in the history
  • Loading branch information
Mr-Leshiy committed Jan 13, 2025
1 parent 40d5da4 commit 338f28a
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ impl FullSignedDoc {

/// Returns the document author.
#[allow(dead_code)]
pub(crate) fn author(&self) -> &String {
self.body.author()
pub(crate) fn authors(&self) -> &Vec<String> {
self.body.authors()
}

/// Returns the `SignedDocBody`.
Expand Down Expand Up @@ -92,7 +92,7 @@ impl FullSignedDoc {
},
Err(err) if err.is::<NotFoundError>() => {
EventDB::modify(INSERT_SIGNED_DOCS, &self.postgres_db_fields()).await?;
Ok(false)
Ok(true)
},
Err(err) => Err(err),
}
Expand Down Expand Up @@ -154,7 +154,7 @@ impl FullSignedDoc {
*id,
ver,
row.try_get("type")?,
row.try_get("author")?,
row.try_get("authors")?,
row.try_get("metadata")?,
),
payload: row.try_get("payload")?,
Expand Down
4 changes: 2 additions & 2 deletions catalyst-gateway/bin/src/db/event/signed_docs/query_filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub(crate) enum DocsQueryFilter {
DocId(uuid::Uuid),
/// Select docs with the specific `id` and `ver` field
DocVer(uuid::Uuid, uuid::Uuid),
/// Select docs with the specific `author` field
/// Select docs with the specific `authors` field
Author(String),
}

Expand All @@ -26,7 +26,7 @@ impl Display for DocsQueryFilter {
Self::DocVer(id, ver) => {
write!(f, "signed_docs.id = '{id}' AND signed_docs.ver = '{ver}'")
},
Self::Author(author) => write!(f, "signed_docs.author = '{author}'"),
Self::Author(author) => write!(f, "'{author}' in signed_docs.authors"),
}
}
}
21 changes: 10 additions & 11 deletions catalyst-gateway/bin/src/db/event/signed_docs/signed_doc_body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ pub(crate) struct SignedDocBody {
ver: uuid::Uuid,
/// `signed_doc` table `type` field
doc_type: uuid::Uuid,
/// `signed_doc` table `author` field
author: String,
/// `signed_doc` table `authors` field
authors: Vec<String>,
/// `signed_doc` table `metadata` field
metadata: Option<serde_json::Value>,
}
Expand All @@ -40,9 +40,9 @@ impl SignedDocBody {
&self.ver
}

/// Returns the document author.
pub(crate) fn author(&self) -> &String {
&self.author
/// Returns the document authors.
pub(crate) fn authors(&self) -> &Vec<String> {
&self.authors
}

/// Returns all signed document fields for the event db queries
Expand All @@ -51,22 +51,21 @@ impl SignedDocBody {
&self.id,
&self.ver,
&self.doc_type,
&self.author,
&self.authors,
&self.metadata,
]
}

/// Creates a `SignedDocBody` instance.
#[allow(dead_code)]
pub(crate) fn new(
id: uuid::Uuid, ver: uuid::Uuid, doc_type: uuid::Uuid, author: String,
id: uuid::Uuid, ver: uuid::Uuid, doc_type: uuid::Uuid, authors: Vec<String>,
metadata: Option<serde_json::Value>,
) -> Self {
Self {
id,
ver,
doc_type,
author,
authors,
metadata,
}
}
Expand All @@ -91,13 +90,13 @@ impl SignedDocBody {
let id = row.try_get("id")?;
let ver = row.try_get("ver")?;
let doc_type = row.try_get("type")?;
let author = row.try_get("author")?;
let authors = row.try_get("authors")?;
let metadata = row.try_get("metadata")?;
Ok(Self {
id,
ver,
doc_type,
author,
authors,
metadata,
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ SELECT
signed_docs.id,
signed_docs.ver,
signed_docs.type,
signed_docs.author,
signed_docs.authors,
signed_docs.metadata
FROM signed_docs
WHERE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ INSERT INTO signed_docs
id,
ver,
type,
author,
authors,
metadata,
payload,
raw
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
SELECT
{% if not ver %} signed_docs.ver, {% endif %}
signed_docs.type,
signed_docs.author,
signed_docs.authors,
signed_docs.metadata,
signed_docs.payload,
signed_docs.raw
Expand Down
16 changes: 11 additions & 5 deletions catalyst-gateway/bin/src/db/event/signed_docs/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ async fn queries_test() {
uuid::Uuid::now_v7(),
uuid::Uuid::now_v7(),
doc_type,
"Alex".to_string(),
vec!["Alex".to_string()],
Some(serde_json::Value::Null),
),
Some(serde_json::Value::Null),
Expand All @@ -29,7 +29,7 @@ async fn queries_test() {
uuid::Uuid::now_v7(),
uuid::Uuid::now_v7(),
doc_type,
"Steven".to_string(),
vec!["Steven".to_string()],
Some(serde_json::Value::Null),
),
Some(serde_json::Value::Null),
Expand All @@ -40,7 +40,7 @@ async fn queries_test() {
uuid::Uuid::now_v7(),
uuid::Uuid::now_v7(),
doc_type,
"Sasha".to_string(),
vec!["Sasha".to_string()],
None,
),
None,
Expand All @@ -54,7 +54,13 @@ async fn queries_test() {
assert!(!doc.store().await.unwrap());
// try another doc with the same `id` and `ver` and with different other fields
let another_doc = FullSignedDoc::new(
SignedDocBody::new(*doc.id(), *doc.ver(), doc_type, "Neil".to_string(), None),
SignedDocBody::new(
*doc.id(),
*doc.ver(),
doc_type,
vec!["Neil".to_string()],
None,
),
None,
vec![],
);
Expand Down Expand Up @@ -87,7 +93,7 @@ async fn queries_test() {
assert!(res_docs.try_next().await.unwrap().is_none());

let mut res_docs = SignedDocBody::retrieve(
&DocsQueryFilter::Author(doc.author().clone()),
&DocsQueryFilter::Author(doc.authors().first().unwrap().clone()),
&QueryLimits::ALL,
)
.await
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,8 @@ pub(crate) type AllResponses = WithErrorResponses<Responses>;
pub(crate) async fn endpoint(doc_bytes: Vec<u8>) -> AllResponses {
match CatalystSignedDocument::try_from(doc_bytes.as_slice()) {
Ok(doc) => {
let doc_body = SignedDocBody::new(
doc.doc_id(),
doc.doc_ver(),
doc.doc_type(),
String::new(),
None,
);
let doc_body =
SignedDocBody::new(doc.doc_id(), doc.doc_ver(), doc.doc_type(), vec![], None);

let payload = if doc.doc_content().is_json() {
match serde_json::from_slice(doc.doc_content().bytes()) {
Expand Down
18 changes: 9 additions & 9 deletions catalyst-gateway/event-db/migrations/V2__signed_documents.sql
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ CREATE TABLE IF NOT EXISTS signed_docs (
id UUID NOT NULL, -- UUID v7
ver UUID NOT NULL, -- UUID v7
type UUID NOT NULL, -- UUID v4
author TEXT NOT NULL,
authors TEXT[] NOT NULL,
metadata JSONB NULL,
payload JSONB NULL,
raw BYTEA NOT NULL,
Expand All @@ -32,8 +32,8 @@ COMMENT ON COLUMN signed_docs.ver IS
'The Signed Documents Document Version Number (ULID).';
COMMENT ON COLUMN signed_docs.type IS
'The Signed Document type identifier.';
COMMENT ON COLUMN signed_docs.author IS
'The Primary Author of the Signed Document.';
COMMENT ON COLUMN signed_docs.authors IS
'The Primary Author`s list of the Signed Document.';
COMMENT ON COLUMN signed_docs.metadata IS
'Extra metadata extracted from the Signed Document, and encoded as JSON.';
COMMENT ON COLUMN signed_docs.payload IS
Expand All @@ -45,13 +45,13 @@ CREATE INDEX IF NOT EXISTS idx_signed_docs_type ON signed_docs (type);
COMMENT ON INDEX idx_signed_docs_type IS
'Index to help finding documents by a known type faster.';

CREATE INDEX IF NOT EXISTS idx_signed_docs_author ON signed_docs (author);
COMMENT ON INDEX idx_signed_docs_author IS
'Index to help finding documents by a known author faster.';
CREATE INDEX IF NOT EXISTS idx_signed_docs_authors ON signed_docs (authors);
COMMENT ON INDEX idx_signed_docs_authors IS
'Index to help finding documents by a known authors faster.';

CREATE INDEX IF NOT EXISTS idx_signed_docs_type_author ON signed_docs (type, author);
COMMENT ON INDEX idx_signed_docs_type_author IS
'Index to help finding documents by a known author for a specific document type faster.';
CREATE INDEX IF NOT EXISTS idx_signed_docs_type_authors ON signed_docs (type, authors);
COMMENT ON INDEX idx_signed_docs_type_authors IS
'Index to help finding documents by a known authors for a specific document type faster.';


CREATE INDEX IF NOT EXISTS idx_signed_docs_metadata ON signed_docs USING gin (metadata);
Expand Down

0 comments on commit 338f28a

Please sign in to comment.