aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/api-config/src/schema/mod.rs66
-rw-r--r--lib/warden-core/src/lib.rs1
-rw-r--r--lib/warden-core/src/state/database.rs16
-rw-r--r--lib/warden-core/src/state/mod.rs28
4 files changed, 33 insertions, 78 deletions
diff --git a/lib/api-config/src/schema/mod.rs b/lib/api-config/src/schema/mod.rs
index 7c65d49..f626e87 100644
--- a/lib/api-config/src/schema/mod.rs
+++ b/lib/api-config/src/schema/mod.rs
@@ -1,12 +1,12 @@
mod create;
pub use create::CreateSchema;
+use sqlx::PgPool;
use tracing::debug;
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use std::fmt::Debug;
use time::OffsetDateTime;
-use warden_core::state::AppState;
use crate::ConfigurationError;
@@ -60,31 +60,31 @@ pub struct TransactionSchema {
pub updated_at: OffsetDateTime,
}
+pub struct SchemaService {
+ pub database: PgPool,
+}
+
#[async_trait]
-pub trait SchemaDriver {
+pub trait SchemaDriver: Send + Sync {
async fn create_schema(
&self,
- kind: impl AsRef<str> + Send + Sync + Debug,
- version: impl AsRef<str> + Send + Sync + Debug,
+ kind: &str,
+ version: &str,
schema: &serde_json::Value,
) -> Result<TransactionSchema, ConfigurationError>;
- async fn delete_schema(
- &self,
- kind: impl AsRef<str> + Send + Sync + Debug,
- version: impl AsRef<str> + Send + Sync + Debug,
- ) -> Result<(), ConfigurationError>;
+ async fn delete_schema(&self, kind: &str, version: &str) -> Result<(), ConfigurationError>;
async fn get_schema(
&self,
- kind: impl AsRef<str> + Send + Sync + Debug,
- version: impl AsRef<str> + Send + Sync + Debug,
+ kind: &str,
+ version: &str,
) -> Result<Option<TransactionSchema>, ConfigurationError>;
async fn update_schema(
&self,
- kind: impl AsRef<str> + Send + Sync + Debug,
- version: impl AsRef<str> + Send + Sync + Debug,
+ kind: &str,
+ version: &str,
schema: &serde_json::Value,
) -> Result<Option<TransactionSchema>, ConfigurationError>;
@@ -92,17 +92,17 @@ pub trait SchemaDriver {
&self,
limit: i64,
first: Option<i64>,
- after: Option<impl AsRef<str> + Send + Sync + Debug>,
+ after: Option<&str>,
) -> Result<Vec<TransactionSchema>, ConfigurationError>;
}
#[async_trait]
-impl SchemaDriver for AppState {
+impl SchemaDriver for SchemaService {
#[tracing::instrument(skip(self, schema))]
async fn create_schema(
&self,
- kind: impl AsRef<str> + Send + Sync + Debug,
- version: impl AsRef<str> + Send + Sync + Debug,
+ kind: &str,
+ version: &str,
schema: &serde_json::Value,
) -> Result<TransactionSchema, crate::ConfigurationError> {
debug!("creating transaction schema");
@@ -111,8 +111,8 @@ impl SchemaDriver for AppState {
"insert into transaction_schema (schema_type, schema_version, schema) values ($1, $2, $3)
returning *
",
- kind.as_ref(),
- version.as_ref(),
+ kind,
+ version,
sqlx::types::Json(&schema) as _
)
.fetch_one(&self.database)
@@ -123,14 +123,14 @@ impl SchemaDriver for AppState {
#[tracing::instrument(skip(self))]
async fn delete_schema(
&self,
- kind: impl AsRef<str> + Send + Sync + Debug,
- version: impl AsRef<str> + Send + Sync + Debug,
+ kind: &str,
+ version: &str,
) -> Result<(), crate::ConfigurationError> {
debug!("deleting transaction schema");
sqlx::query!(
"delete from transaction_schema where schema_type = $1 and schema_version = $2",
- kind.as_ref(),
- version.as_ref(),
+ kind,
+ version,
)
.execute(&self.database)
.await?;
@@ -140,8 +140,8 @@ impl SchemaDriver for AppState {
#[tracing::instrument(skip(self))]
async fn get_schema(
&self,
- kind: impl AsRef<str> + Send + Sync + Debug,
- version: impl AsRef<str> + Send + Sync + Debug,
+ kind: &str,
+ version: &str,
) -> Result<Option<TransactionSchema>, crate::ConfigurationError> {
debug!("getting transaction schema");
let result = sqlx::query_as!(
@@ -149,8 +149,8 @@ impl SchemaDriver for AppState {
"select
*
from transaction_schema where schema_type = $1 and schema_version = $2",
- kind.as_ref(),
- version.as_ref(),
+ kind,
+ version,
)
.fetch_optional(&self.database)
.await?;
@@ -161,8 +161,8 @@ impl SchemaDriver for AppState {
#[tracing::instrument(skip(self, schema))]
async fn update_schema(
&self,
- kind: impl AsRef<str> + Send + Sync + Debug,
- version: impl AsRef<str> + Send + Sync + Debug,
+ kind: &str,
+ version: &str,
schema: &serde_json::Value,
) -> Result<Option<TransactionSchema>, crate::ConfigurationError> {
debug!("updating transaction schema");
@@ -178,8 +178,8 @@ impl SchemaDriver for AppState {
and schema_version = $2
returning *
",
- kind.as_ref(),
- version.as_ref(),
+ kind,
+ version,
sqlx::types::Json(&schema) as _
)
.fetch_optional(&self.database)
@@ -192,7 +192,7 @@ impl SchemaDriver for AppState {
&self,
limit: i64,
first: Option<i64>,
- after: Option<impl AsRef<str> + Send + Sync + Debug>,
+ after: Option<&str>,
) -> Result<Vec<TransactionSchema>, ConfigurationError> {
debug!("getting transaction schemas");
let limit = first.unwrap_or(limit);
@@ -200,7 +200,7 @@ impl SchemaDriver for AppState {
let mut last_version = String::default();
if let Some(s) = after {
- let parts: Vec<&str> = s.as_ref().split(',').collect();
+ let parts: Vec<&str> = s.split(',').collect();
if parts.len() == 2 {
last_type = parts[0].to_string();
last_version = parts[1].to_string();
diff --git a/lib/warden-core/src/lib.rs b/lib/warden-core/src/lib.rs
index 413087b..f200ba1 100644
--- a/lib/warden-core/src/lib.rs
+++ b/lib/warden-core/src/lib.rs
@@ -1,4 +1,3 @@
mod error;
pub use error::WardenError;
pub mod config;
-pub mod state;
diff --git a/lib/warden-core/src/state/database.rs b/lib/warden-core/src/state/database.rs
deleted file mode 100644
index 4167424..0000000
--- a/lib/warden-core/src/state/database.rs
+++ /dev/null
@@ -1,16 +0,0 @@
-use sqlx::PgPool;
-use tracing::{debug, error};
-
-use crate::{WardenError, config::cli::database::Database};
-
-pub(crate) async fn connect(config: &Database) -> Result<PgPool, WardenError> {
- let url = config.get_url()?;
- let host = url.host_str();
- debug!(host = host, "connecting to database");
-
- Ok(sqlx::postgres::PgPoolOptions::new()
- .max_connections(config.database_pool_size.unwrap_or(10))
- .connect(url.as_str())
- .await
- .inspect_err(|e| error!("{e}"))?)
-}
diff --git a/lib/warden-core/src/state/mod.rs b/lib/warden-core/src/state/mod.rs
deleted file mode 100644
index 18e44b8..0000000
--- a/lib/warden-core/src/state/mod.rs
+++ /dev/null
@@ -1,28 +0,0 @@
-pub(crate) mod database;
-use sqlx::PgPool;
-use tracing::{debug, trace};
-use tracing_subscriber::EnvFilter;
-
-use crate::{WardenError, config::Configuration};
-
-pub type LogHandle = tracing_subscriber::reload::Handle<EnvFilter, tracing_subscriber::Registry>;
-
-#[derive(Debug, Clone)]
-pub struct AppState {
- pub log_handle: LogHandle,
- pub database: PgPool,
-}
-
-impl AppState {
- pub async fn new(log_handle: LogHandle, config: &Configuration) -> Result<Self, WardenError> {
- let database = database::connect(&config.database).await?;
- trace!("running database migrations");
- sqlx::migrate!("../../migrations").run(&database).await?;
- debug!("database up to date");
-
- Ok(Self {
- log_handle,
- database,
- })
- }
-}