From 9ae5fb471b18a90f2fe98650b25780574c776522 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=BC=D0=B8=D1=82=D1=80=D0=B8=D0=B9?= Date: Sat, 10 Oct 2026 23:34:29 +0500 Subject: [PATCH] Rehearse Windows updates and preserve complete MySQL recovery --- .gitignore | 2 + ...20261010-complete-mysql-update-recovery.md | 7 + .../20261010-missing-database-defaults.md | 7 + ...61010-private-database-update-rehearsal.md | 7 + .../coa-core/examples/update_recovery_e2e.rs | 7 +- crates/coa-core/src/backup.rs | 16 + crates/coa-core/src/diag.rs | 2 + crates/coa-core/src/lib.rs | 1 + crates/coa-core/src/mysql_snapshot.rs | 474 ++++++++++++++++++ crates/coa-core/src/release_schema.rs | 3 +- crates/coa-core/src/schema_check.rs | 49 ++ crates/coa-core/src/update.rs | 173 ++++++- src/lib/api.ts | 1 + src/screens/BackupsPage.tsx | 2 +- tools/make-fixture.ps1 | 6 +- 15 files changed, 746 insertions(+), 11 deletions(-) create mode 100644 changelog.d/20261010-complete-mysql-update-recovery.md create mode 100644 changelog.d/20261010-missing-database-defaults.md create mode 100644 changelog.d/20261010-private-database-update-rehearsal.md create mode 100644 crates/coa-core/src/mysql_snapshot.rs diff --git a/.gitignore b/.gitignore index 42ca83f..56ac59b 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,5 @@ dist *.log .env src-tauri/gen +# Disposable database fixtures and private recovery data +.cache/ diff --git a/changelog.d/20261010-complete-mysql-update-recovery.md b/changelog.d/20261010-complete-mysql-update-recovery.md new file mode 100644 index 0000000..8f6478d --- /dev/null +++ b/changelog.d/20261010-complete-mysql-update-recovery.md @@ -0,0 +1,7 @@ +--- +area: manager +type: fixed +audience: admins +title: Windows update recovery preserves database procedures triggers and views +--- +A verified copy of stopped MySQL restores all databases and accounts together. Interrupted restoration can be retried, and the replaced database directory is retained. diff --git a/changelog.d/20261010-missing-database-defaults.md b/changelog.d/20261010-missing-database-defaults.md new file mode 100644 index 0000000..c612fc6 --- /dev/null +++ b/changelog.d/20261010-missing-database-defaults.md @@ -0,0 +1,7 @@ +--- +area: manager +type: fixed +audience: admins +title: Updates repair missing database defaults from the verified release schema +--- +Only missing literal defaults are restored when column properties match. Existing rows, custom defaults, types and nullability are preserved; unresolved incompatibilities still block the update. diff --git a/changelog.d/20261010-private-database-update-rehearsal.md b/changelog.d/20261010-private-database-update-rehearsal.md new file mode 100644 index 0000000..4cdf780 --- /dev/null +++ b/changelog.d/20261010-private-database-update-rehearsal.md @@ -0,0 +1,7 @@ +--- +area: manager +type: added +audience: admins +title: Windows server updates are tested on a private copy before changing your installation +--- +The Manager tests migrations and server startup using your databases and configurations. A failed rehearsal leaves the installed server unchanged and preserves a diagnostic report. diff --git a/crates/coa-core/examples/update_recovery_e2e.rs b/crates/coa-core/examples/update_recovery_e2e.rs index 7e24705..fe29875 100644 --- a/crates/coa-core/examples/update_recovery_e2e.rs +++ b/crates/coa-core/examples/update_recovery_e2e.rs @@ -27,6 +27,7 @@ fn exercise(root: &Path) -> Result<()> { MetaDir::create(root, &installed)?; backup::with_database(root, |db| { db.query("CREATE TABLE acore_characters.manager_recovery_probe (id INT); INSERT INTO acore_characters.manager_recovery_probe VALUES (42); CREATE TABLE acore_world.manager_recovery_probe (id INT); INSERT INTO acore_world.manager_recovery_probe VALUES (43);")?; + db.query("CREATE VIEW acore_characters.manager_recovery_view AS SELECT id FROM acore_characters.manager_recovery_probe; CREATE TRIGGER acore_characters.manager_recovery_trigger BEFORE INSERT ON acore_characters.manager_recovery_probe FOR EACH ROW SET NEW.id=NEW.id+1; CREATE PROCEDURE acore_characters.manager_recovery_proc() SELECT id FROM acore_characters.manager_recovery_probe;")?; Ok(()) })?; let source = root.join("fixture-update-source"); @@ -54,13 +55,15 @@ fn exercise(root: &Path) -> Result<()> { cancel: Cancel::default(), resolutions: BTreeMap::new(), env: &env, fail_after_ops: None, }, &|step, pct| println!("{pct}% {step}" )).unwrap_err(); assert!(error.to_string().contains("probe_world"), "{error}"); - assert!(error.to_string().contains("were restored"), "{error}"); + assert!(error.to_string().contains("private update rehearsal failed"), "{error}"); assert!(!root.join("Core/recovery-probe.txt").exists()); assert!(update::unfinished(&meta).is_none()); assert_eq!(MetaDir::open(&meta)?.1.core.version.as_deref(), Some("1.0.0")); backup::with_database(root, |db| { assert_eq!(db.query("SELECT id FROM acore_characters.manager_recovery_probe;")?, "42"); assert_eq!(db.query("SELECT id FROM acore_world.manager_recovery_probe;")?, "43"); + assert_eq!(db.query("SELECT id FROM acore_characters.manager_recovery_view;")?, "42"); + assert_eq!(db.recovery_objects("acore_characters")?.iter().filter(|object| object["name"].as_str().is_some_and(|name| name.starts_with("manager_recovery_"))).count(), 3); assert!(!coa_core::migrations::status(db, &manifest.migrations)?.iter().any(|m| m.status == coa_core::migrations::Status::Applied)); Ok(()) })?; @@ -68,6 +71,6 @@ fn exercise(root: &Path) -> Result<()> { let point = backup::list(&meta).into_iter().find(|p| p.trigger == backup::Trigger::BeforeUpdate).ok_or_else(|| Error::Invalid("missing backup".into()))?; env.restore_snapshot(&point.id)?; assert_eq!(Db::from_repack(root, Account::Admin)?.realm(), coa_core::realms::Mode::Coa); - println!("PASS: failed SQL restored world, characters, ledger, files and version; repeated recovery succeeded"); + println!("PASS: private rehearsal caught failed SQL before changing the installed world, characters, ledger, objects, files or version; complete recovery succeeded"); Ok(()) } diff --git a/crates/coa-core/src/backup.rs b/crates/coa-core/src/backup.rs index 3914f23..51e68e4 100644 --- a/crates/coa-core/src/backup.rs +++ b/crates/coa-core/src/backup.rs @@ -67,6 +67,8 @@ pub struct RecoveryPoint { pub components: Vec, #[serde(default)] pub realm: crate::realms::Mode, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub(crate) mysql_snapshot: Option, } const LOCATION_FILE: &str = "backup-location.json"; @@ -334,6 +336,16 @@ pub fn create(root: &Path, meta: &Path, kind: Kind, trigger: Trigger, label: Opt return Err(e); } }; + let mysql_snapshot = if kind == Kind::Full && trigger == Trigger::BeforeUpdate && cfg!(windows) && !crate::docker::is_docker(root) { + progress("Saving a complete stopped MySQL recovery copy"); + match crate::mysql_snapshot::capture(root, &partial) { + Ok(snapshot) => Some(snapshot), + Err(error) => { + if let Ok(owned) = fsx::ensure_within(&backups_dir(meta), &partial) { let _ = fs::remove_dir_all(owned); } + return Err(error); + } + } + } else { None }; let point = RecoveryPoint { realm: crate::realms::state(root)?.active, schema: 1, @@ -344,6 +356,7 @@ pub fn create(root: &Path, meta: &Path, kind: Kind, trigger: Trigger, label: Opt created_at: chrono::Utc::now().to_rfc3339(), manager_version: crate::MANAGER_VERSION.into(), components, + mysql_snapshot, }; fsx::atomic_write_json(&partial.join("backup.json"), &point)?; fs::rename(&partial, &final_dir)?; @@ -375,6 +388,9 @@ pub fn verify(meta: &Path, id: &str) -> Result { let point = get(meta, id)?; let dir = point_dir(meta, id)?; let mut problems = Vec::new(); + if let Some(snapshot) = &point.mysql_snapshot { + if let Err(error) = crate::mysql_snapshot::verify(&dir, snapshot) { problems.push(error.to_string()); } + } let mut names = std::collections::BTreeSet::new(); let mut schemas = std::collections::BTreeSet::new(); for c in &point.components { diff --git a/crates/coa-core/src/diag.rs b/crates/coa-core/src/diag.rs index be21f31..a5ad4e8 100644 --- a/crates/coa-core/src/diag.rs +++ b/crates/coa-core/src/diag.rs @@ -405,6 +405,8 @@ pub fn export_package(root: &Path, meta_dir: &Path, manager_log: &Path, meta: &I .into_iter().filter(|(path, _, _)| path.file_name().is_some_and(|name| { let name = name.to_string_lossy(); name.starts_with("schema-validation-") || name.starts_with("database-recovery-") + || name.starts_with("database-rehearsal-") || name.starts_with("database-restore-") + || name.starts_with("database-repair-") })).take(10) { if size <= 4 * 1024 * 1024 { if let Ok(text) = fs::read_to_string(&path) { diff --git a/crates/coa-core/src/lib.rs b/crates/coa-core/src/lib.rs index 745faba..3476ebf 100644 --- a/crates/coa-core/src/lib.rs +++ b/crates/coa-core/src/lib.rs @@ -45,6 +45,7 @@ pub mod report; pub mod release_schema; pub mod update; mod update_isolation; +mod mysql_snapshot; pub mod wine; pub mod upnp; pub mod signing; diff --git a/crates/coa-core/src/mysql_snapshot.rs b/crates/coa-core/src/mysql_snapshot.rs new file mode 100644 index 0000000..bed8658 --- /dev/null +++ b/crates/coa-core/src/mysql_snapshot.rs @@ -0,0 +1,474 @@ +use std::{collections::{BTreeMap, BTreeSet}, fs, path::{Path, PathBuf}}; +use std::io::{BufReader, BufWriter, Write}; +use serde::{Deserialize, Serialize}; +use crate::{fsx, Error, Result}; + +#[derive(Debug, Clone, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +pub(crate) struct Snapshot { + pub path: String, + pub files: BTreeMap, + pub directories: BTreeSet, + pub server_sha256: String, + #[serde(default)] + pub bytes: u64, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub archive_sha256: Option, + #[serde(default)] + pub expanded_bytes: u64, +} + +fn reject_link(metadata: &fs::Metadata) -> Result<()> { + let linked = metadata.file_type().is_symlink(); + #[cfg(windows)] let linked = { use std::os::windows::fs::MetadataExt; linked || metadata.file_attributes() & 0x400 != 0 }; + if linked { return Err(Error::Invalid("Recovery copies cannot follow symbolic links or junctions.".into())); } + Ok(()) +} + +fn directories(root: &Path) -> Result> { + let mut result = BTreeSet::new(); + let mut pending = vec![root.to_path_buf()]; + while let Some(directory) = pending.pop() { + for entry in fs::read_dir(directory)? { + let entry = entry?; + reject_link(&fs::symlink_metadata(entry.path())?)?; + let path = fsx::ensure_within(root, &entry.path())?; + if entry.file_type()?.is_dir() { + let relative = path.strip_prefix(root).map_err(|e| Error::Invalid(e.to_string()))?.to_string_lossy().replace('\\', "/"); + result.insert(relative); + pending.push(path); + } + } + } + Ok(result) +} + +fn copy_snapshot(point: &Path, snapshot: &Snapshot, target: &Path) -> Result<()> { + if snapshot.archive_sha256.is_some() { + verify(point, snapshot)?; + if target.exists() { return Err(Error::Invalid("The recovery destination already exists.".into())); } + let parent = target.parent().ok_or_else(|| Error::Invalid("Invalid recovery destination.".into()))?; + fs::create_dir_all(parent)?; + fsx::require_space(parent, snapshot.expanded_bytes)?; + fs::create_dir(target)?; + let input = fsx::ensure_within(point, &fsx::safe_join(point, &snapshot.path)?)?; + let decoder = zstd::Decoder::new(BufReader::new(fs::File::open(input)?))?; + let mut archive = tar::Archive::new(decoder); + let mut seen = BTreeSet::new(); + let mut expanded = 0u64; + for entry in archive.entries()? { + let mut entry = entry?; + let name = entry.path()?.to_string_lossy().trim_end_matches('/').to_string(); + if !seen.insert(name.clone()) { return Err(Error::Invalid("Duplicate entry in MySQL recovery archive.".into())); } + let output = fsx::ensure_within(target, &fsx::safe_join(target, &name)?)?; + if entry.header().entry_type().is_dir() && snapshot.directories.contains(&name) { + fs::create_dir_all(output)?; + } else if entry.header().entry_type().is_file() && snapshot.files.contains_key(&name) { + expanded = expanded.checked_add(entry.size()).filter(|bytes| *bytes <= snapshot.expanded_bytes) + .ok_or_else(|| Error::Invalid("MySQL recovery archive exceeds its declared size.".into()))?; + fs::create_dir_all(output.parent().unwrap())?; + let mut file = fs::OpenOptions::new().write(true).create_new(true).open(output)?; + if std::io::copy(&mut entry, &mut file)? != entry.size() { return Err(Error::Invalid("Truncated MySQL recovery file.".into())); } + file.flush()?; + } else { return Err(Error::Invalid("Unexpected entry in MySQL recovery archive.".into())); } + } + if expanded != snapshot.expanded_bytes || inventory(target)? != snapshot.files { + return Err(Error::Invalid("MySQL recovery files failed verification.".into())); + } + } else { copy_verified(&point.join(&snapshot.path), target, &snapshot.files)?; } + for directory in &snapshot.directories { + fs::create_dir_all(fsx::ensure_within(target, &fsx::safe_join(target, directory)?)?)?; + } + if directories(target)? != snapshot.directories { return Err(Error::Invalid("The recovery directory inventory differs.".into())); } + Ok(()) +} + +fn compress(source: &Path, point: &Path, snapshot: &mut Snapshot) -> Result<()> { + snapshot.path = "mysql-data.tar.zst".into(); + let path = fsx::safe_join(point, &snapshot.path)?; + fs::create_dir_all(point)?; + fsx::require_space(point, snapshot.expanded_bytes)?; + let file = fs::OpenOptions::new().write(true).create_new(true).open(&path)?; + let encoder = zstd::Encoder::new(BufWriter::new(file), 3)?; + let mut archive = tar::Builder::new(encoder); + for directory in &snapshot.directories { + archive.append_dir(directory, fsx::ensure_within(source, &fsx::safe_join(source, directory)?)?)?; + } + for name in snapshot.files.keys() { + let input = fsx::ensure_within(source, &fsx::safe_join(source, name)?)?; + archive.append_file(name, &mut fs::File::open(input)?)?; + } + archive.into_inner()?.finish()?.flush()?; + snapshot.archive_sha256 = Some(fsx::sha256_file(&path)?); + snapshot.bytes = fs::metadata(path)?.len(); + Ok(()) +} + +pub(crate) fn inventory(root: &Path) -> Result> { + let mut files = BTreeMap::new(); + let mut directories = vec![root.to_path_buf()]; + while let Some(directory) = directories.pop() { + for entry in fs::read_dir(directory)? { + let entry = entry?; + let metadata = fs::symlink_metadata(entry.path())?; + let path = fsx::ensure_within(root, &entry.path())?; + reject_link(&metadata)?; + if metadata.is_dir() { directories.push(path); continue; } + if !metadata.is_file() { return Err(Error::Invalid("Unsupported database snapshot file.".into())); } + let relative = path.strip_prefix(root).map_err(|e| Error::Invalid(e.to_string()))?.to_string_lossy().replace('\\', "/"); + fsx::safe_join(root, &relative)?; + if relative.ends_with(".isl") { return Err(Error::Invalid("External MySQL tablespaces require a separate recovery method.".into())); } + files.insert(relative, fsx::sha256_file(&path)?); + } + } + Ok(files) +} + +pub(crate) fn copy_verified(source: &Path, target: &Path, files: &BTreeMap) -> Result<()> { + if target.exists() { return Err(Error::Invalid("Database snapshot destination already exists.".into())); } + let bytes = files.keys().try_fold(0u64, |total, name| -> Result { + let path = fsx::ensure_within(source, &fsx::safe_join(source, name)?)?; + total.checked_add(fs::metadata(path)?.len()).ok_or_else(|| Error::Invalid("Snapshot size overflow.".into())) + })?; + let parent = target.parent().ok_or_else(|| Error::Invalid("Invalid snapshot destination.".into()))?; + fs::create_dir_all(parent)?; + fsx::require_space(parent, bytes)?; + fs::create_dir(target)?; + for (relative, expected) in files { + let source_file = fsx::ensure_within(source, &fsx::safe_join(source, relative)?)?; + let target_file = fsx::ensure_within(target, &fsx::safe_join(target, relative)?)?; + fs::create_dir_all(target_file.parent().unwrap())?; + fs::copy(&source_file, &target_file)?; + if fsx::sha256_file(&target_file)? != *expected { return Err(Error::Invalid(format!("Database snapshot file {relative} failed verification."))); } + } + if inventory(target)? != *files { return Err(Error::Invalid("Database snapshot inventory differs.".into())); } + Ok(()) +} + +pub(crate) fn stop(root: &Path) -> Result<()> { + let observed = crate::process::observe(root, &crate::layout::read_ports(root)); + if [observed.mysql.state, observed.auth.state, observed.world.state].iter().all(|state| *state == crate::process::ServiceState::Stopped) + && !crate::multiworld::is_running(root) { return Ok(()); } + let outcome = crate::driver::run(root, crate::driver::Verb::StopAll)?; + let status = crate::process::observe(root, &crate::layout::read_ports(root)); + if !outcome.ok || [status.mysql.state, status.auth.state, status.world.state].iter().any(|state| *state != crate::process::ServiceState::Stopped) + || crate::multiworld::is_running(root) { + return Err(Error::Invalid("The complete server must be stopped before copying or restoring MySQL.".into())); + } + Ok(()) +} + +pub(crate) fn stop_previous_rehearsals(meta: &Path) -> Result<()> { + let location = meta.join("rehearsals"); + if !location.exists() { return Ok(()); } + for entry in fs::read_dir(&location)? { + let entry = entry?; + let name = entry.file_name().to_string_lossy().into_owned(); + if name.len() != 32 || !name.bytes().all(|byte| byte.is_ascii_hexdigit()) { continue; } + let fixture = fsx::ensure_within(&location, &entry.path().join("server"))?; + let metadata = crate::registry::metadata_dir_for(&fixture)?; + if !metadata.join("install.json").is_file() { continue; } + let (_, installed) = crate::registry::MetaDir::open(&metadata)?; + if fsx::canonicalize_lenient(Path::new(&installed.server_path))? != fsx::canonicalize_lenient(&fixture)? { + return Err(Error::Invalid("The interrupted rehearsal metadata does not match its private server.".into())); + } + stop(&fixture)?; + } + Ok(()) +} + +pub(crate) fn capture(root: &Path, partial: &Path) -> Result { + if crate::docker::is_docker(root) || !cfg!(windows) { return Err(Error::Invalid("Cold MySQL snapshots require a Windows repack.".into())); } + crate::backup::with_database(root, |db| { + if db.query("SELECT COUNT(*) FROM performance_schema.replication_connection_configuration;")?.trim() != "0" { + return Err(Error::Invalid("A replicated MySQL instance requires a separate isolated recovery method.".into())); + } + let data = fsx::ensure_within(root, &root.join("mysql/data"))?; + for path in db.query("SELECT PATH FROM information_schema.INNODB_DATAFILES;")?.lines() { + fsx::ensure_within(&data, &data.join(path))?; + } + Ok(()) + })?; + stop(root)?; + let data = fsx::ensure_within(root, &root.join("mysql/data"))?; + if !data.join("mysql").is_dir() || !data.join("ibdata1").is_file() { + return Err(Error::Invalid("The bundled MySQL data directory is incomplete.".into())); + } + let files = inventory(&data)?; + let bytes = files.keys().try_fold(0u64, |total, file| -> Result { + total.checked_add(fs::metadata(fsx::safe_join(&data, file)?)?.len()).ok_or_else(|| Error::Invalid("Snapshot size overflow.".into())) + })?; + let mut snapshot = Snapshot { path: "mysql-data".into(), files, directories:directories(&data)?, server_sha256: fsx::sha256_file(&root.join("mysql/bin/mysqld.exe"))?, bytes, expanded_bytes:bytes, archive_sha256:None }; + compress(&data, partial, &mut snapshot)?; + verify(partial, &snapshot)?; + Ok(snapshot) +} + +pub(crate) fn verify(point: &Path, snapshot: &Snapshot) -> Result<()> { + if snapshot.files.is_empty() || snapshot.server_sha256.len() != 64 { + return Err(Error::Invalid("Invalid cold MySQL recovery inventory.".into())); + } + let data = fsx::ensure_within(point, &fsx::safe_join(point, &snapshot.path)?)?; + if let Some(hash) = &snapshot.archive_sha256 { + if snapshot.path != "mysql-data.tar.zst" || snapshot.expanded_bytes == 0 || fsx::sha256_file(&data)? != *hash { + return Err(Error::Invalid("The compressed MySQL recovery copy is damaged.".into())); + } + for name in snapshot.files.keys().chain(snapshot.directories.iter()) { fsx::safe_join(point, name)?; } + return Ok(()); + } + if snapshot.path != "mysql-data" { return Err(Error::Invalid("Unsupported MySQL recovery format.".into())); } + if inventory(&data)? != snapshot.files { return Err(Error::Invalid("The cold MySQL recovery copy is damaged or incomplete.".into())); } + if directories(&data)? != snapshot.directories { return Err(Error::Invalid("The cold MySQL recovery directories are incomplete.".into())); } + Ok(()) +} + +pub(crate) fn rehearsal_copy(root: &Path, meta: &Path, point: &Path, snapshot: &Snapshot) -> Result { + verify(point, snapshot)?; + let destination = meta.join("rehearsals").join(uuid::Uuid::new_v4().simple().to_string()).join("server"); + let mut files = BTreeMap::new(); + let mut directories = vec![root.to_path_buf()]; + while let Some(directory) = directories.pop() { + for entry in fs::read_dir(directory)? { + let entry = entry?; + let raw = entry.path(); + let relative = raw.strip_prefix(root).map_err(|e| Error::Invalid(e.to_string()))?.to_string_lossy().replace('\\', "/"); + let lower = relative.to_ascii_lowercase(); + let first = lower.split('/').next().unwrap(); + if entry.file_type()?.is_dir() && !relative.contains('/') + && !["core", "data", "scripts", "settings", "runtime", "mysql", ".realms", "bugreport", "extras", "licenses"].contains(&first) { continue; } + if lower == "mysql/data" || lower.starts_with("mysql/data-") + || lower.split('/').any(|part| part == ".state") + || lower == "bugreport/reports" || lower == "core/logs" || lower == "mysql/logs" { continue; } + let metadata = fs::symlink_metadata(&raw)?; + reject_link(&metadata)?; + let path = fsx::ensure_within(root, &raw)?; + if metadata.is_dir() { directories.push(path); } + else if metadata.is_file() { files.insert(relative, fsx::sha256_file(&path)?); } + else { return Err(Error::Invalid("Unsupported rehearsal file.".into())); } + } + } + copy_verified(root, &destination, &files)?; + copy_snapshot(point, snapshot, &destination.join("mysql/data"))?; + let (_, mut installed) = crate::registry::MetaDir::open(meta)?; + installed.id = uuid::Uuid::new_v4().to_string(); + installed.server_path = destination.to_string_lossy().into_owned(); + installed.client_path = None; + crate::registry::MetaDir::create(&destination, &installed)?; + Ok(destination) +} + +pub(crate) fn isolate_connections(root: &Path) -> Result<()> { + for relative in ["Settings/worldserver.conf.template", "Settings/authserver.conf.template", "Core/configs/worldserver.conf", "Core/configs/authserver.conf"] { + let path = fsx::ensure_within(root, &root.join(relative))?; + if !path.is_file() { + if relative.ends_with(".template") { return Err(Error::Invalid("The private update copy lacks connection templates.".into())); } + continue; + } + let mut config = crate::config::parser::ConfFile::parse_bytes(&fs::read(&path)?)?; + for key in ["LoginDatabaseInfo", "WorldDatabaseInfo", "CharacterDatabaseInfo"] { + if relative.contains("worldserver") || key == "LoginDatabaseInfo" { + config.set(key, &format!("\"@{key}@\""), &[]); + } + } + config.set("BindIP", "\"127.0.0.1\"", &[]); + if relative.contains("worldserver") { + config.set("Ra.IP", "\"127.0.0.1\"", &[]); + config.set("DataDir", "\"@DATA@\"", &[]); + } + fsx::atomic_write(&path, config.to_text().as_bytes())?; + } + Ok(()) +} + +pub(crate) fn unchanged(root: &Path, snapshot: &Snapshot) -> Result<()> { + let data = fsx::ensure_within(root, &root.join("mysql/data"))?; + if inventory(&data)? != snapshot.files || directories(&data)? != snapshot.directories { + return Err(Error::Invalid("The installed database changed during the private rehearsal. Retry after stopping other database access.".into())); + } + Ok(()) +} + +pub(crate) fn validate_connections(templates: &Path, installation: &Path) -> Result<()> { + let port = crate::layout::read_ports(installation).mysql.to_string(); + let (_, password) = crate::db::credentials(installation)?; + let realm = crate::realms::state(installation)?.active; + for relative in ["Settings/worldserver.conf.template", "Settings/authserver.conf.template"] { + let path = fsx::ensure_within(templates, &templates.join(relative))?; + let config = crate::config::parser::ConfFile::parse_bytes(&fs::read(path)?)?; + for (key, kind) in [("LoginDatabaseInfo", "auth"), ("WorldDatabaseInfo", "world"), ("CharacterDatabaseInfo", "characters")] { + if relative.contains("authserver") && kind != "auth" { continue; } + let value = config.get(key).ok_or_else(|| Error::Invalid(format!("The update cannot isolate the missing {key} connection.")))?.trim().trim_matches('"'); + if value == format!("@{key}@") { continue; } + let fields: Vec<_> = value.split(';').collect(); + if fields.len() != 5 || !matches!(fields[0], "127.0.0.1" | "localhost") || fields[1] != port || fields[2] != "acore" || fields[3] != password || fields[4] != realm.schema(kind)? { + return Err(Error::Invalid(format!("The {key} connection does not use this repack's bundled database. A private update cannot safely qualify an external database."))); + } + } + } + Ok(()) +} + +// Keep the replaced data directory. If interrupted between renames, a retry can install a new verified copy. +pub(crate) fn swap(root: &Path, meta: &Path, point: &Path, snapshot: &Snapshot, fail_after_move: bool) -> Result { + verify(point, snapshot)?; + if fsx::sha256_file(&root.join("mysql/bin/mysqld.exe"))? != snapshot.server_sha256 { + return Err(Error::Invalid("The MySQL executable differs from the recovery point.".into())); + } + let mysql = fsx::ensure_within(root, &root.join("mysql"))?; + let token = uuid::Uuid::new_v4().simple().to_string(); + let staging = fsx::ensure_within(&mysql, &mysql.join(format!("data-restore-{token}")))?; + let previous = fsx::ensure_within(&mysql, &mysql.join(format!("data-before-restore-{token}")))?; + let live = fsx::ensure_within(&mysql, &mysql.join("data"))?; + copy_snapshot(point, snapshot, &staging)?; + let journal = meta.join("diagnostics").join(format!("database-restore-{token}.json")); + fsx::atomic_write_json(&journal, &serde_json::json!({"schema":1,"state":"prepared","previous":previous,"staging":staging}))?; + if live.exists() { fs::rename(&live, &previous)?; } + if fail_after_move { return Err(Error::Invalid("Simulated interruption after saving the previous MySQL directory.".into())); } + fs::rename(&staging, &live)?; + fsx::atomic_write_json(&journal, &serde_json::json!({"schema":1,"state":"restored","previous":previous}))?; + Ok(previous) +} + +#[cfg(test)] +mod tests { + use super::*; + #[cfg(windows)] + #[test] + #[ignore = "requires COA_MYSQL_TEST_BIN pointing to the bundled MySQL tools"] + fn real_mysql_restores_objects_users_rows_and_missing_defaults() { + use std::process::{Child, Command, Stdio}; + use std::os::windows::process::CommandExt; + struct Server(Child); + impl Drop for Server { fn drop(&mut self) { let _ = self.0.kill(); let _ = self.0.wait(); } } + let bin = PathBuf::from(std::env::var("COA_MYSQL_TEST_BIN").unwrap()); + let temp = tempfile::tempdir().unwrap(); + let root = temp.path().join("server"); + let meta = temp.path().join("metadata"); + let point = temp.path().join("point"); + copy_verified(&bin, &root.join("mysql/bin"), &inventory(&bin).unwrap()).unwrap(); + let data = root.join("mysql/data"); + let executable = root.join("mysql/bin/mysqld.exe"); + let initialized = Command::new(&executable).args(["--no-defaults", "--initialize-insecure"]) + .arg(format!("--datadir={}", data.display())).creation_flags(0x0800_0000).output().unwrap(); + assert!(initialized.status.success(), "{}", String::from_utf8_lossy(&initialized.stderr)); + let port = crate::install::free_port().unwrap(); + fsx::atomic_write_json(&root.join("Settings/repack.json"), &serde_json::json!({"mysqlPort":port})).unwrap(); + fsx::atomic_write_json(&root.join("Settings/database.json"), &serde_json::json!({"rootPassword":"","appPassword":""})).unwrap(); + let db = crate::db::Db::from_repack(&root, crate::db::Account::Admin).unwrap(); + let start = || { + let server = Server(Command::new(&executable).args(["--no-defaults", "--bind-address=127.0.0.1", "--mysqlx=0", "--skip-log-bin"]) + .arg(format!("--datadir={}", data.display())).arg(format!("--port={port}")) + .arg(format!("--log-error={}", root.join("mysql-test.log").display())) + .stdout(Stdio::null()).stderr(Stdio::null()).creation_flags(0x0800_0000).spawn().unwrap()); + let deadline = std::time::Instant::now() + std::time::Duration::from_secs(60); + while !db.ping() { assert!(std::time::Instant::now() < deadline, "{}", fs::read_to_string(root.join("mysql-test.log")).unwrap_or_default()); std::thread::sleep(std::time::Duration::from_millis(200)); } + server + }; + let mut server = start(); + db.query("CREATE DATABASE acore_characters; CREATE DATABASE acore_world; CREATE DATABASE acore_auth; CREATE TABLE acore_characters.probe(id INT PRIMARY KEY, note VARCHAR(40)); CREATE TABLE acore_world.probe(id INT NOT NULL DEFAULT 0); INSERT INTO acore_world.probe VALUES(43); CREATE TABLE acore_auth.probe(id INT); INSERT INTO acore_auth.probe VALUES(44); CREATE USER 'fixture_user'@'localhost' IDENTIFIED BY 'fixture'; CREATE TRIGGER acore_characters.probe_insert BEFORE INSERT ON acore_characters.probe FOR EACH ROW SET NEW.note='triggered'; INSERT INTO acore_characters.probe VALUES(42,'original'); CREATE VIEW acore_characters.probe_view AS SELECT c.id,w.id AS world_id FROM acore_characters.probe c CROSS JOIN acore_world.probe w; CREATE PROCEDURE acore_characters.probe_proc() SELECT id FROM acore_characters.probe; CREATE FUNCTION acore_characters.probe_func() RETURNS INT DETERMINISTIC RETURN 73;").unwrap(); + assert_eq!(db.recovery_objects("acore_characters").unwrap().len(), 4); + crate::schema_check::capture(&db, &root).unwrap(); + let hash = fsx::sha256_file(&root.join(crate::schema_check::CONTRACT)).unwrap(); + db.query("ALTER TABLE acore_world.probe ALTER COLUMN id DROP DEFAULT;").unwrap(); + crate::schema_check::repair_missing_defaults(&db, &root, Some(&hash)).unwrap(); + assert_eq!(db.query("SELECT COLUMN_DEFAULT FROM information_schema.COLUMNS WHERE TABLE_SCHEMA='acore_world' AND TABLE_NAME='probe' AND COLUMN_NAME='id';").unwrap(), "0"); + assert_eq!(db.query("SELECT id FROM acore_world.probe;").unwrap(), "43"); + db.query("ALTER TABLE acore_world.probe ALTER COLUMN id SET DEFAULT 99;").unwrap(); + crate::schema_check::repair_missing_defaults(&db, &root, Some(&hash)).unwrap(); + assert_eq!(db.query("SELECT COLUMN_DEFAULT FROM information_schema.COLUMNS WHERE TABLE_SCHEMA='acore_world' AND TABLE_NAME='probe' AND COLUMN_NAME='id';").unwrap(), "99"); + db.query("SHUTDOWN;").unwrap(); server.0.wait().unwrap(); + let files = inventory(&data).unwrap(); + copy_verified(&data, &point.join("mysql-data"), &files).unwrap(); + let expanded_bytes = files.keys().map(|name| fs::metadata(data.join(name)).unwrap().len()).sum(); + let mut snapshot = Snapshot {path:"mysql-data".into(), files, directories:directories(&data).unwrap(), server_sha256:fsx::sha256_file(&executable).unwrap(), bytes:0, expanded_bytes, archive_sha256:None}; + for directory in &snapshot.directories { fs::create_dir_all(point.join("mysql-data").join(directory)).unwrap(); } + compress(&data, &point, &mut snapshot).unwrap(); + let mut changed = start(); + db.query("DROP VIEW acore_characters.probe_view; DROP TRIGGER acore_characters.probe_insert; DROP PROCEDURE acore_characters.probe_proc; DROP FUNCTION acore_characters.probe_func; DELETE FROM acore_characters.probe; DROP USER 'fixture_user'@'localhost';").unwrap(); + db.query("SHUTDOWN;").unwrap(); changed.0.wait().unwrap(); + swap(&root, &meta, &point, &snapshot, false).unwrap(); + let mut restored = start(); + assert_eq!(db.recovery_objects("acore_characters").unwrap().len(), 4); + assert_eq!(db.query("SELECT * FROM acore_characters.probe_view; CALL acore_characters.probe_proc(); SELECT acore_characters.probe_func(); SELECT COUNT(*) FROM mysql.user WHERE user='fixture_user';").unwrap().replace("\r\n", "\n"), "42\t43\n42\n73\n1"); + db.query("INSERT INTO acore_characters.probe VALUES(45,'check');").unwrap(); + assert_eq!(db.query("SELECT note FROM acore_characters.probe WHERE id=45;").unwrap(), "triggered"); + db.query("SHUTDOWN;").unwrap(); restored.0.wait().unwrap(); + } + fn fixture() -> (tempfile::TempDir, PathBuf, PathBuf, PathBuf, Snapshot) { + let temp = tempfile::tempdir().unwrap(); + let root = temp.path().join("server"); + let meta = temp.path().join("metadata"); + let point = temp.path().join("point"); + fsx::atomic_write(&root.join("mysql/bin/mysqld.exe"), b"same-engine").unwrap(); + fsx::atomic_write(&root.join("mysql/data/characters.ibd"), b"live").unwrap(); + fsx::atomic_write(&point.join("mysql-data/characters.ibd"), b"saved").unwrap(); + fs::create_dir_all(point.join("mysql-data/empty_schema")).unwrap(); + let snapshot = Snapshot { path:"mysql-data".into(), files:inventory(&point.join("mysql-data")).unwrap(), directories:directories(&point.join("mysql-data")).unwrap(), server_sha256:fsx::sha256_file(&root.join("mysql/bin/mysqld.exe")).unwrap(), bytes:5, expanded_bytes:5, archive_sha256:None }; + (temp, root, meta, point, snapshot) + } + #[test] + fn interrupted_directory_swap_is_recoverable_and_keeps_previous_bytes() { + let (_temp, root, meta, point, snapshot) = fixture(); + assert!(swap(&root, &meta, &point, &snapshot, true).is_err()); + assert!(!root.join("mysql/data").exists()); + let old = fs::read_dir(root.join("mysql")).unwrap().flatten().find(|e| e.file_name().to_string_lossy().starts_with("data-before-")).unwrap().path(); + assert_eq!(fs::read(old.join("characters.ibd")).unwrap(), b"live"); + swap(&root, &meta, &point, &snapshot, false).unwrap(); + assert_eq!(fs::read(root.join("mysql/data/characters.ibd")).unwrap(), b"saved"); + assert!(root.join("mysql/data/empty_schema").is_dir()); + } + #[test] + fn corrupt_or_different_engine_snapshots_do_not_move_live_data() { + let (_temp, root, meta, point, snapshot) = fixture(); + fsx::atomic_write(&point.join("mysql-data/characters.ibd"), b"corrupt").unwrap(); + assert!(swap(&root, &meta, &point, &snapshot, false).is_err()); + assert_eq!(fs::read(root.join("mysql/data/characters.ibd")).unwrap(), b"live"); + fsx::atomic_write(&point.join("mysql-data/characters.ibd"), b"saved").unwrap(); + fsx::atomic_write(&root.join("mysql/bin/mysqld.exe"), b"new-engine").unwrap(); + assert!(swap(&root, &meta, &point, &snapshot, false).is_err()); + assert_eq!(fs::read(root.join("mysql/data/characters.ibd")).unwrap(), b"live"); + } + #[test] + fn compressed_snapshots_verify_every_file_and_reject_unlisted_entries() { + let (_temp, root, meta, point, mut snapshot) = fixture(); + let source = point.join("mysql-data"); + compress(&source, &point, &mut snapshot).unwrap(); + assert!(snapshot.archive_sha256.is_some()); + swap(&root, &meta, &point, &snapshot, false).unwrap(); + assert_eq!(fs::read(root.join("mysql/data/characters.ibd")).unwrap(), b"saved"); + fsx::atomic_write(&root.join("mysql/data/characters.ibd"), b"preserve").unwrap(); + let archive_path = point.join(&snapshot.path); + let encoder = zstd::Encoder::new(fs::File::create(&archive_path).unwrap(), 3).unwrap(); + let mut archive = tar::Builder::new(encoder); + archive.append_file("unlisted.ibd", &mut fs::File::open(source.join("characters.ibd")).unwrap()).unwrap(); + archive.into_inner().unwrap().finish().unwrap(); + snapshot.archive_sha256 = Some(fsx::sha256_file(&archive_path).unwrap()); + assert!(swap(&root, &meta, &point, &snapshot, false).is_err()); + assert_eq!(fs::read(root.join("mysql/data/characters.ibd")).unwrap(), b"preserve"); + fsx::atomic_write(&archive_path, b"damaged").unwrap(); + assert!(verify(&point, &snapshot).is_err()); + } + #[test] + fn private_rehearsals_reject_external_database_connections() { + let (_temp, root, _meta, _point, _snapshot) = fixture(); + fsx::atomic_write_json(&root.join("Settings/repack.json"), &serde_json::json!({"mysqlPort":3307})).unwrap(); + fsx::atomic_write_json(&root.join("Settings/database.json"), &serde_json::json!({"rootPassword":"test-root","appPassword":"test-app"})).unwrap(); + fsx::atomic_write(&root.join("Settings/worldserver.conf.template"), b"LoginDatabaseInfo = \"@LoginDatabaseInfo@\"\nWorldDatabaseInfo = \"@WorldDatabaseInfo@\"\nCharacterDatabaseInfo = \"@CharacterDatabaseInfo@\"\n").unwrap(); + fsx::atomic_write(&root.join("Settings/authserver.conf.template"), b"LoginDatabaseInfo = \"127.0.0.1;3307;acore;test-app;acore_auth\"\n").unwrap(); + validate_connections(&root, &root).unwrap(); + fsx::atomic_write(&root.join("Settings/authserver.conf.template"), b"LoginDatabaseInfo = \"192.0.2.1;3307;acore;test-app;acore_auth\"\n").unwrap(); + let error = validate_connections(&root, &root).unwrap_err().to_string(); + assert!(error.contains("external database")); + assert!(!error.contains("test-app")); + } + #[test] + fn a_changed_live_database_invalidates_the_rehearsal() { + let (_temp, root, _meta, _point, snapshot) = fixture(); + assert!(unchanged(&root, &snapshot).is_err()); + fsx::atomic_write(&root.join("mysql/data/characters.ibd"), b"saved").unwrap(); + fs::create_dir_all(root.join("mysql/data/empty_schema")).unwrap(); + unchanged(&root, &snapshot).unwrap(); + fsx::atomic_write(&root.join("mysql/data/extra.ibd"), b"new data").unwrap(); + assert!(unchanged(&root, &snapshot).is_err()); + } +} diff --git a/crates/coa-core/src/release_schema.rs b/crates/coa-core/src/release_schema.rs index d784a1d..c79f57c 100644 --- a/crates/coa-core/src/release_schema.rs +++ b/crates/coa-core/src/release_schema.rs @@ -13,6 +13,7 @@ impl crate::update::Env for QualificationEnv<'_> { fn preflight(&self, manifest: &crate::manifest::Manifest) -> Result<()> { self.0.preflight(manifest) } fn snapshot(&self) -> Result { self.0.snapshot() } fn verify_snapshot(&self, id: &str) -> Result<()> { self.0.verify_snapshot(id) } + fn rehearse(&self, manifest: &crate::manifest::Manifest, tree: &Path, items: &[crate::update::PlanItem], point: &str) -> Result<()> { self.0.rehearse(manifest, tree, items, point) } fn restore_snapshot(&self, id: &str) -> Result<()> { self.0.restore_snapshot(id) } fn migrate(&self, manifest: &crate::manifest::Manifest, staged: &Path) -> Result { self.0.migrate(manifest, staged) } fn validate(&self) -> Result<()> { self.0.validate() } @@ -76,7 +77,7 @@ pub fn qualify_upgrade(base: &Path, source: &Path, candidate: &Path, fixture: &P "schema": 1, "result": "passed", "fromVersion": source_manifest.version, "toVersion": candidate_manifest.version, "fromManifestSha256": fsx::sha256_bytes(&source_bytes), "fromSchemaSha256": source_manifest.files.iter().find(|file| file.path == schema_check::CONTRACT).map(|file| &file.sha256), - "checks": ["manager-transaction", "database-schema", "private-auth-world-startup", "user-file-preservation"], + "checks": ["manager-transaction", "private-user-database-rehearsal", "database-schema", "private-auth-world-startup", "existing-identities", "user-file-preservation"], })) } diff --git a/crates/coa-core/src/schema_check.rs b/crates/coa-core/src/schema_check.rs index c00244d..8e692af 100644 --- a/crates/coa-core/src/schema_check.rs +++ b/crates/coa-core/src/schema_check.rs @@ -85,6 +85,46 @@ pub fn capture(db: &Db, root: &Path) -> Result<()> { fsx::atomic_write_json(&root.join(CONTRACT), &Contract { schema: 1, columns }) } +fn missing_default(expected: &str, actual: &str) -> Option { + let expected: Vec<_> = expected.split('|').collect(); + let actual: Vec<_> = actual.split('|').collect(); + if expected.len() != 4 || actual.len() != 4 || expected[0] != actual[0] + || expected[1] != "NO" || actual[1] != "NO" || expected[3] != "" || actual[3] != "" + || actual[2] != "" || matches!(expected[2], "" | "") { return None; } + String::from_utf8(hex::decode(expected[2]).ok()?).ok() +} + +/// Only restore a missing literal default from the exact signed target contract. Never alter data or custom defaults. +pub(crate) fn repair_missing_defaults(db: &Db, root: &Path, signed_hash: Option<&str>) -> Result<()> { + let Some(signed_hash) = signed_hash else { return Ok(()); }; + if fsx::sha256_file(&root.join(CONTRACT))? != signed_hash { + return Err(crate::Error::Invalid("The target schema contract differs from the signed update.".into())); + } + let contract = require_release_contract(root)?; + let mut repaired = Vec::new(); + let quote = |identifier: &str| format!("`{}`", identifier.replace('`', "``")); + for (kind, tables) in &contract.columns { + let schema = db.realm_schema(crate::db::schema_of(kind)?); + let actual = read_columns(db, kind)?; + let base_tables: BTreeSet<_> = db.tables(crate::db::schema_of(kind)?)?.into_iter().collect(); + for (table, columns) in tables { + if !base_tables.contains(table) { continue; } + if matches!(table.as_str(), "updates" | "updates_include" | "coa_manager_migrations") { continue; } + for (column, expected) in columns { + let Some(found) = actual.get(table).and_then(|columns| columns.get(column)) else { continue; }; + let Some(value) = missing_default(expected, found) else { continue; }; + db.query(&format!("SET SESSION sql_mode=CONCAT_WS(',',@@SESSION.sql_mode,'NO_BACKSLASH_ESCAPES'); ALTER TABLE {}.{} ALTER COLUMN {} SET DEFAULT '{}';", quote(schema), quote(table), quote(column), value.replace('\'', "''")))?; + repaired.push(serde_json::json!({"database":schema,"table":table,"column":column,"action":"restore-missing-default"})); + } + } + } + if !repaired.is_empty() { + let path = crate::registry::metadata_dir_for(root)?.join("diagnostics").join(format!("database-repair-{}.json", uuid::Uuid::new_v4())); + fsx::atomic_write_json(&path, &serde_json::json!({"schema":1,"checkedAt":chrono::Utc::now().to_rfc3339(),"contractSha256":signed_hash,"repairs":repaired}))?; + } + Ok(()) +} + pub fn check(db: &Db, root: &Path) -> Result> { let contract = if root.join(CONTRACT).is_file() { let contract: Contract = fsx::read_json(&root.join(CONTRACT))?; @@ -175,6 +215,15 @@ fn missing_coa_starts(rows: &str) -> Vec { #[cfg(test)] mod tests { use super::*; + #[test] + fn default_repair_only_accepts_missing_literal_defaults_without_other_schema_changes() { + assert_eq!(missing_default("int unsigned|NO|30|", "int unsigned|NO||"), Some("0".into())); + for actual in ["int unsigned|NO|3939|", "int|NO||", "int unsigned|YES||", "int unsigned|NO||6175746f5f696e6372656d656e74"] { + assert!(missing_default("int unsigned|NO|30|", actual).is_none()); + } + assert!(missing_default("timestamp|NO|43555252454e545f54494d455354414d50|44454641554c545f47454e455241544544", "timestamp|NO||").is_none()); + assert!(missing_default("int|NO|zz|", "int|NO||").is_none()); + } #[test] fn schema_defaults_are_readable_and_not_conflated() { diff --git a/crates/coa-core/src/update.rs b/crates/coa-core/src/update.rs index a1a3aff..b91bf0d 100644 --- a/crates/coa-core/src/update.rs +++ b/crates/coa-core/src/update.rs @@ -92,6 +92,7 @@ pub trait Env { fn ensure_stopped(&self) -> Result<()>; fn preflight(&self, _manifest: &Manifest) -> Result<()> { Ok(()) } fn verify_snapshot(&self, _id: &str) -> Result<()> { Ok(()) } + fn rehearse(&self, _manifest: &Manifest, _tree: &Path, _items: &[PlanItem], _point: &str) -> Result<()> { Ok(()) } /// Create a recovery point for the databases and configuration; returns its id. fn snapshot(&self) -> Result; fn restore_snapshot(&self, id: &str) -> Result<()>; @@ -493,6 +494,14 @@ pub fn apply(p: &Params, report: &dyn Fn(&str, u8)) -> Result { txn.recovery_point = Some(p.env.snapshot().map_err(|e| Error::Invalid(format!("The update was not applied because the safety backup failed: {e}")))?); let point_meta = crate::backup::point_json(meta_dir, txn.recovery_point.as_deref().unwrap())?; if point_meta.is_file() { txn.recovery_hashes.insert("@point".into(), fsx::sha256_file(&point_meta)?); } + save(meta_dir, &txn)?; + step(report, "Testing the update on a private copy of your server", 54); + if let Err(error) = p.env.rehearse(&m, &tree, &items, txn.recovery_point.as_deref().unwrap()) { + txn.state = State::Failed; + txn.message = Some(format!("The private update rehearsal failed: {error}. The installed server was not updated.")); + save(meta_dir, &txn)?; + return Err(Error::Invalid(txn.message.unwrap())); + } txn.ops = items .iter() @@ -516,7 +525,7 @@ pub fn apply(p: &Params, report: &dyn Fn(&str, u8)) -> Result { // MySQL DDL can commit before a later statement fails. Restore the full recovery point on failure. let mut migrated = None; - if !m.migrations.is_empty() { + if !m.migrations.is_empty() || m.files.iter().any(|file| file.path == crate::schema_check::CONTRACT) { step(report, "Updating the database", 75); txn.databases_started = true; save(meta_dir, &txn)?; @@ -535,6 +544,9 @@ pub fn apply(p: &Params, report: &dyn Fn(&str, u8)) -> Result { } step(report, "Starting the updated server", 85); + // Core startup can apply SQL and write character data even when the package has no explicit migrations. + txn.databases_started = true; + save(meta_dir, &txn)?; if let Err(e) = p.env.validate() { if p.env.automatic_rollback() { return fail_after_apply(p, &mut txn, &before, &tree, @@ -849,6 +861,12 @@ impl RepackEnv<'_> { return Err(Error::Invalid("The update recovery point does not contain every required database and configuration.".into())); } if !crate::backup::verify(self.meta_dir, &point.id)?.ok { return Err(Error::Invalid("The recovery point failed verification.".into())); } + if let Some(snapshot) = &point.mysql_snapshot { + if fsx::sha256_file(&self.root.join("mysql/bin/mysqld.exe"))? != snapshot.server_sha256 { + return Err(Error::Invalid("The MySQL executable differs from the recovery point.".into())); + } + return Ok(()); + } crate::backup::with_database(self.root, |db| { let db = db.clone().for_realm(crate::realms::Mode::Coa); let mut objects = Vec::new(); @@ -894,7 +912,79 @@ impl Env for RepackEnv<'_> { } fn automatic_rollback(&self) -> bool { true } + fn rehearse(&self, manifest: &Manifest, tree: &Path, items: &[PlanItem], id: &str) -> Result<()> { + if items.iter().any(|item| item.action != Action::Skip && item.path.to_ascii_lowercase().starts_with("mysql/")) { + return Err(Error::Invalid("A server update cannot replace MySQL while using a cold recovery copy; a separate database engine upgrade is required.".into())); + } + let point = crate::backup::get(self.meta_dir, id)?; + let Some(snapshot) = &point.mysql_snapshot else { + // Docker uses its separate experimental release channel until its clone runner is qualified. + if crate::docker::is_docker(self.root) { return Ok(()); } + return Err(Error::Invalid("This installation has no complete MySQL recovery copy for an update rehearsal.".into())); + }; + crate::mysql_snapshot::validate_connections(self.root, self.root)?; + crate::mysql_snapshot::stop(self.root)?; + let point_dir = crate::backup::point_json(self.meta_dir, id)?.parent().unwrap().to_path_buf(); + let fixture = crate::mysql_snapshot::rehearsal_copy(self.root, self.meta_dir, &point_dir, snapshot)?; + let metadata = crate::registry::metadata_dir_for(&fixture)?; + let env = RepackEnv { root: &fixture, meta_dir: &metadata }; + let result = crate::install::with_scratch_ports(&fixture, || { + let mut txn = Txn { id: "rehearsal".into(), state: State::Applying, from_version: None, + to_version: manifest.version.clone(), recovery_point: None, databases_started: false, + ops: items.iter().filter(|item| item.action != Action::Skip).map(|item| Op { + path: item.path.clone(), action: item.action, reason: item.reason.clone(), + new_sha256: manifest.files.iter().find(|file| file.path == item.path).unwrap().sha256.clone(), + had_previous: fsx::safe_join(&fixture, &item.path).is_ok_and(|path| path.is_file()), started:false, done:false, + }).collect(), message:None, recovery_hashes:BTreeMap::new() }; + let before = txn_dir(&metadata, &txn.id)?.join("before"); + fs::create_dir_all(&before)?; + apply_ops(&fixture, tree, &before, &manifest.files, &mut txn, &metadata, None)?; + crate::mysql_snapshot::validate_connections(&fixture, self.root)?; + crate::mysql_snapshot::isolate_connections(&fixture)?; + 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()))?; + } + verify_database_identities(db, &identities)?; + Ok(identities) + })?; + env.validate()?; + crate::backup::with_database(&fixture, |db| verify_database_identities(db, &identities))?; + env.ensure_stopped() + }); + let stopped = crate::mysql_snapshot::stop(&fixture); + let live_state = if result.is_ok() { crate::mysql_snapshot::stop(self.root).and_then(|()| crate::mysql_snapshot::unchanged(self.root, snapshot)) } else { Ok(()) }; + let logs: BTreeMap<_, _> = ["Core/Logs/auth-console.log", "Core/Logs/world-console.log", "Core/Logs/supervisor.log", "mysql/logs/mysql-error.log"].into_iter() + .map(|path| (path, crate::diag::redact(&crate::health::tail(&fixture.join(path), 32768)))).collect(); + let mut schema_reports = Vec::::new(); + if let Ok(entries) = fs::read_dir(metadata.join("diagnostics")) { + for entry in entries.flatten().filter(|entry| entry.file_name().to_string_lossy().starts_with("schema-validation-")) { + let path = fsx::ensure_within(&metadata, &entry.path())?; + if fs::metadata(&path)?.len() <= 4 * 1024 * 1024 { schema_reports.push(fsx::read_json(&path)?); } + } + } + let report = serde_json::json!({"schema":1,"checkedAt":chrono::Utc::now().to_rfc3339(),"toVersion":manifest.version,"recoveryPoint":id, + "result":if result.is_ok() && stopped.is_ok() && live_state.is_ok() {"passed"} else {"failed"}, "error":result.as_ref().err().map(ToString::to_string), "stopError":stopped.as_ref().err().map(ToString::to_string), "liveStateError":live_state.as_ref().err().map(ToString::to_string), "logs":logs,"schemaReports":schema_reports,"fixture":fixture}); + let report_path = self.meta_dir.join("diagnostics").join(format!("database-rehearsal-{}.json", uuid::Uuid::new_v4())); + fsx::atomic_write_json(&report_path, &report)?; + stopped?; + let parent = fixture.parent().unwrap(); + let owned = fsx::ensure_within(&self.meta_dir.join("rehearsals"), parent)?; + fs::remove_dir_all(owned)?; + live_state?; + result + } fn preflight(&self, manifest: &Manifest) -> Result<()> { + crate::mysql_snapshot::stop_previous_rehearsals(self.meta_dir)?; if let Some(compatibility) = &manifest.compatibility { let (_, meta) = MetaDir::open(self.meta_dir)?; if let Some(source) = meta.core.version.as_ref().and_then(|v| compatibility.source_databases.get(v)) { @@ -909,7 +999,10 @@ impl Env for RepackEnv<'_> { if crate::realms::state(self.root)?.wildcard_created { modes.push(crate::realms::Mode::Wildcard); } for mode in modes { if let Some(problem) = crate::schema_check::check_with_report(&db.clone().for_realm(mode), self.root)?.first() { - return Err(Error::Invalid(format!("The installed {} database does not match its upgrade source: {}.{}: {}", mode.name(), problem.database, problem.table, problem.detail))); + if crate::docker::is_docker(self.root) { + return Err(Error::Invalid(format!("The installed {} database does not match its upgrade source: {}.{}: {}", mode.name(), problem.database, problem.table, problem.detail))); + } + tracing::warn!(realm = mode.name(), table = %problem.table, "Installed schema differs; the private candidate rehearsal must qualify this database"); } } Ok(()) @@ -949,6 +1042,13 @@ impl Env for RepackEnv<'_> { if !["characters", "auth", "world", "configs"].iter().all(|name| point.components.iter().any(|c| c.name == *name)) || !crate::backup::verify(self.meta_dir, id)?.ok { return Err(Error::Invalid("The full database recovery point is incomplete or damaged.".into())); } + if let Some(snapshot) = &point.mysql_snapshot { + crate::mysql_snapshot::stop(self.root)?; + let point_dir = crate::backup::point_json(self.meta_dir, id)?.parent().unwrap().to_path_buf(); + crate::mysql_snapshot::swap(self.root, self.meta_dir, &point_dir, snapshot, false)?; + crate::backup::restore_configs(self.root, self.meta_dir, id)?; + return Ok(()); + } for component in point.components.iter().filter(|c| c.sha256.is_some()) { crate::backup::restore_database(self.root, self.meta_dir, id, &component.name)?; } @@ -960,12 +1060,17 @@ impl Env for RepackEnv<'_> { 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, &|| { @@ -976,12 +1081,14 @@ impl Env for RepackEnv<'_> { 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()))); @@ -992,6 +1099,16 @@ impl Env for RepackEnv<'_> { } fn validate(&self) -> Result<()> { + self.validate_server(true) + } + + fn validate_recovery(&self) -> Result<()> { + self.validate_server(false) + } +} + +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())); } self.ensure_stopped()?; @@ -999,7 +1116,7 @@ impl Env for RepackEnv<'_> { let result = (|| -> Result<()> { crate::update_isolation::begin(self.root, self.meta_dir)?; use crate::process::{observe, ServiceState}; - crate::backup::with_database(self.root, |db| { + if check_schema { crate::backup::with_database(self.root, |db| { let mut modes = vec![crate::realms::Mode::Coa]; if crate::realms::state(self.root)?.wildcard_created { modes.push(crate::realms::Mode::Wildcard); } for mode in modes { @@ -1008,7 +1125,7 @@ impl Env for RepackEnv<'_> { } } Ok(()) - })?; + })?; } // The check start is the first start of the new build: give the module configs the settings the update added // (as a normal start does), or the server logs a "missing property" line for every one of them. match crate::registry::MetaDir::open(self.meta_dir) { @@ -1064,6 +1181,31 @@ impl Env for RepackEnv<'_> { } +fn database_identities(db: &crate::db::Db) -> Result>> { + let db = db.clone().for_realm(crate::realms::Mode::Coa); + let mut identities = BTreeMap::new(); + for (schema, table, columns) in [ + ("acore_characters", "characters", "guid,account,HEX(name)"), + ("acore_characters_wildcard", "characters", "guid,account,HEX(name)"), + ("acore_auth", "account", "id,HEX(username)"), + ] { + if !db.schema_exists(schema)? || !db.tables(schema)?.iter().any(|name| name == table) { continue; } + let rows = db.query(&format!("SELECT {columns} FROM `{schema}`.`{table}`;"))?; + identities.insert(schema.into(), rows.lines().map(str::to_string).collect()); + } + Ok(identities) +} + +fn verify_database_identities(db: &crate::db::Db, before: &BTreeMap>) -> Result<()> { + let after = database_identities(db)?; + for (schema, expected) in before { + if after.get(schema).is_none_or(|actual| !expected.is_subset(actual)) { + return Err(Error::Invalid(format!("The private update changed or removed existing character or account identities in {schema}."))); + } + } + Ok(()) +} + #[cfg(test)] mod tests { use super::*; @@ -1086,15 +1228,19 @@ mod tests { migrate_fail: Cell, preflight_fail: Cell, restore_fail: Cell, + rehearsal_fail: Cell, } impl Fake { fn ok() -> Fake { - Fake { activation_expected: RefCell::new(None), activation_failed: Cell::new(false), activated: Cell::new(false), automatic_rollback: Cell::new(false), recovery_healthy: Cell::new(true), stopped: Cell::new(true), healthy: Cell::new(true), snapshot_ok: Cell::new(true), calls: Default::default(), migrate_fail: Cell::new(false), preflight_fail: Cell::new(false), restore_fail: Cell::new(false) } + Fake { activation_expected: RefCell::new(None), activation_failed: Cell::new(false), activated: Cell::new(false), automatic_rollback: Cell::new(false), recovery_healthy: Cell::new(true), stopped: Cell::new(true), healthy: Cell::new(true), snapshot_ok: Cell::new(true), calls: Default::default(), migrate_fail: Cell::new(false), preflight_fail: Cell::new(false), restore_fail: Cell::new(false), rehearsal_fail: Cell::new(false) } } } impl Env for Fake { + fn rehearse(&self, _: &Manifest, _: &Path, _: &[PlanItem], _: &str) -> Result<()> { + if self.rehearsal_fail.get() { Err(Error::Invalid("SQL failed in the private copy".into())) } else { Ok(()) } + } fn activate(&self) -> Result<()> { if let Some((meta, version)) = self.activation_expected.borrow().as_ref() { ensure_recovered(meta)?; @@ -1695,6 +1841,23 @@ mod tests { assert!(fs::read_dir(w.meta.join("updates")).unwrap().next().is_none()); } + #[test] + fn failed_rehearsal_preserves_installed_files_version_and_recovery_point() { + let w = world(&[], true); + let env = Fake::ok(); + env.rehearsal_fail.set(true); + let original = fsx::sha256_file(&w.meta.join("install.json")).unwrap(); + let error = run(&w, &env, BTreeMap::new(), None).unwrap_err(); + assert!(error.to_string().contains("installed server was not updated")); + assert_eq!(read(&w, "Core/worldserver.exe"), b"world-v1"); + assert_eq!(fsx::sha256_file(&w.meta.join("install.json")).unwrap(), original); + assert_eq!(&*env.calls.borrow(), &["stop", "snapshot"]); + assert!(pending_checked(&w.meta).unwrap().is_none()); + let journal = journals(&w.meta).unwrap().remove(0); + assert_eq!(journal.recovery_point.as_deref(), Some("rp-1")); + assert!(!journal.databases_started && journal.ops.is_empty()); + } + #[test] fn missing_and_semantically_corrupt_journals_block_operations() { let w = world(&[], false); diff --git a/src/lib/api.ts b/src/lib/api.ts index 9d11c35..e3dcb13 100644 --- a/src/lib/api.ts +++ b/src/lib/api.ts @@ -186,6 +186,7 @@ export interface RecoveryPoint { label: string | null; created_at: string; components: { name: string; path: string; bytes: number; tables: number | null; files: string[] | null }[]; + mysql_snapshot?: { bytes: number } | null; } export interface DbRestore { diff --git a/src/screens/BackupsPage.tsx b/src/screens/BackupsPage.tsx index 35b5104..5dc2724 100644 --- a/src/screens/BackupsPage.tsx +++ b/src/screens/BackupsPage.tsx @@ -198,7 +198,7 @@ export function BackupsPage({ serverId }: { serverId: string }) {

