use chrono::{DateTime, Utc};
use rustango::sql::Auto;
use rustango::Model;
use serde::{Deserialize, Serialize};
use serde_json::json;
#[derive(Model, Debug, Clone, Serialize, Deserialize)]
#[rustango(
table = "cms_redirect",
app = "cms",
display = "from_path",
admin(
list_display = "from_path, to_path, is_permanent, hit_count, updated_at",
search_fields = "from_path, to_path",
ordering = "from_path",
list_filter = "is_permanent",
)
)]
pub struct Redirect {
#[rustango(primary_key)]
pub id: Auto<i64>,
#[rustango(max_length = 510, index, unique)]
pub from_path: String,
#[rustango(max_length = 1024)]
pub to_path: String,
#[rustango(default = "true")]
pub is_permanent: bool,
#[rustango(max_length = 255, default = "''")]
pub note: String,
#[rustango(default = "0")]
pub hit_count: i64,
#[rustango(default = "false")]
pub overrides_live: bool,
pub from_page_id: Option<i64>,
pub to_page_id: Option<i64>,
#[rustango(default = "true")]
pub is_active: bool,
#[rustango(max_length = 255, default = "''")]
pub disabled_reason: String,
pub last_hit_at: Option<DateTime<Utc>>,
#[rustango(auto_now_add)]
pub created_at: Auto<DateTime<Utc>>,
#[rustango(auto_now)]
pub updated_at: Auto<DateTime<Utc>>,
}
pub async fn ensure_columns(pool: &rustango::sql::Pool) -> Result<(), rustango::sql::ExecError> {
let last_hit_at = match pool.dialect().name() {
"postgres" => "ALTER TABLE cms_redirect ADD COLUMN last_hit_at TIMESTAMPTZ",
"mysql" => "ALTER TABLE cms_redirect ADD COLUMN last_hit_at DATETIME(6) NULL",
_ => "ALTER TABLE cms_redirect ADD COLUMN last_hit_at TIMESTAMP",
};
let cols: &[&str] = &[
"ALTER TABLE cms_redirect ADD COLUMN overrides_live BOOLEAN NOT NULL DEFAULT FALSE",
"ALTER TABLE cms_redirect ADD COLUMN from_page_id BIGINT",
"ALTER TABLE cms_redirect ADD COLUMN to_page_id BIGINT",
"ALTER TABLE cms_redirect ADD COLUMN is_active BOOLEAN NOT NULL DEFAULT TRUE",
"ALTER TABLE cms_redirect ADD COLUMN disabled_reason VARCHAR(255) NOT NULL DEFAULT ''",
last_hit_at,
];
for stmt in cols {
if let Err(e) = rustango::sql::raw_execute_pool(pool, stmt, Vec::new()).await {
let msg = e.to_string().to_ascii_lowercase();
if msg.contains("duplicate column") || msg.contains("already exists") {
continue;
}
return Err(e);
}
}
repair_pg_last_hit_at(pool).await
}
async fn repair_pg_last_hit_at(pool: &rustango::sql::Pool) -> Result<(), rustango::sql::ExecError> {
#[cfg(feature = "postgres")]
if let Some(pg) = pool.as_postgres() {
let data_type: Option<String> = rustango::sql::sqlx::query_scalar(
"SELECT data_type FROM information_schema.columns \
WHERE table_schema = current_schema() AND table_name = 'cms_redirect' \
AND column_name = 'last_hit_at'",
)
.fetch_optional(pg)
.await
.map_err(rustango::sql::ExecError::from)?;
if data_type.as_deref() == Some("timestamp without time zone") {
rustango::sql::raw_execute_pool(
pool,
"ALTER TABLE cms_redirect ALTER COLUMN last_hit_at TYPE TIMESTAMPTZ \
USING last_hit_at AT TIME ZONE 'UTC'",
Vec::new(),
)
.await?;
}
}
#[cfg(not(feature = "postgres"))]
let _ = pool;
Ok(())
}
pub async fn find_for_path(
pool: &rustango::sql::Pool,
path: &str,
) -> Result<Option<Redirect>, rustango::sql::ExecError> {
use rustango::core::Column as _;
use rustango::sql::FetcherPool as _;
let rows: Vec<Redirect> = Redirect::objects()
.where_(Redirect::from_path.eq(path.to_owned()))
.where_(Redirect::is_active.eq(true))
.fetch(pool)
.await?;
Ok(rows.into_iter().next())
}
pub async fn override_rules(
pool: &rustango::sql::Pool,
) -> Result<Vec<Redirect>, rustango::sql::ExecError> {
use rustango::core::Column as _;
use rustango::sql::FetcherPool as _;
Redirect::objects()
.where_(Redirect::overrides_live.eq(true))
.where_(Redirect::is_active.eq(true))
.fetch(pool)
.await
}
fn rules_key(tenant_slug: &str, kind: &str) -> String {
format!("cms:redirect:{tenant_slug}:{kind}")
}
pub async fn override_rules_cached(
tenant_slug: &str,
pool: &rustango::sql::Pool,
cache: &rustango::cache::BoxedCache,
ttl: Option<std::time::Duration>,
) -> Vec<Redirect> {
let json = rustango::cache_fragment::cached_render(
cache.as_ref(),
&rules_key(tenant_slug, "overrides"),
ttl,
|| async {
let rules = override_rules(pool).await.unwrap_or_default();
serde_json::to_string(&rules).unwrap_or_else(|_| "[]".to_owned())
},
)
.await;
serde_json::from_str(&json).unwrap_or_default()
}
pub async fn resolve_destination(pool: &rustango::sql::Pool, r: &Redirect) -> String {
use rustango::core::Column as _;
if let Some(pid) = r.to_page_id {
let page = crate::page::Page::objects()
.where_(crate::page::Page::id.eq(pid))
.where_(crate::page::Page::status.is_in(crate::resolver::served_statuses()))
.first(pool)
.await
.ok()
.flatten()
.filter(|p| crate::resolver::visible_now(p, chrono::Utc::now()));
if let Some(p) = page {
return p.url_path;
}
tracing::warn!(
redirect_id = r.id.get().copied().unwrap_or_default(),
to_page_id = pid,
"redirect destination page missing/unpublished; using to_path fallback"
);
}
r.to_path.clone()
}
#[must_use]
pub fn is_wildcard(from_path: &str) -> bool {
from_path.contains('*')
}
#[must_use]
pub fn match_wildcard(pattern: &str, path: &str) -> Option<String> {
let star = pattern.find('*')?;
let prefix = &pattern[..star];
let suffix = &pattern[star + 1..];
if path.len() < prefix.len() + suffix.len() {
return None;
}
if !path.starts_with(prefix) || !path.ends_with(suffix) {
return None;
}
Some(path[prefix.len()..path.len() - suffix.len()].to_owned())
}
#[must_use]
pub fn apply_capture(to_path: &str, capture: &str) -> String {
match to_path.find('*') {
Some(star) => format!("{}{}{}", &to_path[..star], capture, &to_path[star + 1..]),
None => to_path.to_owned(),
}
}
#[must_use]
pub fn redirect_loops(from: &str, to: &str) -> bool {
let norm = |p: &str| {
let t = p.trim().trim_end_matches('/');
if t.is_empty() { "/".to_owned() } else { t.to_owned() }
};
if !from.contains('*') {
return norm(from) == norm(to);
}
let dest = apply_capture(to, "x");
match_wildcard(from, &dest).is_some() || match_wildcard(from, &format!("{}/", norm(&dest))).is_some()
}
fn best_wildcard_match<'a>(rules: &'a [Redirect], path: &str) -> Option<(&'a Redirect, String)> {
let mut best: Option<(&Redirect, String, usize, i64)> = None;
for r in rules {
if !is_wildcard(&r.from_path) {
continue;
}
let Some(cap) = match_wildcard(&r.from_path, path) else {
continue;
};
let prefix_len = r.from_path.find('*').unwrap_or(0);
let id = r.id.get().copied().unwrap_or(i64::MAX);
let take = match &best {
None => true,
Some((_, _, bp, bid)) => prefix_len > *bp || (prefix_len == *bp && id < *bid),
};
if take {
best = Some((r, cap, prefix_len, id));
}
}
best.map(|(r, cap, _, _)| (r, cap))
}
async fn destination_for(
pool: &rustango::sql::Pool,
rule: &Redirect,
capture: Option<&str>,
) -> String {
match (capture, rule.to_page_id) {
(Some(cap), None) => apply_capture(&rule.to_path, cap),
_ => resolve_destination(pool, rule).await,
}
}
pub async fn wildcard_rules(
pool: &rustango::sql::Pool,
) -> Result<Vec<Redirect>, rustango::sql::ExecError> {
use rustango::core::Column as _;
use rustango::sql::FetcherPool as _;
Redirect::objects()
.where_(Redirect::is_active.eq(true))
.where_(Redirect::from_path.contains("*"))
.fetch(pool)
.await
}
pub async fn wildcard_rules_cached(
tenant_slug: &str,
pool: &rustango::sql::Pool,
cache: &rustango::cache::BoxedCache,
ttl: Option<std::time::Duration>,
) -> Vec<Redirect> {
let json = rustango::cache_fragment::cached_render(
cache.as_ref(),
&rules_key(tenant_slug, "wildcards"),
ttl,
|| async {
let rules = wildcard_rules(pool).await.unwrap_or_default();
serde_json::to_string(&rules).unwrap_or_else(|_| "[]".to_owned())
},
)
.await;
serde_json::from_str(&json).unwrap_or_default()
}
fn build_response(to: String, permanent: bool, query: Option<&str>) -> axum::response::Response {
rustango::redirects::build_redirect_response(
&rustango::redirects::RedirectRule { to, permanent },
query,
)
}
pub(crate) async fn serve_override(
tenant: &rustango::extractors::Tenant,
cache: Option<(&rustango::cache::BoxedCache, std::time::Duration)>,
path: &str,
query: Option<&str>,
) -> Option<axum::response::Response> {
let rules = match cache {
Some((c, ttl)) => override_rules_cached(&tenant.org.slug, tenant.pool(), c, Some(ttl)).await,
None => override_rules(tenant.pool()).await.unwrap_or_default(),
};
let (rule, capture): (Redirect, Option<String>) = if let Some(r) = rules
.iter()
.find(|r| !is_wildcard(&r.from_path) && r.from_path == path)
{
(r.clone(), None)
} else {
let (r, cap) = best_wildcard_match(&rules, path)?;
(r.clone(), Some(cap))
};
let to = destination_for(tenant.pool(), &rule, capture.as_deref()).await;
let resp = build_response(to, rule.is_permanent, query);
record_hit(
tenant.pool(),
&tenant.org.slug,
rule.id.get().copied().unwrap_or_default(),
);
Some(resp)
}
pub(crate) async fn serve_wildcard(
tenant: &rustango::extractors::Tenant,
cache: Option<(&rustango::cache::BoxedCache, std::time::Duration)>,
path: &str,
query: Option<&str>,
) -> Option<axum::response::Response> {
let rules = match cache {
Some((c, ttl)) => wildcard_rules_cached(&tenant.org.slug, tenant.pool(), c, Some(ttl)).await,
None => wildcard_rules(tenant.pool()).await.unwrap_or_default(),
};
let (rule, capture) = best_wildcard_match(&rules, path)?;
let rule = rule.clone();
let to = destination_for(tenant.pool(), &rule, Some(&capture)).await;
let resp = build_response(to, rule.is_permanent, query);
record_hit(
tenant.pool(),
&tenant.org.slug,
rule.id.get().copied().unwrap_or_default(),
);
Some(resp)
}
pub(crate) async fn record_renames_tx(
tx: &mut rustango::sql::PoolTx<'_>,
moves: &[(i64, String, String)],
) -> Result<(), rustango::sql::ExecError> {
use rustango::core::Column as _;
use rustango::sql::FetcherTx as _;
for (page_id, old, new) in moves {
if old.is_empty() || old == new {
continue;
}
let existing = Redirect::objects()
.where_(Redirect::from_path.eq(old.clone()))
.fetch_tx(tx)
.await?
.into_iter()
.next();
let mut rule = existing.unwrap_or_else(|| Redirect {
id: Auto::Unset,
from_path: old.clone(),
to_path: String::new(),
is_permanent: true,
note: "Automatic: the page was renamed.".to_owned(),
hit_count: 0,
overrides_live: false,
from_page_id: None,
to_page_id: None,
is_active: true,
disabled_reason: String::new(),
last_hit_at: None,
created_at: Auto::Unset,
updated_at: Auto::Unset,
});
rule.to_path = new.clone();
rule.to_page_id = Some(*page_id);
rule.is_active = true;
rule.disabled_reason.clear();
rule.save_tx(tx).await?;
}
Ok(())
}
pub(crate) fn rebase_path(url: &str, new_base: &str, old_base: &str) -> Option<String> {
let rest = if new_base == "/" {
url.strip_prefix('/')?
} else {
url.strip_prefix(new_base)?.strip_prefix('/')?
};
Some(if old_base == "/" {
format!("/{rest}")
} else {
format!("{}/{rest}", old_base.trim_end_matches('/'))
})
}
pub async fn disable_for_page(
pool: &rustango::sql::Pool,
page_id: i64,
reason: &str,
) -> Result<u64, rustango::sql::ExecError> {
use rustango::core::Column as _;
use rustango::sql::FetcherPool as _;
let mut rows: Vec<Redirect> = Redirect::objects()
.where_(Redirect::from_page_id.eq(Some(page_id)))
.where_(Redirect::is_active.eq(true))
.fetch(pool)
.await?;
let more: Vec<Redirect> = Redirect::objects()
.where_(Redirect::to_page_id.eq(Some(page_id)))
.where_(Redirect::is_active.eq(true))
.fetch(pool)
.await?;
let seen: std::collections::HashSet<i64> =
rows.iter().filter_map(|r| r.id.get().copied()).collect();
rows.extend(
more.into_iter()
.filter(|r| r.id.get().copied().map_or(true, |id| !seen.contains(&id))),
);
let mut disabled = 0u64;
for mut row in rows {
row.is_active = false;
row.disabled_reason = reason.chars().take(255).collect();
row.save(pool).await?;
disabled += 1;
}
Ok(disabled)
}
#[must_use]
pub fn to_rule(r: &Redirect) -> rustango::redirects::RedirectRule {
rustango::redirects::RedirectRule {
to: r.to_path.clone(),
permanent: r.is_permanent,
}
}
pub async fn bump_hit_count(pool: &rustango::sql::Pool, mut row: Redirect) {
row.hit_count = row.hit_count.saturating_add(1);
if let Err(e) = row.save(pool).await {
tracing::warn!(error = %e, "cms_redirect: failed to bump hit_count");
}
}
const FLUSH_INTERVAL_SECS: i64 = 60;
#[derive(Default)]
struct HitAccum {
pending: std::collections::HashMap<(String, i64), (u64, i64)>,
last_flush: std::collections::HashMap<String, i64>,
}
static HITS: std::sync::LazyLock<std::sync::Mutex<HitAccum>> =
std::sync::LazyLock::new(|| std::sync::Mutex::new(HitAccum::default()));
fn lock_hits() -> std::sync::MutexGuard<'static, HitAccum> {
HITS.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner)
}
pub(crate) fn record_hit(pool: &rustango::sql::Pool, tenant_slug: &str, redirect_id: i64) {
if redirect_id == 0 {
return; }
let now = chrono::Utc::now().timestamp();
let due = {
let mut acc = lock_hits();
let e = acc
.pending
.entry((tenant_slug.to_owned(), redirect_id))
.or_insert((0, now));
e.0 = e.0.saturating_add(1);
e.1 = e.1.max(now);
let last = acc.last_flush.get(tenant_slug).copied().unwrap_or(0);
if now - last >= FLUSH_INTERVAL_SECS {
acc.last_flush.insert(tenant_slug.to_owned(), now);
true
} else {
false
}
};
if due {
let pool = pool.clone();
let slug = tenant_slug.to_owned();
rustango::__private_runtime::tokio::spawn(async move {
flush_tenant(&pool, &slug).await;
});
}
}
pub(crate) async fn flush_tenant(pool: &rustango::sql::Pool, tenant_slug: &str) {
let drained: Vec<(i64, u64, i64)> = {
let mut acc = lock_hits();
let keys: Vec<(String, i64)> = acc
.pending
.keys()
.filter(|(s, _)| s == tenant_slug)
.cloned()
.collect();
keys.into_iter()
.filter_map(|k| acc.pending.remove(&k).map(|(d, ts)| (k.1, d, ts)))
.collect()
};
if drained.is_empty() {
return;
}
let dialect = pool.dialect();
let sqlite = dialect.name() == "sqlite";
let sql = format!(
"UPDATE cms_redirect SET hit_count = hit_count + {}, \
last_hit_at = CASE WHEN last_hit_at IS NULL OR last_hit_at < {} THEN {} ELSE last_hit_at END \
WHERE id = {}",
dialect.placeholder(1),
dialect.placeholder(2),
dialect.placeholder(3),
dialect.placeholder(4),
);
for (id, delta, ts) in drained {
let ts_dt = chrono::DateTime::from_timestamp(ts, 0).unwrap_or_else(chrono::Utc::now);
let ts_bind = if sqlite {
rustango::core::SqlValue::String(ts_dt.format("%Y-%m-%d %H:%M:%S").to_string())
} else {
rustango::core::SqlValue::DateTime(ts_dt)
};
let binds = vec![
rustango::core::SqlValue::I64(delta as i64),
ts_bind.clone(),
ts_bind,
rustango::core::SqlValue::I64(id),
];
if let Err(e) = rustango::sql::raw_execute_pool(pool, &sql, binds).await {
tracing::warn!(redirect_id = id, error = %e, "redirect hit flush failed");
}
}
}
pub async fn flush_all(pools: &[(String, rustango::sql::Pool)]) {
for (slug, pool) in pools {
flush_tenant(pool, slug).await;
}
}
pub const STALE_AFTER_DAYS: i64 = 90;
#[must_use]
pub(crate) fn staleness(r: &Redirect, now: DateTime<Utc>) -> &'static str {
let cutoff = now - chrono::Duration::days(STALE_AFTER_DAYS);
if !r.is_active {
"disabled"
} else if r.last_hit_at.is_none() {
"unused"
} else if r.last_hit_at.map_or(false, |t| t < cutoff) {
"stale"
} else {
"active"
}
}
#[derive(Default)]
pub struct StaleRedirectsReport;
#[async_trait::async_trait]
impl crate::admin::report::Report for StaleRedirectsReport {
fn slug(&self) -> &'static str {
"stale-redirects"
}
fn title(&self) -> &'static str {
"Stale redirects"
}
fn description(&self) -> Option<&'static str> {
Some("Redirects nothing has used recently — candidates to retire. 'unused' never served; 'stale' hasn't served in 90+ days; 'disabled' was switched off when a linked page was removed.")
}
fn icon(&self) -> Option<&'static str> {
Some("link_off")
}
fn columns(&self) -> Vec<crate::admin::report::ReportColumn> {
use crate::admin::report::ReportColumn;
vec![
ReportColumn::text("from", "From"),
ReportColumn::text("to", "To"),
ReportColumn::status("verdict", "Status"),
ReportColumn::number("hits", "Hits"),
ReportColumn::datetime("last_used", "Last used"),
]
}
async fn rows(
&self,
pool: &rustango::sql::Pool,
_user_id: Option<i64>,
) -> Vec<serde_json::Value> {
use rustango::sql::FetcherPool as _;
let now = chrono::Utc::now();
let mut rows: Vec<Redirect> = Redirect::objects().fetch(pool).await.unwrap_or_default();
let rank = |v: &str| -> u8 {
match v {
"disabled" => 0,
"unused" => 1,
"stale" => 2,
_ => 3,
}
};
rows.sort_by(|a, b| {
rank(staleness(a, now))
.cmp(&rank(staleness(b, now)))
.then_with(|| a.last_hit_at.cmp(&b.last_hit_at))
});
rows.into_iter()
.filter(|r| rank(staleness(r, now)) < 3) .map(|r| {
let v = staleness(&r, now);
let id = r.id.get().copied().unwrap_or_default();
json!({
"from": r.from_path,
"to": r.to_path,
"verdict": match v { "disabled" => "archived", "unused" => "draft", _ => "scheduled" },
"hits": r.hit_count,
"last_used": r.last_hit_at.map(|t| t.to_rfc3339()),
"edit_url": format!("/cms-admin/redirects/{id}/edit"),
})
})
.collect()
}
}
crate::register_report!(StaleRedirectsReport);
#[cfg(all(test, feature = "sqlite"))]
mod tests {
use super::*;
#[test]
fn rule_sets_are_cached_per_tenant() {
assert_ne!(super::rules_key("acme", "overrides"), super::rules_key("globex", "overrides"));
assert_ne!(super::rules_key("acme", "overrides"), super::rules_key("acme", "wildcards"));
}
use rustango::core::Column as _;
use rustango::core::Model as _;
use rustango::sql::FetcherPool as _;
use rustango::sql::Pool;
async fn mem_pool() -> Pool {
let pool = Pool::connect("sqlite::memory:")
.await
.expect("in-memory sqlite");
for schema in [
&crate::page_type_model::PageType::SCHEMA,
&crate::media::Media::SCHEMA,
&crate::theme::Theme::SCHEMA,
&crate::page::Page::SCHEMA,
&Redirect::SCHEMA,
] {
let ddl = rustango::migrate::ddl::create_table_if_not_exists_sql_with_dialect(
pool.dialect(),
schema,
);
for stmt in ddl.split(';').map(str::trim).filter(|s| !s.is_empty()) {
rustango::sql::raw_execute_pool(&pool, stmt, Vec::new())
.await
.expect("ddl");
}
}
pool
}
fn mk_redirect(from: &str, to: &str) -> Redirect {
Redirect {
id: Auto::Unset,
from_path: from.to_owned(),
to_path: to.to_owned(),
is_permanent: true,
note: String::new(),
hit_count: 0,
overrides_live: false,
from_page_id: None,
to_page_id: None,
is_active: true,
disabled_reason: String::new(),
last_hit_at: None,
created_at: Auto::Unset,
updated_at: Auto::Unset,
}
}
#[test]
fn staleness_classifies_all_four_verdicts() {
let now = chrono::Utc::now();
let mut r = mk_redirect("/a", "/b");
r.last_hit_at = Some(now - chrono::Duration::days(1));
assert_eq!(staleness(&r, now), "active");
r.last_hit_at = Some(now - chrono::Duration::days(STALE_AFTER_DAYS + 5));
assert_eq!(staleness(&r, now), "stale");
r.last_hit_at = None;
assert_eq!(staleness(&r, now), "unused");
r.is_active = false;
r.last_hit_at = Some(now);
assert_eq!(staleness(&r, now), "disabled");
}
async fn mk_page(pool: &Pool, slug: &str, status: &str) -> i64 {
let mut page = crate::page::Page {
id: Auto::Unset,
page_type_id: 1,
title: format!("Page {slug}"),
slug: slug.to_owned(),
path: format!("0001{slug}/"),
url_path: format!("/{slug}"),
preview_path: String::new(),
template_override: String::new(),
depth: 1,
parent_id: None,
locale_variant_of: None,
alias_of: None,
theme_id: None,
sort_order: 0,
status: status.to_owned(),
published_at: None,
last_published_at: None,
go_live_at: None,
expire_at: None,
seo_title: String::new(),
seo_description: String::new(),
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,
};
let existing = crate::page_type_model::PageType::objects()
.fetch(pool)
.await
.expect("pt fetch");
if existing.is_empty() {
let mut pt = crate::page_type_model::PageType {
id: Auto::Unset,
app_label: "cms".to_owned(),
type_name: "TestPage".to_owned(),
verbose_name: "Test page".to_owned(),
default_template: "page.html".to_owned(),
view_mode: "auto".to_owned(),
is_creatable: true,
allowed_parent_types: serde_json::json!([]),
allowed_child_types: serde_json::json!([]),
workflow: String::new(),
created_at: Auto::Unset,
updated_at: Auto::Unset,
};
pt.insert(pool).await.expect("insert page type");
}
page.insert(pool).await.expect("insert page");
page.id.get().copied().expect("page id")
}
#[test]
fn rebase_path_swaps_the_moved_base() {
assert_eq!(rebase_path("/shop/moon-jar", "/shop", "/store").as_deref(), Some("/store/moon-jar"));
assert_eq!(rebase_path("/a/b/c", "/a/b", "/x").as_deref(), Some("/x/c"));
assert_eq!(rebase_path("/shop", "/", "/home").as_deref(), Some("/home/shop"));
assert_eq!(rebase_path("/home/shop", "/home", "/").as_deref(), Some("/shop"));
assert_eq!(rebase_path("/other", "/shop", "/store"), None);
assert_eq!(rebase_path("/shopping", "/shop", "/store"), None);
}
#[tokio::test]
async fn renames_leave_rules_that_follow_the_page() {
let pool = mem_pool().await;
let mut stale = mk_redirect("/old-jar", "/nowhere");
stale.is_active = false;
stale.disabled_reason = "Linked page was unpublished".to_owned();
stale.save(&pool).await.expect("stale rule");
let moves = vec![
(7, "/old-jar".to_owned(), "/shop/jar".to_owned()),
(8, "/shop/vase".to_owned(), "/shop/vase".to_owned()), (9, "/shop/bowl".to_owned(), "/store/bowl".to_owned()),
];
let mut tx = rustango::sql::transaction_pool(&pool).await.expect("tx");
record_renames_tx(&mut tx, &moves).await.expect("record");
tx.commit().await.expect("commit");
let rules: Vec<Redirect> = Redirect::objects().fetch(&pool).await.expect("rules");
assert_eq!(rules.len(), 2, "one per moved address, the stale one reused: {rules:?}");
let jar = rules.iter().find(|r| r.from_path == "/old-jar").expect("re-pointed");
assert_eq!((jar.to_path.as_str(), jar.to_page_id, jar.is_active), ("/shop/jar", Some(7), true));
assert!(jar.disabled_reason.is_empty());
let bowl = rules.iter().find(|r| r.from_path == "/shop/bowl").expect("created");
assert_eq!((bowl.to_page_id, bowl.is_permanent), (Some(9), true));
}
#[tokio::test]
async fn ensure_columns_is_idempotent() {
let pool = mem_pool().await;
ensure_columns(&pool).await.expect("first ensure");
ensure_columns(&pool).await.expect("second ensure");
}
#[tokio::test]
async fn ensure_columns_upgrades_old_table() {
let pool = Pool::connect("sqlite::memory:").await.expect("pool");
rustango::sql::raw_execute_pool(
&pool,
"CREATE TABLE cms_redirect (
id INTEGER PRIMARY KEY AUTOINCREMENT,
from_path VARCHAR(510) NOT NULL UNIQUE,
to_path VARCHAR(1024) NOT NULL,
is_permanent BOOLEAN NOT NULL DEFAULT TRUE,
note VARCHAR(255) NOT NULL DEFAULT '',
hit_count BIGINT NOT NULL DEFAULT 0,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
)",
Vec::new(),
)
.await
.expect("old-shape table");
ensure_columns(&pool).await.expect("upgrade");
let mut row = mk_redirect("/old", "/new");
row.insert(&pool).await.expect("insert");
let got = find_for_path(&pool, "/old").await.expect("query");
let got = got.expect("row found");
assert!(got.is_active);
assert!(!got.overrides_live);
assert_eq!(got.disabled_reason, "");
}
#[tokio::test]
async fn find_for_path_skips_disabled_rules() {
let pool = mem_pool().await;
let mut row = mk_redirect("/dead", "/alive");
row.is_active = false;
row.disabled_reason = "Linked page was deleted".to_owned();
row.insert(&pool).await.expect("insert");
assert!(
find_for_path(&pool, "/dead")
.await
.expect("query")
.is_none(),
"disabled rules must never serve"
);
}
#[tokio::test]
async fn override_rules_returns_only_active_overrides() {
let pool = mem_pool().await;
let mut normal = mk_redirect("/a", "/b");
normal.insert(&pool).await.expect("insert normal");
let mut over = mk_redirect("/live", "/elsewhere");
over.overrides_live = true;
over.insert(&pool).await.expect("insert override");
let mut dead_over = mk_redirect("/live2", "/elsewhere2");
dead_over.overrides_live = true;
dead_over.is_active = false;
dead_over.insert(&pool).await.expect("insert dead");
let rules = override_rules(&pool).await.expect("query");
assert_eq!(rules.len(), 1);
assert_eq!(rules[0].from_path, "/live");
}
#[tokio::test]
async fn resolve_destination_follows_current_page_url() {
let pool = mem_pool().await;
let pid = mk_page(&pool, "target", "published").await;
let mut rule = mk_redirect("/old", "/stale-snapshot");
rule.to_page_id = Some(pid);
assert_eq!(resolve_destination(&pool, &rule).await, "/target");
let mut page = crate::page::Page::objects()
.where_(crate::page::Page::id.eq(pid))
.first(&pool)
.await
.expect("q")
.expect("page");
page.url_path = "/moved/target".to_owned();
page.save(&pool).await.expect("save");
assert_eq!(resolve_destination(&pool, &rule).await, "/moved/target");
}
#[tokio::test]
async fn resolve_destination_follows_an_archived_page() {
let pool = mem_pool().await;
let pid = mk_page(&pool, "kept", "archived").await;
let mut rule = mk_redirect("/old", "/stale-snapshot");
rule.to_page_id = Some(pid);
assert_eq!(resolve_destination(&pool, &rule).await, "/kept");
}
#[tokio::test]
async fn resolve_destination_falls_back_when_page_unpublished() {
let pool = mem_pool().await;
let pid = mk_page(&pool, "gone", "draft").await;
let mut rule = mk_redirect("/old", "/fallback");
rule.to_page_id = Some(pid);
assert_eq!(
resolve_destination(&pool, &rule).await,
"/fallback",
"unpublished target page must fall back to to_path"
);
}
#[test]
fn wildcard_prefix_match_and_capture() {
assert_eq!(
match_wildcard("/old-blog/*", "/old-blog/2024/hi").as_deref(),
Some("2024/hi")
);
assert_eq!(
match_wildcard("/old-blog/*", "/old-blog/").as_deref(),
Some("")
);
assert_eq!(match_wildcard("/old-blog/*", "/old-blog"), None);
assert_eq!(match_wildcard("/old-blog/*", "/other/x"), None);
}
#[test]
fn wildcard_suffix_match_and_capture() {
assert_eq!(
match_wildcard("*/feed", "/blog/feed").as_deref(),
Some("/blog")
);
assert_eq!(
match_wildcard("*/feed", "/a/b/feed").as_deref(),
Some("/a/b")
);
assert_eq!(match_wildcard("*/feed", "/feedx"), None);
}
#[test]
fn loops_are_detected_and_ordinary_rules_are_not() {
assert!(redirect_loops("/a", "/a"));
assert!(redirect_loops("/a/", "/a"));
assert!(redirect_loops("/blog/*", "/blog/archive/*"));
assert!(redirect_loops("/blog/*", "/blog/"));
assert!(redirect_loops("*/feed", "/new/*/feed"));
assert!(!redirect_loops("/a", "/b"));
assert!(!redirect_loops("/old-blog/*", "/blog/*"));
assert!(!redirect_loops("/blog/*", "https://example.test/blog/*"));
assert!(!redirect_loops("*/feed", "*/rss"));
}
#[test]
fn apply_capture_substitutes_or_drops() {
assert_eq!(apply_capture("/blog/*", "2024/hi"), "/blog/2024/hi");
assert_eq!(apply_capture("/archive", "2024/hi"), "/archive"); }
#[test]
fn best_wildcard_match_prefers_longest_prefix_then_lowest_id() {
let mut broad = mk_redirect("/docs/*", "/d/*");
broad.id = Auto::Set(2);
let mut specific = mk_redirect("/docs/0.44/*", "/d44/*");
specific.id = Auto::Set(5);
let rules = vec![broad, specific];
let (r, cap) = best_wildcard_match(&rules, "/docs/0.44/intro").expect("match");
assert_eq!(r.from_path, "/docs/0.44/*");
assert_eq!(cap, "intro");
let (r2, cap2) = best_wildcard_match(&rules, "/docs/other").expect("match");
assert_eq!(r2.from_path, "/docs/*");
assert_eq!(cap2, "other");
}
#[tokio::test]
async fn wildcard_rules_query_returns_only_active_patterns() {
let pool = mem_pool().await;
mk_redirect("/exact", "/x")
.insert(&pool)
.await
.expect("exact");
mk_redirect("/glob/*", "/g/*")
.insert(&pool)
.await
.expect("glob");
let mut dead = mk_redirect("/dead/*", "/d/*");
dead.is_active = false;
dead.insert(&pool).await.expect("dead");
let rules = wildcard_rules(&pool).await.expect("query");
assert_eq!(rules.len(), 1, "only the active pattern");
assert_eq!(rules[0].from_path, "/glob/*");
}
#[tokio::test]
async fn disable_for_page_hits_both_link_columns_and_records_reason() {
let pool = mem_pool().await;
let mut from_linked = mk_redirect("/f", "/x");
from_linked.from_page_id = Some(7);
from_linked.insert(&pool).await.expect("i1");
let mut to_linked = mk_redirect("/t", "/y");
to_linked.to_page_id = Some(7);
to_linked.insert(&pool).await.expect("i2");
let mut unrelated = mk_redirect("/u", "/z");
unrelated.from_page_id = Some(8);
unrelated.insert(&pool).await.expect("i3");
let n = disable_for_page(&pool, 7, "Linked page “X” was deleted")
.await
.expect("disable");
assert_eq!(n, 2, "both linked rules disabled, unrelated untouched");
assert!(find_for_path(&pool, "/f").await.expect("q").is_none());
assert!(find_for_path(&pool, "/t").await.expect("q").is_none());
assert!(find_for_path(&pool, "/u").await.expect("q").is_some());
let rows: Vec<Redirect> = Redirect::objects()
.where_(Redirect::from_path.eq("/f".to_owned()))
.fetch(&pool)
.await
.expect("fetch");
assert_eq!(rows[0].disabled_reason, "Linked page “X” was deleted");
let again = disable_for_page(&pool, 7, "x").await.expect("again");
assert_eq!(again, 0);
}
#[tokio::test]
async fn flush_tenant_applies_deltas_additively_and_sets_last_hit() {
let pool = mem_pool().await;
let mut r = mk_redirect("/flush", "/dest");
r.insert(&pool).await.expect("insert");
let id = Redirect::objects()
.where_(Redirect::from_path.eq("/flush".to_owned()))
.fetch(&pool)
.await
.expect("fetch")[0]
.id
.get()
.copied()
.expect("id");
let ts = chrono::Utc::now().timestamp();
{
let mut acc = lock_hits();
acc.pending
.insert(("flushtest_555".to_owned(), id), (3, ts));
}
flush_tenant(&pool, "flushtest_555").await;
let row = Redirect::objects()
.where_(Redirect::id.eq(id))
.fetch(&pool)
.await
.expect("refetch")
.remove(0);
assert_eq!(row.hit_count, 3, "delta applied");
assert!(row.last_hit_at.is_some(), "last_hit_at stamped");
flush_tenant(&pool, "flushtest_555").await;
let row2 = Redirect::objects()
.where_(Redirect::id.eq(id))
.fetch(&pool)
.await
.expect("refetch2")
.remove(0);
assert_eq!(row2.hit_count, 3, "no double count on empty flush");
}
}
#[cfg(all(test, feature = "postgres"))]
mod pg_tests {
use rustango::sql::sqlx;
#[tokio::test]
#[ignore = "needs RCMS_TEST_PG_URL"]
async fn last_hit_at_is_timestamptz_after_ensure() {
let url = std::env::var("RCMS_TEST_PG_URL")
.expect("set RCMS_TEST_PG_URL to a throwaway Postgres database to run this");
let schema = format!("rcms_test_redirect_{}", std::process::id());
let admin = sqlx::PgPool::connect(&url).await.expect("connect");
sqlx::query(&format!("CREATE SCHEMA {schema}")).execute(&admin).await.expect("schema");
let sep = if url.contains('?') { '&' } else { '?' };
let pool = rustango::sql::Pool::connect(&format!(
"{url}{sep}options=-c%20search_path%3D{schema}"
))
.await
.expect("scoped");
let pg = pool.as_postgres().expect("pg");
sqlx::query(
"CREATE TABLE cms_redirect (id BIGSERIAL PRIMARY KEY, \
from_path VARCHAR(510) NOT NULL UNIQUE, to_path VARCHAR(1024) NOT NULL, \
is_permanent BOOLEAN NOT NULL DEFAULT TRUE, note VARCHAR(255) NOT NULL DEFAULT '', \
hit_count BIGINT NOT NULL DEFAULT 0, last_hit_at TIMESTAMP, \
created_at TIMESTAMPTZ NOT NULL DEFAULT now(), updated_at TIMESTAMPTZ NOT NULL DEFAULT now())",
)
.execute(pg)
.await
.expect("old table");
sqlx::query("INSERT INTO cms_redirect (from_path, to_path, last_hit_at) VALUES ('/a', '/b', '2026-01-02 03:04:05')")
.execute(pg)
.await
.expect("hit row");
super::ensure_columns(&pool).await.expect("ensure");
super::ensure_columns(&pool).await.expect("idempotent");
let data_type: String = sqlx::query_scalar(
"SELECT data_type FROM information_schema.columns WHERE table_schema = $1 \
AND table_name = 'cms_redirect' AND column_name = 'last_hit_at'",
)
.bind(&schema)
.fetch_one(pg)
.await
.expect("type");
let found = super::find_for_path(&pool, "/a").await;
sqlx::query(&format!("DROP SCHEMA {schema} CASCADE")).execute(&admin).await.expect("drop");
assert_eq!(data_type, "timestamp with time zone");
let row = found.expect("a hit row decodes").expect("row");
assert_eq!(row.last_hit_at.map(|t| t.to_rfc3339()).as_deref(), Some("2026-01-02T03:04:05+00:00"));
}
}