v0.3.4-pre.003

This commit is contained in:
2026-08-30 21:37:02 +02:00
parent dfa3723a7f
commit a704a5722e
37 changed files with 1088 additions and 75 deletions

View File

@@ -1,5 +1,5 @@
// file: crates/ksp-store-postgres-lib/src/lib.rs
// version: 15
// version: 16
#![warn(missing_docs)]
#![deny(unreachable_pub)]
@@ -23,6 +23,9 @@
//! `PostgresBackend` while preserving the existing narrow backend bridge.
//! `0.3.4-pre.002` registers additive V002 and its two minimal RAW account
//! tables with canonical state/observation PKs and the observation-state FK.
//! `0.3.4-pre.003` completes V002 with exact physical bounds, one unfiltered
//! navigation index, external-schema compatibility and the bounded prerelease
//! checksum transition from the provisional `pre.002` schema.
//!
//! This crate depends on `ksp-store-api` and never on `ksp-store-lib`. The
//! common facade consumes only this crate's narrow backend bridge and never
@@ -99,9 +102,11 @@ pub(crate) use self::schema::V001_RESOURCES;
pub(crate) use self::schema::V002_RESOURCES;
/// Private physical schema resource inspector consumed by the migration engine.
pub(crate) use self::schema::inspect_resource;
/// Private V001 adoption probe consumed by the migration engine.
pub(crate) use self::schema::managed_v001_objects_exist;
/// Private external-schema compatibility gate consumed by the migration engine.
/// Private managed-schema adoption probe consumed by the migration engine.
pub(crate) use self::schema::managed_schema_objects_exist;
/// Private V001 external-schema compatibility gate consumed by the migration engine.
pub(crate) use self::schema::verify_v001_external_compatibility;
/// Private V002 external-schema compatibility gate consumed by the migration engine.
pub(crate) use self::schema::verify_v002_external_compatibility;
const _: &str = crate::TRACING_TARGET;

View File

