Skip to content

Commit

Permalink
editoast: add categorie to rolling stock model
Browse files Browse the repository at this point in the history
Signed-off-by: hamz2a <[email protected]>
  • Loading branch information
hamz2a committed Feb 6, 2025
1 parent 716e6c8 commit 35f70ec
Show file tree
Hide file tree
Showing 15 changed files with 253 additions and 2 deletions.
4 changes: 4 additions & 0 deletions editoast/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion editoast/diesel.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

[print_schema]
file = "editoast_models/src/tables.rs"
generate_missing_sql_type_definitions = false
filter = { except_tables = ["spatial_ref_sys"] }
import_types = ["diesel::sql_types::*", "postgis_diesel::sql_types::*"]

Expand Down
4 changes: 4 additions & 0 deletions editoast/editoast_models/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ testing = []
[dependencies]
diesel.workspace = true
diesel-async.workspace = true
editoast_common.workspace = true
editoast_derive.workspace = true
futures.workspace = true
futures-util.workspace = true
Expand All @@ -18,11 +19,14 @@ opentelemetry-semantic-conventions.workspace = true
postgis_diesel.workspace = true
postgres-openssl.workspace = true
regex.workspace = true
serde.workspace = true
strum.workspace = true
thiserror.workspace = true
tokio.workspace = true
tokio-postgres.workspace = true
tracing.workspace = true
url.workspace = true
utoipa.workspace = true

[dev-dependencies]
# The feature 'testing' is needed for all of the doc-tests
Expand Down
5 changes: 5 additions & 0 deletions editoast/editoast_models/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
pub mod db_connection_pool;
pub mod model;
pub mod rolling_stock;
pub mod tables;

pub use db_connection_pool::DbConnection;
pub use db_connection_pool::DbConnectionPoolV2;

editoast_common::schemas! {
rolling_stock::schemas(),
}

/// Generic error type to forward errors from the database
///
/// Useful for functions which only points of failure are the DB calls.
Expand Down
10 changes: 10 additions & 0 deletions editoast/editoast_models/src/rolling_stock.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
mod rolling_stock_category;
pub use rolling_stock_category::RollingStockCategory;

mod rolling_stock_categories;
pub use rolling_stock_categories::RollingStockCategories;

editoast_common::schemas! {
rolling_stock_category::schemas(),
rolling_stock_categories::schemas(),
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use serde::Deserialize;
use serde::Serialize;
use utoipa::ToSchema;

use super::RollingStockCategory;

editoast_common::schemas! {
RollingStockCategories,
}

#[derive(Clone, Debug, PartialEq, Deserialize, Serialize, ToSchema)]
pub struct RollingStockCategories(pub Vec<RollingStockCategory>);

impl From<Vec<Option<RollingStockCategory>>> for RollingStockCategories {
fn from(categories: Vec<Option<RollingStockCategory>>) -> Self {
Self(categories.into_iter().flatten().collect())
}
}
impl From<RollingStockCategories> for Vec<Option<RollingStockCategory>> {
fn from(categories: RollingStockCategories) -> Self {
categories.0.into_iter().map(Some).collect()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
use std::io::Write;
use std::str::FromStr;

use diesel::deserialize::FromSql;
use diesel::deserialize::FromSqlRow;
use diesel::expression::AsExpression;
use diesel::pg::Pg;
use diesel::pg::PgValue;
use diesel::serialize::Output;
use diesel::serialize::ToSql;
use serde::Deserialize;
use serde::Serialize;
use strum::Display;
use strum::EnumString;
use strum::IntoStaticStr;
use utoipa::ToSchema;

editoast_common::schemas! {
RollingStockCategory,
}

#[derive(
Debug,
Clone,
PartialEq,
Serialize,
Deserialize,
ToSchema,
FromSqlRow,
AsExpression,
EnumString,
IntoStaticStr,
Display,
)]
#[diesel(sql_type = crate::tables::sql_types::RollingStockCategory)]
#[strum(serialize_all = "SCREAMING_SNAKE_CASE")]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum RollingStockCategory {
Unknown,
HighSpeedTrain,
IntercityTrain,
RegionalTrainMU,
NightTrain,
CommuterTrain,
FreightTrain,
FastFreightTrain,
TramTrain,
TouristicTrain,
WorkTrain,
}

impl FromSql<crate::tables::sql_types::RollingStockCategory, Pg> for RollingStockCategory {
fn from_sql(value: PgValue) -> diesel::deserialize::Result<Self> {
let s = std::str::from_utf8(value.as_bytes()).map_err(|_| "Invalid UTF-8 data")?;
RollingStockCategory::from_str(s)
.map_err(|_| "Unrecognized enum variant for RollingStockCategory".into())
}
}

impl ToSql<crate::tables::sql_types::RollingStockCategory, Pg> for RollingStockCategory {
fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Pg>) -> diesel::serialize::Result {
let variant: &str = self.into();
out.write_all(variant.as_bytes())?;
Ok(diesel::serialize::IsNull::No)
}
}
25 changes: 25 additions & 0 deletions editoast/editoast_models/src/tables.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
// @generated automatically by Diesel CLI.

pub mod sql_types {
#[derive(diesel::sql_types::SqlType)]
#[diesel(postgres_type(name = "geometry"))]
pub struct Geometry;

#[derive(diesel::sql_types::SqlType)]
#[diesel(postgres_type(name = "rolling_stock_category"))]
pub struct RollingStockCategory;
}

