Skip to content

Commit 635ba54

Browse files
authored
Merge pull request input-output-hk#1715 from input-output-hk/djo/1711/remove-connexion-from-provider
Remove connection from database provider
2 parents 8b66e45 + c3a60e6 commit 635ba54

File tree

83 files changed

+2162
-2585
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

83 files changed

+2162
-2585
lines changed

Cargo.lock

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/mithril-persistence/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "mithril-persistence"
3-
version = "0.1.13"
3+
version = "0.2.0"
44
description = "Common types, interfaces, and utilities to persist data for Mithril nodes."
55
authors = { workspace = true }
66
edition = { workspace = true }

internal/mithril-persistence/src/database/db_version.rs

+30-80
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,7 @@ use std::{
77
fmt::{Debug, Display},
88
};
99

10-
use crate::sqlite::{
11-
HydrationError, Projection, Provider, SourceAlias, SqLiteEntity, SqliteConnection,
12-
WhereCondition,
13-
};
10+
use crate::sqlite::{HydrationError, Projection, Query, SourceAlias, SqLiteEntity, WhereCondition};
1411

1512
use super::DbVersion;
1613

@@ -106,65 +103,27 @@ impl PartialOrd for DatabaseVersion {
106103
}
107104
}
108105

109-
/// Provider for the [DatabaseVersion] entities using the `DatabaseVersionProjection`.
110-
pub struct DatabaseVersionProvider<'conn> {
111-
connection: &'conn SqliteConnection,
106+
/// Query to get [DatabaseVersion] entities.
107+
pub struct GetDatabaseVersionQuery {
108+
condition: WhereCondition,
112109
}
113110

