Preserve applied core SQL and share account migration history across realms

This commit is contained in:
Дмитрий committed 2026-10-16 13:32:27 +05:00
1 parent 7db4401ce8
commit e043ba9059
13 files changed
+206 -71

No files matched your search

+11 -2
View File
@@ -2,12 +2,21 @@
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());}
if args.len()!=3 && args.len()!=4 {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 (_,installed)=MetaDir::open(&meta)?;
assert_eq!(coa_core::fsx::canonicalize_lenient(std::path::Path::new(&installed.server_path))?,coa_core::fsx::canonicalize_lenient(root)?,"Fixture metadata must point to the copy");
assert!(installed.client_path.is_none(),"Detach the real client from fixture metadata");
let env=RepackEnv{root,meta_dir:&meta};
if args.get(3).is_some_and(|flag|flag=="--validate-only") {
let result=env.validate();
let _=coa_core::driver::run(root,coa_core::driver::Verb::StopAll);
result?;
println!("Startup and identity validation passed");
return Ok(());
}
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));
+113 -47
View File
@@ -11,6 +11,7 @@ use crate::fsx;
use crate::manifest::Migration;
const LEDGER_SCHEMA: &str = "acore_world";
const AUTH_LEDGER_SCHEMA: &str = "acore_auth";
const LEDGER_TABLE: &str = "coa_manager_migrations";
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
@@ -37,6 +38,10 @@ pub trait Store {
fn load(&self) -> Result<Vec<LedgerRow>>;
fn load_readonly(&self) -> Result<Vec<LedgerRow>> { self.load() }
fn put(&self, row: &LedgerRow) -> Result<()>;
fn put_batch(&self, rows: &[LedgerRow]) -> Result<()> {
for row in rows { self.put(row)?; }
Ok(())
}
fn run_file(&self, schema: &str, file: &Path) -> Result<()>;
}
@@ -48,59 +53,101 @@ fn ident_ok(s: &str) -> bool {
!s.is_empty() && s.len() <= 150 && s.chars().all(|c| c.is_ascii_alphanumeric() || matches!(c, '_' | '.' | '-'))
}
pub(crate) fn shared_auth(kind: &str) -> bool {
matches!(db::schema_of(kind), Ok("acore_auth"))
}
fn create_ledger(db: &Db, schema: &str) -> Result<()> {
db.query(&format!("CREATE TABLE IF NOT EXISTS `{schema}`.`{LEDGER_TABLE}` (`db` VARCHAR(32) NOT NULL, `id` VARCHAR(190) NOT NULL, `sha256` CHAR(64) NOT NULL, `status` VARCHAR(16) NOT NULL, `applied_at` DATETIME NOT NULL, `error` TEXT NULL, `baseline` TINYINT NOT NULL DEFAULT 0, PRIMARY KEY (`db`,`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;"))?;
Ok(())
}
impl Store for Db {
fn load(&self) -> Result<Vec<LedgerRow>> {
self.query(&format!(
"CREATE TABLE IF NOT EXISTS `{LEDGER_SCHEMA}`.`{LEDGER_TABLE}` (`db` VARCHAR(32) NOT NULL, `id` VARCHAR(190) NOT NULL, `sha256` CHAR(64) NOT NULL, `status` VARCHAR(16) NOT NULL, `applied_at` DATETIME NOT NULL, `error` TEXT NULL, `baseline` TINYINT NOT NULL DEFAULT 0, PRIMARY KEY (`db`,`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;"
))?;
self.load_readonly()
create_ledger(self, LEDGER_SCHEMA)?;
create_ledger(self, AUTH_LEDGER_SCHEMA)?;
let rows = self.load_readonly()?;
// Preserve already-recorded core history even if a module later replaces its updates table.
// A zero checksum remains unknown; importing it does not invent a checksum for old SQL.
let selected: std::collections::BTreeMap<_, _> = rows.iter().map(|row| ((row.db.clone(), row.id.to_ascii_lowercase()), row)).collect();
let core: Vec<_> = selected.values().filter(|r| r.status == Status::Applied && r.baseline && r.sha256 == "0".repeat(64)
&& matches!(r.db.as_str(), "world" | "characters") && ident_ok(&r.id)).map(|r| (*r).clone()).collect();
self.put_batch(&core)?;
Ok(rows)
}
fn load_readonly(&self) -> Result<Vec<LedgerRow>> {
let out = if self.tables(LEDGER_SCHEMA)?.iter().any(|name| name == LEDGER_TABLE) {
self.query(&format!("SELECT `db`,`id`,`sha256`,`status`,`baseline`,COALESCE(HEX(`error`),'') FROM `{LEDGER_SCHEMA}`.`{LEDGER_TABLE}` ORDER BY `db`,`id`;"))?
} else { String::new() };
let mut rows = Vec::new();
// Files the core's own updater (or whoever prepared this database) already recorded count as applied.
for (kind, schema) in db::SCHEMAS {
if self.tables(schema)?.iter().any(|name| name == "updates") {
let names = self.query(&format!("SELECT name FROM `{schema}`.`updates`;"))?;
for name in names.lines().filter(|n| n.ends_with(".sql")) {
rows.push(LedgerRow { db: kind.into(), id: name.trim_end_matches(".sql").into(), sha256: "0".repeat(64), status: Status::Applied, error: None, baseline: true });
}
// Resolve physical schemas once, then read all history in two client calls.
// Using a neutral connection prevents a Wildcard query from rerouting the other realm.
let connection = self.clone().for_realm(crate::realms::Mode::Coa);
let world = self.realm_schema("acore_world");
let characters = self.realm_schema("acore_characters");
let other = if self.realm() == crate::realms::Mode::Coa { "acore_world_wildcard" } else { "acore_world" };
let schemas: std::collections::BTreeSet<_> = [world, characters, other, AUTH_LEDGER_SCHEMA].into_iter().collect();
let catalog = connection.query(&format!("SELECT table_schema,table_name FROM information_schema.tables WHERE table_schema IN ('{}') AND table_name IN ('updates','{LEDGER_TABLE}') AND table_type='BASE TABLE';", schemas.into_iter().collect::<Vec<_>>().join("','")))?;
let available: std::collections::BTreeSet<_> = catalog.lines().filter_map(|line| line.split_once('\t')).collect();
let text = |expression: &str| format!("CONVERT({expression} USING utf8mb4) COLLATE utf8mb4_bin");
let mut queries = Vec::new();
for (kind, schema) in [("characters", characters), ("auth", AUTH_LEDGER_SCHEMA), ("world", world)] {
if available.contains(&(schema, "updates")) {
queries.push(format!("SELECT 'core','{kind}',{},REPEAT('0',64),'applied',1,'' FROM `{schema}`.`updates` WHERE BINARY RIGHT(name,4)='.sql'",text("LEFT(name,CHAR_LENGTH(name)-4)")));
}
}
for line in out.lines().filter(|l| !l.is_empty()) {
let f: Vec<&str> = line.split('\t').collect();
if f.len() < 5 {
return Err(Error::Invalid("The database migration history is malformed; no SQL was replayed.".into()));
for (origin, schema, filter) in [("local",world,""), ("other",other," WHERE `db` IN ('auth','coa-auth','wildcard-auth')"), ("shared",AUTH_LEDGER_SCHEMA,"")] {
if available.contains(&(schema, LEDGER_TABLE)) {
queries.push(format!("SELECT '{origin}',{},{},{},{},`baseline`,{} FROM `{schema}`.`{LEDGER_TABLE}`{filter}",text("`db`"),text("`id`"),text("`sha256`"),text("`status`"),text("COALESCE(HEX(`error`),'')")));
}
if !matches!(f[3], "applied" | "failed" | "running") || !ident_ok(f[1]) || f[2].len() != 64 || !f[2].chars().all(|c| c.is_ascii_hexdigit()) {
}
if queries.is_empty() { return Ok(vec![]); }
let out = connection.query(&queries.join(" UNION ALL "))?;
let (mut rows, mut local, mut auth, mut shared) = (Vec::new(), Vec::new(), Vec::new(), Vec::new());
for line in out.lines().filter(|l| !l.is_empty()) {
let f: Vec<_> = line.split('\t').collect();
if f.len() < 6 { return Err(Error::Invalid("The database migration history is malformed; no SQL was replayed.".into())); }
// Unrelated legacy filenames cannot match a valid manifest migration ID.
if f[0] == "core" && !ident_ok(f[2]) { continue; }
if !matches!(f[4], "applied" | "failed" | "running") || !ident_ok(f[2]) || f[3].len() != 64 || !f[3].chars().all(|c| c.is_ascii_hexdigit()) {
return Err(Error::Invalid("The database migration history is invalid; no SQL was replayed.".into()));
}
let err = f.get(5).filter(|h| !h.is_empty()).and_then(|h| hex::decode(h).ok()).map(|b| String::from_utf8_lossy(&b).into_owned());
rows.push(LedgerRow {
db: f[0].into(),
id: f[1].into(),
sha256: f[2].into(),
status: match f[3] { "applied" => Status::Applied, "running" => Status::Running, _ => Status::Failed },
baseline: f[4] == "1",
error: err,
});
let error = f.get(6).filter(|h| !h.is_empty()).and_then(|h| hex::decode(h).ok()).map(|b| String::from_utf8_lossy(&b).into_owned());
let row = LedgerRow { db:f[1].into(), id:f[2].into(), sha256:f[3].into(), status:match f[4] { "applied"=>Status::Applied,"running"=>Status::Running,_=>Status::Failed }, baseline:f[5]=="1", error };
match f[0] {
"core" => rows.push(row),
"local" if !shared_auth(&row.db) => local.push(row),
"local" | "other" => auth.push(row),
"shared" => shared.push(row),
_ => return Err(Error::Invalid("Unknown migration history source.".into())),
}
}
rows.extend(local);
// An interrupted legacy auth execution must not be hidden by another realm's applied row.
auth.sort_by_key(|r| (match r.status { Status::Applied=>0,Status::Running=>1,Status::Failed=>2,Status::Pending=>3 },r.sha256!="0".repeat(64)));
for mut row in auth { row.db="auth".into(); rows.push(row); }
// New auth records live with the shared account database and are authoritative.
for mut row in shared {
if !shared_auth(&row.db) { return Err(Error::Invalid("The shared authentication migration history is invalid.".into())); }
rows.retain(|r| !(shared_auth(&r.db) && r.id.eq_ignore_ascii_case(&row.id)));
row.db="auth".into(); rows.push(row);
}
Ok(rows)
}
fn put(&self, r: &LedgerRow) -> Result<()> {
if !ident_ok(&r.id) || !ident_ok(&r.db) || r.sha256.len() != 64 || !r.sha256.chars().all(|c| c.is_ascii_hexdigit()) {
return Err(Error::Invalid("migration record has an invalid identifier".into()));
fn put(&self, row: &LedgerRow) -> Result<()> { self.put_batch(std::slice::from_ref(row)) }
fn put_batch(&self, rows: &[LedgerRow]) -> Result<()> {
let mut groups = std::collections::BTreeMap::<&str, Vec<String>>::new();
for r in rows {
if !ident_ok(&r.id) || !ident_ok(&r.db) || r.sha256.len() != 64 || !r.sha256.chars().all(|c| c.is_ascii_hexdigit()) {
return Err(Error::Invalid("migration record has an invalid identifier".into()));
}
let status = match r.status { Status::Applied => "applied", Status::Running => "running", _ => "failed" };
let error = r.error.as_deref().map(|e| format!("UNHEX('{}')", hex_of(e))).unwrap_or_else(|| "NULL".into());
let (schema, kind) = if shared_auth(&r.db) { (AUTH_LEDGER_SCHEMA, "auth") } else { (LEDGER_SCHEMA, r.db.as_str()) };
groups.entry(schema).or_default().push(format!("('{}','{}','{}','{status}',NOW(),{error},{})",kind,r.id,r.sha256,r.baseline as u8));
}
for (schema, values) in groups {
self.query(&format!("REPLACE INTO `{schema}`.`{LEDGER_TABLE}` (`db`,`id`,`sha256`,`status`,`applied_at`,`error`,`baseline`) VALUES {};",values.join(",")))?;
}
let status = match r.status { Status::Applied => "applied", Status::Running => "running", _ => "failed" };
let err = r.error.as_deref().map(|e| format!("UNHEX('{}')", hex_of(e))).unwrap_or_else(|| "NULL".into());
self.query(&format!(
"REPLACE INTO `{LEDGER_SCHEMA}`.`{LEDGER_TABLE}` (`db`,`id`,`sha256`,`status`,`applied_at`,`error`,`baseline`) VALUES ('{}','{}','{}','{status}',NOW(),{err},{});",
r.db, r.id, r.sha256, r.baseline as u8
))?;
Ok(())
}
@@ -137,21 +184,34 @@ pub fn status(store: &dyn Store, list: &[Migration]) -> Result<Vec<Item>> {
.map(|m| {
// Manager records are appended after the core's updates table and are authoritative.
let row = recorded(&rows, m);
let changed = row.is_some_and(|r| hash_changed(r, m));
let history = effective_history(&rows, m);
let changed = history.iter().any(|r| hash_changed(r,m));
let interrupted = history.iter().find(|r| matches!(r.status, Status::Running | Status::Failed));
Item {
id: m.id.clone(),
db: m.db.clone(),
status: if changed || row.is_some_and(|r| r.status == Status::Running) { Status::Failed } else { row.map(|r| r.status).unwrap_or(Status::Pending) },
status: if changed || interrupted.is_some() { Status::Failed } else { row.map(|r| r.status).unwrap_or(Status::Pending) },
destructive: m.destructive,
baseline: row.map(|r| r.baseline).unwrap_or(false),
error: if changed { Some("An already applied SQL file changed. Publish a new migration instead of replaying it.".into()) } else { row.and_then(|r| r.error.clone()) },
error: if changed { Some("An already applied SQL file changed. Publish a new migration instead of replaying it.".into()) } else { interrupted.and_then(|r| r.error.clone()).or_else(|| row.and_then(|r| r.error.clone())) },
}
})
.collect())
}
fn recorded<'a>(rows: &'a [LedgerRow], m: &Migration) -> Option<&'a LedgerRow> {
rows.iter().rev().find(|r| r.db == m.db && r.id == m.id)
rows.iter().rev().find(|r| (r.db == m.db || shared_auth(&r.db) && shared_auth(&m.db)) && r.id.eq_ignore_ascii_case(&m.id))
}
fn effective_history<'a>(rows: &'a [LedgerRow], m: &Migration) -> Vec<&'a LedgerRow> {
if shared_auth(&m.db) { rows.iter().filter(|r| shared_auth(&r.db) && r.id.eq_ignore_ascii_case(&m.id)).collect() }
else { recorded(rows, m).into_iter().collect() }
}
fn preserve_auth_history(store: &dyn Store, rows: &[LedgerRow], list: &[Migration]) -> Result<()> {
let applied: Vec<_> = list.iter().filter(|m| shared_auth(&m.db)).filter_map(|m| recorded(rows,m))
.filter(|r| r.status == Status::Applied).cloned().collect();
store.put_batch(&applied)
}
/// Migrations that were published with different bytes but have the same effect, so a server that applied either
@@ -166,7 +226,7 @@ const SAME_EFFECT: &[(&str, &[&str])] = &[(
)];
fn same_effect(id: &str, a: &str, b: &str) -> bool {
SAME_EFFECT.iter().any(|(known, hashes)| *known == id
SAME_EFFECT.iter().any(|(known, hashes)| known.eq_ignore_ascii_case(id)
&& hashes.iter().any(|h| h.eq_ignore_ascii_case(a)) && hashes.iter().any(|h| h.eq_ignore_ascii_case(b)))
}
@@ -208,7 +268,7 @@ fn validate_list(list: &[Migration]) -> Result<()> {
for m in list {
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)) {
if !seen.insert((schema, m.id.to_ascii_lowercase())) {
return Err(Error::InvalidManifest(format!("duplicate migration {} ({})", m.id, m.db)));
}
}
@@ -223,12 +283,14 @@ fn pending_migrations<'a>(rows: &[LedgerRow], list: &'a [Migration]) -> Result<V
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)) {
for row in effective_history(rows,m) {
if matches!(row.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)));
}
if recorded(rows, m).is_some_and(|r| hash_changed(r, m)) {
if hash_changed(row,m) {
return Err(Error::Invalid(format!("Applied database update {} ({}) has a different checksum. A new migration is required; it was not replayed.", m.id, m.db)));
}
}
}
Ok(())
}
@@ -237,9 +299,11 @@ fn check_history(rows: &[LedgerRow], list: &[Migration]) -> Result<()> {
/// was brought up to date by hand).
pub fn baseline(store: &dyn Store, list: &[Migration]) -> Result<usize> {
let rows = store.load()?;
check_history(&rows,list)?;
preserve_auth_history(store,&rows,list)?;
let mut n = 0;
for m in list {
if rows.iter().any(|r| r.db == m.db && r.id == m.id && r.status == Status::Applied) {
if recorded(&rows,m).is_some_and(|r| r.status == Status::Applied) {
continue;
}
store.put(&LedgerRow { db: m.db.clone(), id: m.id.clone(), sha256: m.sha256.clone(), status: Status::Applied, error: None, baseline: true })?;
@@ -274,11 +338,13 @@ pub fn apply_pending(store: &dyn Store, list: &[Migration], dir: &Path, snapshot
let mut report = ApplyReport { applied: Vec::new(), failed: None, snapshot: None };
if files.is_empty() {
preserve_auth_history(store,&rows,list)?;
return Ok(report);
}
if any_destructive {
report.snapshot = Some(snapshot().map_err(|e| Error::Invalid(format!("No database update was applied because the safety backup failed: {e}")))?);
}
preserve_auth_history(store,&rows,list)?;
for (m, schema, path) in files {
tracing::info!(migration = %m.id, database = %m.db, "database migration starting");
store.put(&LedgerRow { db: m.db.clone(), id: m.id.clone(), sha256: m.sha256.clone(), status: Status::Running, error: Some("Interrupted SQL must be recovered before replay.".into()), baseline: false })?;
+40
View File
@@ -471,6 +471,46 @@ mod tests {
};
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();
// Shared auth must not run again when the active realm changes.
use crate::migrations::Store;
db.query("CREATE DATABASE acore_world_wildcard;").unwrap();
let migration_dir = root.join("migration-fixture");
fs::create_dir_all(migration_dir.join("auth")).unwrap();
fs::create_dir_all(migration_dir.join("world")).unwrap();
let auth_sql = b"INSERT INTO acore_auth.probe VALUES(45);";
fs::write(migration_dir.join("auth/shared_once.sql"), auth_sql).unwrap();
let auth_list = vec![crate::manifest::Migration { id:"shared_once".into(), db:"auth".into(), sha256:fsx::sha256_bytes(auth_sql), destructive:false, compatible_sha256:vec![] }];
assert_eq!(crate::migrations::apply_pending(&db, &auth_list, &migration_dir, &|| Ok("fixture".into())).unwrap().applied.len(), 1);
let wildcard = db.clone().for_realm(crate::realms::Mode::Wildcard);
assert_eq!(crate::migrations::pending_count(&wildcard, &auth_list).unwrap(), 0, "auth is shared by both realms");
assert!(crate::migrations::apply_pending(&wildcard, &auth_list, &migration_dir, &|| Ok("fixture".into())).unwrap().applied.is_empty());
assert_eq!(db.query("SELECT COUNT(*) FROM acore_auth.probe WHERE id=45;").unwrap(), "1");
let mut failed = crate::migrations::LedgerRow { db:"auth".into(), id:"shared_once".into(), sha256:auth_list[0].sha256.clone(), status:crate::migrations::Status::Failed, error:Some("interrupted fixture".into()), baseline:false };
db.put(&failed).unwrap();
assert!(crate::migrations::pending_count(&wildcard, &auth_list).is_err());
failed.status = crate::migrations::Status::Applied; failed.error = None; db.put(&failed).unwrap();
// Legacy per-world auth records are adopted without replaying their SQL.
let legacy_sql = b"INSERT INTO acore_auth.probe VALUES(46);";
fs::write(migration_dir.join("auth/legacy_auth.sql"), legacy_sql).unwrap();
db.query(&format!("INSERT INTO acore_auth.probe VALUES(46); REPLACE INTO acore_world.coa_manager_migrations VALUES('auth','legacy_auth','{}','applied',NOW(),NULL,0);", fsx::sha256_bytes(legacy_sql))).unwrap();
let legacy_auth = vec![crate::manifest::Migration { id:"legacy_auth".into(), db:"auth".into(), sha256:fsx::sha256_bytes(legacy_sql), destructive:false, compatible_sha256:vec![] }];
assert_eq!(crate::migrations::pending_count(&wildcard, &legacy_auth).unwrap(), 0);
assert!(crate::migrations::apply_pending(&wildcard, &legacy_auth, &migration_dir, &|| Ok("fixture".into())).unwrap().applied.is_empty());
assert_eq!(db.query("SELECT COUNT(*) FROM acore_auth.probe WHERE id=46;").unwrap(), "1");
// A core history cleanup cannot make previously recorded SQL pending again.
db.query("CREATE TABLE acore_world.updates(name VARCHAR(150) PRIMARY KEY) DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; INSERT INTO acore_world.updates VALUES('LEGACY_WORLD.sql'); INSERT INTO acore_world.probe VALUES(99);").unwrap();
db.load().unwrap();
db.query("DROP TABLE acore_world.updates;").unwrap();
let legacy_world_sql = b"INSERT INTO acore_world.probe VALUES(99);";
fs::write(migration_dir.join("world/legacy_world.sql"), legacy_world_sql).unwrap();
let legacy_world = vec![crate::manifest::Migration { id:"legacy_world".into(), db:"world".into(), sha256:fsx::sha256_bytes(legacy_world_sql), destructive:false, compatible_sha256:vec![] }];
assert_eq!(crate::migrations::pending_count(&db, &legacy_world).unwrap(), 0);
assert!(crate::migrations::apply_pending(&db, &legacy_world, &migration_dir, &|| Ok("fixture".into())).unwrap().applied.is_empty());
assert_eq!(db.query("SELECT COUNT(*) FROM acore_world.probe WHERE id=99;").unwrap(), "1");
db.query("DELETE FROM acore_world.probe WHERE id=99;").unwrap();
assert_eq!(db.recovery_objects("acore_characters").unwrap().len(), 4);
db.query("CREATE TABLE acore_characters.coa_squid_pending(path VARCHAR(255), sha256 CHAR(64)); INSERT INTO acore_characters.coa_squid_pending VALUES('playerbots/base/ai_playerbot_texts.sql', REPEAT('a',64));").unwrap();
let squid_problems = crate::squid::database_problems(&root, crate::realms::Mode::Coa, &|sql| {
+2 -1
View File
@@ -348,7 +348,8 @@ AC>";
let (port, _h) = fake_ra("Account not found.");
let mut ra = Ra::connect_to(port, "u", "p").unwrap();
assert!(ra.set_account_password("x", "newpass9").is_err(), "the name is validated before anything is sent");
assert!(ra.set_account_password("Nobody", "newpass9").unwrap_err().to_string().contains("not found"));
let error = ra.set_account_password("Nobody", "newpass9").unwrap_err().to_string();
assert!(error.contains("not found"), "Unexpected console error: {error}");
}
#[test]
+15 -12
View File
@@ -896,7 +896,7 @@ impl RepackEnv<'_> {
if realms.wildcard_created { modes.push(if realms.active == crate::realms::Mode::Coa { crate::realms::Mode::Wildcard } else { crate::realms::Mode::Coa }); }
let mut count = 0;
for (index, mode) in modes.into_iter().enumerate() {
let list: Vec<_> = manifest.migrations.iter().filter(|m| index == 0 || m.db != "auth").cloned().collect();
let list: Vec<_> = manifest.migrations.iter().filter(|m| index == 0 || !crate::migrations::shared_auth(&m.db)).cloned().collect();
count += crate::migrations::pending_count(&db.clone().for_realm(mode), &list)?;
}
Ok(count)
@@ -1061,16 +1061,18 @@ impl RepackEnv<'_> {
let result = (|| -> Result<()> {
crate::update_isolation::begin(self.root, self.meta_dir)?;
use crate::process::{observe, ServiceState};
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 {
if let Some(p) = crate::schema_check::check_with_report(&db.clone().for_realm(mode), self.root)?.first() {
return Err(Error::Invalid(format!("Database validation failed on {}: {}.{}: {}", mode.name(), p.database, p.table, p.detail)));
let identities = crate::backup::with_database(self.root, |db| {
if check_schema {
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 {
if let Some(p) = crate::schema_check::check_with_report(&db.clone().for_realm(mode), self.root)?.first() {
return Err(Error::Invalid(format!("Database validation failed on {}: {}.{}: {}", mode.name(), p.database, p.table, p.detail)));
}
}
}
Ok(())
})?; }
database_identities(db)
})?;
// 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) {
@@ -1108,6 +1110,7 @@ impl RepackEnv<'_> {
}
};
if healthy {
crate::backup::with_database(self.root, |db| verify_database_identities(db, &identities))?;
return Ok(());
}
let cause = crate::driver::startup_failure(self.root, &started);
@@ -1132,7 +1135,7 @@ fn migrate_databases(db: &crate::db::Db, root: &Path, manifest: &Manifest, stage
}
// 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();
let list: Vec<_> = manifest.migrations.iter().filter(|m| index == 0 || !crate::migrations::shared_auth(&m.db)).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());
@@ -1140,7 +1143,7 @@ fn migrate_databases(db: &crate::db::Db, root: &Path, manifest: &Manifest, stage
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();
let list: Vec<_> = manifest.migrations.iter().filter(|m| index == 0 || !crate::migrations::shared_auth(&m.db)).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() }
@@ -1176,7 +1179,7 @@ fn verify_database_identities(db: &crate::db::Db, before: &BTreeMap<String, std:
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}.")));
return Err(Error::Invalid(format!("The update changed or removed existing character or account identities in {schema}.")));
}
}
Ok(())