fix(import): never clobber native content on re-import

Track imported pages/blocks, stable fallback ids, per-document savepoints,
rename-safe page lookup, append on first import into existing pages.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
dasemuandClaude Sonnet 5.5 committed 2026-10-21 14:48:03 +02:00
1 parent 3266ddda3a
commit 5fdc4bb48a
5 files changed
+500 -67

No files matched your search

+208 -59
View File
@@ -9,6 +9,7 @@ mod config;
mod names;
mod outline;
mod rewrite;
mod track;
use chrono::{Local, NaiveDate, TimeZone};
use config::Config;
@@ -25,6 +26,7 @@ use uuid::Uuid;
pub use config::DateFormat;
/// Namespace for deterministic block ids of blocks without a Logseq `id::`.
/// Namespace of fallback ids (kept for id stability across versions).
const BLOCK_NS: Uuid = Uuid::from_u128(0x2b6d_41c9_0e87_4f3a_9d15_c7a8_e340_5b12);
const CHUNK: usize = 256;
@@ -184,6 +186,7 @@ pub fn import_logseq(
// One transaction for the whole write phase: much faster, and a failed import leaves the
// graph untouched.
track::init(graph)?;
graph.transaction(|g| write_all(g, opts, &docs, &origin, &mut report, &mut progress))?;
Ok(report)
}
@@ -213,7 +216,8 @@ fn write_all(
total: order.len(),
});
let d = &docs[i];
match ensure_page(graph, d, origin.get(&d.key).copied()) {
let o = origin.get(&d.key).copied();
match graph.transaction(|g| ensure_page(g, d, o)) {
Ok(r) => {
ids.insert(i, r);
}
@@ -237,7 +241,8 @@ fn write_all(
continue;
};
let d = &docs[i];
match write_doc(graph, d, pid, existed, opts, report) {
// A failing document rolls back as a whole and becomes a warning.
match graph.transaction(|g| write_doc(g, d, pid, existed, opts, report)) {
Ok(()) => {}
Err(e) => report.warnings.push(format!("{}: {e}", d.rel)),
}
@@ -452,13 +457,19 @@ fn dedupe(mut docs: Vec<Doc>, warnings: &mut Vec<String>) -> Vec<Doc> {
out
}
/// Finds or creates the page row. Returns `(id, already existed)`.
/// Finds or creates the page row. Returns `(id, already existed)`. A page previously imported
/// from the same file is found by source path, so renames made in Synapse are respected.
fn ensure_page(g: &Graph, d: &Doc, origin: Option<NaiveDate>) -> Result<(PageId, bool)> {
if let Some(date) = d.journal {
let existed = g.get_page(synapse_core::store::journal_id(date))?.is_some();
let p = g.journal(date)?;
return Ok((p.id, existed));
}
if let Some(id) = track::page_of(g, &d.rel)?
&& g.get_page(id)?.is_some()
{
return Ok((id, true));
}
if let Some(p) = g.page_by_title(&d.title)? {
if title_key(&p.title) != d.key {
return Err(synapse_core::Error::Conflict(format!(
@@ -487,94 +498,232 @@ fn write_doc(
report: &mut Report,
) -> Result<()> {
let existing = if existed { g.page_tree(pid)? } else { vec![] };
let tracked = track::blocks_of(g, pid)?;
let first_import = tracked.is_empty();
if existed && opts.existing == ExistingPages::Skip && !existing.is_empty() {
report.unchanged += 1;
return Ok(());
}
let old: HashMap<Uuid, &Block> = existing.iter().map(|(_, b)| (b.id, b)).collect();
// Final ids: Logseq `id::` when free, else deterministic.
// Final ids: Logseq `id::` when free; otherwise derived from (source file, parent id,
// content, duplicate index) so edits elsewhere in the file never shift them.
let mut ids: Vec<Uuid> = Vec::with_capacity(d.blocks.len());
let mut used: HashSet<Uuid> = HashSet::new();
for (i, b) in d.blocks.iter().enumerate() {
let fallback = || Uuid::new_v5(&BLOCK_NS, format!("{}\0{i}", d.key).as_bytes());
let mut dups: HashMap<(Option<Uuid>, String), usize> = HashMap::new();
for b in &d.blocks {
let parent = b.parent.map(|p| ids[p]);
let fallback = |dups: &mut HashMap<(Option<Uuid>, String), usize>| {
let h = track::id_hash(&b.content, &b.properties);
let n = dups.entry((parent, h.clone())).or_default();
let name = format!("{}\0{parent:?}\0{h}\0{n}", d.rel);
*n += 1;
Uuid::new_v5(&BLOCK_NS, name.as_bytes())
};
let id = match b.explicit_id {
Some(id) => {
let taken_elsewhere =
!used.contains(&id) && g.get_block(id)?.is_some_and(|x| x.page_id != pid);
if used.contains(&id) || taken_elsewhere {
let elsewhere = !old.contains_key(&id)
&& !used.contains(&id)
&& g.get_block(id)?.is_some_and(|x| x.page_id != pid);
if used.contains(&id) || (elsewhere && !tracked.contains_key(&id)) {
report.warnings.push(format!(
"{}: duplicate block id {id}, generated a new one",
d.rel
));
fallback()
fallback(&mut dups)
} else {
id
}
}
None => fallback(),
None => fallback(&mut dups),
};
used.insert(id);
ids.push(id);
}
let blocks: Vec<Block> = d
.blocks
.iter()
.enumerate()
.map(|(i, b)| Block {
// New root blocks of a page that already has content are appended after it.
let mut roots_after: Option<String> = None;
if first_import && !existing.is_empty() {
roots_after = existing
.iter()
.filter(|(_, b)| b.parent_id.is_none())
.map(|(_, b)| b.position.clone())
.max();
}
let mut written = 0usize;
let mut kept: Vec<String> = Vec::new();
for (i, b) in d.blocks.iter().enumerate() {
let mut position = b.position.clone();
if b.parent.is_none() && roots_after.is_some() {
position = synapse_core::position::between(roots_after.as_deref(), None);
roots_after = Some(position.clone());
}
let block = Block {
id: ids[i],
page_id: pid,
parent_id: b.parent.map(|p| ids[p]),
position: b.position.clone(),
position,
content: b.content.clone(),
properties: b.properties.clone(),
collapsed: b.collapsed,
created_at: d.created,
updated_at: d.updated,
})
.collect();
let page = g.get_page(pid)?;
let props_differ = page.as_ref().is_some_and(|p| p.properties != d.props);
let old: HashMap<Uuid, &Block> = existing.iter().map(|(_, b)| (b.id, b)).collect();
let same_blocks = old.len() == blocks.len()
&& blocks.iter().all(|b| {
old.get(&b.id).is_some_and(|o| {
(
o.parent_id,
&o.position,
&o.content,
&o.properties,
o.collapsed,
) == (
b.parent_id,
&b.position,
&b.content,
&b.properties,
b.collapsed,
)
})
});
if existed && same_blocks && !props_differ {
report.unchanged += 1;
} else {
for b in &blocks {
g.put_block(b)?;
}
let stale: Vec<Uuid> = old
.keys()
.filter(|id| !used.contains(id))
.copied()
.collect();
for id in stale {
match g.delete_block(id) {
Ok(()) | Err(synapse_core::Error::NotFound(_)) => {}
Err(e) => return Err(e),
};
let new_hash = track::state_hash(&block);
match (old.get(&block.id), tracked.get(&block.id)) {
(None, None) => {
// Only explicit ids can collide with blocks living elsewhere.
if b.explicit_id.is_some()
&& let Some(other) = g.get_block(block.id)?
&& other.page_id != pid
{
continue; // moved away from this page in Synapse
}
g.put_block(&block)?;
track::set_block(g, &block, &d.rel, &new_hash)?;
written += 1;
}
(None, Some(h)) => {
// Deleted in Synapse: only resurrect when the file changed it since.
if *h != new_hash && g.get_block(block.id)?.is_none() {
kept.push(format!("{}: block deleted in Synapse kept deleted", d.rel));
}
}
(Some(cur), Some(h)) => {
let cur_hash = track::state_hash(cur);
if cur_hash == new_hash {
if *h != new_hash {
track::set_block(g, &block, &d.rel, &new_hash)?;
}
} else if cur_hash == *h {
// untouched in Synapse: file wins
g.put_block(&block)?;
track::set_block(g, &block, &d.rel, &new_hash)?;
written += 1;
} else {
if *h != new_hash {
kept.push(format!(
"{}: block {} edited in both Synapse and the file, kept the Synapse version",
d.rel, block.id
));
}
track::set_block(g, cur, &d.rel, &cur_hash)?;
}
}
(Some(cur), None) => {
// A Synapse block with the same (explicit) id that was never imported.
if track::state_hash(cur) != new_hash {
kept.push(format!(
"{}: block {} already exists with other content, kept it",
d.rel, block.id
));
}
track::set_block(g, cur, &d.rel, &track::state_hash(cur))?;
}
}
if props_differ {
g.set_page_properties(pid, d.props.clone())?;
}
report.warnings.extend(kept);
// Imported blocks that vanished from the file: delete only those untouched in Synapse
// and without native descendants.
let mut deleted = 0usize;
let stale: Vec<(&Uuid, &String)> = tracked
.iter()
.filter(|(id, _)| !used.contains(id))
.collect();
if !stale.is_empty() {
let tree = g.page_tree(pid)?;
let mut kids: HashMap<Uuid, Vec<&Block>> = HashMap::new();
let mut byid: HashMap<Uuid, &Block> = HashMap::new();
for (_, b) in &tree {
byid.insert(b.id, b);
if let Some(p) = b.parent_id {
kids.entry(p).or_default().push(b);
}
}
report.blocks += blocks.len();
let stale_ids: HashSet<Uuid> = stale.iter().map(|(id, _)| **id).collect();
for (id, hash) in &stale {
let Some(b) = byid.get(id) else {
track::drop_block(g, **id)?;
continue;
};
let mut subtree = vec![*b];
let mut i = 0;
while i < subtree.len() {
if let Some(k) = kids.get(&subtree[i].id) {
subtree.extend(k.iter().copied());
}
i += 1;
}
let clean = subtree.iter().all(|x| {
stale_ids.contains(&x.id)
&& tracked
.get(&x.id)
.is_some_and(|h| *h == track::state_hash(x))
});
if track::state_hash(b) == **hash && clean {
g.delete_block(b.id)?;
deleted += 1;
for x in subtree {
track::drop_block(g, x.id)?;
}
} else {
report.warnings.push(format!(
"{}: block {} was removed from the file but edited or extended in Synapse, kept",
d.rel, b.id
));
track::drop_block(g, b.id)?;
}
}
}
// Page properties: merge into native pages, follow the file while unedited.
let page = g
.get_page(pid)?
.ok_or_else(|| synapse_core::Error::NotFound(pid.to_string()))?;
let stored = track::page_props_hash(g, &d.rel)?;
let cur_hash = track::props_hash(&page.properties);
let new_hash = track::props_hash(&d.props);
let mut props_changed = false;
if (existed || d.journal.is_some()) && cur_hash != new_hash {
let mut next = page.properties.clone();
match &stored {
Some(h) if *h == cur_hash => next = d.props.clone(),
Some(_) => {
if Some(&new_hash) != stored.as_ref() {
report.warnings.push(format!(
"{}: page properties edited in Synapse, kept",
d.rel
));
}
}
None => {
for (k, v) in &d.props {
next.entry(k.clone()).or_insert_with(|| v.clone());
}
}
}
if next != page.properties {
let mut np = page.clone();
np.properties = next;
if d.journal.is_none() {
np.updated_at = d.updated;
}
g.put_page(&np)?;
props_changed = true;
}
}
let final_props = g.get_page(pid)?.map(|p| p.properties).unwrap_or_default();
let final_hash = track::props_hash(&final_props);
if stored.as_deref() != Some(final_hash.as_str()) {
track::set_page(g, &d.rel, pid, &final_hash)?;
}
if existed && written == 0 && deleted == 0 && !props_changed {
report.unchanged += 1;
} else {
report.blocks += written;
if d.journal.is_some() {
report.journals += 1;
} else {
+116
View File
@@ -0,0 +1,116 @@
//! Bookkeeping of what the importer wrote, so re-imports never clobber native Synapse content.
//! The tables are local to the graph database and not part of the op log.
use serde_json::Value;
use std::collections::HashMap;
use synapse_core::{Block, BlockId, Graph, PageId, Properties, Result};
use uuid::Uuid;
pub fn init(g: &Graph) -> Result<()> {
g.conn().execute_batch(
"CREATE TABLE IF NOT EXISTS import_pages(
source_path TEXT PRIMARY KEY, page_id TEXT NOT NULL, props_hash TEXT NOT NULL);
CREATE TABLE IF NOT EXISTS import_blocks(
block_id TEXT PRIMARY KEY, page_id TEXT NOT NULL, source_path TEXT NOT NULL,
content_hash TEXT NOT NULL);
CREATE INDEX IF NOT EXISTS import_blocks_page ON import_blocks(page_id);",
)?;
Ok(())
}
fn fnv(parts: &[&[u8]]) -> String {
let mut h: u64 = 0xcbf2_9ce4_8422_2325;
for p in parts {
for b in p.iter().chain(&[0u8]) {
h ^= u64::from(*b);
h = h.wrapping_mul(0x0100_0000_01b3);
}
}
format!("{h:016x}")
}
fn json(props: &Properties) -> String {
serde_json::to_string(&Value::Object(props.clone())).unwrap_or_default()
}
/// Hash used for deriving fallback ids (ignores `collapsed`).
pub fn id_hash(content: &str, props: &Properties) -> String {
fnv(&[content.as_bytes(), json(props).as_bytes()])
}
/// Hash of the user-editable state of a block.
pub fn state_hash(b: &Block) -> String {
fnv(&[
b.content.as_bytes(),
json(&b.properties).as_bytes(),
&[u8::from(b.collapsed)],
])
}
pub fn props_hash(p: &Properties) -> String {
fnv(&[json(p).as_bytes()])
}
pub fn page_of(g: &Graph, source: &str) -> Result<Option<PageId>> {
let mut st = g
.conn()
.prepare_cached("SELECT page_id FROM import_pages WHERE source_path=?1")?;
let rows: Vec<String> = st
.query_map([source], |r| r.get(0))?
.collect::<std::result::Result<_, _>>()?;
Ok(rows.first().and_then(|s| Uuid::parse_str(s).ok()))
}
pub fn page_props_hash(g: &Graph, source: &str) -> Result<Option<String>> {
let mut st = g
.conn()
.prepare_cached("SELECT props_hash FROM import_pages WHERE source_path=?1")?;
let rows: Vec<String> = st
.query_map([source], |r| r.get(0))?
.collect::<std::result::Result<_, _>>()?;
Ok(rows.into_iter().next())
}
pub fn set_page(g: &Graph, source: &str, page: PageId, props_hash: &str) -> Result<()> {
g.conn().execute(
"INSERT OR REPLACE INTO import_pages(source_path, page_id, props_hash) VALUES (?1, ?2, ?3)",
[source, &page.to_string(), props_hash],
)?;
Ok(())
}
/// `block id → state hash at import` for the blocks this importer wrote on `page`.
pub fn blocks_of(g: &Graph, page: PageId) -> Result<HashMap<BlockId, String>> {
let mut st = g
.conn()
.prepare_cached("SELECT block_id, content_hash FROM import_blocks WHERE page_id=?1")?;
let rows = st.query_map([page.to_string()], |r| {
Ok((r.get::<_, String>(0)?, r.get::<_, String>(1)?))
})?;
let mut out = HashMap::new();
for r in rows {
let (id, h) = r?;
if let Ok(id) = Uuid::parse_str(&id) {
out.insert(id, h);
}
}
Ok(out)
}
pub fn set_block(g: &Graph, b: &Block, source: &str, hash: &str) -> Result<()> {
g.conn()
.prepare_cached(
"INSERT OR REPLACE INTO import_blocks(block_id, page_id, source_path, content_hash)
VALUES (?1, ?2, ?3, ?4)",
)?
.execute([&b.id.to_string(), &b.page_id.to_string(), source, hash])?;
Ok(())
}
pub fn drop_block(g: &Graph, id: BlockId) -> Result<()> {
g.conn().execute(
"DELETE FROM import_blocks WHERE block_id=?1",
[id.to_string()],
)?;
Ok(())
}