diesel::table! {
use diesel::sql_types::*;
use postgis_diesel::sql_types::*;
Expand Down Expand Up @@ -102,6 +112,7 @@ diesel::table! {
diesel::table! {
use diesel::sql_types::*;
use postgis_diesel::sql_types::*;
use super::sql_types::Geometry;

infra_layer_buffer_stop (id) {
id -> Int8,
Expand All @@ -115,6 +126,7 @@ diesel::table! {
diesel::table! {
use diesel::sql_types::*;
use postgis_diesel::sql_types::*;
use super::sql_types::Geometry;

infra_layer_detector (id) {
id -> Int8,
Expand All @@ -128,6 +140,7 @@ diesel::table! {
diesel::table! {
use diesel::sql_types::*;
use postgis_diesel::sql_types::*;
use super::sql_types::Geometry;

infra_layer_electrification (id) {
id -> Int8,
Expand All @@ -141,6 +154,7 @@ diesel::table! {
diesel::table! {
use diesel::sql_types::*;
use postgis_diesel::sql_types::*;
use super::sql_types::Geometry;

infra_layer_error (id) {
id -> Int8,
Expand All @@ -155,6 +169,7 @@ diesel::table! {
diesel::table! {
use diesel::sql_types::*;
use postgis_diesel::sql_types::*;
use super::sql_types::Geometry;

infra_layer_neutral_section (id) {
id -> Int8,
Expand All @@ -168,6 +183,7 @@ diesel::table! {
diesel::table! {
use diesel::sql_types::*;
use postgis_diesel::sql_types::*;
use super::sql_types::Geometry;

infra_layer_neutral_sign (id) {
id -> Int8,
Expand All @@ -183,6 +199,7 @@ diesel::table! {
diesel::table! {
use diesel::sql_types::*;
use postgis_diesel::sql_types::*;
use super::sql_types::Geometry;

infra_layer_operational_point (id) {
id -> Int8,
Expand All @@ -198,6 +215,7 @@ diesel::table! {
diesel::table! {
use diesel::sql_types::*;
use postgis_diesel::sql_types::*;
use super::sql_types::Geometry;

infra_layer_psl_sign (id) {
id -> Int8,
Expand All @@ -213,6 +231,7 @@ diesel::table! {
diesel::table! {
use diesel::sql_types::*;
use postgis_diesel::sql_types::*;
use super::sql_types::Geometry;

infra_layer_signal (id) {
id -> Int8,
Expand All @@ -231,6 +250,7 @@ diesel::table! {
diesel::table! {
use diesel::sql_types::*;
use postgis_diesel::sql_types::*;
use super::sql_types::Geometry;

infra_layer_speed_section (id) {
id -> Int8,
Expand All @@ -244,6 +264,7 @@ diesel::table! {
diesel::table! {
use diesel::sql_types::*;
use postgis_diesel::sql_types::*;
use super::sql_types::Geometry;

infra_layer_switch (id) {
id -> Int8,
Expand All @@ -257,6 +278,7 @@ diesel::table! {
diesel::table! {
use diesel::sql_types::*;
use postgis_diesel::sql_types::*;
use super::sql_types::Geometry;

infra_layer_track_section (id) {
id -> Int8,
Expand Down Expand Up @@ -455,6 +477,7 @@ diesel::table! {
diesel::table! {
use diesel::sql_types::*;
use postgis_diesel::sql_types::*;
use super::sql_types::RollingStockCategory;

rolling_stock (id) {
id -> Int8,
Expand Down Expand Up @@ -484,6 +507,8 @@ diesel::table! {
version -> Int8,
supported_signaling_systems -> Array<Nullable<Text>>,
etcs_brake_params -> Jsonb,
primary_category -> RollingStockCategory,
other_categories -> Array<Nullable<RollingStockCategory>>,
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
ALTER TABLE rolling_stock
DROP COLUMN primary_category,
DROP COLUMN other_categories;

DROP TYPE rolling_stock_category;
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
CREATE TYPE rolling_stock_category AS ENUM (
'UNKNOWN',
'HIGH_SPEED_TRAIN',
'INTERCITY_TRAIN',
'REGIONAL_TRAIN_MU',
'NIGHT_TRAIN',
'COMMUTER_TRAIN',
'FREIGHT_TRAIN',
'FAST_FREIGHT_TRAIN',
'TRAM_TRAIN',
'TOURISTIC_TRAIN',
'WORK_TRAIN'
);

ALTER TABLE rolling_stock
ADD COLUMN primary_category rolling_stock_category NOT NULL DEFAULT 'UNKNOWN',
ADD COLUMN other_categories rolling_stock_category[] NOT NULL DEFAULT '{}';
24 changes: 24 additions & 0 deletions editoast/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9410,6 +9410,8 @@ components:
- raise_pantograph_time
- version
- supported_signaling_systems
- primary_category
- other_categories
properties:
base_power_class:
type: string
Expand Down Expand Up @@ -9466,10 +9468,14 @@ components:
nullable: true
name:
type: string
other_categories:
$ref: '#/components/schemas/RollingStockCategories'
power_restrictions:
type: object
additionalProperties:
type: string
primary_category:
$ref: '#/components/schemas/RollingStockCategory'
railjson_version:
type: string
raise_pantograph_time:
Expand All @@ -9494,6 +9500,24 @@ components:
version:
type: integer
format: int64
RollingStockCategories:
type: array
items:
$ref: '#/components/schemas/RollingStockCategory'
RollingStockCategory:
type: string
enum:
- UNKNOWN
- HIGH_SPEED_TRAIN
- INTERCITY_TRAIN
- REGIONAL_TRAIN_M_U
- NIGHT_TRAIN
- COMMUTER_TRAIN
- FREIGHT_TRAIN
- FAST_FREIGHT_TRAIN
- TRAM_TRAIN
- TOURISTIC_TRAIN
- WORK_TRAIN
RollingStockForm:
type: object
required:
Expand Down
Loading

0 comments on commit 35f70ec

Please sign in to comment.