Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nullable lldp_link_config_id column #6436

Merged
merged 2 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion common/src/api/external/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2371,7 +2371,7 @@ pub struct SwitchPortLinkConfig {

/// The link-layer discovery protocol service configuration id for this
/// link.
pub lldp_link_config_id: Uuid,
pub lldp_link_config_id: Option<Uuid>,

/// The name of this link.
pub link_name: String,
Expand Down
2 changes: 1 addition & 1 deletion nexus/db-model/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ table! {
fec -> crate::SwitchLinkFecEnum,
speed -> crate::SwitchLinkSpeedEnum,
autoneg -> Bool,
lldp_link_config_id -> Uuid,
lldp_link_config_id -> Nullable<Uuid>,
}
}

Expand Down
3 changes: 2 additions & 1 deletion nexus/db-model/src/schema_versions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use std::collections::BTreeMap;
///
/// This must be updated when you change the database schema. Refer to
/// schema/crdb/README.adoc in the root of this repository for details.
pub const SCHEMA_VERSION: SemverVersion = SemverVersion::new(91, 0, 0);
pub const SCHEMA_VERSION: SemverVersion = SemverVersion::new(92, 0, 0);

/// List of all past database schema versions, in *reverse* order
///
Expand All @@ -29,6 +29,7 @@ static KNOWN_VERSIONS: Lazy<Vec<KnownVersion>> = Lazy::new(|| {
// | leaving the first copy as an example for the next person.
// v
// KnownVersion::new(next_int, "unique-dirname-with-the-sql-files"),
KnownVersion::new(92, "lldp-link-config-nullable"),
KnownVersion::new(91, "add-management-gateway-producer-kind"),
KnownVersion::new(90, "lookup-bgp-config-by-asn"),
KnownVersion::new(89, "collapse_lldp_settings"),
Expand Down
4 changes: 2 additions & 2 deletions nexus/db-model/src/switch_port.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ impl Into<external::SwitchPortConfig> for SwitchPortConfig {
#[diesel(table_name = switch_port_settings_link_config)]
pub struct SwitchPortLinkConfig {
pub port_settings_id: Uuid,
pub lldp_link_config_id: Uuid,
pub lldp_link_config_id: Option<Uuid>,
pub link_name: String,
pub mtu: SqlU16,
pub fec: SwitchLinkFec,
Expand All @@ -401,7 +401,7 @@ impl SwitchPortLinkConfig {
) -> Self {
Self {
port_settings_id,
lldp_link_config_id,
lldp_link_config_id: Some(lldp_link_config_id),
smklein marked this conversation as resolved.
Show resolved Hide resolved
link_name,
fec,
speed,
Expand Down
4 changes: 2 additions & 2 deletions nexus/db-queries/src/db/datastore/switch_port.rs
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ impl DataStore {
let lldp_link_ids: Vec<Uuid> = result
.links
.iter()
.map(|link| link.lldp_link_config_id)
.filter_map(|link| link.lldp_link_config_id)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file deserves extra scrutiny - this function (switch_port_settings_get) and the call below (do_switch_port_settings_delete) can now see an empty vec of lldp_link_ids if this field is nullable.

.collect();

use db::schema::lldp_link_config;
Expand Down Expand Up @@ -1511,7 +1511,7 @@ async fn do_switch_port_settings_delete(
// delete lldp configs
use db::schema::lldp_link_config;
let lldp_link_ids: Vec<Uuid> =
links.iter().map(|link| link.lldp_link_config_id).collect();
links.iter().filter_map(|link| link.lldp_link_config_id).collect();
diesel::delete(lldp_link_config::dsl::lldp_link_config)
.filter(lldp_link_config::id.eq_any(lldp_link_ids))
.execute_async(conn)
Expand Down
2 changes: 1 addition & 1 deletion schema/crdb/collapse_lldp_settings/up2.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
* Add a pointer to this link's LLDP config settings.
*/
ALTER TABLE omicron.public.switch_port_settings_link_config ADD COLUMN IF NOT EXISTS lldp_link_config_id UUID NOT NULL;
ALTER TABLE omicron.public.switch_port_settings_link_config ADD COLUMN IF NOT EXISTS lldp_link_config_id UUID;
4 changes: 2 additions & 2 deletions schema/crdb/dbinit.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2657,7 +2657,7 @@ CREATE TABLE IF NOT EXISTS omicron.public.switch_port_settings_link_config (
fec omicron.public.switch_link_fec,
speed omicron.public.switch_link_speed,
autoneg BOOL NOT NULL DEFAULT false,
lldp_link_config_id UUID NOT NULL,
lldp_link_config_id UUID,

PRIMARY KEY (port_settings_id, link_name)
);
Expand Down Expand Up @@ -4214,7 +4214,7 @@ INSERT INTO omicron.public.db_metadata (
version,
target_version
) VALUES
(TRUE, NOW(), NOW(), '91.0.0', NULL)
(TRUE, NOW(), NOW(), '92.0.0', NULL)
ON CONFLICT DO NOTHING;

COMMIT;
20 changes: 20 additions & 0 deletions schema/crdb/lldp-link-config-nullable/up1.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
-- Refer to https://github.com/oxidecomputer/omicron/issues/6433 for the justificaiton
-- behind this schema change.
--
-- In short: the "collapse_lldp_settings" schema change was edited after
-- merging. That change included a schema change which added a non-null column
-- to an existing table. Such a data-modifying statement is only valid for
-- tables with no rows - however, in our test systems, we observed rows, which
-- prevented this schema change from progressing.
--
-- To resolve:
-- 1. Within the old "collapse_lldp_settings" change, we retroactively dropped the
-- non-null constraint. For systems with populated
-- "switch_port_settings_link_config" tables, this allows the schema update to
-- complete without an error.
-- 2. Within this new "lldp-link-config-nullable" change, we ALSO dropped the
-- non-null constraint. For systems without populated
-- "switch_port_settings_link_config" tables -- which may have been able to
-- apply the "collapse_lldp_settings" change successfully -- this converges the state
-- of the database to the same outcome, where the columns is nullable.
ALTER TABLE omicron.public.switch_port_settings_link_config ALTER COLUMN lldp_link_config_id DROP NOT NULL;
Loading