Implement DieselConnectionLifecycleStore
This implements a LifecycleStore for a single connection
instead of a pool. These will be used by the store factories
used in StoreCommands.
@@ -18,7 +18,10 @@ mod schema;
use std::sync::{Arc, RwLock};
-use diesel::r2d2::{ConnectionManager, Pool};
+use diesel::{
+ connection::AnsiTransactionManager,
+ r2d2::{ConnectionManager, Pool},
+};
use crate::runtime::service::{
LifecycleService, LifecycleStatus, LifecycleStore, LifecycleStoreError,
@@ -175,6 +178,90 @@ impl DieselLifecycleStore<diesel::sqlite::SqliteConnection> {
}
}
+pub struct DieselConnectionLifecycleStore<'a, C>
+where
+ C: diesel::Connection<TransactionManager = AnsiTransactionManager> + 'static,
+ C::Backend: diesel::backend::UsesAnsiSavepointSyntax,
+{
+ connection: &'a C,
+}
+
+impl<'a, C> DieselConnectionLifecycleStore<'a, C>
+where
+ C: diesel::Connection<TransactionManager = AnsiTransactionManager> + 'static,
+ C::Backend: diesel::backend::UsesAnsiSavepointSyntax,
+{
+ pub fn new(connection: &'a C) -> Self {
+ DieselConnectionLifecycleStore { connection }
+ }
+}
+
+#[cfg(feature = "postgres")]
+impl<'a> LifecycleStore for DieselConnectionLifecycleStore<'a, diesel::pg::PgConnection> {
+ fn add_service(&self, service: LifecycleService) -> Result<(), LifecycleStoreError> {
+ LifecycleStoreOperations::new(self.connection).add_service(service)
+ }
+
+ fn update_service(&self, service: LifecycleService) -> Result<(), LifecycleStoreError> {
+ LifecycleStoreOperations::new(self.connection).update_service(service)
+ }
+
+ fn remove_service(
+ &self,
+ service_id: &FullyQualifiedServiceId,
+ ) -> Result<(), LifecycleStoreError> {
+ LifecycleStoreOperations::new(self.connection).remove_service(service_id)
+ }
+
+ fn get_service(
+ &self,
+ service_id: &FullyQualifiedServiceId,
+ ) -> Result<Option<LifecycleService>, LifecycleStoreError> {
+ LifecycleStoreOperations::new(self.connection).get_service(service_id)
+ }
+
+ // list services that have the provided LifecycleStatus
+ fn list_services(
+ &self,
+ status: &LifecycleStatus,
+ ) -> Result<Vec<LifecycleService>, LifecycleStoreError> {
+ LifecycleStoreOperations::new(self.connection).list_service(status)
+ }
+}
+
+#[cfg(feature = "sqlite")]
+impl<'a> LifecycleStore for DieselConnectionLifecycleStore<'a, diesel::sqlite::SqliteConnection> {
+ fn add_service(&self, service: LifecycleService) -> Result<(), LifecycleStoreError> {
+ LifecycleStoreOperations::new(self.connection).add_service(service)
+ }
+
+ fn update_service(&self, service: LifecycleService) -> Result<(), LifecycleStoreError> {
+ LifecycleStoreOperations::new(self.connection).update_service(service)
+ }
+
+ fn remove_service(
+ &self,
+ service_id: &FullyQualifiedServiceId,
+ ) -> Result<(), LifecycleStoreError> {
+ LifecycleStoreOperations::new(self.connection).remove_service(service_id)
+ }
+
+ fn get_service(
+ &self,
+ service_id: &FullyQualifiedServiceId,
+ ) -> Result<Option<LifecycleService>, LifecycleStoreError> {
+ LifecycleStoreOperations::new(self.connection).get_service(service_id)
+ }
+
+ // list services that have the provided LifecycleStatus
+ fn list_services(
+ &self,
+ status: &LifecycleStatus,
+ ) -> Result<Vec<LifecycleService>, LifecycleStoreError> {
+ LifecycleStoreOperations::new(self.connection).list_service(status)
+ }
+}
+
#[cfg(all(test, feature = "sqlite"))]
pub mod tests {
use super::*;
