Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
835 changes: 764 additions & 71 deletions src/backend/Cargo.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/backend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ futures-util = { version = "0.3", default-features = false, features = ["std"] }
axum = "0.8"

# Database (MariaDB via SeaORM)
sea-orm = { version = "1.1", features = ["runtime-tokio", "sqlx-mysql", "with-uuid", "with-chrono", "with-json", "macros"] }
sea-orm-migration = { version = "1.1", features = ["runtime-tokio", "sqlx-mysql"] }
sea-orm = { version = "2.0", features = ["runtime-tokio", "sqlx-mysql", "with-uuid", "with-chrono", "with-json", "macros"] }
sea-orm-migration = { version = "2.0", features = ["runtime-tokio", "sqlx-mysql"] }

# IDs and time
uuid = { version = "1.19", features = ["serde", "v7"] }
Expand Down
22 changes: 11 additions & 11 deletions src/backend/services/analytics/src/domain/metric_crud.rs
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ async fn insert_graph<C: ConnectionTrait>(
let source_id = Uuid::now_v7();
let definition_id = Uuid::now_v7();

conn.execute(Statement::from_sql_and_values(
conn.execute_raw(Statement::from_sql_and_values(
conn.get_database_backend(),
"INSERT INTO metric_sources \
(id, tenant_id, source_key, source_kind, source_ref, observation_sql, origin, is_enabled, schema_status) \
Expand All @@ -491,7 +491,7 @@ async fn insert_graph<C: ConnectionTrait>(
for measure in &graph.measures {
let measure_id = Uuid::now_v7();
measure_ids.insert(measure.as_str(), measure_id);
conn.execute(Statement::from_sql_and_values(
conn.execute_raw(Statement::from_sql_and_values(
conn.get_database_backend(),
"INSERT INTO metric_source_measures (id, source_id, measure_key, is_enabled) \
VALUES (?, ?, ?, TRUE)",
Expand All @@ -508,7 +508,7 @@ async fn insert_graph<C: ConnectionTrait>(
for (order, dimension) in graph.dimensions.iter().enumerate() {
let dimension_id = Uuid::now_v7();
dimension_ids.insert(dimension.as_str(), dimension_id);
conn.execute(Statement::from_sql_and_values(
conn.execute_raw(Statement::from_sql_and_values(
conn.get_database_backend(),
"INSERT INTO metric_source_dimensions (id, source_id, dimension_key, display_order) \
VALUES (?, ?, ?, ?)",
Expand All @@ -522,7 +522,7 @@ async fn insert_graph<C: ConnectionTrait>(
.await?;
}

conn.execute(Statement::from_sql_and_values(
conn.execute_raw(Statement::from_sql_and_values(
conn.get_database_backend(),
"INSERT INTO metric_definitions \
(id, tenant_id, metric_key, label, short_label, subject, description, explanation, unit, \
Expand Down Expand Up @@ -561,7 +561,7 @@ async fn insert_graph<C: ConnectionTrait>(
input.measure_key
))
})?;
conn.execute(Statement::from_sql_and_values(
conn.execute_raw(Statement::from_sql_and_values(
conn.get_database_backend(),
"INSERT INTO metric_definition_inputs \
(id, metric_definition_id, input_role, source_measure_id) \
Expand All @@ -582,7 +582,7 @@ async fn insert_graph<C: ConnectionTrait>(
"dimension {dimension} missing from source dimensions"
))
})?;
conn.execute(Statement::from_sql_and_values(
conn.execute_raw(Statement::from_sql_and_values(
conn.get_database_backend(),
"INSERT INTO metric_definition_dimensions \
(id, metric_definition_id, source_dimension_id, display_order) \
Expand All @@ -598,7 +598,7 @@ async fn insert_graph<C: ConnectionTrait>(
}

for (order, tag) in graph.tags.iter().enumerate() {
conn.execute(Statement::from_sql_and_values(
conn.execute_raw(Statement::from_sql_and_values(
conn.get_database_backend(),
"INSERT INTO metric_definition_tags \
(id, metric_definition_id, tag, display_order) \
Expand Down Expand Up @@ -636,14 +636,14 @@ async fn delete_graph<C: ConnectionTrait>(
.await?
.map(|source| source.source_id);

conn.execute(Statement::from_sql_and_values(
conn.execute_raw(Statement::from_sql_and_values(
conn.get_database_backend(),
"DELETE FROM metric_definitions WHERE id = ?",
[uuid_value(definition_id)],
))
.await?;
if let Some(source_id) = source_id {
conn.execute(Statement::from_sql_and_values(
conn.execute_raw(Statement::from_sql_and_values(
conn.get_database_backend(),
"DELETE FROM metric_sources WHERE id = ? AND origin = 'custom'",
[uuid_value(source_id)],
Expand Down Expand Up @@ -1008,7 +1008,7 @@ async fn exists<C: ConnectionTrait>(
values: Vec<Value>,
) -> Result<bool, DbErr> {
Ok(conn
.query_one(Statement::from_sql_and_values(
.query_one_raw(Statement::from_sql_and_values(
conn.get_database_backend(),
sql,
values,
Expand All @@ -1018,7 +1018,7 @@ async fn exists<C: ConnectionTrait>(
}

fn uuid_value(id: Uuid) -> Value {
Value::Bytes(Some(Box::new(id.as_bytes().to_vec())))
Value::Bytes(Some(id.as_bytes().to_vec()))
}

fn order_value(idx: usize) -> i32 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ async fn definition_with_no_inputs_is_still_deletable() -> R {
// the definition's input rows out of band. The definition is now
// unreachable through the inputs join, but it must not become a listed,
// unremovable ghost.
db.execute(Statement::from_sql_and_values(
db.execute_raw(Statement::from_sql_and_values(
db.get_database_backend(),
"DELETE i FROM metric_definition_inputs i \
INNER JOIN metric_definitions d ON d.id = i.metric_definition_id \
Expand All @@ -351,7 +351,7 @@ async fn definition_with_no_inputs_is_still_deletable() -> R {
/// least one is guaranteed to exist.
async fn a_builtin_metric_key(db: &DatabaseConnection) -> Result<String, sea_orm::DbErr> {
let row = db
.query_one(Statement::from_string(
.query_one_raw(Statement::from_string(
db.get_database_backend(),
"SELECT metric_key FROM metric_definitions \
WHERE origin = 'builtin' AND tenant_id IS NULL LIMIT 1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

use std::collections::{BTreeMap, HashMap};

use sea_orm::{ConnectionTrait, DatabaseConnection, FromQueryResult, Statement, Value};
use sea_orm::{DatabaseConnection, FromQueryResult, Statement, Value};
use serde::Serialize;
use toolkit_canonical_errors::CanonicalError;
use uuid::Uuid;
Expand Down Expand Up @@ -270,7 +270,7 @@ async fn fetch_listing_rows(
FROM metric_definitions d \
WHERE d.tenant_id IS NULL OR d.tenant_id = ? \
ORDER BY d.metric_key",
[Value::Bytes(Some(Box::new(tenant_id.as_bytes().to_vec())))],
[Value::Bytes(Some(tenant_id.as_bytes().to_vec()))],
))
.all(db)
.await
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ async fn connect_or_skip() -> Option<DatabaseConnection> {
/// one is guaranteed to exist.
async fn a_product_metric_key(db: &DatabaseConnection) -> Result<String, sea_orm::DbErr> {
let row = db
.query_one(Statement::from_string(
.query_one_raw(Statement::from_string(
db.get_database_backend(),
"SELECT metric_key FROM metric_definitions WHERE tenant_id IS NULL LIMIT 1",
))
Expand All @@ -60,14 +60,14 @@ async fn insert_definition(
label: &str,
) -> Result<Uuid, sea_orm::DbErr> {
let id = Uuid::now_v7();
db.execute(Statement::from_sql_and_values(
db.execute_raw(Statement::from_sql_and_values(
db.get_database_backend(),
"INSERT INTO metric_definitions \
(id, tenant_id, metric_key, label, format, direction, entity_type, computation_type, origin) \
VALUES (?, ?, ?, ?, 'integer', 'higher_is_better', 'person', 'sum', 'custom')",
[
Value::Bytes(Some(Box::new(id.as_bytes().to_vec()))),
Value::Bytes(Some(Box::new(tenant.as_bytes().to_vec()))),
Value::Bytes(Some(id.as_bytes().to_vec())),
Value::Bytes(Some(tenant.as_bytes().to_vec())),
Value::from(metric_key),
Value::from(label),
],
Expand All @@ -81,10 +81,10 @@ async fn stored_last_observed(
id: Uuid,
) -> Result<Option<chrono::NaiveDate>, sea_orm::DbErr> {
let row = db
.query_one(Statement::from_sql_and_values(
.query_one_raw(Statement::from_sql_and_values(
db.get_database_backend(),
"SELECT last_observed_date FROM metric_definitions WHERE id = ?",
[Value::Bytes(Some(Box::new(id.as_bytes().to_vec())))],
[Value::Bytes(Some(id.as_bytes().to_vec()))],
))
.await?
.ok_or_else(|| sea_orm::DbErr::Custom("definition disappeared".to_owned()))?;
Expand Down Expand Up @@ -284,12 +284,12 @@ async fn invalid_evidence_reference_is_an_error_not_unchecked() -> anyhow::Resul
};
let fixture = DrilldownFixture::insert(&db, &["git.commits"], &[]).await?;
let result = async {
db.execute(Statement::from_sql_and_values(
db.execute_raw(Statement::from_sql_and_values(
db.get_database_backend(),
"UPDATE metric_sources SET evidence_ref = ? WHERE id = ?",
[
Value::from("Not A Relation"),
Value::Bytes(Some(Box::new(fixture.source_id.as_bytes().to_vec()))),
Value::Bytes(Some(fixture.source_id.as_bytes().to_vec())),
],
))
.await?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ async fn fetch_definition_rows(
);

let mut values = metric_keys.iter().map(Value::from).collect::<Vec<_>>();
values.push(Value::Bytes(Some(Box::new(tenant_id.as_bytes().to_vec()))));
values.push(Value::Bytes(Some(tenant_id.as_bytes().to_vec())));

DefinitionRow::find_by_statement(Statement::from_sql_and_values(
db.get_database_backend(),
Expand Down Expand Up @@ -279,7 +279,7 @@ async fn fetch_input_rows(
);
let values = definition_ids
.iter()
.map(|id| Value::Bytes(Some(Box::new(id.as_bytes().to_vec()))))
.map(|id| Value::Bytes(Some(id.as_bytes().to_vec())))
.collect::<Vec<_>>();

InputRow::find_by_statement(Statement::from_sql_and_values(
Expand Down Expand Up @@ -434,7 +434,7 @@ pub(super) async fn fetch_dimensions(
);
let values = definition_ids
.iter()
.map(|id| Value::Bytes(Some(Box::new(id.as_bytes().to_vec()))))
.map(|id| Value::Bytes(Some(id.as_bytes().to_vec())))
.collect::<Vec<_>>();

DimensionRow::find_by_statement(Statement::from_sql_and_values(
Expand Down Expand Up @@ -474,7 +474,7 @@ pub(super) async fn fetch_tags(
);
let values = definition_ids
.iter()
.map(|id| Value::Bytes(Some(Box::new(id.as_bytes().to_vec()))))
.map(|id| Value::Bytes(Some(id.as_bytes().to_vec())))
.collect::<Vec<_>>();

TagRow::find_by_statement(Statement::from_sql_and_values(
Expand Down Expand Up @@ -672,7 +672,7 @@ pub async fn update_evidence_status(
error_code: Option<MetricSchemaErrorCode>,
) -> Result<(), sea_orm::DbErr> {
let result = db
.execute(Statement::from_sql_and_values(
.execute_raw(Statement::from_sql_and_values(
db.get_database_backend(),
"UPDATE metric_sources \
SET evidence_schema_status = ?, \
Expand Down Expand Up @@ -709,7 +709,7 @@ pub async fn update_source_status(
error_code: Option<MetricSchemaErrorCode>,
) -> Result<(), sea_orm::DbErr> {
let result = db
.execute(Statement::from_sql_and_values(
.execute_raw(Statement::from_sql_and_values(
db.get_database_backend(),
"UPDATE metric_sources \
SET schema_status = ?, \
Expand Down Expand Up @@ -744,7 +744,7 @@ pub async fn update_definitions_for_source_status(
status: SchemaStatus,
error_code: Option<MetricSchemaErrorCode>,
) -> Result<(), sea_orm::DbErr> {
db.execute(Statement::from_sql_and_values(
db.execute_raw(Statement::from_sql_and_values(
db.get_database_backend(),
"UPDATE metric_definitions \
SET schema_status = ?, \
Expand All @@ -763,7 +763,7 @@ pub async fn update_definitions_for_source_status(
Some(code) => Value::from(code.as_db()),
None => Value::String(None),
},
Value::Bytes(Some(Box::new(source_id.as_bytes().to_vec()))),
Value::Bytes(Some(source_id.as_bytes().to_vec())),
],
))
.await?;
Expand All @@ -785,7 +785,7 @@ pub async fn update_definition_status(
Some(date) => Value::from(date.to_string()),
None => Value::String(None),
};
db.execute(Statement::from_sql_and_values(
db.execute_raw(Statement::from_sql_and_values(
db.get_database_backend(),
"UPDATE metric_definitions \
SET schema_status = ?, \
Expand Down Expand Up @@ -814,7 +814,7 @@ pub async fn update_definition_status(
}

fn uuid_value(value: Uuid) -> Value {
Value::Bytes(Some(Box::new(value.as_bytes().to_vec())))
Value::Bytes(Some(value.as_bytes().to_vec()))
}

fn unavailable(metric_key: &str) -> CanonicalError {
Expand Down
Loading
Loading