95 lines
2.8 KiB
Rust
95 lines
2.8 KiB
Rust
use sea_orm_migration::{prelude::*, schema::*};
|
|
|
|
#[derive(DeriveMigrationName)]
|
|
pub struct Migration;
|
|
|
|
#[async_trait::async_trait]
|
|
impl MigrationTrait for Migration {
|
|
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
|
|
manager
|
|
.create_table(
|
|
Table::create()
|
|
.table(NodeGroup::Table)
|
|
.if_not_exists()
|
|
.col(
|
|
uuid(NodeGroup::Id)
|
|
.extra("DEFAULT gen_random_uuid()")
|
|
.primary_key(),
|
|
)
|
|
.col(string(NodeGroup::Name))
|
|
.to_owned(),
|
|
)
|
|
.await?;
|
|
|
|
manager
|
|
.create_table(
|
|
Table::create()
|
|
.table(Node::Table)
|
|
.if_not_exists()
|
|
.col(
|
|
uuid(Node::Id)
|
|
.extra("DEFAULT gen_random_uuid()")
|
|
.primary_key(),
|
|
)
|
|
.col(string(Node::Name))
|
|
.col(boolean(Node::Status).default(false))
|
|
.col(double(Node::CoordLa))
|
|
.col(double(Node::CoordLo))
|
|
.col(float(Node::Temperature).default(-127))
|
|
.col(double(Node::BatteryMinimum).default(-127))
|
|
.col(double(Node::BatteryCurrent).default(-127))
|
|
.col(double(Node::BatteryMaximum).default(-127))
|
|
.col(double(Node::Voltage).default(-127))
|
|
.col(big_unsigned(Node::Uptime).default(0))
|
|
.col(uuid(Node::Group))
|
|
.foreign_key(
|
|
ForeignKey::create()
|
|
.name("fk-node-group_id")
|
|
.from(Node::Table, Node::Group)
|
|
.to(NodeGroup::Table, NodeGroup::Id)
|
|
.on_update(ForeignKeyAction::Cascade)
|
|
.on_delete(ForeignKeyAction::Cascade),
|
|
)
|
|
.to_owned(),
|
|
)
|
|
.await?;
|
|
|
|
Ok(())
|
|
}
|
|
|
|
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
|
|
manager
|
|
.drop_table(Table::drop().table(Node::Table).to_owned())
|
|
.await?;
|
|
|
|
manager
|
|
.drop_table(Table::drop().table(NodeGroup::Table).to_owned())
|
|
.await?;
|
|
|
|
Ok(())
|
|
}
|
|
}
|
|
|
|
#[derive(DeriveIden)]
|
|
enum Node {
|
|
Table,
|
|
Id,
|
|
Name,
|
|
Status,
|
|
CoordLa,
|
|
CoordLo,
|
|
Temperature, // def: -127
|
|
BatteryMinimum, // def: -127
|
|
BatteryCurrent, // def: -127
|
|
BatteryMaximum, // def: -127
|
|
Voltage, // def: -127
|
|
Uptime, // def: 0
|
|
Group,
|
|
}
|
|
|
|
#[derive(DeriveIden)]
|
|
enum NodeGroup {
|
|
Table,
|
|
Id,
|
|
Name,
|
|
}
|