Go from per document to per vault version ids to allow "since" queries

This commit is contained in:
Andras Schmelczer 2024-12-14 17:02:44 +00:00
parent fe8d236948
commit 70471f5142
No known key found for this signature in database
GPG key ID: FC8F2C3D3D1A718C
6 changed files with 127 additions and 53 deletions

View file

@ -1,25 +1,24 @@
CREATE TABLE IF NOT EXISTS documents (
vault_id TEXT NOT NULL,
vault_update_id INTEGER NOT NULL,
relative_path TEXT NOT NULL,
version_id INTEGER NOT NULL,
created_date TIMESTAMP NOT NULL,
updated_date TIMESTAMP NOT NULL,
content BLOB NOT NULL,
is_deleted BOOLEAN NOT NULL,
PRIMARY KEY (vault_id, relative_path, version_id)
PRIMARY KEY (vault_id, vault_update_id)
);
CREATE VIEW IF NOT EXISTS latest_document_versions AS
SELECT d.*
FROM documents d
INNER JOIN (
SELECT vault_id, relative_path, MAX(version_id) AS max_version_id
SELECT vault_id, MAX(vault_update_id) AS max_version_id
FROM documents
GROUP BY vault_id, relative_path
) max_versions
ON d.vault_id = max_versions.vault_id
AND d.relative_path = max_versions.relative_path
AND d.version_id = max_versions.max_version_id;
AND d.vault_update_id = max_versions.max_version_id;
CREATE INDEX IF NOT EXISTS idx_documents_vault_doc
ON documents (vault_id, relative_path);

View file

@ -4,13 +4,13 @@ use serde::Serialize;
use sync_lib::bytes_to_base64;
pub type VaultId = String;
pub type DocumentVersionId = i64;
pub type VaultUpdateId = i64;
#[derive(Debug, Clone)]
pub struct StoredDocumentVersion {
pub vault_id: VaultId,
pub vault_update_id: VaultUpdateId,
pub relative_path: String,
pub version_id: DocumentVersionId,
pub created_date: DateTime<Utc>,
pub updated_date: DateTime<Utc>,
pub content: Vec<u8>,
@ -21,8 +21,8 @@ pub struct StoredDocumentVersion {
#[serde(rename_all = "camelCase")]
pub struct DocumentVersionWithoutContent {
pub vault_id: VaultId,
pub vault_update_id: VaultUpdateId,
pub relative_path: String,
pub version_id: DocumentVersionId,
pub created_date: DateTime<Utc>,
pub updated_date: DateTime<Utc>,
pub is_deleted: bool,
@ -32,8 +32,8 @@ impl From<StoredDocumentVersion> for DocumentVersionWithoutContent {
fn from(value: StoredDocumentVersion) -> Self {
Self {
vault_id: value.vault_id,
vault_update_id: value.vault_update_id,
relative_path: value.relative_path,
version_id: value.version_id,
created_date: value.created_date,
updated_date: value.updated_date,
is_deleted: value.is_deleted,
@ -41,19 +41,12 @@ impl From<StoredDocumentVersion> for DocumentVersionWithoutContent {
}
}
#[derive(Debug, Clone, Serialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct PingResponse {
pub server_version: String,
pub is_authenticated: bool,
}
#[derive(Debug, Clone, Serialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct DocumentVersion {
pub vault_id: VaultId,
pub vault_update_id: VaultUpdateId,
pub relative_path: String,
pub version_id: DocumentVersionId,
pub created_date: DateTime<Utc>,
pub updated_date: DateTime<Utc>,
pub content_base64: String,
@ -64,8 +57,8 @@ impl From<StoredDocumentVersion> for DocumentVersion {
fn from(value: StoredDocumentVersion) -> Self {
Self {
vault_id: value.vault_id,
vault_update_id: value.vault_update_id,
relative_path: value.relative_path,
version_id: value.version_id,
created_date: value.created_date,
updated_date: value.updated_date,
content_base64: bytes_to_base64(&value.content),