fix(openapi): harden story 2.1 production lifecycle
This commit is contained in:
1 parent
2c94af6791
commit
bc03c33387
46 files changed
+2198
-276
No files matched your search
@@ -0,0 +1,44 @@
|
||||
use sqlx::{Postgres, Transaction, query};
|
||||
|
||||
use super::authority::{MigrationDescriptor, MigrationError};
|
||||
|
||||
pub(super) const SOURCE: &str = include_str!("artifact_cleanup_indexes_v13.sql");
|
||||
pub(super) const SOURCE_SHA256: &str =
|
||||
"abe822c967e1b2cc3966ede988e05eb2d9b2a062b539c45aaa8cad677395ab05";
|
||||
|
||||
pub(super) async fn apply(
|
||||
transaction: &mut Transaction<'_, Postgres>,
|
||||
descriptor: &MigrationDescriptor,
|
||||
) -> Result<(), MigrationError> {
|
||||
sqlx::raw_sql(SOURCE)
|
||||
.execute(&mut **transaction)
|
||||
.await
|
||||
.map_err(|_| {
|
||||
MigrationError::new(
|
||||
"apply_failed",
|
||||
"apply.artifact_cleanup_indexes",
|
||||
Some(13),
|
||||
"restore_known_good_backup",
|
||||
)
|
||||
})?;
|
||||
query(
|
||||
"insert into __crank_migrations (version, name, checksum, phase, compatibility)
|
||||
values ($1, $2, $3, $4, $5)",
|
||||
)
|
||||
.bind(descriptor.version)
|
||||
.bind(descriptor.name)
|
||||
.bind(&descriptor.checksum)
|
||||
.bind(descriptor.phase)
|
||||
.bind(descriptor.compatibility)
|
||||
.execute(&mut **transaction)
|
||||
.await
|
||||
.map_err(|_| {
|
||||
MigrationError::new(
|
||||
"apply_failed",
|
||||
"apply.canonical_ledger",
|
||||
Some(13),
|
||||
"restore_known_good_backup",
|
||||
)
|
||||
})?;
|
||||
Ok(())
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
-- Expand-only performance indexes for bounded artifact and import cleanup.
|
||||
-- No data rewrite or destructive schema operation is required.
|
||||
create index artifact_blobs_expired_claim_idx
|
||||
on artifact_blobs(claim_expires_at, digest)
|
||||
where claim_token is not null;
|
||||
|
||||
create index artifact_sources_blob_lifecycle_detached_idx
|
||||
on artifact_sources(blob_digest, lifecycle, detached_at);
|
||||
|
||||
create index artifact_sources_openapi_dangling_idx
|
||||
on artifact_sources(created_at, workspace_id, source_id)
|
||||
where lifecycle = 'active' and left(source_id, 12) = 'src_openapi_';
|
||||
|
||||
create index import_jobs_expires_at_idx
|
||||
on import_jobs(expires_at, id);
|
||||
|
||||
create index import_jobs_openapi_source_idx
|
||||
on import_jobs(workspace_id, ((preview_payload -> 'source' ->> 'source_id')))
|
||||
where preview_payload ? 'source';
|
||||
@@ -1,6 +1,7 @@
|
||||
use super::admin_auth_lifecycle_v8;
|
||||
use super::agent_catalog_lifecycle_v9;
|
||||
use super::approval_side_effects_v10;
|
||||
use super::artifact_cleanup_indexes_v13;
|
||||
use super::artifact_metadata_v12;
|
||||
use super::execution_outcome_v5;
|
||||
use super::master_key_identity_v7;
|
||||
@@ -21,8 +22,8 @@ mod contract;
|
||||
use contract::baseline_source_digest;
|
||||
use contract::validate_descriptors;
|
||||
const MIGRATION_LOCK_ID: i64 = 0x4352_414E_4B4D_4947;
|
||||
const CURRENT_VERSION: i64 = 12;
|
||||
const IMPLEMENTED_VERSIONS: &[i64] = &[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
|
||||
const CURRENT_VERSION: i64 = 13;
|
||||
const IMPLEMENTED_VERSIONS: &[i64] = &[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13];
|
||||
const BASELINE_SOURCE_SHA256: &str =
|
||||
"eb1656fc5b4b5be9ee390d237d1d58e4b2274ae5ba9b7ba06a2f3f860dfda675";
|
||||
const CONSOLIDATION_SOURCE: &str = include_str!("consolidation_v2.sql");
|
||||
@@ -254,6 +255,12 @@ impl MigrationAuthority {
|
||||
artifact_metadata_v12::SOURCE_SHA256,
|
||||
11,
|
||||
),
|
||||
expand_descriptor(
|
||||
13,
|
||||
"artifact-cleanup-indexes-v13",
|
||||
artifact_cleanup_indexes_v13::SOURCE_SHA256,
|
||||
12,
|
||||
),
|
||||
]
|
||||
}
|
||||
pub fn validate_sequence() -> Result<(), MigrationError> {
|
||||
@@ -379,6 +386,9 @@ impl MigrationAuthority {
|
||||
if from < 12 {
|
||||
artifact_metadata_v12::apply(&mut transaction, &Self::sequence()[11]).await?;
|
||||
}
|
||||
if from < 13 {
|
||||
artifact_cleanup_indexes_v13::apply(&mut transaction, &Self::sequence()[12]).await?;
|
||||
}
|
||||
transaction
|
||||
.commit()
|
||||
.await
|
||||
|
||||
@@ -128,6 +128,8 @@ pub(super) fn validate_descriptors(
|
||||
!= onboarding_product_events_v11::SOURCE_SHA256
|
||||
|| sha256_hex(artifact_metadata_v12::SOURCE.as_bytes())
|
||||
!= artifact_metadata_v12::SOURCE_SHA256
|
||||
|| sha256_hex(artifact_cleanup_indexes_v13::SOURCE.as_bytes())
|
||||
!= artifact_cleanup_indexes_v13::SOURCE_SHA256
|
||||
{
|
||||
return Err(MigrationError::new(
|
||||
"invalid_contract",
|
||||
|
||||
@@ -624,6 +624,13 @@ pub(super) async fn validate_schema_fingerprint(
|
||||
} else {
|
||||
super::schema_guard_v12::validate_v12_artifact_metadata(connection).await?;
|
||||
}
|
||||
if current_version < 13 {
|
||||
if !super::schema_guard_v13::validate_v13_absent(connection).await? {
|
||||
return Err(schema_error(current_version));
|
||||
}
|
||||
} else {
|
||||
super::schema_guard_v13::validate_v13_artifact_cleanup_indexes(connection).await?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
@@ -12,6 +12,18 @@ const OWNED_RELATIONS: &[(&str, &str)] = &[
|
||||
("artifact_sources_workspace_created_idx", "i"),
|
||||
];
|
||||
|
||||
const OWNED_RELATIONS_WITH_V13_INDEXES: &[(&str, &str)] = &[
|
||||
("artifact_blobs", "r"),
|
||||
("artifact_blobs_artifact_ref_key", "i"),
|
||||
("artifact_blobs_expired_claim_idx", "i"),
|
||||
("artifact_blobs_pkey", "i"),
|
||||
("artifact_sources", "r"),
|
||||
("artifact_sources_blob_lifecycle_detached_idx", "i"),
|
||||
("artifact_sources_openapi_dangling_idx", "i"),
|
||||
("artifact_sources_pkey", "i"),
|
||||
("artifact_sources_workspace_created_idx", "i"),
|
||||
];
|
||||
|
||||
const BLOB_COLUMNS: &[(&str, &str, bool, Option<&str>)] = &[
|
||||
("digest", "text", false, None),
|
||||
("artifact_ref", "text", false, None),
|
||||
@@ -141,7 +153,8 @@ pub(super) async fn validate_v12_absent(
|
||||
pub(super) async fn validate_v12_artifact_metadata(
|
||||
connection: &mut PgConnection,
|
||||
) -> Result<(), MigrationError> {
|
||||
if owned_relations(connection).await? != OWNED_RELATIONS {
|
||||
let relations = owned_relations(connection).await?;
|
||||
if relations != OWNED_RELATIONS && relations != OWNED_RELATIONS_WITH_V13_INDEXES {
|
||||
return Err(schema_error(12));
|
||||
}
|
||||
validate_table_properties(connection).await?;
|
||||
@@ -202,7 +215,7 @@ async fn owned_relations(
|
||||
let kind = row
|
||||
.try_get::<String, _>("relkind")
|
||||
.map_err(|_| MigrationError::storage("preflight.schema"))?;
|
||||
let expected = OWNED_RELATIONS
|
||||
let expected = OWNED_RELATIONS_WITH_V13_INDEXES
|
||||
.iter()
|
||||
.copied()
|
||||
.find(|(expected_name, expected_kind)| {
|
||||
@@ -346,14 +359,22 @@ async fn validate_indexes(connection: &mut PgConnection) -> Result<(), Migration
|
||||
.iter()
|
||||
.filter_map(|row| row.try_get::<String, _>("index_name").ok())
|
||||
.collect::<Vec<_>>();
|
||||
if names
|
||||
!= [
|
||||
"artifact_blobs_artifact_ref_key",
|
||||
"artifact_blobs_pkey",
|
||||
"artifact_sources_pkey",
|
||||
"artifact_sources_workspace_created_idx",
|
||||
]
|
||||
{
|
||||
let base_indexes = [
|
||||
"artifact_blobs_artifact_ref_key",
|
||||
"artifact_blobs_pkey",
|
||||
"artifact_sources_pkey",
|
||||
"artifact_sources_workspace_created_idx",
|
||||
];
|
||||
let expanded_indexes = [
|
||||
"artifact_blobs_artifact_ref_key",
|
||||
"artifact_blobs_expired_claim_idx",
|
||||
"artifact_blobs_pkey",
|
||||
"artifact_sources_blob_lifecycle_detached_idx",
|
||||
"artifact_sources_openapi_dangling_idx",
|
||||
"artifact_sources_pkey",
|
||||
"artifact_sources_workspace_created_idx",
|
||||
];
|
||||
if names != base_indexes && names != expanded_indexes {
|
||||
return Err(schema_error(12));
|
||||
}
|
||||
let row = rows.iter().find(|row| {
|
||||
|
||||
@@ -0,0 +1,178 @@
|
||||
use sqlx::{PgConnection, Row, query};
|
||||
|
||||
use super::{
|
||||
authority::MigrationError,
|
||||
schema_guard::{normalize_definition, schema_error},
|
||||
};
|
||||
|
||||
struct IndexContract {
|
||||
name: &'static str,
|
||||
table: &'static str,
|
||||
columns: &'static [&'static str],
|
||||
predicate: Option<&'static str>,
|
||||
}
|
||||
|
||||
const CLEANUP_INDEXES: &[IndexContract] = &[
|
||||
IndexContract {
|
||||
name: "artifact_blobs_expired_claim_idx",
|
||||
table: "artifact_blobs",
|
||||
columns: &["claim_expires_at", "digest"],
|
||||
predicate: Some("claim_tokenisnotnull"),
|
||||
},
|
||||
IndexContract {
|
||||
name: "artifact_sources_blob_lifecycle_detached_idx",
|
||||
table: "artifact_sources",
|
||||
columns: &["blob_digest", "lifecycle", "detached_at"],
|
||||
predicate: None,
|
||||
},
|
||||
IndexContract {
|
||||
name: "artifact_sources_openapi_dangling_idx",
|
||||
table: "artifact_sources",
|
||||
columns: &["created_at", "workspace_id", "source_id"],
|
||||
predicate: Some("lifecycle='active'::textand\"left\"source_id,12='src_openapi_'::text"),
|
||||
},
|
||||
IndexContract {
|
||||
name: "import_jobs_expires_at_idx",
|
||||
table: "import_jobs",
|
||||
columns: &["expires_at", "id"],
|
||||
predicate: None,
|
||||
},
|
||||
IndexContract {
|
||||
name: "import_jobs_openapi_source_idx",
|
||||
table: "import_jobs",
|
||||
columns: &[
|
||||
"workspace_id",
|
||||
"preview_payload->'source'::text->>'source_id'::text",
|
||||
],
|
||||
predicate: Some("preview_payload?'source'::text"),
|
||||
},
|
||||
];
|
||||
|
||||
pub(super) async fn validate_v13_absent(
|
||||
connection: &mut PgConnection,
|
||||
) -> Result<bool, MigrationError> {
|
||||
Ok(cleanup_indexes(connection).await?.is_empty())
|
||||
}
|
||||
|
||||
pub(super) async fn validate_v13_artifact_cleanup_indexes(
|
||||
connection: &mut PgConnection,
|
||||
) -> Result<(), MigrationError> {
|
||||
let rows = query(
|
||||
"select idx.relname as index_name, t.relname as table_name, am.amname as access_method,
|
||||
i.indisvalid, i.indisready, i.indisunique, i.indnkeyatts, i.indnatts,
|
||||
pg_get_indexdef(i.indexrelid, 1, true) as first_column,
|
||||
pg_get_indexdef(i.indexrelid, 2, true) as second_column,
|
||||
pg_get_indexdef(i.indexrelid, 3, true) as third_column,
|
||||
pg_get_expr(i.indpred, i.indrelid) as predicate
|
||||
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 pg_catalog.pg_am am on am.oid = idx.relam
|
||||
where n.nspname = current_schema()
|
||||
and idx.relname = any($1)
|
||||
order by idx.relname",
|
||||
)
|
||||
.bind(index_names())
|
||||
.fetch_all(connection)
|
||||
.await
|
||||
.map_err(|_| MigrationError::storage("preflight.schema"))?;
|
||||
if rows.len() != CLEANUP_INDEXES.len() {
|
||||
return Err(schema_error(13));
|
||||
}
|
||||
|
||||
for row in rows {
|
||||
let name = row
|
||||
.try_get::<String, _>("index_name")
|
||||
.map_err(|_| MigrationError::storage("preflight.schema"))?;
|
||||
let Some(contract) = CLEANUP_INDEXES.iter().find(|index| index.name == name) else {
|
||||
return Err(schema_error(13));
|
||||
};
|
||||
if !index_matches_contract(&row, contract) {
|
||||
return Err(schema_error(13));
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn index_matches_contract(row: &sqlx::postgres::PgRow, contract: &IndexContract) -> bool {
|
||||
let Ok(table_name) = row.try_get::<String, _>("table_name") else {
|
||||
return false;
|
||||
};
|
||||
let Ok(access_method) = row.try_get::<String, _>("access_method") else {
|
||||
return false;
|
||||
};
|
||||
let Ok(indnkeyatts) = row.try_get::<i16, _>("indnkeyatts") else {
|
||||
return false;
|
||||
};
|
||||
let Ok(indnatts) = row.try_get::<i16, _>("indnatts") else {
|
||||
return false;
|
||||
};
|
||||
let columns = [
|
||||
row.try_get::<Option<String>, _>("first_column"),
|
||||
row.try_get::<Option<String>, _>("second_column"),
|
||||
row.try_get::<Option<String>, _>("third_column"),
|
||||
];
|
||||
let expected_columns = contract
|
||||
.columns
|
||||
.iter()
|
||||
.map(|column| normalize_definition(column))
|
||||
.collect::<Vec<_>>();
|
||||
let actual_columns = columns
|
||||
.iter()
|
||||
.take(contract.columns.len())
|
||||
.map(|column| {
|
||||
column
|
||||
.as_ref()
|
||||
.ok()
|
||||
.and_then(|column| column.as_deref())
|
||||
.map(normalize_definition)
|
||||
})
|
||||
.collect::<Option<Vec<_>>>();
|
||||
let trailing_columns_are_absent = columns.iter().skip(contract.columns.len()).all(|column| {
|
||||
column
|
||||
.as_ref()
|
||||
.is_ok_and(|column| column.as_deref().is_none_or(str::is_empty))
|
||||
});
|
||||
let predicate = row
|
||||
.try_get::<Option<String>, _>("predicate")
|
||||
.ok()
|
||||
.flatten()
|
||||
.map(|predicate| normalize_definition(&predicate));
|
||||
|
||||
table_name == contract.table
|
||||
&& access_method == "btree"
|
||||
&& row.try_get::<bool, _>("indisvalid").ok() == Some(true)
|
||||
&& row.try_get::<bool, _>("indisready").ok() == Some(true)
|
||||
&& row.try_get::<bool, _>("indisunique").ok() == Some(false)
|
||||
&& indnkeyatts == contract.columns.len() as i16
|
||||
&& indnatts == contract.columns.len() as i16
|
||||
&& actual_columns.as_ref() == Some(&expected_columns)
|
||||
&& trailing_columns_are_absent
|
||||
&& predicate.as_deref() == contract.predicate
|
||||
}
|
||||
|
||||
fn index_names() -> Vec<&'static str> {
|
||||
CLEANUP_INDEXES.iter().map(|index| index.name).collect()
|
||||
}
|
||||
|
||||
async fn cleanup_indexes(connection: &mut PgConnection) -> Result<Vec<String>, MigrationError> {
|
||||
query(
|
||||
"select idx.relname
|
||||
from pg_catalog.pg_class idx
|
||||
join pg_catalog.pg_namespace n on n.oid = idx.relnamespace
|
||||
where n.nspname = current_schema()
|
||||
and idx.relname = any($1)
|
||||
order by idx.relname",
|
||||
)
|
||||
.bind(index_names())
|
||||
.fetch_all(connection)
|
||||
.await
|
||||
.map_err(|_| MigrationError::storage("preflight.schema"))?
|
||||
.into_iter()
|
||||
.map(|row| {
|
||||
row.try_get("relname")
|
||||
.map_err(|_| MigrationError::storage("preflight.schema"))
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
Reference in new issue
Block a user