From 36135bb4442cfbf2373d9245bdf5de77d4dca028 Mon Sep 17 00:00:00 2001 From: Mika Date: Tue, 8 Oct 2024 12:51:33 +0200 Subject: [PATCH] write migration for node/nodegroup and change jwt env variable name --- .env | 2 +- crates/entity/src/lib.rs | 2 + crates/entity/src/node.rs | 36 ++++++++ crates/entity/src/node_group.rs | 33 ++++++++ crates/entity/src/prelude.rs | 2 + crates/migration/src/lib.rs | 6 +- .../src/m20241008_095058_create_table_node.rs | 82 +++++++++++++++++++ 7 files changed, 161 insertions(+), 2 deletions(-) create mode 100644 crates/entity/src/node.rs create mode 100644 crates/entity/src/node_group.rs create mode 100644 crates/migration/src/m20241008_095058_create_table_node.rs diff --git a/.env b/.env index 2c7d7f1..58ded4c 100644 --- a/.env +++ b/.env @@ -1,2 +1,2 @@ DATABASE_URL=postgres://apfel:apfel@localhost:5432/apfel -JWT_SECRET_KEY=9b2cbd156a7a7e0e530acd780fdd16e8f37fa3fd8122c74a9b7e1ce6fc67980ed0e55572be4e382679a0c13d13f0a651d15e9e877bb579e957c899eb762b1bb4 \ No newline at end of file +TOKEN_SECRET=9b2cbd156a7a7e0e530acd780fdd16e8f37fa3fd8122c74a9b7e1ce6fc67980ed0e55572be4e382679a0c13d13f0a651d15e9e877bb579e957c899eb762b1bb4 \ No newline at end of file diff --git a/crates/entity/src/lib.rs b/crates/entity/src/lib.rs index ea71b96..a79fe3a 100644 --- a/crates/entity/src/lib.rs +++ b/crates/entity/src/lib.rs @@ -2,4 +2,6 @@ pub mod prelude; +pub mod node; +pub mod node_group; pub mod user; diff --git a/crates/entity/src/node.rs b/crates/entity/src/node.rs new file mode 100644 index 0000000..6eb416b --- /dev/null +++ b/crates/entity/src/node.rs @@ -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 for Entity { + fn to() -> RelationDef { + Relation::NodeGroup.def() + } +} + +impl ActiveModelBehavior for ActiveModel {} diff --git a/crates/entity/src/node_group.rs b/crates/entity/src/node_group.rs new file mode 100644 index 0000000..ff119f1 --- /dev/null +++ b/crates/entity/src/node_group.rs @@ -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 for Entity { + fn to() -> RelationDef { + Relation::Node.def() + } +} + +impl ActiveModelBehavior for ActiveModel {} diff --git a/crates/entity/src/prelude.rs b/crates/entity/src/prelude.rs index 48ab431..b3fea58 100644 --- a/crates/entity/src/prelude.rs +++ b/crates/entity/src/prelude.rs @@ -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; diff --git a/crates/migration/src/lib.rs b/crates/migration/src/lib.rs index 1646f89..9dc29f7 100644 --- a/crates/migration/src/lib.rs +++ b/crates/migration/src/lib.rs @@ -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> { - 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), + ] } } diff --git a/crates/migration/src/m20241008_095058_create_table_node.rs b/crates/migration/src/m20241008_095058_create_table_node.rs new file mode 100644 index 0000000..4d45dad --- /dev/null +++ b/crates/migration/src/m20241008_095058_create_table_node.rs @@ -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, +}