Skip to content

Commit

Permalink
refactor: move custom sql types into own file and import it in genera…
Browse files Browse the repository at this point in the history
…ted schema.rs
  • Loading branch information
LorenzSchueler committed Jun 5, 2024
1 parent 2deb7fc commit 6824eee
Show file tree
Hide file tree
Showing 10 changed files with 173 additions and 155 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ jobs:
working-directory: sport-log-types
run: |
diesel database setup # --locked-schema TODO
git restore src/schema.rs # patch src/schema.rs src/schema.patch TODO
patch src/schema.rs src/schema.patch
- name: create sport-log-server.toml file
working-directory: sport-log-server
run: |
Expand Down Expand Up @@ -191,7 +191,7 @@ jobs:
working-directory: sport-log-types
run: |
diesel database setup # --locked-schema TODO
git restore src/schema.rs # patch src/schema.rs src/schema.patch TODO
patch src/schema.rs src/schema.patch
- name: create sport-log-server.toml file
working-directory: sport-log-server
run: |
Expand Down
2 changes: 1 addition & 1 deletion sport-log-server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use diesel_async::{
AsyncPgConnection, RunQueryDsl,
};
use diesel_migrations::{EmbeddedMigrations, HarnessWithOutput, MigrationHarness};
use sport_log_types::schema::sql_types::CUSTOM_TYPES;
use sport_log_types::custom_sql_types::CUSTOM_TYPES;
use tokio::fs;
use tracing::{error, info};
use tracing_subscriber::EnvFilter;
Expand Down
3 changes: 2 additions & 1 deletion sport-log-types/diesel.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
[print_schema]
file = "src/schema.rs"
import_types = ["diesel::sql_types::*"]
patch_file = "src/schema.patch"
generate_missing_sql_type_definitions = false
import_types = ["diesel::sql_types::*", "crate::custom_sql_types::*"]

[print_schema.filter]
except_tables = [
Expand Down
131 changes: 131 additions & 0 deletions sport-log-types/src/custom_sql_types.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
//https://github.com/diesel-rs/diesel/blob/db6730c9a79135728ec4f87fabdb745d71cb4578/diesel_derives/src/sql_type.rs
use std::sync::atomic::{AtomicU32, Ordering};

use diesel::pg::PgTypeMetadata;

pub static CUSTOM_TYPES: [&(&str, AtomicU32, AtomicU32); 6] = [
&CARDIO_TYPE,
&DISTANCE_UNIT,
&METCON_TYPE,
&MOVEMENT_DIMENSION,
&POSITION,
&WEEKDAY,
];

pub struct CardioType;

static CARDIO_TYPE: (&str, AtomicU32, AtomicU32) =
("cardio_type", AtomicU32::new(0), AtomicU32::new(0));

impl diesel::sql_types::SqlType for CardioType {
type IsNull = diesel::sql_types::is_nullable::NotNull;
}

impl diesel::sql_types::SingleValue for CardioType {}

impl diesel::sql_types::HasSqlType<CardioType> for diesel::pg::Pg {
fn metadata(_: &mut Self::MetadataLookup) -> PgTypeMetadata {
PgTypeMetadata::new(
CARDIO_TYPE.1.load(Ordering::Acquire),
CARDIO_TYPE.2.load(Ordering::Acquire),
)
}
}

pub struct DistanceUnit;

static DISTANCE_UNIT: (&str, AtomicU32, AtomicU32) =
("distance_unit", AtomicU32::new(0), AtomicU32::new(0));

impl diesel::sql_types::SqlType for DistanceUnit {
type IsNull = diesel::sql_types::is_nullable::NotNull;
}

impl diesel::sql_types::SingleValue for DistanceUnit {}

impl diesel::sql_types::HasSqlType<DistanceUnit> for diesel::pg::Pg {
fn metadata(_: &mut Self::MetadataLookup) -> PgTypeMetadata {
PgTypeMetadata::new(
DISTANCE_UNIT.1.load(Ordering::Acquire),
DISTANCE_UNIT.2.load(Ordering::Acquire),
)
}
}

pub struct MetconType;

static METCON_TYPE: (&str, AtomicU32, AtomicU32) =
("metcon_type", AtomicU32::new(0), AtomicU32::new(0));

impl diesel::sql_types::SqlType for MetconType {
type IsNull = diesel::sql_types::is_nullable::NotNull;
}

impl diesel::sql_types::SingleValue for MetconType {}

impl diesel::sql_types::HasSqlType<MetconType> for diesel::pg::Pg {
fn metadata(_: &mut Self::MetadataLookup) -> PgTypeMetadata {
PgTypeMetadata::new(
METCON_TYPE.1.load(Ordering::Acquire),
METCON_TYPE.2.load(Ordering::Acquire),
)
}
}

pub struct MovementDimension;

static MOVEMENT_DIMENSION: (&str, AtomicU32, AtomicU32) =
("movement_dimension", AtomicU32::new(0), AtomicU32::new(0));

impl diesel::sql_types::SqlType for MovementDimension {
type IsNull = diesel::sql_types::is_nullable::NotNull;
}

impl diesel::sql_types::SingleValue for MovementDimension {}

impl diesel::sql_types::HasSqlType<MovementDimension> for diesel::pg::Pg {
fn metadata(_: &mut Self::MetadataLookup) -> PgTypeMetadata {
PgTypeMetadata::new(
MOVEMENT_DIMENSION.1.load(Ordering::Acquire),
MOVEMENT_DIMENSION.2.load(Ordering::Acquire),
)
}
}

pub struct Position;

static POSITION: (&str, AtomicU32, AtomicU32) = ("position", AtomicU32::new(0), AtomicU32::new(0));

impl diesel::sql_types::SqlType for Position {
type IsNull = diesel::sql_types::is_nullable::NotNull;
}

impl diesel::sql_types::SingleValue for Position {}

impl diesel::sql_types::HasSqlType<Position> for diesel::pg::Pg {
fn metadata(_: &mut Self::MetadataLookup) -> PgTypeMetadata {
PgTypeMetadata::new(
POSITION.1.load(Ordering::Acquire),
POSITION.2.load(Ordering::Acquire),
)
}
}

pub struct Weekday;

static WEEKDAY: (&str, AtomicU32, AtomicU32) = ("weekday", AtomicU32::new(0), AtomicU32::new(0));

impl diesel::sql_types::SqlType for Weekday {
type IsNull = diesel::sql_types::is_nullable::NotNull;
}

impl diesel::sql_types::SingleValue for Weekday {}

impl diesel::sql_types::HasSqlType<Weekday> for diesel::pg::Pg {
fn metadata(_: &mut Self::MetadataLookup) -> PgTypeMetadata {
PgTypeMetadata::new(
WEEKDAY.1.load(Ordering::Acquire),
WEEKDAY.2.load(Ordering::Acquire),
)
}
}
2 changes: 2 additions & 0 deletions sport-log-types/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#[cfg(feature = "db")]
pub mod custom_sql_types;
#[cfg(feature = "db")]
pub mod schema;
mod types;
pub use types::*;
Loading

0 comments on commit 6824eee

Please sign in to comment.