427 lines
15 KiB
Rust
427 lines
15 KiB
Rust
use crank_registry::{MigrationAuthority, MigrationPreflight};
|
|
|
|
// V12 changed-type coverage. Every column gets a reachable `alter column ...
|
|
// type` drift whose `information_schema.data_type` differs from the frozen
|
|
// guard expectation, plus the exact expression that puts the original type
|
|
// back. `character varying` is chosen for the text columns and `timestamp`
|
|
// for the timestamptz columns because both are binary-compatible rewrites:
|
|
// PostgreSQL applies them even while the primary key, unique, foreign key,
|
|
// default, check and index objects stay in place, so the guard rejection is
|
|
// genuinely caused by the type drift and not by a missing dependent object.
|
|
const COLUMN_TYPE_DRIFT: &[(&str, &str, &str, &str)] = &[
|
|
("artifact_blobs", "digest", "character varying", "text"),
|
|
(
|
|
"artifact_blobs",
|
|
"artifact_ref",
|
|
"character varying",
|
|
"text",
|
|
),
|
|
("artifact_blobs", "size_bytes", "integer", "bigint"),
|
|
(
|
|
"artifact_blobs",
|
|
"storage_lifecycle",
|
|
"character varying",
|
|
"text",
|
|
),
|
|
("artifact_blobs", "claim_token", "character varying", "text"),
|
|
(
|
|
"artifact_blobs",
|
|
"claim_expires_at",
|
|
"timestamp",
|
|
"timestamp with time zone",
|
|
),
|
|
(
|
|
"artifact_blobs",
|
|
"created_at",
|
|
"timestamp",
|
|
"timestamp with time zone",
|
|
),
|
|
(
|
|
"artifact_blobs",
|
|
"updated_at",
|
|
"timestamp",
|
|
"timestamp with time zone",
|
|
),
|
|
(
|
|
"artifact_sources",
|
|
"workspace_id",
|
|
"character varying",
|
|
"text",
|
|
),
|
|
("artifact_sources", "source_id", "character varying", "text"),
|
|
(
|
|
"artifact_sources",
|
|
"blob_digest",
|
|
"character varying",
|
|
"text",
|
|
),
|
|
("artifact_sources", "mime_type", "character varying", "text"),
|
|
(
|
|
"artifact_sources",
|
|
"sensitivity",
|
|
"character varying",
|
|
"text",
|
|
),
|
|
("artifact_sources", "lifecycle", "character varying", "text"),
|
|
(
|
|
"artifact_sources",
|
|
"created_at",
|
|
"timestamp",
|
|
"timestamp with time zone",
|
|
),
|
|
(
|
|
"artifact_sources",
|
|
"updated_at",
|
|
"timestamp",
|
|
"timestamp with time zone",
|
|
),
|
|
(
|
|
"artifact_sources",
|
|
"detached_at",
|
|
"timestamp",
|
|
"timestamp with time zone",
|
|
),
|
|
];
|
|
|
|
// Columns that legitimately carry `null` in V12. The remaining non-primary-key
|
|
// columns are `not null`, and the primary-key members cannot have `not null`
|
|
// dropped while the key exists.
|
|
const V12_NULLABLE_COLUMNS: &[(&str, &str)] = &[
|
|
("artifact_blobs", "claim_token"),
|
|
("artifact_blobs", "claim_expires_at"),
|
|
("artifact_sources", "detached_at"),
|
|
];
|
|
|
|
const V12_PRIMARY_KEY_COLUMNS: &[(&str, &str)] = &[
|
|
("artifact_blobs", "digest"),
|
|
("artifact_sources", "workspace_id"),
|
|
("artifact_sources", "source_id"),
|
|
];
|
|
|
|
// V12 changed-definition coverage. Each entry keeps the original constraint
|
|
// name and `pg_constraint.contype` and only changes the definition: every
|
|
// check becomes `check (true)`, both foreign keys change their delete action,
|
|
// and the primary key/unique members are rewritten while staying the same
|
|
// kind. The original definition is captured from the catalog and replayed to
|
|
// restore the exact prior shape.
|
|
const CONSTRAINT_DEFINITION_DRIFT: &[(&str, &str, &str)] = &[
|
|
(
|
|
"artifact_blobs",
|
|
"artifact_blobs_artifact_ref_key",
|
|
"unique (digest)",
|
|
),
|
|
(
|
|
"artifact_blobs",
|
|
"artifact_blobs_claim_shape_check",
|
|
"check (true)",
|
|
),
|
|
(
|
|
"artifact_blobs",
|
|
"artifact_blobs_claim_token_check",
|
|
"check (true)",
|
|
),
|
|
(
|
|
"artifact_blobs",
|
|
"artifact_blobs_digest_check",
|
|
"check (true)",
|
|
),
|
|
(
|
|
"artifact_blobs",
|
|
"artifact_blobs_lifecycle_check",
|
|
"check (true)",
|
|
),
|
|
(
|
|
"artifact_blobs",
|
|
"artifact_blobs_pkey",
|
|
"primary key (digest) include (artifact_ref)",
|
|
),
|
|
("artifact_blobs", "artifact_blobs_ref_check", "check (true)"),
|
|
(
|
|
"artifact_blobs",
|
|
"artifact_blobs_size_check",
|
|
"check (true)",
|
|
),
|
|
(
|
|
"artifact_blobs",
|
|
"artifact_blobs_timestamps_check",
|
|
"check (true)",
|
|
),
|
|
(
|
|
"artifact_sources",
|
|
"artifact_sources_blob_digest_fkey",
|
|
"foreign key (blob_digest) references artifact_blobs(digest) on delete cascade",
|
|
),
|
|
(
|
|
"artifact_sources",
|
|
"artifact_sources_id_check",
|
|
"check (true)",
|
|
),
|
|
(
|
|
"artifact_sources",
|
|
"artifact_sources_lifecycle_check",
|
|
"check (true)",
|
|
),
|
|
(
|
|
"artifact_sources",
|
|
"artifact_sources_mime_type_check",
|
|
"check (true)",
|
|
),
|
|
(
|
|
"artifact_sources",
|
|
"artifact_sources_pkey",
|
|
"primary key (source_id, workspace_id)",
|
|
),
|
|
(
|
|
"artifact_sources",
|
|
"artifact_sources_sensitivity_check",
|
|
"check (true)",
|
|
),
|
|
(
|
|
"artifact_sources",
|
|
"artifact_sources_state_check",
|
|
"check (true)",
|
|
),
|
|
(
|
|
"artifact_sources",
|
|
"artifact_sources_timestamps_check",
|
|
"check (true)",
|
|
),
|
|
(
|
|
"artifact_sources",
|
|
"artifact_sources_workspace_id_fkey",
|
|
"foreign key (workspace_id) references workspaces(id) on delete restrict",
|
|
),
|
|
];
|
|
|
|
async fn catalog_nullable(pool: &sqlx::PgPool, table: &str, column: &str) -> bool {
|
|
sqlx::query_scalar::<_, String>(
|
|
"select is_nullable
|
|
from information_schema.columns
|
|
where table_schema = current_schema() and table_name = $1 and column_name = $2",
|
|
)
|
|
.bind(table)
|
|
.bind(column)
|
|
.fetch_one(pool)
|
|
.await
|
|
.unwrap()
|
|
== "YES"
|
|
}
|
|
|
|
pub(super) async fn assert_column_type_drift(pool: &sqlx::PgPool) {
|
|
assert_eq!(
|
|
COLUMN_TYPE_DRIFT.len(),
|
|
17,
|
|
"every V12 column needs a changed-type drift case"
|
|
);
|
|
for (table, column, drift_type, restore_type) in COLUMN_TYPE_DRIFT {
|
|
let drift = format!("alter table {table} alter column {column} type {drift_type};");
|
|
let restore = format!("alter table {table} alter column {column} type {restore_type};");
|
|
super::assert_v12_drift(pool, &drift, &restore).await;
|
|
}
|
|
}
|
|
|
|
pub(super) async fn assert_column_nullability_drift(pool: &sqlx::PgPool) {
|
|
let mut covered = 0usize;
|
|
for (table, column) in super::V12_BLOB_COLUMNS
|
|
.iter()
|
|
.map(|column| ("artifact_blobs", *column))
|
|
.chain(
|
|
super::V12_SOURCE_COLUMNS
|
|
.iter()
|
|
.map(|column| ("artifact_sources", *column)),
|
|
)
|
|
{
|
|
covered += 1;
|
|
let nullable = catalog_nullable(pool, table, column).await;
|
|
let expected_nullable = V12_NULLABLE_COLUMNS.contains(&(table, column));
|
|
assert_eq!(
|
|
nullable, expected_nullable,
|
|
"nullability inventory drift for {table}.{column}"
|
|
);
|
|
if V12_PRIMARY_KEY_COLUMNS.contains(&(table, column)) {
|
|
assert_primary_key_nullability(pool, table, column).await;
|
|
} else if nullable {
|
|
let drift = format!("alter table {table} alter column {column} set not null;");
|
|
let restore = format!("alter table {table} alter column {column} drop not null;");
|
|
super::assert_v12_drift(pool, &drift, &restore).await;
|
|
} else {
|
|
let drift = format!("alter table {table} alter column {column} drop not null;");
|
|
let restore = format!("alter table {table} alter column {column} set not null;");
|
|
super::assert_v12_drift(pool, &drift, &restore).await;
|
|
}
|
|
}
|
|
assert_eq!(covered, 17, "every V12 column needs a nullability case");
|
|
assert_eq!(
|
|
V12_PRIMARY_KEY_COLUMNS.len() + V12_NULLABLE_COLUMNS.len(),
|
|
6,
|
|
"the V12 nullability exceptions are exactly the three primary-key members and three nullable columns"
|
|
);
|
|
}
|
|
|
|
// PostgreSQL refuses `drop not null` on a primary-key member outright, so this
|
|
// case cannot be reached with the key in place. The rejection is asserted
|
|
// explicitly instead of being skipped, then the nullable shape is reached by
|
|
// dropping the primary key (and the dependent foreign key for artifact_blobs),
|
|
// toggling nullability, and replaying the captured definitions in dependency
|
|
// order.
|
|
async fn assert_primary_key_nullability(pool: &sqlx::PgPool, table: &str, column: &str) {
|
|
let direct = format!("alter table {table} alter column {column} drop not null;");
|
|
let error = sqlx::raw_sql(sqlx::AssertSqlSafe(direct))
|
|
.execute(pool)
|
|
.await
|
|
.expect_err("PostgreSQL must reject dropping NOT NULL on a primary key member");
|
|
assert!(
|
|
error.to_string().contains("is in a primary key"),
|
|
"unexpected rejection for {table}.{column}: {error}"
|
|
);
|
|
assert_eq!(
|
|
MigrationAuthority::preflight(pool).await.unwrap(),
|
|
MigrationPreflight::Current { version: 15 },
|
|
"a rejected primary-key nullability change must leave the schema current"
|
|
);
|
|
|
|
let definitions = super::catalog_constraint_definitions(pool).await;
|
|
let (drift, restore) = if table == "artifact_blobs" {
|
|
let pkey = definitions
|
|
.get("artifact_blobs_pkey")
|
|
.expect("artifact_blobs primary key definition must be captured");
|
|
let fk = definitions
|
|
.get("artifact_sources_blob_digest_fkey")
|
|
.expect("the foreign key that depends on artifact_blobs_pkey must be captured");
|
|
(
|
|
"alter table artifact_sources drop constraint artifact_sources_blob_digest_fkey;\n\
|
|
alter table artifact_blobs drop constraint artifact_blobs_pkey;\n\
|
|
alter table artifact_blobs alter column digest drop not null;"
|
|
.to_owned(),
|
|
format!(
|
|
"alter table artifact_blobs alter column digest set not null;\n\
|
|
alter table artifact_blobs add constraint artifact_blobs_pkey {pkey};\n\
|
|
alter table artifact_sources add constraint artifact_sources_blob_digest_fkey {fk};"
|
|
),
|
|
)
|
|
} else {
|
|
let pkey = definitions
|
|
.get("artifact_sources_pkey")
|
|
.expect("artifact_sources primary key definition must be captured");
|
|
(
|
|
format!(
|
|
"alter table artifact_sources drop constraint artifact_sources_pkey;\n\
|
|
alter table artifact_sources alter column {column} drop not null;"
|
|
),
|
|
format!(
|
|
"alter table artifact_sources alter column {column} set not null;\n\
|
|
alter table artifact_sources add constraint artifact_sources_pkey {pkey};"
|
|
),
|
|
)
|
|
};
|
|
super::assert_v12_drift(pool, &drift, &restore).await;
|
|
}
|
|
|
|
pub(super) async fn assert_constraint_definition_drift(pool: &sqlx::PgPool) {
|
|
assert_eq!(
|
|
CONSTRAINT_DEFINITION_DRIFT.len(),
|
|
18,
|
|
"every V12 constraint needs a same-name/kind changed-definition case"
|
|
);
|
|
let definitions = super::catalog_constraint_definitions(pool).await;
|
|
for (table, constraint, drift_definition) in CONSTRAINT_DEFINITION_DRIFT {
|
|
let original = definitions
|
|
.get(*constraint)
|
|
.unwrap_or_else(|| panic!("missing original definition for {constraint}"));
|
|
if *constraint == "artifact_blobs_pkey" {
|
|
let fk = definitions
|
|
.get("artifact_sources_blob_digest_fkey")
|
|
.expect("the foreign key that depends on artifact_blobs_pkey must be captured");
|
|
let drift = format!(
|
|
"alter table artifact_sources drop constraint artifact_sources_blob_digest_fkey;\n\
|
|
alter table artifact_blobs drop constraint artifact_blobs_pkey;\n\
|
|
alter table artifact_blobs add constraint artifact_blobs_pkey {drift_definition};\n\
|
|
alter table artifact_sources add constraint artifact_sources_blob_digest_fkey {fk};"
|
|
);
|
|
let restore = format!(
|
|
"alter table artifact_sources drop constraint artifact_sources_blob_digest_fkey;\n\
|
|
alter table artifact_blobs drop constraint artifact_blobs_pkey;\n\
|
|
alter table artifact_blobs add constraint artifact_blobs_pkey {original};\n\
|
|
alter table artifact_sources add constraint artifact_sources_blob_digest_fkey {fk};"
|
|
);
|
|
assert_definition_only_drift(pool, constraint, &drift, &restore).await;
|
|
} else {
|
|
let drift = format!(
|
|
"alter table {table} drop constraint {constraint};\n\
|
|
alter table {table} add constraint {constraint} {drift_definition};"
|
|
);
|
|
let restore = format!(
|
|
"alter table {table} drop constraint {constraint};\n\
|
|
alter table {table} add constraint {constraint} {original};"
|
|
);
|
|
assert_definition_only_drift(pool, constraint, &drift, &restore).await;
|
|
}
|
|
}
|
|
}
|
|
|
|
// A definition test must not pass merely because a dependent constraint went
|
|
// missing. Assert that exactly the named definition changed, with the complete
|
|
// name/kind inventory preserved, before asking the production guard to reject it.
|
|
async fn assert_definition_only_drift(
|
|
pool: &sqlx::PgPool,
|
|
changed: &str,
|
|
drift: &str,
|
|
restore: &str,
|
|
) {
|
|
let before = super::catalog_constraint_definitions(pool).await;
|
|
let kinds = constraint_kinds(pool).await;
|
|
sqlx::raw_sql(sqlx::AssertSqlSafe(drift.to_owned()))
|
|
.execute(pool)
|
|
.await
|
|
.unwrap();
|
|
let after = super::catalog_constraint_definitions(pool).await;
|
|
assert_eq!(
|
|
constraint_kinds(pool).await,
|
|
kinds,
|
|
"all constraint names and kinds must survive: {changed}"
|
|
);
|
|
assert_ne!(
|
|
after.get(changed),
|
|
before.get(changed),
|
|
"definition must actually change: {changed}"
|
|
);
|
|
for (name, definition) in &before {
|
|
if name != changed {
|
|
assert_eq!(
|
|
after.get(name),
|
|
Some(definition),
|
|
"unrelated definition changed: {name}"
|
|
);
|
|
}
|
|
}
|
|
let error = MigrationAuthority::preflight(pool).await.unwrap_err();
|
|
assert_eq!(
|
|
error.code(),
|
|
"partial_sequence",
|
|
"definition drift: {changed}"
|
|
);
|
|
assert_eq!(error.version(), Some(12), "definition drift: {changed}");
|
|
sqlx::raw_sql(sqlx::AssertSqlSafe(restore.to_owned()))
|
|
.execute(pool)
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(super::catalog_constraint_definitions(pool).await, before);
|
|
assert_eq!(
|
|
MigrationAuthority::preflight(pool).await.unwrap(),
|
|
MigrationPreflight::Current { version: 15 }
|
|
);
|
|
}
|
|
|
|
async fn constraint_kinds(pool: &sqlx::PgPool) -> Vec<(String, String)> {
|
|
sqlx::query_as(
|
|
"select c.conname::text, c.contype::text 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
|
|
where n.nspname = current_schema()
|
|
and t.relname in ('artifact_blobs', 'artifact_sources') order by c.conname",
|
|
)
|
|
.fetch_all(pool)
|
|
.await
|
|
.unwrap()
|
|
}
|