From 926b5d271cb86d771b0816d7bdc26948eccd0d20 Mon Sep 17 00:00:00 2001 From: Felipe Diniello Date: Mon, 19 Jun 2023 18:42:56 +0200 Subject: [PATCH] Include Antenna model diesel macros --- kairo-common/Cargo.toml | 2 ++ kairo-common/diesel.toml | 9 +++++ kairo-common/migrations/.keep | 0 .../down.sql | 6 ++++ .../up.sql | 36 +++++++++++++++++++ .../down.sql | 3 ++ .../2023-06-18-164844_create_antennas/up.sql | 10 ++++++ kairo-common/src/lib.rs | 9 +++-- kairo-common/src/models/antenna.rs | 15 +++++++- kairo-common/src/postgres.rs | 10 ++++++ kairo-common/src/schema.rs | 13 +++++++ 11 files changed, 110 insertions(+), 3 deletions(-) create mode 100644 kairo-common/diesel.toml create mode 100644 kairo-common/migrations/.keep create mode 100644 kairo-common/migrations/00000000000000_diesel_initial_setup/down.sql create mode 100644 kairo-common/migrations/00000000000000_diesel_initial_setup/up.sql create mode 100644 kairo-common/migrations/2023-06-18-164844_create_antennas/down.sql create mode 100644 kairo-common/migrations/2023-06-18-164844_create_antennas/up.sql create mode 100644 kairo-common/src/postgres.rs create mode 100644 kairo-common/src/schema.rs diff --git a/kairo-common/Cargo.toml b/kairo-common/Cargo.toml index 020e03a..213b17c 100644 --- a/kairo-common/Cargo.toml +++ b/kairo-common/Cargo.toml @@ -20,3 +20,5 @@ influxdb2-structmap = "0.2" influxdb2-derive = "0.1.1" futures = "0.3.28" num-traits = "0.2" +diesel = { version = "2.1.0", features = ["postgres"] } +diesel-async = { version = "0.3.1", features = ["postgres"] } diff --git a/kairo-common/diesel.toml b/kairo-common/diesel.toml new file mode 100644 index 0000000..c028f4a --- /dev/null +++ b/kairo-common/diesel.toml @@ -0,0 +1,9 @@ +# For documentation on how to configure this file, +# see https://diesel.rs/guides/configuring-diesel-cli + +[print_schema] +file = "src/schema.rs" +custom_type_derives = ["diesel::query_builder::QueryId"] + +[migrations_directory] +dir = "migrations" diff --git a/kairo-common/migrations/.keep b/kairo-common/migrations/.keep new file mode 100644 index 0000000..e69de29 diff --git a/kairo-common/migrations/00000000000000_diesel_initial_setup/down.sql b/kairo-common/migrations/00000000000000_diesel_initial_setup/down.sql new file mode 100644 index 0000000..a9f5260 --- /dev/null +++ b/kairo-common/migrations/00000000000000_diesel_initial_setup/down.sql @@ -0,0 +1,6 @@ +-- This file was automatically created by Diesel to setup helper functions +-- and other internal bookkeeping. This file is safe to edit, any future +-- changes will be added to existing projects as new migrations. + +DROP FUNCTION IF EXISTS diesel_manage_updated_at(_tbl regclass); +DROP FUNCTION IF EXISTS diesel_set_updated_at(); diff --git a/kairo-common/migrations/00000000000000_diesel_initial_setup/up.sql b/kairo-common/migrations/00000000000000_diesel_initial_setup/up.sql new file mode 100644 index 0000000..d68895b --- /dev/null +++ b/kairo-common/migrations/00000000000000_diesel_initial_setup/up.sql @@ -0,0 +1,36 @@ +-- This file was automatically created by Diesel to setup helper functions +-- and other internal bookkeeping. This file is safe to edit, any future +-- changes will be added to existing projects as new migrations. + + + + +-- Sets up a trigger for the given table to automatically set a column called +-- `updated_at` whenever the row is modified (unless `updated_at` was included +-- in the modified columns) +-- +-- # Example +-- +-- ```sql +-- CREATE TABLE users (id SERIAL PRIMARY KEY, updated_at TIMESTAMP NOT NULL DEFAULT NOW()); +-- +-- SELECT diesel_manage_updated_at('users'); +-- ``` +CREATE OR REPLACE FUNCTION diesel_manage_updated_at(_tbl regclass) RETURNS VOID AS $$ +BEGIN + EXECUTE format('CREATE TRIGGER set_updated_at BEFORE UPDATE ON %s + FOR EACH ROW EXECUTE PROCEDURE diesel_set_updated_at()', _tbl); +END; +$$ LANGUAGE plpgsql; + +CREATE OR REPLACE FUNCTION diesel_set_updated_at() RETURNS trigger AS $$ +BEGIN + IF ( + NEW IS DISTINCT FROM OLD AND + NEW.updated_at IS NOT DISTINCT FROM OLD.updated_at + ) THEN + NEW.updated_at := current_timestamp; + END IF; + RETURN NEW; +END; +$$ LANGUAGE plpgsql; diff --git a/kairo-common/migrations/2023-06-18-164844_create_antennas/down.sql b/kairo-common/migrations/2023-06-18-164844_create_antennas/down.sql new file mode 100644 index 0000000..8e1248e --- /dev/null +++ b/kairo-common/migrations/2023-06-18-164844_create_antennas/down.sql @@ -0,0 +1,3 @@ +-- This file should undo anything in `up.sql` + +DROP TABLE antennas; \ No newline at end of file diff --git a/kairo-common/migrations/2023-06-18-164844_create_antennas/up.sql b/kairo-common/migrations/2023-06-18-164844_create_antennas/up.sql new file mode 100644 index 0000000..f5902a1 --- /dev/null +++ b/kairo-common/migrations/2023-06-18-164844_create_antennas/up.sql @@ -0,0 +1,10 @@ +-- Your SQL goes here + +CREATE TABLE antennas ( + id VARCHAR(17) PRIMARY KEY, + tssi DOUBLE PRECISION, + pos_x DOUBLE PRECISION, + pos_y DOUBLE PRECISION, + pos_z DOUBLE PRECISION, + comment TEXT +); \ No newline at end of file diff --git a/kairo-common/src/lib.rs b/kairo-common/src/lib.rs index 13f254b..2419d2b 100644 --- a/kairo-common/src/lib.rs +++ b/kairo-common/src/lib.rs @@ -4,6 +4,7 @@ #![allow(confusable_idents)] pub mod influx; +pub mod postgres; // random functions for mqtt pub mod mqtt; @@ -18,7 +19,8 @@ mod types { pub type Point = types::point::Point; pub type MAC = types::mac::MAC; - +/// DB models: for SQL with Diesel and InfluxDB and influxdb-derive +mod schema; mod models { pub mod antenna; pub mod beacon_measure; @@ -30,8 +32,11 @@ mod models { pub data: Vec, } } -pub type Antenna = models::antenna::Antenna; pub type DeviceReport = models::DeviceReport; + +pub type Antenna = models::antenna::Antenna; +pub type NewAntenna<'a> = models::antenna::NewAntenna<'a>; + pub type KnownPosition = models::known_position::KnownPosition; pub type DynamicDeviceStatus = models::dynamic_device_status::DynamicDeviceStatus; pub type BeaconMeasure = models::beacon_measure::BeaconMeasure; diff --git a/kairo-common/src/models/antenna.rs b/kairo-common/src/models/antenna.rs index 9d8faf8..9786b91 100644 --- a/kairo-common/src/models/antenna.rs +++ b/kairo-common/src/models/antenna.rs @@ -1,8 +1,10 @@ use std::f64::consts::PI; +use diesel::prelude::*; use crate::{unit_conversion::UnitsConversion, Point}; -#[derive(Debug, Clone, Default)] +#[derive(Debug, Clone, Default,Queryable, Selectable)] +#[diesel(table_name = crate::schema::antennas)] pub struct Antenna { pub id: String, pub tssi: f64, @@ -12,6 +14,17 @@ pub struct Antenna { pos_z: f64, } +#[derive(Insertable)] +#[diesel(table_name = crate::schema::antennas)] +pub struct NewAntenna<'a> { + id: &'a str, + comment: Option<&'a str>, + tssi: f64, + pos_x: f64, + pos_y: f64, + pos_z: f64, +} + impl Antenna { const C: f64 = 2.99e8; const F: f64 = 2.4e9; diff --git a/kairo-common/src/postgres.rs b/kairo-common/src/postgres.rs new file mode 100644 index 0000000..1925672 --- /dev/null +++ b/kairo-common/src/postgres.rs @@ -0,0 +1,10 @@ +pub mod for_sync { + use diesel::pg::PgConnection; + use diesel::prelude::*; + + pub fn establish_connection() -> PgConnection { + let database_url = dotenv::var("DATABASE_URL").expect("DATABASE_URL must be set"); + PgConnection::establish(&database_url) + .unwrap_or_else(|_| panic!("Error connecting to {}", database_url)) + } +} diff --git a/kairo-common/src/schema.rs b/kairo-common/src/schema.rs new file mode 100644 index 0000000..17c9565 --- /dev/null +++ b/kairo-common/src/schema.rs @@ -0,0 +1,13 @@ +// @generated automatically by Diesel CLI. + +diesel::table! { + antennas (id) { + #[max_length = 17] + id -> Varchar, + tssi -> Nullable, + pos_x -> Nullable, + pos_y -> Nullable, + pos_z -> Nullable, + comment -> Nullable, + } +}