{p.label && p.trigger === "manual" && p.label !== "e2e" ? p.label : trigger(p.trigger)}

- {w(p.created_at)} · {p.realm === "wildcard" ? "Wildcard" : "CoA"} · {t(`bk.kind.${p.kind}` as Key)} · {size(p.components.reduce((a, c) => a + c.bytes, 0))} + {w(p.created_at)} · {p.realm === "wildcard" ? "Wildcard" : "CoA"} · {t(`bk.kind.${p.kind}` as Key)} · {size(p.components.reduce((a, c) => a + c.bytes, p.mysql_snapshot?.bytes ?? 0))}

diff --git a/tools/make-fixture.ps1 b/tools/make-fixture.ps1 index 1f5f64c..76d2960 100644 --- a/tools/make-fixture.ps1 +++ b/tools/make-fixture.ps1 @@ -4,7 +4,8 @@ param( [string]$Source = 'C:\games\CoA-Repack', [string]$Target = 'C:\games\coa-fixture', [int]$PortBase = 13000, - [string]$DatabaseArchive = '' + [string]$DatabaseArchive = '', + [switch]$CopyData ) $ErrorActionPreference = 'Stop' if ((Resolve-Path $Source).Path -eq $Target) { throw 'source and target are the same' } @@ -32,7 +33,8 @@ if (-not $DatabaseArchive) { $DatabaseArchive = "$Source\mysql\data.7z" } & 'C:\Program Files\7-Zip\7z.exe' x $DatabaseArchive "-o$Target\mysql" -y | Out-Null if (-not (Test-Path "$Target\mysql\data\acore_characters")) { throw 'database extraction failed' } -New-Item -ItemType Junction -Path "$Target\Data" -Target "$Source\Data" | Out-Null +if ($CopyData) { Copy-Tree 'Data' } +else { New-Item -ItemType Junction -Path "$Target\Data" -Target "$Source\Data" | Out-Null } $ports = @{ mysqlPort = $PortBase + 307; authPort = $PortBase + 724; worldPort = $PortBase + 85; raPort = $PortBase + 443 } $cfg = Get-Content "$Target\Settings\repack.json" -Raw | ConvertFrom-Json