Skip to content

Commit ad1e837

Browse files
authored
augment blueprint deletion test and make it more future-proof (#8439)
1 parent e6d4ee6 commit ad1e837

File tree

1 file changed

+60
-1
lines changed

1 file changed

+60
-1
lines changed

nexus/db-queries/src/db/datastore/deployment.rs

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1031,6 +1031,7 @@ impl DataStore {
10311031
nclickhouse_cluster_configs,
10321032
nclickhouse_keepers,
10331033
nclickhouse_servers,
1034+
noximeter_policy,
10341035
) = self.transaction_retry_wrapper("blueprint_delete")
10351036
.transaction(&conn, |conn| {
10361037
let err = err.clone();
@@ -1151,6 +1152,17 @@ impl DataStore {
11511152
.await?
11521153
};
11531154

1155+
let noximeter_policy = {
1156+
use nexus_db_schema::schema::
1157+
bp_oximeter_read_policy::dsl;
1158+
diesel::delete(dsl::bp_oximeter_read_policy
1159+
.filter(dsl::blueprint_id.eq(
1160+
to_db_typed_uuid(blueprint_id))),
1161+
)
1162+
.execute_async(&conn)
1163+
.await?
1164+
};
1165+
11541166
Ok((
11551167
nblueprints,
11561168
nsled_metadata,
@@ -1161,6 +1173,7 @@ impl DataStore {
11611173
nclickhouse_cluster_configs,
11621174
nclickhouse_keepers,
11631175
nclickhouse_servers,
1176+
noximeter_policy,
11641177
))
11651178
}
11661179
})
@@ -1180,7 +1193,8 @@ impl DataStore {
11801193
"nnics" => nnics,
11811194
"nclickhouse_cluster_configs" => nclickhouse_cluster_configs,
11821195
"nclickhouse_keepers" => nclickhouse_keepers,
1183-
"nclickhouse_servers" => nclickhouse_servers
1196+
"nclickhouse_servers" => nclickhouse_servers,
1197+
"noximeter_policy" => noximeter_policy,
11841198
);
11851199

11861200
Ok(())
@@ -1951,6 +1965,7 @@ mod tests {
19511965
use rand::Rng;
19521966
use rand::thread_rng;
19531967
use slog::Logger;
1968+
use std::collections::BTreeSet;
19541969
use std::mem;
19551970
use std::net::IpAddr;
19561971
use std::net::Ipv4Addr;
@@ -1999,20 +2014,64 @@ mod tests {
19992014
}};
20002015
}
20012016

2017+
// These tables start with `bp_` but do not represent the contents of a
2018+
// specific blueprint. It should be uncommon to add things to this
2019+
// list.
2020+
let tables_ignored: BTreeSet<_> = ["bp_target"].into_iter().collect();
2021+
2022+
let mut tables_checked = BTreeSet::new();
20022023
for (table_name, result) in [
20032024
query_count!(blueprint, id),
20042025
query_count!(bp_sled_metadata, blueprint_id),
20052026
query_count!(bp_omicron_dataset, blueprint_id),
20062027
query_count!(bp_omicron_physical_disk, blueprint_id),
20072028
query_count!(bp_omicron_zone, blueprint_id),
20082029
query_count!(bp_omicron_zone_nic, blueprint_id),
2030+
query_count!(bp_clickhouse_cluster_config, blueprint_id),
2031+
query_count!(bp_clickhouse_keeper_zone_id_to_node_id, blueprint_id),
2032+
query_count!(bp_clickhouse_server_zone_id_to_node_id, blueprint_id),
2033+
query_count!(bp_oximeter_read_policy, blueprint_id),
20092034
] {
20102035
let count: i64 = result.unwrap();
20112036
assert_eq!(
20122037
count, 0,
20132038
"nonzero row count for blueprint \
20142039
{blueprint_id} in table {table_name}"
20152040
);
2041+
tables_checked.insert(table_name);
2042+
}
2043+
2044+
// Look for likely blueprint-related tables that we didn't check.
2045+
let mut query = QueryBuilder::new();
2046+
query.sql(
2047+
"SELECT table_name \
2048+
FROM information_schema.tables \
2049+
WHERE table_name LIKE 'bp\\_%'",
2050+
);
2051+
let tables_unchecked: Vec<String> = query
2052+
.query::<diesel::sql_types::Text>()
2053+
.load_async(&*conn)
2054+
.await
2055+
.expect("Failed to query information_schema for tables")
2056+
.into_iter()
2057+
.filter(|f: &String| {
2058+
let t = f.as_str();
2059+
!tables_ignored.contains(t) && !tables_checked.contains(t)
2060+
})
2061+
.collect();
2062+
if !tables_unchecked.is_empty() {
2063+
// If you see this message, you probably added a blueprint table
2064+
// whose name started with `bp_*`. Add it to the block above so
2065+
// that this function checks whether deleting a blueprint deletes
2066+
// rows from that table. (You may also find you need to update
2067+
// blueprint_delete() to actually delete said rows.)
2068+
panic!(
2069+
"found table(s) that look related to blueprints, but \
2070+
aren't covered by ensure_blueprint_fully_deleted(). \
2071+
Please add them to that function!\n
2072+
Found: {}",
2073+
tables_unchecked.join(", ")
2074+
);
20162075
}
20172076
}
20182077

0 commit comments

Comments
 (0)