use chrono::Utc;
use rustango::core::Column as _;
use rustango::extractors::Tenant;
use rustango::sql::{
sqlx, write_transaction, Auto, ExecError, FetcherPool as _, FetcherTx as _, Pool, PoolTx,
};
use crate::page::{Page, PageStatus};
use crate::page_type::find_handler;
use crate::page_type_model::PageType;
use crate::tree::{depth_of, descendants_like, MaterializedPath, PathError};
#[derive(Debug, thiserror::Error)]
pub enum TreeError {
#[error(transparent)]
Db(#[from] ExecError),
#[error(transparent)]
Sqlx(#[from] sqlx::Error),
#[error(transparent)]
Path(#[from] PathError),
#[error(transparent)]
Query(#[from] rustango::core::QueryError),
#[error("page id was not assigned after insert")]
MissingId,
#[error("page {0} not found")]
NotFound(i64),
#[error("cms_page_type row {0} not found — has `ensure_seeded` run for this tenant?")]
UnknownPageType(i64),
#[error("cannot move a page beneath itself or its own descendant")]
CycleAttempt,
#[error(
"page-type `{child_type}` is not in `{parent_type}.allowed_child_types` — \
parent refuses this child"
)]
DisallowedChildType {
parent_type: String,
child_type: String,
},
#[error(
"page-type `{child_type}.allowed_parent_types` does not include \
`{parent_type}` — child refuses this parent"
)]
DisallowedParentType {
parent_type: String,
child_type: String,
},
}
pub async fn atomic_tree<T, F>(pool: &Pool, f: F) -> Result<T, TreeError>
where
T: Send + 'static,
F: Send
+ 'static
+ for<'tx> FnOnce(
&'tx mut PoolTx<'_>,
) -> std::pin::Pin<
Box<dyn std::future::Future<Output = Result<T, TreeError>> + Send + 'tx>,
>,
{
let stash: std::sync::Arc<std::sync::Mutex<Option<TreeError>>> =
std::sync::Arc::new(std::sync::Mutex::new(None));
let stash_cb = std::sync::Arc::clone(&stash);
let out = rustango::sql::atomic_immediate(pool, move |tx| {
Box::pin(async move {
let mut guard = tx.lock().await?;
match f(&mut guard).await {
Ok(v) => Ok(v),
Err(e) => {
*stash_cb.lock().expect("atomic_tree stash poisoned") = Some(e);
Err(ExecError::EmptyReturning)
}
}
})
})
.await;
match out {
Ok(v) => Ok(v),
Err(commit_err) => {
let domain = stash.lock().expect("atomic_tree stash poisoned").take();
Err(domain.unwrap_or(TreeError::Db(commit_err)))
}
}
}
#[derive(Debug, Clone)]
pub struct NewPage {
pub page_type_id: i64,
pub title: String,
pub slug: String,
pub status: PageStatus,
pub seo_title: String,
pub seo_description: String,
}
impl NewPage {
pub fn new(page_type_id: i64, title: impl Into<String>, slug: impl Into<String>) -> Self {
Self {
page_type_id,
title: title.into(),
slug: slug.into(),
status: PageStatus::Draft,
seo_title: String::new(),
seo_description: String::new(),
}
}
pub fn with_status(mut self, status: PageStatus) -> Self {
self.status = status;
self
}
pub fn with_seo(mut self, title: impl Into<String>, description: impl Into<String>) -> Self {
self.seo_title = title.into();
self.seo_description = description.into();
self
}
fn into_page(self, parent_id: Option<i64>, sort_order: i32) -> Page {
let published_at = matches!(self.status, PageStatus::Published).then(Utc::now);
let last_published_at = published_at;
Page {
id: Auto::Unset,
page_type_id: self.page_type_id,
title: self.title,
slug: self.slug,
path: String::new(),
url_path: String::new(),
preview_path: String::new(),
template_override: String::new(),
depth: 0,
parent_id,
locale_variant_of: None,
alias_of: None,
theme_id: None,
sort_order,
status: self.status.as_str().to_owned(),
published_at,
last_published_at,
go_live_at: None,
expire_at: None,
seo_title: self.seo_title,
seo_description: self.seo_description,
robots_index: true,
sitemap_priority: 0.5,
show_in_menus: false,
og_title: String::new(),
og_description: String::new(),
og_image_media_id: None,
twitter_card: "summary_large_image".to_owned(),
notification_pre_published_sent: false,
created_at: Auto::Unset,
updated_at: Auto::Unset,
}
}
}
pub fn compute_url_path(parent_url_path: Option<&str>, slug: &str) -> String {
match parent_url_path {
None => {
if slug.is_empty() {
"/".to_owned()
} else {
format!("/{slug}")
}
}
Some(parent) => {
if slug.is_empty() {
parent.to_owned()
} else if parent == "/" {
format!("/{slug}")
} else {
format!("{parent}/{slug}")
}
}
}
}
pub async fn cascade_url_path_to_descendants(
tx: &mut PoolTx<'_>,
root_page: &Page,
) -> Result<usize, TreeError> {
let root_id = page_id(root_page)?;
let pattern = descendants_like(&root_page.path);
let descendants: Vec<Page> = Page::objects()
.where_(Page::path.like(pattern))
.where_(Page::path.ne(root_page.path.clone()))
.order_by(&[("path", false)])
.fetch_tx(tx)
.await?;
let mut by_id: std::collections::HashMap<i64, String> = std::collections::HashMap::new();
by_id.insert(root_id, root_page.url_path.clone());
let mut updated = 0usize;
for mut d in descendants {
let Some(parent_id) = d.parent_id else {
continue;
};
let parent_url_path = by_id.get(&parent_id).cloned().unwrap_or_default();
let new_url = compute_url_path(Some(parent_url_path.as_str()), &d.slug);
if d.url_path == new_url {
let did = d.id.get().copied().unwrap_or_default();
by_id.insert(did, new_url);
continue;
}
d.url_path = new_url.clone();
let did = d.id.get().copied().unwrap_or_default();
by_id.insert(did, new_url);
d.save_tx(tx).await?;
updated += 1;
}
Ok(updated)
}
pub async fn rebuild_all_url_paths(pool: &Pool) -> Result<usize, TreeError> {
let mut iter = Page::objects()
.order_by(&[("path", false)])
.iterator(2_000)?;
let mut by_id: std::collections::HashMap<i64, String> = std::collections::HashMap::new();
let mut updated = 0usize;
while let Some(mut p) = iter.next_row(pool).await? {
let id = page_id(&p)?;
let new_url = match p.parent_id {
None => compute_url_path(None, &p.slug),
Some(parent_id) => {
let parent_url = by_id.get(&parent_id).cloned().unwrap_or_default();
compute_url_path(Some(parent_url.as_str()), &p.slug)
}
};
if p.url_path != new_url {
p.url_path = new_url.clone();
p.save(pool).await?;
updated += 1;
}
by_id.insert(id, new_url);
}
Ok(updated)
}
impl Page {
pub async fn create_root(tenant: &Tenant, new: NewPage) -> Result<Self, TreeError> {
Self::create_root_pool(tenant.pool(), new).await
}
pub async fn create_root_pool(pool: &Pool, new: NewPage) -> Result<Self, TreeError> {
let mut tx = write_transaction(pool).await?;
let sort_order = next_root_sort_order_tx(&mut tx).await?;
let mut page = new.into_page(None, sort_order);
page.insert_tx(&mut tx).await?;
let id = page_id(&page)?;
let path = MaterializedPath::root(id)?;
page.path = path.into_string();
page.depth = 1;
page.url_path = compute_url_path(None, &page.slug);
page.save_tx(&mut tx).await?;
tx.commit().await?;
Ok(page)
}
pub async fn create_child(
tenant: &Tenant,
parent: &Page,
new: NewPage,
) -> Result<Self, TreeError> {
Self::create_child_pool(tenant.pool(), parent, new).await
}
pub async fn create_child_pool(
pool: &Pool,
parent: &Page,
new: NewPage,
) -> Result<Self, TreeError> {
let parent_id = page_id(parent)?;
validate_child_placement(pool, parent.page_type_id, new.page_type_id).await?;
let mut tx = write_transaction(pool).await?;
let _locked_parent: Vec<Page> = Page::objects()
.where_(Page::id.eq(parent_id))
.select_for_update()
.silent_on_sqlite()
.fetch_tx(&mut tx)
.await?;
let sort_order = next_child_sort_order_tx(&mut tx, parent_id).await?;
let mut page = new.into_page(Some(parent_id), sort_order);
page.insert_tx(&mut tx).await?;
let id = page_id(&page)?;
let path = MaterializedPath::child_of(&parent.path, id)?;
page.depth = path.depth();
page.path = path.into_string();
page.url_path = compute_url_path(Some(parent.url_path.as_str()), &page.slug);
page.save_tx(&mut tx).await?;
tx.commit().await?;
Ok(page)
}
pub async fn children(&self, pool: &Pool) -> Result<Vec<Page>, TreeError> {
let id = page_id(self)?;
let rows = Page::objects()
.where_(Page::parent_id.eq(id))
.order_by(&[("sort_order", false), ("id", false)])
.fetch(pool)
.await?;
Ok(rows)
}
pub async fn siblings(&self, pool: &Pool) -> Result<Vec<Page>, TreeError> {
let id = page_id(self)?;
let qs = Page::objects().order_by(&[("sort_order", false), ("id", false)]);
let rows = match self.parent_id {
Some(pid) => qs.where_(Page::parent_id.eq(pid)).fetch(pool).await?,
None => qs.where_(Page::parent_id.is_null()).fetch(pool).await?,
};
Ok(rows
.into_iter()
.filter(|p| p.id.get().copied() != Some(id))
.collect())
}
pub async fn ancestors(&self, pool: &Pool) -> Result<Vec<Page>, TreeError> {
let paths = MaterializedPath::ancestors_of(&self.path);
if paths.is_empty() {
return Ok(Vec::new());
}
let mut rows = Page::objects()
.where_(Page::path.is_in(paths.iter().cloned()))
.order_by(&[("path", false)])
.fetch(pool)
.await?;
rows.sort_by(|a, b| a.path.cmp(&b.path));
Ok(rows)
}
pub async fn descendants(&self, pool: &Pool) -> Result<Vec<Page>, TreeError> {
let pattern = descendants_like(&self.path);
let rows = Page::objects()
.where_(Page::path.like(pattern))
.where_(Page::path.ne(self.path.clone()))
.order_by(&[("path", false)])
.fetch(pool)
.await?;
Ok(rows)
}
pub async fn move_to(
&mut self,
tenant: &Tenant,
new_parent: Option<&Page>,
) -> Result<(), TreeError> {
let self_id = page_id(self)?;
if let Some(np) = new_parent {
let np_id = page_id(np)?;
if np_id == self_id || np.path.starts_with(&self.path) {
return Err(TreeError::CycleAttempt);
}
validate_child_placement(tenant.pool(), np.page_type_id, self.page_type_id).await?;
}
let new_id_path = match new_parent {
Some(np) => MaterializedPath::child_of(&np.path, self_id)?.into_string(),
None => MaterializedPath::root(self_id)?.into_string(),
};
if new_id_path == self.path {
return Ok(());
}
let subtree = self.descendants(tenant.pool()).await?;
let mut tx = write_transaction(tenant.pool()).await?;
self.move_to_in_tx(new_parent, subtree, &mut tx).await?;
tx.commit().await?;
Ok(())
}
pub async fn move_to_in_tx(
&mut self,
new_parent: Option<&Page>,
subtree: Vec<Page>,
tx: &mut PoolTx<'_>,
) -> Result<(), TreeError> {
let self_id = page_id(self)?;
let new_id_path = match new_parent {
Some(np) => MaterializedPath::child_of(&np.path, self_id)?.into_string(),
None => MaterializedPath::root(self_id)?.into_string(),
};
if new_id_path == self.path {
return Ok(());
}
let old_prefix = self.path.clone();
let new_sort_order = match new_parent {
Some(np) => {
let np_id = page_id(np)?;
let _locked_parent: Vec<Page> = Page::objects()
.where_(Page::id.eq(np_id))
.select_for_update()
.silent_on_sqlite()
.fetch_tx(tx)
.await?;
next_child_sort_order_tx(tx, np_id).await?
}
None => next_root_sort_order_tx(tx).await?,
};
self.parent_id = new_parent.and_then(|p| p.id.get().copied());
self.sort_order = new_sort_order;
self.depth = depth_of(&new_id_path);
self.path = new_id_path.clone();
self.url_path = compute_url_path(new_parent.map(|p| p.url_path.as_str()), &self.slug);
self.save_tx(tx).await?;
let mut url_of: std::collections::HashMap<i64, String> = std::collections::HashMap::new();
url_of.insert(self_id, self.url_path.clone());
for mut row in subtree {
let suffix = row
.path
.strip_prefix(&old_prefix)
.expect("descendant path must start with old prefix");
row.path = format!("{new_id_path}{suffix}");
row.depth = depth_of(&row.path);
let parent_url = row.parent_id.and_then(|pid| url_of.get(&pid).cloned());
row.url_path = compute_url_path(parent_url.as_deref(), &row.slug);
if let Some(id) = row.id.get().copied() {
url_of.insert(id, row.url_path.clone());
}
row.save_tx(tx).await?;
}
Ok(())
}
}
fn page_id(page: &Page) -> Result<i64, TreeError> {
page.id.get().copied().ok_or(TreeError::MissingId)
}
async fn next_root_sort_order_tx(tx: &mut PoolTx<'_>) -> Result<i32, TreeError> {
let _lock: Vec<Page> = Page::objects()
.where_(Page::parent_id.is_null())
.order_by(&[("id", false)])
.limit(1)
.select_for_update()
.silent_on_sqlite()
.fetch_tx(tx)
.await?;
let rows = Page::objects()
.where_(Page::parent_id.is_null())
.order_by(&[("sort_order", true)])
.fetch_tx(tx)
.await?;
Ok(rows.first().map(|p| p.sort_order + 1).unwrap_or(0))
}
async fn next_child_sort_order_tx(tx: &mut PoolTx<'_>, parent_id: i64) -> Result<i32, TreeError> {
let rows = Page::objects()
.where_(Page::parent_id.eq(parent_id))
.order_by(&[("sort_order", true)])
.fetch_tx(tx)
.await?;
Ok(rows.first().map(|p| p.sort_order + 1).unwrap_or(0))
}
pub async fn validate_child_placement(
pool: &Pool,
parent_type_id: i64,
child_type_id: i64,
) -> Result<(), TreeError> {
let rows: Vec<PageType> = PageType::objects()
.where_(PageType::id.is_in([parent_type_id, child_type_id]))
.fetch(pool)
.await?;
let parent = rows
.iter()
.find(|r| r.id.get().copied() == Some(parent_type_id))
.ok_or(TreeError::UnknownPageType(parent_type_id))?;
let child = rows
.iter()
.find(|r| r.id.get().copied() == Some(child_type_id))
.ok_or(TreeError::UnknownPageType(child_type_id))?;
if find_handler(&parent.type_name).is_some_and(|h| h.is_leaf()) {
return Err(TreeError::DisallowedChildType {
parent_type: parent.type_name.clone(),
child_type: child.type_name.clone(),
});
}
let allowed = parent.allowed_children();
if !allowed.is_empty() && !allowed.iter().any(|t| *t == child.type_name) {
return Err(TreeError::DisallowedChildType {
parent_type: parent.type_name.clone(),
child_type: child.type_name.clone(),
});
}
let allowed = child.allowed_parents();
if !allowed.is_empty() && !allowed.iter().any(|t| *t == parent.type_name) {
return Err(TreeError::DisallowedParentType {
parent_type: parent.type_name.clone(),
child_type: child.type_name.clone(),
});
}
Ok(())
}
impl MaterializedPath {
pub fn ancestors_of(path: &str) -> Vec<String> {
let mut out = Vec::new();
let mut acc = String::new();
for seg in path.split(crate::tree::SEPARATOR).filter(|s| !s.is_empty()) {
acc.push_str(seg);
acc.push(crate::tree::SEPARATOR);
if acc != path {
out.push(acc.clone());
}
}
if let Some(last) = out.last() {
if last == path {
out.pop();
}
}
out
}
}
#[cfg(all(test, feature = "sqlite"))]
mod atomic_tree_tests {
use super::*;
use rustango::sql::Pool;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
async fn mem_pool() -> Pool {
Pool::connect("sqlite::memory:")
.await
.expect("sqlite in-memory pool")
}
#[tokio::test]
async fn commit_returns_value_and_fires_on_commit() {
let pool = mem_pool().await;
let fired = Arc::new(AtomicBool::new(false));
let f = fired.clone();
let out = atomic_tree(&pool, move |_tx| {
Box::pin(async move {
rustango::sql::on_commit(move || f.store(true, Ordering::SeqCst));
Ok::<i32, TreeError>(7)
})
})
.await;
assert_eq!(out.expect("commit"), 7);
assert!(
fired.load(Ordering::SeqCst),
"on_commit must fire on commit"
);
}
#[tokio::test]
async fn tree_error_propagates_unchanged_and_drops_on_commit() {
let pool = mem_pool().await;
let fired = Arc::new(AtomicBool::new(false));
let f = fired.clone();
let out: Result<(), TreeError> = atomic_tree(&pool, move |_tx| {
Box::pin(async move {
rustango::sql::on_commit(move || f.store(true, Ordering::SeqCst));
Err(TreeError::CycleAttempt)
})
})
.await;
assert!(matches!(out, Err(TreeError::CycleAttempt)), "got {out:?}");
assert!(
!fired.load(Ordering::SeqCst),
"on_commit must be dropped on a TreeError rollback"
);
}
}