use anyhow::Result;
use include_dir::{include_dir, Dir};
use sqlx::Connection;
use sqlx::PgPool;
use std::collections::{HashMap, HashSet};
use std::sync::Arc;
static MIGRATIONS: Dir = include_dir!("$CARGO_MANIFEST_DIR/migrations");
const BASE_TABLES: &[&str] = &[
"instances",
"executions",
"history",
"orchestrator_queue",
"worker_queue",
"instance_locks",
];
const ALL_CORE_TABLES: &[&str] = &[
"instances",
"executions",
"history",
"orchestrator_queue",
"worker_queue",
"instance_locks",
"sessions",
"kv_store",
"kv_delta",
];
#[derive(Debug)]
struct Migration {
version: i64,
name: String,
sql: String,
}
pub struct MigrationRunner {
pool: Arc<PgPool>,
schema_name: String,
}
impl MigrationRunner {
pub fn new(pool: Arc<PgPool>, schema_name: String) -> Self {
Self { pool, schema_name }
}
fn advisory_lock_key(&self) -> i64 {
const OFFSET: u64 = 0xcbf29ce484222325;
const PRIME: u64 = 0x100000001b3;
let mut hash = OFFSET;
for b in b"duroxide_pg:migrations:" {
hash ^= *b as u64;
hash = hash.wrapping_mul(PRIME);
}
for b in self.schema_name.as_bytes() {
hash ^= *b as u64;
hash = hash.wrapping_mul(PRIME);
}
hash as i64
}
async fn lock_for_migrations(&self, conn: &mut sqlx::postgres::PgConnection) -> Result<()> {
let key = self.advisory_lock_key();
sqlx::query("SELECT pg_advisory_lock($1)")
.bind(key)
.execute(&mut *conn)
.await?;
Ok(())
}
async fn unlock_for_migrations(&self, conn: &mut sqlx::postgres::PgConnection) {
let key = self.advisory_lock_key();
let _ = sqlx::query("SELECT pg_advisory_unlock($1)")
.bind(key)
.execute(&mut *conn)
.await;
}
pub async fn migrate(&self) -> Result<()> {
let mut conn = self.pool.acquire().await?;
let conn = &mut *conn;
self.lock_for_migrations(conn).await?;
let result = match self.check_no_unknown_migrations(conn).await {
Ok(()) => self.migrate_inner(conn).await,
Err(e) => Err(e),
};
self.unlock_for_migrations(conn).await;
result
}
pub async fn verify(&self) -> Result<()> {
let mut conn = self.pool.acquire().await?;
let conn = &mut *conn;
let table_exists = self.check_migration_table_exists(conn).await?;
if !table_exists {
anyhow::bail!(
"duroxide migrations not initialized: schema {:?} does not \
contain _duroxide_migrations. Construct a provider with \
MigrationPolicy::ApplyAll (the default) from a process with \
DDL privileges before using MigrationPolicy::VerifyOnly.",
self.schema_name
);
}
self.check_no_unknown_migrations(conn).await?;
let migrations = self.load_migrations()?;
let applied = self.get_applied_versions(conn).await?;
self.validate_applied_prefix(&migrations, &applied)?;
let existing_tables = self.get_existing_core_relations(conn).await?;
self.validate_core_table_state(&applied, &existing_tables)?;
let applied: std::collections::HashSet<i64> = applied.into_iter().collect();
let mut missing: Vec<i64> = migrations
.iter()
.map(|m| m.version)
.filter(|v| !applied.contains(v))
.collect();
missing.sort_unstable();
if !missing.is_empty() {
anyhow::bail!(
"duroxide migrations not up to date in schema {:?}: missing \
versions {:?}. Run migrations from a provider configured with \
MigrationPolicy::ApplyAll before constructing VerifyOnly \
providers.",
self.schema_name,
missing,
);
}
Ok(())
}
async fn check_no_unknown_migrations(
&self,
conn: &mut sqlx::postgres::PgConnection,
) -> Result<()> {
if !self.check_migration_table_exists(conn).await? {
return Ok(());
}
let applied = self.get_applied_versions(conn).await?;
let expected: std::collections::HashSet<i64> = self
.load_migrations()?
.into_iter()
.map(|m| m.version)
.collect();
let mut unknown: Vec<i64> = applied
.into_iter()
.filter(|v| !expected.contains(v))
.collect();
unknown.sort_unstable();
if !unknown.is_empty() {
anyhow::bail!(
"schema {:?} has migrations not recognized by this version of \
the code: {:?}. The database schema is ahead of the code. \
Update the code to a compatible version.",
self.schema_name,
unknown,
);
}
Ok(())
}
async fn check_migration_table_exists(
&self,
conn: &mut sqlx::postgres::PgConnection,
) -> Result<bool> {
let relation_kind: Option<String> = sqlx::query_scalar(
"SELECT c.relkind::text \
FROM pg_catalog.pg_class c \
JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace \
WHERE n.nspname = $1 AND c.relname = '_duroxide_migrations'",
)
.bind(&self.schema_name)
.fetch_optional(&mut *conn)
.await?;
match relation_kind.as_deref() {
None => Ok(false),
Some("r" | "p") => Ok(true),
Some(kind) => anyhow::bail!(
"duroxide migration tracking object in schema {:?} has PostgreSQL \
relation kind {:?}, not a base table; refusing to read or replace it",
self.schema_name,
kind,
),
}
}
fn validate_applied_prefix(
&self,
migrations: &[Migration],
applied_versions: &[i64],
) -> Result<()> {
for (index, (migration, applied)) in
migrations.iter().zip(applied_versions.iter()).enumerate()
{
if migration.version != *applied {
anyhow::bail!(
"duroxide migration history is inconsistent in schema {:?}: \
expected version {} at ledger position {}, but found version {}. \
Applied migrations must be an ordered prefix of the bundled \
migrations; refusing to apply migrations or modify the ledger.",
self.schema_name,
migration.version,
index + 1,
applied,
);
}
}
if applied_versions.len() > migrations.len() {
anyhow::bail!(
"duroxide migration history is inconsistent in schema {:?}: \
the ledger contains {} entries, but this binary bundles only {}. \
Refusing to apply migrations or modify the ledger.",
self.schema_name,
applied_versions.len(),
migrations.len(),
);
}
Ok(())
}
fn expected_core_tables(applied_versions: &[i64]) -> HashSet<String> {
let Some(latest) = applied_versions.last().copied() else {
return HashSet::new();
};
let mut expected: HashSet<String> =
BASE_TABLES.iter().map(|name| (*name).to_string()).collect();
if latest >= 14 {
expected.insert("sessions".to_string());
}
if latest >= 18 {
expected.insert("kv_store".to_string());
}
if latest >= 20 {
expected.insert("kv_delta".to_string());
}
expected
}
fn validate_core_table_state(
&self,
applied_versions: &[i64],
existing_relations: &HashMap<String, String>,
) -> Result<()> {
let expected_tables = Self::expected_core_tables(applied_versions);
let mut missing: Vec<&str> = expected_tables
.iter()
.filter(|name| !existing_relations.contains_key(*name))
.map(String::as_str)
.collect();
let mut wrong_kind: Vec<(&str, &str)> = expected_tables
.iter()
.filter_map(|name| {
existing_relations.get(name).and_then(|kind| {
(!matches!(kind.as_str(), "r" | "p")).then_some((name.as_str(), kind.as_str()))
})
})
.collect();
let mut unexpected: Vec<&str> = existing_relations
.keys()
.filter(|name| !expected_tables.contains(*name))
.map(String::as_str)
.collect();
missing.sort_unstable();
wrong_kind.sort_unstable();
unexpected.sort_unstable();
if !wrong_kind.is_empty() {
anyhow::bail!(
"duroxide schema {:?} has required relations {:?} with non-table \
PostgreSQL relation kinds. The schema may be corrupted; refusing \
to apply migrations or modify the ledger automatically.",
self.schema_name,
wrong_kind,
);
}
if !missing.is_empty() {
anyhow::bail!(
"duroxide migration history in schema {:?} requires tables {:?}, \
but they are missing. The schema may be corrupted; refusing to \
apply migrations or modify the ledger automatically.",
self.schema_name,
missing,
);
}
if !unexpected.is_empty() {
anyhow::bail!(
"duroxide schema {:?} contains tables {:?} that are not represented \
by its migration ledger. The schema may be corrupted; refusing to \
apply migrations or modify the ledger automatically.",
self.schema_name,
unexpected,
);
}
Ok(())
}
async fn migrate_inner(&self, conn: &mut sqlx::postgres::PgConnection) -> Result<()> {
let schema_exists = self.check_schema_exists(conn).await?;
if !schema_exists && self.schema_name != "public" {
sqlx::query(&format!("CREATE SCHEMA IF NOT EXISTS {}", self.schema_name))
.execute(&mut *conn)
.await?;
}
let migrations = self.load_migrations()?;
tracing::debug!(
"Loaded {} migrations for schema {}",
migrations.len(),
self.schema_name
);
let tracking_exists = self.check_migration_table_exists(conn).await?;
let existing_tables = self.get_existing_core_relations(conn).await?;
if !tracking_exists && !existing_tables.is_empty() {
let mut relations: Vec<(&str, &str)> = existing_tables
.iter()
.map(|(name, kind)| (name.as_str(), kind.as_str()))
.collect();
relations.sort_unstable();
anyhow::bail!(
"duroxide relations {:?} exist in schema {:?}, but the migration \
tracking table is missing. The schema may be corrupted; refusing \
to create a new ledger or apply migrations automatically.",
relations,
self.schema_name,
);
}
if !tracking_exists {
self.ensure_migration_table(conn).await?;
}
let applied_versions = self.get_applied_versions(conn).await?;
self.validate_applied_prefix(&migrations, &applied_versions)?;
self.validate_core_table_state(&applied_versions, &existing_tables)?;
tracing::debug!("Applied migrations: {:?}", applied_versions);
for migration in migrations.into_iter().skip(applied_versions.len()) {
tracing::debug!(
"Applying migration {}: {}",
migration.version,
migration.name
);
self.apply_migration(conn, &migration).await?;
}
Ok(())
}
fn load_migrations(&self) -> Result<Vec<Migration>> {
let mut migrations = Vec::new();
let mut files: Vec<_> = MIGRATIONS
.files()
.filter(|file| file.path().extension().and_then(|ext| ext.to_str()) == Some("sql"))
.collect();
files.sort_by_key(|f| f.path());
for file in files {
let file_name = file
.path()
.file_name()
.and_then(|n| n.to_str())
.ok_or_else(|| anyhow::anyhow!("Invalid filename in migrations"))?;
let sql = file
.contents_utf8()
.ok_or_else(|| anyhow::anyhow!("Migration file is not valid UTF-8: {file_name}"))?
.to_string();
let version = self.parse_version(file_name)?;
let name = file_name.to_string();
migrations.push(Migration { version, name, sql });
}
Ok(migrations)
}
fn parse_version(&self, filename: &str) -> Result<i64> {
let version_str = filename
.split('_')
.next()
.ok_or_else(|| anyhow::anyhow!("Invalid migration filename: {filename}"))?;
version_str
.parse::<i64>()
.map_err(|e| anyhow::anyhow!("Invalid migration version {version_str}: {e}"))
}
async fn ensure_migration_table(&self, conn: &mut sqlx::postgres::PgConnection) -> Result<()> {
sqlx::query(&format!(
r#"
CREATE TABLE IF NOT EXISTS {}._duroxide_migrations (
version BIGINT PRIMARY KEY,
name TEXT NOT NULL,
applied_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP
)
"#,
self.schema_name
))
.execute(&mut *conn)
.await?;
Ok(())
}
async fn check_schema_exists(&self, conn: &mut sqlx::postgres::PgConnection) -> Result<bool> {
let exists = sqlx::query_scalar(
"SELECT EXISTS(SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname = $1)",
)
.bind(&self.schema_name)
.fetch_one(&mut *conn)
.await?;
Ok(exists)
}
async fn get_existing_core_relations(
&self,
conn: &mut sqlx::postgres::PgConnection,
) -> Result<HashMap<String, String>> {
let known_tables: Vec<String> = ALL_CORE_TABLES
.iter()
.map(|name| (*name).to_string())
.collect();
let relations: Vec<(String, String)> = sqlx::query_as(
"SELECT c.relname::text, c.relkind::text \
FROM pg_catalog.pg_class c \
JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace \
WHERE n.nspname = $1 AND c.relname = ANY($2::text[])",
)
.bind(&self.schema_name)
.bind(&known_tables)
.fetch_all(&mut *conn)
.await?;
Ok(relations.into_iter().collect())
}
async fn get_applied_versions(
&self,
conn: &mut sqlx::postgres::PgConnection,
) -> Result<Vec<i64>> {
let versions: Vec<i64> = sqlx::query_scalar(&format!(
"SELECT version FROM {}._duroxide_migrations ORDER BY version",
self.schema_name
))
.fetch_all(&mut *conn)
.await?;
Ok(versions)
}
fn split_sql_statements(sql: &str) -> Vec<String> {
let mut statements = Vec::new();
let mut current_statement = String::new();
let chars: Vec<char> = sql.chars().collect();
let mut i = 0;
let mut in_dollar_quote = false;
let mut dollar_tag: Option<String> = None;
while i < chars.len() {
let ch = chars[i];
if !in_dollar_quote {
if ch == '$' {
let mut tag = String::new();
tag.push(ch);
i += 1;
while i < chars.len() {
let next_ch = chars[i];
if next_ch == '$' {
tag.push(next_ch);
dollar_tag = Some(tag.clone());
in_dollar_quote = true;
current_statement.push_str(&tag);
i += 1;
break;
} else if next_ch.is_alphanumeric() || next_ch == '_' {
tag.push(next_ch);
i += 1;
} else {
current_statement.push(ch);
break;
}
}
} else if ch == ';' {
current_statement.push(ch);
let trimmed = current_statement.trim().to_string();
if !trimmed.is_empty() {
statements.push(trimmed);
}
current_statement.clear();
i += 1;
} else {
current_statement.push(ch);
i += 1;
}
} else {
current_statement.push(ch);
if ch == '$' {
let tag = dollar_tag.as_ref().unwrap();
let mut matches = true;
for (j, tag_char) in tag.chars().enumerate() {
if j == 0 {
continue; }
if i + j >= chars.len() || chars[i + j] != tag_char {
matches = false;
break;
}
}
if matches {
for _ in 0..(tag.len() - 1) {
if i + 1 < chars.len() {
current_statement.push(chars[i + 1]);
i += 1;
}
}
in_dollar_quote = false;
dollar_tag = None;
}
}
i += 1;
}
}
let trimmed = current_statement.trim().to_string();
if !trimmed.is_empty() {
statements.push(trimmed);
}
statements
}
fn migration_search_path_stmt(schema_name: &str) -> String {
format!("SET LOCAL search_path TO {schema_name}, pg_temp")
}
async fn apply_migration(
&self,
conn: &mut sqlx::postgres::PgConnection,
migration: &Migration,
) -> Result<()> {
let mut tx = conn.begin().await?;
sqlx::query(&Self::migration_search_path_stmt(&self.schema_name))
.execute(&mut *tx)
.await?;
let sql = migration.sql.trim();
let cleaned_sql: String = sql
.lines()
.map(|line| {
if let Some(idx) = line.find("--") {
let before = &line[..idx];
if before.matches('\'').count() % 2 == 0 {
line[..idx].trim()
} else {
line
}
} else {
line
}
})
.filter(|line| !line.is_empty())
.collect::<Vec<_>>()
.join("\n");
let statements = Self::split_sql_statements(&cleaned_sql);
tracing::debug!(
"Executing {} statements for migration {}",
statements.len(),
migration.version
);
for (idx, statement) in statements.iter().enumerate() {
if !statement.trim().is_empty() {
tracing::debug!(
"Executing statement {} of {}: {}...",
idx + 1,
statements.len(),
&statement.chars().take(50).collect::<String>()
);
sqlx::query(statement)
.execute(&mut *tx)
.await
.map_err(|e| {
anyhow::anyhow!(
"Failed to execute statement {} in migration {}: {}\nStatement: {}",
idx + 1,
migration.version,
e,
statement
)
})?;
}
}
sqlx::query(&format!(
"INSERT INTO {}._duroxide_migrations (version, name) VALUES ($1, $2)",
self.schema_name
))
.bind(migration.version)
.bind(&migration.name)
.execute(&mut *tx)
.await?;
tx.commit().await?;
tracing::info!(
"Applied migration {}: {}",
migration.version,
migration.name
);
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn migration_search_path_pins_pg_temp_last() {
let stmt = MigrationRunner::migration_search_path_stmt("duroxide");
assert_eq!(stmt, "SET LOCAL search_path TO duroxide, pg_temp");
}
}