Separate model for GQL introspection data (#222)

This commit is contained in:
Gregory Schier
2025-06-01 06:56:00 -07:00
committed by GitHub
parent f9ac36caf0
commit af230a8f45
18 changed files with 267 additions and 60 deletions

View File

@@ -0,0 +1,55 @@
use crate::db_context::DbContext;
use crate::error::Result;
use crate::models::{GraphQlIntrospection, GraphQlIntrospectionIden};
use crate::util::UpdateSource;
use chrono::{Duration, Utc};
use sea_query::{Expr, Query, SqliteQueryBuilder};
use sea_query_rusqlite::RusqliteBinder;
impl<'a> DbContext<'a> {
pub fn get_graphql_introspection(&self, request_id: &str) -> Option<GraphQlIntrospection> {
self.find_optional(GraphQlIntrospectionIden::RequestId, request_id)
}
pub fn upsert_graphql_introspection(
&self,
workspace_id: &str,
request_id: &str,
content: Option<String>,
source: &UpdateSource,
) -> Result<GraphQlIntrospection> {
// Clean up old ones every time a new one is upserted
self.delete_expired_graphql_introspections()?;
match self.get_graphql_introspection(request_id) {
None => self.upsert(
&GraphQlIntrospection {
content,
request_id: request_id.to_string(),
workspace_id: workspace_id.to_string(),
..Default::default()
},
source,
),
Some(introspection) => self.upsert(
&GraphQlIntrospection {
content,
..introspection
},
source,
),
}
}
pub fn delete_expired_graphql_introspections(&self) -> Result<()> {
let cutoff = Utc::now().naive_utc() - Duration::days(7);
let (sql, params) = Query::delete()
.from_table(GraphQlIntrospectionIden::Table)
.cond_where(Expr::col(GraphQlIntrospectionIden::UpdatedAt).lt(cutoff))
.build_rusqlite(SqliteQueryBuilder);
let mut stmt = self.conn.resolve().prepare(sql.as_str())?;
stmt.execute(&*params.as_params())?;
Ok(())
}
}

View File

@@ -2,6 +2,7 @@ mod batch;
mod cookie_jars;
mod environments;
mod folders;
mod graphql_introspections;
mod grpc_connections;
mod grpc_events;
mod grpc_requests;