Skip to content

Commit 86cdafb

Browse files
committed
do latest blueprint time properly, add index on time_created
1 parent 6766103 commit 86cdafb

File tree

5 files changed

+35
-23
lines changed

5 files changed

+35
-23
lines changed

nexus/db-model/src/schema_versions.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use std::{collections::BTreeMap, sync::LazyLock};
1616
///
1717
/// This must be updated when you change the database schema. Refer to
1818
/// schema/crdb/README.adoc in the root of this repository for details.
19-
pub const SCHEMA_VERSION: Version = Version::new(189, 0, 0);
19+
pub const SCHEMA_VERSION: Version = Version::new(190, 0, 0);
2020

2121
/// List of all past database schema versions, in *reverse* order
2222
///
@@ -28,6 +28,7 @@ static KNOWN_VERSIONS: LazyLock<Vec<KnownVersion>> = LazyLock::new(|| {
2828
// | leaving the first copy as an example for the next person.
2929
// v
3030
// KnownVersion::new(next_int, "unique-dirname-with-the-sql-files"),
31+
KnownVersion::new(190, "blueprint-time-created-index"),
3132
KnownVersion::new(189, "reconfigurator-chicken-switches-to-config"),
3233
KnownVersion::new(188, "positive-quotas"),
3334
KnownVersion::new(187, "no-default-pool-for-internal-silo"),

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,26 @@ impl DataStore {
137137
Ok(blueprints.into_iter().map(BlueprintMetadata::from).collect())
138138
}
139139

140+
/// Get the time of the most recently created blueprint
141+
pub async fn blueprint_get_latest_time(
142+
&self,
143+
opctx: &OpContext,
144+
) -> Result<Option<DateTime<Utc>>, Error> {
145+
use nexus_db_schema::schema::blueprint;
146+
147+
opctx
148+
.authorize(authz::Action::ListChildren, &authz::BLUEPRINT_CONFIG)
149+
.await?;
150+
151+
let latest_time = blueprint::table
152+
.select(diesel::dsl::max(blueprint::time_created))
153+
.get_result_async(&*self.pool_connection_authorized(opctx).await?)
154+
.await
155+
.map_err(|e| public_error_from_diesel(e, ErrorHandler::Server))?;
156+
157+
Ok(latest_time)
158+
}
159+
140160
/// Store a complete blueprint into the database
141161
pub async fn blueprint_insert(
142162
&self,

nexus/src/app/update.rs

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,11 @@ impl super::Nexus {
169169
let components_by_release =
170170
self.get_component_status_readonly(opctx).await?;
171171

172-
// Get last blueprint time
173-
let last_blueprint_time = self.get_last_blueprint_time(opctx).await?;
172+
let last_blueprint_time = self
173+
.datastore()
174+
.blueprint_get_latest_time(opctx)
175+
.await
176+
.map_err(HttpError::from)?;
174177

175178
// Generate blockers (placeholder for now)
176179
let blockers = self.generate_blockers(opctx).await?;
@@ -183,25 +186,6 @@ impl super::Nexus {
183186
})
184187
}
185188

186-
/// Get the time of the most recently created blueprint
187-
async fn get_last_blueprint_time(
188-
&self,
189-
opctx: &OpContext,
190-
) -> Result<Option<chrono::DateTime<chrono::Utc>>, HttpError> {
191-
use omicron_common::api::external::DataPageParams;
192-
193-
let blueprints = self
194-
.datastore()
195-
.blueprints_list(opctx, &DataPageParams::max_page())
196-
.await
197-
.map_err(HttpError::from)?;
198-
199-
let latest_time =
200-
blueprints.into_iter().map(|bp| bp.time_created).max();
201-
202-
Ok(latest_time)
203-
}
204-
205189
/// Generate list of blockers (placeholder implementation)
206190
async fn generate_blockers(
207191
&self,
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
CREATE INDEX IF NOT EXISTS lookup_blueprint_by_creation ON omicron.public.blueprint (
2+
time_created
3+
);

schema/crdb/dbinit.sql

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4545,6 +4545,10 @@ CREATE TABLE IF NOT EXISTS omicron.public.blueprint (
45454545
nexus_generation INT8 NOT NULL
45464546
);
45474547

4548+
CREATE INDEX IF NOT EXISTS lookup_blueprint_by_creation ON omicron.public.blueprint (
4549+
time_created
4550+
);
4551+
45484552
-- table describing both the current and historical target blueprints of the
45494553
-- system
45504554
CREATE TABLE IF NOT EXISTS omicron.public.bp_target (
@@ -6617,7 +6621,7 @@ INSERT INTO omicron.public.db_metadata (
66176621
version,
66186622
target_version
66196623
) VALUES
6620-
(TRUE, NOW(), NOW(), '189.0.0', NULL)
6624+
(TRUE, NOW(), NOW(), '190.0.0', NULL)
66216625
ON CONFLICT DO NOTHING;
66226626

66236627
COMMIT;

0 commit comments

Comments
 (0)