Skip to content

Commit 46c051a

Browse files
committed
use specific type for database version
1 parent fb4f93d commit 46c051a

File tree

5 files changed

+134
-113
lines changed

5 files changed

+134
-113
lines changed

mithril-aggregator/src/command_args.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ fn setup_genesis_dependencies(
4242
ApplicationNodeType::Aggregator,
4343
config.get_sqlite_file(),
4444
)
45-
.check(env!("CARGO_PKG_VERSION"))?;
45+
.apply()?;
4646

4747
let chain_observer = Arc::new(
4848
mithril_common::chain_observer::CardanoCliChainObserver::new(Box::new(
@@ -309,7 +309,7 @@ impl ServeCommand {
309309
ApplicationNodeType::Aggregator,
310310
config.get_sqlite_file(),
311311
)
312-
.check(env!("CARGO_PKG_VERSION"))?;
312+
.apply()?;
313313

314314
// Init dependencies
315315
let snapshot_store = config.build_snapshot_store()?;

mithril-common/src/database/db_version.rs

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ use std::{
66
};
77

88
use chrono::NaiveDateTime;
9-
use semver::Version;
109
use sqlite::{Connection, Row, Value};
1110

1211
use crate::sqlite::{HydrationError, Projection, ProjectionField, Provider, SqLiteEntity};
1312

13+
use super::DbVersion;
14+
1415
/// Application using a database
1516
#[derive(Debug, Clone, PartialEq, Eq)]
1617
pub enum ApplicationNodeType {
@@ -41,11 +42,11 @@ impl Display for ApplicationNodeType {
4142
}
4243
}
4344

44-
/// Entity related to the `app_version` database table.
45+
/// Entity related to the `db_version` database table.
4546
#[derive(Debug, PartialEq, Eq, Clone)]
4647
pub struct DatabaseVersion {
47-
/// Semver of the database structure.
48-
pub semver: Version,
48+
/// Version of the database structure.
49+
pub version: DbVersion,
4950

5051
/// Name of the application.
5152
pub application_type: ApplicationNodeType,
@@ -58,8 +59,7 @@ pub struct DatabaseVersion {
5859
impl SqLiteEntity for DatabaseVersion {
5960
fn hydrate(row: Row) -> Result<Self, HydrationError> {
6061
Ok(Self {
61-
semver: Version::parse(&row.get::<String, _>(0))
62-
.map_err(|e| HydrationError::InvalidData(format!("{}", e)))?,
62+
version: row.get::<i64, _>(0),
6363
application_type: ApplicationNodeType::new(&row.get::<String, _>(1))
6464
.map_err(|e| HydrationError::InvalidData(format!("{}", e)))?,
6565
updated_at: NaiveDateTime::parse_from_str(
@@ -76,7 +76,7 @@ impl PartialOrd for DatabaseVersion {
7676
if self.application_type != other.application_type {
7777
None
7878
} else {
79-
self.semver.partial_cmp(&other.semver)
79+
self.version.partial_cmp(&other.version)
8080
}
8181
}
8282
}
@@ -98,13 +98,13 @@ impl Projection for DatabaseVersionProjection {
9898
impl DatabaseVersionProjection {
9999
pub fn new() -> Self {
100100
let mut projection = Self { fields: Vec::new() };
101-
projection.add_field("semver", "{:app_version:}.semver", "text");
101+
projection.add_field("version", "{:db_version:}.version", "text");
102102
projection.add_field(
103103
"application_type",
104-
"{:app_version:}.application_type",
104+
"{:db_version:}.application_type",
105105
"text",
106106
);
107-
projection.add_field("updated_at", "{:app_version:}.updated_at", "timestamp");
107+
projection.add_field("updated_at", "{:db_version:}.updated_at", "timestamp");
108108

109109
projection
110110
}
@@ -129,7 +129,7 @@ impl<'conn> DatabaseVersionProvider<'conn> {
129129
/// This code is temporary and should not last.
130130
pub fn create_table_if_not_exists(&self) -> Result<(), Box<dyn Error>> {
131131
let connection = self.get_connection();
132-
let sql = "select exists(select name from sqlite_master where type='table' and name='app_version') as table_exists";
132+
let sql = "select exists(select name from sqlite_master where type='table' and name='db_version') as table_exists";
133133
let table_exists = connection
134134
.prepare(sql)?
135135
.into_cursor()
@@ -141,7 +141,7 @@ impl<'conn> DatabaseVersionProvider<'conn> {
141141

142142
if !table_exists {
143143
let sql = r#"
144-
create table app_version (application_type text not null primary key, semver text not null, updated_at timestamp not null default CURRENT_TIMESTAMP)
144+
create table db_version (application_type text not null primary key, version integer not null, updated_at timestamp not null default CURRENT_TIMESTAMP)
145145
"#;
146146
connection.execute(sql)?;
147147
}
@@ -176,13 +176,13 @@ impl<'conn> Provider<'conn> for DatabaseVersionProvider<'conn> {
176176
fn get_definition(&self, condition: Option<&str>) -> String {
177177
let where_clause = condition.unwrap_or("true");
178178
let mut aliases = HashMap::new();
179-
let _ = aliases.insert("{:app_version:}".to_string(), "app_version".to_string());
179+
let _ = aliases.insert("{:db_version:}".to_string(), "db_version".to_string());
180180
let projection = self.get_projection().expand(aliases);
181181

182182
format!(
183183
r#"
184184
select {projection}
185-
from app_version
185+
from db_version
186186
where {where_clause}
187187
"#
188188
)
@@ -209,7 +209,7 @@ impl<'conn> DatabaseVersionUpdater<'conn> {
209209
pub fn save(&self, version: DatabaseVersion) -> Result<DatabaseVersion, Box<dyn Error>> {
210210
let params = [
211211
Value::String(format!("{}", version.application_type)),
212-
Value::String(version.semver.to_string()),
212+
Value::Integer(version.version),
213213
];
214214
let entity = self
215215
.find(None, &params)?
@@ -234,13 +234,13 @@ impl<'conn> Provider<'conn> for DatabaseVersionUpdater<'conn> {
234234
fn get_definition(&self, condition: Option<&str>) -> String {
235235
let _where_clause = condition.unwrap_or("true");
236236
let mut aliases = HashMap::new();
237-
let _ = aliases.insert("{:app_version:}".to_string(), "app_version".to_string());
237+
let _ = aliases.insert("{:db_version:}".to_string(), "db_version".to_string());
238238
let projection = self.get_projection().expand(aliases);
239239

240240
format!(
241241
r#"
242-
insert into app_version (application_type, semver) values (?, ?)
243-
on conflict (application_type) do update set semver = excluded.semver, updated_at = CURRENT_TIMESTAMP
242+
insert into db_version (application_type, version) values (?, ?)
243+
on conflict (application_type) do update set version = excluded.version, updated_at = CURRENT_TIMESTAMP
244244
returning {projection}
245245
"#
246246
)
@@ -255,10 +255,10 @@ mod tests {
255255
fn test_projection() {
256256
let projection = DatabaseVersionProjection::new();
257257
let mut aliases: HashMap<String, String> = HashMap::new();
258-
let _ = aliases.insert("{:app_version:}".to_string(), "whatever".to_string());
258+
let _ = aliases.insert("{:db_version:}".to_string(), "whatever".to_string());
259259

260260
assert_eq!(
261-
"whatever.semver as semver, whatever.application_type as application_type, whatever.updated_at as updated_at"
261+
"whatever.version as version, whatever.application_type as application_type, whatever.updated_at as updated_at"
262262
.to_string(),
263263
projection.expand(aliases)
264264
);
@@ -271,8 +271,8 @@ mod tests {
271271

272272
assert_eq!(
273273
r#"
274-
select app_version.semver as semver, app_version.application_type as application_type, app_version.updated_at as updated_at
275-
from app_version
274+
select db_version.version as version, db_version.application_type as application_type, db_version.updated_at as updated_at
275+
from db_version
276276
where true
277277
"#,
278278
provider.get_definition(None)
@@ -286,9 +286,9 @@ where true
286286

287287
assert_eq!(
288288
r#"
289-
insert into app_version (application_type, semver) values (?, ?)
290-
on conflict (application_type) do update set semver = excluded.semver, updated_at = CURRENT_TIMESTAMP
291-
returning app_version.semver as semver, app_version.application_type as application_type, app_version.updated_at as updated_at
289+
insert into db_version (application_type, version) values (?, ?)
290+
on conflict (application_type) do update set version = excluded.version, updated_at = CURRENT_TIMESTAMP
291+
returning db_version.version as version, db_version.application_type as application_type, db_version.updated_at as updated_at
292292
"#,
293293
provider.get_definition(None)
294294
)

mithril-common/src/database/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,8 @@
44
mod db_version;
55
mod version_checker;
66

7+
/// Database version.
8+
pub type DbVersion = i64;
9+
710
pub use db_version::*;
811
pub use version_checker::{DatabaseVersionChecker, SqlMigration};

0 commit comments

Comments
 (0)