@@ -1,5 +1,5 @@
// file: crates/ksp-store-postgres-lib/src/migration.rs
// version: 8
// version: 9
use sha2::Digest; // rust-rules: trait-import
@@ -9,6 +9,7 @@ const EMBEDDED_MIGRATIONS: &[EmbeddedMigration] = &[
checksum: MigrationChecksum::LegacySql(include_str!("../migrations/v000_bootstrap/tables/001_ksp_store_schema_migrations.sql")),
hook: MigrationHook::None,
name: "bootstrap",
previous_checksums: &[],
resources: crate::V000_RESOURCES,
version: 0,
},
@@ -16,6 +17,7 @@ const EMBEDDED_MIGRATIONS: &[EmbeddedMigration] = &[
checksum: MigrationChecksum::Resources,
hook: MigrationHook::StoreIdentity,
name: "raw_transaction",
previous_checksums: &[],
resources: crate::V001_RESOURCES,
version: 1,
},
@@ -23,12 +25,14 @@ const EMBEDDED_MIGRATIONS: &[EmbeddedMigration] = &[
checksum: MigrationChecksum::Resources,
hook: MigrationHook::None,
name: "raw_account_state",
previous_checksums: &[V002_PROVISIONAL_CHECKSUM_PRE_002],
resources: crate::V002_RESOURCES,
version: 2,
},
];
const HEX_LOWER: &[u8; 16] = b"0123456789abcdef";
const HISTORY_INSERT_SQL: &str = "INSERT INTO ksp_store_schema_migrations (version, name, checksum, applied_at) VALUES ($1, $2, $3, CURRENT_TIMESTAMP)";
const HISTORY_UPDATE_CHECKSUM_SQL: &str = "UPDATE ksp_store_schema_migrations SET checksum = $1 WHERE version = $2 AND name = $3 AND checksum = $4";
const HISTORY_LOAD_SQL: &str = "SELECT version, name, checksum FROM ksp_store_schema_migrations ORDER BY version";
const IDENTITY_INSERT_SQL: &str = "INSERT INTO ksp_store_identity (singleton, network) VALUES (1, $1)";
const IDENTITY_LOAD_SQL: &str = "SELECT singleton, network FROM ksp_store_identity ORDER BY singleton LIMIT 2";
@@ -40,6 +44,7 @@ const METADATA_EXISTS_SQL: &str = r#"SELECT EXISTS (
AND table_type = 'BASE TABLE'
)"#;
const SET_STATEMENT_TIMEOUT_SQL: &str = "SELECT set_config('statement_timeout', $1, true)";
const V002_PROVISIONAL_CHECKSUM_PRE_002: &str = "30ac87496f1bb3805d816660891d7eab2127c599a636eb40c17ade5926311f55";
struct AppliedMigration {
checksum: std::string::String,
@@ -58,6 +63,7 @@ struct EmbeddedMigration {
checksum: MigrationChecksum,
hook: MigrationHook,
name: &'static str,
previous_checksums: &'static [&'static str],
resources: &'static [crate::SchemaResource],
version: i64,
}
@@ -140,7 +146,7 @@ async fn bootstrap_inner(
std::result::Result::Ok(value) => value,
std::result::Result::Err(error) => return std::result::Result::Err(error),
};
let (next_index, mutation_mode) = if metadata_exists {
let (next_index, mutation_mode, applied_history) = if metadata_exists {
let metadata_result = crate::inspect_resource(&transaction, &crate::V000_RESOURCES[0]).await;
match metadata_result {
std::result::Result::Ok(crate::SchemaResourceState::Compatible) => {},
@@ -155,18 +161,18 @@ async fn bootstrap_inner(
std::result::Result::Ok(value) => value,
std::result::Result::Err(error) => return std::result::Result::Err(error),
};
let validation_result = validate_history(history.as_slice(), EMBEDDED_MIGRATIONS);
let validation_result = validate_history(history.as_slice(), EMBEDDED_MIGRATIONS, schema_autoupdate);
let index = match validation_result {
std::result::Result::Ok(value) => value,
std::result::Result::Err(error) => return std::result::Result::Err(error),
};
(index, SchemaMutationMode::Update)
(index, SchemaMutationMode::Update, std::option::Option::Some(history))
} else {
if !schema_autocreate {
log_schema_block("schema_autocreate_disabled");
return std::result::Result::Err(crate::PostgresBackendError::new(crate::PostgresBackendErrorKind::MigrationFailed, "schema_autocreate_disabled"));
}
let managed_result = crate::managed_v001_objects_exist(&transaction).await;
let managed_result = crate::managed_schema_objects_exist(&transaction).await;
let managed_objects_exist = match managed_result {
std::result::Result::Ok(value) => value,
std::result::Result::Err(error) => return std::result::Result::Err(error),
@@ -175,12 +181,18 @@ async fn bootstrap_inner(
log_schema_block("schema_adoption_disabled");
return std::result::Result::Err(crate::PostgresBackendError::new(crate::PostgresBackendErrorKind::MigrationFailed, "schema_adoption_disabled"));
}
(0, SchemaMutationMode::Create)
(0, SchemaMutationMode::Create, std::option::Option::None)
};
let existing_schema_result = verify_or_repair_applied_migrations(&transaction, next_index, schema_autoupdate).await;
if let std::result::Result::Err(error) = existing_schema_result {
return std::result::Result::Err(error);
}
if let std::option::Option::Some(history) = applied_history.as_deref() {
let checksum_result = reconcile_applied_history_checksums(&transaction, history, next_index, schema_autoupdate).await;
if let std::result::Result::Err(error) = checksum_result {
return std::result::Result::Err(error);
}
}
let existing_hook_result = run_applied_migration_hooks(&transaction, network, next_index).await;
if let std::result::Result::Err(error) = existing_hook_result {
return std::result::Result::Err(error);
@@ -528,12 +540,14 @@ async fn set_statement_timeout(
}
async fn verify_migration_contract(transaction: &deadpool_postgres::Transaction<'_>, version: i64) -> std::result::Result<(), crate::PostgresBackendError> {
if version == 1 {
let result = crate::verify_v001_external_compatibility(transaction).await;
if let std::result::Result::Err(error) = result {
log_schema_block(error.phase());
return std::result::Result::Err(error);
}
let result = match version {
1 => crate::verify_v001_external_compatibility(transaction).await,
2 => crate::verify_v002_external_compatibility(transaction).await,
_ => std::result::Result::Ok(()),
};
if let std::result::Result::Err(error) = result {
log_schema_block(error.phase());
return std::result::Result::Err(error);
}
return std::result::Result::Ok(());
}
@@ -582,6 +596,41 @@ async fn verify_or_repair_applied_migrations(
return std::result::Result::Ok(());
}
async fn reconcile_applied_history_checksums(
transaction: &deadpool_postgres::Transaction<'_>,
history: &[AppliedMigration],
applied_count: usize,
schema_autoupdate: bool,
) -> std::result::Result<(), crate::PostgresBackendError> {
if !schema_autoupdate {
return std::result::Result::Ok(());
}
for (index, applied) in history.iter().take(applied_count).enumerate() {
let migration = &EMBEDDED_MIGRATIONS[index];
let expected_checksum = migration_checksum(migration);
if applied.checksum == expected_checksum {
continue;
}
if !migration.previous_checksums.contains(&applied.checksum.as_str()) {
return std::result::Result::Err(crate::PostgresBackendError::new(crate::PostgresBackendErrorKind::MigrationMismatch, "history_diverged"));
}
let result = transaction.execute(HISTORY_UPDATE_CHECKSUM_SQL, &[&expected_checksum, &migration.version, &migration.name, &applied.checksum]).await;
match result {
std::result::Result::Ok(1) => {
ksp_logging_lib::warn!(
target: crate::TRACING_TARGET,
migration_version = migration.version,
"upgraded known prerelease PostgreSQL Store migration checksum after schema reconciliation"
);
},
std::result::Result::Ok(_) | std::result::Result::Err(_) => {
return std::result::Result::Err(crate::PostgresBackendError::new(crate::PostgresBackendErrorKind::MigrationFailed, "history_checksum_update"));
},
}
}
return std::result::Result::Ok(());
}
fn schema_autoupdate_disabled_error() -> crate::PostgresBackendError {
return crate::PostgresBackendError::new(crate::PostgresBackendErrorKind::MigrationMismatch, "schema_autoupdate_disabled");
}
@@ -615,7 +664,11 @@ fn validate_embedded_registry(migrations: &[EmbeddedMigration]) -> std::result::
return std::result::Result::Ok(());
}
fn validate_history(history: &[AppliedMigration], migrations: &[EmbeddedMigration]) -> std::result::Result<usize, crate::PostgresBackendError> {
fn validate_history(
history: &[AppliedMigration],
migrations: &[EmbeddedMigration],
allow_previous_checksums: bool,
) -> std::result::Result<usize, crate::PostgresBackendError> {
if history.is_empty() {
return std::result::Result::Err(crate::PostgresBackendError::new(crate::PostgresBackendErrorKind::MigrationMismatch, "history_missing"));
}
@@ -630,7 +683,8 @@ fn validate_history(history: &[AppliedMigration], migrations: &[EmbeddedMigratio
}
let expected = &migrations[index];
let expected_checksum = migration_checksum(expected);
if applied.version != expected.version || applied.name != expected.name || applied.checksum != expected_checksum {
let previous_checksum_matches = allow_previous_checksums && expected.previous_checksums.contains(&applied.checksum.as_str());
if applied.version != expected.version || applied.name != expected.name || (applied.checksum != expected_checksum && !previous_checksum_matches) {
return std::result::Result::Err(crate::PostgresBackendError::new(crate::PostgresBackendErrorKind::MigrationMismatch, "history_diverged"));
}
index += 1;

View File

@@ -1,5 +1,5 @@
// file: crates/ksp-store-postgres-lib/src/schema.rs
// version: 8
// version: 9
/// Immutable V000 physical schema resource inventory.
pub(crate) const V000_RESOURCES: &[SchemaResource] = &[SchemaResource {
@@ -370,7 +370,7 @@ pub(crate) const V001_RESOURCES: &[SchemaResource] = &[
access_method: "btree",
key_fragment: "slot,signature",
name: "ix_ksp_raw_transactions_slot_signature",
predicate_fragment: "retention_state<>'purged'",
predicate_fragment: std::option::Option::Some("retention_state<>'purged'"),
table: "ksp_raw_transactions",
unique: false,
}),
@@ -378,7 +378,7 @@ pub(crate) const V001_RESOURCES: &[SchemaResource] = &[
sql: include_str!("../migrations/v001_raw_transaction/indexes/001_ix_ksp_raw_transactions_slot_signature.sql"),
},
];
/// V002 physical schema resource inventory for the minimal RAW account state foundation.
/// Final V002 physical schema resource inventory for RAW account state persistence.
pub(crate) const V002_RESOURCES: &[SchemaResource] = &[
SchemaResource {
id: "tables/001_ksp_raw_account_states.sql",
@@ -426,6 +426,259 @@ pub(crate) const V002_RESOURCES: &[SchemaResource] = &[
repair_existing: true,
sql: include_str!("../migrations/v002_raw_account_state/constraints/003_fk_ksp_raw_account_observations_state.sql"),
},
SchemaResource {
id: "constraints/004_ck_ksp_raw_account_states_pubkey.sql",
object: SchemaObjectContract::Constraint(ConstraintContract { kind: "c", name: "ck_ksp_raw_account_states_pubkey", table: "ksp_raw_account_states" }),
repair_existing: true,
sql: include_str!("../migrations/v002_raw_account_state/constraints/004_ck_ksp_raw_account_states_pubkey.sql"),
},
SchemaResource {
id: "constraints/005_ck_ksp_raw_account_states_slot.sql",
object: SchemaObjectContract::Constraint(ConstraintContract { kind: "c", name: "ck_ksp_raw_account_states_slot", table: "ksp_raw_account_states" }),
repair_existing: true,
sql: include_str!("../migrations/v002_raw_account_state/constraints/005_ck_ksp_raw_account_states_slot.sql"),
},
SchemaResource {
id: "constraints/006_ck_ksp_raw_account_states_state_hash.sql",
object: SchemaObjectContract::Constraint(ConstraintContract {
kind: "c",
name: "ck_ksp_raw_account_states_state_hash",
table: "ksp_raw_account_states",
}),
repair_existing: true,
sql: include_str!("../migrations/v002_raw_account_state/constraints/006_ck_ksp_raw_account_states_state_hash.sql"),
},
SchemaResource {
id: "constraints/007_ck_ksp_raw_account_states_lamports.sql",
object: SchemaObjectContract::Constraint(ConstraintContract { kind: "c", name: "ck_ksp_raw_account_states_lamports", table: "ksp_raw_account_states" }),
repair_existing: true,
sql: include_str!("../migrations/v002_raw_account_state/constraints/007_ck_ksp_raw_account_states_lamports.sql"),
},
SchemaResource {
id: "constraints/008_ck_ksp_raw_account_states_owner.sql",
object: SchemaObjectContract::Constraint(ConstraintContract { kind: "c", name: "ck_ksp_raw_account_states_owner", table: "ksp_raw_account_states" }),
repair_existing: true,
sql: include_str!("../migrations/v002_raw_account_state/constraints/008_ck_ksp_raw_account_states_owner.sql"),
},
SchemaResource {
id: "constraints/009_ck_ksp_raw_account_states_rent_epoch.sql",
object: SchemaObjectContract::Constraint(ConstraintContract {
kind: "c",
name: "ck_ksp_raw_account_states_rent_epoch",
table: "ksp_raw_account_states",
}),
repair_existing: true,
sql: include_str!("../migrations/v002_raw_account_state/constraints/009_ck_ksp_raw_account_states_rent_epoch.sql"),
},
SchemaResource {
id: "constraints/010_ck_ksp_raw_account_states_data.sql",
object: SchemaObjectContract::Constraint(ConstraintContract { kind: "c", name: "ck_ksp_raw_account_states_data", table: "ksp_raw_account_states" }),
repair_existing: true,
sql: include_str!("../migrations/v002_raw_account_state/constraints/010_ck_ksp_raw_account_states_data.sql"),
},
SchemaResource {
id: "constraints/011_ck_ksp_raw_account_observations_key.sql",
object: SchemaObjectContract::Constraint(ConstraintContract {
kind: "c",
name: "ck_ksp_raw_account_observations_key",
table: "ksp_raw_account_observations",
}),
repair_existing: true,
sql: include_str!("../migrations/v002_raw_account_state/constraints/011_ck_ksp_raw_account_observations_key.sql"),
},
SchemaResource {
id: "constraints/012_ck_ksp_raw_account_observations_account_pubkey.sql",
object: SchemaObjectContract::Constraint(ConstraintContract {
kind: "c",
name: "ck_ksp_raw_account_observations_account_pubkey",
table: "ksp_raw_account_observations",
}),
repair_existing: true,
sql: include_str!("../migrations/v002_raw_account_state/constraints/012_ck_ksp_raw_account_observations_account_pubkey.sql"),
},
SchemaResource {
id: "constraints/013_ck_ksp_raw_account_observations_account_slot.sql",
object: SchemaObjectContract::Constraint(ConstraintContract {
kind: "c",
name: "ck_ksp_raw_account_observations_account_slot",
table: "ksp_raw_account_observations",
}),
repair_existing: true,
sql: include_str!("../migrations/v002_raw_account_state/constraints/013_ck_ksp_raw_account_observations_account_slot.sql"),
},
SchemaResource {
id: "constraints/014_ck_ksp_raw_account_observations_account_state_hash.sql",
object: SchemaObjectContract::Constraint(ConstraintContract {
kind: "c",
name: "ck_ksp_raw_account_observations_account_state_hash",
table: "ksp_raw_account_observations",
}),
repair_existing: true,
sql: include_str!("../migrations/v002_raw_account_state/constraints/014_ck_ksp_raw_account_observations_account_state_hash.sql"),
},
SchemaResource {
id: "constraints/015_ck_ksp_raw_account_observations_provider.sql",
object: SchemaObjectContract::Constraint(ConstraintContract {
kind: "c",
name: "ck_ksp_raw_account_observations_provider",
table: "ksp_raw_account_observations",
}),
repair_existing: true,
sql: include_str!("../migrations/v002_raw_account_state/constraints/015_ck_ksp_raw_account_observations_provider.sql"),
},
SchemaResource {
id: "constraints/016_ck_ksp_raw_account_observations_protocol.sql",
object: SchemaObjectContract::Constraint(ConstraintContract {
kind: "c",
name: "ck_ksp_raw_account_observations_protocol",
table: "ksp_raw_account_observations",
}),
repair_existing: true,
sql: include_str!("../migrations/v002_raw_account_state/constraints/016_ck_ksp_raw_account_observations_protocol.sql"),
},
SchemaResource {
id: "constraints/017_ck_ksp_raw_account_observations_method.sql",
object: SchemaObjectContract::Constraint(ConstraintContract {
kind: "c",
name: "ck_ksp_raw_account_observations_method",
table: "ksp_raw_account_observations",
}),
repair_existing: true,
sql: include_str!("../migrations/v002_raw_account_state/constraints/017_ck_ksp_raw_account_observations_method.sql"),
},
SchemaResource {
id: "constraints/018_ck_ksp_raw_account_observations_origin.sql",
object: SchemaObjectContract::Constraint(ConstraintContract {
kind: "c",
name: "ck_ksp_raw_account_observations_origin",
table: "ksp_raw_account_observations",
}),
repair_existing: true,
sql: include_str!("../migrations/v002_raw_account_state/constraints/018_ck_ksp_raw_account_observations_origin.sql"),
},
SchemaResource {
id: "constraints/019_ck_ksp_raw_account_observations_received_at.sql",
object: SchemaObjectContract::Constraint(ConstraintContract {
kind: "c",
name: "ck_ksp_raw_account_observations_received_at",
table: "ksp_raw_account_observations",
}),
repair_existing: true,
sql: include_str!("../migrations/v002_raw_account_state/constraints/019_ck_ksp_raw_account_observations_received_at.sql"),
},
SchemaResource {
id: "constraints/020_ck_ksp_raw_account_observations_capture_session.sql",
object: SchemaObjectContract::Constraint(ConstraintContract {
kind: "c",
name: "ck_ksp_raw_account_observations_capture_session",
table: "ksp_raw_account_observations",
}),
repair_existing: true,
sql: include_str!("../migrations/v002_raw_account_state/constraints/020_ck_ksp_raw_account_observations_capture_session.sql"),
},
SchemaResource {
id: "constraints/021_ck_ksp_raw_account_observations_commitment.sql",
object: SchemaObjectContract::Constraint(ConstraintContract {
kind: "c",
name: "ck_ksp_raw_account_observations_commitment",
table: "ksp_raw_account_observations",
}),
repair_existing: true,
sql: include_str!("../migrations/v002_raw_account_state/constraints/021_ck_ksp_raw_account_observations_commitment.sql"),
},
SchemaResource {
id: "constraints/022_ck_ksp_raw_account_observations_endpoint.sql",
object: SchemaObjectContract::Constraint(ConstraintContract {
kind: "c",
name: "ck_ksp_raw_account_observations_endpoint",
table: "ksp_raw_account_observations",
}),
repair_existing: true,
sql: include_str!("../migrations/v002_raw_account_state/constraints/022_ck_ksp_raw_account_observations_endpoint.sql"),
},
SchemaResource {
id: "constraints/023_ck_ksp_raw_account_observations_filter.sql",
object: SchemaObjectContract::Constraint(ConstraintContract {
kind: "c",
name: "ck_ksp_raw_account_observations_filter",
table: "ksp_raw_account_observations",
}),
repair_existing: true,
sql: include_str!("../migrations/v002_raw_account_state/constraints/023_ck_ksp_raw_account_observations_filter.sql"),
},
SchemaResource {
id: "constraints/024_ck_ksp_raw_account_observations_observed_at.sql",
object: SchemaObjectContract::Constraint(ConstraintContract {
kind: "c",
name: "ck_ksp_raw_account_observations_observed_at",
table: "ksp_raw_account_observations",
}),
repair_existing: true,
sql: include_str!("../migrations/v002_raw_account_state/constraints/024_ck_ksp_raw_account_observations_observed_at.sql"),
},
SchemaResource {
id: "constraints/025_ck_ksp_raw_account_observations_time_order.sql",
object: SchemaObjectContract::Constraint(ConstraintContract {
kind: "c",
name: "ck_ksp_raw_account_observations_time_order",
table: "ksp_raw_account_observations",
}),
repair_existing: true,
sql: include_str!("../migrations/v002_raw_account_state/constraints/025_ck_ksp_raw_account_observations_time_order.sql"),
},
SchemaResource {
id: "constraints/026_ck_ksp_raw_account_observations_source_hash.sql",
object: SchemaObjectContract::Constraint(ConstraintContract {
kind: "c",
name: "ck_ksp_raw_account_observations_source_hash",
table: "ksp_raw_account_observations",
}),
repair_existing: true,
sql: include_str!("../migrations/v002_raw_account_state/constraints/026_ck_ksp_raw_account_observations_source_hash.sql"),
},
SchemaResource {
id: "constraints/027_ck_ksp_raw_account_observations_source_size.sql",
object: SchemaObjectContract::Constraint(ConstraintContract {
kind: "c",
name: "ck_ksp_raw_account_observations_source_size",
table: "ksp_raw_account_observations",
}),
repair_existing: true,
sql: include_str!("../migrations/v002_raw_account_state/constraints/027_ck_ksp_raw_account_observations_source_size.sql"),
},
SchemaResource {
id: "constraints/028_ck_ksp_raw_account_observations_transaction_signature.sql",
object: SchemaObjectContract::Constraint(ConstraintContract {
kind: "c",
name: "ck_ksp_raw_account_observations_transaction_signature",
table: "ksp_raw_account_observations",
}),
repair_existing: true,
sql: include_str!("../migrations/v002_raw_account_state/constraints/028_ck_ksp_raw_account_observations_transaction_signature.sql"),
},
SchemaResource {
id: "constraints/029_ck_ksp_raw_account_observations_write_version.sql",
object: SchemaObjectContract::Constraint(ConstraintContract {
kind: "c",
name: "ck_ksp_raw_account_observations_write_version",
table: "ksp_raw_account_observations",
}),
repair_existing: true,
sql: include_str!("../migrations/v002_raw_account_state/constraints/029_ck_ksp_raw_account_observations_write_version.sql"),
},
SchemaResource {
id: "indexes/001_ix_ksp_raw_account_states_slot_pubkey_state_hash.sql",
object: SchemaObjectContract::Index(IndexContract {
access_method: "btree",
key_fragment: "slot,pubkey,state_hash",
name: "ix_ksp_raw_account_states_slot_pubkey_state_hash",
predicate_fragment: std::option::Option::None,
table: "ksp_raw_account_states",
unique: false,
}),
repair_existing: true,
sql: include_str!("../migrations/v002_raw_account_state/indexes/001_ix_ksp_raw_account_states_slot_pubkey_state_hash.sql"),
},
];
const COLUMN_LOAD_SQL: &str = r#"SELECT column_name::TEXT, udt_name::TEXT, (is_nullable = 'YES') AS nullable, numeric_precision::INTEGER, numeric_scale::INTEGER, column_default::TEXT, is_identity::TEXT, is_generated::TEXT
@@ -453,7 +706,7 @@ WHERE ns.nspname = current_schema() AND table_rel.relname = $1 AND index_rel.rel
const MANAGED_OBJECT_EXISTS_SQL: &str = r#"SELECT EXISTS (
SELECT 1 FROM information_schema.tables
WHERE table_schema = current_schema()
AND table_name IN ('ksp_store_identity', 'ksp_raw_transactions', 'ksp_raw_transaction_observations', 'ksp_raw_transaction_archive_payloads')
AND table_name IN ('ksp_store_identity', 'ksp_raw_transactions', 'ksp_raw_transaction_observations', 'ksp_raw_transaction_archive_payloads', 'ksp_raw_account_states', 'ksp_raw_account_observations')
AND table_type = 'BASE TABLE'
)"#;
const PRIMARY_KEY_LOAD_SQL: &str = r#"SELECT string_agg(att.attname::TEXT, ',' ORDER BY key_part.ord)::TEXT
@@ -513,7 +766,7 @@ struct IndexContract {
access_method: &'static str,
key_fragment: &'static str,
name: &'static str,
predicate_fragment: &'static str,
predicate_fragment: std::option::Option<&'static str>,
table: &'static str,
unique: bool,
}
@@ -969,6 +1222,7 @@ const RAW_TRANSACTION_ARCHIVE_PAYLOADS_COLUMNS: &[ColumnContract] = &[
},
];
const V001_TABLE_NAMES: &[&str] = &["ksp_store_identity", "ksp_raw_transactions", "ksp_raw_transaction_observations", "ksp_raw_transaction_archive_payloads"];
const V002_TABLE_NAMES: &[&str] = &["ksp_raw_account_states", "ksp_raw_account_observations"];
/// Inspects one embedded schema resource against the effective PostgreSQL catalog.
pub(crate) async fn inspect_resource(
@@ -982,8 +1236,8 @@ pub(crate) async fn inspect_resource(
};
}
/// Returns whether any V001-managed base table already exists in the active schema.
pub(crate) async fn managed_v001_objects_exist(transaction: &deadpool_postgres::Transaction<'_>) -> std::result::Result<bool, crate::PostgresBackendError> {
/// Returns whether any KSP-managed Store base table already exists in the active schema.
pub(crate) async fn managed_schema_objects_exist(transaction: &deadpool_postgres::Transaction<'_>) -> std::result::Result<bool, crate::PostgresBackendError> {
let result = transaction.query_one(MANAGED_OBJECT_EXISTS_SQL, &[]).await;
let row = match result {
std::result::Result::Ok(value) => value,
@@ -999,7 +1253,22 @@ pub(crate) async fn managed_v001_objects_exist(transaction: &deadpool_postgres::
pub(crate) async fn verify_v001_external_compatibility(
transaction: &deadpool_postgres::Transaction<'_>,
) -> std::result::Result<(), crate::PostgresBackendError> {
for table in V001_TABLE_NAMES {
return verify_external_compatibility(transaction, V001_TABLE_NAMES, V001_RESOURCES).await;
}
/// Rejects external V002 schema extensions that can constrain or mutate KSP writes.
pub(crate) async fn verify_v002_external_compatibility(
transaction: &deadpool_postgres::Transaction<'_>,
) -> std::result::Result<(), crate::PostgresBackendError> {
return verify_external_compatibility(transaction, V002_TABLE_NAMES, V002_RESOURCES).await;
}
async fn verify_external_compatibility(
transaction: &deadpool_postgres::Transaction<'_>,
tables: &[&str],
resources: &[SchemaResource],
) -> std::result::Result<(), crate::PostgresBackendError> {
for table in tables {
let table = *table;
let constraint_rows = transaction.query(UNEXPECTED_CONSTRAINTS_SQL, &[&table]).await;
let constraint_rows = match constraint_rows {
@@ -1024,11 +1293,11 @@ pub(crate) async fn verify_v001_external_compatibility(
) => (name, kind, validated, deferrable, deferred, definition),
_ => return schema_query_error("schema_constraint_inventory_decode"),
};
if is_expected_constraint(table, name.as_str()) {
if is_expected_constraint(resources, table, name.as_str()) {
continue;
}
let definition = normalize_catalog_sql(definition.as_str());
if !validated || deferrable || deferred || !matches_expected_constraint_definition(table, kind.as_str(), definition.as_str()) {
if !validated || deferrable || deferred || !matches_expected_constraint_definition(resources, table, kind.as_str(), definition.as_str()) {
return schema_incompatible("schema_external_constraint");
}
}
@@ -1129,9 +1398,10 @@ async fn inspect_index(
};
let definition = normalize_catalog_sql(definition.as_str());
let predicate = predicate.map(|value| return normalize_catalog_sql(value.as_str()));
let predicate_matches = match predicate.as_deref() {
std::option::Option::Some(value) => value.contains(contract.predicate_fragment),
std::option::Option::None => false,
let predicate_matches = match (contract.predicate_fragment, predicate.as_deref()) {
(std::option::Option::Some(expected), std::option::Option::Some(value)) => value.contains(expected),
(std::option::Option::None, std::option::Option::None) => true,
(std::option::Option::Some(_), std::option::Option::None) | (std::option::Option::None, std::option::Option::Some(_)) => false,
};
if unique != contract.unique || access_method != contract.access_method || !definition.contains(contract.key_fragment) || !predicate_matches {
return std::result::Result::Ok(SchemaResourceState::Incompatible);
@@ -1347,8 +1617,8 @@ fn expected_constraint_definition(resource_sql: &str, name: &str) -> std::option
return std::option::Option::Some(normalize_catalog_sql(definition));
}
fn is_expected_constraint(table: &str, name: &str) -> bool {
for resource in V001_RESOURCES {
fn is_expected_constraint(resources: &[SchemaResource], table: &str, name: &str) -> bool {
for resource in resources {
let contract = match resource.object {
SchemaObjectContract::Constraint(value) => value,
SchemaObjectContract::Index(_) | SchemaObjectContract::Table(_) => continue,
@@ -1360,8 +1630,8 @@ fn is_expected_constraint(table: &str, name: &str) -> bool {
return false;
}
fn matches_expected_constraint_definition(table: &str, kind: &str, definition: &str) -> bool {
for resource in V001_RESOURCES {
fn matches_expected_constraint_definition(resources: &[SchemaResource], table: &str, kind: &str, definition: &str) -> bool {
for resource in resources {
let contract = match resource.object {
SchemaObjectContract::Constraint(value) => value,
SchemaObjectContract::Index(_) | SchemaObjectContract::Table(_) => continue,