114-
impl<'conn> DatabaseVersionProvider<'conn> {
115-
/// [DatabaseVersionProvider] constructor.
116-
pub fn new(connection: &'conn SqliteConnection) -> Self {
117-
Self { connection }
118-
}
119-
120-
/// Method to create the table at the beginning of the migration procedure.
121-
/// This code is temporary and should not last.
122-
pub fn create_table_if_not_exists(
123-
&self,
124-
application_type: &ApplicationNodeType,
125-
) -> StdResult<()> {
126-
let connection = self.get_connection();
127-
let sql = "select exists(select name from sqlite_master where type='table' and name='db_version') as table_exists";
128-
let table_exists = connection
129-
.prepare(sql)?
130-
.iter()
131-
.next()
132-
.unwrap()
133-
.unwrap()
134-
.read::<i64, _>(0)
135-
== 1;
136-
137-
if !table_exists {
138-
let sql = format!("
139-
create table db_version (application_type text not null primary key, version integer not null, updated_at text not null);
140-
insert into db_version (application_type, version, updated_at) values ('{application_type}', 0, '{}');
141-
", Utc::now().to_rfc3339());
142-
connection.execute(sql)?;
143-
}
144-
145-
Ok(())
146-
}
147-
148-
/// Read the application version from the database.
149-
pub fn get_application_version(
150-
&self,
151-
application_type: &ApplicationNodeType,
152-
) -> StdResult<Option<DatabaseVersion>> {
111+
impl GetDatabaseVersionQuery {
112+
/// Query to read the application version from the database.
113+
pub fn get_application_version(application_type: &ApplicationNodeType) -> Self {
153114
let filters = WhereCondition::new(
154115
"application_type = ?*",
155116
vec![Value::String(format!("{application_type}"))],
156117
);
157-
let result = self.find(filters)?.next();
158-
159-
Ok(result)
118+
Self { condition: filters }
160119
}
161120
}
162121

163-
impl<'conn> Provider<'conn> for DatabaseVersionProvider<'conn> {
122+
impl Query for GetDatabaseVersionQuery {
164123
type Entity = DatabaseVersion;
165124

166-
fn get_connection(&'conn self) -> &SqliteConnection {
167-
self.connection
125+
fn filters(&self) -> WhereCondition {
126+
self.condition.clone()
168127
}
169128

170129
fn get_definition(&self, condition: &str) -> String {
@@ -181,20 +140,14 @@ where {condition}
181140
}
182141
}
183142

184-
/// Write [Provider] for the [DatabaseVersion] entities.
185-
/// This will perform an UPSERT and return the updated entity.
186-
pub struct DatabaseVersionUpdater<'conn> {
187-
connection: &'conn SqliteConnection,
143+
/// Query to UPSERT [DatabaseVersion] entities.
144+
pub struct UpdateDatabaseVersionQuery {
145+
condition: WhereCondition,
188146
}
189147

190-
impl<'conn> DatabaseVersionUpdater<'conn> {
191-
/// [DatabaseVersionUpdater] constructor.
192-
pub fn new(connection: &'conn SqliteConnection) -> Self {
193-
Self { connection }
194-
}
195-
196-
/// Persist the given entity and return the projection of the saved entity.
197-
pub fn save(&self, version: DatabaseVersion) -> StdResult<DatabaseVersion> {
148+
impl UpdateDatabaseVersionQuery {
149+
/// Define a query that will UPSERT the given version.
150+
pub fn one(version: DatabaseVersion) -> Self {
198151
let filters = WhereCondition::new(
199152
"",
200153
vec![
@@ -203,20 +156,16 @@ impl<'conn> DatabaseVersionUpdater<'conn> {
203156
Value::String(version.updated_at.to_rfc3339()),
204157
],
205158
);
206-
let entity = self
207-
.find(filters)?
208-
.next()
209-
.ok_or(anyhow!("No data returned after insertion"))?;
210159

211-
Ok(entity)
160+
Self { condition: filters }
212161
}
213162
}
214163

215-
impl<'conn> Provider<'conn> for DatabaseVersionUpdater<'conn> {
164+
impl Query for UpdateDatabaseVersionQuery {
216165
type Entity = DatabaseVersion;
217166

218-
fn get_connection(&'conn self) -> &SqliteConnection {
219-
self.connection
167+
fn filters(&self) -> WhereCondition {
168+
self.condition.clone()
220169
}
221170

222171
fn get_definition(&self, _condition: &str) -> String {
@@ -235,8 +184,6 @@ returning {projection}
235184

236185
#[cfg(test)]
237186
mod tests {
238-
use sqlite::Connection;
239-
240187
use super::*;
241188

242189
#[test]
@@ -253,31 +200,34 @@ mod tests {
253200

254201
#[test]
255202
fn test_definition() {
256-
let connection = Connection::open_thread_safe(":memory:").unwrap();
257-
let provider = DatabaseVersionProvider::new(&connection);
203+
let query =
204+
GetDatabaseVersionQuery::get_application_version(&ApplicationNodeType::Aggregator);
258205

259206
assert_eq!(
260207
r#"
261208
select db_version.version as version, db_version.application_type as application_type, db_version.updated_at as updated_at
262209
from db_version
263210
where true
264211
"#,
265-
provider.get_definition("true")
212+
query.get_definition("true")
266213
)
267214
}
268215

269216
#[test]
270217
fn test_updated_entity() {
271-
let connection = Connection::open_thread_safe(":memory:").unwrap();
272-
let provider = DatabaseVersionUpdater::new(&connection);
218+
let query = UpdateDatabaseVersionQuery::one(DatabaseVersion {
219+
version: 0,
220+
application_type: ApplicationNodeType::Aggregator,
221+
updated_at: Default::default(),
222+
});
273223

274224
assert_eq!(
275225
r#"
276226
insert into db_version (application_type, version, updated_at) values (?, ?, ?)
277227
on conflict (application_type) do update set version = excluded.version, updated_at = excluded.updated_at
278228
returning db_version.version as version, db_version.application_type as application_type, db_version.updated_at as updated_at
279229
"#,
280-
provider.get_definition("true")
230+
query.get_definition("true")
281231
)
282232
}
283233
}

internal/mithril-persistence/src/database/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
//! database module.
2-
//! This module contains providers and entities shared between all application types.
2+
//! This module contains queries and entities shared between all application types.
33
44
pub mod cardano_transaction_migration;
55
mod db_version;
66
mod hydrator;
7-
pub(crate) mod provider;
7+
pub(crate) mod query;
88
pub mod record;
99
pub mod repository;
1010
mod version_checker;

internal/mithril-persistence/src/database/provider/block_range_root/get_block_range_root.rs

-38
This file was deleted.

internal/mithril-persistence/src/database/provider/cardano_transaction/get_cardano_transaction.rs

-91
This file was deleted.

0 commit comments

Comments
 (0)