write migration for node/nodegroup and change jwt env variable name

This commit is contained in:
Mika Bomm 2024-10-08 12:51:33 +02:00
parent 2e32e24f4a
commit 36135bb444
7 changed files with 161 additions and 2 deletions

2
.env
View file

@ -1,2 +1,2 @@
DATABASE_URL=postgres://apfel:apfel@localhost:5432/apfel
JWT_SECRET_KEY=9b2cbd156a7a7e0e530acd780fdd16e8f37fa3fd8122c74a9b7e1ce6fc67980ed0e55572be4e382679a0c13d13f0a651d15e9e877bb579e957c899eb762b1bb4
TOKEN_SECRET=9b2cbd156a7a7e0e530acd780fdd16e8f37fa3fd8122c74a9b7e1ce6fc67980ed0e55572be4e382679a0c13d13f0a651d15e9e877bb579e957c899eb762b1bb4

View file

@ -2,4 +2,6 @@
pub mod prelude;
pub mod node;
pub mod node_group;
pub mod user;

36
crates/entity/src/node.rs Normal file
View file

@ -0,0 +1,36 @@
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.0.1
use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Serialize, Deserialize)]
#[sea_orm(table_name = "node")]
pub struct Model {
#[sea_orm(primary_key, auto_increment = false)]
pub id: Uuid,
pub name: String,
pub status: bool,
#[sea_orm(column_type = "Double")]
pub coord_la: f64,
#[sea_orm(column_type = "Double")]
pub coord_lo: f64,
#[sea_orm(column_type = "Float")]
pub temperature: f32,
#[sea_orm(column_type = "Double")]
pub battery: f64,
pub uptime: i64,
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
#[sea_orm(has_many = "super::node_group::Entity")]
NodeGroup,
}
impl Related<super::node_group::Entity> for Entity {
fn to() -> RelationDef {
Relation::NodeGroup.def()
}
}
impl ActiveModelBehavior for ActiveModel {}

View file

@ -0,0 +1,33 @@
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.0.1
use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq, Serialize, Deserialize)]
#[sea_orm(table_name = "node_group")]
pub struct Model {
#[sea_orm(primary_key, auto_increment = false)]
pub id: Uuid,
pub name: String,
pub node: Uuid,
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
#[sea_orm(
belongs_to = "super::node::Entity",
from = "Column::Node",
to = "super::node::Column::Id",
on_update = "Cascade",
on_delete = "Cascade"
)]
Node,
}
impl Related<super::node::Entity> for Entity {
fn to() -> RelationDef {
Relation::Node.def()
}
}
impl ActiveModelBehavior for ActiveModel {}

View file

@ -1,3 +1,5 @@
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.0.1
pub use super::node::Entity as Node;
pub use super::node_group::Entity as NodeGroup;
pub use super::user::Entity as User;

View file

@ -1,12 +1,16 @@
pub use sea_orm_migration::prelude::*;
mod m20241008_091626_create_table_user;
mod m20241008_095058_create_table_node;
pub struct Migrator;
#[async_trait::async_trait]
impl MigratorTrait for Migrator {
fn migrations() -> Vec<Box<dyn MigrationTrait>> {
vec![Box::new(m20241008_091626_create_table_user::Migration)]
vec![
Box::new(m20241008_091626_create_table_user::Migration),
Box::new(m20241008_095058_create_table_node::Migration),
]
}
}

View file

@ -0,0 +1,82 @@
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(Node::Table)
.if_not_exists()
.col(
uuid(Node::Id)
.extra("DEFAULT gen_random_uuid()")
.primary_key(),
)
.col(string(Node::Name))
.col(boolean(Node::Status))
.col(double(Node::CoordLa))
.col(double(Node::CoordLo))
.col(float(Node::Temperature))
.col(double(Node::Battery))
.col(big_unsigned(Node::Uptime))
.to_owned(),
)
.await?;
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))
.col(uuid(NodeGroup::Node))
.foreign_key(
ForeignKey::create()
.name("fk-node_id")
.from(NodeGroup::Table, NodeGroup::Node)
.to(Node::Table, Node::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
}
}
#[derive(DeriveIden)]
enum Node {
Table,
Id,
Name, //Default mac address, kann auch geändert werden über die API
Status,
CoordLa,
CoordLo,
Temperature,
Battery, //Measured in volts
Uptime,
}
#[derive(DeriveIden)]
enum NodeGroup {
Table,
Id,
Name,
Node,
}