Fix syncing when network latency is present (#4)
* WIP * Add debug * Dedupe inserts * Add deterministic ordering * Fix whitespaces * Update insta * Add integration test script * Rename * Add test * Working for non-deletes * omg it mostly works for deletes * Isdeleted fix * remove created dates * update api * Take document id * No max attempt * works * Use string uuids * . * working!!!! (hopefully) * Improve bundling * Add module * lint * . * lint * Fix CI * use toolchain * clean up * Add useSlowFileEvents * Delete fuzz * Fix CI * use docker * fix script * clean up * Clean up * change node version * Build docker image on every commit * fix ci * 1 db per vault * Add scritps folder * Bump versions * Lint * . * Fix tests for real * Style * . * try * Consistent ordering * Fix tests * hmm * . * Clean up diff * Fixes * . * Fix version bump * . * . * .
This commit is contained in:
parent
bcf48c428d
commit
8b8f1d91d9
91 changed files with 2252 additions and 1586 deletions
|
|
@ -42,9 +42,12 @@ impl Config {
|
|||
}
|
||||
|
||||
pub async fn load_from_file(path: &Path) -> Result<Self> {
|
||||
let contents = fs::read_to_string(path)
|
||||
.await
|
||||
.with_context(|| format!("Cannot load configuration from disk from ({path:?})"))?;
|
||||
let contents = fs::read_to_string(path).await.with_context(|| {
|
||||
format!(
|
||||
"Cannot load configuration from disk from {}",
|
||||
path.display()
|
||||
)
|
||||
})?;
|
||||
|
||||
let config = serde_yaml::from_str(&contents).context("Failed to parse configuration")?;
|
||||
|
||||
|
|
|
|||
|
|
@ -1,31 +1,33 @@
|
|||
use std::path::PathBuf;
|
||||
|
||||
use log::debug;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::consts::{DEFAULT_MAX_CONNECTIONS, DEFAULT_SQLITE_URL};
|
||||
use crate::consts::{DEFAULT_DATABASES_DIRECTORY_PATH, DEFAULT_MAX_CONNECTIONS};
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize, Clone)]
|
||||
pub struct DatabaseConfig {
|
||||
#[serde(default = "default_sqlite_url")]
|
||||
pub sqlite_url: String,
|
||||
#[serde(default = "default_databases_directory_path")]
|
||||
pub databases_directory_path: PathBuf,
|
||||
|
||||
#[serde(default = "default_max_connections")]
|
||||
pub max_connections: u32,
|
||||
}
|
||||
|
||||
fn default_sqlite_url() -> String {
|
||||
debug!("Using default sqlite url: {}", DEFAULT_SQLITE_URL);
|
||||
DEFAULT_SQLITE_URL.to_owned()
|
||||
fn default_databases_directory_path() -> PathBuf {
|
||||
debug!("Using default databases directory path: {DEFAULT_DATABASES_DIRECTORY_PATH:?}");
|
||||
PathBuf::from(DEFAULT_DATABASES_DIRECTORY_PATH)
|
||||
}
|
||||
|
||||
fn default_max_connections() -> u32 {
|
||||
debug!("Using default max connections: {}", DEFAULT_MAX_CONNECTIONS);
|
||||
debug!("Using default max connections: {DEFAULT_MAX_CONNECTIONS}");
|
||||
DEFAULT_MAX_CONNECTIONS
|
||||
}
|
||||
|
||||
impl Default for DatabaseConfig {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
sqlite_url: default_sqlite_url(),
|
||||
databases_directory_path: default_databases_directory_path(),
|
||||
max_connections: default_max_connections(),
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,20 +15,17 @@ pub struct ServerConfig {
|
|||
}
|
||||
|
||||
fn default_host() -> String {
|
||||
debug!("Using default server host: {}", DEFAULT_HOST);
|
||||
debug!("Using default server host: {DEFAULT_HOST}");
|
||||
DEFAULT_HOST.to_owned()
|
||||
}
|
||||
|
||||
fn default_port() -> u16 {
|
||||
debug!("Using default server port: {}", DEFAULT_PORT);
|
||||
debug!("Using default server port: {DEFAULT_PORT}");
|
||||
DEFAULT_PORT
|
||||
}
|
||||
|
||||
fn default_max_body_size_mb() -> usize {
|
||||
debug!(
|
||||
"Using default max body size (MB): {}",
|
||||
DEFAULT_MAX_BODY_SIZE_MB
|
||||
);
|
||||
debug!("Using default max body size (MB): {DEFAULT_MAX_BODY_SIZE_MB}");
|
||||
DEFAULT_MAX_BODY_SIZE_MB
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
pub const CONFIG_PATH: &str = "config.yml";
|
||||
pub const DEFAULT_SQLITE_URL: &str = "db.sqlite3";
|
||||
pub const DEFAULT_DATABASES_DIRECTORY_PATH: &str = "databases";
|
||||
pub const DEFAULT_HOST: &str = "127.0.0.1";
|
||||
pub const DEFAULT_PORT: u16 = 3000;
|
||||
pub const DEFAULT_MAX_CONNECTIONS: u32 = 12;
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
use core::{str::FromStr as _, time::Duration};
|
||||
use core::time::Duration;
|
||||
use std::{collections::HashMap, sync::Arc};
|
||||
|
||||
use anyhow::{Context as _, Result};
|
||||
use models::{
|
||||
|
|
@ -7,19 +8,66 @@ use models::{
|
|||
use sqlx::{sqlite::SqliteConnectOptions, types::chrono::Utc};
|
||||
pub mod models;
|
||||
use sqlx::{Pool, Sqlite, sqlite::SqlitePoolOptions};
|
||||
use tokio::sync::Mutex;
|
||||
use uuid::fmt::Hyphenated;
|
||||
|
||||
use crate::config::database_config::DatabaseConfig;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Database {
|
||||
connection_pool: Pool<Sqlite>,
|
||||
config: DatabaseConfig,
|
||||
connection_pools: Arc<Mutex<HashMap<VaultId, Pool<Sqlite>>>>,
|
||||
}
|
||||
|
||||
pub type Transaction<'a> = sqlx::Transaction<'a, Sqlite>;
|
||||
|
||||
impl Database {
|
||||
pub async fn try_new(config: &DatabaseConfig) -> Result<Self> {
|
||||
let connection_options = SqliteConnectOptions::from_str(&config.sqlite_url)?
|
||||
tokio::fs::create_dir_all(&config.databases_directory_path)
|
||||
.await
|
||||
.with_context(|| {
|
||||
format!(
|
||||
"Failed to create databases directory: {}",
|
||||
config.databases_directory_path.to_string_lossy()
|
||||
)
|
||||
})?;
|
||||
|
||||
let mut connection_pools = std::collections::HashMap::new();
|
||||
|
||||
let mut entries = tokio::fs::read_dir(&config.databases_directory_path).await?;
|
||||
while let Some(entry) = entries.next_entry().await? {
|
||||
if !entry.file_name().to_string_lossy().ends_with(".sqlite") {
|
||||
continue;
|
||||
}
|
||||
|
||||
let vault: VaultId = entry
|
||||
.file_name()
|
||||
.to_string_lossy()
|
||||
.trim_end_matches(".sqlite")
|
||||
.to_owned();
|
||||
|
||||
connection_pools.insert(
|
||||
vault.clone(),
|
||||
Self::create_vault_database(config, &vault).await?,
|
||||
);
|
||||
}
|
||||
|
||||
Ok(Self {
|
||||
config: config.clone(),
|
||||
connection_pools: Arc::new(Mutex::new(connection_pools)),
|
||||
})
|
||||
}
|
||||
|
||||
async fn create_vault_database(
|
||||
config: &DatabaseConfig,
|
||||
vault: &VaultId,
|
||||
) -> Result<Pool<Sqlite>> {
|
||||
let file_name = config
|
||||
.databases_directory_path
|
||||
.join(format!("{vault}.sqlite"));
|
||||
|
||||
let connection_options = SqliteConnectOptions::new()
|
||||
.filename(file_name.clone())
|
||||
.create_if_missing(true)
|
||||
.busy_timeout(Duration::from_secs(3600))
|
||||
.journal_mode(sqlx::sqlite::SqliteJournalMode::Wal);
|
||||
|
|
@ -29,18 +77,11 @@ impl Database {
|
|||
.test_before_acquire(true)
|
||||
.connect_with(connection_options)
|
||||
.await
|
||||
.with_context(|| {
|
||||
format!(
|
||||
"Cannot connect to database with url: {}",
|
||||
&config.sqlite_url
|
||||
)
|
||||
})?;
|
||||
.with_context(|| format!("Cannot open database at {}", file_name.display()))?;
|
||||
|
||||
Self::run_migrations(&pool).await?;
|
||||
|
||||
Ok(Self {
|
||||
connection_pool: pool,
|
||||
})
|
||||
Ok(pool)
|
||||
}
|
||||
|
||||
async fn run_migrations(pool: &Pool<Sqlite>) -> Result<()> {
|
||||
|
|
@ -50,17 +91,38 @@ impl Database {
|
|||
.context("Cannot check for pending migrations")
|
||||
}
|
||||
|
||||
async fn get_connection_pool(&mut self, vault: &VaultId) -> Result<Pool<Sqlite>> {
|
||||
let mut pools = self.connection_pools.lock().await;
|
||||
if !pools.contains_key(vault) {
|
||||
let pool = Self::create_vault_database(&self.config, vault).await?;
|
||||
pools.insert(vault.clone(), pool);
|
||||
}
|
||||
|
||||
let pool = pools
|
||||
.get(vault)
|
||||
.expect("Pool was just inserted or already exists");
|
||||
|
||||
Ok(pool.clone())
|
||||
}
|
||||
|
||||
/// Attempting to write from this transaction might result in a
|
||||
/// database locked error. Use this transaction for read-only operations.
|
||||
pub async fn create_readonly_transaction(&self) -> Result<Transaction<'_>> {
|
||||
self.connection_pool
|
||||
pub async fn create_readonly_transaction(
|
||||
&mut self,
|
||||
vault: &VaultId,
|
||||
) -> Result<Transaction<'static>> {
|
||||
self.get_connection_pool(vault)
|
||||
.await?
|
||||
.begin()
|
||||
.await
|
||||
.context("Cannot create transaction")
|
||||
}
|
||||
|
||||
pub async fn create_write_transaction(&self) -> Result<Transaction<'_>> {
|
||||
let mut transaction = self.create_readonly_transaction().await?;
|
||||
pub async fn create_write_transaction(
|
||||
&mut self,
|
||||
vault: &VaultId,
|
||||
) -> Result<Transaction<'static>> {
|
||||
let mut transaction = self.create_readonly_transaction(vault).await?;
|
||||
|
||||
// sqlx doesn't support immediate transactions for sqlite: https://github.com/launchbadge/sqlx/issues/481
|
||||
sqlx::query!("END; BEGIN IMMEDIATE;")
|
||||
|
|
@ -72,7 +134,7 @@ impl Database {
|
|||
|
||||
/// Return the latest state of all documents in the vault
|
||||
pub async fn get_latest_documents(
|
||||
&self,
|
||||
&mut self,
|
||||
vault: &VaultId,
|
||||
transaction: Option<&mut Transaction<'_>>,
|
||||
) -> Result<Vec<DocumentVersionWithoutContent>> {
|
||||
|
|
@ -80,24 +142,22 @@ impl Database {
|
|||
DocumentVersionWithoutContent,
|
||||
r#"
|
||||
select
|
||||
vault_id,
|
||||
vault_update_id,
|
||||
document_id as "document_id: uuid::Uuid",
|
||||
document_id as "document_id: Hyphenated",
|
||||
relative_path,
|
||||
created_date as "created_date: chrono::DateTime<Utc>",
|
||||
updated_date as "updated_date: chrono::DateTime<Utc>",
|
||||
is_deleted
|
||||
from latest_document_versions
|
||||
where vault_id = ?
|
||||
order by vault_update_id desc
|
||||
"#,
|
||||
vault,
|
||||
);
|
||||
|
||||
if let Some(transaction) = transaction {
|
||||
query.fetch_all(&mut **transaction).await
|
||||
} else {
|
||||
query.fetch_all(&self.connection_pool).await
|
||||
query
|
||||
.fetch_all(&self.get_connection_pool(vault).await?)
|
||||
.await
|
||||
}
|
||||
.context("Cannot fetch latest documents")
|
||||
}
|
||||
|
|
@ -105,7 +165,7 @@ impl Database {
|
|||
/// Return the latest state of all documents (including deleted) in the
|
||||
/// vault which have changed since the given update id
|
||||
pub async fn get_latest_documents_since(
|
||||
&self,
|
||||
&mut self,
|
||||
vault: &VaultId,
|
||||
vault_update_id: VaultUpdateId,
|
||||
transaction: Option<&mut Transaction<'_>>,
|
||||
|
|
@ -114,25 +174,24 @@ impl Database {
|
|||
DocumentVersionWithoutContent,
|
||||
r#"
|
||||
select
|
||||
vault_id,
|
||||
vault_update_id,
|
||||
document_id as "document_id: uuid::Uuid",
|
||||
document_id as "document_id: Hyphenated",
|
||||
relative_path,
|
||||
created_date as "created_date: chrono::DateTime<Utc>",
|
||||
updated_date as "updated_date: chrono::DateTime<Utc>",
|
||||
is_deleted
|
||||
from latest_document_versions
|
||||
where vault_id = ? and vault_update_id > ?
|
||||
where vault_update_id > ?
|
||||
order by vault_update_id desc
|
||||
"#,
|
||||
vault,
|
||||
vault_update_id
|
||||
);
|
||||
|
||||
if let Some(transaction) = transaction {
|
||||
query.fetch_all(&mut **transaction).await
|
||||
} else {
|
||||
query.fetch_all(&self.connection_pool).await
|
||||
query
|
||||
.fetch_all(&self.get_connection_pool(vault).await?)
|
||||
.await
|
||||
}
|
||||
.with_context(|| {
|
||||
format!("Cannot fetch latest documents since vault_update_id {vault_update_id}")
|
||||
|
|
@ -140,7 +199,7 @@ impl Database {
|
|||
}
|
||||
|
||||
pub async fn get_max_update_id_in_vault(
|
||||
&self,
|
||||
&mut self,
|
||||
vault: &VaultId,
|
||||
transaction: Option<&mut Transaction<'_>>,
|
||||
) -> Result<i64> {
|
||||
|
|
@ -148,22 +207,22 @@ impl Database {
|
|||
r#"
|
||||
select coalesce(max(vault_update_id), 0) as max_vault_update_id
|
||||
from documents
|
||||
where vault_id = ?
|
||||
"#,
|
||||
vault
|
||||
);
|
||||
|
||||
if let Some(transaction) = transaction {
|
||||
query.fetch_one(&mut **transaction).await
|
||||
} else {
|
||||
query.fetch_one(&self.connection_pool).await
|
||||
query
|
||||
.fetch_one(&self.get_connection_pool(vault).await?)
|
||||
.await
|
||||
}
|
||||
.map(|row| row.max_vault_update_id)
|
||||
.context("Cannot fetch max update id in vault")
|
||||
}
|
||||
|
||||
pub async fn get_latest_document_by_path(
|
||||
&self,
|
||||
&mut self,
|
||||
vault: &VaultId,
|
||||
relative_path: &str,
|
||||
transaction: Option<&mut Transaction<'_>>,
|
||||
|
|
@ -172,68 +231,67 @@ impl Database {
|
|||
StoredDocumentVersion,
|
||||
r#"
|
||||
select
|
||||
vault_id,
|
||||
vault_update_id,
|
||||
document_id as "document_id: uuid::Uuid",
|
||||
document_id as "document_id: Hyphenated",
|
||||
relative_path,
|
||||
created_date as "created_date: chrono::DateTime<Utc>",
|
||||
updated_date as "updated_date: chrono::DateTime<Utc>",
|
||||
content,
|
||||
is_deleted
|
||||
from latest_document_versions
|
||||
where vault_id = ? and relative_path = ?
|
||||
where relative_path = ?
|
||||
order by vault_update_id desc -- `latest_document_versions` only contains a single latest version of each document, however,
|
||||
-- multiple documents can have the same `relative_path`, if they have been deleted. That's
|
||||
-- why we only care about the latest version of the document with the given relative path.
|
||||
limit 1
|
||||
"#,
|
||||
vault,
|
||||
relative_path
|
||||
);
|
||||
|
||||
if let Some(transaction) = transaction {
|
||||
query.fetch_optional(&mut **transaction).await
|
||||
} else {
|
||||
query.fetch_optional(&self.connection_pool).await
|
||||
query
|
||||
.fetch_optional(&self.get_connection_pool(vault).await?)
|
||||
.await
|
||||
}
|
||||
.context("Cannot fetch latest document version")
|
||||
}
|
||||
|
||||
pub async fn get_latest_document(
|
||||
&self,
|
||||
&mut self,
|
||||
vault: &VaultId,
|
||||
document_id: &DocumentId,
|
||||
transaction: Option<&mut Transaction<'_>>,
|
||||
) -> Result<Option<StoredDocumentVersion>> {
|
||||
let document_id = document_id.as_hyphenated();
|
||||
let query = sqlx::query_as!(
|
||||
StoredDocumentVersion,
|
||||
r#"
|
||||
select
|
||||
vault_id,
|
||||
vault_update_id,
|
||||
document_id as "document_id: uuid::Uuid",
|
||||
document_id as "document_id: Hyphenated",
|
||||
relative_path,
|
||||
created_date as "created_date: chrono::DateTime<Utc>",
|
||||
updated_date as "updated_date: chrono::DateTime<Utc>",
|
||||
content,
|
||||
is_deleted
|
||||
from latest_document_versions
|
||||
where vault_id = ? and document_id = ?
|
||||
where document_id = ?
|
||||
"#,
|
||||
vault,
|
||||
document_id
|
||||
);
|
||||
|
||||
if let Some(transaction) = transaction {
|
||||
query.fetch_optional(&mut **transaction).await
|
||||
} else {
|
||||
query.fetch_optional(&self.connection_pool).await
|
||||
query
|
||||
.fetch_optional(&self.get_connection_pool(vault).await?)
|
||||
.await
|
||||
}
|
||||
.context("Cannot fetch latest document version")
|
||||
}
|
||||
|
||||
pub async fn get_document_version(
|
||||
&self,
|
||||
&mut self,
|
||||
vault: &VaultId,
|
||||
vault_update_id: VaultUpdateId,
|
||||
transaction: Option<&mut Transaction<'_>>,
|
||||
|
|
@ -242,52 +300,49 @@ impl Database {
|
|||
StoredDocumentVersion,
|
||||
r#"
|
||||
select
|
||||
vault_id,
|
||||
vault_update_id,
|
||||
document_id as "document_id: uuid::Uuid",
|
||||
document_id as "document_id: Hyphenated",
|
||||
relative_path,
|
||||
created_date as "created_date: chrono::DateTime<Utc>",
|
||||
updated_date as "updated_date: chrono::DateTime<Utc>",
|
||||
content,
|
||||
is_deleted
|
||||
from documents
|
||||
where vault_id = ? and vault_update_id = ?"#,
|
||||
vault,
|
||||
where vault_update_id = ?"#,
|
||||
vault_update_id
|
||||
);
|
||||
|
||||
if let Some(transaction) = transaction {
|
||||
query.fetch_optional(&mut **transaction).await
|
||||
} else {
|
||||
query.fetch_optional(&self.connection_pool).await
|
||||
query
|
||||
.fetch_optional(&self.get_connection_pool(vault).await?)
|
||||
.await
|
||||
}
|
||||
.context("Cannot fetch document version")
|
||||
}
|
||||
|
||||
pub async fn insert_document_version(
|
||||
&self,
|
||||
&mut self,
|
||||
vault: &VaultId,
|
||||
version: &StoredDocumentVersion,
|
||||
transaction: Option<&mut Transaction<'_>>,
|
||||
) -> Result<()> {
|
||||
let document_id = version.document_id.as_hyphenated();
|
||||
let query = sqlx::query!(
|
||||
r#"
|
||||
insert into documents (
|
||||
vault_id,
|
||||
vault_update_id,
|
||||
document_id,
|
||||
relative_path,
|
||||
created_date,
|
||||
updated_date,
|
||||
content,
|
||||
is_deleted
|
||||
)
|
||||
values (?, ?, ?, ?, ?, ?, ?, ?)
|
||||
values (?, ?, ?, ?, ?, ?)
|
||||
"#,
|
||||
version.vault_id,
|
||||
version.vault_update_id,
|
||||
version.document_id,
|
||||
document_id,
|
||||
version.relative_path,
|
||||
version.created_date,
|
||||
version.updated_date,
|
||||
version.content,
|
||||
version.is_deleted
|
||||
|
|
@ -296,7 +351,7 @@ impl Database {
|
|||
if let Some(transaction) = transaction {
|
||||
query.execute(&mut **transaction).await
|
||||
} else {
|
||||
query.execute(&self.connection_pool).await
|
||||
query.execute(&self.get_connection_pool(vault).await?).await
|
||||
}
|
||||
.context("Cannot insert document version")?;
|
||||
|
||||
|
|
|
|||
|
|
@ -1,25 +1,21 @@
|
|||
CREATE TABLE IF NOT EXISTS documents (
|
||||
vault_id TEXT NOT NULL,
|
||||
vault_update_id INTEGER NOT NULL,
|
||||
vault_update_id INTEGER NOT NULL PRIMARY KEY,
|
||||
document_id TEXT NOT NULL,
|
||||
relative_path TEXT 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, vault_update_id)
|
||||
is_deleted BOOLEAN NOT NULL
|
||||
);
|
||||
|
||||
CREATE VIEW IF NOT EXISTS latest_document_versions AS
|
||||
SELECT d.*
|
||||
FROM documents d
|
||||
INNER JOIN (
|
||||
SELECT vault_id, MAX(vault_update_id) AS max_version_id
|
||||
SELECT MAX(vault_update_id) AS max_version_id
|
||||
FROM documents
|
||||
GROUP BY vault_id, document_id
|
||||
GROUP BY document_id
|
||||
) max_versions
|
||||
ON d.vault_id = max_versions.vault_id
|
||||
AND d.vault_update_id = max_versions.max_version_id;
|
||||
ON d.vault_update_id = max_versions.max_version_id;
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_documents_vault_id_relative_path
|
||||
ON documents (vault_id, relative_path);
|
||||
ON documents (relative_path);
|
||||
|
|
|
|||
|
|
@ -9,30 +9,24 @@ pub type DocumentId = uuid::Uuid;
|
|||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct StoredDocumentVersion {
|
||||
pub vault_id: VaultId,
|
||||
pub vault_update_id: VaultUpdateId,
|
||||
pub document_id: DocumentId,
|
||||
pub relative_path: String,
|
||||
pub created_date: DateTime<Utc>,
|
||||
pub updated_date: DateTime<Utc>,
|
||||
pub content: Vec<u8>,
|
||||
pub is_deleted: bool,
|
||||
}
|
||||
|
||||
impl PartialEq<Self> for StoredDocumentVersion {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.vault_id == other.vault_id && self.vault_update_id == other.vault_update_id
|
||||
}
|
||||
fn eq(&self, other: &Self) -> bool { self.vault_update_id == other.vault_update_id }
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, JsonSchema)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct DocumentVersionWithoutContent {
|
||||
pub vault_id: VaultId,
|
||||
pub vault_update_id: VaultUpdateId,
|
||||
pub document_id: DocumentId,
|
||||
pub relative_path: String,
|
||||
pub created_date: DateTime<Utc>,
|
||||
pub updated_date: DateTime<Utc>,
|
||||
pub is_deleted: bool,
|
||||
}
|
||||
|
|
@ -40,11 +34,9 @@ pub struct DocumentVersionWithoutContent {
|
|||
impl From<StoredDocumentVersion> for DocumentVersionWithoutContent {
|
||||
fn from(value: StoredDocumentVersion) -> Self {
|
||||
Self {
|
||||
vault_id: value.vault_id,
|
||||
vault_update_id: value.vault_update_id,
|
||||
document_id: value.document_id,
|
||||
relative_path: value.relative_path,
|
||||
created_date: value.created_date,
|
||||
updated_date: value.updated_date,
|
||||
is_deleted: value.is_deleted,
|
||||
}
|
||||
|
|
@ -54,11 +46,9 @@ impl From<StoredDocumentVersion> for DocumentVersionWithoutContent {
|
|||
#[derive(Debug, Clone, Serialize, JsonSchema)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct DocumentVersion {
|
||||
pub vault_id: VaultId,
|
||||
pub vault_update_id: VaultUpdateId,
|
||||
pub document_id: DocumentId,
|
||||
pub relative_path: String,
|
||||
pub created_date: DateTime<Utc>,
|
||||
pub updated_date: DateTime<Utc>,
|
||||
pub content_base64: String,
|
||||
pub is_deleted: bool,
|
||||
|
|
@ -67,11 +57,9 @@ pub struct DocumentVersion {
|
|||
impl From<StoredDocumentVersion> for DocumentVersion {
|
||||
fn from(value: StoredDocumentVersion) -> Self {
|
||||
Self {
|
||||
vault_id: value.vault_id,
|
||||
vault_update_id: value.vault_update_id,
|
||||
document_id: value.document_id,
|
||||
relative_path: value.relative_path,
|
||||
created_date: value.created_date,
|
||||
updated_date: value.updated_date,
|
||||
content_base64: bytes_to_base64(&value.content),
|
||||
is_deleted: value.is_deleted,
|
||||
|
|
|
|||
|
|
@ -33,12 +33,12 @@ pub enum SyncServerError {
|
|||
impl SyncServerError {
|
||||
pub fn serialize(&self) -> SerializedError {
|
||||
match self {
|
||||
Self::InitError(error) => error.into(),
|
||||
Self::ClientError(error) => error.into(),
|
||||
Self::ServerError(error) => error.into(),
|
||||
Self::NotFound(error) => error.into(),
|
||||
Self::Unauthorized(error) => error.into(),
|
||||
Self::PermissionDeniedError(error) => error.into(),
|
||||
Self::InitError(error)
|
||||
| Self::ClientError(error)
|
||||
| Self::ServerError(error)
|
||||
| Self::NotFound(error)
|
||||
| Self::Unauthorized(error)
|
||||
| Self::PermissionDeniedError(error) => error.into(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -48,9 +48,10 @@ impl IntoResponse for SyncServerError {
|
|||
let body = Json(self.serialize());
|
||||
|
||||
match self {
|
||||
Self::InitError(_) => (StatusCode::INTERNAL_SERVER_ERROR, body).into_response(),
|
||||
Self::InitError(_) | Self::ServerError(_) => {
|
||||
(StatusCode::INTERNAL_SERVER_ERROR, body).into_response()
|
||||
}
|
||||
Self::ClientError(_) => (StatusCode::BAD_REQUEST, body).into_response(),
|
||||
Self::ServerError(_) => (StatusCode::INTERNAL_SERVER_ERROR, body).into_response(),
|
||||
Self::NotFound(_) => (StatusCode::NOT_FOUND, body).into_response(),
|
||||
Self::Unauthorized(_) => (StatusCode::UNAUTHORIZED, body).into_response(),
|
||||
Self::PermissionDeniedError(_) => (StatusCode::FORBIDDEN, body).into_response(),
|
||||
|
|
|
|||
|
|
@ -16,6 +16,7 @@ use axum::{
|
|||
extract::{DefaultBodyLimit, Request},
|
||||
http::{self, HeaderValue, Method},
|
||||
response::IntoResponse,
|
||||
routing::IntoMakeService,
|
||||
};
|
||||
use log::{error, info};
|
||||
use tokio::signal;
|
||||
|
|
@ -30,7 +31,10 @@ use tower_http::{
|
|||
};
|
||||
use tracing::{Level, info_span};
|
||||
|
||||
use crate::errors::{SerializedError, not_found_error};
|
||||
use crate::{
|
||||
config::server_config::ServerConfig,
|
||||
errors::{SerializedError, not_found_error},
|
||||
};
|
||||
mod app_state;
|
||||
mod auth;
|
||||
mod create_document;
|
||||
|
|
@ -52,24 +56,9 @@ pub async fn create_server() -> Result<()> {
|
|||
.await
|
||||
.context("Failed to initialise app state")?;
|
||||
|
||||
let address = format!(
|
||||
"{}:{}",
|
||||
&app_state.config.server.host, &app_state.config.server.port
|
||||
);
|
||||
|
||||
let mut api = OpenApi {
|
||||
info: Info {
|
||||
title: "VaultLink sync server".to_owned(),
|
||||
summary: Some(
|
||||
"Simple API for syncing documents between concurrent clients.".to_owned(),
|
||||
),
|
||||
description: Some(include_str!("../README.md").to_owned()),
|
||||
version: env!("CARGO_PKG_VERSION").to_owned(),
|
||||
..Info::default()
|
||||
},
|
||||
..OpenApi::default()
|
||||
};
|
||||
let server_config = app_state.config.server.clone();
|
||||
|
||||
let mut api = create_open_api();
|
||||
let app = ApiRouter::new()
|
||||
.api_route("/ping", get(ping::ping))
|
||||
.api_route(
|
||||
|
|
@ -140,11 +129,42 @@ pub async fn create_server() -> Result<()> {
|
|||
.allow_methods([Method::GET, Method::POST, Method::PUT, Method::DELETE]),
|
||||
)
|
||||
.with_state(app_state)
|
||||
.finish_api_with(&mut api, api_docs)
|
||||
.finish_api_with(&mut api, add_api_docs_error_example)
|
||||
.layer(Extension(Arc::new(api))) // https://github.com/tamasfe/aide/blob/507f4a8822bc0c13cbda0f589da1e0f4cbcdb812/examples/example-axum/src/main.rs#L39
|
||||
.fallback(handler_404)
|
||||
.into_make_service();
|
||||
|
||||
start_server(app, &server_config).await
|
||||
}
|
||||
|
||||
async fn serve_api(Extension(api): Extension<Arc<OpenApi>>) -> impl IntoResponse { Json(api) }
|
||||
|
||||
fn create_open_api() -> OpenApi {
|
||||
OpenApi {
|
||||
info: Info {
|
||||
title: "VaultLink sync server".to_owned(),
|
||||
summary: Some(
|
||||
"Simple API for syncing documents between concurrent clients.".to_owned(),
|
||||
),
|
||||
description: Some(include_str!("../README.md").to_owned()),
|
||||
version: env!("CARGO_PKG_VERSION").to_owned(),
|
||||
..Info::default()
|
||||
},
|
||||
..OpenApi::default()
|
||||
}
|
||||
}
|
||||
|
||||
fn add_api_docs_error_example(api: TransformOpenApi<'_>) -> TransformOpenApi<'_> {
|
||||
api.default_response_with::<Json<SerializedError>, _>(|res| {
|
||||
res.example(SerializedError {
|
||||
message: "An error has occurred".to_owned(),
|
||||
causes: vec![],
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
async fn start_server(app: IntoMakeService<axum::Router>, config: &ServerConfig) -> Result<()> {
|
||||
let address = format!("{}:{}", config.host, config.port);
|
||||
let listener = tokio::net::TcpListener::bind(address.clone())
|
||||
.await
|
||||
.with_context(|| format!("Failed to bind to address: {address}"))?;
|
||||
|
|
@ -163,17 +183,6 @@ pub async fn create_server() -> Result<()> {
|
|||
.context("Failed to start server")
|
||||
}
|
||||
|
||||
async fn serve_api(Extension(api): Extension<Arc<OpenApi>>) -> impl IntoResponse { Json(api) }
|
||||
|
||||
fn api_docs(api: TransformOpenApi<'_>) -> TransformOpenApi<'_> {
|
||||
api.default_response_with::<Json<SerializedError>, _>(|res| {
|
||||
res.example(SerializedError {
|
||||
message: "An error has occurred".to_owned(),
|
||||
causes: vec![],
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
async fn shutdown_signal() {
|
||||
let ctrl_c = async {
|
||||
signal::ctrl_c()
|
||||
|
|
@ -193,8 +202,8 @@ async fn shutdown_signal() {
|
|||
let terminate = std::future::pending::<()>();
|
||||
|
||||
tokio::select! {
|
||||
_ = ctrl_c => {},
|
||||
_ = terminate => {},
|
||||
() = ctrl_c => {},
|
||||
() = terminate => {},
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -6,7 +6,6 @@ use axum_extra::{
|
|||
headers::{Authorization, authorization::Bearer},
|
||||
};
|
||||
use axum_jsonschema::Json;
|
||||
use chrono::{DateTime, Utc};
|
||||
use schemars::JsonSchema;
|
||||
use serde::Deserialize;
|
||||
use sync_lib::base64_to_bytes;
|
||||
|
|
@ -17,7 +16,7 @@ use super::{
|
|||
requests::{CreateDocumentVersion, CreateDocumentVersionMultipart},
|
||||
};
|
||||
use crate::{
|
||||
database::models::{DocumentVersionWithoutContent, StoredDocumentVersion, VaultId},
|
||||
database::models::{DocumentId, DocumentVersionWithoutContent, StoredDocumentVersion, VaultId},
|
||||
errors::{SyncServerError, client_error, server_error},
|
||||
utils::sanitize_path,
|
||||
};
|
||||
|
|
@ -44,8 +43,8 @@ pub async fn create_document_multipart(
|
|||
auth_header,
|
||||
state,
|
||||
vault_id,
|
||||
request.document_id,
|
||||
request.relative_path,
|
||||
request.created_date,
|
||||
request.content.contents.to_vec(),
|
||||
)
|
||||
.await
|
||||
|
|
@ -69,8 +68,8 @@ pub async fn create_document_json(
|
|||
auth_header,
|
||||
state,
|
||||
vault_id,
|
||||
request.document_id,
|
||||
request.relative_path,
|
||||
request.created_date,
|
||||
content_bytes,
|
||||
)
|
||||
.await
|
||||
|
|
@ -78,20 +77,39 @@ pub async fn create_document_json(
|
|||
|
||||
async fn internal_create_document(
|
||||
auth_header: Authorization<Bearer>,
|
||||
state: AppState,
|
||||
mut state: AppState,
|
||||
vault_id: VaultId,
|
||||
document_id: Option<DocumentId>,
|
||||
relative_path: String,
|
||||
created_date: DateTime<Utc>,
|
||||
content: Vec<u8>,
|
||||
) -> Result<Json<DocumentVersionWithoutContent>, SyncServerError> {
|
||||
auth(&state, auth_header.token())?;
|
||||
|
||||
let mut transaction = state
|
||||
.database
|
||||
.create_write_transaction()
|
||||
.create_write_transaction(&vault_id)
|
||||
.await
|
||||
.map_err(server_error)?;
|
||||
|
||||
let document_id = match document_id {
|
||||
Some(document_id) => {
|
||||
let existing_version = state
|
||||
.database
|
||||
.get_latest_document(&vault_id, &document_id, Some(&mut transaction))
|
||||
.await
|
||||
.map_err(server_error)?;
|
||||
|
||||
if existing_version.is_some() {
|
||||
return Err(client_error(anyhow::anyhow!(
|
||||
"Document with the same ID already exists"
|
||||
)));
|
||||
}
|
||||
|
||||
document_id
|
||||
}
|
||||
None => uuid::Uuid::new_v4(),
|
||||
};
|
||||
|
||||
let last_update_id = state
|
||||
.database
|
||||
.get_max_update_id_in_vault(&vault_id, Some(&mut transaction))
|
||||
|
|
@ -101,19 +119,17 @@ async fn internal_create_document(
|
|||
let sanitized_relative_path = sanitize_path(&relative_path);
|
||||
|
||||
let new_version = StoredDocumentVersion {
|
||||
vault_id,
|
||||
vault_update_id: last_update_id + 1,
|
||||
document_id: uuid::Uuid::new_v4(),
|
||||
document_id,
|
||||
relative_path: sanitized_relative_path,
|
||||
content,
|
||||
created_date,
|
||||
updated_date: chrono::Utc::now(),
|
||||
is_deleted: false,
|
||||
};
|
||||
|
||||
state
|
||||
.database
|
||||
.insert_document_version(&new_version, Some(&mut transaction))
|
||||
.insert_document_version(&vault_id, &new_version, Some(&mut transaction))
|
||||
.await
|
||||
.map_err(server_error)?;
|
||||
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@ use serde::Deserialize;
|
|||
|
||||
use super::{app_state::AppState, auth::auth, requests::DeleteDocumentVersion};
|
||||
use crate::{
|
||||
database::models::{DocumentId, StoredDocumentVersion, VaultId},
|
||||
database::models::{DocumentId, DocumentVersionWithoutContent, StoredDocumentVersion, VaultId},
|
||||
errors::{SyncServerError, server_error},
|
||||
utils::sanitize_path,
|
||||
};
|
||||
|
|
@ -29,14 +29,14 @@ pub async fn delete_document(
|
|||
vault_id,
|
||||
document_id,
|
||||
}): Path<PathParams>,
|
||||
State(state): State<AppState>,
|
||||
State(mut state): State<AppState>,
|
||||
Json(request): Json<DeleteDocumentVersion>,
|
||||
) -> Result<(), SyncServerError> {
|
||||
) -> Result<Json<DocumentVersionWithoutContent>, SyncServerError> {
|
||||
auth(&state, auth_header.token())?;
|
||||
|
||||
let mut transaction = state
|
||||
.database
|
||||
.create_write_transaction()
|
||||
.create_write_transaction(&vault_id)
|
||||
.await
|
||||
.map_err(server_error)?;
|
||||
|
||||
|
|
@ -47,19 +47,17 @@ pub async fn delete_document(
|
|||
.map_err(server_error)?;
|
||||
|
||||
let new_version = StoredDocumentVersion {
|
||||
vault_id,
|
||||
vault_update_id: last_update_id + 1,
|
||||
document_id,
|
||||
relative_path: sanitize_path(&request.relative_path),
|
||||
content: vec![],
|
||||
created_date: request.created_date,
|
||||
updated_date: chrono::Utc::now(),
|
||||
is_deleted: true,
|
||||
};
|
||||
|
||||
state
|
||||
.database
|
||||
.insert_document_version(&new_version, Some(&mut transaction))
|
||||
.insert_document_version(&vault_id, &new_version, Some(&mut transaction))
|
||||
.await
|
||||
.map_err(server_error)?;
|
||||
|
||||
|
|
@ -69,5 +67,5 @@ pub async fn delete_document(
|
|||
.context("Failed to commit successful transaction")
|
||||
.map_err(server_error)?;
|
||||
|
||||
Ok(())
|
||||
Ok(Json(new_version.into()))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -30,7 +30,7 @@ pub async fn fetch_document_version(
|
|||
document_id,
|
||||
vault_update_id,
|
||||
}): Path<PathParams>,
|
||||
State(state): State<AppState>,
|
||||
State(mut state): State<AppState>,
|
||||
) -> Result<Json<DocumentVersion>, SyncServerError> {
|
||||
auth(&state, auth_header.token())?;
|
||||
|
||||
|
|
@ -39,12 +39,14 @@ pub async fn fetch_document_version(
|
|||
.get_document_version(&vault_id, vault_update_id, None)
|
||||
.await
|
||||
.map_err(server_error)?
|
||||
.map(Ok)
|
||||
.unwrap_or_else(|| {
|
||||
Err(not_found_error(anyhow!(
|
||||
"Document with vault update id `{vault_update_id}` not found",
|
||||
)))
|
||||
})?;
|
||||
.map_or_else(
|
||||
|| {
|
||||
Err(not_found_error(anyhow!(
|
||||
"Document with vault update id `{vault_update_id}` not found",
|
||||
)))
|
||||
},
|
||||
Ok,
|
||||
)?;
|
||||
|
||||
if result.document_id != document_id {
|
||||
return Err(not_found_error(anyhow!(
|
||||
|
|
|
|||
|
|
@ -32,7 +32,7 @@ pub async fn fetch_document_version_content(
|
|||
document_id,
|
||||
vault_update_id,
|
||||
}): Path<PathParams>,
|
||||
State(state): State<AppState>,
|
||||
State(mut state): State<AppState>,
|
||||
) -> Result<Bytes, SyncServerError> {
|
||||
auth(&state, auth_header.token())?;
|
||||
|
||||
|
|
@ -41,12 +41,14 @@ pub async fn fetch_document_version_content(
|
|||
.get_document_version(&vault_id, vault_update_id, None)
|
||||
.await
|
||||
.map_err(server_error)?
|
||||
.map(Ok)
|
||||
.unwrap_or_else(|| {
|
||||
Err(not_found_error(anyhow!(
|
||||
"Document with vault update id `{vault_update_id}` not found",
|
||||
)))
|
||||
})?;
|
||||
.map_or_else(
|
||||
|| {
|
||||
Err(not_found_error(anyhow!(
|
||||
"Document with vault update id `{vault_update_id}` not found",
|
||||
)))
|
||||
},
|
||||
Ok,
|
||||
)?;
|
||||
|
||||
if result.document_id != document_id {
|
||||
return Err(not_found_error(anyhow!(
|
||||
|
|
|
|||
|
|
@ -28,7 +28,7 @@ pub async fn fetch_latest_document_version(
|
|||
vault_id,
|
||||
document_id,
|
||||
}): Path<PathParams>,
|
||||
State(state): State<AppState>,
|
||||
State(mut state): State<AppState>,
|
||||
) -> Result<Json<DocumentVersion>, SyncServerError> {
|
||||
auth(&state, auth_header.token())?;
|
||||
|
||||
|
|
@ -37,12 +37,14 @@ pub async fn fetch_latest_document_version(
|
|||
.get_latest_document(&vault_id, &document_id, None)
|
||||
.await
|
||||
.map_err(server_error)?
|
||||
.map(Ok)
|
||||
.unwrap_or_else(|| {
|
||||
Err(not_found_error(anyhow!(
|
||||
"Document with id `{document_id}` not found",
|
||||
)))
|
||||
})?;
|
||||
.map_or_else(
|
||||
|| {
|
||||
Err(not_found_error(anyhow!(
|
||||
"Document with id `{document_id}` not found",
|
||||
)))
|
||||
},
|
||||
Ok,
|
||||
)?;
|
||||
|
||||
Ok(Json(latest_version.into()))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -30,7 +30,7 @@ pub async fn fetch_latest_documents(
|
|||
TypedHeader(auth_header): TypedHeader<Authorization<Bearer>>,
|
||||
Path(PathParams { vault_id }): Path<PathParams>,
|
||||
Query(QueryParams { since_update_id }): Query<QueryParams>,
|
||||
State(state): State<AppState>,
|
||||
State(mut state): State<AppState>,
|
||||
) -> Result<Json<FetchLatestDocumentsResponse>, SyncServerError> {
|
||||
auth(&state, auth_header.token())?;
|
||||
|
||||
|
|
|
|||
|
|
@ -1,24 +1,27 @@
|
|||
use aide_axum_typed_multipart::FieldData;
|
||||
use axum::body::Bytes;
|
||||
use axum_typed_multipart::TryFromMultipart;
|
||||
use chrono::{DateTime, Utc};
|
||||
use schemars::JsonSchema;
|
||||
use serde::{self, Deserialize};
|
||||
|
||||
use crate::database::models::VaultUpdateId;
|
||||
use crate::database::models::{DocumentId, VaultUpdateId};
|
||||
|
||||
#[derive(Debug, Deserialize, JsonSchema)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct CreateDocumentVersion {
|
||||
/// The client can decide the document id (if it wishes to) in order
|
||||
/// to help with syncing. If the client does not provide a document id,
|
||||
/// the server will generate one. If the client provides a document id
|
||||
/// it must not already exist in the database.
|
||||
pub document_id: Option<DocumentId>,
|
||||
pub relative_path: String,
|
||||
pub created_date: DateTime<Utc>,
|
||||
pub content_base64: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, TryFromMultipart, JsonSchema)]
|
||||
pub struct CreateDocumentVersionMultipart {
|
||||
pub document_id: Option<DocumentId>,
|
||||
pub relative_path: String,
|
||||
pub created_date: DateTime<Utc>,
|
||||
#[form_data(limit = "unlimited")]
|
||||
pub content: FieldData<Bytes>,
|
||||
}
|
||||
|
|
@ -28,7 +31,6 @@ pub struct CreateDocumentVersionMultipart {
|
|||
pub struct UpdateDocumentVersion {
|
||||
pub parent_version_id: VaultUpdateId,
|
||||
pub relative_path: String,
|
||||
pub created_date: DateTime<Utc>,
|
||||
pub content_base64: String,
|
||||
}
|
||||
|
||||
|
|
@ -37,7 +39,6 @@ pub struct UpdateDocumentVersion {
|
|||
pub struct UpdateDocumentVersionMultipart {
|
||||
pub parent_version_id: VaultUpdateId,
|
||||
pub relative_path: String,
|
||||
pub created_date: DateTime<Utc>,
|
||||
#[form_data(limit = "unlimited")]
|
||||
pub content: FieldData<Bytes>,
|
||||
}
|
||||
|
|
@ -46,5 +47,4 @@ pub struct UpdateDocumentVersionMultipart {
|
|||
#[serde(rename_all = "camelCase")]
|
||||
pub struct DeleteDocumentVersion {
|
||||
pub relative_path: String,
|
||||
pub created_date: DateTime<Utc>,
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,7 +6,6 @@ use axum_extra::{
|
|||
headers::{Authorization, authorization::Bearer},
|
||||
};
|
||||
use axum_jsonschema::Json;
|
||||
use chrono::{DateTime, Utc};
|
||||
use log::info;
|
||||
use schemars::JsonSchema;
|
||||
use serde::Deserialize;
|
||||
|
|
@ -50,7 +49,6 @@ pub async fn update_document_multipart(
|
|||
document_id,
|
||||
request.parent_version_id,
|
||||
request.relative_path,
|
||||
request.created_date,
|
||||
request.content.contents.to_vec(),
|
||||
)
|
||||
.await
|
||||
|
|
@ -77,21 +75,19 @@ pub async fn update_document_json(
|
|||
document_id,
|
||||
request.parent_version_id,
|
||||
request.relative_path,
|
||||
request.created_date,
|
||||
content_bytes,
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
#[allow(clippy::too_many_arguments, clippy::too_many_lines)]
|
||||
async fn internal_update_document(
|
||||
auth_header: Authorization<Bearer>,
|
||||
state: AppState,
|
||||
mut state: AppState,
|
||||
vault_id: VaultId,
|
||||
document_id: DocumentId,
|
||||
parent_version_id: VaultUpdateId,
|
||||
relative_path: String,
|
||||
created_date: DateTime<Utc>,
|
||||
content: Vec<u8>,
|
||||
) -> Result<Json<DocumentUpdateResponse>, SyncServerError> {
|
||||
auth(&state, auth_header.token())?;
|
||||
|
|
@ -114,7 +110,7 @@ async fn internal_update_document(
|
|||
|
||||
let mut transaction = state
|
||||
.database
|
||||
.create_write_transaction()
|
||||
.create_write_transaction(&vault_id)
|
||||
.await
|
||||
.map_err(server_error)?;
|
||||
|
||||
|
|
@ -138,6 +134,18 @@ async fn internal_update_document(
|
|||
Ok,
|
||||
)?;
|
||||
|
||||
if latest_version.is_deleted {
|
||||
transaction
|
||||
.rollback()
|
||||
.await
|
||||
.context("Failed to roll back transaction")
|
||||
.map_err(server_error)?;
|
||||
|
||||
return Ok(Json(DocumentUpdateResponse::FastForwardUpdate(
|
||||
latest_version.into(),
|
||||
)));
|
||||
}
|
||||
|
||||
let sanitized_relative_path = sanitize_path(&relative_path);
|
||||
|
||||
// Return the latest version if the content and path are the same as the latest
|
||||
|
|
@ -168,7 +176,7 @@ async fn internal_update_document(
|
|||
let new_relative_path = if parent_document.relative_path == latest_version.relative_path
|
||||
&& latest_version.relative_path != sanitized_relative_path
|
||||
{
|
||||
let mut new_relative_path = Default::default();
|
||||
let mut new_relative_path = String::default();
|
||||
for candidate in deduped_file_paths(&sanitized_relative_path) {
|
||||
if state
|
||||
.database
|
||||
|
|
@ -188,19 +196,17 @@ async fn internal_update_document(
|
|||
};
|
||||
|
||||
let new_version = StoredDocumentVersion {
|
||||
vault_id,
|
||||
document_id,
|
||||
vault_update_id: last_update_id + 1,
|
||||
relative_path: new_relative_path,
|
||||
content: merged_content,
|
||||
created_date,
|
||||
updated_date: chrono::Utc::now(),
|
||||
is_deleted: latest_version.is_deleted,
|
||||
is_deleted: false,
|
||||
};
|
||||
|
||||
state
|
||||
.database
|
||||
.insert_document_version(&new_version, Some(&mut transaction))
|
||||
.insert_document_version(&vault_id, &new_version, Some(&mut transaction))
|
||||
.await
|
||||
.map_err(server_error)?;
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue