197 lines
7.6 KiB
Rust
197 lines
7.6 KiB
Rust
use sha2::{Digest, Sha256};
|
|
use sqlx::PgConnection;
|
|
|
|
use super::authority::MigrationError;
|
|
|
|
// Exact PostgreSQL 16 catalog contract produced by the last published
|
|
// pre-ledger Community schema (commit 8318e4b).
|
|
const LEDGERLESS_BASELINE_FINGERPRINT_SHA256: &str =
|
|
"36624ca42a28c1388f9c50e6d6c489a8e5ca1f73441a894d9af89e5e115b542c";
|
|
const LEDGERLESS_MCP_FINGERPRINTS_SHA256: &[&str] = &[
|
|
// Initial published session table.
|
|
"9240c3d85dbc9eeddb1cd99661ec8f7d8d6ece0e62ec486151dcc951a7f3c81c",
|
|
// Published session table after supports_elicitation was added.
|
|
"ac9f99a7e667552a07480d5d45379dd0a8b529b00ba98855ce6725535198249f",
|
|
];
|
|
const LEDGERLESS_EXTENSION_FINGERPRINT_SHA256: &str =
|
|
"0809a80c0bb6e80f68d0557006c4b62f2e63556f61f6fdedc5abc324950e2587";
|
|
|
|
const BASELINE_RELATIONS: &[&str] = &[
|
|
"workspaces",
|
|
"users",
|
|
"memberships",
|
|
"user_sessions",
|
|
"invitation_tokens",
|
|
"platform_api_keys",
|
|
"operations",
|
|
"operation_versions",
|
|
"published_operations",
|
|
"operation_samples",
|
|
"descriptors",
|
|
"agents",
|
|
"agent_versions",
|
|
"published_agents",
|
|
"agent_operation_bindings",
|
|
"secrets",
|
|
"secret_versions",
|
|
"auth_profiles",
|
|
"workspace_upstreams",
|
|
"yaml_import_jobs",
|
|
"import_jobs",
|
|
"approval_requests",
|
|
"invocation_logs",
|
|
"usage_rollups",
|
|
];
|
|
|
|
pub(super) async fn validate_ledgerless_baseline_fingerprint(
|
|
connection: &mut PgConnection,
|
|
) -> Result<(), MigrationError> {
|
|
let actual = catalog_fingerprint(connection, BASELINE_RELATIONS).await?;
|
|
if actual == LEDGERLESS_BASELINE_FINGERPRINT_SHA256 {
|
|
Ok(())
|
|
} else {
|
|
Err(fingerprint_error())
|
|
}
|
|
}
|
|
|
|
pub(super) async fn validate_ledgerless_optional_fingerprints(
|
|
connection: &mut PgConnection,
|
|
has_mcp_sessions: bool,
|
|
has_extension_ledger: bool,
|
|
) -> Result<(), MigrationError> {
|
|
if has_mcp_sessions {
|
|
let actual = catalog_fingerprint(connection, &["mcp_transport_sessions"]).await?;
|
|
if !LEDGERLESS_MCP_FINGERPRINTS_SHA256.contains(&actual.as_str()) {
|
|
return Err(fingerprint_error());
|
|
}
|
|
}
|
|
if has_extension_ledger {
|
|
let actual = catalog_fingerprint(connection, &["__crank_ext_migrations"]).await?;
|
|
if actual != LEDGERLESS_EXTENSION_FINGERPRINT_SHA256 {
|
|
return Err(fingerprint_error());
|
|
}
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
async fn catalog_fingerprint(
|
|
connection: &mut PgConnection,
|
|
relations: &[&str],
|
|
) -> Result<String, MigrationError> {
|
|
let relations = relations
|
|
.iter()
|
|
.map(|value| (*value).to_owned())
|
|
.collect::<Vec<_>>();
|
|
let unsafe_catalog_state = sqlx::query_scalar::<_, bool>(
|
|
"with selected(table_name) as (select unnest($1::text[]))
|
|
select
|
|
exists (
|
|
select 1 from pg_catalog.pg_class c
|
|
join pg_catalog.pg_namespace n on n.oid = c.relnamespace
|
|
join selected s on s.table_name = c.relname
|
|
where n.nspname = current_schema()
|
|
and (c.relpersistence <> 'p' or c.relrowsecurity
|
|
or c.relforcerowsecurity or c.relreplident <> 'd')
|
|
)
|
|
or exists (
|
|
select 1 from pg_catalog.pg_index i
|
|
join pg_catalog.pg_class t on t.oid = i.indrelid
|
|
join pg_catalog.pg_namespace n on n.oid = t.relnamespace
|
|
join selected s on s.table_name = t.relname
|
|
where n.nspname = current_schema()
|
|
and (not i.indisvalid or not i.indisready or not i.indislive)
|
|
)
|
|
or exists (
|
|
select 1 from pg_catalog.pg_policy p
|
|
join pg_catalog.pg_class t on t.oid = p.polrelid
|
|
join pg_catalog.pg_namespace n on n.oid = t.relnamespace
|
|
join selected s on s.table_name = t.relname
|
|
where n.nspname = current_schema()
|
|
)
|
|
or exists (
|
|
select 1 from pg_catalog.pg_trigger tg
|
|
join pg_catalog.pg_class t on t.oid = tg.tgrelid
|
|
join pg_catalog.pg_namespace n on n.oid = t.relnamespace
|
|
join selected s on s.table_name = t.relname
|
|
where n.nspname = current_schema() and tg.tgenabled <> 'O'
|
|
)",
|
|
)
|
|
.bind(relations.clone())
|
|
.fetch_one(&mut *connection)
|
|
.await
|
|
.map_err(|_| MigrationError::storage("preflight.legacy_fingerprint"))?;
|
|
if unsafe_catalog_state {
|
|
return Err(fingerprint_error());
|
|
}
|
|
let fingerprint = sqlx::query_scalar::<_, String>(
|
|
"with baseline(table_name) as (select unnest($1::text[])), relation_rows as (
|
|
select jsonb_build_array('relation', c.relname, c.relkind::text) item
|
|
from pg_catalog.pg_class c
|
|
join pg_catalog.pg_namespace n on n.oid = c.relnamespace
|
|
join baseline b on b.table_name = c.relname
|
|
where n.nspname = current_schema()
|
|
), column_rows as (
|
|
select jsonb_build_array(
|
|
'column', c.table_name, c.column_name,
|
|
c.data_type, c.udt_name, c.is_nullable, coalesce(c.column_default, '')
|
|
) item
|
|
from information_schema.columns c
|
|
join baseline b using (table_name)
|
|
where c.table_schema = current_schema()
|
|
), constraint_rows as (
|
|
select jsonb_build_array(
|
|
'constraint', t.relname, c.conname, c.contype::text,
|
|
c.convalidated, pg_get_constraintdef(c.oid, true)
|
|
) item
|
|
from pg_catalog.pg_constraint c
|
|
join pg_catalog.pg_class t on t.oid = c.conrelid
|
|
join pg_catalog.pg_namespace n on n.oid = t.relnamespace
|
|
join baseline b on b.table_name = t.relname
|
|
where n.nspname = current_schema()
|
|
), index_rows as (
|
|
select jsonb_build_array(
|
|
'index', t.relname, idx.relname,
|
|
replace(pg_get_indexdef(i.indexrelid), format('%I.', current_schema()), '')
|
|
) item
|
|
from pg_catalog.pg_index i
|
|
join pg_catalog.pg_class idx on idx.oid = i.indexrelid
|
|
join pg_catalog.pg_class t on t.oid = i.indrelid
|
|
join pg_catalog.pg_namespace n on n.oid = t.relnamespace
|
|
join baseline b on b.table_name = t.relname
|
|
where n.nspname = current_schema()
|
|
), trigger_rows as (
|
|
select jsonb_build_array(
|
|
'trigger', t.relname, tg.tgname,
|
|
replace(pg_get_triggerdef(tg.oid, true), format('%I.', current_schema()), '')
|
|
) item
|
|
from pg_catalog.pg_trigger tg
|
|
join pg_catalog.pg_class t on t.oid = tg.tgrelid
|
|
join pg_catalog.pg_namespace n on n.oid = t.relnamespace
|
|
join baseline b on b.table_name = t.relname
|
|
where n.nspname = current_schema() and not tg.tgisinternal
|
|
), all_rows as (
|
|
select item from relation_rows
|
|
union all select item from column_rows
|
|
union all select item from constraint_rows
|
|
union all select item from index_rows
|
|
union all select item from trigger_rows
|
|
)
|
|
select coalesce(jsonb_agg(item order by item::text), '[]'::jsonb)::text
|
|
from all_rows",
|
|
)
|
|
.bind(relations)
|
|
.fetch_one(connection)
|
|
.await
|
|
.map_err(|_| MigrationError::storage("preflight.legacy_fingerprint"))?;
|
|
Ok(format!("{:x}", Sha256::digest(fingerprint.as_bytes())))
|
|
}
|
|
|
|
fn fingerprint_error() -> MigrationError {
|
|
MigrationError::new(
|
|
"partial_sequence",
|
|
"preflight.legacy_fingerprint",
|
|
Some(1),
|
|
"restore_known_good_backup",
|
|
)
|
|
}
|