Share migration planning and execution across update phases

This commit is contained in:
Дмитрий committed 2026-10-16 11:17:44 +05:00
1 parent cda7875a9a
commit cd3ada8f5b
10 files changed
+191 -62

No files matched your search

@@ -0,0 +1,57 @@
//! Read-only audit, or explicit SQL checks on a disposable database fixture.
use coa_core::{db::{Account, Db}, migrations::{self, Store}, manifest::Manifest, realms::Mode};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let args: Vec<_> = std::env::args().collect();
if args.len() < 3 { return Err("usage: migration_plan <root> <signed-manifest> [--apply|--sql-only <migration-directory>]".into()); }
let root = std::path::Path::new(&args[1]);
let bytes = std::fs::read(&args[2])?;
let signature = std::fs::read_to_string(format!("{}.sig",args[2]))?;
coa_core::signing::verify(&bytes, &signature, coa_core::signing::EMBEDDED_PUBLIC_KEY)?;
let manifest = Manifest::parse(&bytes)?;
for (index,mode) in [Mode::Coa, Mode::Wildcard].into_iter().enumerate() {
let db = Db::from_repack(root, Account::Admin)?.for_realm(mode);
let rows = db.load_readonly()?;
let relevant: Vec<_> = rows.iter().filter(|r| r.id == "rev_20260903_01_live_class_baseline").map(|r| format!("{} {:?} {}",r.db,r.status,r.baseline)).collect();
println!("{} baseline history: {:?}",mode.name(),relevant);
let list: Vec<_> = manifest.migrations.iter().filter(|m|index==0 || m.db!="auth").cloned().collect();
let plan = migrations::status(&db,&list)?;
println!("{} pending: {:?}",mode.name(),plan.iter().filter(|m|m.status!=migrations::Status::Applied).map(|m|(&m.db,&m.id,m.status)).collect::<Vec<_>>());
}
if args.get(3).is_some_and(|s| s == "--apply" || s == "--sql-only") {
assert!(root.file_name().unwrap().to_string_lossy().starts_with("update-database-diagnosis-") && root.join(".coa-update-fixture").is_file(), "Only disposable fixture roots may be changed");
if args[3] == "--sql-only" {
let db = Db::from_repack(root, Account::Admin)?.for_realm(Mode::Coa);
let identity = || -> coa_core::Result<String> {
let mut rows = String::new();
for (schema,table,columns) in [("acore_auth","account","id,HEX(username)"),("acore_characters","characters","guid,account,HEX(name)"),("acore_characters_wildcard","characters","guid,account,HEX(name)")] {
rows.push_str(schema);
rows.push_str(&db.query(&format!("SELECT {columns} FROM {schema}.{table} ORDER BY 1;"))?);
}
Ok(coa_core::fsx::sha256_bytes(rows.as_bytes()))
};
let before = identity()?;
migrations::verify_files(&manifest.migrations,std::path::Path::new(&args[4]))?;
for (index,mode) in [Mode::Coa,Mode::Wildcard].into_iter().enumerate() {
let realm_db = db.clone().for_realm(mode);
let list: Vec<_> = manifest.migrations.iter().filter(|m|index==0 || m.db!="auth").cloned().collect();
let result = migrations::apply_pending(&realm_db,&list,std::path::Path::new(&args[4]),&||Ok("disposable fixture".into()))?;
println!("{} SQL result: {}",mode.name(),serde_json::to_string(&result)?);
if result.failed.is_some() {return Err("SQL failed; fixture preserved".into());}
}
assert_eq!(before,identity()?,"Account or character identity changed");
println!("Account and character identities preserved: {}",before);
let capture = root.join("captured-schema");
coa_core::schema_check::capture(&db,&capture)?;
println!("Captured schema SHA256: {}",coa_core::fsx::sha256_file(&capture.join(coa_core::schema_check::CONTRACT))?);
return Ok(());
}
if args.len() != 5 { return Err("Migration directory required".into()); }
use coa_core::update::{Env, RepackEnv};
let meta = coa_core::registry::metadata_dir_for(root)?;
let env = RepackEnv { root, meta_dir: &meta };
let result = env.migrate(&manifest, std::path::Path::new(&args[4]));
let _ = coa_core::driver::run(root, coa_core::driver::Verb::StopAll);
println!("Application result: {}",serde_json::to_string(&result?)?);
}
Ok(())
}
@@ -0,0 +1,19 @@
//! Exercise the production updater only on a disposable copy of an installed server.
use coa_core::{registry::MetaDir, update::{self, Env, RepackEnv}};
fn main() -> Result<(),Box<dyn std::error::Error>> {
let args:Vec<_>=std::env::args().collect();
if args.len()!=3 {return Err("usage: upgrade_fixture <disposable-root> <signed-package-directory>".into());}
let root=std::path::Path::new(&args[1]);
assert!(root.file_name().unwrap().to_string_lossy().starts_with("coa-schema-fixture-real") && root.join(".coa-update-fixture").is_file(),"Disposable fixture required");
let meta=coa_core::registry::metadata_dir_for(root)?;
let _=MetaDir::open(&meta)?;
let env=RepackEnv{root,meta_dir:&meta};
let outcome=update::apply(&update::Params{root,meta_dir:&meta,source:coa_core::pkgsource::Source::Dir(args[2].clone().into()),trusted_key:coa_core::signing::EMBEDDED_PUBLIC_KEY,cancel:Default::default(),resolutions:Default::default(),env:&env,fail_after_ops:None},&|phase,percent|println!("{percent}% {phase}"));
let stopped=coa_core::driver::run(root,coa_core::driver::Verb::StopAll);
println!("Shutdown after test: {:?}",stopped.map(|r|r.ok));
let outcome=outcome?;
println!("Final transaction: {:?}",outcome.txn.state);
env.ensure_stopped()?;
assert_eq!(outcome.txn.state,update::State::Committed);
Ok(())
}
+36 -6
View File
@@ -183,8 +183,7 @@ pub fn preflight(store: &dyn Store, list: &[Migration]) -> Result<()> {
pub fn pending_count(store: &dyn Store, list: &[Migration]) -> Result<usize> {
let rows = store.load_readonly()?;
check_history(&rows, list)?;
Ok(list.iter().filter(|m| recorded(&rows, m).is_none_or(|r| r.status != Status::Applied)).count())
Ok(pending_migrations(&rows, list)?.len())
}
fn verified_file(m: &Migration, dir: &Path) -> Result<(&'static str, std::path::PathBuf, bool)> {
@@ -199,15 +198,30 @@ fn verified_file(m: &Migration, dir: &Path) -> Result<(&'static str, std::path::
/// Check every migration artifact, including applied history, before replacing server files.
pub fn verify_files(list: &[Migration], dir: &Path) -> Result<()> {
validate_list(list)?;
for m in list { verified_file(m, dir)?; }
Ok(())
}
fn validate_list(list: &[Migration]) -> Result<()> {
let mut seen = std::collections::BTreeSet::new();
for m in list {
if !seen.insert((&m.db, &m.id)) { return Err(Error::InvalidManifest(format!("duplicate migration {} ({})", m.id, m.db))); }
verified_file(m, dir)?;
if !ident_ok(&m.id) { return Err(Error::InvalidManifest(format!("migration id {:?} is not allowed", m.id))); }
let schema = db::schema_of(&m.db)?;
if !seen.insert((schema, &m.id)) {
return Err(Error::InvalidManifest(format!("duplicate migration {} ({})", m.id, m.db)));
}
}
Ok(())
}
fn pending_migrations<'a>(rows: &[LedgerRow], list: &'a [Migration]) -> Result<Vec<&'a Migration>> {
check_history(rows, list)?;
Ok(list.iter().filter(|m| recorded(rows, m).is_none_or(|r| r.status != Status::Applied)).collect())
}
fn check_history(rows: &[LedgerRow], list: &[Migration]) -> Result<()> {
validate_list(list)?;
for m in list {
if recorded(rows, m).is_some_and(|r| matches!(r.status, Status::Running | Status::Failed)) {
return Err(Error::Invalid(format!("Database update {} ({}) failed or was interrupted and may be partially applied. Restore its recovery point before retrying.", m.id, m.db)));
@@ -246,8 +260,7 @@ pub struct ApplyReport {
pub fn apply_pending(store: &dyn Store, list: &[Migration], dir: &Path, snapshot: &dyn Fn() -> Result<String>) -> Result<ApplyReport> {
let rows = store.load()?;
// The list order is the release's order of application (released updates, then pending, then modules).
check_history(&rows, list)?;
let todo: Vec<&Migration> = list.iter().filter(|m| recorded(&rows, m).is_none_or(|r| r.status != Status::Applied)).collect();
let todo = pending_migrations(&rows, list)?;
tracing::info!(total = list.len(), pending = todo.len(), skipped = list.len() - todo.len(), "database migration plan");
// Resolve and verify every file before running anything.
@@ -368,6 +381,23 @@ mod tests {
assert!(store.ran.borrow().is_empty());
}
#[test]
fn every_entry_point_rejects_duplicate_sql_before_execution() {
let (dir, original) = setup(&[("world", "once", "SELECT 1;\n")]);
for alias in ["world", "coa-world"] {
let mut list = original.clone();
let mut duplicate = original[0].clone();
duplicate.db = alias.into();
list.push(duplicate);
let store = Mem::default();
assert!(preflight(&store, &list).is_err());
assert!(pending_count(&store, &list).is_err());
assert!(apply_pending(&store, &list, dir.path(), &no_snapshot).is_err());
assert!(store.ran.borrow().is_empty());
assert!(store.rows.borrow().is_empty());
}
}
#[test]
fn staged_files_reject_missing_corrupt_duplicate_and_invalid_migrations() {
let (dir, list) = setup(&[("characters", "repair", "SELECT 1;\n")]);
+41 -54
View File
@@ -1010,16 +1010,9 @@ impl Env for RepackEnv<'_> {
step(report, "Testing database updates on the private server copy", 56);
let identities = crate::backup::with_database(&fixture, |db| {
let identities = database_identities(db)?;
let realms = crate::realms::state(&fixture)?;
let mut modes = vec![realms.active];
if realms.wildcard_created { modes.push(if realms.active == crate::realms::Mode::Coa { crate::realms::Mode::Wildcard } else { crate::realms::Mode::Coa }); }
for (index, mode) in modes.into_iter().enumerate() {
let db = db.clone().for_realm(mode);
crate::schema_check::repair_missing_defaults(&db, &fixture, manifest.files.iter().find(|file| file.path == crate::schema_check::CONTRACT).map(|file| file.sha256.as_str()))?;
let list: Vec<_> = manifest.migrations.iter().filter(|migration| index == 0 || migration.db != "auth").cloned().collect();
let report = crate::migrations::apply_pending(&db, &list, &tree.join("_migrations"), &|| Ok("private rehearsal".into()))?;
if let Some((migration, error)) = report.failed { return Err(Error::Invalid(format!("Rehearsal migration {migration} failed: {error}"))); }
crate::schema_check::repair_missing_defaults(&db, &fixture, manifest.files.iter().find(|file| file.path == crate::schema_check::CONTRACT).map(|file| file.sha256.as_str()))?;
let result = migrate_databases(db, &fixture, manifest, &tree.join("_migrations"), &|| Ok("private rehearsal".into()))?;
if let Some((migration, error)) = result.failed {
return Err(Error::Invalid(format!("Rehearsal migration {migration} failed: {error}")));
}
verify_database_identities(db, &identities)?;
Ok(identities)
@@ -1126,44 +1119,8 @@ impl Env for RepackEnv<'_> {
}
fn migrate(&self, manifest: &Manifest, staged: &Path) -> Result<ApplyReport> {
let root = self.root;
crate::backup::with_database(root, |db| {
let realms = crate::realms::state(root)?;
if let Some(contract) = manifest.files.iter().find(|file| file.path == crate::schema_check::CONTRACT) {
self.migration_snapshot()?;
crate::schema_check::repair_missing_defaults(db, root, Some(&contract.sha256))?;
}
let mut result = crate::migrations::apply_pending(db, &manifest.migrations, staged, &|| {
self.migration_snapshot()
})?;
if realms.wildcard_created && result.failed.is_none() {
let other = if realms.active == crate::realms::Mode::Coa { crate::realms::Mode::Wildcard } else { crate::realms::Mode::Coa };
let other_db = db.clone().for_realm(other);
crate::schema_check::repair_missing_defaults(&other_db, root, manifest.files.iter().find(|file| file.path == crate::schema_check::CONTRACT).map(|file| file.sha256.as_str()))?;
let shared_snapshot = result.snapshot.clone();
let migrations: Vec<_> = manifest.migrations.iter().filter(|m| m.db != "auth").cloned().collect();
let extra = crate::migrations::apply_pending(&other_db, &migrations, staged, &|| {
if let Some(id) = &shared_snapshot { return Ok(id.clone()); }
self.migration_snapshot()
})?;
result.applied.extend(extra.applied);
result.failed = extra.failed;
if result.snapshot.is_none() { result.snapshot = extra.snapshot; }
if result.failed.is_none() {
crate::schema_check::repair_missing_defaults(&other_db, root, manifest.files.iter().find(|file| file.path == crate::schema_check::CONTRACT).map(|file| file.sha256.as_str()))?;
if let Some(p) = crate::schema_check::check_with_report(&other_db, root)?.first() {
return Err(Error::Invalid(format!("Database validation failed on {}: {}.{}: {}", other.name(), p.table, p.column, p.detail)));
}
}
}
if result.failed.is_none() {
crate::schema_check::repair_missing_defaults(db, root, manifest.files.iter().find(|file| file.path == crate::schema_check::CONTRACT).map(|file| file.sha256.as_str()))?;
let problems = crate::schema_check::check_with_report(db, root)?;
if let Some(p) = problems.first() {
return Err(Error::Invalid(format!("Database validation failed: {}.{}.{}: {} ({} problems).", p.database, p.table, p.column, p.detail, problems.len())));
}
}
Ok(result)
crate::backup::with_database(self.root, |db| {
migrate_databases(db, self.root, manifest, staged, &|| self.migration_snapshot())
})
}
@@ -1178,8 +1135,7 @@ impl Env for RepackEnv<'_> {
impl RepackEnv<'_> {
fn validate_server(&self, check_schema: bool) -> Result<()> {
let stopped = crate::driver::run(self.root, crate::driver::Verb::StopAll)?;
if !stopped.ok { return Err(Error::Invalid("Could not stop the server before isolated validation.".into())); }
crate::mysql_snapshot::stop(self.root)?;
self.ensure_stopped()?;
self.recover_validation()?;
let result = (|| -> Result<()> {
@@ -1239,10 +1195,7 @@ impl RepackEnv<'_> {
let _ = crate::driver::run(self.root, crate::driver::Verb::StopAll);
Err(Error::Invalid(cause))
})();
let stopped = crate::driver::run(self.root, crate::driver::Verb::StopAll)?;
if !stopped.ok {
return Err(Error::Invalid("Isolated validation could not stop the server; recovery configurations were preserved.".into()));
}
crate::mysql_snapshot::stop(self.root)?;
self.ensure_stopped()?;
self.recover_validation()?;
result
@@ -1250,6 +1203,40 @@ impl RepackEnv<'_> {
}
/// Rehearsal and installation use the same database order, history checks and SQL executor.
fn migrate_databases(db: &crate::db::Db, root: &Path, manifest: &Manifest, staged: &Path, snapshot: &dyn Fn() -> Result<String>) -> Result<ApplyReport> {
let realms = crate::realms::state(root)?;
let mut modes = vec![realms.active];
if realms.wildcard_created {
modes.push(if realms.active == crate::realms::Mode::Coa { crate::realms::Mode::Wildcard } else { crate::realms::Mode::Coa });
}
// Reject a broken history in either realm before changing the first one.
for (index, mode) in modes.iter().enumerate() {
let list: Vec<_> = manifest.migrations.iter().filter(|m| index == 0 || m.db != "auth").cloned().collect();
crate::migrations::preflight(&db.clone().for_realm(*mode), &list)?;
}
let contract = manifest.files.iter().find(|file| file.path == crate::schema_check::CONTRACT).map(|file| file.sha256.as_str());
let mut result = ApplyReport { applied: vec![], failed: None, snapshot: None };
if contract.is_some() { result.snapshot = Some(snapshot()?); }
for (index, mode) in modes.into_iter().enumerate() {
let db = db.clone().for_realm(mode);
let list: Vec<_> = manifest.migrations.iter().filter(|m| index == 0 || m.db != "auth").cloned().collect();
crate::schema_check::repair_missing_defaults(&db, root, contract)?;
let extra = crate::migrations::apply_pending(&db, &list, staged, &|| {
if let Some(id) = &result.snapshot { Ok(id.clone()) } else { snapshot() }
})?;
result.applied.extend(extra.applied);
result.failed = extra.failed;
if result.snapshot.is_none() { result.snapshot = extra.snapshot; }
if result.failed.is_some() { return Ok(result); }
crate::schema_check::repair_missing_defaults(&db, root, contract)?;
if let Some(p) = crate::schema_check::check_with_report(&db, root)?.first() {
return Err(Error::Invalid(format!("Database validation failed on {}: {}.{}: {}", mode.name(), p.table, p.column, p.detail)));
}
}
Ok(result)
}
fn database_identities(db: &crate::db::Db) -> Result<BTreeMap<String, std::collections::BTreeSet<String>>> {
let db = db.clone().for_realm(crate::realms::Mode::Coa);
let mut identities = BTreeMap::new();