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
@@ -0,0 +1,198 @@
|
||||
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 =
|
||||
"0dbd7357a80c0d772de03ea2833013932697187b32fbd7b54be594e224e5f82f";
|
||||
const LEDGERLESS_MCP_FINGERPRINTS_SHA256: &[&str] = &[
|
||||
// Initial published session table.
|
||||
"53b58899dc388609cbd83fe8321a48878d610952a39d13b5fc8f05149b703683",
|
||||
// Published session table after supports_elicitation was added.
|
||||
"250e3e57f02283e9300bdff549305d49a6a875717ec4e4c332a2b9af3f883f09",
|
||||
// Same published contract after an in-place upgrade from the initial layout.
|
||||
"23c02db67cf83a3834b1ed7caa417eaafd7bcc69657d598ba831f06aeded4a62",
|
||||
];
|
||||
const LEDGERLESS_EXTENSION_FINGERPRINT_SHA256: &str =
|
||||
"da06947028e3c18cc6ba93a58775722b425fd9989095f069316a146a2f45e921";
|
||||
|
||||
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.ordinal_position, 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",
|
||||
)
|
||||
}
|
||||
Reference in new issue
Block a user