fix(migrations): adopt published ledgerless baseline
This commit is contained in:
1 parent
527efb510f
commit
2759fde81f
7 files changed
+572
-21
No files matched your search
@@ -11,6 +11,9 @@ use super::platform_key_name_reuse_v6;
|
||||
use super::schema_guard::{
|
||||
OWNED_RELATIONS, relation_exists, validate_required_relations, validate_schema_fingerprint,
|
||||
};
|
||||
use super::schema_guard_legacy_v1::{
|
||||
validate_ledgerless_baseline_fingerprint, validate_ledgerless_optional_fingerprints,
|
||||
};
|
||||
use super::{BASELINE_CHECKSUM, BASELINE_VERSION, apply_baseline};
|
||||
use crate::ext::ExtensionMigration;
|
||||
use sqlx::{PgConnection, PgPool, Row, Transaction, query};
|
||||
@@ -389,6 +392,19 @@ impl MigrationAuthority {
|
||||
if from < 13 {
|
||||
artifact_cleanup_indexes_v13::apply(&mut transaction, &Self::sequence()[12]).await?;
|
||||
}
|
||||
match inspect(&mut transaction).await? {
|
||||
MigrationPreflight::Current {
|
||||
version: CURRENT_VERSION,
|
||||
} => {}
|
||||
_ => {
|
||||
return Err(MigrationError::new(
|
||||
"apply_failed",
|
||||
"apply.postflight",
|
||||
Some(CURRENT_VERSION),
|
||||
"restore_known_good_backup",
|
||||
));
|
||||
}
|
||||
}
|
||||
transaction
|
||||
.commit()
|
||||
.await
|
||||
@@ -408,6 +424,12 @@ async fn inspect(connection: &mut PgConnection) -> Result<MigrationPreflight, Mi
|
||||
owned_exists |= relation_exists(connection, relation).await?;
|
||||
}
|
||||
if owned_exists {
|
||||
if is_ledgerless_legacy_baseline(connection, canonical_exists).await? {
|
||||
return Ok(MigrationPreflight::MigrationRequired {
|
||||
current: 0,
|
||||
target: CURRENT_VERSION,
|
||||
});
|
||||
}
|
||||
return Err(MigrationError::new(
|
||||
"partial_sequence",
|
||||
"preflight.core_missing",
|
||||
@@ -541,6 +563,47 @@ async fn inspect(connection: &mut PgConnection) -> Result<MigrationPreflight, Mi
|
||||
Ok(MigrationPreflight::Current { version: current })
|
||||
}
|
||||
}
|
||||
async fn is_ledgerless_legacy_baseline(
|
||||
connection: &mut PgConnection,
|
||||
canonical_exists: bool,
|
||||
) -> Result<bool, MigrationError> {
|
||||
if canonical_exists {
|
||||
return Ok(false);
|
||||
}
|
||||
for relation in [
|
||||
"__crank_migration_legacy_audit",
|
||||
"master_key_identities",
|
||||
"master_key_rotations",
|
||||
"admin_bootstrap_contracts",
|
||||
"admin_login_backoff",
|
||||
"admin_security_audit_events",
|
||||
"product_events",
|
||||
"product_event_daily_rollups",
|
||||
"onboarding_selections",
|
||||
"artifact_blobs",
|
||||
"artifact_sources",
|
||||
] {
|
||||
if relation_exists(connection, relation).await? {
|
||||
return Ok(false);
|
||||
}
|
||||
}
|
||||
for relation in owned_relations::BASELINE {
|
||||
if !relation_exists(connection, relation).await? {
|
||||
return Ok(false);
|
||||
}
|
||||
}
|
||||
validate_required_relations(connection, owned_relations::BASELINE, 1).await?;
|
||||
validate_ledgerless_baseline_fingerprint(connection).await?;
|
||||
let mcp_ledger = relation_exists(connection, "__crank_mcp_migrations").await?;
|
||||
if mcp_ledger {
|
||||
return Ok(false);
|
||||
}
|
||||
let mcp_sessions = relation_exists(connection, "mcp_transport_sessions").await?;
|
||||
let extension_ledger = relation_exists(connection, "__crank_ext_migrations").await?;
|
||||
validate_ledgerless_optional_fingerprints(connection, mcp_sessions, extension_ledger).await?;
|
||||
inspect_optional_legacy_for_ledgerless_baseline(connection).await?;
|
||||
Ok(true)
|
||||
}
|
||||
async fn validate_core_ledger(connection: &mut PgConnection) -> Result<(), MigrationError> {
|
||||
let rows = query("select version, description, checksum from __crank_core_migrations order by version limit 2")
|
||||
.fetch_all(connection)
|
||||
@@ -577,9 +640,21 @@ async fn validate_core_ledger(connection: &mut PgConnection) -> Result<(), Migra
|
||||
Ok(())
|
||||
}
|
||||
async fn inspect_optional_legacy(connection: &mut PgConnection) -> Result<(), MigrationError> {
|
||||
inspect_optional_legacy_with_policy(connection, false).await
|
||||
}
|
||||
async fn inspect_optional_legacy_for_ledgerless_baseline(
|
||||
connection: &mut PgConnection,
|
||||
) -> Result<(), MigrationError> {
|
||||
inspect_optional_legacy_with_policy(connection, true).await
|
||||
}
|
||||
async fn inspect_optional_legacy_with_policy(
|
||||
connection: &mut PgConnection,
|
||||
allow_sessions_without_ledger: bool,
|
||||
) -> Result<(), MigrationError> {
|
||||
let mcp_ledger = relation_exists(connection, "__crank_mcp_migrations").await?;
|
||||
let mcp_sessions = relation_exists(connection, "mcp_transport_sessions").await?;
|
||||
if mcp_ledger != mcp_sessions {
|
||||
if mcp_ledger != mcp_sessions && !(allow_sessions_without_ledger && !mcp_ledger && mcp_sessions)
|
||||
{
|
||||
return Err(MigrationError::new(
|
||||
"legacy_conflict",
|
||||
"preflight.legacy_mcp",
|
||||
|
||||
Reference in new issue
Block a user