-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor database code around transactions
- Force usage of database transactions for all operations around models. - Add atmospheric data ingest (but no egest? yet) - Update most dependencies
- Loading branch information
Showing
57 changed files
with
2,652 additions
and
1,672 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,31 @@ | ||
[package] | ||
name = "ganymede" | ||
version = "2.0.0" | ||
authors = ["David Bourgault <[email protected]>"] | ||
version = "3.0.0" | ||
authors = ["David Bourgault <[email protected]"] | ||
edition = "2021" | ||
|
||
[dependencies] | ||
async-trait = "0.1.68" | ||
chrono = "0.4.26" | ||
chrono-tz = "0.8.2" | ||
env_logger = "0.10.0" | ||
futures = "0.3.28" | ||
jsonwebtoken = { version = "8.3.0", features = ["use_pem"] } | ||
log = "0.4.19" | ||
prost = "0.11.9" | ||
prost-types = "0.11.9" | ||
regex = "1.8.4" | ||
serde = "1.0.164" | ||
serde_json = "1.0.96" | ||
sqlx = { version = "0.6.3", features = ["runtime-tokio-rustls", "postgres", "uuid", "json"] } | ||
time = "0.3.26" | ||
tokio = { version = "1.28.2", features = ["macros", "rt-multi-thread"] } | ||
toml = "0.5.8" | ||
tonic = "0.9.2" | ||
tonic-reflection = "0.9.2" | ||
uuid = { version = "1.3.4", features = ["v4", "macro-diagnostics"] } | ||
|
||
[build-dependencies] | ||
tonic-build = "0.9.2" | ||
chrono = "0.4.38" | ||
chrono-tz = "0.9.0" | ||
clap = { version = "4.5.17", features = ["derive"] } | ||
env_logger = "0.11.5" | ||
futures-core = "0.3.30" | ||
jsonwebtoken = { version = "9.3.0", features = ["use_pem"] } | ||
log = "0.4.22" | ||
prost = "0.13.2" | ||
prost-types = "0.13.2" | ||
serde = "1.0.209" | ||
serde_json = "1.0.128" | ||
sqlx = { version = "0.8.2", features = ["postgres", "uuid", "json", "chrono", "runtime-tokio-rustls"] } | ||
tokio = { version = "1.40.0", features = ["macros", "rt-multi-thread"] } | ||
toml = "0.8.19" | ||
tonic = "0.12.2" | ||
tonic-reflection = "0.12.2" | ||
uuid = { version = "1.10.0", features = ["v4"] } | ||
|
||
[[bin]] | ||
name = "ganymede" | ||
path = "src/main.rs" | ||
|
||
[build-dependencies] | ||
tonic-build = "0.12.2" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
INSERT INTO config ( | ||
config_id, | ||
domain_id, | ||
display_name, | ||
poll_period, | ||
light_config | ||
) VALUES ( | ||
'00000000-0000-0000-0000-000000000000'::UUID, | ||
'00000000-0000-0000-0000-000000000000'::UUID, | ||
'Test Config', | ||
'1H'::INTERVAL, | ||
'{}'::JSONB | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
INSERT INTO device ( | ||
device_id, | ||
domain_id, | ||
display_name, | ||
mac, | ||
config_id, | ||
description, | ||
timezone, | ||
last_poll | ||
) VALUES ( | ||
'00000000-0000-0000-0000-000000000000'::UUID, | ||
'00000000-0000-0000-0000-000000000000'::UUID, | ||
'Test Display', | ||
'00:00:00:00:00:00', | ||
'00000000-0000-0000-0000-000000000000'::UUID, | ||
'This describes a device', | ||
'America/Montreal', | ||
NULL | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
INSERT INTO domain( | ||
domain_id, | ||
name, | ||
display_name | ||
) VALUES ( | ||
'00000000-0000-0000-0000-000000000000'::UUID, | ||
'test-domain', | ||
'Test Domain' | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
ALTER TABLE device ADD COLUMN last_poll TIMESTAMP WITH TIME ZONE; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
CREATE EXTENSION IF NOT EXISTS timescaledb; | ||
|
||
CREATE TABLE "atmosphere" ( | ||
observed_on TIMESTAMP WITH TIME ZONE DEFAULT (NOW() AT TIME ZONE 'UTC') NOT NULL, | ||
domain_id UUID NOT NULL, | ||
device_id UUID NOT NULL, | ||
temperature FLOAT, | ||
relative_humidity FLOAT, | ||
|
||
FOREIGN KEY (domain_id) REFERENCES domain(domain_id) ON DELETE CASCADE, | ||
FOREIGN KEY (device_id) REFERENCES device(device_id) ON DELETE CASCADE | ||
); | ||
SELECT create_hypertable('atmosphere', by_range('observed_on')); | ||
|
||
CREATE TABLE "soil" ( | ||
observed_on TIMESTAMP WITH TIME ZONE DEFAULT (NOW() AT TIME ZONE 'UTC') NOT NULL, | ||
domain_id UUID NOT NULL, | ||
device_id UUID NOT NULL, | ||
temperature FLOAT, | ||
humidity FLOAT, | ||
|
||
FOREIGN KEY (domain_id) REFERENCES domain(domain_id) ON DELETE CASCADE, | ||
FOREIGN KEY (device_id) REFERENCES device(device_id) ON DELETE CASCADE | ||
); | ||
SELECT create_hypertable('soil', by_range('observed_on')); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
ALTER TABLE config ADD COLUMN poll_period INTERVAL NOT NULL DEFAULT '1H'; |
1 change: 1 addition & 0 deletions
1
migrations/20240907005328_Rename_device_mac_unique_constraint.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
ALTER TABLE device RENAME CONSTRAINT device_mac_key TO device_mac_unique; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
max_width = 120 | ||
chain_width = 120 |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
use std::sync::Arc; | ||
|
||
use crate::{Error, Result}; | ||
|
||
use super::transaction::DomainDatabaseTransaction; | ||
|
||
#[derive(Clone)] | ||
pub struct Database { | ||
connection_pool: Arc<sqlx::Pool<sqlx::Postgres>>, | ||
} | ||
|
||
impl Database { | ||
pub async fn try_from_uri(uri: &str) -> Result<Self> { | ||
let pool = sqlx::postgres::PgPoolOptions::new().max_connections(5).connect(uri).await.map_err(|err| { | ||
log::error!("Failed to connect to postgres: {err}"); | ||
Error::DatabaseError(err.to_string()) | ||
})?; | ||
|
||
Ok(Database::new(pool)) | ||
} | ||
|
||
pub fn new(connection_pool: sqlx::Pool<sqlx::Postgres>) -> Self { | ||
Database { | ||
connection_pool: Arc::new(connection_pool), | ||
} | ||
} | ||
|
||
pub fn for_domain(&self, domain_id: uuid::Uuid) -> DomainDatabase { | ||
DomainDatabase::new(self.connection_pool.clone(), domain_id) | ||
} | ||
} | ||
|
||
pub struct DomainDatabase { | ||
connection_pool: Arc<sqlx::Pool<sqlx::Postgres>>, | ||
domain_id: uuid::Uuid, | ||
} | ||
|
||
impl DomainDatabase { | ||
pub fn new(connection_pool: Arc<sqlx::Pool<sqlx::Postgres>>, domain_id: uuid::Uuid) -> Self { | ||
DomainDatabase { | ||
connection_pool, | ||
domain_id, | ||
} | ||
} | ||
|
||
pub async fn begin(&self) -> Result<DomainDatabaseTransaction> { | ||
match self.connection_pool.begin().await { | ||
Ok(tx) => Ok(DomainDatabaseTransaction::new(tx, self.domain_id.clone())), | ||
Err(err) => Err(Error::DatabaseError(err.to_string())), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
pub mod database; | ||
pub mod models; | ||
pub mod transaction; | ||
|
||
pub type Database = database::Database; | ||
pub type DomainDatabaseTransaction = transaction::DomainDatabaseTransaction; |
3 changes: 1 addition & 2 deletions
3
src/device/device/mod.rs → src/database/models/atmosphere_data/mod.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
pub mod errors; | ||
pub mod model; | ||
pub mod operations; | ||
pub mod protobuf; | ||
pub mod tonic; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
use chrono::{DateTime, Utc}; | ||
use sqlx::FromRow; | ||
|
||
use crate::types::{Celsius, RelativeHumidity}; | ||
|
||
#[derive(Debug, Clone, PartialEq, FromRow)] | ||
pub struct AtmosphereDataModel { | ||
// Timestamp for data measurement | ||
pub observed_on: DateTime<Utc>, | ||
|
||
// UUID of the device that produced the measurement | ||
pub device_id: uuid::Uuid, | ||
|
||
// Air temperature | ||
pub temperature: Celsius, | ||
|
||
// Air humidity (0-1) | ||
pub relative_humidity: RelativeHumidity, | ||
} |
Oops, something went wrong.