feat(db): device_readings 新增 metric 字段用于多行拆分存储
This commit is contained in:
@@ -11,6 +11,8 @@ pub struct Model {
|
|||||||
#[sea_orm(skip_serializing_if = "Option::is_none")]
|
#[sea_orm(skip_serializing_if = "Option::is_none")]
|
||||||
pub device_id: Option<String>,
|
pub device_id: Option<String>,
|
||||||
pub device_type: String,
|
pub device_type: String,
|
||||||
|
#[sea_orm(nullable)]
|
||||||
|
pub metric: Option<String>,
|
||||||
#[sea_orm(skip_serializing_if = "Option::is_none")]
|
#[sea_orm(skip_serializing_if = "Option::is_none")]
|
||||||
pub device_model: Option<String>,
|
pub device_model: Option<String>,
|
||||||
pub raw_value: serde_json::Value,
|
pub raw_value: serde_json::Value,
|
||||||
|
|||||||
@@ -207,6 +207,7 @@ async fn batch_insert_readings(
|
|||||||
patient_id: Set(patient_id),
|
patient_id: Set(patient_id),
|
||||||
device_id: Set(Some(device_id.to_string())),
|
device_id: Set(Some(device_id.to_string())),
|
||||||
device_type: Set(r.device_type.clone()),
|
device_type: Set(r.device_type.clone()),
|
||||||
|
metric: Set(r.values.get("metric").and_then(|v| v.as_str()).map(String::from)),
|
||||||
device_model: Set(device_model.map(String::from)),
|
device_model: Set(device_model.map(String::from)),
|
||||||
raw_value: Set(r.values.clone()),
|
raw_value: Set(r.values.clone()),
|
||||||
measured_at: Set(*measured_at),
|
measured_at: Set(*measured_at),
|
||||||
|
|||||||
@@ -91,6 +91,7 @@ mod m20260428_000088_rls_policy_strict;
|
|||||||
mod m20260428_000089_blind_indexes;
|
mod m20260428_000089_blind_indexes;
|
||||||
mod m20260428_000090_critical_alerts;
|
mod m20260428_000090_critical_alerts;
|
||||||
mod m20260428_000091_dead_letter_events;
|
mod m20260428_000091_dead_letter_events;
|
||||||
|
mod m20260429_000092_device_readings_metric;
|
||||||
|
|
||||||
pub struct Migrator;
|
pub struct Migrator;
|
||||||
|
|
||||||
@@ -189,6 +190,7 @@ impl MigratorTrait for Migrator {
|
|||||||
Box::new(m20260428_000089_blind_indexes::Migration),
|
Box::new(m20260428_000089_blind_indexes::Migration),
|
||||||
Box::new(m20260428_000090_critical_alerts::Migration),
|
Box::new(m20260428_000090_critical_alerts::Migration),
|
||||||
Box::new(m20260428_000091_dead_letter_events::Migration),
|
Box::new(m20260428_000091_dead_letter_events::Migration),
|
||||||
|
Box::new(m20260429_000092_device_readings_metric::Migration),
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,48 @@
|
|||||||
|
use sea_orm_migration::prelude::*;
|
||||||
|
|
||||||
|
#[derive(DeriveMigrationName)]
|
||||||
|
pub struct Migration;
|
||||||
|
|
||||||
|
#[async_trait::async_trait]
|
||||||
|
impl MigrationTrait for Migration {
|
||||||
|
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
|
||||||
|
manager
|
||||||
|
.alter_table(
|
||||||
|
Table::alter()
|
||||||
|
.table(Alias::new("device_readings"))
|
||||||
|
.add_column(ColumnDef::new(Alias::new("metric")).string().null())
|
||||||
|
.to_owned(),
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
manager
|
||||||
|
.create_index(
|
||||||
|
Index::create()
|
||||||
|
.name("idx_dr_metric")
|
||||||
|
.table(Alias::new("device_readings"))
|
||||||
|
.col(Alias::new("tenant_id"))
|
||||||
|
.col(Alias::new("patient_id"))
|
||||||
|
.col(Alias::new("metric"))
|
||||||
|
.col(Alias::new("measured_at"))
|
||||||
|
.to_owned(),
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
|
||||||
|
manager
|
||||||
|
.drop_index(Index::drop().name("idx_dr_metric").to_owned())
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
manager
|
||||||
|
.alter_table(
|
||||||
|
Table::alter()
|
||||||
|
.table(Alias::new("device_readings"))
|
||||||
|
.drop_column(Alias::new("metric"))
|
||||||
|
.to_owned(),
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user